mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-24 04:00:59 +01:00
mobile: run sync when necessary
This commit is contained in:
committed by
Abdullah Atta
parent
88858308fb
commit
ed32552155
@@ -23,20 +23,18 @@ import { View } from "react-native";
|
||||
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
|
||||
import { useTotalNotes } from "../../hooks/use-db-item";
|
||||
|
||||
import { db } from "../../common/database";
|
||||
import { eSubscribeEvent } from "../../services/event-manager";
|
||||
import Navigation from "../../services/navigation";
|
||||
import { useFavoriteStore } from "../../stores/use-favorite-store";
|
||||
import useNavigationStore, {
|
||||
RouteParams
|
||||
} from "../../stores/use-navigation-store";
|
||||
import { useNoteStore } from "../../stores/use-notes-store";
|
||||
import { useTrashStore } from "../../stores/use-trash-store";
|
||||
import { eAfterSync } from "../../utils/events";
|
||||
import { SideMenuItem } from "../../utils/menu-items";
|
||||
import { defaultBorderRadius, AppFontSize } from "../../utils/size";
|
||||
import { AppFontSize, defaultBorderRadius } from "../../utils/size";
|
||||
import { DefaultAppStyles } from "../../utils/styles";
|
||||
import { Pressable } from "../ui/pressable";
|
||||
import Paragraph from "../ui/typography/paragraph";
|
||||
import { useMonographStore } from "../../stores/use-monograph-store";
|
||||
import { useReminderStore } from "../../stores/use-reminder-store";
|
||||
|
||||
function _MenuItem({
|
||||
item,
|
||||
@@ -65,57 +63,37 @@ function _MenuItem({
|
||||
: totalNotes.totalNotes(item.data.id);
|
||||
|
||||
useEffect(() => {
|
||||
let unsub: () => void;
|
||||
if (!item.data) {
|
||||
switch (item.id) {
|
||||
case "Notes":
|
||||
unsub = useNoteStore.subscribe((state) => {
|
||||
setItemCount(
|
||||
useNoteStore.getState().items?.placeholders?.length || 0
|
||||
);
|
||||
});
|
||||
setItemCount(
|
||||
useNoteStore.getState().items?.placeholders?.length || 0
|
||||
);
|
||||
break;
|
||||
case "Favorites":
|
||||
unsub = useFavoriteStore.subscribe((state) => {
|
||||
setItemCount(state.items?.placeholders.length || 0);
|
||||
});
|
||||
setItemCount(
|
||||
useFavoriteStore.getState().items?.placeholders?.length || 0
|
||||
);
|
||||
break;
|
||||
case "Reminders":
|
||||
unsub = useReminderStore.subscribe((state) => {
|
||||
setItemCount(state.items?.placeholders.length || 0);
|
||||
});
|
||||
setItemCount(
|
||||
useReminderStore.getState().items?.placeholders?.length || 0
|
||||
);
|
||||
break;
|
||||
case "Monographs":
|
||||
unsub = useMonographStore.subscribe((state) => {
|
||||
setItemCount(state.items?.placeholders.length || 0);
|
||||
});
|
||||
setItemCount(
|
||||
useMonographStore.getState().items?.placeholders?.length || 0
|
||||
);
|
||||
break;
|
||||
case "Trash":
|
||||
unsub = useTrashStore.subscribe((state) => {
|
||||
setItemCount(state.items?.placeholders.length || 0);
|
||||
});
|
||||
setItemCount(
|
||||
useTrashStore.getState().items?.placeholders?.length || 0
|
||||
);
|
||||
break;
|
||||
const onSyncComplete = async () => {
|
||||
try {
|
||||
if (!item.data) {
|
||||
switch (item.id) {
|
||||
case "Notes":
|
||||
setItemCount(await db.notes.all.count());
|
||||
break;
|
||||
case "Favorites":
|
||||
setItemCount(await db.notes.favorites.count());
|
||||
break;
|
||||
case "Reminders":
|
||||
setItemCount(await db.reminders.all.count());
|
||||
break;
|
||||
case "Monographs":
|
||||
setItemCount(await db.monographs.all.count());
|
||||
break;
|
||||
case "Trash":
|
||||
setItemCount((await db.trash.all()).length);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
getTotalNotesRef.current?.([item.data.id]);
|
||||
}
|
||||
} catch (e) {
|
||||
/** Empty */
|
||||
}
|
||||
} else {
|
||||
getTotalNotesRef.current?.([item.data.id]);
|
||||
}
|
||||
};
|
||||
const event = eSubscribeEvent(eAfterSync, onSyncComplete);
|
||||
onSyncComplete();
|
||||
return () => {
|
||||
unsub?.();
|
||||
event?.unsubscribe();
|
||||
};
|
||||
}, [item.data, item.id]);
|
||||
|
||||
|
||||
@@ -264,7 +264,7 @@ async function checkForShareExtensionLaunchedInBackground() {
|
||||
}
|
||||
eSendEvent(refreshNotesPage);
|
||||
MMKV.removeItem("notesAddedFromIntent");
|
||||
initAfterSync();
|
||||
initAfterSync("full");
|
||||
eSendEvent(refreshNotesPage);
|
||||
}
|
||||
|
||||
@@ -437,7 +437,7 @@ export const useAppEvents = () => {
|
||||
>({});
|
||||
|
||||
const onSyncComplete = useCallback(async () => {
|
||||
initAfterSync();
|
||||
initAfterSync(Sync.getLastSyncType() as "full" | "send");
|
||||
setLastSynced(await db.lastSynced());
|
||||
eSendEvent(eCloseSheet, "sync_progress");
|
||||
}, [setLastSynced]);
|
||||
|
||||
@@ -36,6 +36,7 @@ export const ignoredMessages = [
|
||||
];
|
||||
let pendingSync: any = undefined;
|
||||
let syncTimer: NodeJS.Timeout;
|
||||
let lastSyncType = "full";
|
||||
|
||||
const run = async (
|
||||
context = "global",
|
||||
@@ -69,11 +70,13 @@ const run = async (
|
||||
|
||||
if (!user || SettingsService.get().disableSync) {
|
||||
userstore.setSyncing(false);
|
||||
initAfterSync();
|
||||
initAfterSync(type != "send" ? "full" : "send");
|
||||
pendingSync = undefined;
|
||||
return onCompleted?.(SyncStatus.Failed);
|
||||
}
|
||||
|
||||
lastSyncType = type != "send" ? "full" : "send";
|
||||
|
||||
let error: Error | undefined = undefined;
|
||||
|
||||
try {
|
||||
@@ -103,7 +106,6 @@ const run = async (
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
initAfterSync();
|
||||
userstore.setSyncing(
|
||||
false,
|
||||
error ? SyncStatus.Failed : SyncStatus.Passed
|
||||
@@ -122,7 +124,8 @@ const run = async (
|
||||
};
|
||||
|
||||
const Sync = {
|
||||
run
|
||||
run,
|
||||
getLastSyncType: () => lastSyncType
|
||||
};
|
||||
|
||||
export default Sync;
|
||||
|
||||
@@ -18,9 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { db } from "../common/database";
|
||||
import { eSendEvent } from "../services/event-manager";
|
||||
import Navigation from "../services/navigation";
|
||||
import { NotePreviewWidget } from "../services/note-preview-widget";
|
||||
import Notifications from "../services/notifications";
|
||||
import { eAfterSync } from "../utils/events";
|
||||
import { useFavoriteStore } from "./use-favorite-store";
|
||||
import { useMenuStore } from "./use-menu-store";
|
||||
import { useMonographStore } from "./use-monograph-store";
|
||||
@@ -32,30 +34,25 @@ import { useTagStore } from "./use-tag-store";
|
||||
import { useTrashStore } from "./use-trash-store";
|
||||
import { useUserStore } from "./use-user-store";
|
||||
|
||||
export function initAfterSync() {
|
||||
Navigation.queueRoutesForUpdate();
|
||||
// Whenever sync completes, try to reschedule
|
||||
// any new/updated reminders.
|
||||
useUserStore.setState({
|
||||
profile: db.settings.getProfile()
|
||||
});
|
||||
initialize();
|
||||
NotePreviewWidget.updateNotes();
|
||||
export function initAfterSync(type: "full" | "send" = "send") {
|
||||
if (type === "full") {
|
||||
Navigation.queueRoutesForUpdate();
|
||||
// Whenever sync completes, try to reschedule
|
||||
// any new/updated reminders.
|
||||
Notifications.setupReminders(true);
|
||||
useRelationStore.getState().update();
|
||||
useMenuStore.getState().setColorNotes();
|
||||
useMenuStore.getState().setMenuPins();
|
||||
useUserStore.setState({
|
||||
profile: db.settings.getProfile()
|
||||
});
|
||||
|
||||
NotePreviewWidget.updateNotes();
|
||||
eSendEvent(eAfterSync);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initialize() {
|
||||
Notifications.setupReminders(true);
|
||||
useRelationStore.getState().update();
|
||||
useMenuStore.getState().setColorNotes();
|
||||
useMenuStore.getState().setMenuPins();
|
||||
useMonographStore.getState().refresh();
|
||||
useTrashStore.getState().refresh();
|
||||
useNotebookStore.getState().refresh();
|
||||
useNoteStore.getState().refresh();
|
||||
useTagStore.getState().refresh();
|
||||
useFavoriteStore.getState().refresh();
|
||||
useReminderStore.getState().refresh();
|
||||
}
|
||||
export async function initialize() {}
|
||||
|
||||
export function clearAllStores() {
|
||||
useNotebookStore.getState().clear();
|
||||
|
||||
@@ -175,3 +175,4 @@ export const eUpdateNoteInEditor = "620";
|
||||
export const eOnEnterEditor = "621";
|
||||
export const eOnExitEditor = "622";
|
||||
export const eEditorReset = "623";
|
||||
export const eAfterSync = "624";
|
||||
|
||||
Reference in New Issue
Block a user