Skip to content

Commit

Permalink
basically done
Browse files Browse the repository at this point in the history
  • Loading branch information
Soopyboo32 committed Sep 25, 2024
1 parent bb57863 commit b5658e1
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/hooks/persisted/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface AppSettings {
export interface BrowseSettings {
showMyAnimeList: boolean;
showAniList: boolean;
globalSearchConcurrency: number;
}

export interface LibrarySettings {
Expand Down Expand Up @@ -153,6 +154,7 @@ const initialAppSettings: AppSettings = {
const initialBrowseSettings: BrowseSettings = {
showMyAnimeList: true,
showAniList: true,
globalSearchConcurrency: 5,
};

export const initialChapterGeneralSettings: ChapterGeneralSettings = {
Expand Down
2 changes: 1 addition & 1 deletion src/navigators/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import MigrateNovel from '../screens/browse/migration/MigrationNovels';
import MalTopNovels from '../screens/browse/discover/MalTopNovels';
import AniListTopNovels from '../screens/browse/discover/AniListTopNovels';
import NewUpdateDialog from '../components/NewUpdateDialog';
import BrowseSettings from '../screens/browse/BrowseSettings';
import BrowseSettings from '@screens/browse/settings/BrowseSettings';
import WebviewScreen from '@screens/WebviewScreen/WebviewScreen';
import { RootStackParamList } from './types';
import Color from 'color';
Expand Down
18 changes: 2 additions & 16 deletions src/screens/GlobalSearchScreen/GlobalSearchScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,19 @@ interface Props {
};
}

let lastSearchStart = 0;

const GlobalSearchScreen = (props: Props) => {
const theme = useTheme();
const { searchText, setSearchText, clearSearchbar } = useSearch(
props?.route?.params?.searchText,
);
const onChangeText = (text: string) => setSearchText(text);
const onSubmitEditing = () => {
globalSearch(searchText);
lastSearchStart = Date.now();
};
const onSubmitEditing = () => globalSearch(searchText);

const { searchResults, globalSearch, progress } = useGlobalSearch({
defaultSearchText: searchText,
});

console.log('Rendering... ' + progress);
if (progress >= 0.9 && lastSearchStart > 0) {
console.log('Search took ' + (Date.now() - lastSearchStart) + 'ms');
lastSearchStart = 0;
}

let start = Date.now();
let ret = (
return (
<>
<SearchbarV2
searchText={searchText}
Expand Down Expand Up @@ -71,8 +59,6 @@ const GlobalSearchScreen = (props: Props) => {
/>
</>
);
console.log('Rendering took ' + (Date.now() - start) + 'ms');
return ret;
};

export default GlobalSearchScreen;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import GlobalSearchNovelItem from './GlobalSearchNovelItem';
import { useLibraryNovels } from '@screens/library/hooks/useLibrary';
import { LibraryNovelInfo } from '@database/types';
import { switchNovelToLibrary } from '@database/queries/NovelQueries';
import GlobalSearchSkeletonLoading from '@screens/browse/loadingAnimation/GlobalSearchSkeletonLoading';

interface GlobalSearchResultsListProps {
searchResults: GlobalSearchResult[];
Expand Down Expand Up @@ -60,7 +61,7 @@ const GlobalSearchSourceResults: React.FC<{ item: GlobalSearchResult }> = ({
[],
);

let elm = useMemo(
return useMemo(
() => (
<>
<View>
Expand Down Expand Up @@ -94,9 +95,9 @@ const GlobalSearchSourceResults: React.FC<{ item: GlobalSearchResult }> = ({
/>
</Pressable>
{item.isLoading ? (
// <GlobalSearchSkeletonLoading theme={theme} />
<Text style={[styles.error, { color: errorColor }]}>Loading</Text>
) : item.error ? (
<GlobalSearchSkeletonLoading theme={theme} />
) : // <Text style={[styles.error, {color: errorColor}]}>Loading</Text>
item.error ? (
<Text style={[styles.error, { color: errorColor }]}>
{item.error}
</Text>
Expand Down Expand Up @@ -155,8 +156,6 @@ const GlobalSearchSourceResults: React.FC<{ item: GlobalSearchResult }> = ({
),
[item.isLoading],
);

return elm;
};

export default GlobalSearchResultsList;
Expand Down
16 changes: 9 additions & 7 deletions src/screens/GlobalSearchScreen/hooks/useGlobalSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react';

import { NovelItem, PluginItem } from '@plugins/types';
import { getPlugin } from '@plugins/pluginManager';
import { usePlugins } from '@hooks/persisted';
import { useBrowseSettings, usePlugins } from '@hooks/persisted';

interface Props {
defaultSearchText?: string;
Expand All @@ -23,6 +23,8 @@ export const useGlobalSearch = ({ defaultSearchText }: Props) => {
const [searchResults, setSearchResults] = useState<GlobalSearchResult[]>([]);
const [progress, setProgress] = useState(0);

const { globalSearchConcurrency } = useBrowseSettings();

const globalSearch = (searchText: string) => {
const defaultResult: GlobalSearchResult[] = filteredInstalledPlugins.map(
plugin => ({
Expand All @@ -35,11 +37,6 @@ export const useGlobalSearch = ({ defaultSearchText }: Props) => {

setSearchResults(defaultResult);

//Sort so we load the plugins results in the same order as they show on the list
let filteredSortedInstalledPlugins = [...filteredInstalledPlugins].sort(
(a, b) => a.name.localeCompare(b.name),
);

let running = 0;

async function searchInPlugin(_plugin: PluginItem) {
Expand Down Expand Up @@ -98,12 +95,17 @@ export const useGlobalSearch = ({ defaultSearchText }: Props) => {
}
}

//Sort so we load the plugins results in the same order as they show on the list
let filteredSortedInstalledPlugins = [...filteredInstalledPlugins].sort(
(a, b) => a.name.localeCompare(b.name),
);

(async () => {
for (let _plugin of filteredSortedInstalledPlugins) {
if (!isMounted.current) {
break;
}
while (running >= 2) {
while (running >= globalSearchConcurrency) {
await new Promise(resolve => setTimeout(resolve, 100));
}
searchInPlugin(_plugin).then();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ import { FlatList, StyleSheet } from 'react-native';
import React from 'react';
import { Appbar, List, SwitchItem } from '@components';

import { useBrowseSettings, usePlugins, useTheme } from '@hooks/persisted';
import {
useBrowseSettings,
usePlugins,
useTheme,
} from '@hooks/persisted/index';
import { getString } from '@strings/translations';
import { languages } from '@utils/constants/languages';
import { BrowseSettingsScreenProp } from '@navigators/types';
import { BrowseSettingsScreenProp } from '@navigators/types/index';
import { useBoolean } from '@hooks';
import ConcurrentSearchesModal from '@screens/browse/settings/modals/ConcurrentSearchesModal';

const BrowseSettings = ({ navigation }: BrowseSettingsScreenProp) => {
const theme = useTheme();
const { goBack } = navigation;

const { languagesFilter, toggleLanguageFilter } = usePlugins();
const { showMyAnimeList, showAniList, setBrowseSettings } =
useBrowseSettings();
const {
showMyAnimeList,
showAniList,
globalSearchConcurrency,
setBrowseSettings,
} = useBrowseSettings();

const globalSearchConcurrencyModal = useBoolean();

return (
<>
Expand All @@ -22,16 +34,23 @@ const BrowseSettings = ({ navigation }: BrowseSettingsScreenProp) => {
handleGoBack={goBack}
theme={theme}
/>
<ConcurrentSearchesModal
globalSearchConcurrency={globalSearchConcurrency}
modalVisible={globalSearchConcurrencyModal.value}
hideModal={globalSearchConcurrencyModal.setFalse}
theme={theme}
/>
<FlatList
contentContainerStyle={styles.container}
ListHeaderComponent={
<>
<List.SubHeader theme={theme}>
{getString('browseScreen.globalSearch')}
</List.SubHeader>
<List.InfoItem
title={getString('browseSettingsScreen.searchAllWarning')}
icon="information-outline"
<List.Item
title={getString('browseSettingsScreen.concurrentSearches')}
description={globalSearchConcurrency.toString()}
onPress={globalSearchConcurrencyModal.setTrue}
theme={theme}
/>
<List.Divider theme={theme} />
Expand Down
68 changes: 68 additions & 0 deletions src/screens/browse/settings/modals/ConcurrentSearchesModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';
import { Text, StyleSheet } from 'react-native';

import { Portal, Modal, overlay } from 'react-native-paper';

import { RadioButton } from '@components/RadioButton/RadioButton';
import { ThemeColors } from '@theme/types';
import { getString } from '@strings/translations';
import { useBrowseSettings } from '@hooks/persisted/index';

interface DisplayModeModalProps {
globalSearchConcurrency: number;
modalVisible: boolean;
hideModal: () => void;
theme: ThemeColors;
}

const ConcurrentSearchesModal: React.FC<DisplayModeModalProps> = ({
theme,
globalSearchConcurrency,
hideModal,
modalVisible,
}) => {
const { setBrowseSettings } = useBrowseSettings();

return (
<Portal>
<Modal
visible={modalVisible}
onDismiss={hideModal}
contentContainerStyle={[
styles.containerStyle,
{ backgroundColor: overlay(2, theme.surface) },
]}
>
<Text style={[styles.modalHeader, { color: theme.onSurface }]}>
{getString('browseSettingsScreen.concurrentSearches')}
</Text>
{[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(concurrency => (
<RadioButton
key={concurrency}
status={globalSearchConcurrency === concurrency}
onPress={() =>
setBrowseSettings({ globalSearchConcurrency: concurrency })
}
label={concurrency.toString()}
theme={theme}
/>
))}
</Modal>
</Portal>
);
};

export default ConcurrentSearchesModal;

const styles = StyleSheet.create({
containerStyle: {
paddingVertical: 20,
margin: 20,
borderRadius: 28,
},
modalHeader: {
paddingHorizontal: 24,
fontSize: 24,
marginBottom: 10,
},
});
1 change: 1 addition & 0 deletions strings/languages/en/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
},
"browseSettings": "Browse Settings",
"browseSettingsScreen": {
"concurrentSearches": "Concurrent Source Searches",
"languages": "Languages",
"onlyShowPinnedSources": "Only show pinned sources",
"searchAllSources": "Search all sources",
Expand Down
1 change: 1 addition & 0 deletions strings/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface StringMap {
'browseScreen.updateFailed': 'string';
'browseScreen.updatedTo': 'string';
'browseSettings': 'string';
'browseSettingsScreen.concurrentSearches': 'string';
'browseSettingsScreen.languages': 'string';
'browseSettingsScreen.onlyShowPinnedSources': 'string';
'browseSettingsScreen.searchAllSources': 'string';
Expand Down

0 comments on commit b5658e1

Please sign in to comment.