mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-06-18 08:26:27 +10:00
0e074fd806
This adds region management: the user can manage a list of available regions and for each channel pick a region from that list to apply to messages. Region discovery from nearby repeaters will be done in a separate PR. This is a part of the work needed for #120.
54 lines
1.4 KiB
Dart
54 lines
1.4 KiB
Dart
import 'package:meshcore_open/storage/channel_region_store.dart';
|
|
import 'package:meshcore_open/storage/channel_store.dart';
|
|
|
|
import 'prefs_manager.dart';
|
|
|
|
typedef Region = String;
|
|
|
|
class RegionStore {
|
|
static const String key = 'regions';
|
|
String publicKeyHex = '';
|
|
set setPublicKeyHex(String value) =>
|
|
publicKeyHex = value.length >= 10 ? value.substring(0, 10) : '';
|
|
|
|
List<Region> loadRegions() {
|
|
final prefs = PrefsManager.instance;
|
|
List<Region>? region = prefs.getStringList(key);
|
|
return region ?? [];
|
|
}
|
|
|
|
void saveRegions(List<Region> regions) {
|
|
final prefs = PrefsManager.instance;
|
|
|
|
var distinctRegions = [
|
|
...{...regions},
|
|
];
|
|
|
|
distinctRegions.sort();
|
|
prefs.setStringList(key, distinctRegions);
|
|
}
|
|
|
|
void addRegion(Region region) {
|
|
final regions = loadRegions();
|
|
regions.add(region);
|
|
saveRegions(regions);
|
|
}
|
|
|
|
Future<void> removeRegion(Region region) async {
|
|
final regions = loadRegions();
|
|
final channelStore = ChannelStore();
|
|
final channelRegionStore = ChannelRegionStore();
|
|
channelStore.setPublicKeyHex = publicKeyHex;
|
|
channelRegionStore.setPublicKeyHex = publicKeyHex;
|
|
|
|
for (var channel in await channelStore.loadChannels()) {
|
|
var channelRegion = await channelRegionStore.loadRegion(channel.index);
|
|
if (channelRegion == region) {
|
|
channelRegionStore.saveRegion(channel.index, '');
|
|
}
|
|
}
|
|
regions.remove(region);
|
|
saveRegions(regions);
|
|
}
|
|
}
|