Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import it.niedermann.owncloud.notes.main.items.section.SectionItemDecoration;
import it.niedermann.owncloud.notes.main.items.selection.ItemSelectionTracker;
import it.niedermann.owncloud.notes.main.menu.MenuAdapter;
import it.niedermann.owncloud.notes.main.navigation.CategoryExpandState;
import it.niedermann.owncloud.notes.main.navigation.NavigationAdapter;
import it.niedermann.owncloud.notes.main.navigation.NavigationClickListener;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
Expand Down Expand Up @@ -690,18 +691,12 @@ private void selectItem(NavigationItem item, boolean closeNavigation) {

@Override
public void onIconClick(NavigationItem item) {
final var expandedCategoryLiveData = mainViewModel.getExpandedCategory();
expandedCategoryLiveData.observe(MainActivity.this, expandedCategory -> {
if (item.icon == NavigationAdapter.ICON_MULTIPLE && !item.label.equals(expandedCategory)) {
mainViewModel.postExpandedCategory(item.label);
selectItem(item, false);
} else if (item.icon == NavigationAdapter.ICON_MULTIPLE || item.icon == NavigationAdapter.ICON_MULTIPLE_OPEN && item.label.equals(expandedCategory)) {
mainViewModel.postExpandedCategory(null);
} else {
onItemClick(item);
}
expandedCategoryLiveData.removeObservers(MainActivity.this);
});
if (item.expandState != CategoryExpandState.NOT_EXPANDABLE
&& item instanceof NavigationItem.CategoryNavigationItem categoryItem) {
mainViewModel.toggleExpandedCategory(categoryItem.category);
} else {
onItemClick(item);
}
}
});
adapterCategories.setSelectedItem(ADAPTER_KEY_RECENT);
Expand Down
123 changes: 29 additions & 94 deletions app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import static androidx.lifecycle.Transformations.map;
import static androidx.lifecycle.Transformations.switchMap;
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
import static it.niedermann.owncloud.notes.main.MainActivity.ADAPTER_KEY_RECENT;
import static it.niedermann.owncloud.notes.main.MainActivity.ADAPTER_KEY_STARRED;
import static it.niedermann.owncloud.notes.main.slots.SlotterUtil.fillListByCategory;
import static it.niedermann.owncloud.notes.main.slots.SlotterUtil.fillListByInitials;
import static it.niedermann.owncloud.notes.main.slots.SlotterUtil.fillListByTime;
Expand All @@ -21,7 +19,6 @@
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.FAVORITES;
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.RECENT;
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.UNCATEGORIZED;
import static it.niedermann.owncloud.notes.shared.util.DisplayUtils.convertToCategoryNavigationItem;

