Files
pipelines/app/lib/features/chat/ui/sessions/chat_seen_focus.dart
T
alan 746e23788d Mark chats seen after user interaction
Prevent passive open, restored, or unfocused chats from clearing notifications. Track qualifying focused interactions and cover passive and active seen-watermark behavior with tests and notes.
2026-11-12 02:25:09 +00:00

47 lines
1.4 KiB
Dart

import 'package:flutter/widgets.dart';
/// Whether the app window is focused and visible: the only state in which a
/// chat can be read. A backgrounded or unfocused window never marks a chat
/// seen, so a reply that arrives there stays New until the user interacts.
class ChatSeenFocus extends ChangeNotifier {
ChatSeenFocus({bool focused = true}) {
_focused = focused;
}
/// The app's own focus, driven by the framework's lifecycle state. A null
/// lifecycle state (before the first report) counts as focused, so a window
/// that starts visible is not stuck unread.
static final ChatSeenFocus app = ChatSeenFocus._observingApp();
factory ChatSeenFocus._observingApp() {
final focus = ChatSeenFocus(
focused: _isFocusedState(WidgetsBinding.instance.lifecycleState),
);
WidgetsBinding.instance.addObserver(_LifecycleFocus(focus));
return focus;
}
late bool _focused;
bool get focused => _focused;
set focused(bool value) {
if (value == _focused) return;
_focused = value;
notifyListeners();
}
static bool _isFocusedState(AppLifecycleState? state) =>
state == null || state == AppLifecycleState.resumed;
}
class _LifecycleFocus with WidgetsBindingObserver {
_LifecycleFocus(this._focus);
final ChatSeenFocus _focus;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_focus.focused = ChatSeenFocus._isFocusedState(state);
}
}