commit bcbc2ef3152275fa9ec70365a0e3c6ba45b78410 Author: Fabio Mazza Date: Fri Mar 1 11:12:06 2024 +0100 Avoid overwriting lines on import, fix null bug diff --git a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt index afc4048..7df1b0c 100644 --- a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt +++ b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt @@ -24,7 +24,7 @@ class MQTTMatoClient(): MqttCallbackExtended{ private var isStarted = false private var subscribedToAll = false - private lateinit var client: MqttAndroidClient + private var client: MqttAndroidClient? = null //private var clientID = "" private val respondersMap = HashMap>>() @@ -66,9 +66,9 @@ class MQTTMatoClient(): MqttCallbackExtended{ Log.d(DEBUG_TAG,"client name: $clientID") //actually connect - client.connect(options,null, iMqttActionListener) + client!!.connect(options,null, iMqttActionListener) isStarted = true - client.setCallback(this) + client!!.setCallback(this) if (this.context ==null) this.context = context.applicationContext @@ -92,8 +92,8 @@ class MQTTMatoClient(): MqttCallbackExtended{ disconnectedBufferOptions.bufferSize = 100 disconnectedBufferOptions.isPersistBuffer = false disconnectedBufferOptions.isDeleteOldestMessages = false - client.setBufferOpts(disconnectedBufferOptions) - client.subscribe(topic, QoS.AtMostOnce.value) + client!!.setBufferOpts(disconnectedBufferOptions) + client!!.subscribe(topic, QoS.AtMostOnce.value) isStarted = true } @@ -122,7 +122,7 @@ class MQTTMatoClient(): MqttCallbackExtended{ connectTopic(topic) //wait for connection } else { - client.subscribe(topic, QoS.AtMostOnce.value) + client!!.subscribe(topic, QoS.AtMostOnce.value) } } @@ -156,7 +156,7 @@ class MQTTMatoClient(): MqttCallbackExtended{ //if (done) break if (v.isEmpty()){ //actually unsubscribe - client.unsubscribe( mapTopic(line)) + client!!.unsubscribe( mapTopic(line)) } removed = done || removed } @@ -227,7 +227,7 @@ class MQTTMatoClient(): MqttCallbackExtended{ if(elms.isEmpty()) respondersMap.remove(line) else { - client.subscribe(topic, QoS.AtMostOnce.value, null, null) + client!!.subscribe(topic, QoS.AtMostOnce.value, null, null) Log.d(DEBUG_TAG, "Resubscribed with topic $topic") } } @@ -327,7 +327,7 @@ class MQTTMatoClient(): MqttCallbackExtended{ //try unsubscribing to all if(emptyResp) { Log.d(DEBUG_TAG, "Unsubscribe all") - client.unsubscribe(LINES_ALL) + client!!.unsubscribe(LINES_ALL) } } //Log.d(DEBUG_TAG, "We have update on line $lineId, vehicle $vehicleId") @@ -353,7 +353,7 @@ class MQTTMatoClient(): MqttCallbackExtended{ }*/ fun disconnect(){ - client.disconnect() + client?.disconnect() } diff --git a/app/src/main/java/it/reyboz/bustorino/data/PreferencesHolder.java b/app/src/main/java/it/reyboz/bustorino/data/PreferencesHolder.java index ae3fb99..82c832e 100644 --- a/app/src/main/java/it/reyboz/bustorino/data/PreferencesHolder.java +++ b/app/src/main/java/it/reyboz/bustorino/data/PreferencesHolder.java @@ -39,6 +39,7 @@ public abstract class PreferencesHolder { public static final String DB_LAST_UPDATE_KEY = "NextGenDB.LastDBUpdate"; public static final String PREF_FAVORITE_LINES = "pref_favorite_lines"; + public static final Set KEYS_MERGE_SET = Set.of(PREF_FAVORITE_LINES); public static final Set IGNORE_KEYS_LOAD_MAIN = Set.of(PREF_GTFS_DB_VERSION, PREF_INTRO_ACTIVITY_RUN, DB_GTT_VERSION_KEY, DB_LAST_UPDATE_KEY); public static SharedPreferences getMainSharedPreferences(Context context){ diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/BackupImportFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/BackupImportFragment.kt index b5479ff..47468b2 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/BackupImportFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/BackupImportFragment.kt @@ -18,7 +18,7 @@ import de.siegmar.fastcsv.writer.CsvWriter import it.reyboz.bustorino.R import it.reyboz.bustorino.data.PreferencesHolder import it.reyboz.bustorino.data.UserDB -import it.reyboz.bustorino.util.Saving +import it.reyboz.bustorino.util.ImportExport import java.io.* import java.text.DateFormat import java.text.SimpleDateFormat @@ -162,13 +162,13 @@ class BackupImportFragment : Fragment() { //write main preferences zipOutputStream.putNextEntry(ZipEntry(MAIN_PREF_NAME)) var sharedPrefs = PreferencesHolder.getMainSharedPreferences(context) - var jsonPref = Saving.writeSharedPreferencesIntoJSON(sharedPrefs) + var jsonPref = ImportExport.writeSharedPreferencesIntoJSON(sharedPrefs) writeStringToZipOS(jsonPref.toString(2), zipOutputStream) zipOutputStream.closeEntry() zipOutputStream.putNextEntry(ZipEntry(APP_PREF_NAME)) sharedPrefs = PreferencesHolder.getAppPreferences(context) - jsonPref = Saving.writeSharedPreferencesIntoJSON(sharedPrefs) + jsonPref = ImportExport.writeSharedPreferencesIntoJSON(sharedPrefs) writeStringToZipOS(jsonPref.toString(2), zipOutputStream) zipOutputStream.closeEntry() //add CSV @@ -210,7 +210,7 @@ class BackupImportFragment : Fragment() { val jsonString = readFileToString(zipstream) try { val pref = PreferencesHolder.getAppPreferences(context) - Saving.importJsonToSharedPreferences(pref, jsonString, null, Regex("osmdroid\\.")) + ImportExport.importJsonToSharedPreferences(pref, jsonString, null, Regex("osmdroid\\.")) } catch (e: Exception){ Log.e(DEBUG_TAG, "Cannot read app preferences from file") e.printStackTrace() @@ -223,7 +223,8 @@ class BackupImportFragment : Fragment() { val pref = PreferencesHolder.getMainSharedPreferences(context) //In the future, if we move the favorite lines to a different file, // We should check here if the key is in the jsonObject, and copy it to the other file - Saving.importJsonToSharedPreferences(pref, jsonString, PreferencesHolder.IGNORE_KEYS_LOAD_MAIN, null) + ImportExport.importJsonToSharedPreferences(pref, jsonString, PreferencesHolder.IGNORE_KEYS_LOAD_MAIN, null, + PreferencesHolder.KEYS_MERGE_SET) } catch (e: Exception){ Log.e(DEBUG_TAG, "Cannot read main preferences from file") e.printStackTrace() diff --git a/app/src/main/java/it/reyboz/bustorino/util/Saving.kt b/app/src/main/java/it/reyboz/bustorino/util/ImportExport.kt similarity index 86% rename from app/src/main/java/it/reyboz/bustorino/util/Saving.kt rename to app/src/main/java/it/reyboz/bustorino/util/ImportExport.kt index 6eb7ca6..d846c0f 100644 --- a/app/src/main/java/it/reyboz/bustorino/util/Saving.kt +++ b/app/src/main/java/it/reyboz/bustorino/util/ImportExport.kt @@ -8,7 +8,7 @@ import org.json.JSONObject import java.io.* -class Saving { +class ImportExport { companion object{ @@ -87,10 +87,20 @@ class Saving { } return editor.commit() } - + fun importJsonToSharedPreferences(sharedPreferences: SharedPreferences, + allJsonAsString: String, + ignoreKeys: Set?, + ignoreKeyRegex: Regex?): Int{ + return importJsonToSharedPreferences(sharedPreferences, allJsonAsString, + ignoreKeys, ignoreKeyRegex, HashSet()) + } fun importJsonToSharedPreferences(sharedPreferences: SharedPreferences, - allJsonAsString: String, ignoreKeys: Set?, ignoreKeyRegex: Regex?): Int { + allJsonAsString: String, + ignoreKeys: Set?, + ignoreKeyRegex: Regex?, + mergeSetKeys: Set + ): Int { // Parse JSON val jsonObject = JSONObject(allJsonAsString) @@ -111,6 +121,8 @@ class Saving { is String -> editor.putString(key, value) is JSONArray -> { // Handle arrays val set = mutableSetOf() + if (mergeSetKeys.contains(key)) + sharedPreferences.getStringSet(key, mutableSetOf())?.let { set.addAll(it) } for (i in 0 until value.length()) { set.add(value.optString(i)) }