Fix #1254 🐞 Change grid view option not always applied directly

Signed-off-by: Stefan Niedermann <info@niedermann.it>
This commit is contained in:
Stefan Niedermann 2021-06-24 23:58:03 +02:00
parent 632804ce49
commit 0925290ec5
4 changed files with 24 additions and 13 deletions

View file

@ -3,28 +3,28 @@ package it.niedermann.owncloud.notes.preferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import it.niedermann.owncloud.notes.LockedActivity;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ActivityPreferencesBinding;
/**
* Allows to change application settings.
*/
public class PreferencesActivity extends LockedActivity {
private PreferencesViewModel viewModel;
private ActivityPreferencesBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = new ViewModelProvider(this).get(PreferencesViewModel.class);
viewModel.resultCode$.observe(this, this::setResult);
binding = ActivityPreferencesBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
setResult(RESULT_CANCELED);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_view, new PreferencesFragment())
.commit();

View file

@ -8,6 +8,7 @@ import android.util.Log;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
@ -24,6 +25,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
private static final String TAG = PreferencesFragment.class.getSimpleName();
private PreferencesViewModel viewModel;
private BrandedSwitchPreference fontPref;
private BrandedSwitchPreference lockPref;
private BrandedSwitchPreference wifiOnlyPref;
@ -31,15 +34,12 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
private BrandedSwitchPreference preventScreenCapturePref;
private BrandedSwitchPreference backgroundSyncPref;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
viewModel = new ViewModelProvider(requireActivity()).get(PreferencesViewModel.class);
fontPref = findPreference(getString(R.string.pref_key_font));
gridViewPref = findPreference(getString(R.string.pref_key_gridview));
@ -47,7 +47,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
gridViewPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> {
final Boolean gridView = (Boolean) newValue;
Log.v(TAG, "gridView: " + gridView);
requireActivity().setResult(Activity.RESULT_OK);
viewModel.resultCode$.setValue(Activity.RESULT_OK);
NotesApplication.updateGridViewEnabled(gridView);
return true;
});
@ -78,7 +78,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
assert themePref != null;
themePref.setOnPreferenceChangeListener((preference, newValue) -> {
NotesApplication.setAppTheme(DarkModeSetting.valueOf((String) newValue));
requireActivity().setResult(Activity.RESULT_OK);
viewModel.resultCode$.setValue(Activity.RESULT_OK);
ActivityCompat.recreate(requireActivity());
return true;
});
@ -114,6 +114,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
/**
* Change color for backgroundSyncPref as well
* https://github.com/stefan-niedermann/nextcloud-deck/issues/531
*
* @param mainColor color of main brand
* @param textColor color of text
*/

View file

@ -0,0 +1,9 @@
package it.niedermann.owncloud.notes.preferences;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class PreferencesViewModel extends ViewModel {
public final MutableLiveData<Integer> resultCode$ = new MutableLiveData<>();
}

View file

@ -1,2 +1,3 @@
- ✅ Checkboxes using an uppercase X can not be toggled (#1276)
- 🐞 Search field does not immediately focus (#1282)
- 🐞 Change grid view option not always applied directly (#1254)