Skip to content

Commit

Permalink
feat: ported to new Alert
Browse files Browse the repository at this point in the history
  • Loading branch information
Vali-98 committed Oct 13, 2024
1 parent e4cd2af commit 21f6696
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 231 deletions.
90 changes: 50 additions & 40 deletions app/AppSettingsMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Alert } from '@components/Alert'
import { rawdb } from '@db'
import { copyFile, DocumentDirectoryPath, DownloadDirectoryPath } from '@dr.pogodin/react-native-fs'
import { Style, AppSettings, Logger, Characters } from '@globals'
Expand All @@ -7,7 +8,7 @@ import { getDocumentAsync } from 'expo-document-picker'
import { documentDirectory, copyAsync, deleteAsync } from 'expo-file-system'
import { Stack, useRouter } from 'expo-router'
import React from 'react'
import { StyleSheet, Text, View, Switch, TouchableOpacity, Alert, ScrollView } from 'react-native'
import { StyleSheet, Text, View, Switch, TouchableOpacity, ScrollView } from 'react-native'
import { useMMKVBoolean } from 'react-native-mmkv'

const appVersion = appConfig.expo.version
Expand Down Expand Up @@ -44,50 +45,46 @@ const SwitchComponent: React.FC<SwitchComponentProps> = ({ title, value, onValue
)
}

const WarningAlert = (title: string, description: string, onPress: () => void) => {
Alert.alert(title, description, [
{ text: `Cancel`, style: `cancel` },
{
text: `Confirm`,
style: `destructive`,
onPress: onPress,
},
])
}

