Extract StorageEditor interface

This commit is contained in:
cketti 2018-12-17 17:00:38 +01:00
parent db1dcb4618
commit 86818fbffb
5 changed files with 47 additions and 9 deletions

View file

@ -1,6 +1,19 @@
package com.fsck.k9 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.helper.Utility
import com.fsck.k9.mail.NetworkType import com.fsck.k9.mail.NetworkType
import com.fsck.k9.mail.filter.Base64 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.Storage
import com.fsck.k9.preferences.StorageEditor import com.fsck.k9.preferences.StorageEditor
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.ArrayList
class AccountPreferenceSerializer( class AccountPreferenceSerializer(
private val storageManager: StorageManager, private val storageManager: StorageManager,
@ -588,4 +601,4 @@ class AccountPreferenceSerializer(
const val DEFAULT_STRIP_SIGNATURE = true const val DEFAULT_STRIP_SIGNATURE = true
const val DEFAULT_REMOTE_SEARCH_NUM_RESULTS = 25 const val DEFAULT_REMOTE_SEARCH_NUM_RESULTS = 25
} }
} }

View file

@ -22,6 +22,7 @@ import com.fsck.k9.backend.BackendManager;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mailstore.LocalStore; import com.fsck.k9.mailstore.LocalStore;
import com.fsck.k9.mailstore.LocalStoreProvider; import com.fsck.k9.mailstore.LocalStoreProvider;
import com.fsck.k9.preferences.K9StorageEditor;
import com.fsck.k9.preferences.Storage; import com.fsck.k9.preferences.Storage;
import com.fsck.k9.preferences.StorageEditor; import com.fsck.k9.preferences.StorageEditor;
import com.fsck.k9.preferences.StoragePersister; import com.fsck.k9.preferences.StoragePersister;
@ -88,7 +89,7 @@ public class Preferences {
} }
public StorageEditor createStorageEditor() { public StorageEditor createStorageEditor() {
return new StorageEditor(storage, storagePersister); return new K9StorageEditor(storage, storagePersister);
} }
@RestrictTo(Scope.TESTS) @RestrictTo(Scope.TESTS)

View file

@ -14,7 +14,7 @@ import com.fsck.k9.preferences.StoragePersister.StoragePersistOperations;
import timber.log.Timber; import timber.log.Timber;
public class StorageEditor { public class K9StorageEditor implements StorageEditor {
private Storage storage; private Storage storage;
private StoragePersister storagePersister; private StoragePersister storagePersister;
@ -23,12 +23,13 @@ public class StorageEditor {
private Map<String, String> snapshot = new HashMap<>(); private Map<String, String> snapshot = new HashMap<>();
public StorageEditor(Storage storage, StoragePersister storagePersister) { public K9StorageEditor(Storage storage, StoragePersister storagePersister) {
this.storage = storage; this.storage = storage;
this.storagePersister = storagePersister; this.storagePersister = storagePersister;
snapshot.putAll(storage.getAll()); snapshot.putAll(storage.getAll());
} }
@Override
public void copy(android.content.SharedPreferences input) { public void copy(android.content.SharedPreferences input) {
Map < String, ? > oldVals = input.getAll(); Map < String, ? > oldVals = input.getAll();
for (Entry < String, ? > entry : oldVals.entrySet()) { for (Entry < String, ? > entry : oldVals.entrySet()) {
@ -43,6 +44,7 @@ public class StorageEditor {
} }
} }
@Override
public boolean commit() { public boolean commit() {
try { try {
commitChanges(); commitChanges();
@ -87,22 +89,26 @@ public class StorageEditor {
Timber.i("Preferences commit took %d ms", endTime - startTime); Timber.i("Preferences commit took %d ms", endTime - startTime);
} }
@Override
public StorageEditor putBoolean(String key, public StorageEditor putBoolean(String key,
boolean value) { boolean value) {
changes.put(key, "" + value); changes.put(key, "" + value);
return this; return this;
} }
@Override
public StorageEditor putInt(String key, int value) { public StorageEditor putInt(String key, int value) {
changes.put(key, "" + value); changes.put(key, "" + value);
return this; return this;
} }
@Override
public StorageEditor putLong(String key, long value) { public StorageEditor putLong(String key, long value) {
changes.put(key, "" + value); changes.put(key, "" + value);
return this; return this;
} }
@Override
public StorageEditor putString(String key, String value) { public StorageEditor putString(String key, String value) {
if (value == null) { if (value == null) {
remove(key); remove(key);
@ -112,6 +118,7 @@ public class StorageEditor {
return this; return this;
} }
@Override
public StorageEditor remove(String key) { public StorageEditor remove(String key) {
removals.add(key); removals.add(key);
return this; return this;

View file

@ -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
}

View file

@ -19,7 +19,7 @@ class StorageEditorTest : K9RobolectricTest() {
@Mock private lateinit var storage: Storage @Mock private lateinit var storage: Storage
@Mock private lateinit var storagePersister: StoragePersister @Mock private lateinit var storagePersister: StoragePersister
@Mock private lateinit var storagePersisterOps: StoragePersister.StoragePersistOperations @Mock private lateinit var storagePersisterOps: StoragePersister.StoragePersistOperations
private lateinit var editor: StorageEditor private lateinit var editor: K9StorageEditor
private val workingMap = mutableMapOf<String,String>() private val workingMap = mutableMapOf<String,String>()
private val storageMap = mapOf( private val storageMap = mapOf(
@ -31,7 +31,7 @@ class StorageEditorTest : K9RobolectricTest() {
MockitoAnnotations.initMocks(this) MockitoAnnotations.initMocks(this)
whenever(storage.all).thenReturn(storageMap) whenever(storage.all).thenReturn(storageMap)
editor = StorageEditor(storage, storagePersister) editor = K9StorageEditor(storage, storagePersister)
verify(storage).all verify(storage).all
} }
@ -180,4 +180,4 @@ class StorageEditorTest : K9RobolectricTest() {
verify(storage).replaceAll(eq(workingMap)) verify(storage).replaceAll(eq(workingMap))
} }
} }
} }