Skip to content

Commit

Permalink
update baseStore and use store
Browse files Browse the repository at this point in the history
  • Loading branch information
BilligsterUser committed Jul 9, 2023
1 parent 7455e31 commit dd57cf7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
18 changes: 8 additions & 10 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import { FocusClaimCtx } from '@src/context/FocusClaim'
import { KeyboardProvider } from '@src/context/Keyboard'
import { PinCtx } from '@src/context/Pin'
import { ThemeContext } from '@src/context/Theme'
import { AsyncStore, secureStore } from '@store'
import { secureStore,store } from '@store'
import { addToHistory } from '@store/HistoryStore'
import { dark, globals, light } from '@styles'
import { formatInt, formatMintUrl, hasTrustedMint, isCashuToken, isErr, isNull, isStr, sleep } from '@util'
import { initCrashReporting, routingInstrumentation } from '@util/crashReporting'
import { routingInstrumentation } from '@util/crashReporting'
import { claimToken, isTokenSpendable, runRequestTokenLoop } from '@wallet'
import { getTokenInfo } from '@wallet/proofs'
import * as Clipboard from 'expo-clipboard'
Expand All @@ -45,8 +45,6 @@ interface ILockData {
timestamp: number
}

initCrashReporting()

void SplashScreen.preventAutoHideAsync()

