From 86818fbffb23aefce652e3c3ab2941702b82ed3e Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 17 Dec 2018 17:00:38 +0100 Subject: [PATCH] Extract StorageEditor interface --- .../fsck/k9/AccountPreferenceSerializer.kt | 19 ++++++++++++++++--- .../main/java/com/fsck/k9/Preferences.java | 3 ++- ...torageEditor.java => K9StorageEditor.java} | 11 +++++++++-- .../com/fsck/k9/preferences/StorageEditor.kt | 17 +++++++++++++++++ .../fsck/k9/preferences/StorageEditorTest.kt | 6 +++--- 5 files changed, 47 insertions(+), 9 deletions(-) rename app/core/src/main/java/com/fsck/k9/preferences/{StorageEditor.java => K9StorageEditor.java} (93%) create mode 100644 app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.kt diff --git a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt index e0c6b6e0a..9188a8f2b 100644 --- a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt +++ b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt @@ -1,6 +1,19 @@ package com.fsck.k9 -import com.fsck.k9.Account.* +import com.fsck.k9.Account.DEFAULT_SORT_ASCENDING +import com.fsck.k9.Account.DEFAULT_SORT_TYPE +import com.fsck.k9.Account.DeletePolicy +import com.fsck.k9.Account.Expunge +import com.fsck.k9.Account.FolderMode +import com.fsck.k9.Account.INBOX +import com.fsck.k9.Account.MessageFormat +import com.fsck.k9.Account.NO_OPENPGP_KEY +import com.fsck.k9.Account.QuoteStyle +import com.fsck.k9.Account.Searchable +import com.fsck.k9.Account.ShowPictures +import com.fsck.k9.Account.SortType +import com.fsck.k9.Account.SpecialFolderSelection +import com.fsck.k9.Account.UNASSIGNED_ACCOUNT_NUMBER import com.fsck.k9.helper.Utility import com.fsck.k9.mail.NetworkType import com.fsck.k9.mail.filter.Base64 @@ -8,7 +21,7 @@ import com.fsck.k9.mailstore.StorageManager import com.fsck.k9.preferences.Storage import com.fsck.k9.preferences.StorageEditor import timber.log.Timber -import java.util.* +import java.util.ArrayList class AccountPreferenceSerializer( private val storageManager: StorageManager, @@ -588,4 +601,4 @@ class AccountPreferenceSerializer( const val DEFAULT_STRIP_SIGNATURE = true const val DEFAULT_REMOTE_SEARCH_NUM_RESULTS = 25 } -} \ No newline at end of file +} diff --git a/app/core/src/main/java/com/fsck/k9/Preferences.java b/app/core/src/main/java/com/fsck/k9/Preferences.java index 9154345fb..4901ab496 100644 --- a/app/core/src/main/java/com/fsck/k9/Preferences.java +++ b/app/core/src/main/java/com/fsck/k9/Preferences.java @@ -22,6 +22,7 @@ import com.fsck.k9.backend.BackendManager; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mailstore.LocalStore; import com.fsck.k9.mailstore.LocalStoreProvider; +import com.fsck.k9.preferences.K9StorageEditor; import com.fsck.k9.preferences.Storage; import com.fsck.k9.preferences.StorageEditor; import com.fsck.k9.preferences.StoragePersister; @@ -88,7 +89,7 @@ public class Preferences { } public StorageEditor createStorageEditor() { - return new StorageEditor(storage, storagePersister); + return new K9StorageEditor(storage, storagePersister); } @RestrictTo(Scope.TESTS) diff --git a/app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.java b/app/core/src/main/java/com/fsck/k9/preferences/K9StorageEditor.java similarity index 93% rename from app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.java rename to app/core/src/main/java/com/fsck/k9/preferences/K9StorageEditor.java index aa1d19087..e8b0aac77 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.java +++ b/app/core/src/main/java/com/fsck/k9/preferences/K9StorageEditor.java @@ -14,7 +14,7 @@ import com.fsck.k9.preferences.StoragePersister.StoragePersistOperations; import timber.log.Timber; -public class StorageEditor { +public class K9StorageEditor implements StorageEditor { private Storage storage; private StoragePersister storagePersister; @@ -23,12 +23,13 @@ public class StorageEditor { private Map snapshot = new HashMap<>(); - public StorageEditor(Storage storage, StoragePersister storagePersister) { + public K9StorageEditor(Storage storage, StoragePersister storagePersister) { this.storage = storage; this.storagePersister = storagePersister; snapshot.putAll(storage.getAll()); } + @Override public void copy(android.content.SharedPreferences input) { Map < String, ? > oldVals = input.getAll(); for (Entry < String, ? > entry : oldVals.entrySet()) { @@ -43,6 +44,7 @@ public class StorageEditor { } } + @Override public boolean commit() { try { commitChanges(); @@ -87,22 +89,26 @@ public class StorageEditor { Timber.i("Preferences commit took %d ms", endTime - startTime); } + @Override public StorageEditor putBoolean(String key, boolean value) { changes.put(key, "" + value); return this; } + @Override public StorageEditor putInt(String key, int value) { changes.put(key, "" + value); return this; } + @Override public StorageEditor putLong(String key, long value) { changes.put(key, "" + value); return this; } + @Override public StorageEditor putString(String key, String value) { if (value == null) { remove(key); @@ -112,6 +118,7 @@ public class StorageEditor { return this; } + @Override public StorageEditor remove(String key) { removals.add(key); return this; diff --git a/app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.kt b/app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.kt new file mode 100644 index 000000000..494488d65 --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/preferences/StorageEditor.kt @@ -0,0 +1,17 @@ +package com.fsck.k9.preferences + +import android.content.SharedPreferences + + +interface StorageEditor { + fun copy(input: SharedPreferences) + + fun putBoolean(key: String, value: Boolean): StorageEditor + fun putInt(key: String, value: Int): StorageEditor + fun putLong(key: String, value: Long): StorageEditor + fun putString(key: String, value: String?): StorageEditor + + fun remove(key: String): StorageEditor + + fun commit(): Boolean +} diff --git a/app/core/src/test/java/com/fsck/k9/preferences/StorageEditorTest.kt b/app/core/src/test/java/com/fsck/k9/preferences/StorageEditorTest.kt index ba0ac2337..eeaabc957 100644 --- a/app/core/src/test/java/com/fsck/k9/preferences/StorageEditorTest.kt +++ b/app/core/src/test/java/com/fsck/k9/preferences/StorageEditorTest.kt @@ -19,7 +19,7 @@ class StorageEditorTest : K9RobolectricTest() { @Mock private lateinit var storage: Storage @Mock private lateinit var storagePersister: StoragePersister @Mock private lateinit var storagePersisterOps: StoragePersister.StoragePersistOperations - private lateinit var editor: StorageEditor + private lateinit var editor: K9StorageEditor private val workingMap = mutableMapOf() private val storageMap = mapOf( @@ -31,7 +31,7 @@ class StorageEditorTest : K9RobolectricTest() { MockitoAnnotations.initMocks(this) whenever(storage.all).thenReturn(storageMap) - editor = StorageEditor(storage, storagePersister) + editor = K9StorageEditor(storage, storagePersister) verify(storage).all } @@ -180,4 +180,4 @@ class StorageEditorTest : K9RobolectricTest() { verify(storage).replaceAll(eq(workingMap)) } } -} \ No newline at end of file +}