import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Whether diff pages show every file stacked (GitHub's Files changed) rather /// than one file at a time, kept across reloads in the app's preferences. class DiffLayoutPreference extends ValueNotifier { DiffLayoutPreference() : super(false); static const String storageKey = 'pipelines.diff.all-files.v1'; Future? _restoring; bool _chosen = false; /// Reads the saved layout once. A choice made before the read finishes /// wins over the saved one. Future restore() => _restoring ??= () async { try { final saved = (await SharedPreferences.getInstance()).getBool(storageKey); if (saved != null && !_chosen) super.value = saved; } catch (_) { // Unavailable preferences keep the default layout. } }(); @override set value(bool showAll) { _chosen = true; if (showAll == super.value) return; super.value = showAll; unawaited(_save(showAll)); } Future _save(bool showAll) async { try { await (await SharedPreferences.getInstance()).setBool( storageKey, showAll, ); } catch (_) { // The layout still applies for this session. } } }