diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-05-05 11:08:44 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-05-05 11:08:44 +0300 |
commit | 83fe12cbb24bdeada4ef1b88a8b4cd5eae8664ac (patch) | |
tree | 64490c216e2caf9cff5424bf3998350b0dafcc0d /app/src/main/java/it/niedermann/nextcloud/deck | |
parent | 960935ebd07e717158c89ab5ccaa9180020665bc (diff) |
Move filter stuff to own ViewModel
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck')
8 files changed, 156 insertions, 138 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 cec0e3cef..c52ed0de1 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 @@ -82,6 +82,7 @@ import it.niedermann.nextcloud.deck.ui.card.EditActivity; import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment; import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler; import it.niedermann.nextcloud.deck.ui.filter.FilterDialogFragment; +import it.niedermann.nextcloud.deck.ui.filter.FilterViewModel; import it.niedermann.nextcloud.deck.ui.settings.SettingsActivity; import it.niedermann.nextcloud.deck.ui.stack.DeleteStackDialogFragment; import it.niedermann.nextcloud.deck.ui.stack.DeleteStackListener; @@ -112,7 +113,8 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener protected ActivityMainBinding binding; protected NavHeaderMainBinding headerBinding; - private MainViewModel viewModel; + private MainViewModel mainViewModel; + private FilterViewModel filterViewModel; protected static final int ACTIVITY_ABOUT = 1; protected static final int ACTIVITY_SETTINGS = 2; @@ -155,8 +157,9 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener headerBinding = NavHeaderMainBinding.bind(binding.navigationView.getHeaderView(0)); setContentView(binding.getRoot()); - viewModel = new ViewModelProvider(this).get(MainViewModel.class); - viewModel.getFilterInformation().observe(this, (info) -> invalidateOptionsMenu()); + mainViewModel = new ViewModelProvider(this).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(this).get(FilterViewModel.class); + filterViewModel.getFilterInformation().observe(this, (info) -> invalidateOptionsMenu()); addList = getString(R.string.add_list); addBoard = getString(R.string.add_board); @@ -200,7 +203,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { registerAutoSyncOnNetworkAvailable(); } else { - syncManager.synchronize(new IResponseCallback<Boolean>(viewModel.getCurrentAccount()) { + syncManager.synchronize(new IResponseCallback<Boolean>(mainViewModel.getCurrentAccount()) { @Override public void onResponse(Boolean response) { } @@ -234,7 +237,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener CrossTabDragAndDrop<StackFragment, CardAdapter, FullCard> dragAndDrop = new CrossTabDragAndDrop<>(getResources()); dragAndDrop.register(binding.viewPager, binding.stackTitles, getSupportFragmentManager()); dragAndDrop.addItemMovedByDragListener((movedCard, stackId, position) -> { - syncManager.reorder(viewModel.getCurrentAccount().getId(), movedCard, stackId, position); + syncManager.reorder(mainViewModel.getCurrentAccount().getId(), movedCard, stackId, position); DeckLog.info("Card \"" + movedCard.getCard().getTitle() + "\" was moved to Stack " + stackId + " on position " + position); }); @@ -250,7 +253,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener if (this.boardsList.size() > 0) { try { Long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - startActivity(EditActivity.createNewCardIntent(this, viewModel.getCurrentAccount(), viewModel.getCurrentBoardLocalId(), stackId)); + startActivity(EditActivity.createNewCardIntent(this, mainViewModel.getCurrentAccount(), mainViewModel.getCurrentBoardLocalId(), stackId)); } catch (IndexOutOfBoundsException e) { EditStackDialogFragment.newInstance(NO_STACK_ID).show(getSupportFragmentManager(), addList); } @@ -269,7 +272,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener binding.viewPager.post(() -> { // stackAdapter size might differ from position when an account has been deleted if (stackAdapter.getItemCount() > position) { - Application.saveCurrentStackId(getApplicationContext(), viewModel.getCurrentAccount().getId(), viewModel.getCurrentBoardLocalId(), stackAdapter.getItem(position).getLocalId()); + Application.saveCurrentStackId(getApplicationContext(), mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), stackAdapter.getItem(position).getLocalId()); } else { DeckLog.logError(new IllegalStateException("Tried to save current Stack which cannot be available (stackAdapter doesn't have this position)")); } @@ -299,8 +302,8 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener DeckLog.info("Do not clear Glide caches, because the user currently does not have a working internet connection"); } } else DeckLog.warn("ConnectivityManager is null"); - refreshCapabilities(viewModel.getCurrentAccount()); - syncManager.synchronize(new IResponseCallback<Boolean>(viewModel.getCurrentAccount()) { + refreshCapabilities(mainViewModel.getCurrentAccount()); + syncManager.synchronize(new IResponseCallback<Boolean>(mainViewModel.getCurrentAccount()) { @Override public void onResponse(Boolean response) { runOnUiThread(() -> binding.swipeRefreshLayout.setRefreshing(false)); @@ -361,8 +364,8 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public void onCreateStack(String stackName) { // TODO this outer call is only necessary to get the highest order. Move logic to SyncManager. - observeOnce(syncManager.getStacksForBoard(viewModel.getCurrentAccount().getId(), viewModel.getCurrentBoardLocalId()), MainActivity.this, fullStacks -> { - final Stack s = new Stack(stackName, viewModel.getCurrentBoardLocalId()); + observeOnce(syncManager.getStacksForBoard(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), MainActivity.this, fullStacks -> { + final Stack s = new Stack(stackName, mainViewModel.getCurrentBoardLocalId()); int heighestOrder = 0; for (FullStack fullStack : fullStacks) { int currentStackOrder = fullStack.stack.getOrder(); @@ -371,8 +374,8 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener } } s.setOrder(heighestOrder); - DeckLog.info("Create Stack in account " + viewModel.getCurrentAccount().getName() + " on board " + viewModel.getCurrentBoardLocalId()); - WrappedLiveData<FullStack> createLiveData = syncManager.createStack(viewModel.getCurrentAccount().getId(), s); + DeckLog.info("Create Stack in account " + mainViewModel.getCurrentAccount().getName() + " on board " + mainViewModel.getCurrentBoardLocalId()); + WrappedLiveData<FullStack> createLiveData = syncManager.createStack(mainViewModel.getCurrentAccount().getId(), s); observeOnce(createLiveData, this, (fullStack) -> { if (createLiveData.hasError()) { final Throwable error = createLiveData.getError(); @@ -387,7 +390,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public void onUpdateStack(long localStackId, String stackName) { - observeOnce(syncManager.getStack(viewModel.getCurrentAccount().getId(), localStackId), MainActivity.this, fullStack -> { + observeOnce(syncManager.getStack(mainViewModel.getCurrentAccount().getId(), localStackId), MainActivity.this, fullStack -> { fullStack.getStack().setTitle(stackName); // TODO error handling syncManager.updateStack(fullStack); @@ -403,7 +406,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener final Board boardToCreate = new Board(title, color.startsWith("#") ? color.substring(1) : color); boardToCreate.setPermissionEdit(true); boardToCreate.setPermissionManage(true); - observeOnce(syncManager.createBoard(viewModel.getCurrentAccount().getId(), boardToCreate), this, createdBoard -> { + observeOnce(syncManager.createBoard(mainViewModel.getCurrentAccount().getId(), boardToCreate), this, createdBoard -> { if (createdBoard == null) { Snackbar.make(binding.coordinatorLayout, "Open Deck in web interface first!", Snackbar.LENGTH_LONG).show(); } else { @@ -424,17 +427,17 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @UiThread protected void setCurrentAccount(@NonNull Account account) { - viewModel.setCurrentAccount(account); - SingleAccountHelper.setCurrentAccount(getApplicationContext(), viewModel.getCurrentAccount().getName()); + mainViewModel.setCurrentAccount(account); + SingleAccountHelper.setCurrentAccount(getApplicationContext(), mainViewModel.getCurrentAccount().getName()); syncManager = new SyncManager(this); - Application.saveBrandColors(this, Color.parseColor(viewModel.getCurrentAccount().getColor()), Color.parseColor(viewModel.getCurrentAccount().getTextColor())); - Application.saveCurrentAccountId(this, viewModel.getCurrentAccount().getId()); - if (viewModel.getCurrentAccount().isMaintenanceEnabled()) { - refreshCapabilities(viewModel.getCurrentAccount()); + Application.saveBrandColors(this, Color.parseColor(mainViewModel.getCurrentAccount().getColor()), Color.parseColor(mainViewModel.getCurrentAccount().getTextColor())); + Application.saveCurrentAccountId(this, mainViewModel.getCurrentAccount().getId()); + if (mainViewModel.getCurrentAccount().isMaintenanceEnabled()) { + refreshCapabilities(mainViewModel.getCurrentAccount()); } - lastBoardId = Application.readCurrentBoardId(this, viewModel.getCurrentAccount().getId()); + lastBoardId = Application.readCurrentBoardId(this, mainViewModel.getCurrentAccount().getId()); if (boardsLiveData != null && boardsLiveDataObserver != null) { boardsLiveData.removeObserver(boardsLiveDataObserver); @@ -467,13 +470,13 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener }; boardsLiveData.observe(this, boardsLiveDataObserver); - ViewUtil.addAvatar(headerBinding.drawerCurrentAccount, viewModel.getCurrentAccount().getUrl(), viewModel.getCurrentAccount().getUserName(), R.mipmap.ic_launcher_round); - headerBinding.drawerUsernameFull.setText(viewModel.getCurrentAccount().getName()); + ViewUtil.addAvatar(headerBinding.drawerCurrentAccount, mainViewModel.getCurrentAccount().getUrl(), mainViewModel.getCurrentAccount().getUserName(), R.mipmap.ic_launcher_round); + headerBinding.drawerUsernameFull.setText(mainViewModel.getCurrentAccount().getName()); accountChooserActive = false; inflateAccountMenu(); binding.drawerLayout.closeDrawer(GravityCompat.START); - DeckLog.verbose("Displaying maintenance mode info for " + viewModel.getCurrentAccount().getName() + ": " + viewModel.getCurrentAccount().isMaintenanceEnabled()); - binding.infoBox.setVisibility(viewModel.getCurrentAccount().isMaintenanceEnabled() ? View.VISIBLE : View.GONE); + DeckLog.verbose("Displaying maintenance mode info for " + mainViewModel.getCurrentAccount().getName() + ": " + mainViewModel.getCurrentAccount().isMaintenanceEnabled()); + binding.infoBox.setVisibility(mainViewModel.getCurrentAccount().isMaintenanceEnabled() ? View.VISIBLE : View.GONE); } private void refreshCapabilities(final Account account) { @@ -509,15 +512,15 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener } protected void setCurrentBoard(@NonNull Board board) { - viewModel.setCurrentBoard(board); - viewModel.clearFilterInformation(); + mainViewModel.setCurrentBoard(board); + filterViewModel.clearFilterInformation(); lastBoardId = board.getLocalId(); - Application.saveCurrentBoardId(this, viewModel.getCurrentAccount().getId(), viewModel.getCurrentBoardLocalId()); + Application.saveCurrentBoardId(this, mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()); binding.toolbar.setTitle(board.getTitle()); - if (viewModel.currentBoardHasEditPermission()) { + if (mainViewModel.currentBoardHasEditPermission()) { binding.fab.show(); binding.addStackButton.setVisibility(View.VISIBLE); } else { @@ -530,7 +533,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener binding.swipeRefreshLayout.setVisibility(View.VISIBLE); - syncManager.getStacksForBoard(viewModel.getCurrentAccount().getId(), board.getLocalId()).observe(MainActivity.this, (List<FullStack> fullStacks) -> { + syncManager.getStacksForBoard(mainViewModel.getCurrentAccount().getId(), board.getLocalId()).observe(MainActivity.this, (List<FullStack> fullStacks) -> { if (fullStacks == null) { throw new IllegalStateException("Given List<FullStack> must not be null"); } @@ -547,7 +550,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener int stackPositionInAdapter = 0; stackAdapter.setStacks(fullStacks); - long currentStackId = Application.readCurrentStackId(this, viewModel.getCurrentAccount().getId(), viewModel.getCurrentBoardLocalId()); + long currentStackId = Application.readCurrentStackId(this, mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()); for (int i = 0; i < currentBoardStacksCount; i++) { if (fullStacks.get(i).getLocalId() == currentStackId || currentStackId == NO_STACK_ID) { stackPositionInAdapter = i; @@ -633,7 +636,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener } else { switch (item.getItemId()) { case MENU_ID_ABOUT: - startActivityForResult(AboutActivity.createIntent(this, viewModel.getCurrentAccount()), MainActivity.ACTIVITY_ABOUT); + startActivityForResult(AboutActivity.createIntent(this, mainViewModel.getCurrentAccount()), MainActivity.ACTIVITY_ABOUT); break; case MENU_ID_SETTINGS: startActivityForResult(new Intent(this, SettingsActivity.class), MainActivity.ACTIVITY_SETTINGS); @@ -653,7 +656,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public boolean onCreateOptionsMenu(Menu menu) { final MenuInflater inflater = getMenuInflater(); - if (viewModel.currentBoardHasEditPermission()) { + if (mainViewModel.currentBoardHasEditPermission()) { final int currentViewPagerItem = binding.viewPager.getCurrentItem(); inflater.inflate(R.menu.list_menu, menu); menu.findItem(R.id.rename_list).setVisible(currentBoardHasStacks); @@ -665,7 +668,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener } inflater.inflate(R.menu.main_menu, menu); menu.findItem(R.id.archived_cards).setVisible(false); - menu.findItem(R.id.filter).setIcon(viewModel.getFilterInformation().getValue() == null + menu.findItem(R.id.filter).setIcon(filterViewModel.getFilterInformation().getValue() == null ? R.drawable.ic_filter_list_white_24dp : R.drawable.ic_filter_list_active_white_24dp); return super.onCreateOptionsMenu(menu); @@ -679,12 +682,12 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener return true; } case R.id.archived_cards: { - startActivity(ArchivedCardsActvitiy.createIntent(this, viewModel.getCurrentAccount(), viewModel.getCurrentBoardLocalId(), viewModel.currentBoardHasEditPermission())); + startActivity(ArchivedCardsActvitiy.createIntent(this, mainViewModel.getCurrentAccount(), mainViewModel.getCurrentBoardLocalId(), mainViewModel.currentBoardHasEditPermission())); return true; } case R.id.rename_list: { final long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - observeOnce(syncManager.getStack(viewModel.getCurrentAccount().getId(), stackId), MainActivity.this, fullStack -> + observeOnce(syncManager.getStack(mainViewModel.getCurrentAccount().getId(), stackId), MainActivity.this, fullStack -> EditStackDialogFragment.newInstance(fullStack.getLocalId(), fullStack.getStack().getTitle()) .show(getSupportFragmentManager(), EditStackDialogFragment.class.getCanonicalName())); return true; @@ -694,7 +697,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener // TODO error handling final int stackLeftPosition = binding.viewPager.getCurrentItem() - 1; final long stackLeftId = stackAdapter.getItem(stackLeftPosition).getLocalId(); - syncManager.swapStackOrder(viewModel.getCurrentAccount().getId(), viewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackLeftId)); + syncManager.swapStackOrder(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackLeftId)); stackMoved = true; return true; } @@ -703,13 +706,13 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener // TODO error handling final int stackRightPosition = binding.viewPager.getCurrentItem() + 1; final long stackRightId = stackAdapter.getItem(stackRightPosition).getLocalId(); - syncManager.swapStackOrder(viewModel.getCurrentAccount().getId(), viewModel.getCurrentBoardLocalId(), new Pair<>(stackId, stackRightId)); + 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(viewModel.getCurrentAccount().getId(), stackId), MainActivity.this, (numberOfCards) -> { + 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 { @@ -724,7 +727,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener } protected void showFabIfEditPermissionGranted() { - if (viewModel.currentBoardHasEditPermission()) { + if (mainViewModel.currentBoardHasEditPermission()) { binding.fab.show(); } } @@ -785,7 +788,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener final Snackbar importSnackbar = Snackbar.make(binding.coordinatorLayout, R.string.account_is_getting_imported, Snackbar.LENGTH_INDEFINITE); importSnackbar.show(); - importSyncManager.synchronize(new IResponseCallback<Boolean>(viewModel.getCurrentAccount()) { + importSyncManager.synchronize(new IResponseCallback<Boolean>(mainViewModel.getCurrentAccount()) { @Override public void onResponse(Boolean response) { importSnackbar.dismiss(); @@ -880,7 +883,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public void onAvailable(@NonNull Network network) { DeckLog.log("Got Network connection"); - syncManager.synchronize(new IResponseCallback<Boolean>(viewModel.getCurrentAccount()) { + syncManager.synchronize(new IResponseCallback<Boolean>(mainViewModel.getCurrentAccount()) { @Override public void onResponse(Boolean response) { DeckLog.log("Auto-Sync after connection available successful"); @@ -921,7 +924,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public void onStackDeleted(Long stackLocalId) { long stackId = stackAdapter.getItem(binding.viewPager.getCurrentItem()).getLocalId(); - observeOnce(syncManager.getStack(viewModel.getCurrentAccount().getId(), stackId), MainActivity.this, fullStack -> { + observeOnce(syncManager.getStack(mainViewModel.getCurrentAccount().getId(), stackId), MainActivity.this, fullStack -> { DeckLog.log("Delete stack #" + fullStack.getLocalId() + ": " + fullStack.getStack().getTitle()); // TODO error handling syncManager.deleteStack(fullStack.getStack()); @@ -931,7 +934,7 @@ public class MainActivity extends BrandedActivity implements DeleteStackListener @Override public void onBoardDeleted(Board board) { final int index = this.boardsList.indexOf(board); - if (board.getLocalId().equals(viewModel.getCurrentBoardLocalId())) { + if (board.getLocalId().equals(mainViewModel.getCurrentBoardLocalId())) { if (index > 0) { // Select first board after deletion setCurrentBoard(this.boardsList.get(0)); } else if (this.boardsList.size() > 1) { // Select second board after deletion 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 cebba655a..9017776da 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 @@ -1,82 +1,16 @@ package it.niedermann.nextcloud.deck.ui; -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.Board; -import it.niedermann.nextcloud.deck.model.Label; -import it.niedermann.nextcloud.deck.model.User; -import it.niedermann.nextcloud.deck.model.enums.EDueType; -import it.niedermann.nextcloud.deck.model.internal.FilterInformation; - -import static it.niedermann.nextcloud.deck.model.internal.FilterInformation.hasActiveFilter; @SuppressWarnings("WeakerAccess") public class MainViewModel extends ViewModel { - @NonNull - private MutableLiveData<FilterInformation> filterInformationDraft = new MutableLiveData<>(new FilterInformation()); - @NonNull - private MutableLiveData<FilterInformation> filterInformation = new MutableLiveData<>(); private Account currentAccount; private Board currentBoard; - public void publishFilterInformationDraft() { - this.filterInformation.postValue(hasActiveFilter(filterInformationDraft.getValue()) ? filterInformationDraft.getValue() : null); - } - - public void clearFilterInformation() { - this.filterInformationDraft.postValue(new FilterInformation()); - this.publishFilterInformationDraft(); - } - - @NonNull - public LiveData<FilterInformation> getFilterInformationDraft() { - return this.filterInformationDraft; - } - - public void createFilterInformationDraft() { - this.filterInformationDraft.postValue(new FilterInformation(this.filterInformation.getValue())); - } - - @NonNull - public LiveData<FilterInformation> getFilterInformation() { - return this.filterInformation; - } - - public void setFilterInformationDraftDueType(@NonNull EDueType dueType) { - FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); - newDraft.setDueType(dueType); - this.filterInformationDraft.postValue(newDraft); - } - - public void addFilterInformationDraftLabel(@NonNull Label label) { - FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); - newDraft.addLabel(label); - this.filterInformationDraft.postValue(newDraft); - } - - public void addFilterInformationUser(@NonNull User user) { - FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); - newDraft.addUser(user); - this.filterInformationDraft.postValue(newDraft); - } - - public void removeFilterInformationLabel(@NonNull Label label) { - FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); - newDraft.removeLabel(label); - this.filterInformationDraft.postValue(newDraft); - } - - public void removeFilterInformationUser(@NonNull User user) { - FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); - newDraft.removeUser(user); - this.filterInformationDraft.postValue(newDraft); - } - public Account getCurrentAccount() { return currentAccount; } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java index f26c51f99..6c7c1bd5c 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterAssigneesFragment.java @@ -20,12 +20,14 @@ import it.niedermann.nextcloud.deck.ui.MainViewModel; import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce; import static it.niedermann.nextcloud.deck.util.DimensionUtil.dpToPx; +import static java.util.Objects.requireNonNull; public class FilterAssigneesFragment extends Fragment implements SelectionListener<User> { private LiveData<FilterInformation> filterInformationDraft; private DialogFilterAssigneesBinding binding; private MainViewModel mainViewModel; + private FilterViewModel filterViewModel; private UserFilterAdapter userAdapter; @Nullable @@ -34,11 +36,12 @@ public class FilterAssigneesFragment extends Fragment implements SelectionListen binding = DialogFilterAssigneesBinding.inflate(requireActivity().getLayoutInflater()); mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); final SyncManager syncManager = new SyncManager(requireActivity()); - this.filterInformationDraft = mainViewModel.getFilterInformationDraft(); + this.filterInformationDraft = filterViewModel.getFilterInformationDraft(); observeOnce(syncManager.findProposalsForUsersToAssign(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), requireActivity(), (users) -> { - userAdapter = new UserFilterAdapter(dpToPx(requireContext(), R.dimen.avatar_size), mainViewModel.getCurrentAccount(), users, this.filterInformationDraft.getValue().getUsers(), this); + userAdapter = new UserFilterAdapter(dpToPx(requireContext(), R.dimen.avatar_size), mainViewModel.getCurrentAccount(), users, requireNonNull(this.filterInformationDraft.getValue()).getUsers(), this); binding.users.setNestedScrollingEnabled(false); binding.users.setAdapter(userAdapter); }); @@ -48,11 +51,11 @@ public class FilterAssigneesFragment extends Fragment implements SelectionListen @Override public void onItemSelected(User item) { - mainViewModel.addFilterInformationUser(item); + filterViewModel.addFilterInformationUser(item); } @Override public void onItemDeselected(User item) { - mainViewModel.removeFilterInformationUser(item); + filterViewModel.removeFilterInformationUser(item); } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java index ef6280c1c..0ea8f26cc 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDialogFragment.java @@ -19,7 +19,6 @@ import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.DialogFilterBinding; import it.niedermann.nextcloud.deck.model.enums.EDueType; import it.niedermann.nextcloud.deck.model.internal.FilterInformation; -import it.niedermann.nextcloud.deck.ui.MainViewModel; import it.niedermann.nextcloud.deck.ui.branding.BrandedActivity; import it.niedermann.nextcloud.deck.ui.branding.BrandedAlertDialogBuilder; import it.niedermann.nextcloud.deck.ui.branding.BrandedDialogFragment; @@ -27,7 +26,7 @@ import it.niedermann.nextcloud.deck.ui.branding.BrandedDialogFragment; public class FilterDialogFragment extends BrandedDialogFragment { private DialogFilterBinding binding; - private MainViewModel mainViewModel; + private FilterViewModel filterViewModel; private final static int[] tabTitles = new int[]{ R.string.filter_tags_title, @@ -40,7 +39,7 @@ public class FilterDialogFragment extends BrandedDialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); final AlertDialog.Builder dialogBuilder = new BrandedAlertDialogBuilder(requireContext()); @@ -48,7 +47,7 @@ public class FilterDialogFragment extends BrandedDialogFragment { binding.viewPager.setAdapter(new TabsPagerAdapter(getChildFragmentManager(), getLifecycle())); binding.viewPager.setOffscreenPageLimit(tabTitles.length); - LiveData<FilterInformation> filterInformationDraft = mainViewModel.getFilterInformationDraft(); + LiveData<FilterInformation> filterInformationDraft = filterViewModel.getFilterInformationDraft(); new TabLayoutMediator(binding.tabLayout, binding.viewPager, (tab, position) -> { switch (position) { case 0: @@ -84,14 +83,14 @@ public class FilterDialogFragment extends BrandedDialogFragment { tab.setText(tabTitles[position]); }).attach(); - mainViewModel.createFilterInformationDraft(); + filterViewModel.createFilterInformationDraft(); return dialogBuilder .setTitle(R.string.simple_filter) .setView(binding.getRoot()) .setNeutralButton(android.R.string.cancel, null) - .setNegativeButton(R.string.simple_clear, (a, b) -> mainViewModel.clearFilterInformation()) - .setPositiveButton(R.string.simple_filter, (a, b) -> mainViewModel.publishFilterInformationDraft()) + .setNegativeButton(R.string.simple_clear, (a, b) -> filterViewModel.clearFilterInformation()) + .setPositiveButton(R.string.simple_filter, (a, b) -> filterViewModel.publishFilterInformationDraft()) .create(); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java index 43b399139..654e95744 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterDuedateFragment.java @@ -15,23 +15,22 @@ import androidx.lifecycle.ViewModelProvider; import it.niedermann.nextcloud.deck.databinding.DialogFilterDuedateBinding; import it.niedermann.nextcloud.deck.model.enums.EDueType; import it.niedermann.nextcloud.deck.model.internal.FilterInformation; -import it.niedermann.nextcloud.deck.ui.MainViewModel; public class FilterDuedateFragment extends Fragment implements AdapterView.OnItemSelectedListener { private LiveData<FilterInformation> filterInformationDraft; private DialogFilterDuedateBinding binding; - private MainViewModel mainViewModel; + private FilterViewModel filterViewModel; private OverdueFilterAdapter overdueAdapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = DialogFilterDuedateBinding.inflate(requireActivity().getLayoutInflater()); - mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); overdueAdapter = new OverdueFilterAdapter(requireContext()); binding.overdue.setAdapter(overdueAdapter); - this.filterInformationDraft = mainViewModel.getFilterInformationDraft(); + this.filterInformationDraft = filterViewModel.getFilterInformationDraft(); binding.overdue.setSelection(overdueAdapter.getPosition(this.filterInformationDraft.getValue().getDueType())); binding.overdue.setOnItemSelectedListener(this); return binding.getRoot(); @@ -39,11 +38,11 @@ public class FilterDuedateFragment extends Fragment implements AdapterView.OnIte @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { - mainViewModel.setFilterInformationDraftDueType(overdueAdapter.getItem(position)); + filterViewModel.setFilterInformationDraftDueType(overdueAdapter.getItem(position)); } @Override public void onNothingSelected(AdapterView<?> parent) { - mainViewModel.setFilterInformationDraftDueType(EDueType.NO_FILTER); + filterViewModel.setFilterInformationDraftDueType(EDueType.NO_FILTER); } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java index b2e5e5c58..64384d89f 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsFragment.java @@ -18,12 +18,14 @@ import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import it.niedermann.nextcloud.deck.ui.MainViewModel; import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce; +import static java.util.Objects.requireNonNull; public class FilterLabelsFragment extends Fragment implements SelectionListener<Label> { private LiveData<FilterInformation> filterInformationDraft; private DialogFilterLabelsBinding binding; private MainViewModel mainViewModel; + private FilterViewModel filterViewModel; private LabelFilterAdapter labelAdapter; @Nullable @@ -31,11 +33,12 @@ public class FilterLabelsFragment extends Fragment implements SelectionListener< public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = DialogFilterLabelsBinding.inflate(requireActivity().getLayoutInflater()); mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + filterViewModel = new ViewModelProvider(requireActivity()).get(FilterViewModel.class); final SyncManager syncManager = new SyncManager(requireActivity()); - this.filterInformationDraft = mainViewModel.getFilterInformationDraft(); + this.filterInformationDraft = filterViewModel.getFilterInformationDraft(); observeOnce(syncManager.findProposalsForLabelsToAssign(mainViewModel.getCurrentAccount().getId(), mainViewModel.getCurrentBoardLocalId()), requireActivity(), (labels) -> { - labelAdapter = new LabelFilterAdapter(labels, this.filterInformationDraft.getValue().getLabels(), this); + labelAdapter = new LabelFilterAdapter(labels, requireNonNull(this.filterInformationDraft.getValue()).getLabels(), this); binding.labels.setNestedScrollingEnabled(false); binding.labels.setAdapter(labelAdapter); }); @@ -44,11 +47,11 @@ public class FilterLabelsFragment extends Fragment implements SelectionListener< @Override public void onItemSelected(Label item) { - mainViewModel.addFilterInformationDraftLabel(item); + filterViewModel.addFilterInformationDraftLabel(item); } @Override public void onItemDeselected(Label item) { - mainViewModel.removeFilterInformationLabel(item); + filterViewModel.removeFilterInformationLabel(item); } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterViewModel.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterViewModel.java new file mode 100644 index 000000000..e5734bdda --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterViewModel.java @@ -0,0 +1,75 @@ +package it.niedermann.nextcloud.deck.ui.filter; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import it.niedermann.nextcloud.deck.model.Label; +import it.niedermann.nextcloud.deck.model.User; +import it.niedermann.nextcloud.deck.model.enums.EDueType; +import it.niedermann.nextcloud.deck.model.internal.FilterInformation; + +import static it.niedermann.nextcloud.deck.model.internal.FilterInformation.hasActiveFilter; + +@SuppressWarnings("WeakerAccess") +public class FilterViewModel extends ViewModel { + + @NonNull + private MutableLiveData<FilterInformation> filterInformationDraft = new MutableLiveData<>(new FilterInformation()); + @NonNull + private MutableLiveData<FilterInformation> filterInformation = new MutableLiveData<>(); + + public void publishFilterInformationDraft() { + this.filterInformation.postValue(hasActiveFilter(filterInformationDraft.getValue()) ? filterInformationDraft.getValue() : null); + } + + public void clearFilterInformation() { + this.filterInformationDraft.postValue(new FilterInformation()); + this.publishFilterInformationDraft(); + } + + @NonNull + public LiveData<FilterInformation> getFilterInformationDraft() { + return this.filterInformationDraft; + } + + public void createFilterInformationDraft() { + this.filterInformationDraft.postValue(new FilterInformation(this.filterInformation.getValue())); + } + + @NonNull + public LiveData<FilterInformation> getFilterInformation() { + return this.filterInformation; + } + + public void setFilterInformationDraftDueType(@NonNull EDueType dueType) { + FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); + newDraft.setDueType(dueType); + this.filterInformationDraft.postValue(newDraft); + } + + public void addFilterInformationDraftLabel(@NonNull Label label) { + FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); + newDraft.addLabel(label); + this.filterInformationDraft.postValue(newDraft); + } + + public void addFilterInformationUser(@NonNull User user) { + FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); + newDraft.addUser(user); + this.filterInformationDraft.postValue(newDraft); + } + + public void removeFilterInformationLabel(@NonNull Label label) { + FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); + newDraft.removeLabel(label); + this.filterInformationDraft.postValue(newDraft); + } + + public void removeFilterInformationUser(@NonNull User user) { + FilterInformation newDraft = new FilterInformation(filterInformationDraft.getValue()); + newDraft.removeUser(user); + this.filterInformationDraft.postValue(newDraft); + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java index a221fcbff..def22c3f7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java @@ -24,6 +24,7 @@ import it.niedermann.nextcloud.deck.ui.MainViewModel; import it.niedermann.nextcloud.deck.ui.branding.BrandedFragment; import it.niedermann.nextcloud.deck.ui.card.CardAdapter; import it.niedermann.nextcloud.deck.ui.card.SelectCardListener; +import it.niedermann.nextcloud.deck.ui.filter.FilterViewModel; public class StackFragment extends BrandedFragment implements DragAndDropTab<CardAdapter> { @@ -60,11 +61,12 @@ public class StackFragment extends BrandedFragment implements DragAndDropTab<Car binding = FragmentStackBinding.inflate(inflater, container, false); activity = requireActivity(); - final MainViewModel viewModel = new ViewModelProvider(activity).get(MainViewModel.class); + final MainViewModel mainViewModel = new ViewModelProvider(activity).get(MainViewModel.class); + final FilterViewModel filterViewModel = new ViewModelProvider(activity).get(FilterViewModel.class); syncManager = new SyncManager(activity); - adapter = new CardAdapter(requireContext(), viewModel.getCurrentAccount(), viewModel.getCurrentBoardLocalId(), stackId, viewModel.currentBoardHasEditPermission(), syncManager, this, (requireActivity() instanceof SelectCardListener) ? (SelectCardListener) requireActivity() : null); + adapter = new CardAdapter(requireContext(), mainViewModel.getCurrentAccount(), mainViewModel.getCurrentBoardLocalId(), stackId, mainViewModel.currentBoardHasEditPermission(), syncManager, this, (requireActivity() instanceof SelectCardListener) ? (SelectCardListener) requireActivity() : null); binding.recyclerView.setAdapter(adapter); if (onScrollListener != null) { @@ -79,7 +81,7 @@ public class StackFragment extends BrandedFragment implements DragAndDropTab<Car }); } - if (!viewModel.currentBoardHasEditPermission()) { + if (!mainViewModel.currentBoardHasEditPermission()) { binding.emptyContentView.hideDescription(); } @@ -92,12 +94,12 @@ public class StackFragment extends BrandedFragment implements DragAndDropTab<Car } }); - cardsLiveData = syncManager.getFullCardsForStack(viewModel.getCurrentAccount().getId(), stackId, viewModel.getFilterInformation().getValue()); + cardsLiveData = syncManager.getFullCardsForStack(mainViewModel.getCurrentAccount().getId(), stackId, filterViewModel.getFilterInformation().getValue()); cardsLiveData.observe(getViewLifecycleOwner(), cardsObserver); - viewModel.getFilterInformation().observe(getViewLifecycleOwner(), (filterInformation -> { + filterViewModel.getFilterInformation().observe(getViewLifecycleOwner(), (filterInformation -> { cardsLiveData.removeObserver(cardsObserver); - cardsLiveData = syncManager.getFullCardsForStack(viewModel.getCurrentAccount().getId(), stackId, filterInformation); + cardsLiveData = syncManager.getFullCardsForStack(mainViewModel.getCurrentAccount().getId(), stackId, filterInformation); cardsLiveData.observe(getViewLifecycleOwner(), cardsObserver); })); |