6 package com.google.appinventor.components.runtime;
21 import java.util.ArrayList;
22 import java.util.List;
25 import android.content.Context;
26 import android.content.SharedPreferences;
28 import org.json.JSONException;
53 @DesignerComponent(version = YaVersion.TINYDB_COMPONENT_VERSION,
54 description =
"TinyDB is a non-visible component that stores data for an app. " +
55 "<p> Apps created with App Inventor are initialized each time they run: " +
56 "If an app sets the value of a variable and the user then quits the app, " +
57 "the value of that variable will not be remembered the next time the app is run. " +
58 "In contrast, TinyDB is a <em> persistent </em> data store for the app, " +
59 "that is, the data stored there will be available each time the app is " +
60 "run. An example might be a game that saves the high score and " +
61 "retrieves it each time the game is played. </<p> " +
62 "<p> Data items are strings stored under <em>tags</em> . To store a data " +
63 "item, you specify the tag it should be stored under. Subsequently, you " +
64 "can retrieve the data that was stored under a given tag. </p>" +
65 "<p> There is only one data store per app. Even if you have multiple TinyDB " +
66 "components, they will use the same data store. To get the effect of " +
67 "separate stores, use different keys. Also each app has its own data " +
68 "store. You cannot use TinyDB to pass data between two different apps on " +
69 "the phone, although you <em>can</em> use TinyDb to shares data between the " +
70 "different screens of a multi-screen app. </p> " +
71 "<p>When you are developing apps using the AI Companion, all the apps " +
72 "using that companion will share the same TinyDb. That sharing will disappear " +
73 "once the apps are packaged. But, during development, you should be careful to clear " +
74 "the TinyDb each time you start working on a new app.</p>",
75 category = ComponentCategory.STORAGE,
77 iconName =
"images/tinyDB.png")
84 private SharedPreferences sharedPreferences;
85 private String
namespace;
87 private Context context;
96 super(container.
$form());
97 context = (Context) container.
$context();
115 this.
namespace = namespace;
116 sharedPreferences = context.getSharedPreferences(
namespace, Context.MODE_PRIVATE);
132 @
SimpleFunction(description =
"Store the given value under the given tag. The storage persists "
133 +
"on the phone when the app is restarted.")
134 public
void StoreValue(final String tag, final Object valueToStore) {
135 final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
138 sharedPrefsEditor.commit();
139 }
catch (JSONException e) {
140 throw new YailRuntimeError(
"Value failed to convert to JSON.",
"JSON Creation Error.");
153 @
SimpleFunction(description =
"Retrieve the value stored under the given tag. If there's no "
154 +
"such tag, then return valueIfTagNotThere.")
155 public Object
GetValue(final String tag, final Object valueIfTagNotThere) {
157 String value = sharedPreferences.getString(tag,
"");
161 }
catch (JSONException e) {
162 throw new YailRuntimeError(
"Value failed to convert from JSON.",
"JSON Creation Error.");
171 @
SimpleFunction(description =
"Return a list of all the tags in the data store.")
173 List<String> keyList =
new ArrayList<String>();
176 keyList.addAll(keyValues.keySet());
177 java.util.Collections.sort(keyList);
187 final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
188 sharedPrefsEditor.clear();
189 sharedPrefsEditor.commit();
197 @
SimpleFunction(description =
"Clear the entry with the given tag.")
199 final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
200 sharedPrefsEditor.remove(tag);
201 sharedPrefsEditor.commit();
206 final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
207 sharedPrefsEditor.clear();
208 sharedPrefsEditor.commit();