Use ArbitraryDataProvider to store backup preferences

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-08-19 12:58:42 +02:00
parent 7a1e8189da
commit 68c7eddf46
No known key found for this signature in database
GPG key ID: 2585783189A62105
4 changed files with 12 additions and 44 deletions

View file

@ -20,7 +20,6 @@
package com.nextcloud.client.preferences;
import com.nextcloud.client.account.User;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.utils.FileSortOrder;
@ -378,10 +377,4 @@ public interface AppPreferences {
boolean isStoragePermissionRequested();
void setStoragePermissionRequested(boolean value);
boolean isContactsBackupEnabled(final User user);
void setContactsBackupEnabled(final User user, final boolean value);
boolean isCalendarBackupEnabled(final User user);
void setCalendarBackupEnabled(final User user, final boolean value);
}

View file

@ -24,7 +24,6 @@ package com.nextcloud.client.preferences;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import com.nextcloud.client.account.CurrentAccountProvider;
import com.nextcloud.client.account.User;
@ -100,9 +99,6 @@ public final class AppPreferencesImpl implements AppPreferences {
private static final String PREF__STORAGE_PERMISSION_REQUESTED = "storage_permission_requested";
private static final String PREF__CALENDAR_BACKUP_ENABLED = "calendar_backup_enabled";
private static final String PREF__CONTACTS_BACKUP_ENABLED = "contacts_backup_enabled";
private final Context context;
private final SharedPreferences preferences;
private final CurrentAccountProvider currentAccountProvider;
@ -168,10 +164,6 @@ public final class AppPreferencesImpl implements AppPreferences {
this.preferences.registerOnSharedPreferenceChangeListener(listeners);
}
private String getUserScopedKey(final User user, final String originalKey) {
return Uri.encode(user.getAccountName()) + "__" + originalKey;
}
@Override
public void addListener(@Nullable AppPreferences.Listener listener) {
this.listeners.add(listener);
@ -716,30 +708,6 @@ public final class AppPreferencesImpl implements AppPreferences {
preferences.edit().putBoolean(PREF__STORAGE_PERMISSION_REQUESTED, value).apply();
}
@Override
public boolean isContactsBackupEnabled(final User user) {
final String key = getUserScopedKey(user, PREF__CONTACTS_BACKUP_ENABLED);
return preferences.getBoolean(key, false);
}
@Override
public void setContactsBackupEnabled(final User user, boolean value) {
final String key = getUserScopedKey(user, PREF__CONTACTS_BACKUP_ENABLED);
preferences.edit().putBoolean(key, value).apply();
}
@Override
public boolean isCalendarBackupEnabled(final User user) {
final String key = getUserScopedKey(user, PREF__CALENDAR_BACKUP_ENABLED);
return preferences.getBoolean(key, false);
}
@Override
public void setCalendarBackupEnabled(final User user, boolean value) {
final String key = getUserScopedKey(user, PREF__CALENDAR_BACKUP_ENABLED);
preferences.edit().putBoolean(key, value).apply();
}
@VisibleForTesting
public int computeBruteForceDelay(int count) {
return (int) Math.min(count / 3d, 10);

View file

@ -65,6 +65,10 @@ public class ArbitraryDataProvider {
storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
}
public void storeOrUpdateKeyValue(final String accountName, final String key, final boolean newValue) {
storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
}
public void storeOrUpdateKeyValue(@NonNull String accountName,
@NonNull String key,
@Nullable String newValue) {

View file

@ -84,6 +84,10 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
private static final String KEY_CALENDAR_MONTH = "CALENDAR_MONTH";
private static final String KEY_CALENDAR_YEAR = "CALENDAR_YEAR";
public static final String PREFERENCE_CONTACTS_BACKUP_ENABLED = "PREFERENCE_CONTACTS_BACKUP_ENABLED";
public static final String PREFERENCE_CALENDAR_BACKUP_ENABLED = "PREFERENCE_CALENDAR_BACKUP_ENABLED";
private BackupFragmentBinding binding;
@Inject BackgroundJobManager backgroundJobManager;
@ -93,7 +97,6 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
@Inject ThemeCheckableUtils themeCheckableUtils;
@Inject ThemeButtonUtils themeButtonUtils;
@Inject ArbitraryDataProvider arbitraryDataProvider;
@Inject AppPreferences appPreferences;
private Date selectedDate;
private boolean calendarPickerOpen;
@ -115,19 +118,19 @@ public class BackupFragment extends FileFragment implements DatePickerDialog.OnD
}
private boolean isCalendarBackupEnabled() {
return appPreferences.isCalendarBackupEnabled(user);
return arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CALENDAR_BACKUP_ENABLED);
}
private void setCalendarBackupEnabled(final boolean enabled) {
appPreferences.setCalendarBackupEnabled(user, enabled);
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), PREFERENCE_CALENDAR_BACKUP_ENABLED, enabled);
}
private boolean isContactsBackupEnabled() {
return appPreferences.isContactsBackupEnabled(user);
return arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CONTACTS_BACKUP_ENABLED);
}
private void setContactsBackupEnabled(final boolean enabled) {
appPreferences.setContactsBackupEnabled(user, enabled);
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), PREFERENCE_CONTACTS_BACKUP_ENABLED, enabled);
}
@Override