|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
4 | 4 | import android.content.SharedPreferences;
|
| 5 | +import android.util.Log; |
5 | 6 | import android.widget.Toast;
|
6 | 7 |
|
7 | 8 | import com.fasterxml.jackson.core.JsonProcessingException;
|
8 | 9 | import com.fasterxml.jackson.databind.ObjectMapper;
|
9 | 10 |
|
10 | 11 | import java.io.IOException;
|
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import ca.coffeeshopstudio.gaminginterfaceclient.R; |
11 | 15 |
|
12 | 16 | import static android.content.Context.MODE_PRIVATE;
|
13 | 17 |
|
|
27 | 31 | * limitations under the License.
|
28 | 32 | */
|
29 | 33 | public class ControlDefaults {
|
30 |
| - private final String PREFS_NAME = "gicsScreen"; |
| 34 | + private final String PREFS_NAME_FLUTTER = "FlutterSharedPreferences"; |
| 35 | + |
31 | 36 | private GICControl defaultImageControl;
|
32 | 37 | private GICControl defaultButtonControl;
|
33 | 38 | private GICControl defaultTextControl;
|
34 | 39 | private GICControl defaultSwitchControl;
|
| 40 | + private final String PREFS_FLUTTER_PREFIX = "flutter."; |
| 41 | + |
| 42 | + //constructor purely for making compatible with flutter |
| 43 | + public ControlDefaults(Context context) { |
| 44 | + final String PREFS_NAME_LEGACY = "gicsScreen"; |
| 45 | + SharedPreferences prefsLegacy = context.getApplicationContext().getSharedPreferences(PREFS_NAME_LEGACY, MODE_PRIVATE); |
| 46 | + SharedPreferences prefsFlutter = context.getApplicationContext().getSharedPreferences(PREFS_NAME_FLUTTER, MODE_PRIVATE); |
| 47 | + SharedPreferences.Editor flutterEditor = prefsFlutter.edit(); |
| 48 | + SharedPreferences.Editor legacyEditor = prefsLegacy.edit(); |
| 49 | + |
| 50 | + Map<String, ?> keys = prefsLegacy.getAll(); |
| 51 | + for (Map.Entry<String, ?> entry : keys.entrySet()) { |
| 52 | + Log.d("GICS", "cleanupLegacyDefaults: " + entry.getKey()); |
| 53 | + if (containsKey(entry.getKey()) ) { |
| 54 | + Log.d("GICS", "cleanupLegacy: " + "converting"); |
| 55 | + //we need to convert |
| 56 | + if (entry.getValue() instanceof String) |
| 57 | + flutterEditor.putString(PREFS_FLUTTER_PREFIX + entry.getKey(), (String) entry.getValue()); |
| 58 | + else //gotta be an int |
| 59 | + flutterEditor.putInt(PREFS_FLUTTER_PREFIX + entry.getKey(), (Integer) entry.getValue()); |
| 60 | + //and remove |
| 61 | + legacyEditor.remove(entry.getKey()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + //set resource defaults |
| 66 | + flutterEditor.putInt(PREFS_FLUTTER_PREFIX + "default_button_primary", R.drawable.button_blue); |
| 67 | + flutterEditor.putInt(PREFS_FLUTTER_PREFIX + "default_button_secondary", R.drawable.button_blue_dark); |
| 68 | + flutterEditor.putInt(PREFS_FLUTTER_PREFIX + "default_switch_primary", R.drawable.switch_off); |
| 69 | + flutterEditor.putInt(PREFS_FLUTTER_PREFIX + "default_switch_secondary", R.drawable.switch_on); |
| 70 | + |
| 71 | + legacyEditor.apply(); |
| 72 | + flutterEditor.apply(); |
| 73 | + } |
| 74 | + private boolean containsKey (String key) { |
| 75 | + return key.contains("_image_defaults") || |
| 76 | + key.contains("_button_defaults") || |
| 77 | + key.contains("_text_defaults") || |
| 78 | + key.contains("_switch_defaults"); |
| 79 | + } |
| 80 | + |
35 | 81 |
|
36 | 82 | public ControlDefaults(Context context, int screenId) {
|
37 |
| - SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE); |
| 83 | + SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PREFS_NAME_FLUTTER, MODE_PRIVATE); |
38 | 84 |
|
39 | 85 | String prefString = screenId + "_image_defaults";
|
40 | 86 | defaultImageControl = loadControl(context, prefs, prefString);
|
@@ -92,25 +138,30 @@ private GICControl loadControl(Context context, SharedPreferences prefs, String
|
92 | 138 | }
|
93 | 139 |
|
94 | 140 | public void saveDefaults(Context context, int screenId) {
|
95 |
| - SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE); |
| 141 | + SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PREFS_NAME_FLUTTER, MODE_PRIVATE); |
| 142 | + save(context, screenId, prefs); |
| 143 | + } |
| 144 | + |
| 145 | + //reusable for both saveDefaults and the new upgrade method |
| 146 | + private void save(Context context, int screenId, SharedPreferences prefs) { |
96 | 147 | SharedPreferences.Editor prefsEditor = prefs.edit();
|
97 | 148 | ObjectMapper mapper = new ObjectMapper();
|
98 | 149 | String json;
|
99 | 150 |
|
100 | 151 | try {
|
101 |
| - String prefString = screenId + "_image_defaults"; |
| 152 | + String prefString = PREFS_FLUTTER_PREFIX + screenId + "_image_defaults"; |
102 | 153 | json = mapper.writeValueAsString(defaultImageControl);
|
103 | 154 | prefsEditor.putString(prefString, json);
|
104 | 155 |
|
105 |
| - prefString = screenId + "_text_defaults"; |
| 156 | + prefString = PREFS_FLUTTER_PREFIX + screenId + "_text_defaults"; |
106 | 157 | json = mapper.writeValueAsString(defaultTextControl);
|
107 | 158 | prefsEditor.putString(prefString, json);
|
108 | 159 |
|
109 |
| - prefString = screenId + "_button_defaults"; |
| 160 | + prefString = PREFS_FLUTTER_PREFIX + screenId + "_button_defaults"; |
110 | 161 | json = mapper.writeValueAsString(defaultButtonControl);
|
111 | 162 | prefsEditor.putString(prefString, json);
|
112 | 163 |
|
113 |
| - prefString = screenId + "_switch_defaults"; |
| 164 | + prefString = PREFS_FLUTTER_PREFIX + screenId + "_switch_defaults"; |
114 | 165 | json = mapper.writeValueAsString(defaultSwitchControl);
|
115 | 166 | prefsEditor.putString(prefString, json);
|
116 | 167 | } catch (JsonProcessingException e) {
|
|
0 commit comments