Skip to content

Commit

Permalink
improve loading repos
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Aug 28, 2024
1 parent f4ca592 commit ac9eac6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 deletions.
7 changes: 6 additions & 1 deletion src/activitys/MainApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ const MainApplication = () => {
return [
{
content: (
<ModuleFragment id="explore" modules={modules} renderItem={(module, key) => <ExploreModule key={key} module={module} />} />
<ModuleFragment
id="explore"
modules={modules}
searchBy={["id", "name", "author", "__mmrl_repo_source"]}
renderItem={(module, key) => <ExploreModule key={key} module={module} />}
/>
),
tab: <Tabbar.Tab label={strings("explore")} />,
},
Expand Down
3 changes: 2 additions & 1 deletion src/activitys/fragments/ModuleFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ModuleFragmentProps {
modules: Array<Module>;
group?: FlatListProps<Module>["group"];
disableNoInternet?: boolean;
searchBy?: string[];
renderItem: renderFunc<Module>;
renderFixed?: RenderFunction;
}
Expand Down Expand Up @@ -118,7 +119,7 @@ const ModuleFragment = React.memo<ModuleFragmentProps>((props) => {
sortBy={filter}
search={{
term: search,
by: ["id", "name", "author"],
by: props.searchBy || ["id", "name", "author"],
//onEveryWord: true,
caseInsensitive: true,
}}
Expand Down
27 changes: 20 additions & 7 deletions src/components/module/ExploreModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Typography from "@mui/material/Typography";
import { SuFile } from "@Native/SuFile";
import { useModFS } from "@Hooks/useModFS";
import { useModuleInfo } from "@Hooks/useModuleInfo";
import { Verified, Tag, PersonOutline, CalendarMonth } from "@mui/icons-material";
import { Verified, Tag, PersonOutline, CalendarMonth, Source } from "@mui/icons-material";
import { useBlacklist } from "@Hooks/useBlacklist";
import Box from "@mui/material/Box";

Expand All @@ -22,7 +22,7 @@ interface Props {
}

const ExploreModule = React.memo<Props>((props) => {
const { id, name, author, description, track, timestamp, version, versions, versionCode, features } = props.module;
const { id, name, author, description, track, timestamp, version, versions, versionCode, features, __mmrl_repo_source } = props.module;
const { cover, verified } = useModuleInfo(props.module);

const { context } = useActivity();
Expand Down Expand Up @@ -106,11 +106,24 @@ const ExploreModule = React.memo<Props>((props) => {
{description}
</Typography>

<Stack direction="row" justifyContent="space-between" alignItems="center" spacing={0.5}>
<Typography sx={{ display: "flex", alignItems: "center", gap: 0.5 }} variant="body2">
<CalendarMonth sx={{ fontSize: "unset" }} />
{strings("last_updated", { date: formatLastUpdate })}
</Typography>
<Stack direction="row" justifyContent="space-between" alignItems="end" spacing={0.5}>
<Stack
direction="column"
spacing={0.5}
sx={{
justifyContent: "center",
alignItems: "flex-start",
}}
>
<Typography sx={{ display: "flex", alignItems: "center", gap: 0.5 }} variant="body2">
<CalendarMonth sx={{ fontSize: "unset" }} />
{strings("last_updated", { date: formatLastUpdate })}
</Typography>
<Typography color="text.secondary" sx={{ display: "flex", alignItems: "center", gap: 0.5 }} variant="body2">
<Source sx={{ fontSize: "unset" }} />
{__mmrl_repo_source && __mmrl_repo_source.join(", ")}
</Typography>
</Stack>
<Typography sx={{ display: "flex", alignItems: "center", gap: 0.5 }} variant="body2">
<Tag sx={{ fontSize: "unset" }} /> {versionCode}
</Typography>
Expand Down
40 changes: 26 additions & 14 deletions src/hooks/useRepos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const RepoProvider = (props: React.PropsWithChildren) => {
os.toast("You can't add more than 5 repos", Toast.LENGTH_SHORT);
}
} else {
os.toast("This repo alredy exist", Toast.LENGTH_SHORT);
os.toast("This repo already exists", Toast.LENGTH_SHORT);
}
};

Expand Down Expand Up @@ -134,26 +134,38 @@ export const RepoProvider = (props: React.PropsWithChildren) => {
);

React.useEffect(() => {
// Needs an another solution
setModules([]);
const fetchData = async () => {
const combinedModulesMap = new Map();

for (const repo of repos) {
if (disabledRepos.includes(repo.base_url)) continue;

fetch(`${repo.base_url}json/modules.json`)
.then((res) => {
if (!res.ok) throw new Error(res.statusText);
return res.json();
})
.then((json: Repo) => {
setModules((prev) => {
// Preventing duplicates
var ids = new Set(prev.map((d) => d.id));
var merged = [...prev, ...json.modules.filter((d) => !ids.has(d.id))];
return merged;
});
try {
const res = await fetch(`${repo.base_url.slice(-1) !== "/" ? repo.base_url + "/" : repo.base_url}json/modules.json`);
if (!res.ok) throw new Error(res.statusText);

const json: Repo = await res.json();

json.modules.forEach((mod) => {
if (!combinedModulesMap.has(mod.id)) {
// Add module with repo source if not already in map
combinedModulesMap.set(mod.id, { ...mod, __mmrl_repo_source: [repo.name] });
} else {
// If already in map, append source repo name
const existingModule = combinedModulesMap.get(mod.id);
if (!existingModule.__mmrl_repo_source.includes(repo.name)) {
existingModule.__mmrl_repo_source.push(repo.name);
}
}
});
} catch (error) {
log.e((error as Error).message);
}
}

// Convert map to array for final combined list
setModules(Array.from(combinedModulesMap.values()));
};

void fetchData();
Expand Down
1 change: 1 addition & 0 deletions src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ declare global {
* Local modules only
*/
__mmrl__local__module__?: boolean;
__mmrl_repo_source?: string[];
}

export interface BaseTrack {
Expand Down

0 comments on commit ac9eac6

Please sign in to comment.