Remember the diff layout across reloads

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>
This commit is contained in:
2026-11-22 19:23:21 +00:00
parent 9bf080597a
commit cbc06b2f13
4 changed files with 86 additions and 2 deletions
@@ -0,0 +1,45 @@
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.
}
}
}
@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
@@ -7,6 +9,7 @@ import '../../../ui/navigation/chevron_pager.dart';
import '../domain/file_change.dart';
import 'diff_highlight.dart';
import 'diff_keyboard.dart';
import 'diff_layout_preference.dart';
import 'diff_search_bar.dart';
import 'diff_search_controller.dart';
import 'file_diff_viewer.dart';
@@ -14,8 +17,8 @@ import 'stacked_diff_view.dart';
import '../windows/diff_windows.dart';
/// Whether diff pages show every file stacked (GitHub's Files changed) rather
/// than one file at a time. Remembered for the rest of the session.
final ValueNotifier<bool> diffShowsAllFiles = ValueNotifier(false);
/// than one file at a time; saved across reloads.
final DiffLayoutPreference diffShowsAllFiles = DiffLayoutPreference();
/// How far one up/down (`k`/`j`) press scrolls.
const double diffScrollStep = 64;
@@ -84,6 +87,7 @@ class _FileDiffPageState extends State<FileDiffPage> {
void initState() {
super.initState();
diffShowsAllFiles.addListener(_rebuild);
unawaited(diffShowsAllFiles.restore());
_search.addListener(_rebuild);
}
@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart';
import '../../../shell/list_detail_policy.dart';
@@ -34,6 +36,8 @@ class DiffWindows {
if (_started) return;
_started = true;
manager.kinds.register(kind);
// The saved diff layout is ready before the first diff opens.
unawaited(diffShowsAllFiles.restore());
}
void dispose() {
@@ -0,0 +1,31 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/changes/widgets/diff_layout_preference.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('the chosen layout is saved and comes back after a reload', () async {
SharedPreferences.setMockInitialValues({});
final first = DiffLayoutPreference();
await first.restore();
expect(first.value, isFalse);
first.value = true;
await pumpEventQueue();
// A new app session reads it back.
final next = DiffLayoutPreference();
await next.restore();
expect(next.value, isTrue);
});
test('a choice made while the saved one loads wins', () async {
SharedPreferences.setMockInitialValues({
DiffLayoutPreference.storageKey: true,
});
final layout = DiffLayoutPreference();
final restoring = layout.restore();
layout.value = false;
await restoring;
expect(layout.value, isFalse);
});
}