mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
Refactor SAR marker handling and add template management
- Updated CompassSarList and DetailedCompassDialog to use marker.displayName instead of marker.type.displayName. - Enhanced DrawingLayer and DrawingMarkersLayer to support simple mode for drawing visibility and interaction. - Added toggle switches in DrawingToolbar for showing/hiding received drawings and SAR markers. - Modified MapMarkers to utilize custom emojis and display names for markers. - Introduced RecipientSelectorSheet for selecting message recipients with search functionality. - Refactored SarUpdateSheet to use SAR templates instead of marker types, allowing for emoji and name customization. - Created SarTemplateEditDialog for adding and editing SAR templates with color selection and preview.
This commit is contained in:
@@ -184,7 +184,7 @@ class MapMarkerService {
|
||||
),
|
||||
padding: const EdgeInsets.all(6),
|
||||
child: Text(
|
||||
marker.type.emoji,
|
||||
marker.emoji, // Use custom emoji if available
|
||||
style: const TextStyle(fontSize: 18),
|
||||
),
|
||||
),
|
||||
@@ -198,7 +198,7 @@ class MapMarkerService {
|
||||
borderRadius: BorderRadius.circular(3),
|
||||
),
|
||||
child: Text(
|
||||
marker.type.displayName,
|
||||
marker.displayName, // Uses notes if available, otherwise type.displayName
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 9,
|
||||
|
||||
@@ -401,8 +401,8 @@ class MeshCoreBleService {
|
||||
required double latitude,
|
||||
required double longitude,
|
||||
}) async {
|
||||
// Note: This command does not return an ACK, so we use writeData (fire-and-forget)
|
||||
await _commandSender.writeData(FrameBuilder.buildSetAdvertLatLon(
|
||||
// This command returns OK (0x00) response, so wait for acknowledgment
|
||||
await _commandSender.writeDataAndWaitForAck(FrameBuilder.buildSetAdvertLatLon(
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
));
|
||||
|
||||
71
lib/services/message_destination_preferences.dart
Normal file
71
lib/services/message_destination_preferences.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Service for managing message destination preferences
|
||||
/// Stores the last selected recipient (channel, contact, or room) for sending messages
|
||||
class MessageDestinationPreferences {
|
||||
static const String _destinationTypeKey = 'message_destination_type';
|
||||
static const String _recipientPublicKeyKey = 'message_recipient_public_key';
|
||||
|
||||
/// Destination types
|
||||
static const String destinationTypeChannel = 'channel';
|
||||
static const String destinationTypeContact = 'contact';
|
||||
static const String destinationTypeRoom = 'room';
|
||||
|
||||
/// Get the saved destination configuration
|
||||
/// Returns a map with 'type' and optional 'publicKey'
|
||||
/// Returns null if no preference is saved (defaults to public channel)
|
||||
static Future<Map<String, String>?> getDestination() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final type = prefs.getString(_destinationTypeKey);
|
||||
|
||||
if (type == null) {
|
||||
return null; // Use default (public channel)
|
||||
}
|
||||
|
||||
final publicKey = prefs.getString(_recipientPublicKeyKey);
|
||||
|
||||
return {
|
||||
'type': type,
|
||||
if (publicKey != null) 'publicKey': publicKey,
|
||||
};
|
||||
}
|
||||
|
||||
/// Save the selected destination
|
||||
/// [type] - one of: destinationTypeChannel, destinationTypeContact, destinationTypeRoom
|
||||
/// [recipientPublicKey] - hex string of recipient's public key (required for contact/room)
|
||||
static Future<void> setDestination(
|
||||
String type, {
|
||||
String? recipientPublicKey,
|
||||
}) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
await prefs.setString(_destinationTypeKey, type);
|
||||
|
||||
if (recipientPublicKey != null) {
|
||||
await prefs.setString(_recipientPublicKeyKey, recipientPublicKey);
|
||||
} else {
|
||||
await prefs.remove(_recipientPublicKeyKey);
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear the saved destination (resets to default public channel)
|
||||
static Future<void> clearDestination() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_destinationTypeKey);
|
||||
await prefs.remove(_recipientPublicKeyKey);
|
||||
}
|
||||
|
||||
/// Get display name for destination type
|
||||
static String getDestinationTypeName(String type) {
|
||||
switch (type) {
|
||||
case destinationTypeChannel:
|
||||
return 'Channel';
|
||||
case destinationTypeContact:
|
||||
return 'Contact';
|
||||
case destinationTypeRoom:
|
||||
return 'Room';
|
||||
default:
|
||||
return 'Unknown';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ class NotificationService {
|
||||
|
||||
// Notification IDs
|
||||
static const int _sarNotificationId = 1000;
|
||||
static const int _messageNotificationId = 2000;
|
||||
|
||||
// Notification channels
|
||||
static const String _urgentChannelId = 'sar_urgent';
|
||||
@@ -27,6 +28,11 @@ class NotificationService {
|
||||
static const String _urgentChannelDescription =
|
||||
'Critical alerts for SAR markers (found persons, fires, staging areas)';
|
||||
|
||||
static const String _messagesChannelId = 'messages';
|
||||
static const String _messagesChannelName = 'Messages';
|
||||
static const String _messagesChannelDescription =
|
||||
'Notifications for incoming messages from contacts and channels';
|
||||
|
||||
/// Initialize notification service
|
||||
Future<void> initialize() async {
|
||||
if (_isInitialized) return;
|
||||
@@ -125,8 +131,20 @@ class NotificationService {
|
||||
sound: RawResourceAndroidNotificationSound('notification'),
|
||||
);
|
||||
|
||||
// Messages channel with high priority
|
||||
const messagesChannel = AndroidNotificationChannel(
|
||||
_messagesChannelId,
|
||||
_messagesChannelName,
|
||||
description: _messagesChannelDescription,
|
||||
importance: Importance.high,
|
||||
playSound: true,
|
||||
enableVibration: true,
|
||||
showBadge: true,
|
||||
);
|
||||
|
||||
await androidPlugin.createNotificationChannel(urgentChannel);
|
||||
debugPrint('✅ [NotificationService] Created urgent notification channel');
|
||||
await androidPlugin.createNotificationChannel(messagesChannel);
|
||||
debugPrint('✅ [NotificationService] Created notification channels');
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [NotificationService] Error creating channels: $e');
|
||||
}
|
||||
@@ -303,6 +321,93 @@ class NotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Show notification for regular message (contact or channel)
|
||||
Future<void> showMessageNotification({
|
||||
required String senderName,
|
||||
required String messageText,
|
||||
required bool isChannelMessage,
|
||||
String? channelName,
|
||||
AppLocalizations? localizations,
|
||||
}) async {
|
||||
if (!_isInitialized) {
|
||||
debugPrint('⚠️ [NotificationService] Not initialized, skipping notification');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_permissionGranted) {
|
||||
debugPrint('⚠️ [NotificationService] Permission not granted, skipping notification');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Generate unique notification ID based on timestamp
|
||||
final notificationId = _messageNotificationId + (DateTime.now().millisecondsSinceEpoch % 1000);
|
||||
|
||||
// Build notification title and body
|
||||
final title = isChannelMessage
|
||||
? (localizations != null
|
||||
? '${localizations.channel}: ${channelName ?? "Public"}'
|
||||
: 'Channel: ${channelName ?? "Public"}')
|
||||
: (localizations != null
|
||||
? '${localizations.newMessage} ${localizations.from} $senderName'
|
||||
: 'New message from $senderName');
|
||||
|
||||
final body = messageText.length > 200
|
||||
? '${messageText.substring(0, 200)}...'
|
||||
: messageText;
|
||||
|
||||
// Android notification details
|
||||
final androidDetails = AndroidNotificationDetails(
|
||||
_messagesChannelId,
|
||||
_messagesChannelName,
|
||||
channelDescription: _messagesChannelDescription,
|
||||
importance: Importance.high,
|
||||
priority: Priority.high,
|
||||
ticker: title,
|
||||
playSound: true,
|
||||
enableVibration: true,
|
||||
showWhen: true,
|
||||
when: DateTime.now().millisecondsSinceEpoch,
|
||||
styleInformation: BigTextStyleInformation(
|
||||
body,
|
||||
contentTitle: title,
|
||||
summaryText: senderName,
|
||||
),
|
||||
);
|
||||
|
||||
// iOS notification details
|
||||
final darwinDetails = DarwinNotificationDetails(
|
||||
presentAlert: true,
|
||||
presentBadge: true,
|
||||
presentSound: true,
|
||||
sound: 'default',
|
||||
threadIdentifier: isChannelMessage ? 'channel_messages' : 'direct_messages',
|
||||
subtitle: senderName,
|
||||
);
|
||||
|
||||
// Combined notification details
|
||||
final notificationDetails = NotificationDetails(
|
||||
android: androidDetails,
|
||||
iOS: darwinDetails,
|
||||
);
|
||||
|
||||
// Show notification
|
||||
await _notificationsPlugin.show(
|
||||
notificationId,
|
||||
title,
|
||||
body,
|
||||
notificationDetails,
|
||||
payload: 'message:${isChannelMessage ? "channel" : "contact"}',
|
||||
);
|
||||
|
||||
debugPrint('✅ [NotificationService] Showed message notification');
|
||||
debugPrint(' Sender: $senderName');
|
||||
debugPrint(' Type: ${isChannelMessage ? "Channel" : "Direct"}');
|
||||
} catch (e) {
|
||||
debugPrint('❌ [NotificationService] Error showing message notification: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel all notifications
|
||||
Future<void> cancelAll() async {
|
||||
try {
|
||||
|
||||
244
lib/services/sar_template_service.dart
Normal file
244
lib/services/sar_template_service.dart
Normal file
@@ -0,0 +1,244 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/sar_template.dart';
|
||||
import '../utils/sar_message_parser.dart';
|
||||
|
||||
/// SAR Template Service - Manages SAR templates with persistence
|
||||
class SarTemplateService extends ChangeNotifier {
|
||||
static final SarTemplateService _instance = SarTemplateService._internal();
|
||||
factory SarTemplateService() => _instance;
|
||||
SarTemplateService._internal();
|
||||
|
||||
static const String _storageKey = 'sar_templates';
|
||||
List<SarTemplate> _templates = [];
|
||||
bool _initialized = false;
|
||||
|
||||
/// Get all templates
|
||||
List<SarTemplate> get templates => List.unmodifiable(_templates);
|
||||
|
||||
/// Get default templates
|
||||
List<SarTemplate> get defaultTemplates =>
|
||||
_templates.where((t) => t.isDefault).toList();
|
||||
|
||||
/// Get custom templates
|
||||
List<SarTemplate> get customTemplates =>
|
||||
_templates.where((t) => !t.isDefault).toList();
|
||||
|
||||
/// Check if initialized
|
||||
bool get isInitialized => _initialized;
|
||||
|
||||
/// Initialize service and load templates
|
||||
Future<void> initialize() async {
|
||||
if (_initialized) return;
|
||||
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonString = prefs.getString(_storageKey);
|
||||
|
||||
if (jsonString != null && jsonString.isNotEmpty) {
|
||||
// Load saved templates
|
||||
final List<dynamic> jsonList = json.decode(jsonString);
|
||||
_templates = jsonList.map((json) => SarTemplate.fromJson(json)).toList();
|
||||
|
||||
// Ensure defaults exist (in case user deleted them or version upgrade)
|
||||
_ensureDefaultTemplates();
|
||||
} else {
|
||||
// First time - initialize with defaults
|
||||
_templates = SarTemplate.defaults;
|
||||
await _saveToStorage();
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
notifyListeners();
|
||||
debugPrint('SarTemplateService initialized with ${_templates.length} templates');
|
||||
} catch (e) {
|
||||
debugPrint('Error initializing SAR templates: $e');
|
||||
// Fallback to defaults on error
|
||||
_templates = SarTemplate.defaults;
|
||||
_initialized = true;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure default templates exist
|
||||
void _ensureDefaultTemplates() {
|
||||
final defaults = SarTemplate.defaults;
|
||||
final existingDefaultIds = _templates.where((t) => t.isDefault).map((t) => t.id).toSet();
|
||||
|
||||
// Add missing defaults
|
||||
for (final defaultTemplate in defaults) {
|
||||
if (!existingDefaultIds.contains(defaultTemplate.id)) {
|
||||
_templates.insert(0, defaultTemplate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Save templates to storage
|
||||
Future<void> _saveToStorage() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonList = _templates.map((t) => t.toJson()).toList();
|
||||
final jsonString = json.encode(jsonList);
|
||||
await prefs.setString(_storageKey, jsonString);
|
||||
debugPrint('Saved ${_templates.length} SAR templates to storage');
|
||||
} catch (e) {
|
||||
debugPrint('Error saving SAR templates: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Add new template
|
||||
Future<void> addTemplate(SarTemplate template) async {
|
||||
_templates.add(template);
|
||||
await _saveToStorage();
|
||||
notifyListeners();
|
||||
debugPrint('Added SAR template: ${template.name}');
|
||||
}
|
||||
|
||||
/// Update existing template
|
||||
Future<void> updateTemplate(String id, SarTemplate updatedTemplate) async {
|
||||
final index = _templates.indexWhere((t) => t.id == id);
|
||||
if (index != -1) {
|
||||
_templates[index] = updatedTemplate;
|
||||
await _saveToStorage();
|
||||
notifyListeners();
|
||||
debugPrint('Updated SAR template: ${updatedTemplate.name}');
|
||||
} else {
|
||||
throw Exception('Template with id $id not found');
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete template
|
||||
Future<void> deleteTemplate(String id) async {
|
||||
final template = _templates.firstWhere((t) => t.id == id);
|
||||
_templates.removeWhere((t) => t.id == id);
|
||||
await _saveToStorage();
|
||||
notifyListeners();
|
||||
debugPrint('Deleted SAR template: ${template.name}');
|
||||
}
|
||||
|
||||
/// Get template by ID
|
||||
SarTemplate? getTemplateById(String id) {
|
||||
try {
|
||||
return _templates.firstWhere((t) => t.id == id);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Import templates from clipboard
|
||||
/// Expects SAR message format (one per line):
|
||||
/// S:🧑:0,0:Person found
|
||||
/// S:🔥:0,0:Active fire
|
||||
Future<int> importFromClipboard() async {
|
||||
try {
|
||||
final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
if (clipboardData == null || clipboardData.text == null || clipboardData.text!.trim().isEmpty) {
|
||||
throw Exception('Clipboard is empty');
|
||||
}
|
||||
|
||||
return importFromText(clipboardData.text!);
|
||||
} catch (e) {
|
||||
debugPrint('Error importing from clipboard: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Import templates from text (SAR message format)
|
||||
Future<int> importFromText(String text) async {
|
||||
try {
|
||||
final lines = text.split('\n').where((line) => line.trim().isNotEmpty).toList();
|
||||
int importedCount = 0;
|
||||
final List<String> errors = [];
|
||||
|
||||
for (final line in lines) {
|
||||
final trimmed = line.trim();
|
||||
if (!trimmed.startsWith('S:')) {
|
||||
errors.add('Invalid format: $trimmed');
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate with parser
|
||||
if (!SarMessageParser.isValidFormat(trimmed)) {
|
||||
final error = SarMessageParser.getFormatError(trimmed);
|
||||
errors.add(error ?? 'Invalid SAR message format');
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
final template = SarTemplate.fromSarMessage(trimmed);
|
||||
|
||||
// Check for duplicates (same emoji + name)
|
||||
final isDuplicate = _templates.any((t) =>
|
||||
t.emoji == template.emoji && t.name == template.name
|
||||
);
|
||||
|
||||
if (!isDuplicate) {
|
||||
_templates.add(template);
|
||||
importedCount++;
|
||||
}
|
||||
} catch (e) {
|
||||
errors.add('Error parsing line: $trimmed - $e');
|
||||
}
|
||||
}
|
||||
|
||||
if (importedCount > 0) {
|
||||
await _saveToStorage();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
if (errors.isNotEmpty) {
|
||||
debugPrint('Import errors: ${errors.join(', ')}');
|
||||
}
|
||||
|
||||
debugPrint('Imported $importedCount SAR templates');
|
||||
return importedCount;
|
||||
} catch (e) {
|
||||
debugPrint('Error importing templates: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Export all templates to clipboard (SAR message format)
|
||||
Future<void> exportToClipboard() async {
|
||||
try {
|
||||
final sarMessages = _templates.map((t) => t.toSarMessage()).join('\n');
|
||||
await Clipboard.setData(ClipboardData(text: sarMessages));
|
||||
debugPrint('Exported ${_templates.length} templates to clipboard');
|
||||
} catch (e) {
|
||||
debugPrint('Error exporting to clipboard: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Export templates to text (SAR message format)
|
||||
String exportToText() {
|
||||
return _templates.map((t) => t.toSarMessage()).join('\n');
|
||||
}
|
||||
|
||||
/// Reset to default templates
|
||||
Future<void> resetToDefaults() async {
|
||||
_templates = SarTemplate.defaults;
|
||||
await _saveToStorage();
|
||||
notifyListeners();
|
||||
debugPrint('Reset to default SAR templates');
|
||||
}
|
||||
|
||||
/// Clear all templates (including defaults)
|
||||
Future<void> clearAll() async {
|
||||
_templates.clear();
|
||||
await _saveToStorage();
|
||||
notifyListeners();
|
||||
debugPrint('Cleared all SAR templates');
|
||||
}
|
||||
|
||||
/// Get count of templates
|
||||
int get templateCount => _templates.length;
|
||||
|
||||
/// Check if template exists
|
||||
bool hasTemplate(String id) {
|
||||
return _templates.any((t) => t.id == id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:flutter_map_tile_caching/flutter_map_tile_caching.dart';
|
||||
import 'package:flutter_map_tile_caching/custom_backend_api.dart';
|
||||
import 'package:vector_map_tiles_mbtiles/vector_map_tiles_mbtiles.dart';
|
||||
import 'package:mbtiles/mbtiles.dart';
|
||||
import '../models/map_layer.dart';
|
||||
@@ -173,6 +172,91 @@ class TileCacheService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Export the current tile cache store to an archive file
|
||||
///
|
||||
/// [outputPath] - Full path where the archive should be saved (e.g., '/path/to/export.fmtc')
|
||||
///
|
||||
/// Returns the number of tiles exported
|
||||
Future<int> exportStore(String outputPath) async {
|
||||
if (!_isInitialized) {
|
||||
throw StateError(
|
||||
'TileCacheService not initialized. Call initialize() first.',
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
final external = FMTCRoot.external(pathToArchive: outputPath);
|
||||
final result = await external.export(storeNames: [_storeName]);
|
||||
|
||||
debugPrint('Export completed: $result tiles exported to $outputPath');
|
||||
return result;
|
||||
} catch (e) {
|
||||
debugPrint('Error exporting store: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Import a tile cache store from an archive file
|
||||
///
|
||||
/// [filePath] - Path to the .fmtc archive file to import
|
||||
/// [storeNames] - Optional list of store names to import (null = import all)
|
||||
/// [strategy] - Conflict resolution strategy (default: merge)
|
||||
///
|
||||
/// Returns a map with import statistics (e.g., tile count, stores imported)
|
||||
Future<Map<String, dynamic>> importStore(
|
||||
String filePath, {
|
||||
List<String>? storeNames,
|
||||
ImportConflictStrategy strategy = ImportConflictStrategy.merge,
|
||||
}) async {
|
||||
if (!_isInitialized) {
|
||||
throw StateError(
|
||||
'TileCacheService not initialized. Call initialize() first.',
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
final external = FMTCRoot.external(pathToArchive: filePath);
|
||||
final result = external.import(storeNames: storeNames, strategy: strategy);
|
||||
|
||||
// Wait for the import to complete and get tile count
|
||||
final tileCount = await result.complete;
|
||||
|
||||
// Wait for store states
|
||||
final storesToStates = await result.storesToStates;
|
||||
|
||||
debugPrint('Import completed: $tileCount tiles imported, ${storesToStates.length} stores');
|
||||
|
||||
// Count successful stores (those that weren't skipped)
|
||||
final successfulCount = storesToStates.values.where((state) => state.name != null).length;
|
||||
|
||||
return {
|
||||
'successfulStores': successfulCount,
|
||||
'tileCount': tileCount,
|
||||
'storesToStates': storesToStates,
|
||||
};
|
||||
} catch (e) {
|
||||
debugPrint('Error importing store: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// List all stores available in an archive file without importing
|
||||
///
|
||||
/// [filePath] - Path to the .fmtc archive file to inspect
|
||||
///
|
||||
/// Returns a list of store names contained in the archive
|
||||
Future<List<String>> listArchiveStores(String filePath) async {
|
||||
try {
|
||||
final external = FMTCRoot.external(pathToArchive: filePath);
|
||||
final stores = await external.listStores;
|
||||
debugPrint('Archive contains ${stores.length} stores: $stores');
|
||||
return stores;
|
||||
} catch (e) {
|
||||
debugPrint('Error listing archive stores: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_isInitialized = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user