diff --git a/src/app/settings/about.tsx b/src/app/settings/about.tsx index 91e1fe28..a300d4bd 100644 --- a/src/app/settings/about.tsx +++ b/src/app/settings/about.tsx @@ -1,9 +1,24 @@ import { getVersion } from '@tauri-apps/api/app'; +import { relaunch } from '@tauri-apps/plugin-process'; +import { Update, check } from '@tauri-apps/plugin-updater'; import { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; +import { toast } from 'sonner'; export function AboutScreen() { const [version, setVersion] = useState(''); + const [newUpdate, setNewUpdate] = useState(null); + + const checkUpdate = async () => { + const update = await check(); + if (!update) toast.info('There is no update available'); + setNewUpdate(update); + }; + + const installUpdate = async () => { + await newUpdate.downloadAndInstall(); + await relaunch(); + }; useEffect(() => { async function loadVersion() { @@ -20,19 +35,38 @@ export function AboutScreen() { Lume's logo

Lume

-

Version {version}

+

+ Version {version} +

+ {!newUpdate ? ( + + ) : ( + + )} Website Report a issue diff --git a/src/app/settings/general.tsx b/src/app/settings/general.tsx index e4cd86e0..529b3cc0 100644 --- a/src/app/settings/general.tsx +++ b/src/app/settings/general.tsx @@ -13,6 +13,7 @@ import { DarkIcon, LightIcon, SystemModeIcon } from '@shared/icons'; export function GeneralSettingScreen() { const { db } = useStorage(); const [settings, setSettings] = useState({ + autoupdate: false, autolaunch: false, outbox: false, media: true, @@ -59,6 +60,13 @@ export function GeneralSettingScreen() { setSettings((prev) => ({ ...prev, hashtag: !settings.hashtag })); }; + const toggleAutoupdate = async () => { + await db.createSetting('autoupdate', String(+!settings.autoupdate)); + db.settings.autoupdate = !settings.autoupdate; + // update state + setSettings((prev) => ({ ...prev, autoupdate: !settings.autoupdate })); + }; + const toggleNofitication = async () => { if (settings.notification) return; @@ -82,6 +90,12 @@ export function GeneralSettingScreen() { if (!data) return; data.forEach((item) => { + if (item.key === 'autoupdate') + setSettings((prev) => ({ + ...prev, + autoupdate: !!parseInt(item.value), + })); + if (item.key === 'outbox') setSettings((prev) => ({ ...prev, @@ -114,6 +128,19 @@ export function GeneralSettingScreen() { return (
+
+
+
Updater
+
Auto download new update at Login
+
+ toggleAutoupdate()} + className="relative h-7 w-12 cursor-default rounded-full bg-neutral-200 outline-none data-[state=checked]:bg-blue-500 dark:bg-neutral-800" + > + + +
Startup
diff --git a/src/libs/storage/instance.ts b/src/libs/storage/instance.ts index 929b0e3d..7aca95df 100644 --- a/src/libs/storage/instance.ts +++ b/src/libs/storage/instance.ts @@ -20,13 +20,18 @@ export class LumeStorage { public db: Database; public account: Account | null; public platform: Platform | null; - public settings: { outbox: boolean; media: boolean; hashtag: boolean }; + public settings: { + autoupdate: boolean; + outbox: boolean; + media: boolean; + hashtag: boolean; + }; constructor(sqlite: Database, platform: Platform) { this.db = sqlite; this.account = null; this.platform = platform; - this.settings = { outbox: false, media: true, hashtag: true }; + this.settings = { autoupdate: false, outbox: false, media: true, hashtag: true }; } public async secureSave(key: string, value: string) { diff --git a/src/libs/storage/provider.tsx b/src/libs/storage/provider.tsx index 3fdbdc13..e28b3a50 100644 --- a/src/libs/storage/provider.tsx +++ b/src/libs/storage/provider.tsx @@ -20,7 +20,7 @@ const StorageContext = createContext({ db: undefined, }); -const StorageProvider = ({ children }: PropsWithChildren) => { +const StorageInstance = () => { const [db, setDB] = useState(undefined); const [isNewVersion, setIsNewVersion] = useState(false); @@ -33,6 +33,8 @@ const StorageProvider = ({ children }: PropsWithChildren) => { if (!lumeStorage.account) await lumeStorage.getActiveAccount(); const settings = await lumeStorage.getAllSettings(); + let autoUpdater = false; + if (settings) { settings.forEach((item) => { if (item.key === 'outbox') lumeStorage.settings.outbox = !!parseInt(item.value); @@ -41,16 +43,23 @@ const StorageProvider = ({ children }: PropsWithChildren) => { if (item.key === 'hashtag') lumeStorage.settings.hashtag = !!parseInt(item.value); + + if (item.key === 'autoupdate') { + if (parseInt(item.value)) autoUpdater = true; + } }); } - // check update - const update = await check(); - if (update) { - setIsNewVersion(true); + if (autoUpdater) { + // check update + const update = await check(); + // install new version + if (update) { + setIsNewVersion(true); - await update.downloadAndInstall(); - await relaunch(); + await update.downloadAndInstall(); + await relaunch(); + } } setDB(lumeStorage); @@ -66,6 +75,12 @@ const StorageProvider = ({ children }: PropsWithChildren) => { if (!db) initLumeStorage(); }, []); + return { db, isNewVersion }; +}; + +const StorageProvider = ({ children }: PropsWithChildren) => { + const { db, isNewVersion } = StorageInstance(); + if (!db) return (
) => {

- {isNewVersion ? 'Found a new version, updating' : 'Checking for updates...'} + {isNewVersion ? 'Found a new version, updating...' : 'Starting...'}