mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-06-18 00:16:26 +10:00
Add region management
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.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import '../utils/app_logger.dart';
|
||||
import 'prefs_manager.dart';
|
||||
|
||||
class ChannelRegionStore {
|
||||
static const String _keyPrefix = 'channel_region_';
|
||||
|
||||
String publicKeyHex = '';
|
||||
set setPublicKeyHex(String value) =>
|
||||
publicKeyHex = value.length >= 10 ? value.substring(0, 10) : '';
|
||||
|
||||
String get keyFor => '$_keyPrefix$publicKeyHex';
|
||||
|
||||
Future<String> loadRegion(int channelIndex) async {
|
||||
if (publicKeyHex.isEmpty) {
|
||||
appLogger.warn(
|
||||
'Public key hex is not set. Cannot load channel settings.',
|
||||
);
|
||||
return '';
|
||||
}
|
||||
final prefs = PrefsManager.instance;
|
||||
final key = '$keyFor$channelIndex';
|
||||
String? region = prefs.getString(key);
|
||||
return region ?? '';
|
||||
}
|
||||
|
||||
Future<String> saveRegion(int channelIndex, String region) async {
|
||||
if (publicKeyHex.isEmpty) {
|
||||
appLogger.warn(
|
||||
'Public key hex is not set. Cannot save channel settings.',
|
||||
);
|
||||
return '';
|
||||
}
|
||||
|
||||
final prefs = PrefsManager.instance;
|
||||
final key = '$keyFor$channelIndex';
|
||||
await prefs.setString(key, region);
|
||||
return region;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user