Skip to content

Commit

Permalink
add import/export data
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesthl committed Dec 20, 2023
1 parent dbdaefa commit 44e0809
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 22 deletions.
69 changes: 47 additions & 22 deletions apps/expo/app/settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Share } from "react-native";
import { router } from "expo-router";
import * as Sharing from "expo-sharing";
import {
Box,
ChevronRight,
Expand All @@ -12,7 +14,7 @@ import {
Info,
Mail,
Repeat,
Share,
Share as ShareIcon,
ShieldCheck,
SunMoon,
Trash,
Expand Down Expand Up @@ -119,7 +121,12 @@ const Settings = () => (
<Heart size={20} />
</View>
}
iconAfter={Share}
iconAfter={ShareIcon}
onPress={() => {
void Share.share({
url: "https://apps.apple.com/<country>/app/<app–name>/id<app-ID>", // TODO: Add app store link
});
}}
>
<ListItem.Text>{"Share Digital Break with friends"}</ListItem.Text>
</ListItem>
Expand All @@ -137,6 +144,14 @@ const Settings = () => (
</View>
}
iconAfter={Import}
onPress={async () => {
await SettingsStore.importData().then(() => {
void OverviewStore.init();
if (router.canGoBack()) {
router.back();
}
});
}}
>
<ListItem.Text>{"Import Data"}</ListItem.Text>
</ListItem>
Expand All @@ -151,30 +166,40 @@ const Settings = () => (
</View>
}
iconAfter={Upload}
>
<ListItem.Text>{"Export Data"}</ListItem.Text>
</ListItem>
</YGroup.Item>
<YGroup.Item>
<ListItem
hoverTheme
pressTheme
icon={
<View backgroundColor="$gray5" borderRadius={"$3"} padding="$2">
<Database size={20} />
</View>
}
onPress={() => {
void SettingsStore.generateRandomTestData().then(() => {
router.back();
void OverviewStore.init();
});
onPress={async () => {
const fileUri = await SettingsStore.generateExportFile();
if (fileUri) {
void Sharing.shareAsync(fileUri);
}
}}
iconAfter={Dices}
>
<ListItem.Text>{"Generate Random Data"}</ListItem.Text>
<ListItem.Text>{"Export Data"}</ListItem.Text>
</ListItem>
</YGroup.Item>
{process.env.NODE_ENV === "development" && (
<YGroup.Item>
<ListItem
hoverTheme
pressTheme
icon={
<View backgroundColor="$gray5" borderRadius={"$3"} padding="$2">
<Database size={20} />
</View>
}
onPress={() => {
void SettingsStore.generateRandomTestData().then(() => {
void OverviewStore.init();
if (router.canGoBack()) {
router.back();
}
});
}}
iconAfter={Dices}
>
<ListItem.Text>{"Generate Random Data"}</ListItem.Text>
</ListItem>
</YGroup.Item>
)}
</YGroup>
<H5>{"Legal Information"}</H5>
<YGroup alignSelf="center" bordered size="$4">
Expand Down
5 changes: 5 additions & 0 deletions apps/expo/data/app.statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ export class AppStatisticsStore {
await this.storage.deleteAll();
await this.init();
}

public async importEvents(events: Event[]) {
await this.storage.batchUpdate(events);
await this.init();
}
}
5 changes: 5 additions & 0 deletions apps/expo/data/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export class AppsStore {
await this.init();
};

public importApps = async (apps: App[]): Promise<void> => {
await this.storage.batchUpdate(apps);
await this.init();
};

public get apps() {
return this._apps;
}
Expand Down
44 changes: 44 additions & 0 deletions apps/expo/data/settings.store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as DocumentPicker from "expo-document-picker";
import * as FileSystem from "expo-file-system";
import { makeAutoObservable } from "mobx";

import { AppStatisticsStore } from "./app.statistics";
Expand All @@ -12,6 +14,48 @@ class SettingsStoreSingleton {
makeAutoObservable(this);
}

public async importData(): Promise<void> {
try {
const result = await DocumentPicker.getDocumentAsync({ type: "application/json" });
if (!result.canceled && result.assets.length > 0) {
const [file] = result.assets;
if (!file) {
throw new Error("No file uri");
}
const fileContents = await FileSystem.readAsStringAsync(file.uri);
const data = JSON.parse(fileContents) as {
apps: AppsStore["apps"];
appStatistics: AppStatisticsStore["events"];
};
await Promise.all([
this.appsStore.importApps(data.apps),
this.appStatisticsStore.importEvents(data.appStatistics),
]);
} else {
throw new Error("No file selected");
}
} catch (err) {
console.error("Error reading JSON file", err);
}
}

public async generateExportFile(): Promise<string | undefined> {
const fileName = "digital-break-app.export.json";
const fileUri = FileSystem.documentDirectory + fileName;
await Promise.all([this.appsStore.init(), this.appStatisticsStore.init()]);
const jsonData = JSON.stringify({
apps: this.appsStore.apps,
appStatistics: this.appStatisticsStore.events,
});
try {
await FileSystem.writeAsStringAsync(fileUri, jsonData);
console.log("File saved successfully!");
return fileUri;
} catch (error) {
console.error("Error creating JSON file", error);
}
}

public async generateRandomTestData() {
try {
await this.appsStore.getOrCreateApp({
Expand Down
12 changes: 12 additions & 0 deletions apps/expo/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ PODS:
- ExpoModulesCore
- ExpoCrypto (12.6.0):
- ExpoModulesCore
- ExpoDocumentPicker (11.5.4):
- ExpoModulesCore
- ExpoDynamicAppIcon (1.2.0):
- ExpoModulesCore
- ExpoExitApp (0.1.0):
Expand All @@ -97,6 +99,8 @@ PODS:
- React-NativeModulesApple
- React-RCTAppDelegate
- ReactCommon/turbomodule/core
- ExpoSharing (11.5.0):
- ExpoModulesCore
- ExpoSystemUI (2.6.0):
- ExpoModulesCore
- ExpoWebBrowser (12.5.0):
Expand Down Expand Up @@ -573,13 +577,15 @@ DEPENDENCIES:
- expo-dev-menu-interface (from `../node_modules/expo-dev-menu-interface/ios`)
- ExpoBlur (from `../../../node_modules/expo-blur/ios`)
- ExpoCrypto (from `../../../node_modules/expo-crypto/ios`)
- ExpoDocumentPicker (from `../../../node_modules/expo-document-picker/ios`)
- ExpoDynamicAppIcon (from `../../../node_modules/expo-dynamic-app-icon/ios`)
- ExpoExitApp (from `../../../packages/expo-exit-app/ios`)
- ExpoHead (from `../../../node_modules/expo-head/ios`)
- ExpoKeepAwake (from `../../../node_modules/expo-keep-awake/ios`)
- ExpoLinearGradient (from `../../../node_modules/expo-linear-gradient/ios`)
- ExpoLocalization (from `../../../node_modules/expo-localization/ios`)
- ExpoModulesCore (from `../../../node_modules/expo-modules-core`)
- ExpoSharing (from `../../../node_modules/expo-sharing/ios`)
- ExpoSystemUI (from `../../../node_modules/expo-system-ui/ios`)
- ExpoWebBrowser (from `../../../node_modules/expo-web-browser/ios`)
- EXSplashScreen (from `../node_modules/expo-splash-screen/ios`)
Expand Down Expand Up @@ -671,6 +677,8 @@ EXTERNAL SOURCES:
:path: "../../../node_modules/expo-blur/ios"
ExpoCrypto:
:path: "../../../node_modules/expo-crypto/ios"
ExpoDocumentPicker:
:path: "../../../node_modules/expo-document-picker/ios"
ExpoDynamicAppIcon:
:path: "../../../node_modules/expo-dynamic-app-icon/ios"
ExpoExitApp:
Expand All @@ -685,6 +693,8 @@ EXTERNAL SOURCES:
:path: "../../../node_modules/expo-localization/ios"
ExpoModulesCore:
:path: "../../../node_modules/expo-modules-core"
ExpoSharing:
:path: "../../../node_modules/expo-sharing/ios"
ExpoSystemUI:
:path: "../../../node_modules/expo-system-ui/ios"
ExpoWebBrowser:
Expand Down Expand Up @@ -802,13 +812,15 @@ SPEC CHECKSUMS:
expo-dev-menu-interface: 25a94ce9bead4668f624732d1f981b1791bbe8e2
ExpoBlur: 9e6da7c2bd4a0c5de7e57124694d0a380d7962f7
ExpoCrypto: 42485127a5968dda6f67ac5f2b42d3c0af31d7db
ExpoDocumentPicker: 5cb7389ff935b4addefdd466a606de51a512e922
ExpoDynamicAppIcon: b0f96e53d0bf00412bbe97da0cfc15b8c94a54ce
ExpoExitApp: 5ae87b4d35a0d3b28b5b26bdddcd734e0a41e7f3
ExpoHead: ac95331e2a45cb94f6aee8ba6b8cb3d0a2cc6817
ExpoKeepAwake: be4cbd52d9b177cde0fd66daa1913afa3161fc1d
ExpoLinearGradient: 5d18293f89c063036121281175c4653d6a7c34a2
ExpoLocalization: 2d5f47577d67ce991ebdd951edf14fe1db85fa06
ExpoModulesCore: c480fd4e3c7c8e81f0a6ba3a7c56869f25fe016d
ExpoSharing: 825b2b3fc919a2656f75def0069f584bbd6e359a
ExpoSystemUI: e4afa05539f42fe3be09d21a6498e5495019a5b2
ExpoWebBrowser: b6e56949734089d75f758f21cfe93fad02bd828c
EXSplashScreen: 5ed09ea490155ef603d007d9f194c9e04a4b7980
Expand Down
2 changes: 2 additions & 0 deletions apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
"expo-crypto": "~12.6.0",
"expo-dev-client": "~2.4.12",
"expo-dev-launcher": "^3.1.0",
"expo-document-picker": "~11.5.4",
"expo-dynamic-app-icon": "^1.2.0",
"expo-font": "~11.6.0",
"expo-linear-gradient": "~12.5.0",
"expo-linking": "~6.0.0",
"expo-localization": "^14.5.0",
"expo-router": "2.0.14",
"expo-sharing": "~11.5.0",
"expo-splash-screen": "~0.22.0",
"expo-status-bar": "~1.7.1",
"expo-system-ui": "~2.6.0",
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44e0809

Please sign in to comment.