import dalvik
import serialization
-private import json::serialization
+private import json
in "Java" `{
import android.content.SharedPreferences;
fun get_all: HashMap[JavaString, JavaObject] import HashMap[JavaString, JavaObject],
HashMap[JavaString, JavaObject].[]= in "Java" `{
Map<String, ?> java_map = null;
- int nit_hashmap = new_HashMap_of_JavaString_JavaObject();
+ nit.app.NitObject nit_hashmap = new_HashMap_of_JavaString_JavaObject();
try {
java_map = self.getAll();
} catch (NullPointerException e) {
# Provides services to save and load data for the android platform
class SharedPreferences
- protected var context: NativeActivity
protected var shared_preferences: NativeSharedPreferences
protected var editor: NativeSharedPreferencesEditor
protected init(app: App, file_name: String, mode: Int)
do
- self.context = app.native_activity
sys.jni_env.push_local_frame(1)
- setup(file_name.to_java_string, mode)
+ setup(file_name.to_java_string, mode, app.native_context)
sys.jni_env.pop_local_frame
end
self.editor = editor.new_global_ref
end
- private fun setup(file_name: JavaString, mode: Int) import context, set_vars in "Java" `{
- Activity context = (Activity) SharedPreferences_context(self);
+ private fun setup(file_name: JavaString, mode: Int, context: NativeContext) import set_vars in "Java" `{
SharedPreferences sp;
// Uses default SharedPreferences if file_name is an empty String
if (file_name.equals("")) {
- sp = context.getPreferences((int)mode);
+ Activity activity = (Activity)context;
+ sp = activity.getPreferences((int)mode);
} else {
sp = context.getSharedPreferences(file_name, (int)mode);
}
# Retrieve an `Object` stored via `[]=` function
#
# Returns `null` if there's no serialized object corresponding to the given key
- # Make sure that the serialized object is `auto_serializable` or that it redefines
+ # Make sure that the serialized object is `serialize` or that it redefines
# the appropriate methods. Refer to `Serializable` documentation for further details
fun [](key: String): nullable Object
do
if serialized_string == "" then return null
var deserializer = new JsonDeserializer(serialized_string)
- return deserializer.deserialize
+ var deserialized = deserializer.deserialize
+
+ var errors = deserializer.errors
+ if errors.not_empty then
+ # An update may have broken the versioning compatibility
+ print_error "{class_name} error at deserialization: {errors.join(", ")}"
+ return null # Let's be safe
+ end
+
+ return deserialized
end
end
redef class App
var shared_preferences: SharedPreferences is lazy do
- return new SharedPreferences.privately(self, "")
+ return new SharedPreferences.privately(self, "app.nit")
end
end