Commit 4fb34f90 authored by ColinTree's avatar ColinTree Committed by Jeffrey Schiller

Adding a namespace for TinyDB in the companion (#1111)

Fixes  #907 
parent b5e89d7a
......@@ -2919,6 +2919,10 @@ public interface OdeMessages extends Messages {
@Description("")
String MultiLineProperties();
@DefaultMessage("Namespace")
@Description("")
String NamespaceProperties();
@DefaultMessage("NumbersOnly")
@Description("")
String NumbersOnlyProperties();
......
......@@ -320,6 +320,9 @@ public final class YoungAndroidFormUpgrader {
} else if (componentType.equals("TimePicker")) {
srcCompVersion = upgradeTimePickerProperties(componentProperties, srcCompVersion);
} else if (componentType.equals("TinyDB")) {
srcCompVersion = upgradeTinyDBProperties(componentProperties, srcCompVersion);
} else if (componentType.equals("TinyWebDB")) {
srcCompVersion = upgradeTinyWebDBProperties(componentProperties, srcCompVersion);
......@@ -1296,6 +1299,15 @@ public final class YoungAndroidFormUpgrader {
return srcCompVersion;
}
private static int upgradeTinyDBProperties(Map<String, JSONValue> componentProperties,
int srcCompVersion) {
if (srcCompVersion < 2) {
// Added Property: Namespace
srcCompVersion = 2;
}
return srcCompVersion;
}
private static int upgradeTinyWebDBProperties(Map<String, JSONValue> componentProperties,
int srcCompVersion) {
if (srcCompVersion < 2) {
......
......@@ -2364,7 +2364,10 @@ Blockly.Versioning.AllUpgradeMaps =
"TinyDB": {
//This is initial version. Placeholder for future upgrades
1: "noUpgrade"
1: "noUpgrade",
//Added Property: Namespace
2: "noUpgrade"
}, // End TinyDB upgraders
......
......@@ -1030,7 +1030,9 @@ public class YaVersion {
// - SetTimeToDisplayFromInstant, and Instant property are added.
public static final int TIMEPICKER_COMPONENT_VERSION = 3;
public static final int TINYDB_COMPONENT_VERSION = 1;
// For TINYDB_COMPONENT_VERSION 2:
// - Added Property: Namespace
public static final int TINYDB_COMPONENT_VERSION = 2;
// For TINYWEBDB_COMPONENT_VERSION 2:
// - The TinyWebDB.ShowAlert method was removed. Notifier.ShowAlert should be used instead.
......
......@@ -19,8 +19,6 @@ import com.google.appinventor.components.runtime.util.JsonUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -63,7 +61,10 @@ import org.json.JSONException;
@SimpleObject
public class TinyDB extends AndroidNonvisibleComponent implements Component, Deleteable {
public static final String DEFAULT_NAMESPACE="TinyDB1";
private SharedPreferences sharedPreferences;
private String namespace;
private Context context; // this was a local in constructor and final not private
......@@ -76,7 +77,19 @@ public class TinyDB extends AndroidNonvisibleComponent implements Component, Del
public TinyDB(ComponentContainer container) {
super(container.$form());
context = (Context) container.$context();
sharedPreferences = context.getSharedPreferences("TinyDB1", Context.MODE_PRIVATE);
Namespace(DEFAULT_NAMESPACE);
}
@SimpleProperty(description = "Namespace for storing data.", category = PropertyCategory.BEHAVIOR)
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_TEXT, defaultValue = DEFAULT_NAMESPACE)
public void Namespace(String namespace) {
this.namespace = namespace;
sharedPreferences = context.getSharedPreferences(namespace, Context.MODE_PRIVATE);
}
@SimpleProperty(description = "Namespace for storing data.")
public String Namespace() {
return namespace;
}
/**
......
......@@ -612,7 +612,10 @@ none
<p>TinyDB is a non-visible component that stores data for an app. <p> Apps created with App Inventor are initialized each time they run. This means that if an app sets the value of a variable and the user then quits the app, the value of that variable will not be remembered the next time the app is run. In contrast, TinyDB is a <em> persistent </em> data store for the app. The data stored in a TinyDB will be available each time the app is run. An example might be a game that saves the high score and retrieves it each time the game is played. </p> <p> Data items are strings stored under <em>tags</em>. To store a data item, you specify the tag it should be stored under. Subsequently, you can retrieve the data that was stored under a given tag. </p><p> Each app has its own data store. There is only one data store per app. Even if you have multiple TinyDB components, they will use the same data store. To get the effect of separate stores, use different keys. You cannot use the TinyDB to pass data between two different apps on the phone, although you <em>can</em> use the TinyDB to share data between the different screens of a multi-screen app. </p> <p>When you are developing apps using the AI Companion, all the apps using that Companion will share the same TinyDB. That sharing will disappear once the apps are packaged and installed on the phone. During development you should be careful to clear the Companion app's data each time you start working on a new app.</p></p>
<h3>Properties</h3>
none
<dl>
<dt><code>Namespace</code></dt>
<dd>Namespace for storing data.</dd>
</dl>
<h3>Events</h3>
none
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment