diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-12-09 16:01:34 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-12-09 16:01:34 +0300 |
commit | 12781a2ec9e0b8f0e8048a41d187176dbed4afc4 (patch) | |
tree | 0b73fc553aded3a628993f7d5e2063834816ffe1 /app/src/main | |
parent | 32e3f4bf5d414999af371b887691bc2f1a90ae3e (diff) |
SelectCardForWidgetActivity and ShareTargetActivity should not know anything about the repository layer
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main')
5 files changed, 95 insertions, 80 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java index 5056985fa..1aabeabdb 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java @@ -135,8 +135,8 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @NonNull protected List<Account> accountsList = new ArrayList<>(); - protected SyncManager syncManager; protected SharedPreferences sharedPreferences; + private SyncManager syncManager; private StackAdapter stackAdapter; long lastBoardId; @NonNull @@ -667,74 +667,67 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.archive_cards: { - final Stack stack = stackAdapter.getItem(binding.viewPager.getCurrentItem()); - final long stackLocalId = stack.getLocalId(); - observeOnce(syncManager.countCardsInStack(mainViewModel.getCurrentAccount().getId(), stackLocalId), MainActivity.this, (numberOfCards) -> { - new BrandedAlertDialogBuilder(this) - .setTitle(R.string.archive_cards) - .setMessage(getString(FilterInformation.hasActiveFilter(filterViewModel.getFilterInformation().getValue()) - ? R.string.do_you_want_to_archive_all_cards_of_the_filtered_list - : R.string.do_you_want_to_archive_all_cards_of_the_list, stack.getTitle())) - .setPositiveButton(R.string.simple_archive, (dialog, whichButton) -> { - final FilterInformation filterInformation = filterViewModel.getFilterInformation().getValue(); - final WrappedLiveData<Void> archiveStackLiveData = syncManager.archiveCardsInStack(mainViewModel.getCurrentAccount().getId(), stackLocalId, filterInformation == null ? new FilterInformation() : filterInformation); - observeOnce(archiveStackLiveData, this, (result) -> { - if (archiveStackLiveData.hasError() && !SyncManager.ignoreExceptionOnVoidError(archiveStackLiveData.getError())) { - ExceptionDialogFragment.newInstance(archiveStackLiveData.getError(), mainViewModel.getCurrentAccount()).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()); - } - }); - }) - .setNeutralButton(android.R.string.cancel, null) - .create() - .show(); - }); - return true; - } - case R.id.add_list: { - EditStackDialogFragment.newInstance(NO_STACK_ID).show(getSupportFragmentManager(), addList); - return true; - } - case R.id.rename_list: { - final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - observeOnce(syncManager.getStack(mainViewModel.getCurrentAccount().getId(), stackId), MainActivity.this, fullStack -> - EditStackDialogFragment.newInstance(fullStack.getLocalId(), fullStack.getStack().getTitle()) - .show(getSupportFragmentManager(), EditStackDialogFragment.class.getCanonicalName())); - return true; - } - case R.id.move_list_left: { - final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - // TODO error handling - final int stackLeftPosition = binding.viewPager.getCurrentItem() - 1; - final long stackLeftId = stackAdapter.getItem(stackLeftPosition).getLocalId(); - syncManager.swapStackOrder(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackLeftId)); - stackMoved = true; - return true; - } - case R.id.move_list_right: { - final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - // TODO error handling - final int stackRightPosition = binding.viewPager.getCurrentItem() + 1; - final long stackRightId = stackAdapter.getItem(stackRightPosition).getLocalId(); - syncManager.swapStackOrder(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackRightId)); - stackMoved = true; - return true; - } - case R.id.delete_list: { - final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - observeOnce(syncManager.countCardsInStack(mainViewModel.getCurrentAccount().getId(), stackId), MainActivity.this, (numberOfCards) -> { - if (numberOfCards != null && numberOfCards > 0) { - DeleteStackDialogFragment.newInstance(stackId, numberOfCards).show(getSupportFragmentManager(), DeleteStackDialogFragment.class.getCanonicalName()); - } else { - onStackDeleted(stackId); - } - }); - return true; - } - default: - return super.onOptionsItemSelected(item); + int itemId = item.getItemId(); + if (itemId == R.id.archive_cards) { + final Stack stack = stackAdapter.getItem(binding.viewPager.getCurrentItem()); + final long stackLocalId = stack.getLocalId(); + observeOnce(syncManager.countCardsInStack(mainViewModel.getCurrentAccount().getId(), stackLocalId), MainActivity.this, (numberOfCards) -> { + new BrandedAlertDialogBuilder(this) + .setTitle(R.string.archive_cards) + .setMessage(getString(FilterInformation.hasActiveFilter(filterViewModel.getFilterInformation().getValue()) + ? R.string.do_you_want_to_archive_all_cards_of_the_filtered_list + : R.string.do_you_want_to_archive_all_cards_of_the_list, stack.getTitle())) + .setPositiveButton(R.string.simple_archive, (dialog, whichButton) -> { + final FilterInformation filterInformation = filterViewModel.getFilterInformation().getValue(); + final WrappedLiveData<Void> archiveStackLiveData = syncManager.archiveCardsInStack(mainViewModel.getCurrentAccount().getId(), stackLocalId, filterInformation == null ? new FilterInformation() : filterInformation); + observeOnce(archiveStackLiveData, this, (result) -> { + if (archiveStackLiveData.hasError() && !SyncManager.ignoreExceptionOnVoidError(archiveStackLiveData.getError())) { + ExceptionDialogFragment.newInstance(archiveStackLiveData.getError(), mainViewModel.getCurrentAccount()).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()); + } + }); + }) + .setNeutralButton(android.R.string.cancel, null) + .create() + .show(); + }); + return true; + } else if (itemId == R.id.add_list) { + EditStackDialogFragment.newInstance(NO_STACK_ID).show(getSupportFragmentManager(), addList); + return true; + } else if (itemId == R.id.rename_list) { + final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); + observeOnce(syncManager.getStack(mainViewModel.getCurrentAccount().getId(), stackId), MainActivity.this, fullStack -> + EditStackDialogFragment.newInstance(fullStack.getLocalId(), fullStack.getStack().getTitle()) + .show(getSupportFragmentManager(), EditStackDialogFragment.class.getCanonicalName())); + return true; + } else if (itemId == R.id.move_list_left) { + final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); + // TODO error handling + final int stackLeftPosition = binding.viewPager.getCurrentItem() - 1; + final long stackLeftId = stackAdapter.getItem(stackLeftPosition).getLocalId(); + syncManager.swapStackOrder(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackLeftId)); + stackMoved = true; + return true; + } else if (itemId == R.id.move_list_right) { + final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); + // TODO error handling + final int stackRightPosition = binding.viewPager.getCurrentItem() + 1; + final long stackRightId = stackAdapter.getItem(stackRightPosition).getLocalId(); + syncManager.swapStackOrder(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackRightId)); + stackMoved = true; + return true; + } else if (itemId == R.id.delete_list) { + final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); + observeOnce(syncManager.countCardsInStack(mainViewModel.getCurrentAccount().getId(), stackId), MainActivity.this, (numberOfCards) -> { + if (numberOfCards != null && numberOfCards > 0) { + DeleteStackDialogFragment.newInstance(stackId, numberOfCards).show(getSupportFragmentManager(), DeleteStackDialogFragment.class.getCanonicalName()); + } else { + onStackDeleted(stackId); + } + }); + return true; } + return super.onOptionsItemSelected(item); } protected void showFabIfEditPermissionGranted() { 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 b73b86b33..fecbf78ab 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 @@ -8,13 +8,22 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; +import java.io.File; + import it.niedermann.nextcloud.deck.model.Account; +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.SyncManager; +import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; @SuppressWarnings("WeakerAccess") public class MainViewModel extends AndroidViewModel { - private MutableLiveData<Account> currentAccount = new MutableLiveData<>(); + private final SyncManager syncManager; + + private final MutableLiveData<Account> currentAccount = new MutableLiveData<>(); @Nullable private Board currentBoard; private boolean currentAccountHasArchivedBoards = false; @@ -23,6 +32,7 @@ public class MainViewModel extends AndroidViewModel { public MainViewModel(@NonNull Application application) { super(application); + this.syncManager = new SyncManager(application); } public Account getCurrentAccount() { @@ -43,14 +53,14 @@ public class MainViewModel extends AndroidViewModel { } public Long getCurrentBoardLocalId() { - if(currentBoard == null) { + if (currentBoard == null) { throw new IllegalStateException("getCurrentBoardLocalId() called before setCurrentBoard()"); } return this.currentBoard.getLocalId(); } public Long getCurrentBoardRemoteId() { - if(currentBoard == null) { + if (currentBoard == null) { throw new IllegalStateException("getCurrentBoardRemoteId() called before setCurrentBoard()"); } return this.currentBoard.getId(); @@ -71,4 +81,20 @@ public class MainViewModel extends AndroidViewModel { public boolean isCurrentAccountIsSupportedVersion() { return currentAccountIsSupportedVersion; } + + public WrappedLiveData<FullCard> updateCard(@NonNull FullCard fullCard) { + return syncManager.updateCard(fullCard); + } + + public void addCommentToCard(long accountId, long cardId, @NonNull DeckComment comment) { + syncManager.addCommentToCard(accountId, cardId, comment); + } + + public WrappedLiveData<Attachment> addAttachmentToCard(long accountId, long localCardId, @NonNull String mimeType, @NonNull File file) { + return syncManager.addAttachmentToCard(accountId, localCardId, mimeType, file); + } + + public void addOrUpdateSingleCardWidget(int widgetId, long accountId, long boardId, long localCardId) { + syncManager.addOrUpdateSingleCardWidget(widgetId, accountId, boardId, localCardId); + } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/PickStackActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/PickStackActivity.java index e0a9b6a06..e6766a0ab 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/PickStackActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/PickStackActivity.java @@ -89,11 +89,7 @@ public abstract class PickStackActivity extends AppCompatActivity implements Bra binding.submit.setEnabled(false); } else { applyBrand(board.getColor()); - if (stack == null) { - binding.submit.setEnabled(false); - } else { - binding.submit.setEnabled(true); - } + binding.submit.setEnabled(stack != null); } } 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 d5be7c04d..3a0005fb1 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 @@ -126,7 +126,7 @@ public class ShareTargetActivity extends MainActivity implements SelectCardListe throw new IllegalArgumentException("MimeType of uri is null. [" + uri + "]"); } runOnUiThread(() -> { - final WrappedLiveData<Attachment> liveData = syncManager.addAttachmentToCard(fullCard.getAccountId(), fullCard.getCard().getLocalId(), mimeType, tempFile); + final WrappedLiveData<Attachment> liveData = mainViewModel.addAttachmentToCard(fullCard.getAccountId(), fullCard.getCard().getLocalId(), mimeType, tempFile); liveData.observe(ShareTargetActivity.this, (next) -> { if (liveData.hasError()) { if (liveData.getError() instanceof NextcloudHttpRequestFailedException && ((NextcloudHttpRequestFailedException) liveData.getError()).getStatusCode() == HTTP_CONFLICT) { @@ -160,7 +160,7 @@ public class ShareTargetActivity extends MainActivity implements SelectCardListe ? receivedText : oldDescription + "\n\n" + receivedText ); - WrappedLiveData<FullCard> liveData = syncManager.updateCard(fullCard); + WrappedLiveData<FullCard> liveData = mainViewModel.updateCard(fullCard); observeOnce(liveData, this, (next) -> { if (liveData.hasError()) { cardSelected = false; @@ -174,7 +174,7 @@ public class ShareTargetActivity extends MainActivity implements SelectCardListe case 1: final Account currentAccount = mainViewModel.getCurrentAccount(); final DeckComment comment = new DeckComment(receivedText.trim(), currentAccount.getUserName(), Instant.now()); - syncManager.addCommentToCard(currentAccount.getId(), fullCard.getLocalId(), comment); + mainViewModel.addCommentToCard(currentAccount.getId(), fullCard.getLocalId(), comment); Toast.makeText(getApplicationContext(), getString(R.string.share_success, "\"" + receivedText + "\"", "\"" + fullCard.getCard().getTitle() + "\""), Toast.LENGTH_LONG).show(); finish(); break; diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SelectCardForWidgetActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SelectCardForWidgetActivity.java index 8c1fbe1fa..fe6e969e7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SelectCardForWidgetActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SelectCardForWidgetActivity.java @@ -45,7 +45,7 @@ public class SelectCardForWidgetActivity extends MainActivity implements SelectC @Override public void onCardSelected(FullCard fullCard) { - syncManager.addOrUpdateSingleCardWidget(appWidgetId, mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), fullCard.getLocalId()); + mainViewModel.addOrUpdateSingleCardWidget(appWidgetId, mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), fullCard.getLocalId()); final Intent updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, getApplicationContext(), SingleCardWidget.class) .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |