- Extract reusable widgets (chat composer/send pipeline, message bubble frame, reaction chips, path map scaffold, radio settings dialog, translation settings section, map POI clusters, nav tiles, etc.) and slim down the screens that used them - Map: hide/remove markers with restore, confirm before setting node location, POI clustering, semantics labels - Map cache: cancellable downloads and a bulk tile limit - Repeater: unsaved-changes guard, CLI search/copy/share transcript, framed-command debug preview, disconnected banner - Confirm before clearing chats and debug logs - Keep cached unread totals in sync on per-contact/channel updates - Move remaining hardcoded strings into l10n and translate the 98 new keys into all 17 non-English locales
124 lines
3.5 KiB
Dart
124 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../connector/meshcore_protocol.dart';
|
|
import '../l10n/app_localizations.dart';
|
|
import '../models/contact.dart';
|
|
import '../theme/mesh_theme.dart';
|
|
|
|
/// Shared node-type visuals and last-seen formatting for contact lists
|
|
/// (contacts, discovery, nearby-node scan).
|
|
|
|
/// Accent used for sensor nodes.
|
|
const Color meshSensorTeal = Color(0xFF4ACCC4);
|
|
|
|
/// Node-type accent colour per design language.
|
|
Color contactTypeColor(int type) {
|
|
switch (type) {
|
|
case advTypeRepeater:
|
|
return MeshPalette.warn;
|
|
case advTypeRoom:
|
|
return MeshPalette.magenta;
|
|
case advTypeSensor:
|
|
return meshSensorTeal;
|
|
default:
|
|
return MeshPalette.blue;
|
|
}
|
|
}
|
|
|
|
/// Node-type avatar icon. Returns null for chat nodes so [AvatarCircle]
|
|
/// shows initials.
|
|
IconData? contactTypeIcon(int type) {
|
|
switch (type) {
|
|
case advTypeRepeater:
|
|
return Icons.cell_tower;
|
|
case advTypeRoom:
|
|
return Icons.meeting_room;
|
|
case advTypeSensor:
|
|
return Icons.sensors;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// The most recent activity for [contact]: for chat nodes this includes the
|
|
/// last message, for other node types only the last advert.
|
|
DateTime resolveContactLastSeen(Contact contact) {
|
|
if (contact.type != advTypeChat) return contact.lastSeen;
|
|
return contact.lastMessageAt.isAfter(contact.lastSeen)
|
|
? contact.lastMessageAt
|
|
: contact.lastSeen;
|
|
}
|
|
|
|
/// Short relative "last seen" label ("now", "5 mins ago", "2 days ago").
|
|
String formatContactLastSeen(AppLocalizations l10n, DateTime lastSeen) {
|
|
final diff = DateTime.now().difference(lastSeen);
|
|
|
|
if (diff.isNegative || diff.inMinutes < 5) {
|
|
return l10n.contacts_lastSeenNow;
|
|
}
|
|
if (diff.inMinutes < 60) {
|
|
return l10n.contacts_lastSeenMinsAgo(diff.inMinutes);
|
|
}
|
|
if (diff.inHours < 24) {
|
|
final hours = diff.inHours;
|
|
return hours == 1
|
|
? l10n.contacts_lastSeenHourAgo
|
|
: l10n.contacts_lastSeenHoursAgo(hours);
|
|
}
|
|
final days = diff.inDays;
|
|
return days == 1
|
|
? l10n.contacts_lastSeenDayAgo
|
|
: l10n.contacts_lastSeenDaysAgo(days);
|
|
}
|
|
|
|
/// Memoizes a derived list (filtered/sorted contacts) so it is only
|
|
/// recomputed when the input elements or the filter key change. Contacts are
|
|
/// immutable, so element identity is a sufficient change check even though
|
|
/// the connector hands out a fresh list copy on every read.
|
|
class DerivedListCache<T> {
|
|
List<T>? _inputs;
|
|
List<Object?>? _key;
|
|
List<T>? _result;
|
|
|
|
List<T> get(List<T> inputs, List<Object?> key, List<T> Function() compute) {
|
|
final cached = _result;
|
|
if (cached != null &&
|
|
_sameElements(_inputs!, inputs) &&
|
|
_sameKey(_key!, key)) {
|
|
return cached;
|
|
}
|
|
final result = compute();
|
|
_inputs = List<T>.of(inputs);
|
|
_key = key;
|
|
_result = result;
|
|
return result;
|
|
}
|
|
|
|
void invalidate() => _result = null;
|
|
|
|
static bool _sameElements<E>(List<E> a, List<E> b) {
|
|
if (a.length != b.length) return false;
|
|
for (var i = 0; i < a.length; i++) {
|
|
if (!identical(a[i], b[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool _sameKey(List<Object?> a, List<Object?> b) {
|
|
if (a.length != b.length) return false;
|
|
for (var i = 0; i < a.length; i++) {
|
|
final x = a[i];
|
|
final y = b[i];
|
|
if (x is List && y is List) {
|
|
if (x.length != y.length) return false;
|
|
for (var j = 0; j < x.length; j++) {
|
|
if (x[j] != y[j]) return false;
|
|
}
|
|
} else if (x != y) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|