cbc06b2f13
One file at a time or all files stacked is saved in the app's preferences and restored when the app starts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.3 KiB
Dart
46 lines
1.3 KiB
Dart
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<bool> {
|
|
DiffLayoutPreference() : super(false);
|
|
|
|
static const String storageKey = 'pipelines.diff.all-files.v1';
|
|
|
|
Future<void>? _restoring;
|
|
bool _chosen = false;
|
|
|
|
/// Reads the saved layout once. A choice made before the read finishes
|
|
/// wins over the saved one.
|
|
Future<void> 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<void> _save(bool showAll) async {
|
|
try {
|
|
await (await SharedPreferences.getInstance()).setBool(
|
|
storageKey,
|
|
showAll,
|
|
);
|
|
} catch (_) {
|
|
// The layout still applies for this session.
|
|
}
|
|
}
|
|
}
|