Skip to content

Commit ac02e81

Browse files
committed
fix(mobile): show cached note counts instantly on app restart
Fixes #39 Load note counts from AsyncStorage immediately on mount to prevent showing "0" for several seconds while API loads. The app now displays last known counts instantly, then updates with fresh data from the API in the background. Flow: - On mount: Load cached counts from AsyncStorage (instant display) - In background: Fetch fresh counts from API - On success: Update display and save to AsyncStorage for next launch
1 parent 7c0c1ca commit ac02e81

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

apps/mobile/v1/src/screens/Home/index.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,27 @@ export default function HomeScreen() {
132132
const isFirstMountRef = useRef(true);
133133
const hasDataRef = useRef(false);
134134

135+
// Load cached counts from AsyncStorage on mount
136+
useEffect(() => {
137+
const loadCachedCounts = async () => {
138+
try {
139+
const cached = await AsyncStorage.getItem('cached_note_counts');
140+
if (cached) {
141+
const cachedCounts = JSON.parse(cached);
142+
setCounts(cachedCounts);
143+
if (__DEV__) {
144+
console.log('[HomeScreen] Loaded cached counts:', cachedCounts);
145+
}
146+
}
147+
} catch (error) {
148+
if (__DEV__) {
149+
console.error('[HomeScreen] Failed to load cached counts:', error);
150+
}
151+
}
152+
};
153+
loadCachedCounts();
154+
}, []);
155+
135156
// Load folders data
136157
const loadFoldersData = useCallback(async (isRefresh = false, forceRefresh = false) => {
137158
try {
@@ -177,6 +198,18 @@ export default function HomeScreen() {
177198

178199
setCounts(newCounts);
179200

201+
// Cache counts in AsyncStorage for instant display on next app start
202+
try {
203+
await AsyncStorage.setItem('cached_note_counts', JSON.stringify(newCounts));
204+
if (__DEV__) {
205+
console.log('[HomeScreen] Cached note counts saved');
206+
}
207+
} catch (error) {
208+
if (__DEV__) {
209+
console.error('[HomeScreen] Failed to cache counts:', error);
210+
}
211+
}
212+
180213
// Mark that we have data now (prevents loading state on refocus)
181214
hasDataRef.current = true;
182215
} catch (error) {

0 commit comments

Comments
 (0)