From 1ac78de724e80f35a22e4b5270b9a3bd890bd4df Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Mon, 13 Nov 2023 18:13:23 +1300 Subject: [PATCH] Fix some database drift --- App.tsx | 2 -- AppStack.tsx | 15 +++++++-------- EditPlan.tsx | 4 ++-- ExerciseList.tsx | 4 ++-- PlanItem.tsx | 4 ++-- PlanList.tsx | 4 ++-- SetList.tsx | 12 ++++++------ WeightList.tsx | 2 +- gym-set.ts | 16 ++++++++-------- plan.ts | 14 ++++++++++---- settings.ts | 37 +++++++++++++++++-------------------- weight.ts | 6 +++--- 12 files changed, 60 insertions(+), 60 deletions(-) diff --git a/App.tsx b/App.tsx index fff0d88..26ad353 100644 --- a/App.tsx +++ b/App.tsx @@ -46,8 +46,6 @@ const App = () => { darkColor: CombinedDarkTheme.colors.primary, }); - console.log("Rerendered App"); - useEffect(() => { (async () => { if (!AppDataSource.isInitialized) await AppDataSource.initialize(); diff --git a/AppStack.tsx b/AppStack.tsx index 0ee2995..bac5e3a 100644 --- a/AppStack.tsx +++ b/AppStack.tsx @@ -6,39 +6,38 @@ import EditPlan from "./EditPlan"; import EditSet from "./EditSet"; import EditSets from "./EditSets"; import EditWeight from "./EditWeight"; -import GymSet from "./gym-set"; -import { Plan } from "./plan"; import StartPlan from "./StartPlan"; import ViewGraph from "./ViewGraph"; import ViewSetList from "./ViewSetList"; import ViewWeightGraph from "./ViewWeightGraph"; +import GymSet from "./gym-set"; +import { Plan } from "./plan"; import Weight from "./weight"; -import Settings from "./settings"; export type StackParams = { Drawer: {}; EditSet: { - set: GymSet; + set: Partial; }; EditSets: { ids: number[]; }; EditPlan: { - plan: Plan; + plan: Partial; }; StartPlan: { plan: Plan; - first?: GymSet; + first: Partial; }; ViewGraph: { name: string; }; EditWeight: { - weight: Weight; + weight: Partial; }; ViewWeightGraph: {}; EditExercise: { - gymSet: GymSet; + gymSet: Partial; }; EditExercises: { names: string[]; diff --git a/EditPlan.tsx b/EditPlan.tsx index a5b7271..101ce5d 100644 --- a/EditPlan.tsx +++ b/EditPlan.tsx @@ -21,7 +21,7 @@ import { MARGIN, PADDING } from "./constants"; import { DAYS } from "./days"; import { planRepo, setRepo } from "./db"; import { DrawerParams } from "./drawer-param-list"; -import { defaultSet } from "./gym-set"; +import GymSet, { defaultSet } from "./gym-set"; import StackHeader from "./StackHeader"; import Switch from "./Switch"; @@ -144,7 +144,7 @@ export default function EditPlan() { const newPlan = await planRepo.findOne({ where: { id: plan.id }, }); - let first = await setRepo.findOne({ + let first: Partial = await setRepo.findOne({ where: { name: exercises[0] }, order: { created: "desc" }, }); diff --git a/ExerciseList.tsx b/ExerciseList.tsx index c004047..5dc70b2 100644 --- a/ExerciseList.tsx +++ b/ExerciseList.tsx @@ -12,7 +12,7 @@ import { LIMIT } from "./constants"; import { setRepo, settingsRepo } from "./db"; import DrawerHeader from "./DrawerHeader"; import ExerciseItem from "./ExerciseItem"; -import GymSet from "./gym-set"; +import GymSet, { defaultSet } from "./gym-set"; import ListMenu from "./ListMenu"; import Page from "./Page"; import SetList from "./SetList"; @@ -92,7 +92,7 @@ export default function ExerciseList() { const onAdd = useCallback(async () => { navigation.navigate("EditExercise", { - gymSet: new GymSet(), + gymSet: defaultSet, }); }, [navigation]); diff --git a/PlanItem.tsx b/PlanItem.tsx index ab0fd07..a34ca82 100644 --- a/PlanItem.tsx +++ b/PlanItem.tsx @@ -10,7 +10,7 @@ import { StackParams } from "./AppStack"; import { DARK_RIPPLE, LIGHT_RIPPLE } from "./constants"; import { DAYS } from "./days"; import { setRepo } from "./db"; -import { defaultSet } from "./gym-set"; +import GymSet, { defaultSet } from "./gym-set"; import { Plan } from "./plan"; import useDark from "./use-dark"; @@ -37,7 +37,7 @@ export default function PlanItem({ const start = useCallback(async () => { const exercise = item.exercises.split(",")[0]; - let first = await setRepo.findOne({ + let first: Partial = await setRepo.findOne({ where: { name: exercise }, order: { created: "desc" }, }); diff --git a/PlanList.tsx b/PlanList.tsx index c3591c0..10c36ea 100644 --- a/PlanList.tsx +++ b/PlanList.tsx @@ -12,7 +12,7 @@ import { planRepo } from "./db"; import DrawerHeader from "./DrawerHeader"; import ListMenu from "./ListMenu"; import Page from "./Page"; -import { Plan } from "./plan"; +import { Plan, defaultPlan } from "./plan"; import PlanItem from "./PlanItem"; export default function PlanList() { @@ -58,7 +58,7 @@ export default function PlanList() { const onAdd = () => navigation.navigate("EditPlan", { - plan: { title: "", days: "", exercises: "" }, + plan: defaultPlan, }); const edit = useCallback(async () => { diff --git a/SetList.tsx b/SetList.tsx index 9e0be42..be62002 100644 --- a/SetList.tsx +++ b/SetList.tsx @@ -8,13 +8,13 @@ import { FlatList } from "react-native"; import { List } from "react-native-paper"; import { Like } from "typeorm"; import { StackParams } from "./AppStack"; -import { LIMIT } from "./constants"; -import { getNow, setRepo, settingsRepo } from "./db"; import DrawerHeader from "./DrawerHeader"; -import GymSet, { defaultSet } from "./gym-set"; import ListMenu from "./ListMenu"; import Page from "./Page"; import SetItem from "./SetItem"; +import { LIMIT } from "./constants"; +import { getNow, setRepo, settingsRepo } from "./db"; +import GymSet, { defaultSet } from "./gym-set"; import Settings from "./settings"; export default function SetList() { @@ -35,8 +35,8 @@ export default function SetList() { skip: 0, order: { created: "DESC" }, }); - console.log(`${SetList.name}.reset:`, { value, offset }); setSets(newSets); + console.log(`${SetList.name}.reset:`, { value, offset }); setEnd(false); }, [offset] @@ -95,7 +95,7 @@ export default function SetList() { const onAdd = useCallback(async () => { const now = await getNow(); - let set = sets?.[0]; + let set: Partial = { ...sets[0] }; if (!set) set = { ...defaultSet }; set.created = now; delete set.id; @@ -150,7 +150,7 @@ export default function SetList() { onEndReached={next} onEndReachedThreshold={0.5} refreshing={refreshing} - keyExtractor={(set) => set.id?.toString()} + keyExtractor={(set) => set.id.toString()} onRefresh={() => { setOffset(0); setRefreshing(true); diff --git a/WeightList.tsx b/WeightList.tsx index f9b7f14..e86c059 100644 --- a/WeightList.tsx +++ b/WeightList.tsx @@ -100,7 +100,7 @@ export default function WeightList() { const onAdd = useCallback(async () => { const now = await getNow(); - let weight = weights?.[0]; + let weight: Partial = { ...weights[0] }; if (!weight) weight = { ...defaultWeight }; weight.created = now; delete weight.id; diff --git a/gym-set.ts b/gym-set.ts index b07ff02..2da855e 100644 --- a/gym-set.ts +++ b/gym-set.ts @@ -7,7 +7,7 @@ export const GYM_SET_CREATED = "gym-set-created"; @Entity("sets") export default class GymSet { @PrimaryGeneratedColumn() - id?: number; + id: number; @Column("text") name: string; @@ -18,6 +18,9 @@ export default class GymSet { @Column("int") weight: number; + @Column("text") + created: string; + @Column("int") sets = 3; @@ -31,19 +34,16 @@ export default class GymSet { hidden = false; @Column("text") - created: string; + unit = "kg"; @Column("text") - unit: string; + image: string | null; @Column("text") - image: string; - - @Column("text") - steps?: string; + steps: string | null; } -export const defaultSet: GymSet = { +export const defaultSet: Partial = { created: "", name: "", image: "", diff --git a/plan.ts b/plan.ts index ae7cb1c..58dcd2d 100644 --- a/plan.ts +++ b/plan.ts @@ -3,14 +3,20 @@ import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; @Entity("plans") export class Plan { @PrimaryGeneratedColumn() - id?: number; - - @Column("text") - title?: string; + id: number; @Column("text") days: string; @Column("text") exercises: string; + + @Column("text") + title: string | null; } + +export const defaultPlan: Partial = { + days: "", + exercises: "", + title: "", +}; diff --git a/settings.ts b/settings.ts index e5cc7f6..30f3435 100644 --- a/settings.ts +++ b/settings.ts @@ -3,56 +3,53 @@ import { Column, Entity, PrimaryColumn } from "typeorm"; @Entity() export default class Settings { @PrimaryColumn("boolean") - alarm: boolean; + alarm = false; @Column("boolean") - vibrate: boolean; + vibrate = true; @Column("text") - sound: string; + sound: string | null; @Column("boolean") - notify: boolean; + notify = false; @Column("boolean") - images: boolean; + images = true; @Column("boolean") - showUnit: boolean; + showUnit = true; @Column("text") - lightColor?: string; + lightColor: string | null; @Column("text") - darkColor?: string; + darkColor: string | null; @Column("boolean") - steps: boolean; + steps = true; @Column("text") - date: string; + date: string | null; @Column("boolean") - showDate: boolean; + showDate = false; @Column("text") - theme: string; + theme: string | null; @Column("boolean") - showSets: boolean; + noSound = false; @Column("boolean") - noSound: boolean; - - @Column("boolean") - backup: boolean; + backup = false; @Column("text") - backupDir: string; + backupDir: string | null; @Column("int") - duration: number; + duration: number | null; @Column("text") - startup: string; + startup: string | null; } diff --git a/weight.ts b/weight.ts index 0178266..b7c91b0 100644 --- a/weight.ts +++ b/weight.ts @@ -3,7 +3,7 @@ import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; @Entity("weights") export default class Weight { @PrimaryGeneratedColumn() - id?: number; + id: number; @Column("int") value: number; @@ -12,10 +12,10 @@ export default class Weight { created: string; @Column("text") - unit: string; + unit = "kg"; } -export const defaultWeight: Weight = { +export const defaultWeight: Partial = { created: "", unit: "kg", value: 0,