Fix my-list issues
* fix entries can be added multiple times to my list * fix entries can’t be removed from my list after the app was restarted * closes #15
This commit is contained in:
parent
14377c3f18
commit
c8d80ddc9f
|
@ -1,10 +1,12 @@
|
||||||
package org.mosad.teapod.util
|
package org.mosad.teapod.util
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.GsonBuilder
|
import com.google.gson.JsonParser
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.lang.Exception
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This controller contains the logic for permanently saved data.
|
* This controller contains the logic for permanently saved data.
|
||||||
|
@ -21,17 +23,21 @@ object StorageController {
|
||||||
|
|
||||||
if (!file.exists()) runBlocking { saveMyList(context).join() }
|
if (!file.exists()) runBlocking { saveMyList(context).join() }
|
||||||
|
|
||||||
myList.clear()
|
try {
|
||||||
myList.addAll(
|
myList.clear()
|
||||||
GsonBuilder().create().fromJson(file.readText(), ArrayList<Int>().javaClass)
|
myList.addAll(JsonParser.parseString(file.readText()).asJsonArray.map { it.asInt }.distinct())
|
||||||
)
|
} catch (ex: Exception) {
|
||||||
|
myList.clear()
|
||||||
|
Log.e(javaClass.name, "Parsing of My-List failed.")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveMyList(context: Context): Job {
|
fun saveMyList(context: Context): Job {
|
||||||
val file = File(context.filesDir, fileNameMyList)
|
val file = File(context.filesDir, fileNameMyList)
|
||||||
|
|
||||||
return GlobalScope.launch(Dispatchers.IO) {
|
return GlobalScope.launch(Dispatchers.IO) {
|
||||||
file.writeText(Gson().toJson(myList))
|
file.writeText(Gson().toJson(myList.distinct()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue