A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
mobile: fix add notes to notebook selection behavior (#10104)
Kashaf Ansari10a08d571931
diff --git a/apps/mobile/app/components/container/floating-button.tsx b/apps/mobile/app/components/container/floating-button.tsxindex 4baef0bc5..b74bbc966 100644--- a/apps/mobile/app/components/container/floating-button.tsx+++ b/apps/mobile/app/components/container/floating-button.tsx@@ -36,6 +36,7 @@ import { getElevationStyle } from "../../utils/elevation";import { AppFontSize, normalize } from "../../utils/size";import { DefaultAppStyles } from "../../utils/styles";import { hexToRGBA, RGB_Linear_Shade } from "../../utils/colors";+import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets";interface FloatingButtonProps {onPress: () => void;@@ -47,6 +48,7 @@ interface FloatingButtonProps {position?: "left" | "right";size?: "small" | "large";style?: ViewStyle;+ hideOnKeyboard?: boolean;}const FloatingButton = ({@@ -57,26 +59,28 @@ const FloatingButton = ({testID,position = "right",size = "large",- style+ style,+ hideOnKeyboard = true}: FloatingButtonProps) => {const { colors } = useThemeColors();const deviceMode = useSettingStore((state) => state.deviceMode);const selectionMode = useSelectionStore((state) => state.selectionMode);const translate = useSharedValue(0);+ const keyboardHeight = useSharedValue(0);const route = useRoute();+ const insets = useGlobalSafeAreaInsets();- const animatedStyle = useAnimatedStyle(() => {- return {- transform: [- {- translateX: translate.value- },- {- translateY: translate.value- }- ]- };- });+ const animatedStyle = useAnimatedStyle(() => ({+ transform: [+ {+ translateX: hideOnKeyboard ? translate.value : 0+ },+ {+ translateY: hideOnKeyboard ? translate.value : 0+ }+ ],+ bottom: hideOnKeyboard ? 15 : keyboardHeight.value + 15+ }));const animate = useCallback((toValue: number) => {@@ -97,12 +101,22 @@ const FloatingButton = ({editorState().keyboardState = false;if (deviceMode !== "mobile") return;animate(0);+ keyboardHeight.value = withTiming(0);};- const onKeyboardShow = () => {+ const onKeyboardShow = (e: any) => {editorState().keyboardState = true;if (deviceMode !== "mobile") return;- animate(150);+ if (hideOnKeyboard) {+ animate(150);+ } else {+ keyboardHeight.value = withTiming(+ e.endCoordinates.height - insets.bottom,+ {+ duration: 250+ }+ );+ }};const sub = [@@ -110,9 +124,9 @@ const FloatingButton = ({Keyboard.addListener("keyboardDidHide", onKeyboardHide)];return () => {- sub.forEach((sub) => sub?.remove?.());+ sub.forEach((sub) => sub.remove());};- }, [deviceMode, animate]);+ }, [deviceMode, animate, hideOnKeyboard, keyboardHeight, insets.bottom]);return deviceMode !== "mobile" && !alwaysVisible ? null : (<Animated.View@@ -161,10 +175,10 @@ const FloatingButton = ({icon? icon: route.name === "Notebooks"- ? "notebook-plus"- : route.name === "Trash"- ? "delete"- : "plus"+ ? "notebook-plus"+ : route.name === "Trash"+ ? "delete"+ : "plus"}color={color || colors.primary.accent}size={size === "small" ? AppFontSize.xl : AppFontSize.xxxl}diff --git a/apps/mobile/app/screens/move-notes/index.tsx b/apps/mobile/app/screens/move-notes/index.tsxindex 37d439ff4..6c3109c73 100644--- a/apps/mobile/app/screens/move-notes/index.tsx+++ b/apps/mobile/app/screens/move-notes/index.tsx@@ -35,7 +35,10 @@ import Paragraph from "../../components/ui/typography/paragraph";import { useDBItem } from "../../hooks/use-db-item";import { useNavigationFocus } from "../../hooks/use-navigation-focus";import Navigation, { NavigationProps } from "../../services/navigation";-import { createItemSelectionStore } from "../../stores/item-selection-store";+import {+ createItemSelectionStore,+ SelectionState+} from "../../stores/item-selection-store";import { updateNotebook } from "../../utils/notebooks";import { AppFontSize } from "../../utils/size";import { DefaultAppStyles } from "../../utils/styles";@@ -47,14 +50,12 @@ export const MoveNotes = (props: NavigationProps<"MoveNotes">) => {const currentNotebook = props.route.params.notebook;const inputRef = useRef<TextInput>(null);const [loading, setLoading] = useState(false);- const selectionCount = useItemSelectionStore(- (state) =>- Object.keys(state.selection).filter(- (k) =>- state.selection?.[k] === "selected" ||- state.selection[k] === "deselected"- )?.length > 0- );++ const hasPendingChanges = useItemSelectionStore((state) => {+ return Object.keys(state.selection).some((id) => {+ return state.selection[id] !== state.initialState[id];+ });+ });useNavigationFocus(props.navigation, { focusOnInit: true });@@ -76,20 +77,6 @@ export const MoveNotes = (props: NavigationProps<"MoveNotes">) => {.catch(() => {setLoading(false);});-- db.relations- .from(currentNotebook, "note")- .get()- .then((existingNotes) => {- const selection: { [name: string]: any } = {};- existingNotes.forEach((rel) => {- selection[rel.toId] = "selected";- });- useItemSelectionStore.setState({- selection: selection,- initialState: selection- });- });},[currentNotebook]);@@ -97,6 +84,25 @@ export const MoveNotes = (props: NavigationProps<"MoveNotes">) => {useEffect(() => {loadNotes();+ const loadSelection = async () => {+ const existingNotes = await db.relations+ .from(currentNotebook, "note")+ .get();++ const selection: Record<string, SelectionState> = {};++ existingNotes.forEach((rel) => {+ selection[rel.toId] = "selected";+ });++ useItemSelectionStore.setState({+ selection,+ initialState: selection+ });+ };++ loadSelection();+return () => {useItemSelectionStore.getState().reset();};@@ -159,6 +165,8 @@ export const MoveNotes = (props: NavigationProps<"MoveNotes">) => {/><FlatList+ keyboardDismissMode="on-drag"+ keyboardShouldPersistTaps="handled"ListEmptyComponent={<Viewstyle={{@@ -176,17 +184,19 @@ export const MoveNotes = (props: NavigationProps<"MoveNotes">) => {)}</View>}- style={{- flexGrow: 1+ style={{ flex: 1 }}+ contentContainerStyle={{+ paddingBottom: hasPendingChanges ? 80 : 0}}data={loading ? [] : notes?.placeholders}renderItem={renderItem}/>- {selectionCount ? (+ {hasPendingChanges ? (<FloatingButtonicon="check"alwaysVisible+ hideOnKeyboard={false}onPress={async () => {await db.notes?.addToNotebook(currentNotebook.id,diff --git a/apps/mobile/app/stores/item-selection-store.ts b/apps/mobile/app/stores/item-selection-store.tsindex fa2cba947..ecbe39a93 100644--- a/apps/mobile/app/stores/item-selection-store.ts+++ b/apps/mobile/app/stores/item-selection-store.ts@@ -56,17 +56,14 @@ export function createItemSelectionStore(canEnableMultiSelectMode: multiSelectMode,initialState: {},markAs: (item, state) => {- set({- selection: {- ...get().selection,- [item.id]:- state === "deselected"- ? get().initialState === undefined- ? undefined- : "deselected"- : state- }- });+ const selection = { ...get().selection };+ const initial = get().initialState[item.id];+ if (state === "deselected" && initial === undefined) {+ delete selection[item.id];+ } else {+ selection[item.id] = state;+ }+ set({ selection });},multiSelect: false,toggleMultiSelect: () => {