-
-
Notifications
You must be signed in to change notification settings - Fork 162
feat(navigation): support arbitrary-depth category tree in drawer #3275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Plaoo
wants to merge
1
commit into
nextcloud:main
Choose a base branch
from
Plaoo:fix/1630-nested-category-tree
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/it/niedermann/owncloud/notes/main/navigation/CategoryExpandState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
120 changes: 120 additions & 0 deletions
120
app/src/main/java/it/niedermann/owncloud/notes/main/navigation/CategoryTreeMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
Kotlinfor new classes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure