Skip to content

Commit 35c29bb

Browse files
Add support for update shared pref
1 parent d9f8a47 commit 35c29bb

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private String getTableListResponse(String route) {
230230
if (Constants.APP_SHARED_PREFERENCES.equals(database)) {
231231
response = PrefHelper.getAllPrefTableName(mContext);
232232
closeDatabase();
233-
mSelectedDatabase = null;
233+
mSelectedDatabase = Constants.APP_SHARED_PREFERENCES;
234234
} else {
235235
openDatabase(database);
236236
response = DatabaseHelper.getAllTableName(mDatabase);
@@ -247,7 +247,11 @@ private String updateTableDataAndGetResponse(String route) {
247247
String updatedData = uri.getQueryParameter("updatedData");
248248
List<RowDataRequest> rowDataRequests = mGson.fromJson(updatedData, new TypeToken<List<RowDataRequest>>() {
249249
}.getType());
250-
response = DatabaseHelper.updateRow(mDatabase, tableName, rowDataRequests);
250+
if (Constants.APP_SHARED_PREFERENCES.equals(mSelectedDatabase)) {
251+
response = PrefHelper.updateRow(mContext, tableName, rowDataRequests);
252+
} else {
253+
response = DatabaseHelper.updateRow(mDatabase, tableName, rowDataRequests);
254+
}
251255
return mGson.toJson(response);
252256
} catch (Exception e) {
253257
e.printStackTrace();

debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static UpdateRowResponse updateRow(SQLiteDatabase db, String tableName, L
183183

184184
UpdateRowResponse updateRowResponse = new UpdateRowResponse();
185185

186-
if (rowDataRequests == null) {
186+
if (rowDataRequests == null || tableName == null) {
187187
updateRowResponse.isSuccessful = false;
188188
return updateRowResponse;
189189
}

debug-db/src/main/java/com/amitshekhar/utils/PrefHelper.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
import android.content.SharedPreferences;
2424

2525
import com.amitshekhar.model.Response;
26+
import com.amitshekhar.model.RowDataRequest;
2627
import com.amitshekhar.model.TableDataResponse;
28+
import com.amitshekhar.model.UpdateRowResponse;
29+
30+
import org.json.JSONArray;
2731

2832
import java.io.File;
2933
import java.util.ArrayList;
3034
import java.util.Collections;
35+
import java.util.HashSet;
3136
import java.util.List;
3237
import java.util.Map;
3338
import java.util.Set;
@@ -134,4 +139,50 @@ public static TableDataResponse getAllPrefData(Context context, String tag) {
134139
return response;
135140

136141
}
142+
143+
public static UpdateRowResponse updateRow(Context context, String tableName, List<RowDataRequest> rowDataRequests) {
144+
UpdateRowResponse updateRowResponse = new UpdateRowResponse();
145+
146+
RowDataRequest rowDataKey = rowDataRequests.get(0);
147+
RowDataRequest rowDataValue = rowDataRequests.get(1);
148+
149+
String key = rowDataKey.value;
150+
String value = rowDataValue.value;
151+
152+
SharedPreferences preferences = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
153+
Map<String, ?> allEntries = preferences.getAll();
154+
155+
Object prevValue = allEntries.get(key);
156+
157+
try {
158+
if (prevValue instanceof String) {
159+
preferences.edit().putString(key, value).apply();
160+
updateRowResponse.isSuccessful = true;
161+
} else if (prevValue instanceof Integer) {
162+
preferences.edit().putInt(key, Integer.valueOf(value)).apply();
163+
updateRowResponse.isSuccessful = true;
164+
} else if (prevValue instanceof Long) {
165+
preferences.edit().putLong(key, Long.valueOf(value)).apply();
166+
updateRowResponse.isSuccessful = true;
167+
} else if (prevValue instanceof Float) {
168+
preferences.edit().putFloat(key, Float.valueOf(value)).apply();
169+
updateRowResponse.isSuccessful = true;
170+
} else if (prevValue instanceof Boolean) {
171+
preferences.edit().putBoolean(key, Boolean.valueOf(value)).apply();
172+
updateRowResponse.isSuccessful = true;
173+
} else if (prevValue instanceof Set) {
174+
JSONArray jsonArray = new JSONArray(value);
175+
Set<String> stringSet = new HashSet<>();
176+
for (int i = 0; i < jsonArray.length(); i++) {
177+
stringSet.add(jsonArray.getString(i));
178+
}
179+
preferences.edit().putStringSet(key, stringSet).apply();
180+
updateRowResponse.isSuccessful = true;
181+
}
182+
} catch (Exception e) {
183+
e.printStackTrace();
184+
}
185+
186+
return updateRowResponse;
187+
}
137188
}

0 commit comments

Comments
 (0)