|
23 | 23 | import android.content.SharedPreferences; |
24 | 24 |
|
25 | 25 | import com.amitshekhar.model.Response; |
| 26 | +import com.amitshekhar.model.RowDataRequest; |
26 | 27 | import com.amitshekhar.model.TableDataResponse; |
| 28 | +import com.amitshekhar.model.UpdateRowResponse; |
| 29 | + |
| 30 | +import org.json.JSONArray; |
27 | 31 |
|
28 | 32 | import java.io.File; |
29 | 33 | import java.util.ArrayList; |
30 | 34 | import java.util.Collections; |
| 35 | +import java.util.HashSet; |
31 | 36 | import java.util.List; |
32 | 37 | import java.util.Map; |
33 | 38 | import java.util.Set; |
@@ -134,4 +139,50 @@ public static TableDataResponse getAllPrefData(Context context, String tag) { |
134 | 139 | return response; |
135 | 140 |
|
136 | 141 | } |
| 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 | + } |
137 | 188 | } |
0 commit comments