Skip to content
This repository was archived by the owner on Aug 24, 2026. It is now read-only.
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
23 changes: 23 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"react-native": "0.79.6",
"react-native-draggable-flatlist": "^4.0.3",
"react-native-gesture-handler": "~2.24.0",
"react-native-mmkv": "^4.1.2",
"react-native-nitro-modules": "^0.34.1",
"react-native-reanimated": "~3.17.4",
"react-native-safe-area-context": "5.4.0",
"react-native-screens": "~4.11.1",
Expand Down
32 changes: 15 additions & 17 deletions src/core/storage/storageService.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
// src/core/storage/storageService.ts
import * as FileSystem from 'expo-file-system';
import { createMMKV } from 'react-native-mmkv';
import { AppData } from '../store/appStore';

const dataFilePath = `${FileSystem.documentDirectory}muhsinData.json`;
const storage = createMMKV();
const DATA_KEY = 'muhsinData';

/**
* Loads the application data from the file system.
* Loads the application data from storage.
* @returns {Promise<AppData | null>} The parsed data or null if it doesn't exist.
*/
export const loadDataFromFile = async (): Promise<AppData | null> => {
export const loadData = async (): Promise<AppData | null> => {
try {
const fileInfo = await FileSystem.getInfoAsync(dataFilePath);
if (!fileInfo.exists) {
console.log('Data file does not exist. Starting fresh.');
return null;
const storedData = storage.getString(DATA_KEY);
if (storedData) {
return JSON.parse(storedData) as AppData;
}
const content = await FileSystem.readAsStringAsync(dataFilePath);
return JSON.parse(content) as AppData;

return null;
} catch (error) {
console.error('Failed to load data from file:', error);
// In case of parsing error, etc., we treat it as no data found.
console.error('Failed to load data from storage:', error);
return null;
}
};

/**
* Saves the application data to the file system.
* Saves the application data to MMKV storage.
* @param {AppData} data The data to save.
*/
export const saveDataToFile = async (data: AppData): Promise<void> => {
export const saveData = (data: AppData): void => {
try {
const content = JSON.stringify(data, null, 2); // Pretty print JSON
await FileSystem.writeAsStringAsync(dataFilePath, content);
storage.set(DATA_KEY, JSON.stringify(data));
} catch (error) {
console.error('Failed to save data to file:', error);
console.error('Failed to save data to MMKV:', error);
}
};
10 changes: 5 additions & 5 deletions src/core/store/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { create } from 'zustand';
import { Deed, DeedLog, DeedStatus } from '../data/models';
import { GENERIC_STATUSES, MOCK_DEEDS, MOCK_LOGS, SUGGESTED_DEEDS } from '../data/mock';
import { loadDataFromFile, saveDataToFile } from '../storage/storageService';
import { loadData, saveData } from '../storage/storageService';
import { formatISO } from 'date-fns';
import { Alert, Appearance, I18nManager } from 'react-native';
import { generateDemoLogs } from '../data/demoData';
Expand Down Expand Up @@ -82,7 +82,7 @@ const persistState = (state: AppState) => {
dataToSave.logs = loadedData.logs;
}

saveDataToFile(dataToSave);
saveData(dataToSave);
};

const useAppStore = create<AppState & AppActions>((set, get) => ({
Expand All @@ -96,7 +96,7 @@ const useAppStore = create<AppState & AppActions>((set, get) => ({

// --- ACTIONS ---
initialize: async () => {
const loadedData = await loadDataFromFile();
const loadedData = await loadData();
let settings = defaultSettings;
let initialDeeds = MOCK_DEEDS;
let initialLogs = MOCK_LOGS;
Expand Down Expand Up @@ -310,7 +310,7 @@ const useAppStore = create<AppState & AppActions>((set, get) => ({
Alert.alert(i18n.t('alerts.demoModeEnabledTitle'), i18n.t('alerts.demoModeEnabledMessage'));
} else {
// Disabling Demo Mode: Reload user's original data from disk
const originalData = await loadDataFromFile();
const originalData = await loadData();
set({
deeds: originalData?.deeds || MOCK_DEEDS,
logs: originalData?.logs || [],
Expand All @@ -334,7 +334,7 @@ const useAppStore = create<AppState & AppActions>((set, get) => ({
} else {
Appearance.setColorScheme(theme);
}
await saveDataToFile(initialState);
saveData(initialState);
await cancelAllReminders();
},

Expand Down