Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/pages/chat_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ class _ChatInfoPageState extends State<ChatInfoPage> {
);
}

/// Pick a day and hand it back to the chat page, which jumps to the first
/// message of that day (or the first one after it).
Future<void> _jumpToDate() async {
final now = DateTime.now();
final picked = await showDatePicker(
context: context,
initialDate: now,
firstDate: DateTime(now.year - 10),
lastDate: now,
);
if (picked != null && mounted) {
Navigator.pop(context, picked);
}
}

/// Search this conversation. A picked hit is handed back to the chat page,
/// which jumps to it.
Future<void> _openSearch() async {
Expand Down Expand Up @@ -133,6 +148,12 @@ class _ChatInfoPageState extends State<ChatInfoPage> {
label: 'searchInChat'.tr(),
onPressed: _openSearch,
),
// Jump to a day
SettingsTile(
icon: Icons.event,
label: 'jumpToDate'.tr(),
onPressed: _jumpToDate,
),
// Chat theme
SettingsTile(
icon: Icons.palette,
Expand Down
44 changes: 13 additions & 31 deletions lib/pages/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,47 +377,34 @@ class _ChatPageState extends State<ChatPage> {
}

Future<void> _openChatInfo() async {
// The info page hands back a timestamp when a search hit was picked there.
final jumpTo = await Navigator.of(context)
// The info page hands back a timestamp when a search hit or a date was
// picked there.
final target = await Navigator.of(context)
.push<DateTime>(stepRoute(ChatInfoPage(widget.user.id)));
// The theme (and possibly the user) may have changed while it was open.
await _loadChatTheme();
await _loadMessages();
if (jumpTo != null && mounted) {
await _jumpToMessage(jumpTo);
if (target != null && mounted) {
await _jumpTo(target);
}
}

/// Jump to the first message of a picked day (or the first one after it).
Future<void> _jumpToDate() async {
/// Jumps to [target], or to the first message after it: a search hit is an
/// exact instant, a picked day is midnight.
Future<void> _jumpTo(DateTime target) async {
if (_messages.isEmpty) {
return;
}
final oldest = _messages.first.sentAt.toLocal();
final firstDay = DateTime(oldest.year, oldest.month, oldest.day);
final now = DateTime.now();
final picked = await showDatePicker(
context: context,
initialDate: now,
firstDate: firstDay.isBefore(DateTime(2000)) ? DateTime(2000) : firstDay,
lastDate: now,
);
if (picked == null || !mounted) {
return;
}
DateTime? target;
for (final message in _messages) {
if (!message.sentAt.isBefore(picked)) {
target = message.sentAt;
break;
}
var index = _messages.indexWhere((message) => message.sentAt == target);
if (index < 0) {
index = _messages.indexWhere((message) => message.sentAt.isAfter(target));
}
if (target == null) {
if (index < 0) {
await Fluttertoast.showToast(
msg: 'noMessagesOnDate'.tr(), backgroundColor: Colors.orange);
return;
}
await _jumpToMessage(target);
await _jumpToMessage(_messages[index].sentAt);
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1368,11 +1355,6 @@ class _ChatPageState extends State<ChatPage> {
),
// Action buttons
actions: [
IconButton(
icon: const Icon(Icons.event),
tooltip: 'jumpToDate'.tr(),
onPressed: _jumpToDate,
),
IconButton(
icon: const Icon(Icons.info),
tooltip: 'infoButton'.tr(),
Expand Down
Loading