A lot of small fixes and cleanups not really related.
Pull-Request: #768
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
do
add """
<!DOCTYPE html>
+<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
import test_shared_preferences
import test_assets_and_resources
import test_target_api
+import test_data_store
import test_intent
img = load_image( "fighter.png" )
end
- var r: Float = 0.0
- var g: Float = 0.0
- var b: Float = 0.0
+ var r = 0.0 is writable
+ var g = 0.0 is writable
+ var b = 0.0 is writable
redef fun frame_core( display )
do
b = b + 0.01
import simple
import mnit_linux
+import test_data_store
+
super
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Test for the shared_preferences module of App.nit framework
+module test_data_store
+
+import simple
+import app::data_store
+
+redef class App
+ redef fun window_created
+ do
+ super
+
+ # load colors from data store
+ var r = data_store["r"]
+ if r isa Float then
+ self.r = r
+ print "r {r}"
+ else assert r == null
+
+ var g = data_store["g"]
+ if g isa Float then
+ self.g = g
+ print "g {g}"
+ else assert g == null
+ end
+
+ redef fun input(ie)
+ do
+ if ie isa PointerEvent then
+ # save color for next execution
+ data_store["r"] = r
+ data_store["g"] = g
+ end
+
+ return super
+ end
+end
import native_app_glue
import dalvik
private import log
+private import android_data_store
# Uses Android logs to print everything
redef fun print(text) do log_write(priority_info, app.log_prefix.to_cstring, text.to_s.to_cstring)
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Implements `app::data_store` using `shared_preferences`
+#
+# We use the shared preferences named "data_store" to store the data.
+module android_data_store
+
+import app::data_store
+private import shared_preferences
+
+redef class App
+ redef var data_store = new SharedPreferenceView
+end
+
+private class SharedPreferenceView
+ super DataStore
+
+ # The `SharedPreferences` used to implement the `DataStore`
+ var shared_preferences = new SharedPreferences.privately(app, "data_store") is lazy
+
+ redef fun [](key) do return shared_preferences[key]
+
+ redef fun []=(key, value) do shared_preferences[key] = value
+end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# app.nit is a framework to create cross-platform applications
+#
+# The features offered by this modules are common to all platforms, but
+# may not be available on all devices.
+module app
+
+import app_base
# See the License for the specific language governing permissions and
# limitations under the License.
-# app.nit is a framework to create cross-platform applications
-#
-# The features offered by this modules are common to all platforms, but
-# may not be available on all devices.
-module app is
+module app_base is
new_annotation app_name
new_annotation app_version
end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Simple data storage services
+#
+# The implementation varies per platform.
+module data_store
+
+import app_base
+import serialization
+
+redef class App
+ # Services to store and load data
+ fun data_store: DataStore is abstract
+end
+
+# Simple data storage facility
+interface DataStore
+
+ # Get the object stored at `key`, or null if nothing is available
+ fun [](key: String): nullable Object is abstract
+
+ # Store `value` at `key`
+ fun []=(key: String, value: Serializable) is abstract
+end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Implementation of app.nit for the Linux platform
+module linux
+
+import app
+private import linux_data_store
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module linux_data_store
+
+import app::data_store
+private import xdg_basedir
+private import sqlite3
+private import json_serialization
+
+redef class App
+ redef var data_store = new LinuxStore
+end
+
+private class LinuxStore
+ super DataStore
+
+ # File path of the Sqlite3 DB file
+ fun db_file: String do return "data_store.db"
+
+ # Sqlite3 table used
+ fun db_table: String do return "data_store"
+
+ private var db_cache: nullable Sqlite3DB = null
+
+ # Database to use to implement the `DataStore`
+ fun db: nullable Sqlite3DB
+ do
+ var db = db_cache
+ if db != null then return db
+
+ # Find DB path
+ var config_home = xdg_basedir.config_home.to_s
+ var config_dir = config_home.join_path(sys.program_name.basename(""))
+ if not config_dir.file_exists then config_dir.mkdir
+ var db_path = config_dir.join_path(db_file)
+
+ # Open DB connection
+ db = new Sqlite3DB.open(db_path)
+ if not db.is_open then
+ app.log_error "Data store unavaible, cannot load/save data. (at '{db_path}' with '{db.error or else "unknown"}')"
+ return null
+ end
+
+ # Create DB table
+ db.create_table "IF NOT EXISTS {db_table} (key TEXT PRIMARY KEY, value TEXT)"
+
+ db_cache = db
+ return db
+ end
+
+ redef fun [](key)
+ do
+ # Get DB
+ var db = self.db
+ if db == null then return null
+
+ # Prepare SELECT statement
+ var stmt = db.select("* FROM {db_table} WHERE key == {key.to_sql_string}")
+
+ # Execute statment
+ for row in stmt do
+ # Get from statement
+ var serialized = row[1].to_s
+ stmt.close
+
+ # Deserialize
+ var deserializer = new JsonDeserializer(serialized)
+ var deserialized = deserializer.deserialize
+
+ return deserialized
+ end
+
+ stmt.close
+ return null
+ end
+
+ redef fun []=(key, value)
+ do
+ # Get DB
+ var db = self.db
+ if db == null then return
+
+ # Serialize
+ var stream = new StringOStream
+ var serializer = new JsonSerializer(stream)
+ serializer.serialize value
+ var serialized = stream.to_s
+
+ # Save in DB
+ db.execute "BEGIN TRANSACTION"
+ db.insert "OR REPLACE INTO {db_table} VALUES({key.to_sql_string}, {serialized.to_sql_string})"
+ db.execute "COMMIT"
+ end
+end
import mnit
import sdl
import linux_opengles1
+import linux
in "C" `{
#include <EGL/egl.h>
private var native: nullable NativeRegex = null
# Cache of a single `regmatch_t` to prevent many calls to `malloc`
- private var native_match = new NativeMatchArray.malloc(0) is lazy
+ private var native_match = new NativeMatchArray.malloc(1) is lazy
# `cflags` of the last successful `compile`
private var cflags_cache = 0
init_inherit
init_linext
+android
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.