Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve start up #2107

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions native/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import * as SplashScreen from "expo-splash-screen";

import MainDrawer from "@/app/UI/MainDrawer.tsx";
import Login from "@/app/UI/Login.tsx";
import DetailsView from "@/app/inventory/pages/details/DetailsView.tsx";
import type { RootStackParamList } from "@/app/Root.tsx";
import { useGGStore } from "@/app/store/GGStore.ts";

const RootStack = createNativeStackNavigator<RootStackParamList>();

function App() {
"use memo";

const stateHydrated = useGGStore((state) => state.stateHydrated);

if (!stateHydrated) {
return null;
}
SplashScreen.hideAsync();

return (
<RootStack.Navigator>
<RootStack.Group>
Expand Down
7 changes: 0 additions & 7 deletions native/app/App.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import MainDrawer from "@/app/UI/MainDrawer.tsx";
import Login from "@/app/UI/Login.tsx";
import DetailsView from "@/app/inventory/pages/details/DetailsView.tsx";
import type { RootStackParamList } from "@/app/Root.tsx";
import { useGGStore } from "@/app/store/GGStore.ts";

// Native app uses a native stack navigator which has no animations on web.
// so this is a workaround to get the animations working.
Expand All @@ -23,12 +22,6 @@ function App() {

injectWebCss();

const stateHydrated = useGGStore((state) => state.stateHydrated);

if (!stateHydrated) {
return null;
}

return (
<RootStack.Navigator screenOptions={{ animationEnabled: true }}>
<RootStack.Group>
Expand Down
1 change: 1 addition & 0 deletions native/app/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function Root() {
useEffect(() => {
if (authenticated === "NO-AUTHENTICATION" && stateHydrated) {
if (navigationRef.current) {
SplashScreen.hideAsync();
navigationRef.current.navigate("Login");
} else {
console.error("No navigationRef");
Expand Down
8 changes: 8 additions & 0 deletions native/app/UI/MainDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type DrawerContentComponentProps, createDrawerNavigator, DrawerItem } f
import { Image } from "expo-image";
import { Platform, StyleSheet, Text, TouchableOpacity, View } from "react-native";
import * as Haptics from "expo-haptics";
import * as SplashScreen from "expo-splash-screen";
import { useSafeAreaInsets } from "react-native-safe-area-context";

import { useGGStore } from "@/app/store/GGStore.ts";
Expand Down Expand Up @@ -170,6 +171,13 @@ function CustomDrawerContent({ navigation, state }: DrawerContentComponentProps)
export default function MainDrawer() {
"use memo";

const appReady = useGGStore((state) => state.appReady);

if (!appReady) {
return null;
}
SplashScreen.hideAsync();

return (
<Drawer.Navigator
drawerContent={CustomDrawerContent}
Expand Down
5 changes: 5 additions & 0 deletions native/app/store/Account/AccountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type AccountSliceGetter = Parameters<StateCreator<IStore, [], [], Account

export interface AccountSlice {
stateHydrated: boolean;
appReady: boolean;

appStartupTime: number;
refreshing: boolean;
Expand Down Expand Up @@ -131,6 +132,7 @@ export interface AccountSlice {

export const createAccountSlice: StateCreator<IStore, [], [], AccountSlice> = (set, get) => ({
stateHydrated: false,
appReady: false,

appStartupTime: 0,
refreshing: false,
Expand Down Expand Up @@ -169,6 +171,9 @@ export const createAccountSlice: StateCreator<IStore, [], [], AccountSlice> = (s
setInitialAccountDataReady: () => {
if (!get().initialAccountDataReady) {
set({ initialAccountDataReady: true });
if (get().itemsDefinitionReady && get().bungieDefinitionsReady) {
set({ appReady: true });
}
}
},
setAppStartupTime: (appStartupTime) => set({ appStartupTime }),
Expand Down
54 changes: 35 additions & 19 deletions native/app/store/DefinitionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export interface DefinitionsSlice {
inventorySectionWidth: number;
itemDefinitionVersion: string;
bungieDefinitionVersions: string;
setItemsDefinitionReady: () => void;
setBungieDefinitionsReady: () => void;
loadCustomDefinitions: (uniqueKey: string | null) => Promise<void>;
loadBungieDefinitions: (bungieManifest: BungieManifest | null) => Promise<void>;
showSnackBar: (message: string) => void;
Expand All @@ -86,22 +88,34 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
inventorySectionWidth: 0,
itemDefinitionVersion: "",
bungieDefinitionVersions: "",
setItemsDefinitionReady: () => {
set({ itemsDefinitionReady: true });
if (get().bungieDefinitionsReady && get().stateHydrated) {
set({ appReady: true });
}
},
setBungieDefinitionsReady: () => {
set({ bungieDefinitionsReady: true });
if (get().itemsDefinitionReady && get().stateHydrated) {
set({ appReady: true });
}
},
loadCustomDefinitions: async (uniqueKey) => {
const storedVersion = get().itemDefinitionVersion;
if (storedVersion === "") {
// download a version
console.log("download a version");
await downloadAndStoreItemDefinition(set);
await downloadAndStoreItemDefinition(get, set);
} else if (uniqueKey === null) {
// try to use the already downloaded version
await loadLocalItemDefinitionVersion(set);
await loadLocalItemDefinitionVersion(get, set);
} else if (uniqueKey === storedVersion) {
// use the already downloaded version
await loadLocalItemDefinitionVersion(set);
await loadLocalItemDefinitionVersion(get, set);
} else {
// download a new version
console.log("download a new version as KEY is different");
await downloadAndStoreItemDefinition(set);
await downloadAndStoreItemDefinition(get, set);
}
},
loadBungieDefinitions: async (bungieManifest) => {
Expand All @@ -117,17 +131,17 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
try {
if (storedVersion === "") {
// download a version
await downloadAndStoreBungieDefinitions(set, bungieManifest);
await downloadAndStoreBungieDefinitions(get, set, bungieManifest);
} else if (versionKey === null) {
// try to use the already downloaded version
await loadLocalBungieDefinitions(set);
await loadLocalBungieDefinitions(get, set);
} else if (versionKey === storedVersion) {
// use the already downloaded version
await loadLocalBungieDefinitions(set);
await loadLocalBungieDefinitions(get, set);
} else {
// download a new version
console.log("download a new bungie definitions as KEY is different");
await downloadAndStoreBungieDefinitions(set, bungieManifest);
await downloadAndStoreBungieDefinitions(get, set, bungieManifest);
}
updateBucketSizes();
updateDestinyText();
Expand All @@ -147,25 +161,25 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
},
});

async function loadLocalItemDefinitionVersion(set: DefinitionsSliceSetter): Promise<void> {
async function loadLocalItemDefinitionVersion(get: DefinitionsSliceGetter, set: DefinitionsSliceSetter): Promise<void> {
try {
const loadedDefinition = await getData("ITEM_DEFINITION", "getItemDefinition()");
const itemDefinition = parse(ItemResponseSchema, loadedDefinition);
set(parseAndSet(itemDefinition));
parseAndSet(get, itemDefinition);
} catch (e) {
console.error("Failed to load itemDefinition version. Downloading new version...", e);
await downloadAndStoreItemDefinition(set);
await downloadAndStoreItemDefinition(get, set);
}
}

async function downloadAndStoreItemDefinition(set: DefinitionsSliceSetter): Promise<void> {
async function downloadAndStoreItemDefinition(get: DefinitionsSliceGetter, set: DefinitionsSliceSetter): Promise<void> {
try {
const downloadedDefinition = await getCustomItemDefinition();
const itemDefinition = parse(ItemResponseSchema, downloadedDefinition);
const versionKey = itemDefinition.id;
set({ itemDefinitionVersion: versionKey });
await setData(itemDefinition as unknown as JSON, "ITEM_DEFINITION", "setupItemDefinition()");
return set(parseAndSet(itemDefinition));
parseAndSet(get, itemDefinition);
} catch (e) {
// TODO: Show big error message
console.error("Failed to download and save itemDefinition", e);
Expand All @@ -187,6 +201,7 @@ const NonInterpolationTable = [
let failCount = 0;

async function downloadAndStoreBungieDefinitions(
get: DefinitionsSliceGetter,
set: DefinitionsSliceSetter,
bungieManifest: BungieManifest | null,
): Promise<void> {
Expand Down Expand Up @@ -267,13 +282,14 @@ async function downloadAndStoreBungieDefinitions(
} else {
throw new Error("No DestinyInventoryBucketDefinition");
}
set({ bungieDefinitionVersions: versionKey, bungieDefinitionsReady: true });
set({ bungieDefinitionVersions: versionKey });
get().setBungieDefinitionsReady();
} catch (e) {
console.error("Failed to download and save bungieDefinition", e);
if (failCount < 3) {
failCount++;
console.error("Failed to download and save bungieDefinition", e);
await downloadAndStoreBungieDefinitions(set, bungieManifest);
await downloadAndStoreBungieDefinitions(get, set, bungieManifest);
} else {
// show error toast
console.error("Failed to download and save bungieDefinition", e);
Expand All @@ -282,7 +298,7 @@ async function downloadAndStoreBungieDefinitions(
}
}

async function loadLocalBungieDefinitions(set: DefinitionsSliceSetter): Promise<void> {
async function loadLocalBungieDefinitions(get: DefinitionsSliceGetter, set: DefinitionsSliceSetter): Promise<void> {
try {
const loadSocketTypeDefinition = await getAsyncStorage("DestinySocketCategoryDefinition");
const socketDefJson = JSON.parse(loadSocketTypeDefinition);
Expand All @@ -299,15 +315,15 @@ async function loadLocalBungieDefinitions(set: DefinitionsSliceSetter): Promise<
const loadInventoryBucketDefinition = await getAsyncStorage("DestinyInventoryBucketDefinition");
const inventoryBucketDefJson = JSON.parse(loadInventoryBucketDefinition);
setDestinyInventoryBucketDefinition(inventoryBucketDefJson as InventoryBucketDefinition);
set({ bungieDefinitionsReady: true });
get().setBungieDefinitionsReady();
} catch (e) {
console.error("Failed to load bungieDefinition version", e);
set({ bungieDefinitionVersions: "", bungieDefinitionsReady: false });
// TODO: Should error. Show something to the user that lets them restart the app. In the sense of running init again.
}
}

function parseAndSet(itemDefinition: ItemResponse) {
function parseAndSet(get: DefinitionsSliceGetter, itemDefinition: ItemResponse) {
setItemDefinition(itemDefinition.items as ItemsDefinition);
setBucketTypeHashArray(itemDefinition.helpers.BucketTypeHash);
setDamageTypeHashes(itemDefinition.helpers.DamageTypeHashes);
Expand Down Expand Up @@ -338,7 +354,7 @@ function parseAndSet(itemDefinition: ItemResponse) {
setUiPlugLabel(itemDefinition.helpers.UiPlugLabel);
setIcons(itemDefinition.helpers.Icons);

return { itemsDefinitionReady: true };
get().setItemsDefinitionReady();
}

function getData(storageKey: StorageKey, errorMessage: string): Promise<JSON> {
Expand Down