diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-05-26 20:00:51 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-05-26 20:00:51 +0300 |
commit | d38be55c974632247364663a610583ac0d65af74 (patch) | |
tree | 69577297b9043f330ee79312dd739a83aa25fa2b /app/src/main/java/it/niedermann/nextcloud | |
parent | a2f85c61808a03b97741d629b9c87c2a0f61c8ea (diff) |
#971 Refactoring
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditCardViewModel.java | 31 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/details/CardDetailsFragment.java | 7 |
2 files changed, 18 insertions, 20 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 fdf1fe745..c55cfb1db 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 @@ -2,6 +2,7 @@ package it.niedermann.nextcloud.deck.ui.card; import android.app.Application; import android.content.SharedPreferences; +import android.text.TextUtils; import androidx.annotation.ColorInt; import androidx.annotation.NonNull; @@ -57,21 +58,28 @@ public class EditCardViewModel extends AndroidViewModel { sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application); } - public LiveData<Boolean> descriptionIsPreviewMode() { + /** + * The result {@link LiveData} will emit <code>true</code> if the preview mode is enabled and <code>false</code> if the edit mode is enabled. + * In {@link #createMode} it will not emit the last persisted state but only a temporary value. + */ + public LiveData<Boolean> getDescriptionMode() { if (isCreateMode()) { return distinctUntilChanged(descriptionIsPreview); } else { - return distinctUntilChanged( - switchMap(distinctUntilChanged( - new SharedPreferenceBooleanLiveData(sharedPreferences, getApplication().getString(R.string.shared_preference_description_preview), false)), (isPreview) -> { - this.descriptionIsPreview.setValue(isPreview); - return descriptionIsPreview; - })); + return distinctUntilChanged(switchMap(distinctUntilChanged(new SharedPreferenceBooleanLiveData(sharedPreferences, getApplication().getString(R.string.shared_preference_description_preview), false)), (isPreview) -> { + // When we are in preview mode but the description of the card is empty, we explicitly switch to the edit mode + if (isPreview && TextUtils.isEmpty(getFullCard().getCard().getDescription())) { + descriptionIsPreview.setValue(false); + } else { + descriptionIsPreview.setValue(isPreview); + } + return descriptionIsPreview; + })); } } /** - * Will set toggle description mode and persist the new state if not in {@link #createMode}. + * Will toggle the edit / preview mode and persist the new state if not in {@link #createMode}. */ public void toggleDescriptionPreviewMode() { final boolean newValue = Boolean.FALSE.equals(descriptionIsPreview.getValue()); @@ -84,13 +92,6 @@ 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 f8547b046..b3bb6bd53 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 @@ -131,11 +131,8 @@ public class CardDetailsFragment extends Fragment implements OnDateSetListener, private void setupDescription() { if (viewModel.canEdit()) { binding.descriptionViewer.setMovementMethod(LinkMovementMethod.getInstance()); - viewModel.descriptionIsPreviewMode().observe(getViewLifecycleOwner(), (isPreview) -> { - if (isPreview) { - if (TextUtils.isEmpty(viewModel.getFullCard().getCard().getDescription())) { - viewModel.setDescriptionIsEditMode(); - } + viewModel.getDescriptionMode().observe(getViewLifecycleOwner(), (isPreviewMode) -> { + if (isPreviewMode) { toggleEditorView(binding.descriptionViewer, binding.descriptionEditorWrapper, binding.descriptionViewer); binding.descriptionToggle.setImageResource(R.drawable.ic_edit_grey600_24dp); } else { |