mirror of
https://github.com/FoggedLens/deflock-app.git
synced 2026-07-08 13:47:56 +02:00
builds, needs work
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../models/camera_profile.dart';
|
||||
import '../models/osm_camera_node.dart';
|
||||
|
||||
// ------------------ AddCameraSession ------------------
|
||||
class AddCameraSession {
|
||||
@@ -11,11 +12,28 @@ class AddCameraSession {
|
||||
LatLng? target;
|
||||
}
|
||||
|
||||
// ------------------ EditCameraSession ------------------
|
||||
class EditCameraSession {
|
||||
EditCameraSession({
|
||||
required this.originalNode,
|
||||
required this.profile,
|
||||
required this.directionDegrees,
|
||||
required this.target,
|
||||
});
|
||||
|
||||
final OsmCameraNode originalNode; // The original camera being edited
|
||||
CameraProfile profile;
|
||||
double directionDegrees;
|
||||
LatLng target; // Current position (can be dragged)
|
||||
}
|
||||
|
||||
class SessionState extends ChangeNotifier {
|
||||
AddCameraSession? _session;
|
||||
EditCameraSession? _editSession;
|
||||
|
||||
// Getter
|
||||
// Getters
|
||||
AddCameraSession? get session => _session;
|
||||
EditCameraSession? get editSession => _editSession;
|
||||
|
||||
void startAddSession(List<CameraProfile> enabledProfiles) {
|
||||
final submittableProfiles = enabledProfiles.where((p) => p.isSubmittable).toList();
|
||||
@@ -23,9 +41,46 @@ class SessionState extends ChangeNotifier {
|
||||
? submittableProfiles.first
|
||||
: enabledProfiles.first; // Fallback to any enabled profile
|
||||
_session = AddCameraSession(profile: defaultProfile);
|
||||
_editSession = null; // Clear any edit session
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void startEditSession(OsmCameraNode node, List<CameraProfile> enabledProfiles) {
|
||||
final submittableProfiles = enabledProfiles.where((p) => p.isSubmittable).toList();
|
||||
|
||||
// Try to find a matching profile based on the node's tags
|
||||
CameraProfile matchingProfile = submittableProfiles.isNotEmpty
|
||||
? submittableProfiles.first
|
||||
: enabledProfiles.first;
|
||||
|
||||
// Attempt to find a better match by comparing tags
|
||||
for (final profile in submittableProfiles) {
|
||||
if (_profileMatchesTags(profile, node.tags)) {
|
||||
matchingProfile = profile;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_editSession = EditCameraSession(
|
||||
originalNode: node,
|
||||
profile: matchingProfile,
|
||||
directionDegrees: node.directionDeg ?? 0,
|
||||
target: node.coord,
|
||||
);
|
||||
_session = null; // Clear any add session
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool _profileMatchesTags(CameraProfile profile, Map<String, String> tags) {
|
||||
// Simple matching: check if all profile tags are present in node tags
|
||||
for (final entry in profile.tags.entries) {
|
||||
if (tags[entry.key] != entry.value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void updateSession({
|
||||
double? directionDeg,
|
||||
CameraProfile? profile,
|
||||
@@ -49,11 +104,39 @@ class SessionState extends ChangeNotifier {
|
||||
if (dirty) notifyListeners();
|
||||
}
|
||||
|
||||
void updateEditSession({
|
||||
double? directionDeg,
|
||||
CameraProfile? profile,
|
||||
LatLng? target,
|
||||
}) {
|
||||
if (_editSession == null) return;
|
||||
|
||||
bool dirty = false;
|
||||
if (directionDeg != null && directionDeg != _editSession!.directionDegrees) {
|
||||
_editSession!.directionDegrees = directionDeg;
|
||||
dirty = true;
|
||||
}
|
||||
if (profile != null && profile != _editSession!.profile) {
|
||||
_editSession!.profile = profile;
|
||||
dirty = true;
|
||||
}
|
||||
if (target != null && target != _editSession!.target) {
|
||||
_editSession!.target = target;
|
||||
dirty = true;
|
||||
}
|
||||
if (dirty) notifyListeners();
|
||||
}
|
||||
|
||||
void cancelSession() {
|
||||
_session = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void cancelEditSession() {
|
||||
_editSession = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
AddCameraSession? commitSession() {
|
||||
if (_session?.target == null) return null;
|
||||
|
||||
@@ -62,4 +145,13 @@ class SessionState extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
return session;
|
||||
}
|
||||
|
||||
EditCameraSession? commitEditSession() {
|
||||
if (_editSession == null) return null;
|
||||
|
||||
final session = _editSession!;
|
||||
_editSession = null;
|
||||
notifyListeners();
|
||||
return session;
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,35 @@ class UploadQueueState extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// Add a completed edit session to the upload queue
|
||||
void addFromEditSession(EditCameraSession session) {
|
||||
final upload = PendingUpload(
|
||||
coord: session.target,
|
||||
direction: session.directionDegrees,
|
||||
profile: session.profile,
|
||||
originalNodeId: session.originalNode.id, // Track which node we're editing
|
||||
);
|
||||
|
||||
_queue.add(upload);
|
||||
_saveQueue();
|
||||
|
||||
// Update the original camera in the cache to mark it as having a pending edit
|
||||
final originalTags = Map<String, String>.from(session.originalNode.tags);
|
||||
originalTags['_pending_upload'] = 'true'; // Mark as pending for UI distinction
|
||||
|
||||
final updatedNode = OsmCameraNode(
|
||||
id: session.originalNode.id,
|
||||
coord: session.originalNode.coord,
|
||||
tags: originalTags,
|
||||
);
|
||||
|
||||
CameraCache.instance.addOrUpdate([updatedNode]);
|
||||
// Notify camera provider to update the map
|
||||
CameraProviderWithCache.instance.notifyListeners();
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearQueue() {
|
||||
_queue.clear();
|
||||
_saveQueue();
|
||||
|
||||
Reference in New Issue
Block a user