Skip to content

Commit

Permalink
Fix chapters logic & Rework Plugins management (UI) (#993)
Browse files Browse the repository at this point in the history
* fix: not download/remove local chapters

* fix: deleteChapters callback

* remove plugin id: no sense fot user

* rework plugin management
  • Loading branch information
nyagami authored Mar 9, 2024
1 parent db47813 commit 202feb1
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 431 deletions.
2 changes: 1 addition & 1 deletion src/hooks/persisted/useNovel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export const useNovel = (novelPath: string, pluginId: string) => {
});
}
},
[novel],
[novel, chapters],
);

const getNovel = useCallback(async () => {
Expand Down
45 changes: 22 additions & 23 deletions src/hooks/persisted/usePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,34 @@ export default function usePlugins() {
};

const uninstallPlugin = (plugin: PluginItem) => {
return _uninstall(plugin).then(() => {
if (lastUsedPlugin?.id === plugin.id) {
MMKVStorage.delete(LAST_USED_PLUGIN);
}
const installedPlugins =
getMMKVObject<PluginItem[]>(INSTALLED_PLUGINS) || [];
const availablePlugins =
getMMKVObject<PluginsMap>(AVAILABLE_PLUGINS) || ({} as PluginsMap);
if (lastUsedPlugin?.id === plugin.id) {
MMKVStorage.delete(LAST_USED_PLUGIN);
}
const installedPlugins =
getMMKVObject<PluginItem[]>(INSTALLED_PLUGINS) || [];
const availablePlugins =
getMMKVObject<PluginsMap>(AVAILABLE_PLUGINS) || ({} as PluginsMap);

// safe
if (!availablePlugins[plugin.lang]?.some(_plg => _plg.id === plugin.id)) {
availablePlugins[plugin.lang] = [
...(availablePlugins[plugin.lang] || []),
plugin,
];
setMMKVObject(AVAILABLE_PLUGINS, availablePlugins);
}
setMMKVObject(
INSTALLED_PLUGINS,
installedPlugins.filter(plg => plg.id !== plugin.id),
);
filterPlugins(languagesFilter);
});
// safe
if (!availablePlugins[plugin.lang]?.some(_plg => _plg.id === plugin.id)) {
availablePlugins[plugin.lang] = [
...(availablePlugins[plugin.lang] || []),
plugin,
];
setMMKVObject(AVAILABLE_PLUGINS, availablePlugins);
}
setMMKVObject(
INSTALLED_PLUGINS,
installedPlugins.filter(plg => plg.id !== plugin.id),
);
filterPlugins(languagesFilter);
return _uninstall(plugin).then(() => {});
};

const updatePlugin = (plugin: PluginItem) => {
return _update(plugin).then(_plg => {
if (plugin.version === _plg?.version) {
throw new Error(getString('browseScreen.tryAgain'));
throw new Error('No update found!');
}
if (_plg) {
const installedPlugins =
Expand Down
101 changes: 8 additions & 93 deletions src/screens/browse/BrowseScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,17 @@ import { TabView, TabBar } from 'react-native-tab-view';
import color from 'color';

import { useSearch } from '@hooks';
import { useBrowseSettings, usePlugins, useTheme } from '@hooks/persisted';
import { usePlugins, useTheme } from '@hooks/persisted';
import { getString } from '@strings/translations';

import { Language } from '@utils/constants/languages';
import { EmptyView, SearchbarV2 } from '@components';
import { BrowseScreenProps } from '@navigators/types';
import { PluginsMap } from '@hooks/persisted/usePlugins';
import PluginSection from './components/PluginSection';
import { AvailableTab, InstalledTab } from './components/BrowseTabs';

const BrowseScreen = ({ navigation }: BrowseScreenProps) => {
const theme = useTheme();
const { searchText, setSearchText, clearSearchbar } = useSearch();
const {
filteredAvailablePlugins,
filteredInstalledPlugins,
languagesFilter,
lastUsedPlugin,
} = usePlugins();

const searchedInstalledPlugins = useMemo(() => {
return filteredInstalledPlugins.filter(plg =>
plg.name.toLocaleLowerCase().includes(searchText.toLocaleLowerCase()),
);
}, [searchText, filteredInstalledPlugins]);

const searchedAvailablePlugins = useMemo(() => {
return languagesFilter.reduce((pre, cur) => {
pre[cur] = filteredAvailablePlugins[cur]?.filter(plg =>
plg.name.toLocaleLowerCase().includes(searchText.toLocaleLowerCase()),
);
return pre;
}, {} as PluginsMap);
}, [searchText, filteredAvailablePlugins]);

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

const searchbarActions = useMemo(
() => [
Expand All @@ -58,58 +34,6 @@ const BrowseScreen = ({ navigation }: BrowseScreenProps) => {
[],
);

const availableSections = useMemo(() => {
const list = [];
if (searchText) {
list.push({
header: getString('common.searchResults'),
data: [],
});
languagesFilter.forEach(lang => {
const plugins = searchedAvailablePlugins[lang as Language];
if (plugins?.length) {
list.push({
header: lang,
data: plugins,
});
}
});
} else {
languagesFilter.forEach(lang => {
const plugins = filteredAvailablePlugins[lang as Language];
if (plugins?.length) {
list.push({
header: lang,
data: plugins,
});
}
});
}
return list;
}, [searchedAvailablePlugins]);

const installedSections = useMemo(() => {
const list = [];
if (searchText) {
list.push({
header: getString('common.searchResults'),
data: searchedInstalledPlugins,
});
} else if (filteredInstalledPlugins.length) {
if (lastUsedPlugin) {
list.push({
header: getString('browseScreen.lastUsed'),
data: [lastUsedPlugin],
});
}
list.push({
header: getString('browseScreen.installedPlugins'),
data: filteredInstalledPlugins,
});
}
return list;
}, [lastUsedPlugin, searchedInstalledPlugins]);

const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'installedRoute', title: getString('browseScreen.installed') },
Expand Down Expand Up @@ -141,23 +65,13 @@ const BrowseScreen = ({ navigation }: BrowseScreenProps) => {
}
switch (route.key) {
case 'availableRoute':
return (
<PluginSection
sections={availableSections}
installedTab={false}
theme={theme}
navigation={navigation}
/>
);
return <AvailableTab theme={theme} searchText={searchText} />;
default:
return (
<PluginSection
sections={installedSections}
installedTab={true}
showMyAnimeList={showMyAnimeList}
showAnilist={showAniList}
theme={theme}
<InstalledTab
navigation={navigation}
theme={theme}
searchText={searchText}
/>
);
}
Expand All @@ -182,6 +96,7 @@ const BrowseScreen = ({ navigation }: BrowseScreenProps) => {
android_ripple={{ color: theme.rippleColor }}
/>
)}
swipeEnabled={false}
/>
</>
);
Expand Down
Loading

0 comments on commit 202feb1

Please sign in to comment.