import 'package:shared_preferences/shared_preferences.dart'; import 'app_font.dart'; /// The device's font choice. It lives in this device's (or browser's) own /// preferences and is never sent to a server, so a phone and a browser signed /// in to the same server keep their own choice. const String appFontKey = 'pipelines.next.font.v1'; /// The persistence seam for the font choice. abstract interface class AppFontStore { Future load(); /// Whether the choice is actually stored. Future save(AppFont font); } /// `shared_preferences` storage. An unavailable or unreadable store means the /// default; a failed save returns false rather than throwing. class SharedPreferencesAppFontStore implements AppFontStore { const SharedPreferencesAppFontStore(); @override Future load() async { try { final preferences = await SharedPreferences.getInstance(); return AppFont.fromId(preferences.getString(appFontKey)); } catch (_) { return AppFont.system; } } @override Future save(AppFont font) async { try { final preferences = await SharedPreferences.getInstance(); return await preferences.setString(appFontKey, font.id); } catch (_) { return false; } } } /// An in-memory store for tests and fixtures. class MemoryAppFontStore implements AppFontStore { MemoryAppFontStore([this.font = AppFont.system]); AppFont font; bool failSaves = false; @override Future load() async => font; @override Future save(AppFont font) async { if (failSaves) return false; this.font = font; return true; } }