Files
pipelines/app/lib/features/chat/stack/mobile_chat_navigation.dart
T
alan 130c1e6708 Chat stack: the default, also on a wide phone or tablet; a close button where the pile was
Chat stack is now the default phone chat navigation (a stored choice is
kept), and on Android or iOS it stays on a wide layout (an unfolded
foldable) above the active pane, where floating is ignored too. When
expanded, the pile turns into a close button at its own spot and the
column rises above it; while a chat is dragged that button is the remove
target, replacing the separate x at the bottom. Chat widget tests start
on Quick switch (they run as Android); the stack's tests choose it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:47:57 +00:00

115 lines
3.0 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
const mobileChatNavigationKey = 'pipelines.mobile-chat-navigation.v1';
/// How a phone-width layout (and any layout on a phone or tablet, an unfolded
/// foldable too, for the Chat stack) moves between the open chats.
enum MobileChatNavigation {
quickSwitch(
'Quick switch',
'The next open chat peeks below the current one; tap it to swap.',
),
chatStack(
'Chat stack',
'A stack of the open chats floats above the composer; tap it to pick one.',
);
const MobileChatNavigation(this.label, this.description);
final String label;
final String description;
}
abstract interface class MobileChatNavigationStore {
Future<String?> load();
Future<bool> save(String value);
}
class SharedMobileChatNavigationStore implements MobileChatNavigationStore {
const SharedMobileChatNavigationStore();
@override
Future<String?> load() async {
try {
return (await SharedPreferences.getInstance()).getString(
mobileChatNavigationKey,
);
} catch (_) {
return null;
}
}
@override
Future<bool> save(String value) async {
try {
return await (await SharedPreferences.getInstance()).setString(
mobileChatNavigationKey,
value,
);
} catch (_) {
return false;
}
}
}
/// This device's choice of phone chat navigation, applied live.
class MobileChatNavigationController extends ChangeNotifier {
MobileChatNavigationController({MobileChatNavigationStore? store})
: _store = store ?? const SharedMobileChatNavigationStore();
static final instance = MobileChatNavigationController();
final MobileChatNavigationStore _store;
/// Without a stored choice, the Chat stack.
static const fallback = MobileChatNavigation.chatStack;
MobileChatNavigation _value = fallback;
int _revision = 0;
bool _disposed = false;
bool saveFailed = false;
MobileChatNavigation get value => _value;
bool get stack => _value == MobileChatNavigation.chatStack;
Future<void> restore() async {
final revision = _revision;
final saved = await _store.load();
if (_disposed || revision != _revision) return;
for (final mode in MobileChatNavigation.values) {
if (mode.name == saved && mode != _value) {
_value = mode;
notifyListeners();
}
}
}
Future<void> setValue(MobileChatNavigation mode) async {
if (_disposed) return;
final revision = ++_revision;
_value = mode;
saveFailed = false;
notifyListeners();
final saved = await _store.save(mode.name);
if (_disposed || revision != _revision) return;
saveFailed = !saved;
notifyListeners();
}
/// Sets [mode] without saving it (a test's starting point).
@visibleForTesting
void debugUse(MobileChatNavigation mode) {
_revision++;
saveFailed = false;
if (mode == _value) return;
_value = mode;
notifyListeners();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}