switching to async storage

also switching to not storing music data from api unless downloaded
This commit is contained in:
austinried
2021-06-27 09:50:16 +09:00
parent 50be0a6f85
commit 71e34a6066
16 changed files with 350 additions and 200 deletions

View File

@@ -0,0 +1,98 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
const key = {
downloadedSongKeys: '@downloadedSongKeys',
downloadedAlbumKeys: '@downloadedAlbumKeys',
downloadedArtistKeys: '@downloadedArtistKeys',
downloadedPlaylistKeys: '@downloadedPlaylistKeys',
};
export async function getItem(key: string): Promise<string | null> {
try {
return await AsyncStorage.getItem(key);
} catch (e) {
console.error(`getItem error (key: ${key})`, e);
return null;
}
}
export async function multiGet(keys: string[]): Promise<[string, string | null][]> {
try {
return await AsyncStorage.multiGet(keys);
} catch (e) {
console.error(`multiGet error`, e);
return [];
}
}
export async function setItem(key: string, item: string): Promise<void> {
try {
await AsyncStorage.setItem(key, item);
} catch (e) {
console.error(`setItem error (key: ${key})`, e);
}
}
export async function multiSet(items: string[][]): Promise<void> {
try {
await AsyncStorage.multiSet(items);
} catch (e) {
console.error(`multiSet error`, e);
}
}
type DownloadedSong = {
id: string;
type: 'song';
name: string;
album: string;
artist: string;
};
type DownloadedAlbum = {
id: string;
type: 'album';
songs: string[];
name: string;
artist: string;
};
type DownloadedArtist = {
id: string;
type: 'artist';
songs: string[];
name: string;
};
type DownloadedPlaylist = {
id: string;
type: 'playlist';
songs: string[];
name: string;
};
export async function getDownloadedSongs(): Promise<DownloadedSong[]> {
const keysItem = await getItem(key.downloadedSongKeys);
const keys: string[] = keysItem ? JSON.parse(keysItem) : [];
const items = await multiGet(keys);
return items.map(x => {
const parsed = JSON.parse(x[1] as string);
return {
id: x[0],
type: 'song',
...parsed,
};
});
}
export async function setDownloadedSongs(items: DownloadedSong[]): Promise<void> {
await multiSet([
[key.downloadedSongKeys, JSON.stringify(items.map(x => x.id))],
...items.map(x => [x.id, JSON.stringify({
name: x.name,
album: x.album,
artist: x.artist,
})]),
]);
}

View File

@@ -1,92 +1,15 @@
import { AppSettings, ServerSettings } from '../models/settings';
import { DbStorage } from './db';
import { AppSettings } from '../models/settings';
import { getItem, setItem } from './asyncstorage';
export class SettingsDb extends DbStorage {
constructor() {
super({ name: 'settings.db', location: 'Library' });
}
const appSettingsKey = '@appSettings';
async createDb(): Promise<void> {
await this.initDb(tx => {
tx.executeSql(`
CREATE TABLE servers (
id TEXT PRIMARY KEY NOT NULL,
address TEXT NOT NULL,
username TEXT NOT NULL,
token TEXT NOT NULL,
salt TEXT NOT NULL
);
`);
tx.executeSql(`
CREATE TABLE app (
server TEXT
);
`);
tx.executeSql(`
INSERT INTO app (server)
VALUES (NULL);
`);
});
}
async getServers(): Promise<ServerSettings[]> {
return (await this.executeSql(`
SELECT * FROM servers;
`))[0].rows.raw().map(x => ({
id: x.id,
address: x.address,
username: x.username,
token: x.token,
salt: x.salt,
}));
}
async getServer(id: string): Promise<ServerSettings | undefined> {
return (await this.getServers()).find(x => x.id === id);
}
// async addServer(server: ServerSettings): Promise<void> {
// await this.executeSql(`
// INSERT INTO servers (id, address, username, token, salt)
// VALUES (?, ?, ?, ?, ?);
// `, [server.id, server.address, server.username, server.token, server.salt]);
// }
// async removeServer(id: string): Promise<void> {
// await this.executeSql(`
// DELETE FROM servers
// WHERE id = ?;
// `, [id]);
// }
async updateServers(servers: ServerSettings[]): Promise<void> {
await this.transaction((tx) => {
tx.executeSql(`
DELETE FROM servers
`);
for (const s of servers) {
tx.executeSql(`
INSERT INTO servers (id, address, username, token, salt)
VALUES (?, ?, ?, ?, ?);
`, [s.id, s.address, s.username, s.token, s.salt]);
}
});
}
async getApp(): Promise<AppSettings> {
return (await this.executeSql(`
SELECT * FROM app;
`))[0].rows.raw().map(x => ({
server: x.server || undefined,
}))[0];
}
async updateApp(app: AppSettings): Promise<void> {
await this.executeSql(`
UPDATE app SET
server = ?;
`, [
app.server || null,
]);
}
export async function getAppSettings(): Promise<AppSettings> {
const item = await getItem(appSettingsKey);
return item ? JSON.parse(item) : {
servers: [],
};
}
export async function setAppSettings(appSettings: AppSettings): Promise<void> {
await setItem(appSettingsKey, JSON.stringify(appSettings));
}