improved data sync save to local part

This commit is contained in:
Mariotaku Lee 2017-01-16 01:09:32 +08:00
parent d90b84fcc6
commit 2aca2be609
4 changed files with 33 additions and 10 deletions

View File

@ -23,6 +23,7 @@ import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.mariotaku.sqliteqb.library.ArgsArray;
@ -41,14 +42,14 @@ public class ContentResolverUtils {
}
public static <T> int bulkDelete(@NonNull final ContentResolver resolver, @NonNull final Uri uri,
@NonNull final String inColumn, final Collection<T> colValues,
@NonNull final String inColumn, @Nullable final Collection<T> colValues,
final String extraWhere) {
if (colValues == null) return 0;
return bulkDelete(resolver, uri, inColumn, colValues.toArray(), extraWhere);
}
public static int bulkDelete(@NonNull final ContentResolver resolver, @NonNull final Uri uri,
@NonNull final String inColumn, final Object colValues,
@NonNull final String inColumn, @Nullable final Object colValues,
final String extraWhere) {
if (colValues == null) return 0;
final int colValuesLength = Array.getLength(colValues), blocksCount = colValuesLength / MAX_BULK_COUNT + 1;

View File

@ -5,6 +5,8 @@ import android.util.Xml
import org.mariotaku.ktextension.nullableContentEquals
import org.mariotaku.twidere.extension.model.*
import org.mariotaku.twidere.model.FiltersData
import org.mariotaku.twidere.provider.TwidereDataStore.Filters
import org.mariotaku.twidere.util.content.ContentResolverUtils
import java.io.Closeable
import java.io.File
import java.io.IOException
@ -45,8 +47,19 @@ abstract class FileBasedFiltersDataSyncAction<DownloadSession : Closeable, Uploa
}
}
override fun FiltersData.saveToLocal() {
this.write(context.contentResolver, deleteOld = true)
override fun addToLocal(data: FiltersData) {
data.write(context.contentResolver, deleteOld = false)
}
override fun removeFromLocal(data: FiltersData) {
ContentResolverUtils.bulkDelete(context.contentResolver, Filters.Users.CONTENT_URI,
Filters.Users.USER_KEY, data.users?.map { it.userKey }, null)
ContentResolverUtils.bulkDelete(context.contentResolver, Filters.Keywords.CONTENT_URI,
Filters.Keywords.VALUE, data.keywords?.map { it.value }, null)
ContentResolverUtils.bulkDelete(context.contentResolver, Filters.Sources.CONTENT_URI,
Filters.Sources.VALUE, data.sources?.map { it.value }, null)
ContentResolverUtils.bulkDelete(context.contentResolver, Filters.Links.CONTENT_URI,
Filters.Links.VALUE, data.links?.map { it.value }, null)
}
override fun newData(): FiltersData {

View File

@ -19,15 +19,22 @@ abstract class FileBasedPreferencesValuesSyncAction<DownloadSession : Closeable,
override final val whatData: String = processor.whatData
override final fun MutableMap<String, String>.saveToLocal() {
override fun addToLocal(data: MutableMap<String, String>) {
val editor = preferences.edit()
editor.clear()
for ((k, v) in this) {
for ((k, v) in data) {
processor.saveValue(editor, k, v)
}
editor.apply()
}
override fun removeFromLocal(data: MutableMap<String, String>) {
val editor = preferences.edit()
for ((k, v) in data) {
editor.remove(k)
}
editor.apply()
}
override final fun loadFromLocal(): MutableMap<String, String> {
val result = HashMap<String, String>()
for ((k, v) in preferences.all) {

View File

@ -64,16 +64,16 @@ abstract class SingleFileBasedDataSyncAction<Data, SnapshotStore, DownloadSessio
if (remoteModified) {
if (remoteDeletedData != null) {
localData.removeAllData(remoteDeletedData)
removeFromLocal(remoteDeletedData)
localModified = localModified or !remoteDeletedData.isDataEmpty()
}
if (remoteAddedData != null) {
localData.addAllData(remoteAddedData)
addToLocal(remoteAddedData)
localModified = localModified or !remoteAddedData.isDataEmpty()
}
}
localData.saveToLocal()
val localModifiedTime = System.currentTimeMillis()
if (shouldCreateRemote || localModified) {
@ -125,7 +125,9 @@ abstract class SingleFileBasedDataSyncAction<Data, SnapshotStore, DownloadSessio
// Local save/load operations
protected abstract fun loadFromLocal(): Data
protected abstract fun Data.saveToLocal()
protected abstract fun addToLocal(data: Data)
protected abstract fun removeFromLocal(data: Data)
// Remote operations
@Throws(FileNotFoundException::class, IOException::class)