Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: add support for sorting/grouping items based on notebook/tag/color id

Ammar Ahmed3f3467e3a7e8
diff --git a/apps/mobile/app/components/list-items/headers/section-header.tsx b/apps/mobile/app/components/list-items/headers/section-header.tsx
index 7f5d09e01..e52d6e4a9 100644
--- a/apps/mobile/app/components/list-items/headers/section-header.tsx
+++ b/apps/mobile/app/components/list-items/headers/section-header.tsx
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import {
GroupHeader,
+ GroupingByIdKey,
GroupingKey,
GroupOptions,
ItemType
@@ -46,6 +47,8 @@ type SectionHeaderProps = {
screen?: RouteName;
groupOptions: GroupOptions;
group: GroupingKey;
+ groupId?: string;
+ type?: GroupingByIdKey;
onOpenJumpToDialog: () => void;
itemCount?: number;
};
@@ -62,7 +65,9 @@ export const SectionHeader = React.memo<
groupOptions,
group,
onOpenJumpToDialog,
- itemCount
+ itemCount,
+ groupId,
+ type
}: SectionHeaderProps) {
const { colors } = useThemeColors();
const isCompactModeEnabled = useIsCompactModeEnabled(
@@ -143,8 +148,10 @@ export const SectionHeader = React.memo<
component: (
<Sort
screen={screen}
- type={dataType}
+ dataType={dataType}
+ type={type}
group={group}
+ groupId={groupId}
hideGroupOptions={
screen === "Reminders" || screen === "Search"
}
diff --git a/apps/mobile/app/components/list/index.tsx b/apps/mobile/app/components/list/index.tsx
index 1f6e53d3c..080c340b9 100644
--- a/apps/mobile/app/components/list/index.tsx
+++ b/apps/mobile/app/components/list/index.tsx
@@ -17,7 +17,12 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { GroupingKey, Item, VirtualizedGrouping } from "@notesnook/core";
+import {
+ GroupingByIdKey,
+ GroupingKey,
+ Item,
+ VirtualizedGrouping
+} from "@notesnook/core";
import { useThemeColors } from "@notesnook/theme";
import { LegendList, LegendListRenderItemProps } from "@legendapp/list";
import React, { useEffect, useRef } from "react";
@@ -55,6 +60,7 @@ type ListProps = {
placeholder?: PlaceholderData;
groupType: GroupingKey;
id?: string;
+ type?: GroupingByIdKey;
};
const onMomentumScrollEnd = () => {
@@ -74,7 +80,7 @@ export default function List(props: ListProps) {
props.dataType === "notebook" ||
notebooksListMode === "compact";
- const groupOptions = useGroupOptions(props.groupType);
+ const groupOptions = useGroupOptions(props.groupType, props.id, props.type);
const _onRefresh = async () => {
Sync.run("global", false, "full", () => {
@@ -96,23 +102,27 @@ export default function List(props: ListProps) {
index={itemProps.index}
isSheet={props.isRenderedInActionSheet || false}
items={props.data}
+ groupId={props.id}
groupOptions={groupOptions}
group={props.groupType as GroupingKey}
renderedInRoute={props.renderedInRoute}
customAccentColor={props.customAccentColor}
dataType={props.dataType}
+ type={props.type}
scrollRef={scrollRef}
/>
);
},
[
- groupOptions,
+ props.isRenderedInActionSheet,
+ props.data,
+ props.id,
props.groupType,
+ props.renderedInRoute,
props.customAccentColor,
- props.data,
props.dataType,
- props.isRenderedInActionSheet,
- props.renderedInRoute
+ groupOptions,
+ props.type
]
);
diff --git a/apps/mobile/app/components/list/list-item.wrapper.tsx b/apps/mobile/app/components/list/list-item.wrapper.tsx
index 0bf7db65e..7ef5f3eca 100644
--- a/apps/mobile/app/components/list/list-item.wrapper.tsx
+++ b/apps/mobile/app/components/list/list-item.wrapper.tsx
@@ -26,6 +26,7 @@ import {
Color,
GroupHeader,
GroupOptions,
+ GroupingByIdKey,
GroupingKey,
HighlightedResult,
Item,
@@ -40,7 +41,7 @@ import {
} from "@notesnook/core";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { View } from "react-native";
-import { db } from "../../common/database";
+import { getGroupOptions } from "../../hooks/use-group-options";
import { useIsCompactModeEnabled } from "../../hooks/use-is-compact-mode-enabled";
import { eSendEvent } from "../../services/event-manager";
import { RouteName } from "../../stores/use-navigation-store";
@@ -49,8 +50,8 @@ import { SectionHeader } from "../list-items/headers/section-header";
import { NoteWrapper } from "../list-items/note/wrapper";
import { NotebookWrapper } from "../list-items/notebook/wrapper";
import ReminderItem from "../list-items/reminder";
-import TagItem from "../list-items/tag";
import { SearchResult } from "../list-items/search-result";
+import TagItem from "../list-items/tag";
type ListItemWrapperProps<TItem = Item> = {
group: GroupingKey;
@@ -62,6 +63,8 @@ type ListItemWrapperProps<TItem = Item> = {
dataType: string;
scrollRef: any;
groupOptions: GroupOptions;
+ groupId?: string;
+ type?: GroupingByIdKey;
};
export function ListItemWrapper(props: ListItemWrapperProps) {
@@ -183,6 +186,8 @@ export function ListItemWrapper(props: ListItemWrapperProps) {
index={index}
dataType={item.type}
group={group}
+ groupId={props.groupId}
+ type={props.type}
color={props.customAccentColor}
groupOptions={groupOptions}
onOpenJumpToDialog={() => {
@@ -220,6 +225,8 @@ export function ListItemWrapper(props: ListItemWrapperProps) {
index={index}
dataType={item.type}
group={group}
+ groupId={props.groupId}
+ type={props.type}
color={props.customAccentColor}
groupOptions={groupOptions}
onOpenJumpToDialog={() => {
@@ -250,6 +257,8 @@ export function ListItemWrapper(props: ListItemWrapperProps) {
group={group}
dataType={item.type}
color={props.customAccentColor}
+ type={props.type}
+ groupId={props.groupId}
groupOptions={groupOptions}
onOpenJumpToDialog={() => {
eSendEvent(eOpenJumpToDialog, {
@@ -276,6 +285,8 @@ export function ListItemWrapper(props: ListItemWrapperProps) {
index={index}
group={group}
dataType={item.type}
+ groupId={props.groupId}
+ type={props.type}
color={props.customAccentColor}
groupOptions={groupOptions}
onOpenJumpToDialog={() => {
@@ -303,6 +314,8 @@ export function ListItemWrapper(props: ListItemWrapperProps) {
index={index}
group={group}
dataType={item.type}
+ groupId={props.groupId}
+ type={props.type}
color={props.customAccentColor}
groupOptions={groupOptions}
itemCount={items?.placeholders.length}
@@ -322,11 +335,16 @@ export function ListItemWrapper(props: ListItemWrapperProps) {
}
}
-function getDate(item: Notebook | Note, groupType?: GroupingKey): number {
+function getDate(
+ item: Notebook | Note,
+ groupType?: GroupingKey,
+ id?: string,
+ type?: GroupingByIdKey
+): number {
return (
getSortValue(
groupType
- ? db.settings.getGroupOptions(groupType)
+ ? getGroupOptions(groupType, id, type)
: {
sortBy: "dateEdited",
sortDirection: "desc"
diff --git a/apps/mobile/app/components/sheets/sort/index.tsx b/apps/mobile/app/components/sheets/sort/index.tsx
index 12faec218..5d2b88597 100644
--- a/apps/mobile/app/components/sheets/sort/index.tsx
+++ b/apps/mobile/app/components/sheets/sort/index.tsx
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {
+ GroupingByIdKey,
GroupingKey,
GroupOptions,
ItemType,
@@ -27,7 +28,10 @@ import { strings } from "@notesnook/intl";
import { useThemeColors } from "@notesnook/theme";
import React, { useState } from "react";
import { View } from "react-native";
-import { db } from "../../../common/database";
+import {
+ getGroupOptions,
+ setGroupOptionsById
+} from "../../../hooks/use-group-options";
import { eSendEvent } from "../../../services/event-manager";
import Navigation from "../../../services/navigation";
import { RouteName } from "../../../stores/use-navigation-store";
@@ -43,20 +47,24 @@ import { Pressable } from "../../ui/pressable";
import Heading from "../../ui/typography/heading";
import Paragraph from "../../ui/typography/paragraph";
const Sort = ({
- type,
+ dataType,
screen,
hideGroupOptions,
- group: groupType
+ group: groupType,
+ groupId,
+ type
}: {
- type: ItemType;
+ dataType: ItemType;
+ type?: GroupingByIdKey;
screen?: RouteName;
group: GroupingKey;
hideGroupOptions?: boolean;
+ groupId?: string;
}) => {
const { colors } = useThemeColors();
const [groupOptions, setGroupOptions] = useState(
- db.settings.getGroupOptions(groupType)
+ getGroupOptions(groupType, groupId, type)
);
const getSortButtonTitle = () => {
@@ -79,16 +87,17 @@ const Sort = ({
};
const updateGroupOptions = async (_groupOptions: GroupOptions) => {
- await db.settings.setGroupOptions(groupType, _groupOptions);
+ console.log(groupId, type);
+ setGroupOptionsById(groupType, _groupOptions, groupId, type);
setGroupOptions(_groupOptions);
setTimeout(() => {
if (screen) Navigation.queueRoutesForUpdate(screen);
- if (type === "notebook") {
+ if (dataType === "notebook") {
useNotebookStore.getState().refresh();
- } else if (type === "tag") {
+ } else if (dataType === "tag") {
useTagStore.getState().refresh();
}
- eSendEvent(eGroupOptionsUpdated, groupType);
+ eSendEvent(eGroupOptionsUpdated, groupType, groupId, type);
eSendEvent(refreshNotesPage);
}, 1);
};
diff --git a/apps/mobile/app/components/side-menu/index.tsx b/apps/mobile/app/components/side-menu/index.tsx
index 146bd695a..d92881ceb 100644
--- a/apps/mobile/app/components/side-menu/index.tsx
+++ b/apps/mobile/app/components/side-menu/index.tsx
@@ -493,7 +493,7 @@ const TabBar = (props: SimpleTabBarProps) => {
presentSheet({
component: (
<Sort
- type={
+ dataType={
props.navigationState.index === 1
? "notebook"
: "tag"
diff --git a/apps/mobile/app/hooks/use-group-options.ts b/apps/mobile/app/hooks/use-group-options.ts
index 1032bfb31..a96e12687 100644
--- a/apps/mobile/app/hooks/use-group-options.ts
+++ b/apps/mobile/app/hooks/use-group-options.ts
@@ -16,28 +16,61 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { db } from "../common/database";
import { eSubscribeEvent, eUnSubscribeEvent } from "../services/event-manager";
import Navigation from "../services/navigation";
import { eGroupOptionsUpdated } from "../utils/events";
import { useSettingStore } from "../stores/use-setting-store";
+import { GroupingByIdKey, GroupingKey, GroupOptions } from "@notesnook/core";
-export function useGroupOptions(type: any) {
+export function getGroupOptions(
+ groupingKey: GroupingKey,
+ id?: string,
+ type?: GroupingByIdKey
+) {
+ return id && type
+ ? db.settings?.getGroupOptionsById(id, type)
+ : db.settings?.getGroupOptions(groupingKey);
+}
+
+export function setGroupOptionsById(
+ groupingKey: GroupingKey,
+ groupOptions: GroupOptions,
+ id?: string,
+ type?: GroupingByIdKey
+) {
+ return id && type
+ ? db.settings?.setGroupOptionsById(id, type, groupOptions)
+ : db.settings?.setGroupOptions(groupingKey, groupOptions);
+}
+
+export function useGroupOptions(
+ groupingKey: GroupingKey,
+ id?: string,
+ type?: GroupingByIdKey
+) {
const appLoading = useSettingStore((state) => state.isAppLoading);
const [groupOptions, setGroupOptions] = useState(
- db.settings?.getGroupOptions(type)
+ getGroupOptions(groupingKey, id, type)
);
+ console.log(groupingKey, id, type, groupOptions, "options");
+ const groupOptionsRef = useRef(groupOptions);
+ groupOptionsRef.current = groupOptions;
+
useEffect(() => {
- const onUpdate = (groupType: string) => {
- if (groupType !== type) return;
- const options = db.settings?.getGroupOptions(type) as any;
+ const onUpdate = (_groupingKey: string, _id?: string, _type?: string) => {
+ if (_groupingKey !== groupingKey) return;
+ if (_id && _type && _id !== id && _type !== type) return;
+
+ const options = getGroupOptions(groupingKey, id, type);
if (!options) return;
if (
- groupOptions?.groupBy !== options.groupBy ||
- groupOptions?.sortBy !== options.sortBy ||
- groupOptions?.sortDirection !== groupOptions?.sortDirection
+ groupOptionsRef.current?.groupBy !== options.groupBy ||
+ groupOptionsRef.current?.sortBy !== options.sortBy ||
+ groupOptionsRef.current?.sortDirection !== options?.sortDirection
) {
+ console.log("onUpdate", _id, _type);
setGroupOptions({ ...options });
Navigation.queueRoutesForUpdate();
}
@@ -46,13 +79,13 @@ export function useGroupOptions(type: any) {
eSubscribeEvent(eGroupOptionsUpdated, onUpdate);
if (!appLoading) {
- onUpdate(type);
+ onUpdate(groupingKey);
}
return () => {
eUnSubscribeEvent(eGroupOptionsUpdated, onUpdate);
};
- }, [type, groupOptions, appLoading]);
+ }, [groupingKey, appLoading, id, type]);
return groupOptions;
}
diff --git a/apps/mobile/app/screens/notebook/index.tsx b/apps/mobile/app/screens/notebook/index.tsx
index ea3aeabcd..08e30c9a4 100644
--- a/apps/mobile/app/screens/notebook/index.tsx
+++ b/apps/mobile/app/screens/notebook/index.tsx
@@ -41,6 +41,7 @@ import { View } from "react-native";
import { Notebooks } from "../../components/sheets/notebooks";
import { useSettingStore } from "../../stores/use-setting-store";
import { rootNavigatorRef } from "../../utils/global-refs";
+import { getGroupOptions } from "../../hooks/use-group-options";
const NotebookScreen = ({ route, navigation }: NavigationProps<"Notebook">) => {
const [notes, setNotes] = useState<VirtualizedGrouping<Note>>();
@@ -120,7 +121,9 @@ const NotebookScreen = ({ route, navigation }: NavigationProps<"Notebook">) => {
params.current.id = notebook.id;
const notes = await db.relations
.from(notebook, "note")
- .selector.grouped(db.settings.getGroupOptions("notes"));
+ .selector.grouped(
+ getGroupOptions("notes", notebook.id, "notebook")
+ );
setNotes(notes);
await notes.item(0, resolveItems);
syncWithNavigation();
@@ -190,6 +193,7 @@ const NotebookScreen = ({ route, navigation }: NavigationProps<"Notebook">) => {
onRequestUpdate();
}}
id={params.current?.id}
+ type="notebook"
renderedInRoute="Notebook"
headerTitle={notebook?.title}
loading={loading}
diff --git a/apps/mobile/app/screens/notes/colored.tsx b/apps/mobile/app/screens/notes/colored.tsx
index 3f727b7d7..1cd92521d 100644
--- a/apps/mobile/app/screens/notes/colored.tsx
+++ b/apps/mobile/app/screens/notes/colored.tsx
@@ -26,6 +26,7 @@ import useNavigationStore, {
NotesScreenParams
} from "../../stores/use-navigation-store";
import { PLACEHOLDER_DATA, openEditor } from "./common";
+import { getGroupOptions } from "../../hooks/use-group-options";
export const ColoredNotes = ({
navigation,
route
@@ -52,7 +53,7 @@ ColoredNotes.get = async (params: NotesScreenParams, grouped = true) => {
return await db.relations
.from({ id: params.id, type: "color" }, "note")
- .selector.grouped(db.settings.getGroupOptions("notes"));
+ .selector.grouped(getGroupOptions("notes", params.id, "color"));
};
ColoredNotes.navigate = (item: Color, canGoBack: boolean) => {
diff --git a/apps/mobile/app/screens/notes/index.tsx b/apps/mobile/app/screens/notes/index.tsx
index 3527db26c..71194342d 100644
--- a/apps/mobile/app/screens/notes/index.tsx
+++ b/apps/mobile/app/screens/notes/index.tsx
@@ -172,7 +172,7 @@ const NotesPage = ({
if (loadingNotes) {
onRequestUpdate(params.current);
}
- }, [loadingNotes, get, isAppLoading]);
+ }, [loadingNotes, get, isAppLoading, onRequestUpdate]);
useEffect(() => {
eSubscribeEvent(route.name, onRequestUpdate);
@@ -222,6 +222,7 @@ const NotesPage = ({
loading={false}
renderedInRoute={route.name}
id={params.current?.id}
+ type={params.current?.item?.type}
headerTitle={title || "Monographs"}
customAccentColor={accentColor}
placeholder={placeholder}
diff --git a/apps/mobile/app/screens/notes/tagged.tsx b/apps/mobile/app/screens/notes/tagged.tsx
index 5ad689407..b48b91212 100644
--- a/apps/mobile/app/screens/notes/tagged.tsx
+++ b/apps/mobile/app/screens/notes/tagged.tsx
@@ -26,6 +26,7 @@ import useNavigationStore, {
NotesScreenParams
} from "../../stores/use-navigation-store";
import { PLACEHOLDER_DATA, openEditor } from "./common";
+import { getGroupOptions } from "../../hooks/use-group-options";
export const TaggedNotes = ({
navigation,
@@ -53,7 +54,7 @@ TaggedNotes.get = async (params: NotesScreenParams, grouped = true) => {
return await db.relations
.from({ id: params.id, type: "tag" }, "note")
- .selector.grouped(db.settings.getGroupOptions("notes"));
+ .selector.grouped(getGroupOptions("notes", params.id, "tag"));
};
TaggedNotes.navigate = (item: Tag, canGoBack?: boolean) => {
diff --git a/apps/mobile/package-lock.json b/apps/mobile/package-lock.json
index bcff135b9..546527553 100644
--- a/apps/mobile/package-lock.json
+++ b/apps/mobile/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@notesnook/mobile",
- "version": "3.3.23",
+ "version": "3.3.24",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@notesnook/mobile",
- "version": "3.3.23",
+ "version": "3.3.24",
"hasInstallScript": true,
"license": "GPL-3.0-or-later",
"dependencies": {