Key per-channel storage by PSK-derived channel ID

Channel messages, settings, region, order and images were persisted by
radio slot index, so a slot reused for a different channel inherited the
old channel's history and settings (#359).

- Channel.storageId: first 8 bytes of SHA-256("meshcore-channel:" + psk).
- All per-channel stores key by channel ID; in-memory state stays by slot.
- The connector keeps a slot -> channel ID map, diffs it against the saved
  map on every channel sync and reloads slots whose channel changed.
- Queued channel frames wait until this session's map is confirmed.
- One-shot per-radio migration from slot keys, using the cached channel
  list the legacy data was written under; rerun-safe if interrupted.
- Deleting a channel clears its data; a channel removed elsewhere keeps its
  history and gets it back when re-added. A PSK edit keeps its place in
  the custom order.
- Slots sharing a PSK share history and settings.
- Image entries record their channel ID; a slot change resets that slot's
  in-progress reassembly.
This commit is contained in:
zjs81 committed 2026-10-11 17:52:59 -07:00
1 parent d30744aa31
commit 679116ed03
24 files changed
+1794 -340

No files matched your search

+33
View File
@@ -8,6 +8,7 @@ import 'prefs_manager.dart';
class ChannelStore {
static const String _keyPrefix = 'channels';
static const String _slotIdsKeyPrefix = 'channel_slot_ids_';
String publicKeyHex = '';
set setPublicKeyHex(String value) =>
publicKeyHex = value.length >= 10 ? value.substring(0, 10) : '';
@@ -70,6 +71,38 @@ class ChannelStore {
await prefs.setString(keyFor, jsonEncode(jsonList));
}
String get _slotIdsKey => '$_slotIdsKeyPrefix$publicKeyHex';
/// The slot -> [Channel.storageId] map confirmed by the last channel sync,
/// used to spot slots another app changed while we were away.
Future<Map<int, String>> loadSlotChannelIds() async {
if (publicKeyHex.isEmpty) return {};
final jsonString = PrefsManager.instance.getString(_slotIdsKey);
if (jsonString == null || jsonString.isEmpty) return {};
try {
final decoded = jsonDecode(jsonString);
if (decoded is! Map) return {};
final ids = <int, String>{};
for (final entry in decoded.entries) {
final slot = int.tryParse('${entry.key}');
final id = entry.value;
if (slot != null && id is String && id.isNotEmpty) ids[slot] = id;
}
return ids;
} catch (e) {
appLogger.warn('Stored channel slot map is unreadable: $e');
return {};
}
}
Future<void> saveSlotChannelIds(Map<int, String> ids) async {
if (publicKeyHex.isEmpty) return;
await PrefsManager.instance.setString(
_slotIdsKey,
jsonEncode({for (final e in ids.entries) '${e.key}': e.value}),
);
}
Map<String, dynamic> _toJson(Channel channel) {
return {
'index': channel.index,