Skip to content

Commit

Permalink
feat: Repositories (LNReader#1051)
Browse files Browse the repository at this point in the history
* feat: Repositories

* feat: Repositories

* Cleanup

* Cleanup

* Cleanup
  • Loading branch information
rajarsheechatterjee authored Apr 28, 2024
1 parent c77dc52 commit 09f7319
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 283 deletions.
10 changes: 10 additions & 0 deletions src/database/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
} from './tables/ChapterTable';
import { dbTxnErrorCallback } from './utils/helpers';
import { noop } from 'lodash-es';
import {
createRepositoryTableQuery,
insertDefaultRepository,
} from './tables/RepositoryTable';

const dbName = 'lnreader.db';

Expand All @@ -28,6 +32,11 @@ export const createTables = () => {
tx.executeSql(createChapterTableQuery);
tx.executeSql(createChapterNovelIdIndexQuery);
});

db.transaction(tx => {
tx.executeSql(createRepositoryTableQuery);
tx.executeSql(insertDefaultRepository);
});
};

/**
Expand All @@ -41,6 +50,7 @@ export const deleteDatabase = async () => {
tx.executeSql('DROP TABLE NovelCategory');
tx.executeSql('DROP TABLE Chapter');
tx.executeSql('DROP TABLE Download');
tx.executeSql('DROP TABLE Repository');
},
dbTxnErrorCallback,
noop,
Expand Down
65 changes: 65 additions & 0 deletions src/database/queries/RepositoryQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as SQLite from 'expo-sqlite';

import { Repository } from '@database/types';

import { txnErrorCallback } from '../utils/helpers';
import { noop } from 'lodash-es';

const db = SQLite.openDatabase('lnreader.db');

const getRepositoriesQuery = 'SELECT * FROM Repository';

export const getRepositoriesFromDb = async (): Promise<Repository[]> => {
return new Promise(resolve =>
db.transaction(tx => {
tx.executeSql(
getRepositoriesQuery,
[],
(txObj, { rows }) => resolve((rows as any)._array),
txnErrorCallback,
);
}),
);
};

const isRepoUrlDuplicateQuery = `
SELECT COUNT(*) as isDuplicate FROM Repository WHERE url = ?
`;

export const isRepoUrlDuplicate = (repoUrl: string): Promise<boolean> => {
return new Promise(resolve =>
db.transaction(tx => {
tx.executeSql(
isRepoUrlDuplicateQuery,
[repoUrl],
(txObj, { rows }) => {
const { _array } = rows as any;
resolve(Boolean(_array[0]?.isDuplicate));
},
txnErrorCallback,
);
}),
);
};

const createRepositoryQuery = 'INSERT INTO Repository (url) VALUES (?)';

export const createRepository = (repoUrl: string): void =>
db.transaction(tx =>
tx.executeSql(createRepositoryQuery, [repoUrl], noop, txnErrorCallback),
);

const deleteRepositoryQuery = 'DELETE FROM Repository WHERE id = ?';

export const deleteRepositoryById = (id: number): void => {
db.transaction(tx => {
tx.executeSql(deleteRepositoryQuery, [id], noop, txnErrorCallback);
});
};

const updateRepositoryQuery = 'UPDATE Repository SET name = ? WHERE id = ?';

export const updateRepository = (id: number, url: string): void =>
db.transaction(tx =>
tx.executeSql(updateRepositoryQuery, [url, id], noop, txnErrorCallback),
);
10 changes: 10 additions & 0 deletions src/database/tables/RepositoryTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const createRepositoryTableQuery = `
CREATE TABLE IF NOT EXISTS Repository (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
UNIQUE(url)
);
`;

export const insertDefaultRepository =
'INSERT OR REPLACE INTO Repository (id, url) VALUES (1, "https://raw.githubusercontent.com/LNReader/lnreader-plugins/plugins/v2.1.0/.dist/plugins.min.json");';
5 changes: 5 additions & 0 deletions src/database/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ export interface BackupNovel extends NovelInfo {
export interface BackupCategory extends Category {
novelIds: number[];
}

export interface Repository {
id: number;
url: string;
}
9 changes: 8 additions & 1 deletion src/hooks/persisted/usePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ export default function usePlugins() {
);
setFilteredAvailablePlugins(
orderBy(
availablePlugins.filter(plg => filter.includes(plg.lang)),
availablePlugins
.filter(
avalilablePlugin =>
!installedPlugins.some(
installedPlugin => installedPlugin.id === avalilablePlugin.id,
),
)
.filter(plg => filter.includes(plg.lang)),
'name',
),
);
Expand Down
2 changes: 2 additions & 0 deletions src/navigators/MoreStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DownloadQueue from '../screens/more/DownloadQueueScreen';
import Downloads from '../screens/more/DownloadsScreen';
import AppearanceSettings from '../screens/settings/SettingsAppearanceScreen';
import CategoriesScreen from '@screens/Categories/CategoriesScreen';
import RespositorySettings from '@screens/settings/SettingsRepositoryScreen/SettingsRepositoryScreen';
// import LibrarySettings from '@screens/settings/SettingsLibraryScreen/SettingsLibraryScreen';
import StatsScreen from '@screens/StatsScreen/StatsScreen';
import { MoreStackParamList, SettingsStackParamList } from './types';
Expand All @@ -33,6 +34,7 @@ const SettingsStack = () => (
<Stack.Screen name="BackupSettings" component={BackupSettings} />
<Stack.Screen name="AppearanceSettings" component={AppearanceSettings} />
<Stack.Screen name="AdvancedSettings" component={AdvancedSettings} />
<Stack.Screen name="RespositorySettings" component={RespositorySettings} />
{/* <Stack.Screen name="LibrarySettings" component={LibrarySettings} /> */}
</Stack.Navigator>
);
Expand Down
1 change: 1 addition & 0 deletions src/navigators/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export type SettingsStackParamList = {
AppearanceSettings: undefined;
AdvancedSettings: undefined;
LibrarySettings: undefined;
RespositorySettings: undefined;
};

export type NovelScreenProps = StackScreenProps<RootStackParamList, 'Novel'>;
Expand Down
Loading

0 comments on commit 09f7319

Please sign in to comment.