Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Niedermann <info@niedermann.it>2023-03-01 14:43:53 +0300
committerStefan Niedermann <info@niedermann.it>2023-03-09 11:53:19 +0300
commit3ea462ca9e2ae18ba9d869125da8d8d07f2c7854 (patch)
tree30257c67768325d5972ec499a6eb41e11017ac6d /app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java
parentbfab286b0bc6dbfac1211eec64d74b66b2ce1e6d (diff)
refactor: Unidirectional data flow and single point of truth for current state
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java')
-rw-r--r--app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java30
1 files changed, 18 insertions, 12 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java
index cc01781e6..79a03ab71 100644
--- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java
+++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java
@@ -1,7 +1,5 @@
package it.niedermann.nextcloud.deck.ui.settings;
-import static it.niedermann.nextcloud.deck.DeckApplication.setAppTheme;
-
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
@@ -9,10 +7,13 @@ import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
+import androidx.lifecycle.ViewModelProvider;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
-import it.niedermann.nextcloud.deck.DeckApplication;
+import java.util.stream.Stream;
+
+import it.niedermann.android.reactivelivedata.ReactiveLiveData;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.persistence.sync.SyncWorker;
@@ -20,6 +21,7 @@ import it.niedermann.nextcloud.deck.ui.theme.ThemedSwitchPreference;
public class SettingsFragment extends PreferenceFragmentCompat {
+ private PreferencesViewModel preferencesViewModel;
private ThemedSwitchPreference wifiOnlyPref;
private ThemedSwitchPreference compactPref;
private ThemedSwitchPreference coverImagesPref;
@@ -31,6 +33,8 @@ public class SettingsFragment extends PreferenceFragmentCompat {
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings, rootKey);
+ preferencesViewModel = new ViewModelProvider(requireActivity()).get(PreferencesViewModel.class);
+
wifiOnlyPref = findPreference(getString(R.string.pref_key_wifi_only));
coverImagesPref = findPreference(getString(R.string.pref_key_cover_images));
compactPref = findPreference(getString(R.string.pref_key_compact));
@@ -61,7 +65,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
final var themePref = findPreference(getString(R.string.pref_key_dark_theme));
if (themePref != null) {
themePref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> {
- setAppTheme(Integer.parseInt((String) newValue));
+ preferencesViewModel.setAppTheme(Integer.parseInt((String) newValue));
requireActivity().setResult(Activity.RESULT_OK);
ActivityCompat.recreate(requireActivity());
return true;
@@ -75,13 +79,15 @@ public class SettingsFragment extends PreferenceFragmentCompat {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
- DeckApplication.readCurrentAccountColor().observe(getViewLifecycleOwner(), (mainColor) -> {
- wifiOnlyPref.applyTheme(mainColor);
- compactPref.applyTheme(mainColor);
- coverImagesPref.applyTheme(mainColor);
- compressImageAttachmentsPref.applyTheme(mainColor);
- debuggingPref.applyTheme(mainColor);
- eTagPref.applyTheme(mainColor);
- });
+ new ReactiveLiveData<>(preferencesViewModel.getCurrentAccountId$())
+ .flatMap(preferencesViewModel::getAccountColor)
+ .observe(getViewLifecycleOwner(), color -> Stream.of(
+ wifiOnlyPref,
+ compactPref,
+ coverImagesPref,
+ compressImageAttachmentsPref,
+ debuggingPref,
+ eTagPref)
+ .forEach(pref -> pref.applyTheme(color)));
}
}