import 'package:shared_preferences/shared_preferences.dart'; /// The device-level key for "the first-run walkthrough was finished or skipped". /// It is per device, not per server, so a new server never repeats it. const String walkthroughSeenKey = 'pipelines.next.firstRunWalkthrough.v1'; /// The key listing the server scopes that were already offered the walkthrough /// because they had no pipelines, runs or chats yet. const String walkthroughOfferedKey = 'pipelines.next.walkthroughOffered.v1'; /// The persistence seam for the walkthrough. A store that cannot be read or /// written reports "seen" and "offered", so an unavailable store never nags. abstract interface class WalkthroughStore { Future isSeen(); Future markSeen(); Future wasOffered(String serverScope); Future markOffered(String serverScope); } Future _openPreferences() async { try { return await SharedPreferences.getInstance(); } catch (_) { return null; } } /// `shared_preferences`-backed storage under the `pipelines.next` keys. /// /// [open] is the seam for an unavailable platform store (tests pass one that /// yields null); production opens the real preferences. class SharedPreferencesWalkthroughStore implements WalkthroughStore { const SharedPreferencesWalkthroughStore({this.open = _openPreferences}); final Future Function() open; Future _preferences() => open(); @override Future isSeen() async { final preferences = await _preferences(); if (preferences == null) return true; try { return preferences.getBool(walkthroughSeenKey) ?? false; } catch (_) { return true; } } @override Future markSeen() async { final preferences = await _preferences(); if (preferences == null) return; try { await preferences.setBool(walkthroughSeenKey, true); } catch (_) { // Nothing to report: the walkthrough is still dismissed for this run. } } @override Future wasOffered(String serverScope) async { final preferences = await _preferences(); if (preferences == null) return true; try { final offered = preferences.getStringList(walkthroughOfferedKey); return offered?.contains(serverScope) ?? false; } catch (_) { return true; } } @override Future markOffered(String serverScope) async { final preferences = await _preferences(); if (preferences == null) return; try { final offered = { ...?preferences.getStringList(walkthroughOfferedKey), serverScope, }; await preferences.setStringList(walkthroughOfferedKey, offered.toList()); } catch (_) { // A failed write only means the offer may show again later. } } } /// An in-memory store for tests and previews. It never touches the platform /// store, so a fixture cannot mark a real device as seen. class MemoryWalkthroughStore implements WalkthroughStore { MemoryWalkthroughStore({this.seen = false, Set? offered}) : _offered = {...?offered}; /// Whether the walkthrough is marked seen. bool seen; final Set _offered; /// The server scopes already offered the walkthrough. Set get offered => Set.unmodifiable(_offered); @override Future isSeen() async => seen; @override Future markSeen() async => seen = true; @override Future wasOffered(String serverScope) async => _offered.contains(serverScope); @override Future markOffered(String serverScope) async => _offered.add(serverScope); }