Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/util/sync/FileBasedPreferencesValuesS...

53 lines
1.5 KiB
Kotlin
Raw Normal View History

2017-01-02 09:33:27 +01:00
package org.mariotaku.twidere.util.sync
import android.content.Context
import android.content.SharedPreferences
import java.io.Closeable
import java.util.*
/**
* Created by mariotaku on 2017/1/2.
*/
abstract class FileBasedPreferencesValuesSyncAction<DownloadSession : Closeable, UploadSession : Closeable>(
2017-01-02 09:33:27 +01:00
context: Context,
var preferences: SharedPreferences,
val processor: Processor
) : FileBasedKeyValueSyncAction<DownloadSession, UploadSession>(context) {
2017-01-02 09:33:27 +01:00
2019-10-24 17:52:11 +02:00
final override val snapshotFileName: String = processor.snapshotFileName
2017-01-02 09:33:27 +01:00
2019-10-24 17:52:11 +02:00
final override val whatData: String = processor.whatData
2017-01-02 09:33:27 +01:00
2017-01-15 18:09:32 +01:00
override fun addToLocal(data: MutableMap<String, String>) {
2017-01-02 09:33:27 +01:00
val editor = preferences.edit()
2017-01-15 18:09:32 +01:00
for ((k, v) in data) {
2017-01-02 09:33:27 +01:00
processor.saveValue(editor, k, v)
}
editor.apply()
}
2017-01-15 18:09:32 +01:00
override fun removeFromLocal(data: MutableMap<String, String>) {
val editor = preferences.edit()
2017-05-21 04:12:01 +02:00
for (k in data.keys) {
2017-01-15 18:09:32 +01:00
editor.remove(k)
}
editor.apply()
}
2019-10-24 17:52:11 +02:00
final override fun loadFromLocal(): MutableMap<String, String> {
2017-01-02 09:33:27 +01:00
val result = HashMap<String, String>()
for ((k, v) in preferences.all) {
processor.loadValue(result, k, v)
}
return result
}
interface Processor {
val snapshotFileName: String
val whatData: String
fun saveValue(editor: SharedPreferences.Editor, key: String, value: String)
fun loadValue(map: MutableMap<String, String>, key: String, value: Any?)
}
}