const exportDB = async () => {
const exportDB = async (notify: boolean = true) => {
await copyFile(
`${DocumentDirectoryPath}/SQLite/db.db`,
`${DownloadDirectoryPath}/${appVersion}-db-backup.db`
)
.then(() => {
Logger.log('Download Successful!', true)
if (notify) Logger.log('Download Successful!', true)
})
.catch((e) => Logger.log('Failed to copy database: ' + e, true))
}

const importDB = async (uri: string, name: string) => {
const copyDB = async () => {
rawdb.closeSync()
await exportDB()
await exportDB(false)
await deleteAsync(`${documentDirectory}SQLite/db.db`).catch(() => {
Logger.debug('Somehow the db is already deleted')
})
await copyAsync({
from: uri,
to: `${documentDirectory}SQLite/db.db`,
}).then(() => {
Logger.log('Copy Successful, Restarting now.')
reloadAppAsync()
})
.then(() => {
Logger.log('Copy Successful, Restarting now.')
reloadAppAsync()
})
.catch((e) => {
Logger.log(`Failed to import database: ${e}`, true)
})
}
const dbAppVersion = name.split('-')[0]

const dbAppVersion = name.split('-')?.[0]
if (dbAppVersion !== appVersion) {
WarningAlert(
'WARNING: Different Version',
`The imported database file has a different app version (${dbAppVersion}) than installed (${appVersion}), this may break or corrupt the database. It is recommended to use the same app version.`,
copyDB
)
Alert.alert({
title: `WARNING: Different Version`,
description: `The imported database file has a different app version (${dbAppVersion}) to installed version (${appVersion}).\n\nImporting this database may break or corrupt the database. It is recommended to use the same app version.`,
buttons: [
{ label: 'Cancel' },
{ label: 'Import Anyways', onPress: copyDB, type: 'warning' },
],
})
} else copyDB()
}

Expand Down Expand Up @@ -184,11 +181,14 @@ const AppSettingsMenu = () => {
<TouchableOpacity
style={styles.button}
onPress={() => {
WarningAlert(
`Regenerate Default Card`,
`This will add the default AI Bot card to your character list.`,
Characters.createDefaultCard
)
Alert.alert({
title: `Regenerate Default Card`,
description: `This will add the default AI Bot card to your character list.`,
buttons: [
{ label: 'Cancel' },
{ label: 'Create Default Card', onPress: Characters.createDefaultCard },
],
})
}}>
<Text style={styles.buttonText}>Regenerate Default Card</Text>
</TouchableOpacity>
Expand All @@ -200,11 +200,14 @@ const AppSettingsMenu = () => {
<TouchableOpacity
style={styles.button}
onPress={() => {
WarningAlert(
`Export Database`,
`Are you sure you want to export the database file?\n\nIt will automatically be downloaded to Downloads`,
exportDB
)
Alert.alert({
title: `Export Database`,
description: `Are you sure you want to export the database file?\n\nIt will automatically be downloaded to Downloads`,
buttons: [
{ label: 'Cancel' },
{ label: 'Export Database', onPress: exportDB },
],
})
}}>
<Text style={styles.buttonText}>Export Database</Text>
</TouchableOpacity>
Expand All @@ -214,11 +217,18 @@ const AppSettingsMenu = () => {
onPress={async () => {
getDocumentAsync({ type: ['application/*'] }).then(async (result) => {
if (result.canceled) return
WarningAlert(
`Import Database`,
`Are you sure you want to import this database? This may will destroy the current database!\n\nA backup will automatically be downloaded.\n\nApp will restart automatically`,
() => importDB(result.assets[0].uri, result.assets[0].name)
)
Alert.alert({
title: `Import Database`,
description: `Are you sure you want to import this database? This may will destroy the current database!\n\nA backup will automatically be downloaded.\n\nApp will restart automatically`,
buttons: [
{ label: 'Cancel' },
{
label: 'Import',
onPress: () =>
importDB(result.assets[0].uri, result.assets[0].name),
},
],
})
})
}}>
<Text style={styles.buttonText}>Import Database</Text>
Expand Down
19 changes: 9 additions & 10 deletions app/CharInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Alert } from '@components/Alert'
import AnimatedView from '@components/AnimatedView'
import useAutosave from '@constants/AutoSave'
import { FontAwesome } from '@expo/vector-icons'
Expand All @@ -14,7 +15,6 @@ import {
StyleSheet,
Image,
TouchableOpacity,
Alert,
ScrollView,
TextInput,
BackHandler,
Expand Down Expand Up @@ -79,24 +79,23 @@ const CharInfo = () => {
})

const deleteCard = () => {
Alert.alert(
`Delete Character`,
`Are you sure you want to delete this character? This cannot be undone.`,
[
{ text: 'Cancel', onPress: () => {}, style: 'cancel' },
Alert.alert({
title: `Delete Character`,
description: `Are you sure you want to delete '${charName}'? This cannot be undone.`,
buttons: [
{ label: 'Cancel' },
{
text: 'Confirm',
label: 'Delete Character',
onPress: () => {
Characters.db.mutate.deleteCard(charId ?? -1)
unloadCharacter()
unloadChat()
router.back()
},
style: 'destructive',
type: 'warning',
},
],
{ cancelable: true }
)
})
}

const handleImportImage = () => {
Expand Down
52 changes: 22 additions & 30 deletions app/Instruct.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { Alert } from '@components/Alert'
import AnimatedView from '@components/AnimatedView'
import CheckboxTitle from '@components/CheckboxTitle'
import SliderItem from '@components/SliderItem'
import TextBox from '@components/TextBox'
import TextBoxModal from '@components/TextBoxModal'
import useAutosave from '@constants/AutoSave'
import { FontAwesome } from '@expo/vector-icons'
import { Instructs, Logger, Style, saveStringToDownload } from '@globals'
import { useLiveQuery } from 'drizzle-orm/expo-sqlite'
import { Stack } from 'expo-router'
import { useState } from 'react'
import useAutosave from '@constants/AutoSave'
import {
View,
SafeAreaView,
TouchableOpacity,
StyleSheet,
Alert,
ScrollView,
Text,
} from 'react-native'
import { View, SafeAreaView, TouchableOpacity, StyleSheet, ScrollView, Text } from 'react-native'
import { Dropdown } from 'react-native-element-dropdown'
import { useMMKVNumber } from 'react-native-mmkv'

const Instruct = () => {
const { currentInstruct, loadInstruct, setCurrentInstruct } = Instructs.useInstruct(
Expand All @@ -42,20 +34,19 @@ const Instruct = () => {
}

const regenerateDefaults = () => {
Alert.alert(
`Regenerate Default Instructs`,
`Are you sure you want to regenerate default Instructs'?`,
[
{ text: `Cancel`, style: `cancel` },
Alert.alert({
title: `Regenerate Default Instructs`,
description: `Are you sure you want to regenerate default Instructs'?`,
buttons: [
{ label: 'Cancel' },
{
text: `Confirm`,
style: `destructive`,
label: 'Regenerate Default Presets',
onPress: async () => {
await Instructs.generateInitialDefaults()
},
},
]
)
],
})
}

useAutosave({ data: currentInstruct, onSave: () => handleSaveInstruct(false), interval: 3000 })
Expand Down Expand Up @@ -131,15 +122,15 @@ const Instruct = () => {
Logger.log(`Cannot delete last Instruct preset.`, true)
return
}
Alert.alert(
`Delete Preset`,
`Are you sure you want to delete '${currentInstruct?.name}'?`,
[
{ text: `Cancel`, style: `cancel` },

Alert.alert({
title: `Delete Preset`,
description: `Are you sure you want to delete '${currentInstruct?.name}'?`,
buttons: [
{ label: 'Cancel' },
{
text: `Confirm`,
style: `destructive`,
onPress: () => {
label: 'Delete Instruct',
onPress: async () => {
if (!instructID) return
const leftover = data.filter(
(item) => item.id !== instructID
Expand All @@ -151,9 +142,10 @@ const Instruct = () => {
Instructs.db.mutate.deleteInstruct(instructID)
loadInstruct(leftover[0].id)
},
type: 'warning',
},
]
)
],
})
}}>
<FontAwesome
size={24}
Expand Down
32 changes: 19 additions & 13 deletions app/Logs.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Alert } from '@components/Alert'
import AnimatedView from '@components/AnimatedView'
import { FontAwesome } from '@expo/vector-icons'
import { Global, Logger, Style, saveStringExternal } from '@globals'
import { Global, Logger, Style, saveStringToDownload } from '@globals'
import { FlashList } from '@shopify/flash-list'
import { Stack } from 'expo-router'
import { View, Text, StyleSheet, TouchableOpacity, Alert } from 'react-native'
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native'
import { useMMKVObject } from 'react-native-mmkv'

const Logs = () => {
Expand All @@ -14,25 +15,30 @@ const Logs = () => {
const handleExportLogs = () => {
if (!logs) return
const data = logs.join('\n')
saveStringExternal('logs.txt', data, 'text/plain')
saveStringToDownload(data, 'logs.txt', 'utf8')
.then(() => {
Logger.log('Logs Downloaded!', true)
})
.catch((e) => {
Logger.log(`Could Not Export Logs: ${e}`, true)
})
}

const handleFlushLogs = () => {
Alert.alert(
`Delete Logs`,
`Are you sure you want to delete logs? This cannot be undone.`,
[
{ text: 'Cancel', onPress: () => {}, style: 'cancel' },
Alert.alert({
title: `Delete Logs`,
description: `Are you sure you want to delete all logs? This cannot be undone.`,
buttons: [
{ label: 'Cancel' },
{
text: 'Confirm',
onPress: () => {
label: 'Delete Logs',
onPress: async () => {
Logger.flushLogs()
},
style: 'destructive',
type: 'warning',
},
],
{ cancelable: true }
)
})
}

return (
Expand Down
Loading

0 comments on commit 21f6696

Please sign in to comment.