diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-04-03 13:21:16 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-04-03 13:21:16 +0300 |
commit | 31e4b9677e401329237485b2d2fb5ceb26803abd (patch) | |
tree | 520d6312bf19fb15424e3540ebc83cee3d1616f1 /app/src/main | |
parent | efe7a2b7232063ea8e1935f9357f0774dd5acd51 (diff) |
#896 Replace observeOnce calls with ResponseCallback when it makes sense
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main')
5 files changed, 32 insertions, 37 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java index 036d74cb4..a8bdae45c 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java @@ -991,8 +991,7 @@ public class SyncManager { // } @AnyThread - public WrappedLiveData<FullCard> createFullCard(long accountId, long localBoardId, long localStackId, @NonNull FullCard card) { - WrappedLiveData<FullCard> liveData = new WrappedLiveData<>(); + public void createFullCard(long accountId, long localBoardId, long localStackId, @NonNull FullCard card, @NonNull ResponseCallback<FullCard> callback) { doAsync(() -> { Account account = dataBaseAdapter.getAccountByIdDirectly(accountId); User owner = dataBaseAdapter.getUserByUidDirectly(accountId, account.getUserName()); @@ -1034,25 +1033,24 @@ public class SyncManager { .setResponseCallback(new IResponseCallback<Boolean>(account) { @Override public void onResponse(Boolean response) { - liveData.postValue(card); + callback.onResponse(card); } @SuppressLint("MissingSuperCall") @Override public void onError(Throwable throwable) { if (throwable.getClass() == DeckException.class && ((DeckException) throwable).getHint().equals(DeckException.Hint.DEPENDENCY_NOT_SYNCED_YET)) { - liveData.postValue(card); + callback.onResponse(card); } else { - liveData.postError(throwable); + callback.onError(throwable); } } }) .doUpSyncFor(new CardDataProvider(null, board, stack)); } else { - liveData.postValue(card); + callback.onResponse(card); } }); - return liveData; } @AnyThread @@ -1163,10 +1161,9 @@ public class SyncManager { } @AnyThread - public WrappedLiveData<FullCard> updateCard(@NonNull FullCard card) { - WrappedLiveData<FullCard> liveData = new WrappedLiveData<>(); + public void updateCard(@NonNull FullCard card, @NonNull ResponseCallback<FullCard> callback) { doAsync(() -> { - FullCard fullCardFromDB = dataBaseAdapter.getFullCardByLocalIdDirectly(card.getAccountId(), card.getLocalId()); + final FullCard fullCardFromDB = dataBaseAdapter.getFullCardByLocalIdDirectly(card.getAccountId(), card.getLocalId()); if (fullCardFromDB == null) { throw new IllegalArgumentException("card to update does not exist."); } @@ -1201,20 +1198,19 @@ public class SyncManager { .setResponseCallback(new IResponseCallback<Boolean>(account) { @Override public void onResponse(Boolean response) { - liveData.postValue(dataBaseAdapter.getFullCardByLocalIdDirectly(card.getAccountId(), card.getLocalId())); + callback.onResponse(dataBaseAdapter.getFullCardByLocalIdDirectly(card.getAccountId(), card.getLocalId())); } @SuppressLint("MissingSuperCall") @Override public void onError(Throwable throwable) { - liveData.postError(throwable); + callback.onError(throwable); } }).doUpSyncFor(new CardPropagationDataProvider(null, board, stack)); } else { - liveData.postValue(card); + callback.onResponse(card); } }); - return liveData; } /** diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainViewModel.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainViewModel.java index 53443d07c..b074dab85 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainViewModel.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainViewModel.java @@ -239,8 +239,8 @@ public class MainViewModel extends AndroidViewModel { syncManager.archiveCardsInStack(accountId, stackLocalId, filterInformation, callback); } - public WrappedLiveData<FullCard> updateCard(@NonNull FullCard fullCard) { - return syncManager.updateCard(fullCard); + public void updateCard(@NonNull FullCard fullCard, @NonNull ResponseCallback<FullCard> callback) { + syncManager.updateCard(fullCard, callback); } public void addCommentToCard(long accountId, long cardId, @NonNull DeckComment comment) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditActivity.java index ba51948dd..4ecd914fe 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/EditActivity.java @@ -30,7 +30,6 @@ import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.Card; import it.niedermann.nextcloud.deck.model.full.FullCard; import it.niedermann.nextcloud.deck.model.ocs.Version; -import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; import it.niedermann.nextcloud.deck.ui.branding.BrandedAlertDialogBuilder; import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler; import it.niedermann.nextcloud.deck.util.CardUtil; @@ -233,12 +232,7 @@ public class EditActivity extends AppCompatActivity { .setOnDismissListener(dialog -> viewModel.setPendingCreation(false)) .show(); } else { - final WrappedLiveData<FullCard> save$ = viewModel.saveCard(); - save$.observe(this, (fullCard) -> { - if (save$.hasError()) { - DeckLog.logError(save$.getError()); - } - }); + viewModel.saveCard(response -> DeckLog.info("Successfully saved card", response.getCard().getTitle())); super.finish(); } } 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 6bbd94afb..19562138a 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 @@ -26,7 +26,6 @@ import it.niedermann.nextcloud.deck.model.full.FullCard; import it.niedermann.nextcloud.deck.model.full.FullCardWithProjects; import it.niedermann.nextcloud.deck.model.ocs.Activity; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; -import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; import static androidx.lifecycle.Transformations.distinctUntilChanged; @@ -158,10 +157,12 @@ public class EditCardViewModel extends AndroidViewModel { /** * Saves the current {@link #fullCard}. If it is a new card, it will be created, otherwise it will be updated. */ - public WrappedLiveData<FullCard> saveCard() { - return isCreateMode() - ? syncManager.createFullCard(getAccount().getId(), getBoardId(), getFullCard().getCard().getStackId(), getFullCard()) - : syncManager.updateCard(getFullCard()); + public void saveCard(@NonNull ResponseCallback<FullCard> callback) { + if (isCreateMode()) { + syncManager.createFullCard(getAccount().getId(), getBoardId(), getFullCard().getCard().getStackId(), getFullCard(), callback); + } else { + syncManager.updateCard(getFullCard(), callback); + } } public LiveData<List<Activity>> syncActivitiesForCard(@NonNull Card card) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/sharetarget/ShareTargetActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/sharetarget/ShareTargetActivity.java index 21aafed30..332c3f194 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/sharetarget/ShareTargetActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/sharetarget/ShareTargetActivity.java @@ -30,14 +30,12 @@ import it.niedermann.nextcloud.deck.model.Attachment; import it.niedermann.nextcloud.deck.model.Board; import it.niedermann.nextcloud.deck.model.full.FullCard; import it.niedermann.nextcloud.deck.model.ocs.comment.DeckComment; -import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; import it.niedermann.nextcloud.deck.ui.MainActivity; import it.niedermann.nextcloud.deck.ui.branding.BrandedAlertDialogBuilder; import it.niedermann.nextcloud.deck.ui.card.SelectCardListener; import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment; import it.niedermann.nextcloud.deck.util.MimeTypeUtil; -import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce; import static it.niedermann.nextcloud.deck.util.FilesUtil.copyContentUriToTempFile; import static java.net.HttpURLConnection.HTTP_CONFLICT; @@ -165,14 +163,20 @@ public class ShareTargetActivity extends MainActivity implements SelectCardListe ? receivedText : oldDescription + "\n\n" + receivedText ); - WrappedLiveData<FullCard> liveData = mainViewModel.updateCard(fullCard); - observeOnce(liveData, this, (next) -> { - if (liveData.hasError()) { - cardSelected = false; - ExceptionDialogFragment.newInstance(liveData.getError(), mainViewModel.getCurrentAccount()).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()); - } else { + mainViewModel.updateCard(fullCard, new ResponseCallback<FullCard>() { + @Override + public void onResponse(FullCard response) { Toast.makeText(getApplicationContext(), getString(R.string.share_success, "\"" + receivedText + "\"", "\"" + fullCard.getCard().getTitle() + "\""), Toast.LENGTH_LONG).show(); - finish(); + runOnUiThread(() -> finish()); + } + + @Override + public void onError(Throwable throwable) { + ResponseCallback.super.onError(throwable); + runOnUiThread(() -> { + cardSelected = false; + ExceptionDialogFragment.newInstance(throwable, mainViewModel.getCurrentAccount()).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()); + }); } }); break; |