import android.accounts.NetworkErrorException;
import android.app.Application;
Expand Down Expand Up @@ -49,23 +46,23 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import it.niedermann.owncloud.notes.BuildConfig;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.exception.IntendedOfflineException;
import it.niedermann.owncloud.notes.main.navigation.NavigationAdapter;
import it.niedermann.owncloud.notes.main.navigation.CategoryTreeMapper;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.persistence.ApiProvider;
import it.niedermann.owncloud.notes.persistence.CapabilitiesClient;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;
import it.niedermann.owncloud.notes.shared.model.Capabilities;
Expand Down Expand Up @@ -98,7 +95,7 @@ public class MainViewModel extends AndroidViewModel {
@NonNull
private final MutableLiveData<NavigationCategory> selectedCategory = new MutableLiveData<>(new NavigationCategory(RECENT));
@NonNull
private final MutableLiveData<String> expandedCategory = new MutableLiveData<>(null);
private final MutableLiveData<Set<String>> expandedCategories = new MutableLiveData<>(Collections.emptySet());

public MainViewModel(@NonNull Application application, @NonNull SavedStateHandle savedStateHandle) {
super(application);
Expand All @@ -118,7 +115,10 @@ public void restoreInstanceState() {
postSelectedCategory(selectedCategory);
Log.v(TAG, "[restoreInstanceState] - selectedCategory: " + selectedCategory);
}
postExpandedCategory(state.get(KEY_EXPANDED_CATEGORY));
final ArrayList<String> restoredExpanded = state.get(KEY_EXPANDED_CATEGORY);
if (restoredExpanded != null) {
postExpandedCategories(new HashSet<>(restoredExpanded));
}
}

@NonNull
Expand Down Expand Up @@ -165,23 +165,13 @@ public void postSelectedCategory(@NonNull NavigationCategory selectedCategory) {
Log.v(TAG, "[postSelectedCategory] - selectedCategory: " + selectedCategory);
this.selectedCategory.postValue(selectedCategory);

// Close sub categories
// Collapse the whole tree when leaving the category navigation
switch (selectedCategory.getType()) {
case RECENT, FAVORITES, UNCATEGORIZED -> {
postExpandedCategory(null);
}
case RECENT, FAVORITES, UNCATEGORIZED -> postExpandedCategories(Collections.emptySet());
default -> {
final String category = selectedCategory.getCategory();
if (category == null) {
postExpandedCategory(null);
if (selectedCategory.getCategory() == null) {
postExpandedCategories(Collections.emptySet());
Log.e(TAG, "navigation selection is a " + DEFAULT_CATEGORY + ", but the contained category is null.");
} else {
int slashIndex = category.indexOf('/');
final String rootCategory = slashIndex < 0 ? category : category.substring(0, slashIndex);
final String expandedCategory = getExpandedCategory().getValue();
if (expandedCategory != null && !expandedCategory.equals(rootCategory)) {
postExpandedCategory(null);
}
}
}
}
Expand All @@ -205,14 +195,23 @@ public LiveData<Void> modifyCategoryOrder(@NonNull NavigationCategory selectedCa
});
}

public void postExpandedCategory(@Nullable String expandedCategory) {
state.set(KEY_EXPANDED_CATEGORY, expandedCategory);
this.expandedCategory.postValue(expandedCategory);
public void postExpandedCategories(@NonNull Set<String> expandedCategories) {
state.set(KEY_EXPANDED_CATEGORY, new ArrayList<>(expandedCategories));
this.expandedCategories.postValue(expandedCategories);
}

public void toggleExpandedCategory(@NonNull String category) {
final var current = expandedCategories.getValue();
final var updated = current == null ? new HashSet<String>() : new HashSet<>(current);
if (!updated.remove(category)) {
updated.add(category);
}
postExpandedCategories(updated);
}

@NonNull
public LiveData<String> getExpandedCategory() {
return distinctUntilChanged(expandedCategory);
public LiveData<Set<String>> getExpandedCategories() {
return distinctUntilChanged(expandedCategories);
}

@NonNull
Expand Down Expand Up @@ -304,14 +303,14 @@ public LiveData<List<NavigationItem>> getNavigationCategories() {
return insufficientInformation;
} else {
Log.v(TAG, "[getNavigationCategories] - currentAccount: " + currentAccount.getAccountName());
return switchMap(getExpandedCategory(), expandedCategory -> {
Log.v(TAG, "[getNavigationCategories] - expandedCategory: " + expandedCategory);
return switchMap(getExpandedCategories(), expandedCategories -> {
Log.v(TAG, "[getNavigationCategories] - expandedCategories: " + expandedCategories);
return switchMap(repo.count$(currentAccount.getId()), (count) -> {
Log.v(TAG, "[getNavigationCategories] - count: " + count);
return switchMap(repo.countFavorites$(currentAccount.getId()), (favoritesCount) -> {
Log.v(TAG, "[getNavigationCategories] - favoritesCount: " + favoritesCount);
return distinctUntilChanged(map(repo.getCategories$(currentAccount.getId()), fromDatabase ->
fromCategoriesWithNotesCount(getApplication(), expandedCategory, fromDatabase, count, favoritesCount)
CategoryTreeMapper.map(getApplication(), expandedCategories, fromDatabase, count, favoritesCount)
));
});
});
Expand All @@ -320,70 +319,6 @@ public LiveData<List<NavigationItem>> getNavigationCategories() {
});
}

private static List<NavigationItem> fromCategoriesWithNotesCount(@NonNull Context context, @Nullable String expandedCategory, @NonNull List<CategoryWithNotesCount> fromDatabase, int count, int favoritesCount) {
final var categories = convertToCategoryNavigationItem(context, fromDatabase);
final var itemRecent = new NavigationItem(ADAPTER_KEY_RECENT, context.getString(R.string.label_all_notes), count, R.drawable.selector_all_notes, RECENT);
final var itemFavorites = new NavigationItem(ADAPTER_KEY_STARRED, context.getString(R.string.label_favorites), favoritesCount, R.drawable.selector_favorites, FAVORITES);

final var items = new ArrayList<NavigationItem>(fromDatabase.size() + 3);
items.add(itemRecent);
items.add(itemFavorites);
NavigationItem lastPrimaryCategory = null;
NavigationItem lastSecondaryCategory = null;
for (final var item : categories) {
final int slashIndex = item.label.indexOf('/');
final String currentPrimaryCategory = slashIndex < 0 ? item.label : item.label.substring(0, slashIndex);
final boolean isCategoryOpen = currentPrimaryCategory.equals(expandedCategory);
String currentSecondaryCategory = null;

if (isCategoryOpen && !currentPrimaryCategory.equals(item.label)) {
final String currentCategorySuffix = item.label.substring(expandedCategory.length() + 1);
final int subSlashIndex = currentCategorySuffix.indexOf('/');
currentSecondaryCategory = subSlashIndex < 0 ? currentCategorySuffix : currentCategorySuffix.substring(0, subSlashIndex);
}

boolean belongsToLastPrimaryCategory = lastPrimaryCategory != null && currentPrimaryCategory.equals(lastPrimaryCategory.label);
final boolean belongsToLastSecondaryCategory = belongsToLastPrimaryCategory && lastSecondaryCategory != null && lastSecondaryCategory.label.equals(currentSecondaryCategory);

if (isCategoryOpen && !belongsToLastPrimaryCategory && currentSecondaryCategory != null) {
lastPrimaryCategory = new NavigationItem("category:" + currentPrimaryCategory, currentPrimaryCategory, 0, NavigationAdapter.ICON_MULTIPLE_OPEN);
items.add(lastPrimaryCategory);
belongsToLastPrimaryCategory = true;
}

if (belongsToLastPrimaryCategory && belongsToLastSecondaryCategory) {
lastSecondaryCategory.count += item.count;
lastSecondaryCategory.icon = NavigationAdapter.ICON_SUB_MULTIPLE;
} else if (belongsToLastPrimaryCategory) {
if (isCategoryOpen) {
if (currentSecondaryCategory == null) {
throw new IllegalStateException("Current secondary category is null. Last primary category: " + lastPrimaryCategory);
}
item.label = currentSecondaryCategory;
item.id = "category:" + item.label;
item.icon = NavigationAdapter.ICON_SUB_FOLDER;
items.add(item);
lastSecondaryCategory = item;
} else {
lastPrimaryCategory.count += item.count;
lastPrimaryCategory.icon = NavigationAdapter.ICON_MULTIPLE;
lastSecondaryCategory = null;
}
} else {
if (isCategoryOpen) {
item.icon = NavigationAdapter.ICON_MULTIPLE_OPEN;
} else {
item.label = currentPrimaryCategory;
item.id = "category:" + item.label;
}
items.add(item);
lastPrimaryCategory = item;
lastSecondaryCategory = null;
}
}
return items;
}

public void synchronizeCapabilitiesAndNotes(Context context, @NonNull Account localAccount, @NonNull IResponseCallback<Void> callback) {
Log.i(TAG, "[synchronizeCapabilitiesAndNotes] Synchronize capabilities for " + localAccount.getAccountName());
synchronizeCapabilities(localAccount, new IResponseCallback<>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.main.navigation;

/**
* Describes whether a navigation item representing a category can be expanded to reveal its
* sub categories, and if so whether it is currently expanded.
*/
public enum CategoryExpandState {
NOT_EXPANDABLE,
COLLAPSED,
EXPANDED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.main.navigation;

import static it.niedermann.owncloud.notes.main.MainActivity.ADAPTER_KEY_RECENT;
import static it.niedermann.owncloud.notes.main.MainActivity.ADAPTER_KEY_STARRED;
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.FAVORITES;
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.RECENT;

import android.content.Context;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount;
import it.niedermann.owncloud.notes.shared.util.DisplayUtils;

/**
* Turns the flat list of categories into the hierarchical list of {@link NavigationItem}s shown in
* the navigation drawer. Categories are organized as a tree along their {@code /} separator and can
* be expanded to an arbitrary depth: a sub category is only listed when every one of its ancestors
* is contained in {@code expandedCategories}.
*/
public final class CategoryTreeMapper {

private static final String CATEGORY_SEPARATOR = "/";

private CategoryTreeMapper() {
throw new UnsupportedOperationException("Do not instantiate this util class.");
}

@NonNull
public static List<NavigationItem> map(@NonNull Context context,
@NonNull Set<String> expandedCategories,
@NonNull List<CategoryWithNotesCount> categories,
int count,
int favoritesCount) {
final var items = new ArrayList<NavigationItem>(categories.size() + 2);
items.add(new NavigationItem(ADAPTER_KEY_RECENT, context.getString(R.string.label_all_notes), count, R.drawable.selector_all_notes, RECENT));
items.add(new NavigationItem(ADAPTER_KEY_STARRED, context.getString(R.string.label_favorites), favoritesCount, R.drawable.selector_favorites, FAVORITES));

final long accountId = categories.isEmpty() ? -1 : categories.get(0).getAccountId();
final var root = buildTree(categories);
flatten(context, root, 0, accountId, expandedCategories, items);
return items;
}

@NonNull
private static CategoryTreeNode buildTree(@NonNull List<CategoryWithNotesCount> categories) {
final var root = new CategoryTreeNode("", "");
for (final var category : categories) {
final String[] segments = category.getCategory().split(CATEGORY_SEPARATOR);
var node = root;
final var path = new StringBuilder();
for (final String segment : segments) {
if (path.length() > 0) {
path.append(CATEGORY_SEPARATOR);
}
path.append(segment);
final String currentPath = path.toString();
node = node.children.computeIfAbsent(segment, label -> new CategoryTreeNode(currentPath, label));
}
node.directNotes = category.getTotalNotes();
}
return root;
}

private static void flatten(@NonNull Context context,
@NonNull CategoryTreeNode parent,
int depth,
long accountId,
@NonNull Set<String> expandedCategories,
@NonNull List<NavigationItem> items) {
for (final var node : parent.children.values()) {
final boolean hasChildren = node.hasChildren();
final boolean expanded = hasChildren && expandedCategories.contains(node.path);
final int notes = expanded ? node.directNotes : node.totalNotes();
final var item = new NavigationItem.CategoryNavigationItem("category:" + node.path, node.label, notes, iconFor(context, node.label, hasChildren, expanded, depth), accountId, node.path);
item.depth = depth;
item.expandState = expandStateFor(hasChildren, expanded);
items.add(item);

if (expanded) {
flatten(context, node, depth + 1, accountId, expandedCategories, items);
}
}
}

@NonNull
private static CategoryExpandState expandStateFor(boolean hasChildren, boolean expanded) {
if (!hasChildren) {
return CategoryExpandState.NOT_EXPANDABLE;
}
return expanded ? CategoryExpandState.EXPANDED : CategoryExpandState.COLLAPSED;
}

@DrawableRes
private static int iconFor(@NonNull Context context, @NonNull String label, boolean hasChildren, boolean expanded, int depth) {
if (hasChildren) {
if (expanded) {
return NavigationAdapter.ICON_MULTIPLE_OPEN;
}
return depth == 0 ? NavigationAdapter.ICON_MULTIPLE : NavigationAdapter.ICON_SUB_MULTIPLE;
}
final int specialIcon = DisplayUtils.getCategoryIcon(context, label);
if (specialIcon != NavigationAdapter.ICON_FOLDER) {
return specialIcon;
}
return depth == 0 ? NavigationAdapter.ICON_FOLDER : NavigationAdapter.ICON_SUB_FOLDER;
}
}
Loading