export default function App(){
Expand Down Expand Up @@ -86,7 +84,7 @@ function _App() {
const handlePinForeground = async () => {
// check if app is locked
const now = Math.ceil(Date.now() / 1000)
const lockData = await AsyncStore.getObj<ILockData>('lock')
const lockData = await store.getObj<ILockData>('lock')
if (lockData) {
// set state acccording to lockData timestamp
const secsPassed = now - lockData.timestamp
Expand All @@ -98,7 +96,7 @@ function _App() {
})
}
// handle app was longer than 5 mins in the background
const bgTimestamp = await AsyncStore.get('authBg')
const bgTimestamp = await store.get('authBg')
if (isStr(bgTimestamp) && bgTimestamp.length > 0) {
if (now - +bgTimestamp > FiveMins) {
setBgAuth(true)
Expand Down Expand Up @@ -259,8 +257,8 @@ function _App() {
}
async function initAuth() {
// await secureStore.delete('pin')
// await AsyncStore.delete('pinSkipped')
const skipped = await AsyncStore.get('pinSkipped')
// await store.delete('pinSkipped')
const skipped = await store.get('pinSkipped')
const pinHash = await secureStore.get('pin')
setAuth({
shouldAuth: isNull(pinHash) ? '' : pinHash,
Expand All @@ -273,7 +271,7 @@ function _App() {
await initDB()
await initContacts()
await initPreferences()
const storedLng = await AsyncStore.get('settings:lang')
const storedLng = await store.get('settings:lang')
if (storedLng?.length) {
await i18n.changeLanguage(storedLng)
}
Expand Down Expand Up @@ -309,7 +307,7 @@ function _App() {
} else {
l('App has gone to the background!')
// store timestamp to activate auth after > 5mins in background
await AsyncStore.set('authBg', `${Math.ceil(Date.now() / 1000)}`)
await store.set('authBg', `${Math.ceil(Date.now() / 1000)}`)
}
appState.current = nextAppState
})
Expand Down
14 changes: 7 additions & 7 deletions src/components/screens/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MinuteInS } from '@consts/time'
import type { TAuthPageProps } from '@model/nav'
import { PinCtx } from '@src/context/Pin'
import { ThemeContext } from '@src/context/Theme'
import { AsyncStore, secureStore } from '@store'
import { secureStore,store } from '@store'
import { globals, highlight as hi } from '@styles'
import { formatSeconds, vib } from '@util'
import { hash256 } from '@util/crypto'
Expand Down Expand Up @@ -64,7 +64,7 @@ export default function AuthPage({ navigation, route }: TAuthPageProps) {
}
// store this info to avoid bypass state on app restart
if (!isConfirm) {
await AsyncStore.setObj('lock', { ...attemptState, timestamp: Math.ceil(Date.now() / 1000) })
await store.setObj('lock', { ...attemptState, timestamp: Math.ceil(Date.now() / 1000) })
}
setAttempts(attemptState)
// reset mismatch state
Expand Down Expand Up @@ -98,14 +98,14 @@ export default function AuthPage({ navigation, route }: TAuthPageProps) {
if (shouldRemove) {
await Promise.all([
secureStore.delete('pin'),
AsyncStore.set('pinSkipped', '1')
store.set('pinSkipped', '1')
])
setAuth('')
}
// remove the lock data and authbg in storage
await Promise.all([
AsyncStore.delete('lock'),
AsyncStore.delete('authBg')
store.delete('lock'),
store.delete('authBg')
])
resetStates()
// User wants to edit his PIN, do not navigate away, just update the state as he had no PIN so he can enter a new PIN
Expand All @@ -130,7 +130,7 @@ export default function AuthPage({ navigation, route }: TAuthPageProps) {
const hash = hash256(pinStr)
await Promise.all([
secureStore.set('pin', hash),
AsyncStore.delete('lock')
store.delete('lock')
])
resetStates()
setSuccess(true)
Expand Down Expand Up @@ -172,7 +172,7 @@ export default function AuthPage({ navigation, route }: TAuthPageProps) {
return
}
// skip pin setup
await AsyncStore.set('pinSkipped', '1')
await store.set('pinSkipped', '1')
navigation.navigate('dashboard')
}
// conditional rendering dots of pin input
Expand Down
4 changes: 2 additions & 2 deletions src/components/screens/Settings/Language.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Separator from '@comps/Separator'
import Txt from '@comps/Txt'
import TopNav from '@nav/TopNav'
import { ThemeContext } from '@src/context/Theme'
import { AsyncStore } from '@store'
import { store } from '@store'
import { globals, highlight as hi } from '@styles'
import { useContext } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -41,7 +41,7 @@ function LangSelection({ code, name, selected, hasSeparator }: ILangSelectionPro
const { color, highlight } = useContext(ThemeContext)
const handleLangChange = async () => {
await i18n.changeLanguage(code)
await AsyncStore.set('settings:lang', code)
await store.set('settings:lang', code)
}
return (
<>
Expand Down
3 changes: 3 additions & 0 deletions src/storage/store/SimpleKeyValueStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ export class SimpleKeyValueStore extends StoreBase {
}
public clear(): Promise<boolean> { return super.clear() }
public close(): void { return super.close() }
public delete(key: string): Promise<boolean> {
return super.removeItem(key)
}
}
12 changes: 8 additions & 4 deletions src/storage/store/StoreBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ export abstract class StoreBase {
)
return result?.item?.(0)?.value
}
protected async updateByValue(oldValue: string, newValue: string) :Promise<boolean> {
protected async updateByValue(oldValue: string, newValue: string): Promise<boolean> {
if (!this._isReady) {
await this._createStore()
if (!this._isReady) { return false }
}
const result = await this._db.exec({
sql: `UPDATE ${this._name} SET value = ? WHERE KEY in (SELECT KEY FROM ${this._name} WHERE value = ? LIMIT 1)`,
args:[newValue,oldValue]
args: [newValue, oldValue]
})
return !!(result && 'rowsAffected' in result && result?.rowsAffected===1)
return !!(result && 'rowsAffected' in result && result?.rowsAffected === 1)
}
protected async updateObjByValue<T extends object>(oldValue: T, newValue: T): Promise<boolean>{
protected async updateObjByValue<T extends object>(oldValue: T, newValue: T): Promise<boolean> {
if (!this._isReady) {
await this._createStore()
if (!this._isReady) { return false }
Expand Down Expand Up @@ -235,4 +235,8 @@ export abstract class StoreBase {
return !!(result?.insertId || result?.rowsAffected)
}
protected close(): void { return this._db.close() }
protected async removeItem(key: string) {
const result = await this._db.execTx({ sql: `delete from ${this._name} where key = ?`, args: [key] })
return result?.rowsAffected === 1
}
}
3 changes: 1 addition & 2 deletions src/storage/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AsyncStore } from './AsyncStore'
import { historyStore } from './HistoryStore'
import { SecureStore } from './SecureStore'
import { SimpleKeyValueStore } from './SimpleKeyValueStore'
Expand All @@ -7,4 +6,4 @@ const store = new SimpleKeyValueStore('__store__')

const secureStore = new SecureStore()

export { AsyncStore, historyStore, secureStore, SimpleKeyValueStore, store }
export { historyStore, secureStore, SimpleKeyValueStore, store }

0 comments on commit dd57cf7

Please sign in to comment.