Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/util/ImportExport.kt
- This file was moved from app/src/main/java/it/reyboz/bustorino/util/Saving.kt.
package it.reyboz.bustorino.util | package it.reyboz.bustorino.util | ||||
import android.content.SharedPreferences | import android.content.SharedPreferences | ||||
import android.util.Log | import android.util.Log | ||||
import it.reyboz.bustorino.BuildConfig | import it.reyboz.bustorino.BuildConfig | ||||
import org.json.JSONArray | import org.json.JSONArray | ||||
import org.json.JSONObject | import org.json.JSONObject | ||||
import java.io.* | import java.io.* | ||||
class Saving { | class ImportExport { | ||||
companion object{ | companion object{ | ||||
const val TAG = "BusTO-Saving"; | const val TAG = "BusTO-Saving"; | ||||
/** | /** | ||||
* Serialize all preferences into an output stream | * Serialize all preferences into an output stream | ||||
* @param os OutputStream to write to | * @param os OutputStream to write to | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | companion object{ | ||||
if (setvalue.iterator().next() is String) | if (setvalue.iterator().next() is String) | ||||
editor.putStringSet(key, value as Set<String?>) | editor.putStringSet(key, value as Set<String?>) | ||||
} else { | } else { | ||||
throw IllegalArgumentException("Type " + value.javaClass.name + " is unknown") | throw IllegalArgumentException("Type " + value.javaClass.name + " is unknown") | ||||
} | } | ||||
} | } | ||||
return editor.commit() | return editor.commit() | ||||
} | } | ||||
fun importJsonToSharedPreferences(sharedPreferences: SharedPreferences, | |||||
allJsonAsString: String, | |||||
ignoreKeys: Set<String>?, | |||||
ignoreKeyRegex: Regex?): Int{ | |||||
return importJsonToSharedPreferences(sharedPreferences, allJsonAsString, | |||||
ignoreKeys, ignoreKeyRegex, HashSet()) | |||||
} | |||||
fun importJsonToSharedPreferences(sharedPreferences: SharedPreferences, | fun importJsonToSharedPreferences(sharedPreferences: SharedPreferences, | ||||
allJsonAsString: String, ignoreKeys: Set<String>?, ignoreKeyRegex: Regex?): Int { | allJsonAsString: String, | ||||
ignoreKeys: Set<String>?, | |||||
ignoreKeyRegex: Regex?, | |||||
mergeSetKeys: Set<String> | |||||
): Int { | |||||
// Parse JSON | // Parse JSON | ||||
val jsonObject = JSONObject(allJsonAsString) | val jsonObject = JSONObject(allJsonAsString) | ||||
try { | try { | ||||
// Write to SharedPreferences | // Write to SharedPreferences | ||||
val editor = sharedPreferences.edit() | val editor = sharedPreferences.edit() | ||||
jsonObject.keys().forEach { key -> | jsonObject.keys().forEach { key -> | ||||
if (ignoreKeys?.contains(key)==true || ignoreKeyRegex?.containsMatchIn(key)==true) | if (ignoreKeys?.contains(key)==true || ignoreKeyRegex?.containsMatchIn(key)==true) | ||||
//do nothing | //do nothing | ||||
return@forEach | return@forEach | ||||
val value = jsonObject.opt(key) | val value = jsonObject.opt(key) | ||||
when (value) { | when (value) { | ||||
is Boolean -> editor.putBoolean(key, value) | is Boolean -> editor.putBoolean(key, value) | ||||
is Int -> editor.putInt(key, value) | is Int -> editor.putInt(key, value) | ||||
is Long -> editor.putLong(key, value) | is Long -> editor.putLong(key, value) | ||||
is Float -> editor.putFloat(key, value) | is Float -> editor.putFloat(key, value) | ||||
is String -> editor.putString(key, value) | is String -> editor.putString(key, value) | ||||
is JSONArray -> { // Handle arrays | is JSONArray -> { // Handle arrays | ||||
val set = mutableSetOf<String>() | val set = mutableSetOf<String>() | ||||
if (mergeSetKeys.contains(key)) | |||||
sharedPreferences.getStringSet(key, mutableSetOf())?.let { set.addAll(it) } | |||||
for (i in 0 until value.length()) { | for (i in 0 until value.length()) { | ||||
set.add(value.optString(i)) | set.add(value.optString(i)) | ||||
} | } | ||||
editor.putStringSet(key, set) | editor.putStringSet(key, set) | ||||
} | } | ||||
// Handle other types as needed | // Handle other types as needed | ||||
} | } | ||||
} | } | ||||
Show All 24 Lines | companion object{ | ||||
json.put(key, value) | json.put(key, value) | ||||
} | } | ||||
} | } | ||||
return json | return json | ||||
} | } | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register