From 859ecea02767ef0272940da2f8f7cd4f99783447 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Sat, 2 Nov 2024 14:09:35 +0000 Subject: [PATCH] Remove auto refresh option --- src/app/browser/main/index.tsx | 47 ++++++---------------------------- src/app/browser/main/main.tsx | 13 +++++++++- src/app/browser/util.tsx | 13 ++-------- src/app/i18n/locale/de-de.ts | 2 -- src/app/i18n/locale/en-gb.ts | 2 -- src/app/i18n/locale/es-es.ts | 2 -- src/app/i18n/locale/ja-jp.ts | 2 -- src/app/main/ipc.ts | 1 + src/app/preload/index.ts | 5 ++++ 9 files changed, 28 insertions(+), 59 deletions(-) diff --git a/src/app/browser/main/index.tsx b/src/app/browser/main/index.tsx index ab2b48e..effbc5a 100644 --- a/src/app/browser/main/index.tsx +++ b/src/app/browser/main/index.tsx @@ -1,14 +1,14 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react'; -import { ScrollView, StyleSheet, Text, TouchableOpacity, useColorScheme, View } from 'react-native'; -import { CheckBox } from 'react-native-web'; -import { Translation, useTranslation } from 'react-i18next'; +import { ScrollView, StyleSheet, Text, useColorScheme, View } from 'react-native'; import ipc, { events } from '../ipc.js'; -import { AccentColourContext, getAccounts, Root, useAsync, useEventListener } from '../util.js'; +import { getAccounts, Root, useAsync, useEventListener } from '../util.js'; import Sidebar from './sidebar.js'; import Update from './update.js'; import Main from './main.js'; import { BACKGROUND_COLOUR_MAIN_DARK, BACKGROUND_COLOUR_MAIN_LIGHT, BACKGROUND_COLOUR_SECONDARY_DARK, BACKGROUND_COLOUR_SECONDARY_LIGHT, TEXT_COLOUR_DARK, TEXT_COLOUR_LIGHT } from '../constants.js'; +const REFRESH_INTERVAL = 30 * 1000; // 30 seconds + export interface AppProps { vibrancy?: boolean; insetTitleBarControls?: boolean; @@ -26,7 +26,8 @@ export default function App(props: AppProps) { const [selectedUserId, setSelectedUserId] = useState(undefined); const selectedUser = useMemo(() => users?.find(u => u.user.id === selectedUserId), [users, selectedUserId]); - const [auto_refresh, setAutoRefresh] = useState(undefined); + const [focused, setFocused] = useState(ipc.getWindowFocused()); + useEventListener(events, 'window:focused', setFocused, []); useEffect(() => { if (!selectedUser) setSelectedUserId(users?.[0]?.user.id); @@ -40,30 +41,14 @@ export default function App(props: AppProps) { > - - ( - setAutoRefresh(v ? 30 * 1000 : undefined)} - color={'#' + accent_colour} - style={styles.checkbox} - /> - )} /> - setAutoRefresh(auto_refresh ? undefined : 30 * 1000)}> - { - t => {t('enable_auto_refresh')} - } - - - + /> {selectedUser ?
: null} + autoRefresh={focused ? REFRESH_INTERVAL : undefined} /> : null} ; @@ -82,22 +67,6 @@ const styles = StyleSheet.create({ scrollerContent: { flex: 1, }, - content: { - paddingVertical: 16, - paddingHorizontal: 20, - }, - checkboxContainer: { - flex: 1, - marginRight: 20, - flexDirection: 'row', - alignItems: 'center', - }, - checkbox: { - marginRight: 10, - }, - checkboxLabel: { - flex: 1, - }, }); const light = StyleSheet.create({ diff --git a/src/app/browser/main/main.tsx b/src/app/browser/main/main.tsx index f4a6b20..1de2bd8 100644 --- a/src/app/browser/main/main.tsx +++ b/src/app/browser/main/main.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import { ActivityIndicator, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { useTranslation } from 'react-i18next'; import ipc, { events } from '../ipc.js'; @@ -28,11 +28,15 @@ export default function Main(props: { const [active_event, active_event_error, active_event_state, forceRefreshActiveEvent] = useAsync(useCallback(() => props.user.nsotoken ? ipc.getCoralActiveEvent(props.user.nsotoken) : Promise.resolve(null), [ipc, props.user.nsotoken])); + const [last_refresh_at, setLastRefreshAt] = useState(() => Date.now()); + const loading = announcements_state === RequestState.LOADING || friends_state === RequestState.LOADING || webservices_state === RequestState.LOADING || active_event_state === RequestState.LOADING; + const refresh = useCallback(() => Promise.all([ + setLastRefreshAt(Date.now()), forceRefreshFriends(), forceRefreshWebServices(), forceRefreshActiveEvent(), ]), [forceRefreshFriends, forceRefreshWebServices, forceRefreshActiveEvent]); @@ -42,6 +46,13 @@ export default function Main(props: { return () => clearTimeout(timeout); }, [ipc, props.user.nsotoken, loading, props.autoRefresh]); + useEffect(() => { + if (loading || !props.autoRefresh) return; + + // When enabling auto refresh, update now if we haven't updated within the interval + if (last_refresh_at + props.autoRefresh < Date.now()) refresh(); + }, [ipc, props.autoRefresh]); + useEventListener(events, 'window:refresh', refresh, []); const showErrorDetails = useCallback(() => { diff --git a/src/app/browser/util.tsx b/src/app/browser/util.tsx index c732ae7..685b5bd 100644 --- a/src/app/browser/util.tsx +++ b/src/app/browser/util.tsx @@ -30,17 +30,8 @@ export function Root(props: React.PropsWithChildren<{ const [accent_colour, setAccentColour] = React.useState(() => ipc.getAccentColour()); useEventListener(events, 'systemPreferences:accent-colour', setAccentColour, []); - const [window_focused, setWindowFocus] = useState(true); - - useEffect(() => { - const handler = (event: FocusEvent) => setWindowFocus(event.type === 'focus'); - window.addEventListener('focus', handler); - window.addEventListener('blur', handler); - return () => { - window.removeEventListener('focus', handler); - window.removeEventListener('blur', handler); - }; - }, []); + const [window_focused, setWindowFocused] = useState(ipc.getWindowFocused()); + useEventListener(events, 'window:focused', setWindowFocused, []); const [i18n, i18n_error] = useAsync(useCallback(async () => { const i18n = createI18n(); diff --git a/src/app/i18n/locale/de-de.ts b/src/app/i18n/locale/de-de.ts index 65642ec..fb39064 100644 --- a/src/app/i18n/locale/de-de.ts +++ b/src/app/i18n/locale/de-de.ts @@ -176,8 +176,6 @@ export const main_window = { add_user: 'User hinzufügen', discord_setup: 'Discord Rich Presence einrichten', - - enable_auto_refresh: 'Automatisch aktualisieren', }, update: { diff --git a/src/app/i18n/locale/en-gb.ts b/src/app/i18n/locale/en-gb.ts index 3e47b57..dd93912 100644 --- a/src/app/i18n/locale/en-gb.ts +++ b/src/app/i18n/locale/en-gb.ts @@ -170,8 +170,6 @@ export const main_window = { add_user: 'Add user', discord_setup: 'Set up Discord Rich Presence', - - enable_auto_refresh: 'Enable auto refresh', }, update: { diff --git a/src/app/i18n/locale/es-es.ts b/src/app/i18n/locale/es-es.ts index e25f1c5..5cc780b 100644 --- a/src/app/i18n/locale/es-es.ts +++ b/src/app/i18n/locale/es-es.ts @@ -177,8 +177,6 @@ export const main_window = { add_user: 'Añadir usuario', discord_setup: 'Configurar Discord Rich Presence', - - enable_auto_refresh: 'Activar actualización automática', }, update: { diff --git a/src/app/i18n/locale/ja-jp.ts b/src/app/i18n/locale/ja-jp.ts index 259c4d4..0214beb 100644 --- a/src/app/i18n/locale/ja-jp.ts +++ b/src/app/i18n/locale/ja-jp.ts @@ -134,8 +134,6 @@ export const main_window = { add_user: 'ユーザーを追加', discord_setup: 'Discord Rich Presenceを設定', - - enable_auto_refresh: '自動更新', }, update: { diff --git a/src/app/main/ipc.ts b/src/app/main/ipc.ts index ea870b4..b3d5323 100644 --- a/src/app/main/ipc.ts +++ b/src/app/main/ipc.ts @@ -27,6 +27,7 @@ export function setupIpc(appinstance: App, ipcMain: IpcMain) { ipcMain.on('nxapi:browser:getwindowdata', e => e.returnValue = getWindowConfiguration(e.sender)); ipcMain.on('nxapi:app:language', e => e.returnValue = appinstance.i18n.resolvedLanguage ?? appinstance.i18n.language); + ipcMain.on('nxapi:window:focused', e => e.returnValue = e.sender.isFocused()); let accent_colour = systemPreferences.getAccentColor?.() || undefined; diff --git a/src/app/preload/index.ts b/src/app/preload/index.ts index 7fd7378..ba3c841 100644 --- a/src/app/preload/index.ts +++ b/src/app/preload/index.ts @@ -100,6 +100,7 @@ const ipc = { getLanguage: () => language, getAccentColour: () => accent_colour, + getWindowFocused: () => focused, platform: process.platform, }; @@ -125,4 +126,8 @@ ipcRenderer.on('nxapi:systemPreferences:accent-colour', (event, c: string) => { events.emit('systemPreferences:accent-colour', c); }); +let focused: boolean = invSync('window:focused'); +window.addEventListener('focus', () => (focused = true, events.emit('window:focused', focused))); +window.addEventListener('blur', () => (focused = false, events.emit('window:focused', focused))); + contextBridge.exposeInMainWorld('nxapiElectronIpc', ipc);