diff --git a/App.tsx b/App.tsx index 44863be9..d790daaa 100644 --- a/App.tsx +++ b/App.tsx @@ -42,6 +42,30 @@ import {openUrl, sleep} from '@rvmob/lib/utils'; const isFoss = getBundleId().match('foss'); +async function openLastChannel() { + try { + const lastServer = await AsyncStorage.getItem('lastServer'); + if (lastServer) { + app.openServer(client.servers.get(lastServer)); + try { + const channelData = await AsyncStorage.getItem('serverLastChannels'); + let serverLastChannels = JSON.parse(channelData || '{}') || {}; + let lastChannel = serverLastChannels[lastServer]; + if (lastChannel) { + let fetchedLastChannel = client.channels.get(lastChannel); + if (fetchedLastChannel) { + app.openChannel(fetchedLastChannel); + } + } + } catch (channelErr) { + console.log(`[APP] Error getting last channel: ${channelErr}`); + } + } + } catch (serverErr) { + console.log(`[APP] Error getting last server: ${serverErr}`); + } +} + class MainView extends ReactComponent { constructor(props) { super(props); @@ -157,39 +181,23 @@ class MainView extends ReactComponent { imports.setUpNotifeeListener(client, this.setState); } - AsyncStorage.getItem('lastServer', async (err, lastServer) => { - if (!err) { - if (lastServer) { - app.openServer(client.servers.get(lastServer)); - await AsyncStorage.getItem('serverLastChannels', (cerr, data) => { - if (!cerr) { - let serverLastChannels = JSON.parse(data || '{}') || {}; - let lastChannel = serverLastChannels[lastServer]; - if (lastChannel) { - let fetchedLastChannel = client.channels.get(lastChannel); - if (fetchedLastChannel) { - app.openChannel(fetchedLastChannel); - } - } - } else { - console.log(`[APP] Error getting last channel: ${err}`); - } - }); - } - } else { - console.log(`[APP] Error getting last server: ${err}`); - } - }); + if (app.settings.get('app.reopenLastChannel')) { + await openLastChannel(); + } }); + client.on('dropped', async () => { this.setState({network: 'dropped'}); }); client.on('message', async msg => { console.log(`[APP] Handling message ${msg._id}`); - let channelNotif = this.state.channelNotifications ? this.state.channelNotifications[msg.channel?._id] : undefined; - let serverNotif = - this.state.serverNotifications ? this.state.serverNotifications[msg.channel?.server?._id] : undefined; + let channelNotif = this.state.channelNotifications + ? this.state.channelNotifications[msg.channel?._id] + : undefined; + let serverNotif = this.state.serverNotifications + ? this.state.serverNotifications[msg.channel?.server?._id] + : undefined; const isMuted = (channelNotif && channelNotif === 'none') || diff --git a/i18n/strings/en.json b/i18n/strings/en.json index 04ad5743..7293307f 100644 --- a/i18n/strings/en.json +++ b/i18n/strings/en.json @@ -170,6 +170,7 @@ "app": { "language": "Select your language", "refetchOnReconnect": "Refetch messages when reconnecting", + "reopenLastChannel": "Reopen the last channel you were in when opening the app", "notifications": { "enabled": "Enable push notifications", "notifyOnSelfPing": "Receive a notification when you ping yourself" diff --git a/src/Generic.tsx b/src/Generic.tsx index 1223a90e..a0c48974 100644 --- a/src/Generic.tsx +++ b/src/Generic.tsx @@ -162,6 +162,12 @@ export const app = { default: true, type: 'boolean', }, + { + key: 'app.reopenLastChannel', + category: 'functionality', + default: true, + type: 'boolean', + }, { key: 'app.notifications.enabled', category: 'functionality',