Skip to content

Commit

Permalink
Move database to database provider from watermelondb
Browse files Browse the repository at this point in the history
  • Loading branch information
chadmuro committed Nov 3, 2023
1 parent 1a2f997 commit 6fff365
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 112 deletions.
4 changes: 2 additions & 2 deletions app/(index)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useEffect } from "react";
import { Q } from "@nozbe/watermelondb";
import { useDatabase } from "@nozbe/watermelondb/react";
import { SplashScreen, Stack, useRouter } from "expo-router";

import { useDatabase } from "../../contexts/databaseContext";
import { useStudy } from "../../contexts/studyContext";
import Settings from "../../model/Settings";

export default function Layout() {
const { loading } = useStudy();
const router = useRouter();
const { database } = useDatabase();
const database = useDatabase();

useEffect(() => {
async function initialLoad() {
Expand Down
106 changes: 57 additions & 49 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { Suspense } from "react";
// import { useColorScheme } from "react-native";
import { useColorScheme } from "react-native";
import { DatabaseProvider, useDatabase } from "@nozbe/watermelondb/react";
import {
DarkTheme,
// DefaultTheme,
DefaultTheme,
ThemeProvider
} from "@react-navigation/native";
import { Edit3, List, Settings } from "@tamagui/lucide-icons";
Expand All @@ -11,15 +12,13 @@ import { SplashScreen, Tabs } from "expo-router";
import { StatusBar } from "expo-status-bar";
import { TamaguiProvider, Text, Theme } from "tamagui";

import { DatabaseProvider } from "../contexts/databaseContext";
import { StudyProvider } from "../contexts/studyContext";
import config from "../tamagui.config";
import databaseProvider from "../utils/database";

SplashScreen.preventAutoHideAsync();

export default function Layout() {
// const colorScheme = useColorScheme();

const [loaded] = useFonts({
Inter: require("@tamagui/font-inter/otf/Inter-Medium.otf"),
InterBold: require("@tamagui/font-inter/otf/Inter-Bold.otf")
Expand All @@ -30,51 +29,60 @@ export default function Layout() {
return (
<TamaguiProvider config={config}>
<Suspense fallback={<Text>Loading...</Text>}>
<Theme name="dark">
<ThemeProvider value={DarkTheme}>
<DatabaseProvider>
<StudyProvider>
<Tabs
screenOptions={{
headerShown: false,
tabBarActiveTintColor: "red"
}}
>
<Tabs.Screen
name="(index)"
options={{
title: "Study",
tabBarIcon: ({ focused }) => (
<Edit3 color={focused ? "red" : "white"} />
)
}}
/>
<Tabs.Screen
name="vocabulary"
options={{
title: "Vocabulary",
tabBarIcon: ({ focused }) => (
<List color={focused ? "red" : "white"} />
)
}}
/>
<Tabs.Screen
name="settings"
options={{
href: null,
title: "Settings",
tabBarIcon: ({ focused }) => (
<Settings color={focused ? "red" : "white"} />
)
}}
/>
</Tabs>
<StatusBar style="light" />
</StudyProvider>
</DatabaseProvider>
</ThemeProvider>
</Theme>
<DatabaseProvider database={databaseProvider}>
<StudyProvider>
<ThemeLayout />
</StudyProvider>
</DatabaseProvider>
</Suspense>
</TamaguiProvider>
);
}

function ThemeLayout() {
const database = useDatabase();
const colorScheme = useColorScheme();

return (
<Theme name="dark">
<ThemeProvider value={DarkTheme}>
<Tabs
screenOptions={{
headerShown: false,
tabBarActiveTintColor: "red"
}}
>
<Tabs.Screen
name="(index)"
options={{
title: "Study",
tabBarIcon: ({ focused }) => (
<Edit3 color={focused ? "red" : "$red5"} />
)
}}
/>
<Tabs.Screen
name="vocabulary"
options={{
title: "Vocabulary",
tabBarIcon: ({ focused }) => (
<List color={focused ? "red" : "$red5"} />
)
}}
/>
<Tabs.Screen
name="settings"
options={{
// href: null,
title: "Settings",
tabBarIcon: ({ focused }) => (
<Settings color={focused ? "red" : "$red5"} />
)
}}
/>
</Tabs>
<StatusBar style="light" />
</ThemeProvider>
</Theme>
);
}
58 changes: 0 additions & 58 deletions contexts/databaseContext.tsx

This file was deleted.

5 changes: 2 additions & 3 deletions contexts/studyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {
useState
} from "react";
import { Q } from "@nozbe/watermelondb";
import { useDatabase } from "@nozbe/watermelondb/react";
import dayjs from "dayjs";

import Review from "../model/Review";
import Study from "../model/Study";
import { generateRandomNumbers } from "../utils/generateNumbers";

import { useDatabase } from "./databaseContext";

type StudyContextType = {
study: Study;
studyIds: number[];
Expand Down Expand Up @@ -42,7 +41,7 @@ export const StudyContext = createContext<StudyContextType | undefined>(
);

const StudyProvider = ({ children }: PropsWithChildren<unknown>) => {
const { database } = useDatabase();
const database = useDatabase();
const [study, setStudy] = useState<Study | null>(null);
const [reviewCards, setReviewCards] = useState<Review[]>([]);
const [loading, setLoading] = useState(true);
Expand Down
31 changes: 31 additions & 0 deletions utils/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Database } from "@nozbe/watermelondb";
import SQLiteAdapter from "@nozbe/watermelondb/adapters/sqlite";

import migrations from "../model/migrations";
import Review from "../model/Review";
import schema from "../model/schema";
import Settings from "../model/Settings";
import Study from "../model/Study";

const adapter = new SQLiteAdapter({
schema,
// (You might want to comment it out for development purposes -- see Migrations documentation)
migrations,
// (optional database name or file system path)
// dbName: 'myapp',
// (recommended option, should work flawlessly out of the box on iOS. On Android,
// additional installation steps have to be taken - disable if you run into issues...)
jsi: true /* Platform.OS === 'ios' */,
// (optional, but you should implement this method)
onSetUpError: (error) => {
// Database failed to load -- offer the user to reload the app or log out
}
});

// Then, make a Watermelon database from it!
const database = new Database({
adapter,
modelClasses: [Review, Study, Settings]
});

export default database;

0 comments on commit 6fff365

Please sign in to comment.