diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-05-26 19:43:44 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-05-26 19:43:44 +0300 |
commit | a2f85c61808a03b97741d629b9c87c2a0f61c8ea (patch) | |
tree | 5a8bd6311a39d77a93f09979e15db9bcf1fd96e2 | |
parent | 3977c1336c68d9960079907645b09fb66db865c5 (diff) |
#971 Remember toggled state of empty description card only after toggling at least once
Signed-off-by: Stefan Niedermann <info@niedermann.it>
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditCardViewModel.java | 29 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java | 2 |
2 files changed, 21 insertions, 10 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditCardViewModel.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditCardViewModel.java index d94398215..fdf1fe745 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditCardViewModel.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditCardViewModel.java @@ -31,7 +31,7 @@ import it.niedermann.nextcloud.deck.model.ocs.Activity; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import static androidx.lifecycle.Transformations.distinctUntilChanged; -import static androidx.lifecycle.Transformations.map; +import static androidx.lifecycle.Transformations.switchMap; @SuppressWarnings("WeakerAccess") public class EditCardViewModel extends AndroidViewModel { @@ -59,20 +59,24 @@ public class EditCardViewModel extends AndroidViewModel { public LiveData<Boolean> descriptionIsPreviewMode() { if (isCreateMode()) { - return descriptionIsPreview; + return distinctUntilChanged(descriptionIsPreview); } else { - return map(distinctUntilChanged(new SharedPreferenceBooleanLiveData(sharedPreferences, getApplication().getString(R.string.shared_preference_description_preview), false)), (isPreview) -> { - this.descriptionIsPreview.setValue(isPreview); - return isPreview; - }); + return distinctUntilChanged( + switchMap(distinctUntilChanged( + new SharedPreferenceBooleanLiveData(sharedPreferences, getApplication().getString(R.string.shared_preference_description_preview), false)), (isPreview) -> { + this.descriptionIsPreview.setValue(isPreview); + return descriptionIsPreview; + })); } } + /** + * Will set toggle description mode and persist the new state if not in {@link #createMode}. + */ public void toggleDescriptionPreviewMode() { final boolean newValue = Boolean.FALSE.equals(descriptionIsPreview.getValue()); - if (isCreateMode()) { - descriptionIsPreview.setValue(newValue); - } else { + descriptionIsPreview.setValue(newValue); + if (!isCreateMode()) { sharedPreferences .edit() .putBoolean(getApplication().getString(R.string.shared_preference_description_preview), newValue) @@ -80,6 +84,13 @@ public class EditCardViewModel extends AndroidViewModel { } } + /** + * This will <strong>not</strong> persist but only set the description mode to edit once. + */ + public void setDescriptionIsEditMode() { + descriptionIsPreview.setValue(false); + } + public LiveData<Integer> getBrandingColor() { return distinctUntilChanged(this.brandingColor$); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java index 8a51967bd..f8547b046 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java @@ -134,7 +134,7 @@ public class CardDetailsFragment extends Fragment implements OnDateSetListener, viewModel.descriptionIsPreviewMode().observe(getViewLifecycleOwner(), (isPreview) -> { if (isPreview) { if (TextUtils.isEmpty(viewModel.getFullCard().getCard().getDescription())) { - binding.getRoot().post(() -> viewModel.toggleDescriptionPreviewMode()); + viewModel.setDescriptionIsEditMode(); } toggleEditorView(binding.descriptionViewer, binding.descriptionEditorWrapper, binding.descriptionViewer); binding.descriptionToggle.setImageResource(R.drawable.ic_edit_grey600_24dp); |