FEATURE: add plain text password toggle to settings (#22)

* FEATURE: add plain text password toggle to settings

* clean up state types, lint, and add migrate

Co-authored-by: austinried <4966622+austinried@users.noreply.github.com>
This commit is contained in:
Theo Salzmann
2021-12-03 07:18:05 +01:00
committed by GitHub
parent 37214fcbdc
commit 9a6f8b86fc
6 changed files with 101 additions and 15 deletions

View File

@@ -4,10 +4,13 @@ import AsyncStorage from '@react-native-async-storage/async-storage'
import create from 'zustand'
import { persist, StateStorage } from 'zustand/middleware'
import { CacheSlice, createCacheSlice } from './cache'
import migrations from './migrations'
import { createMusicMapSlice, MusicMapSlice } from './musicmap'
import { createTrackPlayerSlice, TrackPlayerSlice } from './trackplayer'
import { createTrackPlayerMapSlice, TrackPlayerMapSlice } from './trackplayermap'
const DB_VERSION = migrations.length
export type Store = SettingsSlice &
MusicSlice &
MusicMapSlice &
@@ -51,6 +54,7 @@ export const useStore = create<Store>(
}),
{
name: '@appStore',
version: DB_VERSION,
getStorage: () => storage,
whitelist: ['settings', 'cacheFiles'],
onRehydrateStorage: _preState => {
@@ -59,6 +63,17 @@ export const useStore = create<Store>(
postState?.setHydrated(true)
}
},
migrate: (persistedState, version) => {
if (version > DB_VERSION) {
throw new Error('cannot migrate db on a downgrade, delete all data first')
}
for (let i = version; i < DB_VERSION; i++) {
persistedState = migrations[i](persistedState)
}
return persistedState
},
},
),
)