mirror of
https://github.com/FoggedLens/deflock-app.git
synced 2026-02-13 09:12:56 +00:00
Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34eac41a96 | ||
|
|
816dadfbd1 | ||
|
|
607ecbafaf | ||
|
|
8b44b3abf5 | ||
|
|
a675cf185a | ||
|
|
26b479bf20 | ||
|
|
ae795a7607 | ||
|
|
a05e03567e | ||
|
|
da6887f7d3 | ||
|
|
89fb0d9bbd | ||
|
|
9db7c11a49 | ||
|
|
c3752fd17e | ||
|
|
aab4f6d445 | ||
|
|
f8643da8e2 | ||
|
|
45a72ede30 | ||
|
|
0c324fc78f | ||
|
|
42b5707d0e | ||
|
|
a941a5a5f0 | ||
|
|
6363cabacf | ||
|
|
5312456a15 | ||
|
|
8493679526 | ||
|
|
2047645e89 | ||
|
|
656dbc8ce8 | ||
|
|
eca227032d | ||
|
|
ca7bfc01ad | ||
|
|
4a4fc30828 | ||
|
|
e6b18bf89b | ||
|
|
6ed30dcff8 | ||
|
|
98e7e499d4 | ||
|
|
7fb467872a | ||
|
|
405ec220d0 | ||
|
|
56d55bb922 | ||
|
|
d665db868a | ||
|
|
b0d2ae22fe | ||
|
|
ffec43495b | ||
|
|
16b8acad3a | ||
|
|
4fba26ff55 | ||
|
|
b02623deac | ||
|
|
adbe8c340c | ||
|
|
8c4f53ff7b | ||
|
|
b1a39a2320 | ||
|
|
59064f7165 | ||
|
|
24214e94f9 |
76
DEVELOPER.md
76
DEVELOPER.md
@@ -202,15 +202,24 @@ Deletions don't need position dragging or tag editing - they just need confirmat
|
||||
- Retries: Exponential backoff up to 59 minutes
|
||||
- Failures: OSM auto-closes after 60 minutes, so we eventually give up
|
||||
|
||||
**Queue processing workflow:**
|
||||
**Queue processing workflow (v2.3.0+ concurrent processing):**
|
||||
1. User action (add/edit/delete) → `PendingUpload` created with `UploadState.pending`
|
||||
2. Immediate visual feedback (cache updated with temp markers)
|
||||
3. Background uploader processes queue when online:
|
||||
3. Background uploader starts new uploads every 5 seconds (configurable via `kUploadQueueProcessingInterval`):
|
||||
- **Concurrency limit**: Maximum 5 uploads processing simultaneously (`kMaxConcurrentUploads`)
|
||||
- **Individual lifecycles**: Each upload processes through all three stages independently
|
||||
- **Timer role**: Only used to start new pending uploads, not control stage progression
|
||||
4. Each upload processes through stages without waiting for other uploads:
|
||||
- **Pending** → Create changeset → **CreatingChangeset** → **Uploading**
|
||||
- **Uploading** → Upload node → **ClosingChangeset**
|
||||
- **ClosingChangeset** → Close changeset → **Complete**
|
||||
4. Success → cache updated with real data, temp markers removed
|
||||
5. Failures → appropriate retry logic based on which stage failed
|
||||
5. Success → cache updated with real data, temp markers removed
|
||||
6. Failures → appropriate retry logic based on which stage failed
|
||||
|
||||
**Performance improvement (v2.3.0):**
|
||||
- **Before**: Sequential processing with 10-second delays between each stage of each upload
|
||||
- **After**: Concurrent processing with uploads completing in 10-30 seconds regardless of queue size
|
||||
- **User benefit**: 3-5x faster upload processing for users with good internet connections
|
||||
|
||||
**Why three explicit stages:**
|
||||
The previous implementation conflated changeset creation + node operation as one step, making error handling unclear. The new approach:
|
||||
@@ -242,6 +251,10 @@ Users expect instant response to their actions. By immediately updating the cach
|
||||
- **Orange ring**: Node currently being edited
|
||||
- **Red ring**: Nodes pending deletion
|
||||
|
||||
**Node dimming behavior:**
|
||||
- **Dimmed (50% opacity)**: Non-selected nodes when a specific node is selected for tag viewing, or all nodes during search/navigation modes
|
||||
- **Selection persistence**: When viewing a node's tag sheet, other nodes remain dimmed even when the map is moved, until the sheet is closed (v2.1.3+ fix)
|
||||
|
||||
**Direction cone visual states:**
|
||||
- **Full opacity**: Active session direction (currently being edited)
|
||||
- **Reduced opacity (40%)**: Inactive session directions
|
||||
@@ -284,13 +297,21 @@ These are internal app tags, not OSM tags. The underscore prefix makes this expl
|
||||
- **Rate limiting**: Extended backoff (30s), no splitting (would make it worse)
|
||||
- **Surgical detection**: Only splits on actual limit errors, not network issues
|
||||
|
||||
**Query optimization:**
|
||||
**Query optimization & deduplication:**
|
||||
- **Pre-fetch limit**: 4x user's display limit (e.g., 1000 nodes for 250 display limit)
|
||||
- **Profile deduplication**: Automatically removes redundant profiles from queries using subsumption analysis
|
||||
- **User-initiated detection**: Only reports loading status for user-facing operations
|
||||
- **Background operations**: Pre-fetch runs silently, doesn't trigger loading states
|
||||
|
||||
**Profile subsumption optimization (v2.1.1+):**
|
||||
To reduce Overpass query complexity, profiles are deduplicated before query generation:
|
||||
- **Subsumption rule**: Profile A subsumes profile B if all of A's non-empty tags exist in B with identical values
|
||||
- **Example**: `Generic ALPR` (tags: `man_made=surveillance, surveillance:type=ALPR`) subsumes `Flock` (same tags + `manufacturer=Flock Safety`)
|
||||
- **Result**: Default profile set reduces from ~11 to ~2 query clauses (Generic ALPR + Generic Gunshot)
|
||||
- **UI unchanged**: All enabled profiles still used for post-query filtering and display matching
|
||||
|
||||
**Why this approach:**
|
||||
Dense urban areas (SF, NYC) with many profiles enabled can easily exceed both 50k node limits and 25s timeouts. Splitting reduces query complexity while surgical error detection avoids unnecessary API load from network issues.
|
||||
Dense urban areas (SF, NYC) with many profiles enabled can easily exceed both 50k node limits and 25s timeouts. Profile deduplication reduces query complexity by ~80% for default setups, while automatic splitting handles remaining edge cases. Surgical error detection avoids unnecessary API load from network issues.
|
||||
|
||||
### 6. Uploader Service Architecture (Refactored v1.5.3)
|
||||
|
||||
@@ -399,24 +420,53 @@ Users often want to follow their location while keeping the map oriented north.
|
||||
**Why the change:**
|
||||
The previous approach tracked both tile loading and surveillance data, creating redundancy since tiles already show loading progress visually on the map. Users don't need to be notified about tile loading issues when they can see tiles loading/failing directly. Focusing only on surveillance data makes the indicator more purposeful and less noisy.
|
||||
|
||||
### 11. Suspected Locations
|
||||
### 11. Suspected Locations (v1.8.0+: SQLite Database Storage)
|
||||
|
||||
**Data pipeline:**
|
||||
- **CSV ingestion**: Downloads utility permit data from alprwatch.org
|
||||
- **CSV ingestion**: Downloads utility permit data from alprwatch.org (100MB+ datasets)
|
||||
- **SQLite storage**: Batch insertion into database with geographic indexing (v1.8.0+)
|
||||
- **Dynamic field parsing**: Stores all CSV columns (except `location` and `ticket_no`) for flexible display
|
||||
- **GeoJSON processing**: Handles Point, Polygon, and MultiPolygon geometries
|
||||
- **Proximity filtering**: Hides suspected locations near confirmed devices
|
||||
- **Regional availability**: Currently select locations, expanding regularly
|
||||
|
||||
**Storage architecture (v1.8.0+):**
|
||||
- **Database**: SQLite with spatial indexing for efficient geographic queries
|
||||
- **Hybrid caching**: Sync cache for immediate UI response + async database queries
|
||||
- **Memory efficiency**: No longer loads entire dataset into memory
|
||||
- **Legacy migration**: Automatic migration from SharedPreferences to SQLite
|
||||
|
||||
**Performance improvements:**
|
||||
- **Startup time**: Reduced from 5-15 seconds to <1 second
|
||||
- **Memory usage**: Reduced from 200-400MB to <10MB
|
||||
- **Query time**: Reduced from 100-500ms to 10-50ms with indexed queries
|
||||
- **Progressive loading**: UI shows cached results immediately, updates with fresh data
|
||||
|
||||
**Display approach:**
|
||||
- **Required fields**: `ticket_no` (for heading) and `location` (for map positioning)
|
||||
- **Dynamic display**: All other CSV fields shown automatically, no hardcoded field list
|
||||
- **Server control**: Field names and content controlled server-side via CSV headers
|
||||
- **Brutalist rendering**: Fields displayed as-is from CSV, empty fields hidden
|
||||
|
||||
**Database schema:**
|
||||
```sql
|
||||
CREATE TABLE suspected_locations (
|
||||
ticket_no TEXT PRIMARY KEY,
|
||||
centroid_lat REAL NOT NULL,
|
||||
centroid_lng REAL NOT NULL,
|
||||
bounds TEXT,
|
||||
geo_json TEXT,
|
||||
all_fields TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_centroid ON suspected_locations (centroid_lat, centroid_lng);
|
||||
```
|
||||
|
||||
**Why utility permits:**
|
||||
Utility companies often must file permits when installing surveillance infrastructure. This creates a paper trail that can indicate potential surveillance sites before devices are confirmed through direct observation.
|
||||
|
||||
**Why SQLite migration:**
|
||||
The original SharedPreferences approach became untenable as the CSV dataset grew beyond 100MB, causing memory pressure and long startup times. SQLite provides efficient storage and querying while maintaining the simple, brutalist architecture the project follows.
|
||||
|
||||
### 12. Upload Mode Simplification
|
||||
|
||||
**Release vs Debug builds:**
|
||||
@@ -484,11 +534,13 @@ The major performance issue was discovered to be double caching with expensive o
|
||||
- **Smart queue management**: Drops oldest requests when queue fills up
|
||||
- **Reduced concurrent connections**: 8 threads instead of 10 for better stability across platforms
|
||||
|
||||
### 14. Navigation & Routing (Implemented, Awaiting Integration)
|
||||
### 14. Navigation & Routing (Implemented and Active)
|
||||
|
||||
**Current state:**
|
||||
- **Search functionality**: Fully implemented and active
|
||||
- **Avoidance routing**: Fully implemented and active
|
||||
- **Distance feedback**: Shows real-time distance when selecting second route point
|
||||
- **Long distance warnings**: Alerts users when routes may timeout (configurable threshold)
|
||||
- **Offline routing**: Requires vector map tiles
|
||||
|
||||
**Architecture:**
|
||||
@@ -496,6 +548,12 @@ The major performance issue was discovered to be double caching with expensive o
|
||||
- RoutingService handles API communication and route calculation
|
||||
- SearchService provides location lookup and geocoding
|
||||
|
||||
**Distance warning system (v1.7.0):**
|
||||
- **Real-time distance display**: Shows distance from first to second point during selection
|
||||
- **Configurable threshold**: `kNavigationDistanceWarningThreshold` in dev_config (default 30km)
|
||||
- **User feedback**: Warning message about potential timeouts for long routes
|
||||
- **Brutalist approach**: Simple distance calculation using existing `Distance()` utility
|
||||
|
||||
---
|
||||
|
||||
## Key Design Decisions & Rationales
|
||||
|
||||
43
README.md
43
README.md
@@ -98,35 +98,36 @@ cp lib/keys.dart.example lib/keys.dart
|
||||
## Roadmap
|
||||
|
||||
### Needed Bugfixes
|
||||
- Ensure GPS/follow-me works after recent revamp (loses lock? have to move map for button state to update?)
|
||||
- Clean cache when nodes have been deleted by others
|
||||
- Are offline areas preferred for fast loading even when online? Check working.
|
||||
|
||||
### Current Development
|
||||
- Decide what to do for extracting nodes attached to a way/relation:
|
||||
- Add ability to downvote suspected locations which are old enough
|
||||
- Turn by turn navigation or at least swipe nav sheet up to see a list
|
||||
- Import/Export map providers, profiles (profiles from deflock identify page?)
|
||||
|
||||
### On Pause
|
||||
- Offline navigation (pending vector map tiles)
|
||||
|
||||
### Future Features & Wishlist
|
||||
- Optional reason message when deleting
|
||||
- Update offline area data while browsing?
|
||||
|
||||
### Maybes
|
||||
- Yellow ring for devices missing specific tag details
|
||||
- Android Auto / CarPlay
|
||||
- "Cache accumulating" offline area?
|
||||
- "Offline areas" as tile provider?
|
||||
- Grab the full latest database for each profile just like for suspected locations (instead of overpass)?
|
||||
- Optional custom icons for profiles to aid identification
|
||||
- Custom device providers and OSM/Overpass alternatives
|
||||
- Offer options for extracting nodes which are attached to a way/relation:
|
||||
- Auto extract (how?)
|
||||
- Leave it alone (wrong answer unless user chooses intentionally)
|
||||
- Manual cleanup (cognitive load for users)
|
||||
- Delete the old one (also wrong answer unless user chooses intentionally)
|
||||
- Give multiple of these options??
|
||||
- Dropdown on "refine tags" page to select acceptable options for camera:mount= (is this a boolean property of a profile?)
|
||||
- Option to pull in profiles from NSI (man_made=surveillance only?)
|
||||
|
||||
### On Pause
|
||||
- Import/Export map providers
|
||||
- Clean cache when nodes have disappeared / been deleted by others / queue item was deleted
|
||||
- Improve offline area node refresh live display
|
||||
|
||||
### Future Features & Wishlist
|
||||
- Update offline area nodes while browsing?
|
||||
- Offline navigation (pending vector map tiles)
|
||||
- Android Auto / CarPlay
|
||||
|
||||
### Maybes
|
||||
- Yellow ring for devices missing specific tag details
|
||||
- "Cache accumulating" offline area
|
||||
- "Offline areas" as tile provider
|
||||
- Grab the full latest database for each profile just like for suspected locations (instead of overpass)
|
||||
- Optional custom icons for profiles to aid identification
|
||||
- Custom device providers and OSM/Overpass alternatives
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,243 +0,0 @@
|
||||
# Refactoring Rounds 1 & 2 Complete - v1.6.0
|
||||
|
||||
## Overview
|
||||
Successfully refactored the largest file in the codebase (MapView, 880 lines) by extracting specialized manager classes with clear separation of concerns. This follows the "brutalist code" philosophy of the project - simple, explicit, and maintainable.
|
||||
|
||||
## What Was Accomplished
|
||||
|
||||
### File Size Reduction
|
||||
- **MapView**: 880 lines → 572 lines (**35% reduction, -308 lines**)
|
||||
- **Total new code**: 4 new focused manager classes (351 lines total)
|
||||
- **Net complexity reduction**: Converted monolithic widget into clean orchestrator + specialized managers
|
||||
|
||||
### Step 1.5: Terminology Update (Camera → Node)
|
||||
- **Renamed 3 core files** to use "node" instead of "camera" terminology
|
||||
- **Updated all class names** to reflect current multi-device scope (not just cameras)
|
||||
- **Updated all method names** and comments for consistency
|
||||
- **Updated all imports/references** across the entire codebase
|
||||
- **Benefits**: Consistent terminology that reflects the app's expansion beyond just cameras to all surveillance devices
|
||||
=======
|
||||
|
||||
### New Manager Classes Created
|
||||
|
||||
#### 1. MapDataManager (`lib/widgets/map/map_data_manager.dart`) - 92 lines
|
||||
**Responsibility**: Data fetching, filtering, and node limit logic
|
||||
- `getNodesForRendering()` - Central method for getting filtered/limited nodes
|
||||
- `getMinZoomForNodes()` - Upload mode-aware zoom requirements
|
||||
- `showZoomWarningIfNeeded()` - Zoom level user feedback
|
||||
- `MapDataResult` - Clean result object with all node data + state
|
||||
|
||||
**Benefits**:
|
||||
- Encapsulates all node data logic
|
||||
- Clear separation between data concerns and UI concerns
|
||||
- Easily testable data operations
|
||||
|
||||
#### 2. MapInteractionManager (`lib/widgets/map/map_interaction_manager.dart`) - 45 lines
|
||||
**Responsibility**: Map gesture handling and interaction configuration
|
||||
- `getInteractionOptions()` - Constrained node interaction logic
|
||||
- `mapMovedSignificantly()` - Pan detection for tile queue management
|
||||
|
||||
**Benefits**:
|
||||
- Isolates gesture complexity from UI rendering
|
||||
- Clear constrained node behavior in one place
|
||||
- Reusable interaction logic
|
||||
|
||||
#### 3. MarkerLayerBuilder (`lib/widgets/map/marker_layer_builder.dart`) - 165 lines
|
||||
**Responsibility**: Building all map markers including surveillance nodes, suspected locations, navigation pins, route markers
|
||||
- `buildMarkerLayers()` - Main orchestrator for all marker types
|
||||
- `LocationPin` - Route start/end pin widget (extracted from MapView)
|
||||
- Private methods for each marker category
|
||||
- Proximity filtering for suspected locations
|
||||
|
||||
**Benefits**:
|
||||
- All marker logic in one place
|
||||
- Clean separation of marker types
|
||||
- Reusable marker building functions
|
||||
|
||||
#### 4. OverlayLayerBuilder (`lib/widgets/map/overlay_layer_builder.dart`) - 89 lines
|
||||
**Responsibility**: Building polygons, lines, and route overlays
|
||||
- `buildOverlayLayers()` - Direction cones, edit lines, suspected location bounds, route paths
|
||||
- Clean layer composition
|
||||
- Route visualization logic
|
||||
|
||||
**Benefits**:
|
||||
- Overlay logic separated from marker logic
|
||||
- Clear layer ordering and composition
|
||||
- Easy to add new overlay types
|
||||
|
||||
## Architectural Benefits
|
||||
|
||||
### Brutalist Code Principles Applied
|
||||
1. **Explicit over implicit**: Each manager has one clear responsibility
|
||||
2. **Simple delegation**: MapView orchestrates, managers execute
|
||||
3. **No clever abstractions**: Straightforward method calls and data flow
|
||||
4. **Clear failure points**: Each manager handles its own error cases
|
||||
|
||||
### Maintainability Gains
|
||||
1. **Focused testing**: Each manager can be unit tested independently
|
||||
2. **Clear debugging**: Issues confined to specific domains (data vs UI vs interaction)
|
||||
3. **Easier feature additions**: New marker types go in MarkerLayerBuilder, new data logic goes in MapDataManager
|
||||
4. **Reduced cognitive load**: Developers can focus on one concern at a time
|
||||
|
||||
### Code Organization Improvements
|
||||
1. **Single responsibility**: Each class does exactly one thing
|
||||
2. **Composition over inheritance**: MapView composes managers rather than inheriting complexity
|
||||
3. **Clean interfaces**: Result objects (MapDataResult) provide clear contracts
|
||||
4. **Consistent patterns**: All managers follow same initialization and method patterns
|
||||
|
||||
## Technical Implementation Details
|
||||
|
||||
### Manager Initialization
|
||||
```dart
|
||||
class MapViewState extends State<MapView> {
|
||||
late final MapDataManager _dataManager;
|
||||
late final MapInteractionManager _interactionManager;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// ... existing initialization ...
|
||||
_dataManager = MapDataManager();
|
||||
_interactionManager = MapInteractionManager();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Clean Delegation Pattern
|
||||
```dart
|
||||
// Before: Complex data logic mixed with UI
|
||||
final nodeData = _dataManager.getNodesForRendering(
|
||||
currentZoom: currentZoom,
|
||||
mapBounds: mapBounds,
|
||||
uploadMode: appState.uploadMode,
|
||||
maxNodes: appState.maxNodes,
|
||||
onNodeLimitChanged: widget.onNodeLimitChanged,
|
||||
);
|
||||
|
||||
// Before: Complex marker building mixed with layout
|
||||
final markerLayer = MarkerLayerBuilder.buildMarkerLayers(
|
||||
nodesToRender: nodeData.nodesToRender,
|
||||
mapController: _controller,
|
||||
appState: appState,
|
||||
// ... other parameters
|
||||
);
|
||||
```
|
||||
|
||||
### Result Objects for Clean Interfaces
|
||||
```dart
|
||||
class MapDataResult {
|
||||
final List<OsmNode> allNodes;
|
||||
final List<OsmNode> nodesToRender;
|
||||
final bool isLimitActive;
|
||||
final int validNodesCount;
|
||||
}
|
||||
```
|
||||
|
||||
## Testing Strategy for Round 1
|
||||
|
||||
### Critical Test Areas
|
||||
1. **MapView rendering**: Verify all markers, overlays, and controls still appear correctly
|
||||
2. **Node limit logic**: Test limit indicator shows/hides appropriately
|
||||
3. **Constrained node editing**: Ensure constrained nodes still lock interaction properly
|
||||
4. **Zoom warnings**: Verify zoom level warnings appear at correct thresholds
|
||||
5. **Route visualization**: Test navigation pins and route lines render correctly
|
||||
6. **Suspected locations**: Verify proximity filtering and bounds display
|
||||
7. **Sheet positioning**: Ensure map positioning with sheets still works
|
||||
|
||||
### Regression Prevention
|
||||
- **No functionality changes**: All existing behavior preserved
|
||||
- **Same performance**: No additional overhead from manager pattern
|
||||
- **Clean error handling**: Each manager handles its own error cases
|
||||
- **Memory management**: No memory leaks from manager lifecycle
|
||||
|
||||
## Round 2 Results: HomeScreen Extraction
|
||||
|
||||
Successfully completed HomeScreen refactoring (878 → 604 lines, **31% reduction**):
|
||||
|
||||
### New Coordinator Classes Created
|
||||
|
||||
#### 5. SheetCoordinator (`lib/screens/coordinators/sheet_coordinator.dart`) - 189 lines
|
||||
**Responsibility**: All bottom sheet operations including opening, closing, height tracking
|
||||
- `openAddNodeSheet()`, `openEditNodeSheet()`, `openNavigationSheet()` - Sheet lifecycle management
|
||||
- Height tracking and active sheet calculation
|
||||
- Sheet state management (edit/navigation shown flags)
|
||||
- Sheet transition coordination (prevents map bounce)
|
||||
|
||||
#### 6. NavigationCoordinator (`lib/screens/coordinators/navigation_coordinator.dart`) - 124 lines
|
||||
**Responsibility**: Route planning, navigation, and map centering/zoom logic
|
||||
- `startRoute()`, `resumeRoute()` - Route lifecycle with auto follow-me detection
|
||||
- `handleNavigationButtonPress()` - Search mode and route overview toggling
|
||||
- `zoomToShowFullRoute()` - Intelligent route visualization
|
||||
- Map centering logic based on GPS availability and user proximity
|
||||
|
||||
#### 7. MapInteractionHandler (`lib/screens/coordinators/map_interaction_handler.dart`) - 84 lines
|
||||
**Responsibility**: Map interaction events including node taps and search result selection
|
||||
- `handleNodeTap()` - Node selection with highlighting and centering
|
||||
- `handleSuspectedLocationTap()` - Suspected location interaction
|
||||
- `handleSearchResultSelection()` - Search result processing with map animation
|
||||
- `handleUserGesture()` - Selection clearing on user interaction
|
||||
|
||||
### Round 2 Benefits
|
||||
- **HomeScreen reduced**: 878 lines → 604 lines (**31% reduction, -274 lines**)
|
||||
- **Clear coordinator separation**: Each coordinator handles one domain (sheets, navigation, interactions)
|
||||
- **Simplified HomeScreen**: Now primarily orchestrates coordinators rather than implementing logic
|
||||
- **Better testability**: Coordinators can be unit tested independently
|
||||
- **Enhanced maintainability**: Feature additions have clear homes in appropriate coordinators
|
||||
|
||||
## Combined Results (Both Rounds)
|
||||
|
||||
### Total Impact
|
||||
- **MapView**: 880 → 572 lines (**-308 lines**)
|
||||
- **HomeScreen**: 878 → 604 lines (**-274 lines**)
|
||||
- **Total reduction**: **582 lines** removed from the two largest files
|
||||
- **New focused classes**: 7 manager/coordinator classes with clear responsibilities
|
||||
- **Net code increase**: 947 lines added across all new classes
|
||||
- **Overall impact**: +365 lines total, but dramatically improved organization and maintainability
|
||||
|
||||
### Architectural Transformation
|
||||
- **Before**: Two monolithic files handling multiple concerns each
|
||||
- **After**: Clean orchestrator pattern with focused managers/coordinators
|
||||
- **Maintainability**: Exponentially improved due to separation of concerns
|
||||
- **Testability**: Each manager/coordinator can be independently tested
|
||||
- **Feature Development**: Clear homes for new functionality
|
||||
|
||||
## Next Phase: AppState (Optional Round 3)
|
||||
|
||||
The third largest file is AppState (729 lines). If desired, could extract:
|
||||
1. **SessionCoordinator** - Add/edit session management
|
||||
2. **NavigationStateCoordinator** - Search and route state management
|
||||
3. **DataCoordinator** - Upload queue and node operations
|
||||
|
||||
Expected reduction: ~300-400 lines, but AppState is already well-organized as the central state provider.
|
||||
|
||||
## Files Modified
|
||||
|
||||
### New Files
|
||||
- `lib/widgets/map/map_data_manager.dart`
|
||||
- `lib/widgets/map/map_interaction_manager.dart`
|
||||
- `lib/widgets/map/marker_layer_builder.dart`
|
||||
- `lib/widgets/map/overlay_layer_builder.dart`
|
||||
- `lib/widgets/node_provider_with_cache.dart` (renamed from camera_provider_with_cache.dart)
|
||||
- `lib/widgets/map/node_refresh_controller.dart` (renamed from camera_refresh_controller.dart)
|
||||
- `lib/widgets/map/node_markers.dart` (renamed from camera_markers.dart)
|
||||
|
||||
### Modified Files
|
||||
- `lib/widgets/map_view.dart` (880 → 572 lines)
|
||||
- `lib/app_state.dart` (updated imports and references)
|
||||
- `lib/state/upload_queue_state.dart` (updated all references)
|
||||
- `lib/services/prefetch_area_service.dart` (updated references)
|
||||
|
||||
### Removed Files
|
||||
- `lib/widgets/camera_provider_with_cache.dart` (renamed to node_provider_with_cache.dart)
|
||||
- `lib/widgets/map/camera_refresh_controller.dart` (renamed to node_refresh_controller.dart)
|
||||
- `lib/widgets/map/camera_markers.dart` (renamed to node_markers.dart)
|
||||
|
||||
### Total Impact
|
||||
- **Lines removed**: 308 from MapView
|
||||
- **Lines added**: 351 across 4 focused managers
|
||||
- **Net addition**: 43 lines total
|
||||
- **Complexity reduction**: Significant (monolithic → modular)
|
||||
|
||||
---
|
||||
|
||||
This refactoring maintains backward compatibility while dramatically improving code organization and maintainability. The brutalist approach ensures each component has a clear, single purpose with explicit interfaces.
|
||||
@@ -1,125 +0,0 @@
|
||||
# Upload System Refactor - v1.5.3
|
||||
|
||||
## Overview
|
||||
Refactored the upload queue processing and OSM submission logic to properly handle the three distinct phases of OSM node operations, fixing the core issue where step 2 failures (node operations) weren't handled correctly.
|
||||
|
||||
## Problem Analysis
|
||||
The previous implementation incorrectly treated OSM interaction as a 2-step process:
|
||||
1. ~~Open changeset + submit node~~ (conflated)
|
||||
2. Close changeset
|
||||
|
||||
But OSM actually requires 3 distinct steps:
|
||||
1. **Create changeset**
|
||||
2. **Perform node operation** (create/modify/delete)
|
||||
3. **Close changeset**
|
||||
|
||||
### Issues Fixed:
|
||||
- **Step 2 failure handling**: Node operation failures now properly close orphaned changesets and retry appropriately
|
||||
- **State confusion**: Users now see exactly which of the 3 stages is happening or failed
|
||||
- **Error tracking**: Each stage has appropriate retry logic and error messages
|
||||
- **UI clarity**: Displays "Creating changeset...", "Uploading...", "Closing changeset..." with progress info
|
||||
|
||||
## Changes Made
|
||||
|
||||
### 1. Uploader Service (`lib/services/uploader.dart`)
|
||||
- **Simplified UploadResult**: Replaced complex boolean flags with simple `success/failure` pattern
|
||||
- **Three explicit methods**:
|
||||
- `createChangeset(PendingUpload)` → Returns changeset ID
|
||||
- `performNodeOperation(PendingUpload, changesetId)` → Returns node ID
|
||||
- `closeChangeset(changesetId)` → Returns success/failure
|
||||
- **Legacy compatibility**: `upload()` method still exists for simulate mode
|
||||
- **Better error context**: Each method provides specific error messages for its stage
|
||||
|
||||
### 2. Upload Queue State (`lib/state/upload_queue_state.dart`)
|
||||
- **Three processing methods**:
|
||||
- `_processCreateChangeset()` - Stage 1
|
||||
- `_processNodeOperation()` - Stage 2
|
||||
- `_processChangesetClose()` - Stage 3
|
||||
- **Proper state transitions**: Clear progression through `pending` → `creatingChangeset` → `uploading` → `closingChangeset` → `complete`
|
||||
- **Stage-specific retry logic**:
|
||||
- Stage 1 failure: Simple retry (no cleanup)
|
||||
- Stage 2 failure: Close orphaned changeset, retry from stage 1
|
||||
- Stage 3 failure: Exponential backoff up to 59 minutes
|
||||
- **Simulate mode support**: All three stages work in simulate mode
|
||||
|
||||
### 3. Upload Queue UI (`lib/screens/upload_queue_screen.dart`)
|
||||
- **Enhanced status display**: Shows retry attempts and time remaining (only when changeset close has failed)
|
||||
- **Better error visibility**: Tap error icon to see detailed failure messages
|
||||
- **Stage progression**: Clear visual feedback for each of the 3 stages
|
||||
- **Cleaner progress display**: Time countdown only shows when there have been changeset close issues
|
||||
|
||||
### 4. Cache Cleanup (`lib/state/upload_queue_state.dart`, `lib/services/node_cache.dart`)
|
||||
- **Fixed orphaned pending nodes**: Removing or clearing queue items now properly cleans up temporary cache markers
|
||||
- **Operation-specific cleanup**:
|
||||
- **Creates**: Remove temporary nodes with `_pending_upload` markers
|
||||
- **Edits**: Remove temp nodes + `_pending_edit` markers from originals
|
||||
- **Deletes**: Remove `_pending_deletion` markers from originals
|
||||
- **Extracts**: Remove temp extracted nodes (leave originals unchanged)
|
||||
- **Added NodeCache methods**: `removePendingDeletionMarker()` for deletion cancellation cleanup
|
||||
|
||||
### 5. Documentation Updates
|
||||
- **DEVELOPER.md**: Added detailed explanation of three-stage architecture
|
||||
- **Changelog**: Updated v1.5.3 release notes to highlight the fix
|
||||
- **Code comments**: Improved throughout for clarity
|
||||
|
||||
## Architecture Benefits
|
||||
|
||||
### Brutalist Code Principles Applied:
|
||||
1. **Explicit over implicit**: Three methods instead of one complex method
|
||||
2. **Simple error handling**: Success/failure instead of multiple boolean flags
|
||||
3. **Clear responsibilities**: Each method does exactly one thing
|
||||
4. **Minimal state complexity**: Straightforward state machine progression
|
||||
|
||||
### User Experience Improvements:
|
||||
- **Transparent progress**: Users see exactly what stage is happening
|
||||
- **Better error messages**: Specific context about which stage failed
|
||||
- **Proper retry behavior**: Stage 2 failures no longer leave orphaned changesets
|
||||
- **Time awareness**: Countdown shows when OSM will auto-close changesets
|
||||
|
||||
### Maintainability Gains:
|
||||
- **Easier debugging**: Each stage can be tested independently
|
||||
- **Clear failure points**: No confusion about which step failed
|
||||
- **Simpler testing**: Individual stages are unit-testable
|
||||
- **Future extensibility**: Easy to add new upload operations or modify stages
|
||||
|
||||
## Refined Retry Logic (Post-Testing Updates)
|
||||
|
||||
After initial testing feedback, the retry logic was refined to properly handle the 59-minute changeset window:
|
||||
|
||||
### Three-Phase Retry Strategy:
|
||||
- **Phase 1 (Create Changeset)**: Up to 3 attempts with 20s delays → Error state (user retry required)
|
||||
- **Phase 2 (Submit Node)**: Unlimited attempts within 59-minute window → Error if time expires
|
||||
- **Phase 3 (Close Changeset)**: Unlimited attempts within 59-minute window → Auto-complete if time expires (trust OSM auto-close)
|
||||
|
||||
### Key Behavioral Changes:
|
||||
- **59-minute timer starts** when changeset creation succeeds (not when node operation completes)
|
||||
- **Node submission failures** retry indefinitely within the 59-minute window
|
||||
- **Changeset close failures** retry indefinitely but never error out (always eventually complete)
|
||||
- **UI countdown** only shows when there have been failures in phases 2 or 3
|
||||
- **Proper error messages**: "Failed to create changeset after 3 attempts" vs "Could not submit node within 59 minutes"
|
||||
|
||||
## Testing Recommendations
|
||||
|
||||
When testing this refactor:
|
||||
|
||||
1. **Normal uploads**: Verify all three stages show proper progression
|
||||
2. **Network interruption**:
|
||||
- Test failure at each stage individually
|
||||
- Verify orphaned changesets are properly closed
|
||||
- Check retry logic works appropriately
|
||||
3. **Error handling**:
|
||||
- Tap error icons to see detailed messages
|
||||
- Verify different error types show stage-specific context
|
||||
4. **Simulate mode**: Confirm all three stages work in simulate mode
|
||||
5. **Queue management**: Verify queue continues processing when individual items fail
|
||||
6. **Changeset closing**: Test that changeset close retries work with exponential backoff
|
||||
|
||||
## Rollback Plan
|
||||
If issues are discovered, the legacy `upload()` method can be restored by:
|
||||
1. Reverting `_processCreateChangeset()` to call `up.upload(item)` directly
|
||||
2. Removing `_processNodeOperation()` and `_processChangesetClose()` calls
|
||||
3. This would restore the old 2-stage behavior while keeping the UI improvements
|
||||
|
||||
---
|
||||
|
||||
The core fix addresses the main issue you identified: **step 2 failures (node operations) are now properly tracked and handled with appropriate cleanup and retry logic**.
|
||||
@@ -1,4 +1,87 @@
|
||||
{
|
||||
"2.3.0": {
|
||||
"content": [
|
||||
"• Concurrent upload queue processing",
|
||||
"• Each submission is now much faster"
|
||||
]
|
||||
},
|
||||
"2.2.1": {
|
||||
"content": [
|
||||
"• Fixed network status indicator timing out prematurely",
|
||||
"• Network status now accurately shows 'taking a while' when requests split or backoff, and only shows 'timed out' for actual network failures"
|
||||
]
|
||||
},
|
||||
"2.2.0": {
|
||||
"content": [
|
||||
"• Fixed follow-me sync issues where tracking would sometimes stop working after mode changes",
|
||||
"• Added cancel button to submission guide dialog - users can now go back and revise their submissions",
|
||||
"• When not logged in, submit/edit buttons now say 'Log In' and navigate to account settings instead of being disabled",
|
||||
"• Improved NSI tag suggestions: now only shows values with sufficient usage (100+ occurrences) to avoid rare/unhelpful suggestions like for 'image=' tags",
|
||||
"• Enhanced tag refinement: refine tags sheet now allows arbitrary text entry like the profile editor, not just dropdown selection",
|
||||
"• New tags are now added to the top of the profile tag list for immediate visibility instead of being hidden at the bottom"
|
||||
]
|
||||
},
|
||||
"2.1.3": {
|
||||
"content": [
|
||||
"• Fixed nodes losing their greyed-out appearance when map is moved while viewing a node's tag sheet",
|
||||
"• Improved GPS location handling - follow-me button is now greyed out when location is unavailable",
|
||||
"• Added approximate location fallback - if precise location is denied, app will use approximate location",
|
||||
"• Higher frequency GPS updates when follow-me modes are active for smoother tracking (1-meter updates vs 5-meter)"
|
||||
]
|
||||
},
|
||||
"2.1.2": {
|
||||
"content": [
|
||||
"• New positioning tutorial - first-time users must drag the map to refine location when creating or editing nodes, helping ensure accurate positioning",
|
||||
"• Tutorial automatically dismisses after moving the map at least 1 meter and never shows again"
|
||||
]
|
||||
},
|
||||
"2.1.0": {
|
||||
"content": [
|
||||
"• Profile tag refinement system - any profile tag with an empty value now shows a dropdown in refine tags",
|
||||
"• OSM Name Suggestion Index (NSI) integration - shows most commonly used tag values from TagInfo API, both when creating/editing profiles and refining tags",
|
||||
"• FIXED: Can now remove FOV values from profiles",
|
||||
"• FIXED: Profile deletion while add/edit sheets are open no longer causes a crash"
|
||||
]
|
||||
},
|
||||
"1.8.3": {
|
||||
"content": [
|
||||
"• Fixed node limit indicator disappearing when navigation sheet opens during search/routing",
|
||||
"• Improved indicator architecture - moved node limit indicator to screen coordinates for consistency with other UI overlays"
|
||||
]
|
||||
},
|
||||
"1.8.2": {
|
||||
"content": [
|
||||
"• Fixed map positioning for node tags and suspected location sheets - map now correctly centers above sheet when opened",
|
||||
"• Improved sheet transition coordination - prevents map bounce when transitioning from tag sheet to edit sheet",
|
||||
"• Enhanced debugging for sheet height measurement and coordination"
|
||||
]
|
||||
},
|
||||
"1.8.0": {
|
||||
"content": [
|
||||
"• Better performance and reduced memory usage when using suspected location data by using a database"
|
||||
]
|
||||
},
|
||||
"1.7.0": {
|
||||
"content": [
|
||||
"• Distance display when selecting second navigation point; shows distance from first location in real-time",
|
||||
"• Long distance warning; routes over 20km display a warning about potential timeouts"
|
||||
]
|
||||
},
|
||||
"1.6.3": {
|
||||
"content": [
|
||||
"• Fixed navigation sheet button flow - route to/from buttons no longer reappear after selecting second location",
|
||||
"• Added cancel button when selecting second route point for easier exit from route planning",
|
||||
"• Removed placeholder FOV values from built-in device profiles - oops"
|
||||
]
|
||||
},
|
||||
"1.6.2": {
|
||||
"content": [
|
||||
"• Improved node rendering bounds - nodes appear slightly before sliding into view and stay visible until just after sliding out, eliminating edge blinking",
|
||||
"• Navigation interaction conflict prevention - nodes and suspected locations are now dimmed and non-clickable during route planning and route overview to prevent state conflicts",
|
||||
"• Enhanced route overview behavior - follow-me is automatically disabled when opening overview and intelligently restored when resuming based on proximity to route",
|
||||
"• Smart route resume - centers on GPS location with follow-me if near route, or route start without follow-me if far away, with configurable proximity threshold"
|
||||
]
|
||||
},
|
||||
"1.6.1": {
|
||||
"content": [
|
||||
"• Navigation route calculation timeout increased from 15 to 30 seconds - better success rate for complex routes in dense areas",
|
||||
|
||||
@@ -56,6 +56,10 @@ class AppState extends ChangeNotifier {
|
||||
late final UploadQueueState _uploadQueueState;
|
||||
|
||||
bool _isInitialized = false;
|
||||
|
||||
// Positioning tutorial state
|
||||
LatLng? _tutorialStartPosition; // Track where the tutorial started
|
||||
VoidCallback? _tutorialCompletionCallback; // Callback when tutorial is completed
|
||||
Timer? _messageCheckTimer;
|
||||
|
||||
AppState() {
|
||||
@@ -114,6 +118,8 @@ class AppState extends ChangeNotifier {
|
||||
bool get settingRouteStart => _navigationState.settingRouteStart;
|
||||
bool get isSettingSecondPoint => _navigationState.isSettingSecondPoint;
|
||||
bool get areRoutePointsTooClose => _navigationState.areRoutePointsTooClose;
|
||||
double? get distanceFromFirstPoint => _navigationState.distanceFromFirstPoint;
|
||||
bool get distanceExceedsWarningThreshold => _navigationState.distanceExceedsWarningThreshold;
|
||||
bool get isCalculating => _navigationState.isCalculating;
|
||||
bool get showingOverview => _navigationState.showingOverview;
|
||||
String? get routingError => _navigationState.routingError;
|
||||
@@ -173,7 +179,8 @@ class AppState extends ChangeNotifier {
|
||||
SuspectedLocation? get selectedSuspectedLocation => _suspectedLocationState.selectedLocation;
|
||||
bool get suspectedLocationsEnabled => _suspectedLocationState.isEnabled;
|
||||
bool get suspectedLocationsLoading => _suspectedLocationState.isLoading;
|
||||
DateTime? get suspectedLocationsLastFetch => _suspectedLocationState.lastFetchTime;
|
||||
double? get suspectedLocationsDownloadProgress => _suspectedLocationState.downloadProgress;
|
||||
Future<DateTime?> get suspectedLocationsLastFetch => _suspectedLocationState.lastFetchTime;
|
||||
|
||||
void _onStateChanged() {
|
||||
notifyListeners();
|
||||
@@ -205,6 +212,9 @@ class AppState extends ChangeNotifier {
|
||||
await _operatorProfileState.init(addDefaults: shouldAddOperatorDefaults);
|
||||
await _profileState.init(addDefaults: shouldAddNodeDefaults);
|
||||
|
||||
// Set up callback to clear stale sessions when profiles are deleted
|
||||
_profileState.setProfileDeletedCallback(_onProfileDeleted);
|
||||
|
||||
// Mark defaults as initialized if this was first launch
|
||||
if (isFirstLaunch) {
|
||||
await prefs.setBool(firstLaunchKey, true);
|
||||
@@ -385,6 +395,19 @@ class AppState extends ChangeNotifier {
|
||||
void deleteProfile(NodeProfile p) {
|
||||
_profileState.deleteProfile(p);
|
||||
}
|
||||
|
||||
// Callback when a profile is deleted - clear any stale session references
|
||||
void _onProfileDeleted(NodeProfile deletedProfile) {
|
||||
// Clear add session if it references the deleted profile
|
||||
if (_sessionState.session?.profile?.id == deletedProfile.id) {
|
||||
cancelSession();
|
||||
}
|
||||
|
||||
// Clear edit session if it references the deleted profile
|
||||
if (_sessionState.editSession?.profile?.id == deletedProfile.id) {
|
||||
cancelEditSession();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Operator Profile Methods ----------
|
||||
void addOrUpdateOperatorProfile(OperatorProfile p) {
|
||||
@@ -409,13 +432,20 @@ class AppState extends ChangeNotifier {
|
||||
NodeProfile? profile,
|
||||
OperatorProfile? operatorProfile,
|
||||
LatLng? target,
|
||||
Map<String, String>? refinedTags,
|
||||
}) {
|
||||
_sessionState.updateSession(
|
||||
directionDeg: directionDeg,
|
||||
profile: profile,
|
||||
operatorProfile: operatorProfile,
|
||||
target: target,
|
||||
refinedTags: refinedTags,
|
||||
);
|
||||
|
||||
// Check tutorial completion if position changed
|
||||
if (target != null) {
|
||||
_checkTutorialCompletion(target);
|
||||
}
|
||||
}
|
||||
|
||||
void updateEditSession({
|
||||
@@ -424,6 +454,7 @@ class AppState extends ChangeNotifier {
|
||||
OperatorProfile? operatorProfile,
|
||||
LatLng? target,
|
||||
bool? extractFromWay,
|
||||
Map<String, String>? refinedTags,
|
||||
}) {
|
||||
_sessionState.updateEditSession(
|
||||
directionDeg: directionDeg,
|
||||
@@ -431,7 +462,13 @@ class AppState extends ChangeNotifier {
|
||||
operatorProfile: operatorProfile,
|
||||
target: target,
|
||||
extractFromWay: extractFromWay,
|
||||
refinedTags: refinedTags,
|
||||
);
|
||||
|
||||
// Check tutorial completion if position changed
|
||||
if (target != null) {
|
||||
_checkTutorialCompletion(target);
|
||||
}
|
||||
}
|
||||
|
||||
// For map view to check for pending snap backs
|
||||
@@ -439,6 +476,40 @@ class AppState extends ChangeNotifier {
|
||||
return _sessionState.consumePendingSnapBack();
|
||||
}
|
||||
|
||||
// Positioning tutorial methods
|
||||
void registerTutorialCallback(VoidCallback onComplete) {
|
||||
_tutorialCompletionCallback = onComplete;
|
||||
// Record the starting position when tutorial begins
|
||||
if (session?.target != null) {
|
||||
_tutorialStartPosition = session!.target;
|
||||
} else if (editSession?.target != null) {
|
||||
_tutorialStartPosition = editSession!.target;
|
||||
}
|
||||
}
|
||||
|
||||
void clearTutorialCallback() {
|
||||
_tutorialCompletionCallback = null;
|
||||
_tutorialStartPosition = null;
|
||||
}
|
||||
|
||||
void _checkTutorialCompletion(LatLng newPosition) {
|
||||
if (_tutorialCompletionCallback == null || _tutorialStartPosition == null) return;
|
||||
|
||||
// Calculate distance moved
|
||||
final distance = Distance();
|
||||
final distanceMoved = distance.as(LengthUnit.Meter, _tutorialStartPosition!, newPosition);
|
||||
|
||||
if (distanceMoved >= kPositioningTutorialMinMovementMeters) {
|
||||
// Tutorial completed! Mark as complete and notify callback immediately
|
||||
final callback = _tutorialCompletionCallback;
|
||||
clearTutorialCallback();
|
||||
callback?.call();
|
||||
|
||||
// Mark as complete in background (don't await to avoid delays)
|
||||
ChangelogService().markPositioningTutorialCompleted();
|
||||
}
|
||||
}
|
||||
|
||||
void addDirection() {
|
||||
_sessionState.addDirection();
|
||||
}
|
||||
@@ -633,13 +704,7 @@ class AppState extends ChangeNotifier {
|
||||
await _settingsState.setNetworkStatusIndicatorEnabled(enabled);
|
||||
}
|
||||
|
||||
/// Migrate upload queue to new two-stage changeset system (v1.5.3)
|
||||
Future<void> migrateUploadQueueToTwoStageSystem() async {
|
||||
// Migration is handled automatically in PendingUpload.fromJson via _migrateFromLegacyFields
|
||||
// This method triggers a queue reload to apply migrations
|
||||
await _uploadQueueState.reloadQueue();
|
||||
debugPrint('[AppState] Upload queue migration completed');
|
||||
}
|
||||
|
||||
|
||||
/// Set suspected location minimum distance from real nodes
|
||||
Future<void> setSuspectedLocationMinDistance(int distance) async {
|
||||
@@ -665,6 +730,11 @@ class AppState extends ChangeNotifier {
|
||||
_startUploader(); // resume uploader if not busy
|
||||
}
|
||||
|
||||
/// Reload upload queue from storage (for migration purposes)
|
||||
Future<void> reloadUploadQueue() async {
|
||||
await _uploadQueueState.reloadQueue();
|
||||
}
|
||||
|
||||
// ---------- Suspected Location Methods ----------
|
||||
Future<void> setSuspectedLocationsEnabled(bool enabled) async {
|
||||
await _suspectedLocationState.setEnabled(enabled);
|
||||
@@ -674,6 +744,10 @@ class AppState extends ChangeNotifier {
|
||||
return await _suspectedLocationState.refreshData();
|
||||
}
|
||||
|
||||
Future<void> reinitSuspectedLocations() async {
|
||||
await _suspectedLocationState.init(offlineMode: _settingsState.offlineMode);
|
||||
}
|
||||
|
||||
void selectSuspectedLocation(SuspectedLocation location) {
|
||||
_suspectedLocationState.selectLocation(location);
|
||||
}
|
||||
@@ -682,13 +756,27 @@ class AppState extends ChangeNotifier {
|
||||
_suspectedLocationState.clearSelection();
|
||||
}
|
||||
|
||||
List<SuspectedLocation> getSuspectedLocationsInBounds({
|
||||
Future<List<SuspectedLocation>> getSuspectedLocationsInBounds({
|
||||
required double north,
|
||||
required double south,
|
||||
required double east,
|
||||
required double west,
|
||||
}) async {
|
||||
return await _suspectedLocationState.getLocationsInBounds(
|
||||
north: north,
|
||||
south: south,
|
||||
east: east,
|
||||
west: west,
|
||||
);
|
||||
}
|
||||
|
||||
List<SuspectedLocation> getSuspectedLocationsInBoundsSync({
|
||||
required double north,
|
||||
required double south,
|
||||
required double east,
|
||||
required double west,
|
||||
}) {
|
||||
return _suspectedLocationState.getLocationsInBounds(
|
||||
return _suspectedLocationState.getLocationsInBoundsSync(
|
||||
north: north,
|
||||
south: south,
|
||||
east: east,
|
||||
|
||||
@@ -55,13 +55,18 @@ const String kClientName = 'DeFlock';
|
||||
|
||||
// Upload and changeset configuration
|
||||
const Duration kUploadHttpTimeout = Duration(seconds: 30); // HTTP request timeout for uploads
|
||||
const Duration kUploadQueueProcessingInterval = Duration(seconds: 5); // How often to check for new uploads to start
|
||||
const int kMaxConcurrentUploads = 5; // Maximum number of uploads processing simultaneously
|
||||
const Duration kChangesetCloseInitialRetryDelay = Duration(seconds: 10);
|
||||
const Duration kChangesetCloseMaxRetryDelay = Duration(minutes: 5); // Cap at 5 minutes
|
||||
const Duration kChangesetAutoCloseTimeout = Duration(minutes: 59); // Give up and trust OSM auto-close
|
||||
const double kChangesetCloseBackoffMultiplier = 2.0;
|
||||
|
||||
// Navigation routing configuration
|
||||
const Duration kNavigationRoutingTimeout = Duration(seconds: 30); // HTTP timeout for routing requests
|
||||
const Duration kNavigationRoutingTimeout = Duration(seconds: 90); // HTTP timeout for routing requests
|
||||
|
||||
// Overpass API configuration
|
||||
const Duration kOverpassQueryTimeout = Duration(seconds: 45); // Timeout for Overpass API queries (was 25s hardcoded)
|
||||
|
||||
// Suspected locations CSV URL
|
||||
const String kSuspectedLocationsCsvUrl = 'https://alprwatch.org/suspected-locations/deflock-latest.csv';
|
||||
@@ -93,6 +98,9 @@ const Duration kDebounceCameraRefresh = Duration(milliseconds: 500);
|
||||
|
||||
// Pre-fetch area configuration
|
||||
const double kPreFetchAreaExpansionMultiplier = 3.0; // Expand visible bounds by this factor for pre-fetching
|
||||
const double kNodeRenderingBoundsExpansion = 1.3; // Expand visible bounds by this factor for node rendering to prevent edge blinking
|
||||
const double kRouteProximityThresholdMeters = 500.0; // Distance threshold for determining if user is near route when resuming navigation
|
||||
const double kResumeNavigationZoomLevel = 16.0; // Zoom level when resuming navigation
|
||||
const int kPreFetchZoomLevel = 10; // Always pre-fetch at this zoom level for consistent area sizes
|
||||
const int kMaxPreFetchSplitDepth = 3; // Maximum recursive splits when hitting Overpass node limit
|
||||
|
||||
@@ -123,12 +131,20 @@ const Duration kProximityAlertCooldown = Duration(minutes: 10); // Cooldown betw
|
||||
// Node proximity warning configuration (for new/edited nodes that are too close to existing ones)
|
||||
const double kNodeProximityWarningDistance = 15.0; // meters - distance threshold to show warning
|
||||
|
||||
// Positioning tutorial configuration
|
||||
const double kPositioningTutorialBlurSigma = 3.0; // Blur strength for sheet overlay
|
||||
const double kPositioningTutorialMinMovementMeters = 1.0; // Minimum map movement to complete tutorial
|
||||
|
||||
// Navigation route planning configuration
|
||||
const double kNavigationMinRouteDistance = 100.0; // meters - minimum distance between start and end points
|
||||
const double kNavigationDistanceWarningThreshold = 20000.0; // meters - distance threshold for timeout warning (30km)
|
||||
|
||||
// Node display configuration
|
||||
const int kDefaultMaxNodes = 500; // Default maximum number of nodes to render on the map at once
|
||||
|
||||
// NSI (Name Suggestion Index) configuration
|
||||
const int kNSIMinimumHitCount = 500; // Minimum hit count for NSI suggestions to be considered useful
|
||||
|
||||
// Map interaction configuration
|
||||
const double kNodeDoubleTapZoomDelta = 1.0; // How much to zoom in when double-tapping nodes (was 1.0)
|
||||
const double kScrollWheelVelocity = 0.01; // Mouse scroll wheel zoom speed (default 0.005)
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"ok": "OK",
|
||||
"close": "Schließen",
|
||||
"submit": "Senden",
|
||||
"logIn": "Anmelden",
|
||||
"saveEdit": "Bearbeitung Speichern",
|
||||
"clear": "Löschen",
|
||||
"viewOnOSM": "Auf OSM anzeigen",
|
||||
@@ -53,7 +54,7 @@
|
||||
"aboutSubtitle": "App-Informationen und Credits",
|
||||
"languageSubtitle": "Wählen Sie Ihre bevorzugte Sprache",
|
||||
"maxNodes": "Max. angezeigte Knoten",
|
||||
"maxNodesSubtitle": "Obergrenze für die Anzahl der Knoten auf der Karte festlegen (Standard: 250).",
|
||||
"maxNodesSubtitle": "Obergrenze für die Anzahl der Knoten auf der Karte festlegen.",
|
||||
"maxNodesWarning": "Sie möchten das wahrscheinlich nicht tun, es sei denn, Sie sind absolut sicher, dass Sie einen guten Grund dafür haben.",
|
||||
"offlineMode": "Offline-Modus",
|
||||
"offlineModeSubtitle": "Alle Netzwerkanfragen außer für lokale/Offline-Bereiche deaktivieren.",
|
||||
@@ -368,7 +369,12 @@
|
||||
"additionalTagsTitle": "Zusätzliche Tags",
|
||||
"noTagsDefinedForProfile": "Keine Tags für dieses Betreiber-Profil definiert.",
|
||||
"noOperatorProfiles": "Keine Betreiber-Profile definiert",
|
||||
"noOperatorProfilesMessage": "Erstellen Sie Betreiber-Profile in den Einstellungen, um zusätzliche Tags auf Ihre Knoten-Übertragungen anzuwenden."
|
||||
"noOperatorProfilesMessage": "Erstellen Sie Betreiber-Profile in den Einstellungen, um zusätzliche Tags auf Ihre Knoten-Übertragungen anzuwenden.",
|
||||
"profileTags": "Profil-Tags",
|
||||
"profileTagsDescription": "Geben Sie Werte für Tags an, die verfeinert werden müssen:",
|
||||
"selectValue": "Wert auswählen...",
|
||||
"noValue": "(Kein Wert)",
|
||||
"noSuggestions": "Keine Vorschläge verfügbar"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "Kachel-Typen können während des Herunterladens von Offline-Bereichen nicht geändert werden",
|
||||
@@ -441,6 +447,11 @@
|
||||
"dontShowAgain": "Diese Anleitung nicht mehr anzeigen",
|
||||
"gotIt": "Verstanden!"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "Position verfeinern",
|
||||
"instructions": "Ziehen Sie die Karte, um die Geräte-Markierung präzise über dem Standort des Überwachungsgeräts zu positionieren.",
|
||||
"hint": "Sie können für bessere Genauigkeit vor der Positionierung hineinzoomen."
|
||||
},
|
||||
"navigation": {
|
||||
"searchLocation": "Ort suchen",
|
||||
"searchPlaceholder": "Orte oder Koordinaten suchen...",
|
||||
|
||||
@@ -37,6 +37,11 @@
|
||||
"dontShowAgain": "Don't show this guide again",
|
||||
"gotIt": "Got It!"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "Refine Your Location",
|
||||
"instructions": "Drag the map to position the device marker precisely over the surveillance device's location.",
|
||||
"hint": "You can zoom in for better accuracy before positioning."
|
||||
},
|
||||
"actions": {
|
||||
"tagNode": "New Node",
|
||||
"download": "Download",
|
||||
@@ -47,6 +52,7 @@
|
||||
"ok": "OK",
|
||||
"close": "Close",
|
||||
"submit": "Submit",
|
||||
"logIn": "Log In",
|
||||
"saveEdit": "Save Edit",
|
||||
"clear": "Clear",
|
||||
"viewOnOSM": "View on OSM",
|
||||
@@ -85,7 +91,7 @@
|
||||
"aboutSubtitle": "App information and credits",
|
||||
"languageSubtitle": "Choose your preferred language",
|
||||
"maxNodes": "Max nodes drawn",
|
||||
"maxNodesSubtitle": "Set an upper limit for the number of nodes on the map (default: 250).",
|
||||
"maxNodesSubtitle": "Set an upper limit for the number of nodes on the map.",
|
||||
"maxNodesWarning": "You probably don't want to do that unless you are absolutely sure you have a good reason for it.",
|
||||
"offlineMode": "Offline Mode",
|
||||
"offlineModeSubtitle": "Disable all network requests except for local/offline areas.",
|
||||
@@ -400,7 +406,12 @@
|
||||
"additionalTagsTitle": "Additional Tags",
|
||||
"noTagsDefinedForProfile": "No tags defined for this operator profile.",
|
||||
"noOperatorProfiles": "No operator profiles defined",
|
||||
"noOperatorProfilesMessage": "Create operator profiles in Settings to apply additional tags to your node submissions."
|
||||
"noOperatorProfilesMessage": "Create operator profiles in Settings to apply additional tags to your node submissions.",
|
||||
"profileTags": "Profile Tags",
|
||||
"profileTagsDescription": "Complete these optional tag values for more detailed submissions:",
|
||||
"selectValue": "Select value...",
|
||||
"noValue": "(leave empty)",
|
||||
"noSuggestions": "No suggestions available"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "Cannot change tile types while downloading offline areas",
|
||||
|
||||
@@ -37,6 +37,11 @@
|
||||
"dontShowAgain": "No mostrar esta guía otra vez",
|
||||
"gotIt": "¡Entendido!"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "Refinar Ubicación",
|
||||
"instructions": "Arrastra el mapa para posicionar el marcador del dispositivo con precisión sobre la ubicación del dispositivo de vigilancia.",
|
||||
"hint": "Puedes acercar el zoom para obtener mejor precisión antes de posicionar."
|
||||
},
|
||||
"actions": {
|
||||
"tagNode": "Nuevo Nodo",
|
||||
"download": "Descargar",
|
||||
@@ -47,6 +52,7 @@
|
||||
"ok": "Aceptar",
|
||||
"close": "Cerrar",
|
||||
"submit": "Enviar",
|
||||
"logIn": "Iniciar Sesión",
|
||||
"saveEdit": "Guardar Edición",
|
||||
"clear": "Limpiar",
|
||||
"viewOnOSM": "Ver en OSM",
|
||||
@@ -85,7 +91,7 @@
|
||||
"aboutSubtitle": "Información de la aplicación y créditos",
|
||||
"languageSubtitle": "Elige tu idioma preferido",
|
||||
"maxNodes": "Máx. nodos dibujados",
|
||||
"maxNodesSubtitle": "Establecer un límite superior para el número de nodos en el mapa (predeterminado: 250).",
|
||||
"maxNodesSubtitle": "Establecer un límite superior para el número de nodos en el mapa.",
|
||||
"maxNodesWarning": "Probablemente no quieras hacer eso a menos que estés absolutamente seguro de que tienes una buena razón para ello.",
|
||||
"offlineMode": "Modo Sin Conexión",
|
||||
"offlineModeSubtitle": "Deshabilitar todas las solicitudes de red excepto para áreas locales/sin conexión.",
|
||||
@@ -400,7 +406,12 @@
|
||||
"additionalTagsTitle": "Etiquetas Adicionales",
|
||||
"noTagsDefinedForProfile": "No hay etiquetas definidas para este perfil de operador.",
|
||||
"noOperatorProfiles": "No hay perfiles de operador definidos",
|
||||
"noOperatorProfilesMessage": "Cree perfiles de operador en Configuración para aplicar etiquetas adicionales a sus envíos de nodos."
|
||||
"noOperatorProfilesMessage": "Cree perfiles de operador en Configuración para aplicar etiquetas adicionales a sus envíos de nodos.",
|
||||
"profileTags": "Etiquetas de Perfil",
|
||||
"profileTagsDescription": "Especifique valores para etiquetas que necesitan refinamiento:",
|
||||
"selectValue": "Seleccionar un valor...",
|
||||
"noValue": "(Sin valor)",
|
||||
"noSuggestions": "No hay sugerencias disponibles"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "No se pueden cambiar los tipos de teselas mientras se descargan áreas sin conexión",
|
||||
|
||||
@@ -37,6 +37,11 @@
|
||||
"dontShowAgain": "Ne plus afficher ce guide",
|
||||
"gotIt": "Compris !"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "Affiner la Position",
|
||||
"instructions": "Faites glisser la carte pour positionner le marqueur de l'appareil précisément au-dessus de l'emplacement du dispositif de surveillance.",
|
||||
"hint": "Vous pouvez zoomer pour une meilleure précision avant de positionner."
|
||||
},
|
||||
"actions": {
|
||||
"tagNode": "Nouveau Nœud",
|
||||
"download": "Télécharger",
|
||||
@@ -47,6 +52,7 @@
|
||||
"ok": "OK",
|
||||
"close": "Fermer",
|
||||
"submit": "Soumettre",
|
||||
"logIn": "Se Connecter",
|
||||
"saveEdit": "Sauvegarder Modification",
|
||||
"clear": "Effacer",
|
||||
"viewOnOSM": "Voir sur OSM",
|
||||
@@ -85,7 +91,7 @@
|
||||
"aboutSubtitle": "Informations sur l'application et crédits",
|
||||
"languageSubtitle": "Choisissez votre langue préférée",
|
||||
"maxNodes": "Max. nœuds dessinés",
|
||||
"maxNodesSubtitle": "Définir une limite supérieure pour le nombre de nœuds sur la carte (par défaut: 250).",
|
||||
"maxNodesSubtitle": "Définir une limite supérieure pour le nombre de nœuds sur la carte.",
|
||||
"maxNodesWarning": "Vous ne voulez probablement pas faire cela à moins d'être absolument sûr d'avoir une bonne raison de le faire.",
|
||||
"offlineMode": "Mode Hors Ligne",
|
||||
"offlineModeSubtitle": "Désactiver toutes les requêtes réseau sauf pour les zones locales/hors ligne.",
|
||||
@@ -400,7 +406,12 @@
|
||||
"additionalTagsTitle": "Étiquettes Supplémentaires",
|
||||
"noTagsDefinedForProfile": "Aucune étiquette définie pour ce profil d'opérateur.",
|
||||
"noOperatorProfiles": "Aucun profil d'opérateur défini",
|
||||
"noOperatorProfilesMessage": "Créez des profils d'opérateur dans les Paramètres pour appliquer des étiquettes supplémentaires à vos soumissions de nœuds."
|
||||
"noOperatorProfilesMessage": "Créez des profils d'opérateur dans les Paramètres pour appliquer des étiquettes supplémentaires à vos soumissions de nœuds.",
|
||||
"profileTags": "Étiquettes de Profil",
|
||||
"profileTagsDescription": "Spécifiez des valeurs pour les étiquettes qui nécessitent un raffinement :",
|
||||
"selectValue": "Sélectionner une valeur...",
|
||||
"noValue": "(Aucune valeur)",
|
||||
"noSuggestions": "Aucune suggestion disponible"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "Impossible de changer les types de tuiles pendant le téléchargement des zones hors ligne",
|
||||
|
||||
@@ -37,6 +37,11 @@
|
||||
"dontShowAgain": "Non mostrare più questa guida",
|
||||
"gotIt": "Capito!"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "Affinare la Posizione",
|
||||
"instructions": "Trascina la mappa per posizionare il marcatore del dispositivo precisamente sopra la posizione del dispositivo di sorveglianza.",
|
||||
"hint": "Puoi ingrandire per una maggiore precisione prima di posizionare."
|
||||
},
|
||||
"actions": {
|
||||
"tagNode": "Nuovo Nodo",
|
||||
"download": "Scarica",
|
||||
@@ -47,6 +52,7 @@
|
||||
"ok": "OK",
|
||||
"close": "Chiudi",
|
||||
"submit": "Invia",
|
||||
"logIn": "Accedi",
|
||||
"saveEdit": "Salva Modifica",
|
||||
"clear": "Pulisci",
|
||||
"viewOnOSM": "Visualizza su OSM",
|
||||
@@ -85,7 +91,7 @@
|
||||
"aboutSubtitle": "Informazioni sull'applicazione e crediti",
|
||||
"languageSubtitle": "Scegli la tua lingua preferita",
|
||||
"maxNodes": "Max nodi disegnati",
|
||||
"maxNodesSubtitle": "Imposta un limite superiore per il numero di nodi sulla mappa (predefinito: 250).",
|
||||
"maxNodesSubtitle": "Imposta un limite superiore per il numero di nodi sulla mappa.",
|
||||
"maxNodesWarning": "Probabilmente non vuoi farlo a meno che non sei assolutamente sicuro di avere una buona ragione per farlo.",
|
||||
"offlineMode": "Modalità Offline",
|
||||
"offlineModeSubtitle": "Disabilita tutte le richieste di rete tranne per aree locali/offline.",
|
||||
@@ -400,7 +406,12 @@
|
||||
"additionalTagsTitle": "Tag Aggiuntivi",
|
||||
"noTagsDefinedForProfile": "Nessun tag definito per questo profilo operatore.",
|
||||
"noOperatorProfiles": "Nessun profilo operatore definito",
|
||||
"noOperatorProfilesMessage": "Crea profili operatore nelle Impostazioni per applicare tag aggiuntivi ai tuoi invii di nodi."
|
||||
"noOperatorProfilesMessage": "Crea profili operatore nelle Impostazioni per applicare tag aggiuntivi ai tuoi invii di nodi.",
|
||||
"profileTags": "Tag del Profilo",
|
||||
"profileTagsDescription": "Specificare valori per i tag che necessitano di raffinamento:",
|
||||
"selectValue": "Seleziona un valore...",
|
||||
"noValue": "(Nessun valore)",
|
||||
"noSuggestions": "Nessun suggerimento disponibile"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "Impossibile cambiare tipi di tile durante il download di aree offline",
|
||||
|
||||
@@ -37,6 +37,11 @@
|
||||
"dontShowAgain": "Não mostrar este guia novamente",
|
||||
"gotIt": "Entendi!"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "Refinar Posição",
|
||||
"instructions": "Arraste o mapa para posicionar o marcador do dispositivo precisamente sobre a localização do dispositivo de vigilância.",
|
||||
"hint": "Você pode aumentar o zoom para melhor precisão antes de posicionar."
|
||||
},
|
||||
"actions": {
|
||||
"tagNode": "Novo Nó",
|
||||
"download": "Baixar",
|
||||
@@ -47,6 +52,7 @@
|
||||
"ok": "OK",
|
||||
"close": "Fechar",
|
||||
"submit": "Enviar",
|
||||
"logIn": "Entrar",
|
||||
"saveEdit": "Salvar Edição",
|
||||
"clear": "Limpar",
|
||||
"viewOnOSM": "Ver no OSM",
|
||||
@@ -85,7 +91,7 @@
|
||||
"aboutSubtitle": "Informações do aplicativo e créditos",
|
||||
"languageSubtitle": "Escolha seu idioma preferido",
|
||||
"maxNodes": "Máx. de nós desenhados",
|
||||
"maxNodesSubtitle": "Definir um limite superior para o número de nós no mapa (padrão: 250).",
|
||||
"maxNodesSubtitle": "Definir um limite superior para o número de nós no mapa.",
|
||||
"maxNodesWarning": "Você provavelmente não quer fazer isso a menos que tenha certeza absoluta de que tem uma boa razão para isso.",
|
||||
"offlineMode": "Modo Offline",
|
||||
"offlineModeSubtitle": "Desabilitar todas as requisições de rede exceto para áreas locais/offline.",
|
||||
@@ -400,7 +406,12 @@
|
||||
"additionalTagsTitle": "Tags Adicionais",
|
||||
"noTagsDefinedForProfile": "Nenhuma tag definida para este perfil de operador.",
|
||||
"noOperatorProfiles": "Nenhum perfil de operador definido",
|
||||
"noOperatorProfilesMessage": "Crie perfis de operador nas Configurações para aplicar tags adicionais aos seus envios de nós."
|
||||
"noOperatorProfilesMessage": "Crie perfis de operador nas Configurações para aplicar tags adicionais aos seus envios de nós.",
|
||||
"profileTags": "Tags do Perfil",
|
||||
"profileTagsDescription": "Especifique valores para tags que precisam de refinamento:",
|
||||
"selectValue": "Selecionar um valor...",
|
||||
"noValue": "(Sem valor)",
|
||||
"noSuggestions": "Nenhuma sugestão disponível"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "Não é possível alterar tipos de tiles durante o download de áreas offline",
|
||||
|
||||
@@ -37,6 +37,11 @@
|
||||
"dontShowAgain": "不再显示此指南",
|
||||
"gotIt": "明白了!"
|
||||
},
|
||||
"positioningTutorial": {
|
||||
"title": "精确定位",
|
||||
"instructions": "拖动地图将设备标记精确定位在监控设备的位置上。",
|
||||
"hint": "您可以在定位前放大地图以获得更高的精度。"
|
||||
},
|
||||
"actions": {
|
||||
"tagNode": "新建节点",
|
||||
"download": "下载",
|
||||
@@ -47,6 +52,7 @@
|
||||
"ok": "确定",
|
||||
"close": "关闭",
|
||||
"submit": "提交",
|
||||
"logIn": "登录",
|
||||
"saveEdit": "保存编辑",
|
||||
"clear": "清空",
|
||||
"viewOnOSM": "在OSM上查看",
|
||||
@@ -85,7 +91,7 @@
|
||||
"aboutSubtitle": "应用程序信息和鸣谢",
|
||||
"languageSubtitle": "选择您的首选语言",
|
||||
"maxNodes": "最大节点绘制数",
|
||||
"maxNodesSubtitle": "设置地图上节点数量的上限(默认:250)。",
|
||||
"maxNodesSubtitle": "设置地图上节点数量的上限。",
|
||||
"maxNodesWarning": "除非您确定有充分的理由,否则您可能不想这样做。",
|
||||
"offlineMode": "离线模式",
|
||||
"offlineModeSubtitle": "禁用除本地/离线区域外的所有网络请求。",
|
||||
@@ -400,7 +406,12 @@
|
||||
"additionalTagsTitle": "额外标签",
|
||||
"noTagsDefinedForProfile": "此运营商配置文件未定义标签。",
|
||||
"noOperatorProfiles": "未定义运营商配置文件",
|
||||
"noOperatorProfilesMessage": "在设置中创建运营商配置文件,以将额外标签应用于您的节点提交。"
|
||||
"noOperatorProfilesMessage": "在设置中创建运营商配置文件,以将额外标签应用于您的节点提交。",
|
||||
"profileTags": "配置文件标签",
|
||||
"profileTagsDescription": "为需要细化的标签指定值:",
|
||||
"selectValue": "选择值...",
|
||||
"noValue": "(无值)",
|
||||
"noSuggestions": "无建议可用"
|
||||
},
|
||||
"layerSelector": {
|
||||
"cannotChangeTileTypes": "在下载离线区域时无法更改瓦片类型",
|
||||
|
||||
158
lib/migrations.dart
Normal file
158
lib/migrations.dart
Normal file
@@ -0,0 +1,158 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'app_state.dart';
|
||||
import 'services/profile_service.dart';
|
||||
import 'services/suspected_location_cache.dart';
|
||||
import 'widgets/nuclear_reset_dialog.dart';
|
||||
|
||||
/// One-time migrations that run when users upgrade to specific versions.
|
||||
/// Each migration function is named after the version where it should run.
|
||||
class OneTimeMigrations {
|
||||
/// Enable network status indicator for all existing users (v1.3.1)
|
||||
static Future<void> migrate_1_3_1(AppState appState) async {
|
||||
await appState.setNetworkStatusIndicatorEnabled(true);
|
||||
debugPrint('[Migration] 1.3.1 completed: enabled network status indicator');
|
||||
}
|
||||
|
||||
/// Migrate upload queue to new two-stage changeset system (v1.5.3)
|
||||
static Future<void> migrate_1_5_3(AppState appState) async {
|
||||
// Migration is handled automatically in PendingUpload.fromJson via _migrateFromLegacyFields
|
||||
// This triggers a queue reload to apply migrations
|
||||
await appState.reloadUploadQueue();
|
||||
debugPrint('[Migration] 1.5.3 completed: migrated upload queue to two-stage system');
|
||||
}
|
||||
|
||||
/// Clear FOV values from built-in profiles only (v1.6.3)
|
||||
static Future<void> migrate_1_6_3(AppState appState) async {
|
||||
// Load all custom profiles from storage (includes any customized built-in profiles)
|
||||
final profiles = await ProfileService().load();
|
||||
|
||||
// Find profiles with built-in IDs and clear their FOV values
|
||||
final updatedProfiles = profiles.map((profile) {
|
||||
if (profile.id.startsWith('builtin-') && profile.fov != null) {
|
||||
debugPrint('[Migration] Clearing FOV from profile: ${profile.id}');
|
||||
return profile.copyWith(fov: null);
|
||||
}
|
||||
return profile;
|
||||
}).toList();
|
||||
|
||||
// Save updated profiles back to storage
|
||||
await ProfileService().save(updatedProfiles);
|
||||
|
||||
debugPrint('[Migration] 1.6.3 completed: cleared FOV values from built-in profiles');
|
||||
}
|
||||
|
||||
/// Migrate suspected locations from SharedPreferences to SQLite (v1.8.0)
|
||||
static Future<void> migrate_1_8_0(AppState appState) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// Legacy SharedPreferences keys
|
||||
const legacyProcessedDataKey = 'suspected_locations_processed_data';
|
||||
const legacyLastFetchKey = 'suspected_locations_last_fetch';
|
||||
|
||||
// Check if we have legacy data
|
||||
final legacyData = prefs.getString(legacyProcessedDataKey);
|
||||
final legacyLastFetch = prefs.getInt(legacyLastFetchKey);
|
||||
|
||||
if (legacyData != null && legacyLastFetch != null) {
|
||||
debugPrint('[Migration] 1.8.0: Found legacy suspected location data, migrating to database...');
|
||||
|
||||
// Parse legacy processed data format
|
||||
final List<dynamic> legacyProcessedList = jsonDecode(legacyData);
|
||||
final List<Map<String, dynamic>> rawDataList = [];
|
||||
|
||||
for (final entry in legacyProcessedList) {
|
||||
if (entry is Map<String, dynamic> && entry['rawData'] != null) {
|
||||
rawDataList.add(Map<String, dynamic>.from(entry['rawData']));
|
||||
}
|
||||
}
|
||||
|
||||
if (rawDataList.isNotEmpty) {
|
||||
final fetchTime = DateTime.fromMillisecondsSinceEpoch(legacyLastFetch);
|
||||
|
||||
// Get the cache instance and migrate data
|
||||
final cache = SuspectedLocationCache();
|
||||
await cache.loadFromStorage(); // Initialize database
|
||||
await cache.processAndSave(rawDataList, fetchTime);
|
||||
|
||||
debugPrint('[Migration] 1.8.0: Migrated ${rawDataList.length} entries from legacy storage');
|
||||
}
|
||||
|
||||
// Clean up legacy data after successful migration
|
||||
await prefs.remove(legacyProcessedDataKey);
|
||||
await prefs.remove(legacyLastFetchKey);
|
||||
|
||||
debugPrint('[Migration] 1.8.0: Legacy data cleanup completed');
|
||||
}
|
||||
|
||||
// Ensure suspected locations are reinitialized with new system
|
||||
await appState.reinitSuspectedLocations();
|
||||
|
||||
debugPrint('[Migration] 1.8.0 completed: migrated suspected locations to SQLite database');
|
||||
} catch (e) {
|
||||
debugPrint('[Migration] 1.8.0 ERROR: Failed to migrate suspected locations: $e');
|
||||
// Don't rethrow - migration failure shouldn't break the app
|
||||
// The new system will work fine, users just lose their cached data
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear any active sessions to reset refined tags system (v2.1.0)
|
||||
static Future<void> migrate_2_1_0(AppState appState) async {
|
||||
try {
|
||||
// Clear any existing sessions since they won't have refinedTags field
|
||||
// This is simpler and safer than trying to migrate session data
|
||||
appState.cancelSession();
|
||||
appState.cancelEditSession();
|
||||
|
||||
debugPrint('[Migration] 2.1.0 completed: cleared sessions for refined tags system');
|
||||
} catch (e) {
|
||||
debugPrint('[Migration] 2.1.0 ERROR: Failed to clear sessions: $e');
|
||||
// Don't rethrow - this is non-critical
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the migration function for a specific version
|
||||
static Future<void> Function(AppState)? getMigrationForVersion(String version) {
|
||||
switch (version) {
|
||||
case '1.3.1':
|
||||
return migrate_1_3_1;
|
||||
case '1.5.3':
|
||||
return migrate_1_5_3;
|
||||
case '1.6.3':
|
||||
return migrate_1_6_3;
|
||||
case '1.8.0':
|
||||
return migrate_1_8_0;
|
||||
case '2.1.0':
|
||||
return migrate_2_1_0;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Run migration for a specific version with nuclear reset on failure
|
||||
static Future<void> runMigration(String version, AppState appState, BuildContext? context) async {
|
||||
try {
|
||||
final migration = getMigrationForVersion(version);
|
||||
if (migration != null) {
|
||||
await migration(appState);
|
||||
} else {
|
||||
debugPrint('[Migration] Unknown migration version: $version');
|
||||
}
|
||||
} catch (error, stackTrace) {
|
||||
debugPrint('[Migration] CRITICAL: Migration $version failed: $error');
|
||||
debugPrint('[Migration] Stack trace: $stackTrace');
|
||||
|
||||
// Nuclear option: clear everything and show non-dismissible error dialog
|
||||
if (context != null) {
|
||||
NuclearResetDialog.show(context, error, stackTrace);
|
||||
} else {
|
||||
// If no context available, just log and hope for the best
|
||||
debugPrint('[Migration] No context available for error dialog, migration failure unhandled');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Flock Safety',
|
||||
'manufacturer:wikidata': 'Q108485435',
|
||||
},
|
||||
@@ -62,6 +63,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Motorola Solutions',
|
||||
'manufacturer:wikidata': 'Q634815',
|
||||
},
|
||||
@@ -79,6 +81,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Genetec',
|
||||
'manufacturer:wikidata': 'Q30295174',
|
||||
},
|
||||
@@ -96,6 +99,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Leonardo',
|
||||
'manufacturer:wikidata': 'Q910379',
|
||||
},
|
||||
@@ -113,6 +117,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Neology, Inc.',
|
||||
},
|
||||
builtin: true,
|
||||
@@ -129,6 +134,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Rekor',
|
||||
},
|
||||
builtin: true,
|
||||
@@ -145,6 +151,7 @@ class NodeProfile {
|
||||
'surveillance:type': 'ALPR',
|
||||
'surveillance:zone': 'traffic',
|
||||
'camera:type': 'fixed',
|
||||
'camera:mount': '', // Empty value for refinement
|
||||
'manufacturer': 'Axis Communications',
|
||||
'manufacturer:wikidata': 'Q2347731',
|
||||
},
|
||||
|
||||
@@ -21,6 +21,7 @@ class PendingUpload {
|
||||
final dynamic direction; // Can be double or String for multiple directions
|
||||
final NodeProfile? profile;
|
||||
final OperatorProfile? operatorProfile;
|
||||
final Map<String, String> refinedTags; // User-selected values for empty profile tags
|
||||
final UploadMode uploadMode; // Capture upload destination when queued
|
||||
final UploadOperation operation; // Type of operation: create, modify, or delete
|
||||
final int? originalNodeId; // If this is modify/delete, the ID of the original OSM node
|
||||
@@ -43,6 +44,7 @@ class PendingUpload {
|
||||
required this.direction,
|
||||
this.profile,
|
||||
this.operatorProfile,
|
||||
Map<String, String>? refinedTags,
|
||||
required this.uploadMode,
|
||||
required this.operation,
|
||||
this.originalNodeId,
|
||||
@@ -59,7 +61,8 @@ class PendingUpload {
|
||||
this.lastChangesetCloseAttemptAt,
|
||||
this.nodeSubmissionAttempts = 0,
|
||||
this.lastNodeSubmissionAttemptAt,
|
||||
}) : assert(
|
||||
}) : refinedTags = refinedTags ?? {},
|
||||
assert(
|
||||
(operation == UploadOperation.create && originalNodeId == null) ||
|
||||
(operation == UploadOperation.create) || (originalNodeId != null),
|
||||
'originalNodeId must be null for create operations and non-null for modify/delete/extract operations'
|
||||
@@ -219,7 +222,7 @@ class PendingUpload {
|
||||
return DateTime.now().isAfter(nextRetryTime);
|
||||
}
|
||||
|
||||
// Get combined tags from node profile and operator profile
|
||||
// Get combined tags from node profile, operator profile, and refined tags
|
||||
Map<String, String> getCombinedTags() {
|
||||
// Deletions don't need tags
|
||||
if (operation == UploadOperation.delete || profile == null) {
|
||||
@@ -228,6 +231,14 @@ class PendingUpload {
|
||||
|
||||
final tags = Map<String, String>.from(profile!.tags);
|
||||
|
||||
// Apply refined tags (these fill in empty values from the profile)
|
||||
for (final entry in refinedTags.entries) {
|
||||
// Only apply refined tags if the profile tag value is empty
|
||||
if (tags.containsKey(entry.key) && tags[entry.key]?.trim().isEmpty == true) {
|
||||
tags[entry.key] = entry.value;
|
||||
}
|
||||
}
|
||||
|
||||
// Add operator profile tags (they override node profile tags if there are conflicts)
|
||||
if (operatorProfile != null) {
|
||||
tags.addAll(operatorProfile!.tags);
|
||||
@@ -244,6 +255,10 @@ class PendingUpload {
|
||||
}
|
||||
}
|
||||
|
||||
// Filter out any tags that are still empty after refinement
|
||||
// Empty tags in profiles are fine for refinement UI, but shouldn't be submitted to OSM
|
||||
tags.removeWhere((key, value) => value.trim().isEmpty);
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
@@ -253,6 +268,7 @@ class PendingUpload {
|
||||
'dir': direction,
|
||||
'profile': profile?.toJson(),
|
||||
'operatorProfile': operatorProfile?.toJson(),
|
||||
'refinedTags': refinedTags,
|
||||
'uploadMode': uploadMode.index,
|
||||
'operation': operation.index,
|
||||
'originalNodeId': originalNodeId,
|
||||
@@ -280,6 +296,9 @@ class PendingUpload {
|
||||
operatorProfile: j['operatorProfile'] != null
|
||||
? OperatorProfile.fromJson(j['operatorProfile'])
|
||||
: null,
|
||||
refinedTags: j['refinedTags'] != null
|
||||
? Map<String, String>.from(j['refinedTags'])
|
||||
: {}, // Default empty map for legacy entries
|
||||
uploadMode: j['uploadMode'] != null
|
||||
? UploadMode.values[j['uploadMode']]
|
||||
: UploadMode.production, // Default for legacy entries
|
||||
|
||||
@@ -3,12 +3,14 @@ import 'package:flutter_map_animations/flutter_map_animations.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../app_state.dart';
|
||||
import '../../app_state.dart' show AppState, FollowMeMode;
|
||||
import '../../widgets/map_view.dart';
|
||||
import '../../dev_config.dart';
|
||||
|
||||
/// Coordinates all navigation and routing functionality including route planning,
|
||||
/// map centering, zoom management, and route visualization.
|
||||
class NavigationCoordinator {
|
||||
FollowMeMode? _previousFollowMeMode; // Track follow-me mode before overview
|
||||
|
||||
/// Start a route with automatic follow-me detection and appropriate centering
|
||||
void startRoute({
|
||||
@@ -56,8 +58,7 @@ class NavigationCoordinator {
|
||||
// Hide the overview
|
||||
appState.hideRouteOverview();
|
||||
|
||||
// Zoom and center for resumed route
|
||||
// For resume, we always center on user if GPS is available, otherwise start pin
|
||||
// Get user location to determine centering and follow-me behavior
|
||||
LatLng? userLocation;
|
||||
try {
|
||||
userLocation = mapViewKey?.currentState?.getUserLocation();
|
||||
@@ -65,12 +66,53 @@ class NavigationCoordinator {
|
||||
debugPrint('[NavigationCoordinator] Could not get user location for route resume: $e');
|
||||
}
|
||||
|
||||
_zoomAndCenterForRoute(
|
||||
mapController: mapController,
|
||||
followMeEnabled: appState.followMeMode != FollowMeMode.off, // Use current follow-me state
|
||||
userLocation: userLocation,
|
||||
routeStart: appState.routeStart,
|
||||
);
|
||||
// Determine if user is near the route path
|
||||
bool isNearRoute = false;
|
||||
if (userLocation != null && appState.routePath != null) {
|
||||
isNearRoute = _isUserNearRoute(userLocation, appState.routePath!);
|
||||
}
|
||||
|
||||
// Choose center point and follow-me behavior
|
||||
LatLng centerPoint;
|
||||
bool shouldEnableFollowMe = false;
|
||||
|
||||
if (isNearRoute && userLocation != null) {
|
||||
// User is near route - center on GPS and enable follow-me
|
||||
centerPoint = userLocation;
|
||||
shouldEnableFollowMe = true;
|
||||
debugPrint('[NavigationCoordinator] User near route - centering on GPS with follow-me');
|
||||
} else {
|
||||
// User far from route or no GPS - center on route start
|
||||
centerPoint = appState.routeStart ?? userLocation ?? LatLng(0, 0);
|
||||
shouldEnableFollowMe = false;
|
||||
debugPrint('[NavigationCoordinator] User far from route - centering on start without follow-me');
|
||||
}
|
||||
|
||||
// Apply the centering and zoom
|
||||
try {
|
||||
mapController.animateTo(
|
||||
dest: centerPoint,
|
||||
zoom: kResumeNavigationZoomLevel,
|
||||
duration: const Duration(milliseconds: 800),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[NavigationCoordinator] Could not animate to resume location: $e');
|
||||
}
|
||||
|
||||
// Set follow-me mode based on proximity
|
||||
if (shouldEnableFollowMe) {
|
||||
// Restore previous follow-me mode if user is near route
|
||||
final modeToRestore = _previousFollowMeMode ?? FollowMeMode.follow;
|
||||
appState.setFollowMeMode(modeToRestore);
|
||||
debugPrint('[NavigationCoordinator] Restored follow-me mode: $modeToRestore');
|
||||
} else {
|
||||
// Keep follow-me off if user is far from route
|
||||
debugPrint('[NavigationCoordinator] Keeping follow-me off - user far from route');
|
||||
}
|
||||
|
||||
// Clear stored follow-me mode
|
||||
_previousFollowMeMode = null;
|
||||
}
|
||||
|
||||
/// Handle navigation button press with route overview logic
|
||||
@@ -82,6 +124,9 @@ class NavigationCoordinator {
|
||||
|
||||
if (appState.showRouteButton) {
|
||||
// Route button - show route overview and zoom to show route
|
||||
// Store current follow-me mode and disable it to prevent unexpected map jumps during overview
|
||||
_previousFollowMeMode = appState.followMeMode;
|
||||
appState.setFollowMeMode(FollowMeMode.off);
|
||||
appState.showRouteOverview();
|
||||
zoomToShowFullRoute(appState: appState, mapController: mapController);
|
||||
} else {
|
||||
@@ -146,6 +191,20 @@ class NavigationCoordinator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if user location is near the route path
|
||||
bool _isUserNearRoute(LatLng userLocation, List<LatLng> routePath) {
|
||||
if (routePath.isEmpty) return false;
|
||||
|
||||
// Check distance to each point in the route path
|
||||
for (final routePoint in routePath) {
|
||||
final distance = const Distance().as(LengthUnit.Meter, userLocation, routePoint);
|
||||
if (distance <= kRouteProximityThresholdMeters) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Internal method to zoom and center for route start/resume
|
||||
void _zoomAndCenterForRoute({
|
||||
required AnimatedMapController mapController,
|
||||
|
||||
@@ -239,12 +239,14 @@ class SheetCoordinator {
|
||||
|
||||
/// Update tag sheet height (called externally)
|
||||
void updateTagSheetHeight(double height, VoidCallback onStateChanged) {
|
||||
debugPrint('[SheetCoordinator] Updating tag sheet height: $_tagSheetHeight -> $height');
|
||||
_tagSheetHeight = height;
|
||||
onStateChanged();
|
||||
}
|
||||
|
||||
/// Reset tag sheet height
|
||||
void resetTagSheetHeight(VoidCallback onStateChanged) {
|
||||
debugPrint('[SheetCoordinator] Resetting tag sheet height from: $_tagSheetHeight');
|
||||
_tagSheetHeight = 0.0;
|
||||
onStateChanged();
|
||||
}
|
||||
|
||||
@@ -114,6 +114,9 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
|
||||
}
|
||||
|
||||
void _openEditNodeSheet() {
|
||||
// Set transition flag BEFORE closing tag sheet to prevent map bounce
|
||||
_sheetCoordinator.setTransitioningToEdit(true);
|
||||
|
||||
// Close any existing tag sheet first
|
||||
if (_sheetCoordinator.tagSheetHeight > 0) {
|
||||
Navigator.of(context).pop();
|
||||
@@ -160,7 +163,7 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
|
||||
// Run any needed migrations first
|
||||
final versionsNeedingMigration = await ChangelogService().getVersionsNeedingMigration();
|
||||
for (final version in versionsNeedingMigration) {
|
||||
await ChangelogService().runMigration(version, appState);
|
||||
await ChangelogService().runMigration(version, appState, context);
|
||||
}
|
||||
|
||||
// Determine what popup to show
|
||||
@@ -291,7 +294,7 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
|
||||
mapController: _mapController,
|
||||
onSelectedNodeChanged: (id) => setState(() => _selectedNodeId = id),
|
||||
);
|
||||
|
||||
|
||||
final controller = _scaffoldKey.currentState!.showBottomSheet(
|
||||
(ctx) => Padding(
|
||||
padding: EdgeInsets.only(
|
||||
@@ -348,7 +351,7 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
|
||||
location: location,
|
||||
mapController: _mapController,
|
||||
);
|
||||
|
||||
|
||||
final controller = _scaffoldKey.currentState!.showBottomSheet(
|
||||
(ctx) => Padding(
|
||||
padding: EdgeInsets.only(
|
||||
@@ -430,16 +433,18 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
|
||||
IconButton(
|
||||
tooltip: _getFollowMeTooltip(appState.followMeMode),
|
||||
icon: Icon(_getFollowMeIcon(appState.followMeMode)),
|
||||
onPressed: () {
|
||||
final oldMode = appState.followMeMode;
|
||||
final newMode = _getNextFollowMeMode(oldMode);
|
||||
debugPrint('[HomeScreen] Follow mode changed: $oldMode → $newMode');
|
||||
appState.setFollowMeMode(newMode);
|
||||
// If enabling follow-me, retry location init in case permission was granted
|
||||
if (newMode != FollowMeMode.off) {
|
||||
_mapViewKey.currentState?.retryLocationInit();
|
||||
}
|
||||
},
|
||||
onPressed: _mapViewKey.currentState?.hasLocation == true
|
||||
? () {
|
||||
final oldMode = appState.followMeMode;
|
||||
final newMode = _getNextFollowMeMode(oldMode);
|
||||
debugPrint('[HomeScreen] Follow mode changed: $oldMode → $newMode');
|
||||
appState.setFollowMeMode(newMode);
|
||||
// If enabling follow-me, retry location init in case permission was granted
|
||||
if (newMode != FollowMeMode.off) {
|
||||
_mapViewKey.currentState?.retryLocationInit();
|
||||
}
|
||||
}
|
||||
: null, // Grey out when no location
|
||||
),
|
||||
AnimatedBuilder(
|
||||
animation: LocalizationService.instance,
|
||||
@@ -487,11 +492,24 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
|
||||
_isNodeLimitActive = isLimited;
|
||||
});
|
||||
},
|
||||
onLocationStatusChanged: () {
|
||||
// Re-render when location status changes (for follow-me button state)
|
||||
setState(() {});
|
||||
},
|
||||
onUserGesture: () {
|
||||
_mapInteractionHandler.handleUserGesture(
|
||||
context: context,
|
||||
onSelectedNodeChanged: (id) => setState(() => _selectedNodeId = id),
|
||||
);
|
||||
// Only clear selected node if tag sheet is not open
|
||||
// This prevents nodes from losing their grey-out when map is moved while viewing tags
|
||||
if (_sheetCoordinator.tagSheetHeight == 0) {
|
||||
_mapInteractionHandler.handleUserGesture(
|
||||
context: context,
|
||||
onSelectedNodeChanged: (id) => setState(() => _selectedNodeId = id),
|
||||
);
|
||||
} else {
|
||||
// Tag sheet is open - only handle suspected location clearing, not node selection
|
||||
final appState = context.read<AppState>();
|
||||
appState.clearSuspectedLocationSelection();
|
||||
}
|
||||
|
||||
if (appState.followMeMode != FollowMeMode.off) {
|
||||
appState.setFollowMeMode(FollowMeMode.off);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:uuid/uuid.dart';
|
||||
import '../models/operator_profile.dart';
|
||||
import '../app_state.dart';
|
||||
import '../services/localization_service.dart';
|
||||
import '../widgets/nsi_tag_value_field.dart';
|
||||
|
||||
class OperatorProfileEditor extends StatefulWidget {
|
||||
const OperatorProfileEditor({super.key, required this.profile});
|
||||
@@ -123,14 +124,12 @@ class _OperatorProfileEditorState extends State<OperatorProfileEditor> {
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: TextField(
|
||||
decoration: InputDecoration(
|
||||
hintText: locService.t('profileEditor.valueHint'),
|
||||
border: const OutlineInputBorder(),
|
||||
isDense: true,
|
||||
),
|
||||
controller: valueController,
|
||||
onChanged: (v) => _tags[i] = MapEntry(_tags[i].key, v),
|
||||
child: NSITagValueField(
|
||||
key: ValueKey('${_tags[i].key}_$i'), // Rebuild when key changes
|
||||
tagKey: _tags[i].key,
|
||||
initialValue: _tags[i].value,
|
||||
hintText: locService.t('profileEditor.valueHint'),
|
||||
onChanged: (v) => setState(() => _tags[i] = MapEntry(_tags[i].key, v)),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
@@ -155,8 +154,8 @@ class _OperatorProfileEditorState extends State<OperatorProfileEditor> {
|
||||
|
||||
final tagMap = <String, String>{};
|
||||
for (final e in _tags) {
|
||||
if (e.key.trim().isEmpty || e.value.trim().isEmpty) continue;
|
||||
tagMap[e.key.trim()] = e.value.trim();
|
||||
if (e.key.trim().isEmpty) continue; // Skip only if key is empty
|
||||
tagMap[e.key.trim()] = e.value.trim(); // Allow empty values for refinement
|
||||
}
|
||||
|
||||
final newProfile = widget.profile.copyWith(
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:uuid/uuid.dart';
|
||||
import '../models/node_profile.dart';
|
||||
import '../app_state.dart';
|
||||
import '../services/localization_service.dart';
|
||||
import '../widgets/nsi_tag_value_field.dart';
|
||||
|
||||
class ProfileEditor extends StatefulWidget {
|
||||
const ProfileEditor({super.key, required this.profile});
|
||||
@@ -125,7 +126,7 @@ class _ProfileEditorState extends State<ProfileEditor> {
|
||||
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
|
||||
if (widget.profile.editable)
|
||||
TextButton.icon(
|
||||
onPressed: () => setState(() => _tags.add(const MapEntry('', ''))),
|
||||
onPressed: () => setState(() => _tags.insert(0, const MapEntry('', ''))),
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text(locService.t('profileEditor.addTag')),
|
||||
),
|
||||
@@ -175,17 +176,15 @@ class _ProfileEditorState extends State<ProfileEditor> {
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: TextField(
|
||||
decoration: InputDecoration(
|
||||
hintText: locService.t('profileEditor.valueHint'),
|
||||
border: const OutlineInputBorder(),
|
||||
isDense: true,
|
||||
),
|
||||
controller: valueController,
|
||||
child: NSITagValueField(
|
||||
key: ValueKey('${_tags[i].key}_$i'), // Rebuild when key changes
|
||||
tagKey: _tags[i].key,
|
||||
initialValue: _tags[i].value,
|
||||
hintText: locService.t('profileEditor.valueHint'),
|
||||
readOnly: !widget.profile.editable,
|
||||
onChanged: !widget.profile.editable
|
||||
? null
|
||||
: (v) => _tags[i] = MapEntry(_tags[i].key, v),
|
||||
? (v) {} // No-op when read-only
|
||||
: (v) => setState(() => _tags[i] = MapEntry(_tags[i].key, v)),
|
||||
),
|
||||
),
|
||||
if (widget.profile.editable)
|
||||
@@ -231,8 +230,8 @@ class _ProfileEditorState extends State<ProfileEditor> {
|
||||
|
||||
final tagMap = <String, String>{};
|
||||
for (final e in _tags) {
|
||||
if (e.key.trim().isEmpty || e.value.trim().isEmpty) continue;
|
||||
tagMap[e.key.trim()] = e.value.trim();
|
||||
if (e.key.trim().isEmpty) continue; // Skip only if key is empty
|
||||
tagMap[e.key.trim()] = e.value.trim(); // Allow empty values for refinement
|
||||
}
|
||||
|
||||
if (tagMap.isEmpty) {
|
||||
|
||||
@@ -3,9 +3,33 @@ import 'package:provider/provider.dart';
|
||||
import '../../../app_state.dart';
|
||||
import '../../../services/localization_service.dart';
|
||||
|
||||
class SuspectedLocationsSection extends StatelessWidget {
|
||||
class SuspectedLocationsSection extends StatefulWidget {
|
||||
const SuspectedLocationsSection({super.key});
|
||||
|
||||
@override
|
||||
State<SuspectedLocationsSection> createState() => _SuspectedLocationsSectionState();
|
||||
}
|
||||
|
||||
class _SuspectedLocationsSectionState extends State<SuspectedLocationsSection> {
|
||||
DateTime? _lastFetch;
|
||||
bool _wasLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadLastFetch();
|
||||
}
|
||||
|
||||
void _loadLastFetch() async {
|
||||
final appState = context.read<AppState>();
|
||||
final lastFetch = await appState.suspectedLocationsLastFetch;
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_lastFetch = lastFetch;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AnimatedBuilder(
|
||||
@@ -15,14 +39,31 @@ class SuspectedLocationsSection extends StatelessWidget {
|
||||
final appState = context.watch<AppState>();
|
||||
final isEnabled = appState.suspectedLocationsEnabled;
|
||||
final isLoading = appState.suspectedLocationsLoading;
|
||||
final lastFetch = appState.suspectedLocationsLastFetch;
|
||||
final downloadProgress = appState.suspectedLocationsDownloadProgress;
|
||||
|
||||
// Check if loading just finished and reload last fetch time
|
||||
if (_wasLoading && !isLoading) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_loadLastFetch();
|
||||
});
|
||||
}
|
||||
_wasLoading = isLoading;
|
||||
|
||||
String getLastFetchText() {
|
||||
if (lastFetch == null) {
|
||||
// Show status during loading
|
||||
if (isLoading) {
|
||||
if (downloadProgress != null) {
|
||||
return 'Downloading data... (this may take a few minutes)';
|
||||
} else {
|
||||
return 'Processing data...';
|
||||
}
|
||||
}
|
||||
|
||||
if (_lastFetch == null) {
|
||||
return locService.t('suspectedLocations.neverFetched');
|
||||
} else {
|
||||
final now = DateTime.now();
|
||||
final diff = now.difference(lastFetch);
|
||||
final diff = now.difference(_lastFetch!);
|
||||
if (diff.inDays > 0) {
|
||||
return locService.t('suspectedLocations.daysAgo', params: [diff.inDays.toString()]);
|
||||
} else if (diff.inHours > 0) {
|
||||
@@ -42,6 +83,11 @@ class SuspectedLocationsSection extends StatelessWidget {
|
||||
// The loading state will be managed by suspected location state
|
||||
final success = await appState.refreshSuspectedLocations();
|
||||
|
||||
// Refresh the last fetch time after successful refresh
|
||||
if (success) {
|
||||
_loadLastFetch();
|
||||
}
|
||||
|
||||
// Show result snackbar
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -85,10 +131,31 @@ class SuspectedLocationsSection extends StatelessWidget {
|
||||
title: Text(locService.t('suspectedLocations.lastUpdated')),
|
||||
subtitle: Text(getLastFetchText()),
|
||||
trailing: isLoading
|
||||
? const SizedBox(
|
||||
width: 24,
|
||||
? SizedBox(
|
||||
width: 80,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
child: downloadProgress != null
|
||||
? Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
LinearProgressIndicator(
|
||||
value: downloadProgress,
|
||||
backgroundColor: Colors.grey[300],
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'${(downloadProgress * 100).toInt()}%',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
)
|
||||
: const Center(
|
||||
child: SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
)
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'version_service.dart';
|
||||
import '../app_state.dart';
|
||||
import '../migrations.dart';
|
||||
|
||||
/// Service for managing changelog data and first launch detection
|
||||
class ChangelogService {
|
||||
@@ -14,6 +16,7 @@ class ChangelogService {
|
||||
static const String _lastSeenVersionKey = 'last_seen_version';
|
||||
static const String _hasSeenWelcomeKey = 'has_seen_welcome';
|
||||
static const String _hasSeenSubmissionGuideKey = 'has_seen_submission_guide';
|
||||
static const String _hasCompletedPositioningTutorialKey = 'has_completed_positioning_tutorial';
|
||||
|
||||
Map<String, dynamic>? _changelogData;
|
||||
bool _initialized = false;
|
||||
@@ -80,6 +83,18 @@ class ChangelogService {
|
||||
await prefs.setBool(_hasSeenSubmissionGuideKey, true);
|
||||
}
|
||||
|
||||
/// Check if user has completed the positioning tutorial
|
||||
Future<bool> hasCompletedPositioningTutorial() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getBool(_hasCompletedPositioningTutorialKey) ?? false;
|
||||
}
|
||||
|
||||
/// Mark that user has completed the positioning tutorial
|
||||
Future<void> markPositioningTutorialCompleted() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_hasCompletedPositioningTutorialKey, true);
|
||||
}
|
||||
|
||||
/// Check if app version has changed since last launch
|
||||
Future<bool> hasVersionChanged() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
@@ -207,6 +222,10 @@ class ChangelogService {
|
||||
versionsNeedingMigration.add('1.5.3');
|
||||
}
|
||||
|
||||
if (needsMigration(lastSeenVersion, currentVersion, '1.6.3')) {
|
||||
versionsNeedingMigration.add('1.6.3');
|
||||
}
|
||||
|
||||
// Future versions can be added here
|
||||
// if (needsMigration(lastSeenVersion, currentVersion, '2.0.0')) {
|
||||
// versionsNeedingMigration.add('2.0.0');
|
||||
@@ -262,31 +281,9 @@ class ChangelogService {
|
||||
bool get isInitialized => _initialized;
|
||||
|
||||
/// Run a specific migration by version number
|
||||
Future<void> runMigration(String version, AppState appState) async {
|
||||
Future<void> runMigration(String version, AppState appState, BuildContext? context) async {
|
||||
debugPrint('[ChangelogService] Running $version migration');
|
||||
|
||||
switch (version) {
|
||||
case '1.3.1':
|
||||
// Enable network status indicator for all existing users
|
||||
await appState.setNetworkStatusIndicatorEnabled(true);
|
||||
debugPrint('[ChangelogService] 1.3.1 migration completed: enabled network status indicator');
|
||||
break;
|
||||
|
||||
case '1.5.3':
|
||||
// Migrate upload queue to new two-stage changeset system
|
||||
await appState.migrateUploadQueueToTwoStageSystem();
|
||||
debugPrint('[ChangelogService] 1.5.3 migration completed: migrated upload queue to two-stage system');
|
||||
break;
|
||||
|
||||
// Future migrations can be added here
|
||||
// case '2.0.0':
|
||||
// await appState.doSomethingNew();
|
||||
// debugPrint('[ChangelogService] 2.0.0 migration completed');
|
||||
// break;
|
||||
|
||||
default:
|
||||
debugPrint('[ChangelogService] Unknown migration version: $version');
|
||||
}
|
||||
await OneTimeMigrations.runMigration(version, appState, context);
|
||||
}
|
||||
|
||||
/// Check if a migration should run
|
||||
|
||||
@@ -202,7 +202,11 @@ bool _nodeMatchesProfiles(Map<String, String> nodeTags, List<NodeProfile> profil
|
||||
/// Check if a node's tags match a specific profile
|
||||
bool _nodeMatchesProfile(Map<String, String> nodeTags, NodeProfile profile) {
|
||||
// All profile tags must be present in the node for it to match
|
||||
// Skip empty values as they are for refinement purposes only
|
||||
for (final entry in profile.tags.entries) {
|
||||
if (entry.value.trim().isEmpty) {
|
||||
continue; // Skip empty values - they don't need to match anything
|
||||
}
|
||||
if (nodeTags[entry.key] != entry.value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -78,6 +78,11 @@ Future<List<OsmNode>> _fetchOverpassNodesWithSplitting({
|
||||
// Rate limits should NOT be split - just fail with extended backoff
|
||||
debugPrint('[fetchOverpassNodes] Rate limited - using extended backoff, not splitting');
|
||||
|
||||
// Report slow progress when backing off
|
||||
if (reportStatus) {
|
||||
NetworkStatus.instance.reportSlowProgress();
|
||||
}
|
||||
|
||||
// Wait longer for rate limits before giving up entirely
|
||||
await Future.delayed(const Duration(seconds: 30));
|
||||
return []; // Return empty rather than rethrowing - let caller handle error reporting
|
||||
@@ -88,6 +93,11 @@ Future<List<OsmNode>> _fetchOverpassNodesWithSplitting({
|
||||
return []; // Return empty - let caller handle error reporting
|
||||
}
|
||||
|
||||
// Report slow progress when we start splitting (only at the top level)
|
||||
if (reportStatus) {
|
||||
NetworkStatus.instance.reportSlowProgress();
|
||||
}
|
||||
|
||||
// Split the bounds into 4 quadrants and try each separately
|
||||
debugPrint('[fetchOverpassNodes] Splitting area into quadrants (depth: $splitDepth)');
|
||||
final quadrants = _splitBounds(bounds);
|
||||
@@ -195,10 +205,21 @@ Future<List<OsmNode>> _fetchSingleOverpassQuery({
|
||||
/// Builds an Overpass API query for surveillance nodes matching the given profiles within bounds.
|
||||
/// Also fetches ways and relations that reference these nodes to determine constraint status.
|
||||
String _buildOverpassQuery(LatLngBounds bounds, List<NodeProfile> profiles, int maxResults) {
|
||||
// Build node clauses for each profile
|
||||
final nodeClauses = profiles.map((profile) {
|
||||
// Convert profile tags to Overpass filter format
|
||||
// Deduplicate profiles to reduce query complexity - broader profiles subsume more specific ones
|
||||
final deduplicatedProfiles = _deduplicateProfilesForQuery(profiles);
|
||||
|
||||
// Safety check: if deduplication removed all profiles (edge case), fall back to original list
|
||||
final profilesToQuery = deduplicatedProfiles.isNotEmpty ? deduplicatedProfiles : profiles;
|
||||
|
||||
if (deduplicatedProfiles.length < profiles.length) {
|
||||
debugPrint('[Overpass] Deduplicated ${profiles.length} profiles to ${deduplicatedProfiles.length} for query efficiency');
|
||||
}
|
||||
|
||||
// Build node clauses for deduplicated profiles only
|
||||
final nodeClauses = profilesToQuery.map((profile) {
|
||||
// Convert profile tags to Overpass filter format, excluding empty values
|
||||
final tagFilters = profile.tags.entries
|
||||
.where((entry) => entry.value.trim().isNotEmpty) // Skip empty values
|
||||
.map((entry) => '["${entry.key}"="${entry.value}"]')
|
||||
.join();
|
||||
|
||||
@@ -207,7 +228,7 @@ String _buildOverpassQuery(LatLngBounds bounds, List<NodeProfile> profiles, int
|
||||
}).join('\n ');
|
||||
|
||||
return '''
|
||||
[out:json][timeout:25];
|
||||
[out:json][timeout:${kOverpassQueryTimeout.inSeconds}];
|
||||
(
|
||||
$nodeClauses
|
||||
);
|
||||
@@ -220,6 +241,68 @@ out meta;
|
||||
''';
|
||||
}
|
||||
|
||||
/// Deduplicate profiles for Overpass queries by removing profiles that are subsumed by others.
|
||||
/// A profile A subsumes profile B if all of A's non-empty tags exist in B with identical values.
|
||||
/// This optimization reduces query complexity while returning the same nodes (since broader
|
||||
/// profiles capture all nodes that more specific profiles would).
|
||||
List<NodeProfile> _deduplicateProfilesForQuery(List<NodeProfile> profiles) {
|
||||
if (profiles.length <= 1) return profiles;
|
||||
|
||||
final result = <NodeProfile>[];
|
||||
|
||||
for (final candidate in profiles) {
|
||||
// Skip profiles that only have empty tags - they would match everything and break queries
|
||||
final candidateNonEmptyTags = candidate.tags.entries
|
||||
.where((entry) => entry.value.trim().isNotEmpty)
|
||||
.toList();
|
||||
|
||||
if (candidateNonEmptyTags.isEmpty) continue;
|
||||
|
||||
// Check if any existing profile in our result subsumes this candidate
|
||||
bool isSubsumed = false;
|
||||
for (final existing in result) {
|
||||
if (_profileSubsumes(existing, candidate)) {
|
||||
isSubsumed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isSubsumed) {
|
||||
// This candidate is not subsumed, so add it
|
||||
// But first, remove any existing profiles that this candidate subsumes
|
||||
result.removeWhere((existing) => _profileSubsumes(candidate, existing));
|
||||
result.add(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Check if broaderProfile subsumes specificProfile.
|
||||
/// Returns true if all non-empty tags in broaderProfile exist in specificProfile with identical values.
|
||||
bool _profileSubsumes(NodeProfile broaderProfile, NodeProfile specificProfile) {
|
||||
// Get non-empty tags from both profiles
|
||||
final broaderTags = Map.fromEntries(
|
||||
broaderProfile.tags.entries.where((entry) => entry.value.trim().isNotEmpty)
|
||||
);
|
||||
final specificTags = Map.fromEntries(
|
||||
specificProfile.tags.entries.where((entry) => entry.value.trim().isNotEmpty)
|
||||
);
|
||||
|
||||
// If broader has no non-empty tags, it doesn't subsume anything (would match everything)
|
||||
if (broaderTags.isEmpty) return false;
|
||||
|
||||
// If broader has more non-empty tags than specific, it can't subsume
|
||||
if (broaderTags.length > specificTags.length) return false;
|
||||
|
||||
// Check if all broader tags exist in specific with same values
|
||||
for (final entry in broaderTags.entries) {
|
||||
if (specificTags[entry.key] != entry.value) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Split a LatLngBounds into 4 quadrants (NW, NE, SW, SE).
|
||||
List<LatLngBounds> _splitBounds(LatLngBounds bounds) {
|
||||
final centerLat = (bounds.north + bounds.south) / 2;
|
||||
|
||||
@@ -19,7 +19,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
bool _hasSuccess = false;
|
||||
int _recentOfflineMisses = 0;
|
||||
Timer? _overpassRecoveryTimer;
|
||||
Timer? _waitingTimer;
|
||||
Timer? _noDataResetTimer;
|
||||
Timer? _successResetTimer;
|
||||
// Getters
|
||||
@@ -72,7 +71,25 @@ class NetworkStatus extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set waiting status (show when loading tiles/cameras)
|
||||
/// Report that requests are taking longer than usual (splitting, backoffs, etc.)
|
||||
void reportSlowProgress() {
|
||||
if (!_overpassHasIssues) {
|
||||
_overpassHasIssues = true;
|
||||
_isWaitingForData = false; // Transition from waiting to slow progress
|
||||
notifyListeners();
|
||||
debugPrint('[NetworkStatus] Surveillance data requests taking longer than usual');
|
||||
}
|
||||
|
||||
// Reset recovery timer - we'll clear this when the operation actually completes
|
||||
_overpassRecoveryTimer?.cancel();
|
||||
_overpassRecoveryTimer = Timer(const Duration(minutes: 2), () {
|
||||
_overpassHasIssues = false;
|
||||
notifyListeners();
|
||||
debugPrint('[NetworkStatus] Slow progress status cleared');
|
||||
});
|
||||
}
|
||||
|
||||
/// Set waiting status (show when loading surveillance data)
|
||||
void setWaiting() {
|
||||
// Clear any previous timeout/no-data state when starting new wait
|
||||
_isTimedOut = false;
|
||||
@@ -83,17 +100,7 @@ class NetworkStatus extends ChangeNotifier {
|
||||
if (!_isWaitingForData) {
|
||||
_isWaitingForData = true;
|
||||
notifyListeners();
|
||||
// Don't log routine waiting - only log if we stay waiting too long
|
||||
}
|
||||
|
||||
// Set timeout for genuine network issues (not 404s)
|
||||
_waitingTimer?.cancel();
|
||||
_waitingTimer = Timer(const Duration(seconds: 8), () {
|
||||
_isWaitingForData = false;
|
||||
_isTimedOut = true;
|
||||
debugPrint('[NetworkStatus] Request timed out - likely network issues');
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
/// Show success status briefly when data loads
|
||||
@@ -103,7 +110,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
_hasNoData = false;
|
||||
_hasSuccess = true;
|
||||
_recentOfflineMisses = 0;
|
||||
_waitingTimer?.cancel();
|
||||
_noDataResetTimer?.cancel();
|
||||
notifyListeners();
|
||||
|
||||
@@ -123,7 +129,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
_isTimedOut = false;
|
||||
_hasSuccess = false;
|
||||
_hasNoData = true;
|
||||
_waitingTimer?.cancel();
|
||||
_successResetTimer?.cancel();
|
||||
notifyListeners();
|
||||
|
||||
@@ -145,7 +150,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
_hasNoData = false;
|
||||
_hasSuccess = false;
|
||||
_recentOfflineMisses = 0;
|
||||
_waitingTimer?.cancel();
|
||||
_noDataResetTimer?.cancel();
|
||||
_successResetTimer?.cancel();
|
||||
notifyListeners();
|
||||
@@ -158,7 +162,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
_isTimedOut = true;
|
||||
_hasNoData = false;
|
||||
_hasSuccess = false;
|
||||
_waitingTimer?.cancel();
|
||||
_noDataResetTimer?.cancel();
|
||||
_successResetTimer?.cancel();
|
||||
notifyListeners();
|
||||
@@ -179,7 +182,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
_isTimedOut = false;
|
||||
_hasNoData = false;
|
||||
_hasSuccess = false;
|
||||
_waitingTimer?.cancel();
|
||||
_noDataResetTimer?.cancel();
|
||||
_successResetTimer?.cancel();
|
||||
|
||||
@@ -200,7 +202,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
_isWaitingForData = false;
|
||||
_isTimedOut = false;
|
||||
_hasNoData = true;
|
||||
_waitingTimer?.cancel();
|
||||
notifyListeners();
|
||||
debugPrint('[NetworkStatus] No offline data available for this area');
|
||||
}
|
||||
@@ -217,7 +218,6 @@ class NetworkStatus extends ChangeNotifier {
|
||||
@override
|
||||
void dispose() {
|
||||
_overpassRecoveryTimer?.cancel();
|
||||
_waitingTimer?.cancel();
|
||||
_noDataResetTimer?.cancel();
|
||||
_successResetTimer?.cancel();
|
||||
super.dispose();
|
||||
|
||||
139
lib/services/nsi_service.dart
Normal file
139
lib/services/nsi_service.dart
Normal file
@@ -0,0 +1,139 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../app_state.dart';
|
||||
import '../dev_config.dart';
|
||||
|
||||
/// Service for fetching tag value suggestions from OpenStreetMap Name Suggestion Index
|
||||
class NSIService {
|
||||
static final NSIService _instance = NSIService._();
|
||||
factory NSIService() => _instance;
|
||||
NSIService._();
|
||||
|
||||
static const String _userAgent = 'DeFlock/2.1.0 (OSM surveillance mapping app)';
|
||||
static const Duration _timeout = Duration(seconds: 10);
|
||||
|
||||
// Cache to avoid repeated API calls
|
||||
final Map<String, List<String>> _suggestionCache = {};
|
||||
|
||||
/// Get suggested values for a given OSM tag key
|
||||
/// Returns a list of the most commonly used values, or empty list if none found
|
||||
Future<List<String>> getSuggestionsForTag(String tagKey) async {
|
||||
if (tagKey.trim().isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final cacheKey = tagKey.trim().toLowerCase();
|
||||
|
||||
// Return cached results if available
|
||||
if (_suggestionCache.containsKey(cacheKey)) {
|
||||
return _suggestionCache[cacheKey]!;
|
||||
}
|
||||
|
||||
try {
|
||||
final suggestions = await _fetchSuggestionsForTag(tagKey);
|
||||
_suggestionCache[cacheKey] = suggestions;
|
||||
return suggestions;
|
||||
} catch (e) {
|
||||
debugPrint('[NSIService] Failed to fetch suggestions for $tagKey: $e');
|
||||
// Cache empty result to avoid repeated failures
|
||||
_suggestionCache[cacheKey] = [];
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch tag value suggestions from TagInfo API
|
||||
Future<List<String>> _fetchSuggestionsForTag(String tagKey) async {
|
||||
final uri = Uri.parse('https://taginfo.openstreetmap.org/api/4/key/values')
|
||||
.replace(queryParameters: {
|
||||
'key': tagKey,
|
||||
'format': 'json',
|
||||
'sortname': 'count',
|
||||
'sortorder': 'desc',
|
||||
'page': '1',
|
||||
'rp': '15', // Get top 15 most commonly used values
|
||||
});
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {'User-Agent': _userAgent},
|
||||
).timeout(_timeout);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('TagInfo API returned status ${response.statusCode}');
|
||||
}
|
||||
|
||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final values = data['data'] as List<dynamic>? ?? [];
|
||||
|
||||
// Extract the most commonly used values that meet our minimum hit threshold
|
||||
final suggestions = <String>[];
|
||||
|
||||
for (final item in values) {
|
||||
if (item is Map<String, dynamic>) {
|
||||
final value = item['value'] as String?;
|
||||
final count = item['count'] as int? ?? 0;
|
||||
|
||||
// Only include suggestions that meet our minimum hit count threshold
|
||||
if (value != null &&
|
||||
value.trim().isNotEmpty &&
|
||||
count >= kNSIMinimumHitCount &&
|
||||
_isValidSuggestion(value)) {
|
||||
suggestions.add(value.trim());
|
||||
}
|
||||
}
|
||||
|
||||
// Limit to top 10 suggestions for UI performance
|
||||
if (suggestions.length >= 10) break;
|
||||
}
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
/// Filter out common unwanted values that appear in TagInfo but aren't useful suggestions
|
||||
bool _isValidSuggestion(String value) {
|
||||
final lowercaseValue = value.toLowerCase();
|
||||
|
||||
// Filter out obvious non-useful values
|
||||
final unwanted = {
|
||||
'yes', 'no', 'unknown', '?', 'null', 'none', 'n/a', 'na',
|
||||
'todo', 'fixme', 'check', 'verify', 'test', 'temp', 'temporary'
|
||||
};
|
||||
|
||||
if (unwanted.contains(lowercaseValue)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Filter out very short generic values (except single letters that might be valid)
|
||||
if (value.length == 1 && !RegExp(r'[A-Z]').hasMatch(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Get suggestions for a tag key - returns empty list when offline mode enabled
|
||||
Future<List<String>> getAllSuggestions(String tagKey) async {
|
||||
// Check if app is in offline mode
|
||||
if (AppState.instance.offlineMode) {
|
||||
debugPrint('[NSIService] Offline mode enabled - no suggestions available for $tagKey');
|
||||
return []; // No suggestions when in offline mode - user must input manually
|
||||
}
|
||||
|
||||
// Online mode: try to get suggestions from API
|
||||
try {
|
||||
return await getSuggestionsForTag(tagKey);
|
||||
} catch (e) {
|
||||
debugPrint('[NSIService] API call failed: $e');
|
||||
return []; // No fallback - just return empty list
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear the suggestion cache (useful for testing or memory management)
|
||||
void clearCache() {
|
||||
_suggestionCache.clear();
|
||||
}
|
||||
}
|
||||
160
lib/services/nuclear_reset_service.dart
Normal file
160
lib/services/nuclear_reset_service.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'version_service.dart';
|
||||
|
||||
/// Nuclear reset service - clears ALL app data when migrations fail.
|
||||
/// This is the "big hammer" approach for when something goes seriously wrong.
|
||||
class NuclearResetService {
|
||||
static final NuclearResetService _instance = NuclearResetService._();
|
||||
factory NuclearResetService() => _instance;
|
||||
NuclearResetService._();
|
||||
|
||||
/// Completely clear all app data - SharedPreferences, files, caches, everything.
|
||||
/// After this, the app should behave exactly like a fresh install.
|
||||
static Future<void> clearEverything() async {
|
||||
try {
|
||||
debugPrint('[NuclearReset] Starting complete app data wipe...');
|
||||
|
||||
// Clear ALL SharedPreferences
|
||||
await _clearSharedPreferences();
|
||||
|
||||
// Clear ALL files in app directories
|
||||
await _clearFileSystem();
|
||||
|
||||
debugPrint('[NuclearReset] Complete app data wipe finished');
|
||||
} catch (e) {
|
||||
// Even the nuclear option can fail, but we can't do anything about it
|
||||
debugPrint('[NuclearReset] Error during nuclear reset: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all SharedPreferences data
|
||||
static Future<void> _clearSharedPreferences() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.clear();
|
||||
debugPrint('[NuclearReset] Cleared SharedPreferences');
|
||||
} catch (e) {
|
||||
debugPrint('[NuclearReset] Failed to clear SharedPreferences: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all files and directories in app storage
|
||||
static Future<void> _clearFileSystem() async {
|
||||
try {
|
||||
// Clear Documents directory (offline areas, etc.)
|
||||
await _clearDirectory(() => getApplicationDocumentsDirectory(), 'Documents');
|
||||
|
||||
// Clear Cache directory (tile cache, etc.)
|
||||
await _clearDirectory(() => getTemporaryDirectory(), 'Cache');
|
||||
|
||||
// Clear Support directory if it exists (iOS/macOS)
|
||||
if (!kIsWeb && (Platform.isIOS || Platform.isMacOS)) {
|
||||
await _clearDirectory(() => getApplicationSupportDirectory(), 'Support');
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
debugPrint('[NuclearReset] Failed to clear file system: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear a specific directory, with error handling
|
||||
static Future<void> _clearDirectory(
|
||||
Future<Directory> Function() getDirFunc,
|
||||
String dirName,
|
||||
) async {
|
||||
try {
|
||||
final dir = await getDirFunc();
|
||||
if (dir.existsSync()) {
|
||||
await dir.delete(recursive: true);
|
||||
debugPrint('[NuclearReset] Cleared $dirName directory');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[NuclearReset] Failed to clear $dirName directory: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate error report information (safely, with fallbacks)
|
||||
static Future<String> generateErrorReport(Object error, StackTrace? stackTrace) async {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
// Basic error information (always include this)
|
||||
buffer.writeln('MIGRATION FAILURE ERROR REPORT');
|
||||
buffer.writeln('Generated: ${DateTime.now().toIso8601String()}');
|
||||
buffer.writeln('');
|
||||
buffer.writeln('Error: $error');
|
||||
|
||||
if (stackTrace != null) {
|
||||
buffer.writeln('');
|
||||
buffer.writeln('Stack trace:');
|
||||
buffer.writeln(stackTrace.toString());
|
||||
}
|
||||
|
||||
// Try to add enrichment data, but don't fail if it doesn't work
|
||||
await _addEnrichmentData(buffer);
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/// Add device/app information to error report (with extensive error handling)
|
||||
static Future<void> _addEnrichmentData(StringBuffer buffer) async {
|
||||
try {
|
||||
buffer.writeln('');
|
||||
buffer.writeln('--- System Information ---');
|
||||
|
||||
// App version (should always work)
|
||||
try {
|
||||
buffer.writeln('App Version: ${VersionService().version}');
|
||||
} catch (e) {
|
||||
buffer.writeln('App Version: [Failed to get version: $e]');
|
||||
}
|
||||
|
||||
// Platform information
|
||||
try {
|
||||
if (!kIsWeb) {
|
||||
buffer.writeln('Platform: ${Platform.operatingSystem}');
|
||||
buffer.writeln('OS Version: ${Platform.operatingSystemVersion}');
|
||||
} else {
|
||||
buffer.writeln('Platform: Web');
|
||||
}
|
||||
} catch (e) {
|
||||
buffer.writeln('Platform: [Failed to get platform info: $e]');
|
||||
}
|
||||
|
||||
// Flutter/Dart information
|
||||
try {
|
||||
buffer.writeln('Flutter Mode: ${kDebugMode ? 'Debug' : kProfileMode ? 'Profile' : 'Release'}');
|
||||
} catch (e) {
|
||||
buffer.writeln('Flutter Mode: [Failed to get mode: $e]');
|
||||
}
|
||||
|
||||
// Previous version (if available)
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final lastVersion = prefs.getString('last_seen_version');
|
||||
buffer.writeln('Previous Version: ${lastVersion ?? 'Unknown (fresh install?)'}');
|
||||
} catch (e) {
|
||||
buffer.writeln('Previous Version: [Failed to get: $e]');
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
// If enrichment completely fails, just note it
|
||||
buffer.writeln('');
|
||||
buffer.writeln('--- System Information ---');
|
||||
buffer.writeln('[Failed to gather system information: $e]');
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy text to clipboard (safely)
|
||||
static Future<void> copyToClipboard(String text) async {
|
||||
try {
|
||||
await Clipboard.setData(ClipboardData(text: text));
|
||||
debugPrint('[NuclearReset] Copied error report to clipboard');
|
||||
} catch (e) {
|
||||
debugPrint('[NuclearReset] Failed to copy to clipboard: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,130 +1,109 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../models/suspected_location.dart';
|
||||
import 'suspected_location_service.dart';
|
||||
|
||||
/// Lightweight entry with pre-calculated centroid for efficient bounds checking
|
||||
class SuspectedLocationEntry {
|
||||
final Map<String, dynamic> rawData;
|
||||
final LatLng centroid;
|
||||
|
||||
SuspectedLocationEntry({required this.rawData, required this.centroid});
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'rawData': rawData,
|
||||
'centroid': [centroid.latitude, centroid.longitude],
|
||||
};
|
||||
|
||||
factory SuspectedLocationEntry.fromJson(Map<String, dynamic> json) {
|
||||
final centroidList = json['centroid'] as List;
|
||||
return SuspectedLocationEntry(
|
||||
rawData: Map<String, dynamic>.from(json['rawData']),
|
||||
centroid: LatLng(
|
||||
(centroidList[0] as num).toDouble(),
|
||||
(centroidList[1] as num).toDouble(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
import 'suspected_location_database.dart';
|
||||
|
||||
class SuspectedLocationCache extends ChangeNotifier {
|
||||
static final SuspectedLocationCache _instance = SuspectedLocationCache._();
|
||||
factory SuspectedLocationCache() => _instance;
|
||||
SuspectedLocationCache._();
|
||||
|
||||
static const String _prefsKeyProcessedData = 'suspected_locations_processed_data';
|
||||
static const String _prefsKeyLastFetch = 'suspected_locations_last_fetch';
|
||||
final SuspectedLocationDatabase _database = SuspectedLocationDatabase();
|
||||
|
||||
List<SuspectedLocationEntry> _processedEntries = [];
|
||||
DateTime? _lastFetchTime;
|
||||
final Map<String, List<SuspectedLocation>> _boundsCache = {};
|
||||
// Simple cache: just hold the currently visible locations
|
||||
List<SuspectedLocation> _currentLocations = [];
|
||||
String? _currentBoundsKey;
|
||||
bool _isLoading = false;
|
||||
|
||||
/// Get suspected locations within specific bounds (cached)
|
||||
List<SuspectedLocation> getLocationsForBounds(LatLngBounds bounds) {
|
||||
/// Get suspected locations within specific bounds (async version)
|
||||
Future<List<SuspectedLocation>> getLocationsForBounds(LatLngBounds bounds) async {
|
||||
if (!SuspectedLocationService().isEnabled) {
|
||||
debugPrint('[SuspectedLocationCache] Service not enabled');
|
||||
return [];
|
||||
}
|
||||
|
||||
final boundsKey = '${bounds.north.toStringAsFixed(4)},${bounds.south.toStringAsFixed(4)},${bounds.east.toStringAsFixed(4)},${bounds.west.toStringAsFixed(4)}';
|
||||
final boundsKey = _getBoundsKey(bounds);
|
||||
|
||||
// debugPrint('[SuspectedLocationCache] Getting locations for bounds: $boundsKey, processed entries count: ${_processedEntries.length}');
|
||||
|
||||
// Check cache first
|
||||
if (_boundsCache.containsKey(boundsKey)) {
|
||||
debugPrint('[SuspectedLocationCache] Using cached result: ${_boundsCache[boundsKey]!.length} locations');
|
||||
return _boundsCache[boundsKey]!;
|
||||
// If this is the same bounds we're already showing, return current cache
|
||||
if (boundsKey == _currentBoundsKey) {
|
||||
return _currentLocations;
|
||||
}
|
||||
|
||||
// Filter processed entries for this bounds (very fast since centroids are pre-calculated)
|
||||
final locations = <SuspectedLocation>[];
|
||||
int inBoundsCount = 0;
|
||||
|
||||
for (final entry in _processedEntries) {
|
||||
// Quick bounds check using pre-calculated centroid
|
||||
final lat = entry.centroid.latitude;
|
||||
final lng = entry.centroid.longitude;
|
||||
try {
|
||||
// Query database for locations in bounds
|
||||
final locations = await _database.getLocationsInBounds(bounds);
|
||||
|
||||
if (lat <= bounds.north && lat >= bounds.south &&
|
||||
lng <= bounds.east && lng >= bounds.west) {
|
||||
try {
|
||||
// Only create SuspectedLocation object if it's in bounds
|
||||
final location = SuspectedLocation.fromCsvRow(entry.rawData);
|
||||
locations.add(location);
|
||||
inBoundsCount++;
|
||||
} catch (e) {
|
||||
// Skip invalid entries
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Update cache
|
||||
_currentLocations = locations;
|
||||
_currentBoundsKey = boundsKey;
|
||||
|
||||
return locations;
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationCache] Error querying database: $e');
|
||||
return [];
|
||||
}
|
||||
|
||||
// debugPrint('[SuspectedLocationCache] Checked ${_processedEntries.length} entries, $inBoundsCount in bounds, result: ${locations.length} locations');
|
||||
|
||||
// Cache the result
|
||||
_boundsCache[boundsKey] = locations;
|
||||
|
||||
// Limit cache size to prevent memory issues
|
||||
if (_boundsCache.length > 100) {
|
||||
final oldestKey = _boundsCache.keys.first;
|
||||
_boundsCache.remove(oldestKey);
|
||||
}
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
/// Load processed data from storage
|
||||
Future<void> loadFromStorage() async {
|
||||
/// Get suspected locations within specific bounds (synchronous version for UI)
|
||||
/// Returns current cache immediately, triggers async update if bounds changed
|
||||
List<SuspectedLocation> getLocationsForBoundsSync(LatLngBounds bounds) {
|
||||
if (!SuspectedLocationService().isEnabled) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final boundsKey = _getBoundsKey(bounds);
|
||||
|
||||
// If bounds haven't changed, return current cache immediately
|
||||
if (boundsKey == _currentBoundsKey) {
|
||||
return _currentLocations;
|
||||
}
|
||||
|
||||
// Bounds changed - trigger async update but keep showing current cache
|
||||
if (!_isLoading) {
|
||||
_isLoading = true;
|
||||
_updateCacheAsync(bounds, boundsKey);
|
||||
}
|
||||
|
||||
// Return current cache (keeps suspected locations visible during map movement)
|
||||
return _currentLocations;
|
||||
}
|
||||
|
||||
/// Simple async update - no complex caching, just swap when done
|
||||
void _updateCacheAsync(LatLngBounds bounds, String boundsKey) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final locations = await _database.getLocationsInBounds(bounds);
|
||||
|
||||
// Load last fetch time
|
||||
final lastFetchMs = prefs.getInt(_prefsKeyLastFetch);
|
||||
if (lastFetchMs != null) {
|
||||
_lastFetchTime = DateTime.fromMillisecondsSinceEpoch(lastFetchMs);
|
||||
}
|
||||
|
||||
// Load processed data
|
||||
final processedDataString = prefs.getString(_prefsKeyProcessedData);
|
||||
if (processedDataString != null) {
|
||||
final List<dynamic> processedDataList = jsonDecode(processedDataString);
|
||||
_processedEntries = processedDataList
|
||||
.map((json) => SuspectedLocationEntry.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
debugPrint('[SuspectedLocationCache] Loaded ${_processedEntries.length} processed entries from storage');
|
||||
// Only update if this is still the most recent request
|
||||
if (boundsKey == _getBoundsKey(bounds) || _currentBoundsKey == null) {
|
||||
_currentLocations = locations;
|
||||
_currentBoundsKey = boundsKey;
|
||||
notifyListeners(); // Trigger UI update
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationCache] Error loading from storage: $e');
|
||||
_processedEntries.clear();
|
||||
_lastFetchTime = null;
|
||||
debugPrint('[SuspectedLocationCache] Error updating cache: $e');
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Process raw CSV data and save to storage (calculates centroids once)
|
||||
/// Generate cache key for bounds
|
||||
String _getBoundsKey(LatLngBounds bounds) {
|
||||
return '${bounds.north.toStringAsFixed(4)},${bounds.south.toStringAsFixed(4)},${bounds.east.toStringAsFixed(4)},${bounds.west.toStringAsFixed(4)}';
|
||||
}
|
||||
|
||||
/// Initialize the cache (ensures database is ready)
|
||||
Future<void> loadFromStorage() async {
|
||||
try {
|
||||
await _database.init();
|
||||
debugPrint('[SuspectedLocationCache] Database initialized successfully');
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationCache] Error initializing database: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Process raw CSV data and save to database
|
||||
Future<void> processAndSave(
|
||||
List<Map<String, dynamic>> rawData,
|
||||
DateTime fetchTime,
|
||||
@@ -132,96 +111,39 @@ class SuspectedLocationCache extends ChangeNotifier {
|
||||
try {
|
||||
debugPrint('[SuspectedLocationCache] Processing ${rawData.length} raw entries...');
|
||||
|
||||
final processedEntries = <SuspectedLocationEntry>[];
|
||||
int validCount = 0;
|
||||
int errorCount = 0;
|
||||
int zeroCoordCount = 0;
|
||||
// Clear cache since data will change
|
||||
_currentLocations = [];
|
||||
_currentBoundsKey = null;
|
||||
_isLoading = false;
|
||||
|
||||
for (int i = 0; i < rawData.length; i++) {
|
||||
final rowData = rawData[i];
|
||||
|
||||
// Log progress every 1000 entries for debugging
|
||||
if (i % 1000 == 0) {
|
||||
debugPrint('[SuspectedLocationCache] Processed ${i + 1}/${rawData.length} entries...');
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a temporary SuspectedLocation to extract the centroid
|
||||
final tempLocation = SuspectedLocation.fromCsvRow(rowData);
|
||||
|
||||
// Only save if we have a valid centroid (not at 0,0)
|
||||
if (tempLocation.centroid.latitude != 0 || tempLocation.centroid.longitude != 0) {
|
||||
processedEntries.add(SuspectedLocationEntry(
|
||||
rawData: rowData,
|
||||
centroid: tempLocation.centroid,
|
||||
));
|
||||
validCount++;
|
||||
} else {
|
||||
zeroCoordCount++;
|
||||
}
|
||||
} catch (e) {
|
||||
errorCount++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Insert data into database in batch
|
||||
await _database.insertBatch(rawData, fetchTime);
|
||||
|
||||
debugPrint('[SuspectedLocationCache] Processing complete - Valid: $validCount, Zero coords: $zeroCoordCount, Errors: $errorCount');
|
||||
final totalCount = await _database.getTotalCount();
|
||||
debugPrint('[SuspectedLocationCache] Processed and saved $totalCount entries to database');
|
||||
|
||||
_processedEntries = processedEntries;
|
||||
_lastFetchTime = fetchTime;
|
||||
|
||||
// Clear bounds cache since data changed
|
||||
_boundsCache.clear();
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// Save processed data
|
||||
final processedDataString = jsonEncode(processedEntries.map((e) => e.toJson()).toList());
|
||||
await prefs.setString(_prefsKeyProcessedData, processedDataString);
|
||||
|
||||
// Save last fetch time
|
||||
await prefs.setInt(_prefsKeyLastFetch, fetchTime.millisecondsSinceEpoch);
|
||||
|
||||
// Log coordinate ranges for debugging
|
||||
if (processedEntries.isNotEmpty) {
|
||||
double minLat = processedEntries.first.centroid.latitude;
|
||||
double maxLat = minLat;
|
||||
double minLng = processedEntries.first.centroid.longitude;
|
||||
double maxLng = minLng;
|
||||
|
||||
for (final entry in processedEntries) {
|
||||
final lat = entry.centroid.latitude;
|
||||
final lng = entry.centroid.longitude;
|
||||
if (lat < minLat) minLat = lat;
|
||||
if (lat > maxLat) maxLat = lat;
|
||||
if (lng < minLng) minLng = lng;
|
||||
if (lng > maxLng) maxLng = lng;
|
||||
}
|
||||
|
||||
debugPrint('[SuspectedLocationCache] Coordinate ranges - Lat: $minLat to $maxLat, Lng: $minLng to $maxLng');
|
||||
}
|
||||
|
||||
debugPrint('[SuspectedLocationCache] Processed and saved $validCount valid entries (${processedEntries.length} total)');
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationCache] Error processing and saving: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all cached data
|
||||
void clear() {
|
||||
_processedEntries.clear();
|
||||
_boundsCache.clear();
|
||||
_lastFetchTime = null;
|
||||
Future<void> clear() async {
|
||||
_currentLocations = [];
|
||||
_currentBoundsKey = null;
|
||||
_isLoading = false;
|
||||
await _database.clearAllData();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Get last fetch time
|
||||
DateTime? get lastFetchTime => _lastFetchTime;
|
||||
Future<DateTime?> get lastFetchTime => _database.getLastFetchTime();
|
||||
|
||||
/// Get total count of processed entries
|
||||
int get totalCount => _processedEntries.length;
|
||||
Future<int> get totalCount => _database.getTotalCount();
|
||||
|
||||
/// Check if we have data
|
||||
bool get hasData => _processedEntries.isNotEmpty;
|
||||
Future<bool> get hasData => _database.hasData();
|
||||
}
|
||||
330
lib/services/suspected_location_database.dart
Normal file
330
lib/services/suspected_location_database.dart
Normal file
@@ -0,0 +1,330 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../models/suspected_location.dart';
|
||||
|
||||
/// Database service for suspected location data
|
||||
/// Replaces the SharedPreferences-based cache to handle large datasets efficiently
|
||||
class SuspectedLocationDatabase {
|
||||
static final SuspectedLocationDatabase _instance = SuspectedLocationDatabase._();
|
||||
factory SuspectedLocationDatabase() => _instance;
|
||||
SuspectedLocationDatabase._();
|
||||
|
||||
Database? _database;
|
||||
static const String _dbName = 'suspected_locations.db';
|
||||
static const int _dbVersion = 1;
|
||||
|
||||
// Table and column names
|
||||
static const String _tableName = 'suspected_locations';
|
||||
static const String _columnTicketNo = 'ticket_no';
|
||||
static const String _columnCentroidLat = 'centroid_lat';
|
||||
static const String _columnCentroidLng = 'centroid_lng';
|
||||
static const String _columnBounds = 'bounds';
|
||||
static const String _columnGeoJson = 'geo_json';
|
||||
static const String _columnAllFields = 'all_fields';
|
||||
|
||||
// Metadata table for tracking last fetch time
|
||||
static const String _metaTableName = 'metadata';
|
||||
static const String _metaColumnKey = 'key';
|
||||
static const String _metaColumnValue = 'value';
|
||||
static const String _lastFetchKey = 'last_fetch_time';
|
||||
|
||||
/// Initialize the database
|
||||
Future<void> init() async {
|
||||
if (_database != null) return;
|
||||
|
||||
try {
|
||||
final dbPath = await getDatabasesPath();
|
||||
final fullPath = path.join(dbPath, _dbName);
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] Initializing database at $fullPath');
|
||||
|
||||
_database = await openDatabase(
|
||||
fullPath,
|
||||
version: _dbVersion,
|
||||
onCreate: _createTables,
|
||||
onUpgrade: _upgradeTables,
|
||||
);
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] Database initialized successfully');
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationDatabase] Error initializing database: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Create database tables
|
||||
Future<void> _createTables(Database db, int version) async {
|
||||
debugPrint('[SuspectedLocationDatabase] Creating tables...');
|
||||
|
||||
// Main suspected locations table
|
||||
await db.execute('''
|
||||
CREATE TABLE $_tableName (
|
||||
$_columnTicketNo TEXT PRIMARY KEY,
|
||||
$_columnCentroidLat REAL NOT NULL,
|
||||
$_columnCentroidLng REAL NOT NULL,
|
||||
$_columnBounds TEXT,
|
||||
$_columnGeoJson TEXT,
|
||||
$_columnAllFields TEXT NOT NULL
|
||||
)
|
||||
''');
|
||||
|
||||
// Create spatial indexes for efficient bounds queries
|
||||
// Separate indexes for lat and lng for better query optimization
|
||||
await db.execute('''
|
||||
CREATE INDEX idx_lat ON $_tableName ($_columnCentroidLat)
|
||||
''');
|
||||
await db.execute('''
|
||||
CREATE INDEX idx_lng ON $_tableName ($_columnCentroidLng)
|
||||
''');
|
||||
// Composite index for combined lat/lng queries
|
||||
await db.execute('''
|
||||
CREATE INDEX idx_lat_lng ON $_tableName ($_columnCentroidLat, $_columnCentroidLng)
|
||||
''');
|
||||
|
||||
// Metadata table for tracking last fetch time and other info
|
||||
await db.execute('''
|
||||
CREATE TABLE $_metaTableName (
|
||||
$_metaColumnKey TEXT PRIMARY KEY,
|
||||
$_metaColumnValue TEXT NOT NULL
|
||||
)
|
||||
''');
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] Tables created successfully');
|
||||
}
|
||||
|
||||
/// Handle database upgrades
|
||||
Future<void> _upgradeTables(Database db, int oldVersion, int newVersion) async {
|
||||
debugPrint('[SuspectedLocationDatabase] Upgrading database from version $oldVersion to $newVersion');
|
||||
// Future migrations would go here
|
||||
}
|
||||
|
||||
/// Get database instance, initializing if needed
|
||||
Future<Database> get database async {
|
||||
if (_database == null) {
|
||||
await init();
|
||||
}
|
||||
return _database!;
|
||||
}
|
||||
|
||||
/// Clear all data and recreate tables
|
||||
Future<void> clearAllData() async {
|
||||
try {
|
||||
final db = await database;
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] Clearing all data...');
|
||||
|
||||
// Drop and recreate tables (simpler than DELETE for large datasets)
|
||||
// Indexes are automatically dropped with tables
|
||||
await db.execute('DROP TABLE IF EXISTS $_tableName');
|
||||
await db.execute('DROP TABLE IF EXISTS $_metaTableName');
|
||||
await _createTables(db, _dbVersion);
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] All data cleared successfully');
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationDatabase] Error clearing data: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert suspected locations in batch
|
||||
Future<void> insertBatch(List<Map<String, dynamic>> rawDataList, DateTime fetchTime) async {
|
||||
try {
|
||||
final db = await database;
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] Starting batch insert of ${rawDataList.length} entries...');
|
||||
|
||||
// Clear existing data first
|
||||
await clearAllData();
|
||||
|
||||
// Process entries in batches to avoid memory issues
|
||||
const batchSize = 1000;
|
||||
int totalInserted = 0;
|
||||
int validCount = 0;
|
||||
int errorCount = 0;
|
||||
|
||||
// Start transaction for better performance
|
||||
await db.transaction((txn) async {
|
||||
for (int i = 0; i < rawDataList.length; i += batchSize) {
|
||||
final batch = txn.batch();
|
||||
final endIndex = (i + batchSize < rawDataList.length) ? i + batchSize : rawDataList.length;
|
||||
final currentBatch = rawDataList.sublist(i, endIndex);
|
||||
|
||||
for (final rowData in currentBatch) {
|
||||
try {
|
||||
// Create temporary SuspectedLocation to extract centroid and bounds
|
||||
final tempLocation = SuspectedLocation.fromCsvRow(rowData);
|
||||
|
||||
// Skip entries with zero coordinates
|
||||
if (tempLocation.centroid.latitude == 0 && tempLocation.centroid.longitude == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Prepare data for database insertion
|
||||
final dbRow = {
|
||||
_columnTicketNo: tempLocation.ticketNo,
|
||||
_columnCentroidLat: tempLocation.centroid.latitude,
|
||||
_columnCentroidLng: tempLocation.centroid.longitude,
|
||||
_columnBounds: tempLocation.bounds.isNotEmpty
|
||||
? jsonEncode(tempLocation.bounds.map((p) => [p.latitude, p.longitude]).toList())
|
||||
: null,
|
||||
_columnGeoJson: tempLocation.geoJson != null ? jsonEncode(tempLocation.geoJson!) : null,
|
||||
_columnAllFields: jsonEncode(tempLocation.allFields),
|
||||
};
|
||||
|
||||
batch.insert(_tableName, dbRow, conflictAlgorithm: ConflictAlgorithm.replace);
|
||||
validCount++;
|
||||
|
||||
} catch (e) {
|
||||
errorCount++;
|
||||
// Skip invalid entries
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Commit this batch
|
||||
await batch.commit(noResult: true);
|
||||
totalInserted += currentBatch.length;
|
||||
|
||||
// Log progress every few batches
|
||||
if ((i ~/ batchSize) % 5 == 0) {
|
||||
debugPrint('[SuspectedLocationDatabase] Processed ${i + currentBatch.length}/${rawDataList.length} entries...');
|
||||
}
|
||||
}
|
||||
|
||||
// Insert metadata
|
||||
await txn.insert(
|
||||
_metaTableName,
|
||||
{
|
||||
_metaColumnKey: _lastFetchKey,
|
||||
_metaColumnValue: fetchTime.millisecondsSinceEpoch.toString(),
|
||||
},
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
});
|
||||
|
||||
debugPrint('[SuspectedLocationDatabase] Batch insert complete - Valid: $validCount, Errors: $errorCount');
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationDatabase] Error in batch insert: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get suspected locations within bounding box
|
||||
Future<List<SuspectedLocation>> getLocationsInBounds(LatLngBounds bounds) async {
|
||||
try {
|
||||
final db = await database;
|
||||
|
||||
// Query with spatial bounds (simple lat/lng box filtering)
|
||||
final result = await db.query(
|
||||
_tableName,
|
||||
where: '''
|
||||
$_columnCentroidLat <= ? AND $_columnCentroidLat >= ? AND
|
||||
$_columnCentroidLng <= ? AND $_columnCentroidLng >= ?
|
||||
''',
|
||||
whereArgs: [bounds.north, bounds.south, bounds.east, bounds.west],
|
||||
);
|
||||
|
||||
// Convert database rows to SuspectedLocation objects
|
||||
final locations = <SuspectedLocation>[];
|
||||
for (final row in result) {
|
||||
try {
|
||||
final allFields = Map<String, dynamic>.from(jsonDecode(row[_columnAllFields] as String));
|
||||
|
||||
// Reconstruct bounds if available
|
||||
List<LatLng> boundsList = [];
|
||||
final boundsJson = row[_columnBounds] as String?;
|
||||
if (boundsJson != null) {
|
||||
final boundsData = jsonDecode(boundsJson) as List;
|
||||
boundsList = boundsData.map((b) => LatLng(
|
||||
(b[0] as num).toDouble(),
|
||||
(b[1] as num).toDouble(),
|
||||
)).toList();
|
||||
}
|
||||
|
||||
// Reconstruct GeoJSON if available
|
||||
Map<String, dynamic>? geoJson;
|
||||
final geoJsonString = row[_columnGeoJson] as String?;
|
||||
if (geoJsonString != null) {
|
||||
geoJson = Map<String, dynamic>.from(jsonDecode(geoJsonString));
|
||||
}
|
||||
|
||||
final location = SuspectedLocation(
|
||||
ticketNo: row[_columnTicketNo] as String,
|
||||
centroid: LatLng(
|
||||
row[_columnCentroidLat] as double,
|
||||
row[_columnCentroidLng] as double,
|
||||
),
|
||||
bounds: boundsList,
|
||||
geoJson: geoJson,
|
||||
allFields: allFields,
|
||||
);
|
||||
|
||||
locations.add(location);
|
||||
} catch (e) {
|
||||
// Skip invalid database entries
|
||||
debugPrint('[SuspectedLocationDatabase] Error parsing row: $e');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return locations;
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationDatabase] Error querying bounds: $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Get last fetch time
|
||||
Future<DateTime?> getLastFetchTime() async {
|
||||
try {
|
||||
final db = await database;
|
||||
|
||||
final result = await db.query(
|
||||
_metaTableName,
|
||||
where: '$_metaColumnKey = ?',
|
||||
whereArgs: [_lastFetchKey],
|
||||
);
|
||||
|
||||
if (result.isNotEmpty) {
|
||||
final value = result.first[_metaColumnValue] as String;
|
||||
return DateTime.fromMillisecondsSinceEpoch(int.parse(value));
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationDatabase] Error getting last fetch time: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get total count of entries
|
||||
Future<int> getTotalCount() async {
|
||||
try {
|
||||
final db = await database;
|
||||
final result = await db.rawQuery('SELECT COUNT(*) as count FROM $_tableName');
|
||||
return Sqflite.firstIntValue(result) ?? 0;
|
||||
} catch (e) {
|
||||
debugPrint('[SuspectedLocationDatabase] Error getting total count: $e');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if database has data
|
||||
Future<bool> hasData() async {
|
||||
final count = await getTotalCount();
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
/// Close database connection
|
||||
Future<void> close() async {
|
||||
if (_database != null) {
|
||||
await _database!.close();
|
||||
_database = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,13 +18,13 @@ class SuspectedLocationService {
|
||||
|
||||
static const String _prefsKeyEnabled = 'suspected_locations_enabled';
|
||||
static const Duration _maxAge = Duration(days: 7);
|
||||
static const Duration _timeout = Duration(seconds: 30);
|
||||
static const Duration _timeout = Duration(minutes: 5); // Increased for large CSV files (100MB+)
|
||||
|
||||
final SuspectedLocationCache _cache = SuspectedLocationCache();
|
||||
bool _isEnabled = false;
|
||||
|
||||
/// Get last fetch time
|
||||
DateTime? get lastFetchTime => _cache.lastFetchTime;
|
||||
Future<DateTime?> get lastFetchTime => _cache.lastFetchTime;
|
||||
|
||||
/// Check if suspected locations are enabled
|
||||
bool get isEnabled => _isEnabled;
|
||||
@@ -37,11 +37,12 @@ class SuspectedLocationService {
|
||||
await _cache.loadFromStorage();
|
||||
|
||||
// Only auto-fetch if enabled, data is stale or missing, and we are not offline
|
||||
if (_isEnabled && _shouldRefresh() && !offlineMode) {
|
||||
if (_isEnabled && (await _shouldRefresh()) && !offlineMode) {
|
||||
debugPrint('[SuspectedLocationService] Auto-refreshing CSV data on startup (older than $_maxAge or missing)');
|
||||
await _fetchData();
|
||||
} else if (_isEnabled && _shouldRefresh() && offlineMode) {
|
||||
debugPrint('[SuspectedLocationService] Skipping auto-refresh due to offline mode - data is ${_cache.lastFetchTime != null ? 'outdated' : 'missing'}');
|
||||
} else if (_isEnabled && (await _shouldRefresh()) && offlineMode) {
|
||||
final lastFetch = await _cache.lastFetchTime;
|
||||
debugPrint('[SuspectedLocationService] Skipping auto-refresh due to offline mode - data is ${lastFetch != null ? 'outdated' : 'missing'}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,36 +54,37 @@ class SuspectedLocationService {
|
||||
|
||||
// If disabling, clear the cache
|
||||
if (!enabled) {
|
||||
_cache.clear();
|
||||
await _cache.clear();
|
||||
}
|
||||
// Note: If enabling and no data, the state layer will call fetchDataIfNeeded()
|
||||
}
|
||||
|
||||
/// Check if cache has any data
|
||||
bool get hasData => _cache.hasData;
|
||||
Future<bool> get hasData => _cache.hasData;
|
||||
|
||||
/// Get last fetch time
|
||||
DateTime? get lastFetch => _cache.lastFetchTime;
|
||||
Future<DateTime?> get lastFetch => _cache.lastFetchTime;
|
||||
|
||||
/// Fetch data if needed (for enabling suspected locations when no data exists)
|
||||
Future<bool> fetchDataIfNeeded() async {
|
||||
if (!_shouldRefresh()) {
|
||||
Future<bool> fetchDataIfNeeded({void Function(double)? onProgress}) async {
|
||||
if (!(await _shouldRefresh())) {
|
||||
debugPrint('[SuspectedLocationService] Data is fresh, skipping fetch');
|
||||
return true; // Already have fresh data
|
||||
}
|
||||
return await _fetchData();
|
||||
return await _fetchData(onProgress: onProgress);
|
||||
}
|
||||
|
||||
/// Force refresh the data (for manual refresh button)
|
||||
Future<bool> forceRefresh() async {
|
||||
return await _fetchData();
|
||||
Future<bool> forceRefresh({void Function(double)? onProgress}) async {
|
||||
return await _fetchData(onProgress: onProgress);
|
||||
}
|
||||
|
||||
/// Check if data should be refreshed
|
||||
bool _shouldRefresh() {
|
||||
if (!_cache.hasData) return true;
|
||||
if (_cache.lastFetchTime == null) return true;
|
||||
return DateTime.now().difference(_cache.lastFetchTime!) > _maxAge;
|
||||
Future<bool> _shouldRefresh() async {
|
||||
if (!(await _cache.hasData)) return true;
|
||||
final lastFetch = await _cache.lastFetchTime;
|
||||
if (lastFetch == null) return true;
|
||||
return DateTime.now().difference(lastFetch) > _maxAge;
|
||||
}
|
||||
|
||||
/// Load settings from shared preferences
|
||||
@@ -100,111 +102,175 @@ class SuspectedLocationService {
|
||||
}
|
||||
|
||||
/// Fetch data from the CSV URL
|
||||
Future<bool> _fetchData() async {
|
||||
try {
|
||||
debugPrint('[SuspectedLocationService] Fetching CSV data from $kSuspectedLocationsCsvUrl');
|
||||
|
||||
final response = await http.get(
|
||||
Uri.parse(kSuspectedLocationsCsvUrl),
|
||||
headers: {
|
||||
'User-Agent': 'DeFlock/1.0 (OSM surveillance mapping app)',
|
||||
},
|
||||
).timeout(_timeout);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
debugPrint('[SuspectedLocationService] HTTP error ${response.statusCode}');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse CSV with proper field separator and quote handling
|
||||
final csvData = await compute(_parseCSV, response.body);
|
||||
debugPrint('[SuspectedLocationService] Parsed ${csvData.length} rows from CSV');
|
||||
|
||||
if (csvData.isEmpty) {
|
||||
debugPrint('[SuspectedLocationService] Empty CSV data');
|
||||
return false;
|
||||
}
|
||||
|
||||
// First row should be headers
|
||||
final headers = csvData.first.map((h) => h.toString().toLowerCase()).toList();
|
||||
debugPrint('[SuspectedLocationService] Headers: $headers');
|
||||
final dataRows = csvData.skip(1);
|
||||
debugPrint('[SuspectedLocationService] Data rows count: ${dataRows.length}');
|
||||
|
||||
// Find required column indices - we only need ticket_no and location
|
||||
final ticketNoIndex = headers.indexOf('ticket_no');
|
||||
final locationIndex = headers.indexOf('location');
|
||||
|
||||
debugPrint('[SuspectedLocationService] Column indices - ticket_no: $ticketNoIndex, location: $locationIndex');
|
||||
|
||||
if (ticketNoIndex == -1 || locationIndex == -1) {
|
||||
debugPrint('[SuspectedLocationService] Required columns not found in CSV. Headers: $headers');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse rows and store all data dynamically
|
||||
final List<Map<String, dynamic>> rawDataList = [];
|
||||
int rowIndex = 0;
|
||||
int validRows = 0;
|
||||
for (final row in dataRows) {
|
||||
rowIndex++;
|
||||
try {
|
||||
final Map<String, dynamic> rowData = {};
|
||||
Future<bool> _fetchData({void Function(double)? onProgress}) async {
|
||||
const maxRetries = 3;
|
||||
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
debugPrint('[SuspectedLocationService] Fetching CSV data from $kSuspectedLocationsCsvUrl (attempt $attempt/$maxRetries)');
|
||||
if (attempt == 1) {
|
||||
debugPrint('[SuspectedLocationService] This may take up to ${_timeout.inMinutes} minutes for large datasets...');
|
||||
}
|
||||
|
||||
// Use streaming download for progress tracking
|
||||
final request = http.Request('GET', Uri.parse(kSuspectedLocationsCsvUrl));
|
||||
request.headers['User-Agent'] = 'DeFlock/1.0 (OSM surveillance mapping app)';
|
||||
|
||||
final client = http.Client();
|
||||
final streamedResponse = await client.send(request).timeout(_timeout);
|
||||
|
||||
if (streamedResponse.statusCode != 200) {
|
||||
debugPrint('[SuspectedLocationService] HTTP error ${streamedResponse.statusCode}');
|
||||
client.close();
|
||||
throw Exception('HTTP ${streamedResponse.statusCode}');
|
||||
}
|
||||
|
||||
final contentLength = streamedResponse.contentLength;
|
||||
debugPrint('[SuspectedLocationService] Starting download of ${contentLength != null ? '$contentLength bytes' : 'unknown size'}...');
|
||||
|
||||
// Download with progress tracking
|
||||
final chunks = <List<int>>[];
|
||||
int downloadedBytes = 0;
|
||||
|
||||
await for (final chunk in streamedResponse.stream) {
|
||||
chunks.add(chunk);
|
||||
downloadedBytes += chunk.length;
|
||||
|
||||
// Store all columns dynamically
|
||||
for (int i = 0; i < headers.length && i < row.length; i++) {
|
||||
final headerName = headers[i];
|
||||
final cellValue = row[i];
|
||||
if (cellValue != null) {
|
||||
rowData[headerName] = cellValue;
|
||||
// Report progress if we know the total size
|
||||
if (contentLength != null && onProgress != null) {
|
||||
try {
|
||||
final progress = downloadedBytes / contentLength;
|
||||
onProgress(progress.clamp(0.0, 1.0));
|
||||
} catch (e) {
|
||||
// Don't let progress callback errors break the download
|
||||
debugPrint('[SuspectedLocationService] Progress callback error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Basic validation - must have ticket_no and location
|
||||
if (rowData['ticket_no']?.toString().isNotEmpty == true &&
|
||||
rowData['location']?.toString().isNotEmpty == true) {
|
||||
rawDataList.add(rowData);
|
||||
validRows++;
|
||||
}
|
||||
|
||||
client.close();
|
||||
|
||||
// Combine chunks into single response body
|
||||
final bodyBytes = chunks.expand((chunk) => chunk).toList();
|
||||
final responseBody = String.fromCharCodes(bodyBytes);
|
||||
|
||||
debugPrint('[SuspectedLocationService] Downloaded $downloadedBytes bytes, parsing CSV...');
|
||||
|
||||
// Parse CSV with proper field separator and quote handling
|
||||
final csvData = await compute(_parseCSV, responseBody);
|
||||
debugPrint('[SuspectedLocationService] Parsed ${csvData.length} rows from CSV');
|
||||
|
||||
if (csvData.isEmpty) {
|
||||
debugPrint('[SuspectedLocationService] Empty CSV data');
|
||||
throw Exception('Empty CSV data');
|
||||
}
|
||||
|
||||
// First row should be headers
|
||||
final headers = csvData.first.map((h) => h.toString().toLowerCase()).toList();
|
||||
debugPrint('[SuspectedLocationService] Headers: $headers');
|
||||
final dataRows = csvData.skip(1);
|
||||
debugPrint('[SuspectedLocationService] Data rows count: ${dataRows.length}');
|
||||
|
||||
// Find required column indices - we only need ticket_no and location
|
||||
final ticketNoIndex = headers.indexOf('ticket_no');
|
||||
final locationIndex = headers.indexOf('location');
|
||||
|
||||
debugPrint('[SuspectedLocationService] Column indices - ticket_no: $ticketNoIndex, location: $locationIndex');
|
||||
|
||||
if (ticketNoIndex == -1 || locationIndex == -1) {
|
||||
debugPrint('[SuspectedLocationService] Required columns not found in CSV. Headers: $headers');
|
||||
throw Exception('Required columns not found in CSV');
|
||||
}
|
||||
|
||||
|
||||
// Parse rows and store all data dynamically
|
||||
final List<Map<String, dynamic>> rawDataList = [];
|
||||
int rowIndex = 0;
|
||||
int validRows = 0;
|
||||
for (final row in dataRows) {
|
||||
rowIndex++;
|
||||
try {
|
||||
final Map<String, dynamic> rowData = {};
|
||||
|
||||
// Store all columns dynamically
|
||||
for (int i = 0; i < headers.length && i < row.length; i++) {
|
||||
final headerName = headers[i];
|
||||
final cellValue = row[i];
|
||||
if (cellValue != null) {
|
||||
rowData[headerName] = cellValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Basic validation - must have ticket_no and location
|
||||
if (rowData['ticket_no']?.toString().isNotEmpty == true &&
|
||||
rowData['location']?.toString().isNotEmpty == true) {
|
||||
rawDataList.add(rowData);
|
||||
validRows++;
|
||||
}
|
||||
|
||||
} catch (e, stackTrace) {
|
||||
// Skip rows that can't be parsed
|
||||
debugPrint('[SuspectedLocationService] Error parsing row $rowIndex: $e');
|
||||
continue;
|
||||
}
|
||||
|
||||
} catch (e, stackTrace) {
|
||||
// Skip rows that can't be parsed
|
||||
debugPrint('[SuspectedLocationService] Error parsing row $rowIndex: $e');
|
||||
continue;
|
||||
}
|
||||
|
||||
debugPrint('[SuspectedLocationService] Parsed $validRows valid rows from ${dataRows.length} total rows');
|
||||
|
||||
final fetchTime = DateTime.now();
|
||||
|
||||
// Process raw data and save (calculates centroids once)
|
||||
await _cache.processAndSave(rawDataList, fetchTime);
|
||||
|
||||
debugPrint('[SuspectedLocationService] Successfully fetched and stored $validRows valid raw entries (${rawDataList.length} total)');
|
||||
return true;
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('[SuspectedLocationService] Attempt $attempt failed: $e');
|
||||
|
||||
if (attempt == maxRetries) {
|
||||
debugPrint('[SuspectedLocationService] All $maxRetries attempts failed');
|
||||
debugPrint('[SuspectedLocationService] Stack trace: $stackTrace');
|
||||
return false;
|
||||
} else {
|
||||
// Wait before retrying (exponential backoff)
|
||||
final delay = Duration(seconds: attempt * 10);
|
||||
debugPrint('[SuspectedLocationService] Retrying in ${delay.inSeconds} seconds...');
|
||||
await Future.delayed(delay);
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('[SuspectedLocationService] Parsed $validRows valid rows from ${dataRows.length} total rows');
|
||||
|
||||
final fetchTime = DateTime.now();
|
||||
|
||||
// Process raw data and save (calculates centroids once)
|
||||
await _cache.processAndSave(rawDataList, fetchTime);
|
||||
|
||||
debugPrint('[SuspectedLocationService] Successfully fetched and stored $validRows valid raw entries (${rawDataList.length} total)');
|
||||
return true;
|
||||
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('[SuspectedLocationService] Error fetching data: $e');
|
||||
debugPrint('[SuspectedLocationService] Stack trace: $stackTrace');
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Should never reach here
|
||||
}
|
||||
|
||||
/// Get suspected locations within a bounding box
|
||||
List<SuspectedLocation> getLocationsInBounds({
|
||||
/// Get suspected locations within a bounding box (async)
|
||||
Future<List<SuspectedLocation>> getLocationsInBounds({
|
||||
required double north,
|
||||
required double south,
|
||||
required double east,
|
||||
required double west,
|
||||
}) async {
|
||||
return await _cache.getLocationsForBounds(LatLngBounds(
|
||||
LatLng(north, west),
|
||||
LatLng(south, east),
|
||||
));
|
||||
}
|
||||
|
||||
/// Get suspected locations within a bounding box (sync, for UI)
|
||||
List<SuspectedLocation> getLocationsInBoundsSync({
|
||||
required double north,
|
||||
required double south,
|
||||
required double east,
|
||||
required double west,
|
||||
}) {
|
||||
return _cache.getLocationsForBounds(LatLngBounds(
|
||||
return _cache.getLocationsForBoundsSync(LatLngBounds(
|
||||
LatLng(north, west),
|
||||
LatLng(south, east),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Simple CSV parser for compute() - must be top-level function
|
||||
List<List<dynamic>> _parseCSV(String csvBody) {
|
||||
return const CsvToListConverter(
|
||||
|
||||
@@ -87,6 +87,24 @@ class NavigationState extends ChangeNotifier {
|
||||
return distance < kNavigationMinRouteDistance;
|
||||
}
|
||||
|
||||
/// Get distance from first navigation point to provisional location during second point selection
|
||||
double? get distanceFromFirstPoint {
|
||||
if (!_isSettingSecondPoint || _provisionalPinLocation == null) return null;
|
||||
|
||||
final firstPoint = _nextPointIsStart ? _routeEnd : _routeStart;
|
||||
if (firstPoint == null) return null;
|
||||
|
||||
return const Distance().as(LengthUnit.Meter, firstPoint, _provisionalPinLocation!);
|
||||
}
|
||||
|
||||
/// Check if distance between points would likely cause timeout issues
|
||||
bool get distanceExceedsWarningThreshold {
|
||||
final distance = distanceFromFirstPoint;
|
||||
if (distance == null) return false;
|
||||
|
||||
return distance > kNavigationDistanceWarningThreshold;
|
||||
}
|
||||
|
||||
/// BRUTALIST: Single entry point to search mode
|
||||
void enterSearchMode(LatLng mapCenter) {
|
||||
debugPrint('[NavigationState] enterSearchMode - current mode: $_mode');
|
||||
@@ -207,8 +225,15 @@ class NavigationState extends ChangeNotifier {
|
||||
_routeEndAddress = _provisionalPinAddress;
|
||||
}
|
||||
|
||||
// BRUTALIST FIX: Set calculating state BEFORE clearing isSettingSecondPoint
|
||||
// to prevent UI from briefly showing route buttons again
|
||||
_isSettingSecondPoint = false;
|
||||
_isCalculating = true;
|
||||
_routingError = null; // Clear any previous errors
|
||||
|
||||
// Notify listeners immediately to update UI before async calculation starts
|
||||
notifyListeners();
|
||||
|
||||
_calculateRoute();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,13 @@ class ProfileState extends ChangeNotifier {
|
||||
|
||||
final List<NodeProfile> _profiles = [];
|
||||
final Set<NodeProfile> _enabled = {};
|
||||
|
||||
// Callback for when a profile is deleted (used to clear stale sessions)
|
||||
void Function(NodeProfile)? _onProfileDeleted;
|
||||
|
||||
void setProfileDeletedCallback(void Function(NodeProfile) callback) {
|
||||
_onProfileDeleted = callback;
|
||||
}
|
||||
|
||||
// Getters
|
||||
List<NodeProfile> get profiles => List.unmodifiable(_profiles);
|
||||
@@ -78,6 +85,10 @@ class ProfileState extends ChangeNotifier {
|
||||
}
|
||||
_saveEnabledProfiles();
|
||||
ProfileService().save(_profiles);
|
||||
|
||||
// Notify about profile deletion so other parts can clean up
|
||||
_onProfileDeleted?.call(p);
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,14 +12,17 @@ class AddNodeSession {
|
||||
LatLng? target;
|
||||
List<double> directions; // All directions [90, 180, 270]
|
||||
int currentDirectionIndex; // Which direction we're editing (e.g. 1 = editing the 180°)
|
||||
Map<String, String> refinedTags; // User-selected values for empty profile tags
|
||||
|
||||
AddNodeSession({
|
||||
this.profile,
|
||||
double initialDirection = 0,
|
||||
this.operatorProfile,
|
||||
this.target,
|
||||
Map<String, String>? refinedTags,
|
||||
}) : directions = [initialDirection],
|
||||
currentDirectionIndex = 0;
|
||||
currentDirectionIndex = 0,
|
||||
refinedTags = refinedTags ?? {};
|
||||
|
||||
// Slider always shows the current direction being edited
|
||||
double get directionDegrees => directions[currentDirectionIndex];
|
||||
@@ -35,6 +38,7 @@ class EditNodeSession {
|
||||
List<double> directions; // All directions [90, 180, 270]
|
||||
int currentDirectionIndex; // Which direction we're editing (e.g. 1 = editing the 180°)
|
||||
bool extractFromWay; // True if user wants to extract this constrained node
|
||||
Map<String, String> refinedTags; // User-selected values for empty profile tags
|
||||
|
||||
EditNodeSession({
|
||||
required this.originalNode,
|
||||
@@ -42,8 +46,10 @@ class EditNodeSession {
|
||||
required double initialDirection,
|
||||
required this.target,
|
||||
this.extractFromWay = false,
|
||||
Map<String, String>? refinedTags,
|
||||
}) : directions = [initialDirection],
|
||||
currentDirectionIndex = 0;
|
||||
currentDirectionIndex = 0,
|
||||
refinedTags = refinedTags ?? {};
|
||||
|
||||
// Slider always shows the current direction being edited
|
||||
double get directionDegrees => directions[currentDirectionIndex];
|
||||
@@ -112,6 +118,7 @@ class SessionState extends ChangeNotifier {
|
||||
NodeProfile? profile,
|
||||
OperatorProfile? operatorProfile,
|
||||
LatLng? target,
|
||||
Map<String, String>? refinedTags,
|
||||
}) {
|
||||
if (_session == null) return;
|
||||
|
||||
@@ -132,6 +139,10 @@ class SessionState extends ChangeNotifier {
|
||||
_session!.target = target;
|
||||
dirty = true;
|
||||
}
|
||||
if (refinedTags != null) {
|
||||
_session!.refinedTags = Map<String, String>.from(refinedTags);
|
||||
dirty = true;
|
||||
}
|
||||
if (dirty) notifyListeners();
|
||||
}
|
||||
|
||||
@@ -141,6 +152,7 @@ class SessionState extends ChangeNotifier {
|
||||
OperatorProfile? operatorProfile,
|
||||
LatLng? target,
|
||||
bool? extractFromWay,
|
||||
Map<String, String>? refinedTags,
|
||||
}) {
|
||||
if (_editSession == null) return;
|
||||
|
||||
@@ -174,6 +186,10 @@ class SessionState extends ChangeNotifier {
|
||||
}
|
||||
dirty = true;
|
||||
}
|
||||
if (refinedTags != null) {
|
||||
_editSession!.refinedTags = Map<String, String>.from(refinedTags);
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
if (dirty) notifyListeners();
|
||||
|
||||
|
||||
@@ -8,18 +8,34 @@ class SuspectedLocationState extends ChangeNotifier {
|
||||
|
||||
SuspectedLocation? _selectedLocation;
|
||||
bool _isLoading = false;
|
||||
double? _downloadProgress; // 0.0 to 1.0, null when not downloading
|
||||
|
||||
/// Currently selected suspected location (for detail view)
|
||||
SuspectedLocation? get selectedLocation => _selectedLocation;
|
||||
|
||||
/// Get suspected locations in bounds (this should be called by the map view)
|
||||
List<SuspectedLocation> getLocationsInBounds({
|
||||
/// Get suspected locations in bounds (async)
|
||||
Future<List<SuspectedLocation>> getLocationsInBounds({
|
||||
required double north,
|
||||
required double south,
|
||||
required double east,
|
||||
required double west,
|
||||
}) async {
|
||||
return await _service.getLocationsInBounds(
|
||||
north: north,
|
||||
south: south,
|
||||
east: east,
|
||||
west: west,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get suspected locations in bounds (sync, for UI)
|
||||
List<SuspectedLocation> getLocationsInBoundsSync({
|
||||
required double north,
|
||||
required double south,
|
||||
required double east,
|
||||
required double west,
|
||||
}) {
|
||||
return _service.getLocationsInBounds(
|
||||
return _service.getLocationsInBoundsSync(
|
||||
north: north,
|
||||
south: south,
|
||||
east: east,
|
||||
@@ -32,9 +48,12 @@ class SuspectedLocationState extends ChangeNotifier {
|
||||
|
||||
/// Whether currently loading data
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
/// Download progress (0.0 to 1.0), null when not downloading
|
||||
double? get downloadProgress => _downloadProgress;
|
||||
|
||||
/// Last time data was fetched
|
||||
DateTime? get lastFetchTime => _service.lastFetchTime;
|
||||
Future<DateTime?> get lastFetchTime => _service.lastFetchTime;
|
||||
|
||||
/// Initialize the state
|
||||
Future<void> init({bool offlineMode = false}) async {
|
||||
@@ -47,7 +66,7 @@ class SuspectedLocationState extends ChangeNotifier {
|
||||
await _service.setEnabled(enabled);
|
||||
|
||||
// If enabling and no data exists, fetch it now
|
||||
if (enabled && !_service.hasData) {
|
||||
if (enabled && !(await _service.hasData)) {
|
||||
await _fetchData();
|
||||
}
|
||||
|
||||
@@ -57,13 +76,15 @@ class SuspectedLocationState extends ChangeNotifier {
|
||||
/// Manually refresh the data (force refresh)
|
||||
Future<bool> refreshData() async {
|
||||
_isLoading = true;
|
||||
_downloadProgress = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final success = await _service.forceRefresh();
|
||||
final success = await _service.forceRefresh(onProgress: _updateDownloadProgress);
|
||||
return success;
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
_downloadProgress = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -71,16 +92,24 @@ class SuspectedLocationState extends ChangeNotifier {
|
||||
/// Internal method to fetch data if needed with loading state management
|
||||
Future<bool> _fetchData() async {
|
||||
_isLoading = true;
|
||||
_downloadProgress = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final success = await _service.fetchDataIfNeeded();
|
||||
final success = await _service.fetchDataIfNeeded(onProgress: _updateDownloadProgress);
|
||||
return success;
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
_downloadProgress = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Update download progress
|
||||
void _updateDownloadProgress(double progress) {
|
||||
_downloadProgress = progress;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Select a suspected location for detail view
|
||||
void selectLocation(SuspectedLocation location) {
|
||||
|
||||
@@ -10,16 +10,19 @@ import '../models/node_profile.dart';
|
||||
import '../services/node_cache.dart';
|
||||
import '../services/uploader.dart';
|
||||
import '../widgets/node_provider_with_cache.dart';
|
||||
import '../dev_config.dart';
|
||||
import 'settings_state.dart';
|
||||
import 'session_state.dart';
|
||||
|
||||
class UploadQueueState extends ChangeNotifier {
|
||||
final List<PendingUpload> _queue = [];
|
||||
Timer? _uploadTimer;
|
||||
int _activeUploadCount = 0;
|
||||
|
||||
// Getters
|
||||
int get pendingCount => _queue.length;
|
||||
List<PendingUpload> get pendingUploads => List.unmodifiable(_queue);
|
||||
int get activeUploadCount => _activeUploadCount;
|
||||
|
||||
// Initialize by loading queue from storage and repopulate cache with pending nodes
|
||||
Future<void> init() async {
|
||||
@@ -124,6 +127,7 @@ class UploadQueueState extends ChangeNotifier {
|
||||
direction: _formatDirectionsForSubmission(session.directions, session.profile),
|
||||
profile: session.profile!, // Safe to use ! because commitSession() checks for null
|
||||
operatorProfile: session.operatorProfile,
|
||||
refinedTags: session.refinedTags,
|
||||
uploadMode: uploadMode,
|
||||
operation: UploadOperation.create,
|
||||
);
|
||||
@@ -180,6 +184,7 @@ class UploadQueueState extends ChangeNotifier {
|
||||
direction: _formatDirectionsForSubmission(session.directions, session.profile),
|
||||
profile: session.profile!, // Safe to use ! because commitEditSession() checks for null
|
||||
operatorProfile: session.operatorProfile,
|
||||
refinedTags: session.refinedTags,
|
||||
uploadMode: uploadMode,
|
||||
operation: operation,
|
||||
originalNodeId: session.originalNode.id, // Track which node we're editing
|
||||
@@ -319,19 +324,22 @@ class UploadQueueState extends ChangeNotifier {
|
||||
// No uploads if queue is empty, offline mode is enabled, or queue processing is paused
|
||||
if (_queue.isEmpty || offlineMode || pauseQueueProcessing) return;
|
||||
|
||||
_uploadTimer = Timer.periodic(const Duration(seconds: 10), (t) async {
|
||||
_uploadTimer = Timer.periodic(kUploadQueueProcessingInterval, (t) async {
|
||||
if (_queue.isEmpty || offlineMode || pauseQueueProcessing) {
|
||||
_uploadTimer?.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
// Find next item to process based on state
|
||||
final pendingItems = _queue.where((pu) => pu.uploadState == UploadState.pending).toList();
|
||||
final creatingChangesetItems = _queue.where((pu) => pu.uploadState == UploadState.creatingChangeset).toList();
|
||||
// Check if we can start more uploads (concurrency limit check)
|
||||
if (_activeUploadCount >= kMaxConcurrentUploads) {
|
||||
debugPrint('[UploadQueue] At concurrency limit ($_activeUploadCount/$kMaxConcurrentUploads), waiting for uploads to complete');
|
||||
return;
|
||||
}
|
||||
|
||||
// Process any expired items
|
||||
final uploadingItems = _queue.where((pu) => pu.uploadState == UploadState.uploading).toList();
|
||||
final closingItems = _queue.where((pu) => pu.uploadState == UploadState.closingChangeset).toList();
|
||||
|
||||
// Process any expired items
|
||||
for (final uploadingItem in uploadingItems) {
|
||||
if (uploadingItem.hasChangesetExpired) {
|
||||
debugPrint('[UploadQueue] Changeset expired during node submission - marking as failed');
|
||||
@@ -345,73 +353,109 @@ class UploadQueueState extends ChangeNotifier {
|
||||
if (closingItem.hasChangesetExpired) {
|
||||
debugPrint('[UploadQueue] Changeset expired during close - trusting OSM auto-close (node was submitted successfully)');
|
||||
_markAsCompleting(closingItem, submittedNodeId: closingItem.submittedNodeId!);
|
||||
// Continue processing loop - don't return here
|
||||
}
|
||||
}
|
||||
|
||||
// Find next pending item to start
|
||||
final pendingItems = _queue.where((pu) => pu.uploadState == UploadState.pending).toList();
|
||||
|
||||
// Find next item to process (process in stage order)
|
||||
PendingUpload? item;
|
||||
if (pendingItems.isNotEmpty) {
|
||||
item = pendingItems.first;
|
||||
} else if (creatingChangesetItems.isNotEmpty) {
|
||||
// Already in progress, skip
|
||||
return;
|
||||
} else if (uploadingItems.isNotEmpty) {
|
||||
// Check if any uploading items are ready for retry
|
||||
final readyToRetry = uploadingItems.where((ui) =>
|
||||
!ui.hasChangesetExpired && ui.isReadyForNodeSubmissionRetry
|
||||
).toList();
|
||||
|
||||
if (readyToRetry.isNotEmpty) {
|
||||
item = readyToRetry.first;
|
||||
}
|
||||
} else {
|
||||
// No active items, check if any changeset close items are ready for retry
|
||||
final readyToRetry = closingItems.where((ci) =>
|
||||
!ci.hasChangesetExpired && ci.isReadyForChangesetCloseRetry
|
||||
).toList();
|
||||
|
||||
if (readyToRetry.isNotEmpty) {
|
||||
item = readyToRetry.first;
|
||||
}
|
||||
}
|
||||
|
||||
if (item == null) {
|
||||
// No items ready for processing - check if queue is effectively empty
|
||||
if (pendingItems.isEmpty) {
|
||||
// Check if queue is effectively empty
|
||||
final hasActiveItems = _queue.any((pu) =>
|
||||
pu.uploadState == UploadState.pending ||
|
||||
pu.uploadState == UploadState.creatingChangeset ||
|
||||
(pu.uploadState == UploadState.uploading && !pu.hasChangesetExpired) ||
|
||||
(pu.uploadState == UploadState.closingChangeset && !pu.hasChangesetExpired)
|
||||
pu.uploadState == UploadState.uploading ||
|
||||
pu.uploadState == UploadState.closingChangeset
|
||||
);
|
||||
|
||||
if (!hasActiveItems) {
|
||||
debugPrint('[UploadQueue] No active items remaining, stopping uploader');
|
||||
_uploadTimer?.cancel();
|
||||
}
|
||||
return; // Nothing to process right now
|
||||
return;
|
||||
}
|
||||
|
||||
// Retrieve access after every tick (accounts for re-login)
|
||||
// Retrieve access token
|
||||
final access = await getAccessToken();
|
||||
if (access == null) return; // not logged in
|
||||
|
||||
debugPrint('[UploadQueue] Processing item in state: ${item.uploadState} with uploadMode: ${item.uploadMode}');
|
||||
// Start processing the next pending upload
|
||||
final item = pendingItems.first;
|
||||
debugPrint('[UploadQueue] Starting new upload processing for item at ${item.coord} ($_activeUploadCount/$kMaxConcurrentUploads active)');
|
||||
|
||||
if (item.uploadState == UploadState.pending) {
|
||||
await _processCreateChangeset(item, access);
|
||||
} else if (item.uploadState == UploadState.creatingChangeset) {
|
||||
// Already in progress, skip (shouldn't happen due to filtering above)
|
||||
debugPrint('[UploadQueue] Changeset creation already in progress, skipping');
|
||||
return;
|
||||
} else if (item.uploadState == UploadState.uploading) {
|
||||
await _processNodeOperation(item, access);
|
||||
} else if (item.uploadState == UploadState.closingChangeset) {
|
||||
await _processChangesetClose(item, access);
|
||||
}
|
||||
_activeUploadCount++;
|
||||
_processIndividualUpload(item, access);
|
||||
});
|
||||
}
|
||||
|
||||
// Process an individual upload through all three stages
|
||||
Future<void> _processIndividualUpload(PendingUpload item, String accessToken) async {
|
||||
try {
|
||||
debugPrint('[UploadQueue] Starting individual upload processing for ${item.operation.name} at ${item.coord}');
|
||||
|
||||
// Stage 1: Create changeset
|
||||
await _processCreateChangeset(item, accessToken);
|
||||
if (item.uploadState == UploadState.error) return;
|
||||
|
||||
// Stage 2: Node operation with retry logic
|
||||
bool nodeOperationCompleted = false;
|
||||
while (!nodeOperationCompleted && !item.hasChangesetExpired && item.uploadState != UploadState.error) {
|
||||
await _processNodeOperation(item, accessToken);
|
||||
|
||||
if (item.uploadState == UploadState.closingChangeset) {
|
||||
// Node operation succeeded
|
||||
nodeOperationCompleted = true;
|
||||
} else if (item.uploadState == UploadState.uploading && !item.isReadyForNodeSubmissionRetry) {
|
||||
// Need to wait before retry
|
||||
final delay = item.nextNodeSubmissionRetryDelay;
|
||||
debugPrint('[UploadQueue] Waiting ${delay.inSeconds}s before node submission retry');
|
||||
await Future.delayed(delay);
|
||||
} else if (item.uploadState == UploadState.error) {
|
||||
// Failed permanently
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!nodeOperationCompleted) return; // Failed or expired
|
||||
|
||||
// Stage 3: Close changeset with retry logic
|
||||
bool changesetClosed = false;
|
||||
while (!changesetClosed && !item.hasChangesetExpired && item.uploadState != UploadState.error) {
|
||||
await _processChangesetClose(item, accessToken);
|
||||
|
||||
if (item.uploadState == UploadState.complete) {
|
||||
// Changeset close succeeded
|
||||
changesetClosed = true;
|
||||
} else if (item.uploadState == UploadState.closingChangeset && !item.isReadyForChangesetCloseRetry) {
|
||||
// Need to wait before retry
|
||||
final delay = item.nextChangesetCloseRetryDelay;
|
||||
debugPrint('[UploadQueue] Waiting ${delay.inSeconds}s before changeset close retry');
|
||||
await Future.delayed(delay);
|
||||
} else if (item.uploadState == UploadState.error) {
|
||||
// Failed permanently
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!changesetClosed && item.hasChangesetExpired) {
|
||||
// Trust OSM auto-close if we ran out of time
|
||||
debugPrint('[UploadQueue] Upload completed but changeset close timed out - trusting OSM auto-close');
|
||||
if (item.submittedNodeId != null) {
|
||||
_markAsCompleting(item, submittedNodeId: item.submittedNodeId!);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
debugPrint('[UploadQueue] Unexpected error in individual upload processing: $e');
|
||||
item.setError('Unexpected error: $e');
|
||||
_saveQueue();
|
||||
notifyListeners();
|
||||
} finally {
|
||||
// Always decrement the active upload count
|
||||
_activeUploadCount--;
|
||||
debugPrint('[UploadQueue] Individual upload processing finished ($_activeUploadCount/$kMaxConcurrentUploads active)');
|
||||
}
|
||||
}
|
||||
|
||||
// Process changeset creation (step 1 of 3)
|
||||
Future<void> _processCreateChangeset(PendingUpload item, String access) async {
|
||||
item.markAsCreatingChangeset();
|
||||
|
||||
@@ -11,12 +11,81 @@ import '../services/changelog_service.dart';
|
||||
import 'refine_tags_sheet.dart';
|
||||
import 'proximity_warning_dialog.dart';
|
||||
import 'submission_guide_dialog.dart';
|
||||
import 'positioning_tutorial_overlay.dart';
|
||||
|
||||
class AddNodeSheet extends StatelessWidget {
|
||||
class AddNodeSheet extends StatefulWidget {
|
||||
const AddNodeSheet({super.key, required this.session});
|
||||
|
||||
final AddNodeSession session;
|
||||
|
||||
@override
|
||||
State<AddNodeSheet> createState() => _AddNodeSheetState();
|
||||
}
|
||||
|
||||
class _AddNodeSheetState extends State<AddNodeSheet> {
|
||||
bool _showTutorial = false;
|
||||
bool _isCheckingTutorial = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_checkTutorialStatus();
|
||||
}
|
||||
|
||||
Future<void> _checkTutorialStatus() async {
|
||||
final hasCompleted = await ChangelogService().hasCompletedPositioningTutorial();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_showTutorial = !hasCompleted;
|
||||
_isCheckingTutorial = false;
|
||||
});
|
||||
|
||||
// If tutorial should be shown, register callback with AppState
|
||||
if (_showTutorial) {
|
||||
final appState = context.read<AppState>();
|
||||
appState.registerTutorialCallback(_hideTutorial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Listen for tutorial completion from AppState
|
||||
void _onTutorialCompleted() {
|
||||
_hideTutorial();
|
||||
}
|
||||
|
||||
/// Also check periodically if tutorial was completed by another sheet
|
||||
void _recheckTutorialStatus() async {
|
||||
if (_showTutorial) {
|
||||
final hasCompleted = await ChangelogService().hasCompletedPositioningTutorial();
|
||||
if (hasCompleted && mounted) {
|
||||
setState(() {
|
||||
_showTutorial = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _hideTutorial() {
|
||||
if (mounted && _showTutorial) {
|
||||
setState(() {
|
||||
_showTutorial = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Clear tutorial callback when widget is disposed
|
||||
if (_showTutorial) {
|
||||
try {
|
||||
context.read<AppState>().clearTutorialCallback();
|
||||
} catch (e) {
|
||||
// Context might be unavailable during disposal, ignore
|
||||
}
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _checkProximityAndCommit(BuildContext context, AppState appState, LocalizationService locService) {
|
||||
_checkSubmissionGuideAndProceed(context, appState, locService);
|
||||
}
|
||||
@@ -27,11 +96,16 @@ class AddNodeSheet extends StatelessWidget {
|
||||
|
||||
if (!hasSeenGuide) {
|
||||
// Show submission guide dialog first
|
||||
await showDialog<void>(
|
||||
final shouldProceed = await showDialog<bool>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (context) => const SubmissionGuideDialog(),
|
||||
);
|
||||
|
||||
// If user canceled the submission guide, don't proceed with submission
|
||||
if (shouldProceed != true) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Now proceed with proximity check
|
||||
@@ -40,14 +114,14 @@ class AddNodeSheet extends StatelessWidget {
|
||||
|
||||
void _checkProximityOnly(BuildContext context, AppState appState, LocalizationService locService) {
|
||||
// Only check proximity if we have a target location
|
||||
if (session.target == null) {
|
||||
if (widget.session.target == null) {
|
||||
_commitWithoutCheck(context, appState, locService);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for nearby nodes within the configured distance
|
||||
final nearbyNodes = NodeCache.instance.findNodesWithinDistance(
|
||||
session.target!,
|
||||
widget.session.target!,
|
||||
kNodeProximityWarningDistance,
|
||||
);
|
||||
|
||||
@@ -220,28 +294,42 @@ class AddNodeSheet extends StatelessWidget {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
final session = widget.session;
|
||||
final submittableProfiles = appState.enabledProfiles.where((p) => p.isSubmittable).toList();
|
||||
final allowSubmit = appState.isLoggedIn &&
|
||||
submittableProfiles.isNotEmpty &&
|
||||
session.profile != null &&
|
||||
session.profile!.isSubmittable;
|
||||
|
||||
void _navigateToLogin() {
|
||||
Navigator.pushNamed(context, '/settings/osm-account');
|
||||
}
|
||||
|
||||
void _openRefineTags() async {
|
||||
final result = await Navigator.push<OperatorProfile?>(
|
||||
final result = await Navigator.push<RefineTagsResult?>(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => RefineTagsSheet(
|
||||
selectedOperatorProfile: session.operatorProfile,
|
||||
selectedProfile: session.profile,
|
||||
currentRefinedTags: session.refinedTags,
|
||||
),
|
||||
fullscreenDialog: true,
|
||||
),
|
||||
);
|
||||
if (result != session.operatorProfile) {
|
||||
appState.updateSession(operatorProfile: result);
|
||||
if (result != null) {
|
||||
appState.updateSession(
|
||||
operatorProfile: result.operatorProfile,
|
||||
refinedTags: result.refinedTags,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
return Stack(
|
||||
clipBehavior: Clip.none,
|
||||
fit: StackFit.loose,
|
||||
children: [
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 12),
|
||||
@@ -360,8 +448,8 @@ class AddNodeSheet extends StatelessWidget {
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: allowSubmit ? _commit : null,
|
||||
child: Text(locService.t('actions.submit')),
|
||||
onPressed: !appState.isLoggedIn ? _navigateToLogin : (allowSubmit ? _commit : null),
|
||||
child: Text(!appState.isLoggedIn ? locService.t('actions.logIn') : locService.t('actions.submit')),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -369,6 +457,14 @@ class AddNodeSheet extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
|
||||
// Tutorial overlay - show only if tutorial should be shown and we're done checking
|
||||
if (!_isCheckingTutorial && _showTutorial)
|
||||
Positioned.fill(
|
||||
child: PositioningTutorialOverlay(),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -13,12 +13,64 @@ import 'refine_tags_sheet.dart';
|
||||
import 'advanced_edit_options_sheet.dart';
|
||||
import 'proximity_warning_dialog.dart';
|
||||
import 'submission_guide_dialog.dart';
|
||||
import 'positioning_tutorial_overlay.dart';
|
||||
|
||||
class EditNodeSheet extends StatelessWidget {
|
||||
class EditNodeSheet extends StatefulWidget {
|
||||
const EditNodeSheet({super.key, required this.session});
|
||||
|
||||
final EditNodeSession session;
|
||||
|
||||
@override
|
||||
State<EditNodeSheet> createState() => _EditNodeSheetState();
|
||||
}
|
||||
|
||||
class _EditNodeSheetState extends State<EditNodeSheet> {
|
||||
bool _showTutorial = false;
|
||||
bool _isCheckingTutorial = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_checkTutorialStatus();
|
||||
}
|
||||
|
||||
Future<void> _checkTutorialStatus() async {
|
||||
final hasCompleted = await ChangelogService().hasCompletedPositioningTutorial();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_showTutorial = !hasCompleted;
|
||||
_isCheckingTutorial = false;
|
||||
});
|
||||
|
||||
// If tutorial should be shown, register callback with AppState
|
||||
if (_showTutorial) {
|
||||
final appState = context.read<AppState>();
|
||||
appState.registerTutorialCallback(_hideTutorial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _hideTutorial() {
|
||||
if (mounted && _showTutorial) {
|
||||
setState(() {
|
||||
_showTutorial = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Clear tutorial callback when widget is disposed
|
||||
if (_showTutorial) {
|
||||
try {
|
||||
context.read<AppState>().clearTutorialCallback();
|
||||
} catch (e) {
|
||||
// Context might be unavailable during disposal, ignore
|
||||
}
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _checkProximityAndCommit(BuildContext context, AppState appState, LocalizationService locService) {
|
||||
_checkSubmissionGuideAndProceed(context, appState, locService);
|
||||
}
|
||||
@@ -29,11 +81,16 @@ class EditNodeSheet extends StatelessWidget {
|
||||
|
||||
if (!hasSeenGuide) {
|
||||
// Show submission guide dialog first
|
||||
await showDialog<void>(
|
||||
final shouldProceed = await showDialog<bool>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (context) => const SubmissionGuideDialog(),
|
||||
);
|
||||
|
||||
// If user canceled the submission guide, don't proceed with submission
|
||||
if (shouldProceed != true) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Now proceed with proximity check
|
||||
@@ -43,9 +100,9 @@ class EditNodeSheet extends StatelessWidget {
|
||||
void _checkProximityOnly(BuildContext context, AppState appState, LocalizationService locService) {
|
||||
// Check for nearby nodes within the configured distance, excluding the node being edited
|
||||
final nearbyNodes = NodeCache.instance.findNodesWithinDistance(
|
||||
session.target,
|
||||
widget.session.target,
|
||||
kNodeProximityWarningDistance,
|
||||
excludeNodeId: session.originalNode.id,
|
||||
excludeNodeId: widget.session.originalNode.id,
|
||||
);
|
||||
|
||||
if (nearbyNodes.isNotEmpty) {
|
||||
@@ -217,6 +274,7 @@ class EditNodeSheet extends StatelessWidget {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
final session = widget.session;
|
||||
final submittableProfiles = appState.enabledProfiles.where((p) => p.isSubmittable).toList();
|
||||
final isSandboxMode = appState.uploadMode == UploadMode.sandbox;
|
||||
final allowSubmit = kEnableNodeEdits &&
|
||||
@@ -225,22 +283,35 @@ class EditNodeSheet extends StatelessWidget {
|
||||
session.profile != null &&
|
||||
session.profile!.isSubmittable;
|
||||
|
||||
void _navigateToLogin() {
|
||||
Navigator.pushNamed(context, '/settings/osm-account');
|
||||
}
|
||||
|
||||
void _openRefineTags() async {
|
||||
final result = await Navigator.push<OperatorProfile?>(
|
||||
final result = await Navigator.push<RefineTagsResult?>(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => RefineTagsSheet(
|
||||
selectedOperatorProfile: session.operatorProfile,
|
||||
selectedProfile: session.profile,
|
||||
currentRefinedTags: session.refinedTags,
|
||||
),
|
||||
fullscreenDialog: true,
|
||||
),
|
||||
);
|
||||
if (result != session.operatorProfile) {
|
||||
appState.updateEditSession(operatorProfile: result);
|
||||
if (result != null) {
|
||||
appState.updateEditSession(
|
||||
operatorProfile: result.operatorProfile,
|
||||
refinedTags: result.refinedTags,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
return Stack(
|
||||
clipBehavior: Clip.none,
|
||||
fit: StackFit.loose,
|
||||
children: [
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 12),
|
||||
@@ -433,8 +504,8 @@ class EditNodeSheet extends StatelessWidget {
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: allowSubmit ? _commit : null,
|
||||
child: Text(locService.t('actions.saveEdit')),
|
||||
onPressed: !appState.isLoggedIn ? _navigateToLogin : (allowSubmit ? _commit : null),
|
||||
child: Text(!appState.isLoggedIn ? locService.t('actions.logIn') : locService.t('actions.saveEdit')),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -442,6 +513,14 @@ class EditNodeSheet extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
|
||||
// Tutorial overlay - show only if tutorial should be shown and we're done checking
|
||||
if (!_isCheckingTutorial && _showTutorial)
|
||||
Positioned.fill(
|
||||
child: PositioningTutorialOverlay(),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -451,7 +530,7 @@ class EditNodeSheet extends StatelessWidget {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (context) => AdvancedEditOptionsSheet(node: session.originalNode),
|
||||
builder: (context) => AdvancedEditOptionsSheet(node: widget.session.originalNode),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,156 +10,38 @@ import '../../services/proximity_alert_service.dart';
|
||||
import '../../models/osm_node.dart';
|
||||
import '../../models/node_profile.dart';
|
||||
|
||||
/// Manages GPS location tracking, follow-me modes, and location-based map animations.
|
||||
/// Handles GPS permissions, position streams, and follow-me behavior.
|
||||
/// Simple GPS controller that respects permissions and provides location updates.
|
||||
/// Key principles:
|
||||
/// - Respect "denied forever" - stop trying
|
||||
/// - Retry "denied" - user might enable later
|
||||
/// - Accept whatever accuracy is available once granted
|
||||
class GpsController {
|
||||
StreamSubscription<Position>? _positionSub;
|
||||
LatLng? _currentLatLng;
|
||||
Timer? _retryTimer;
|
||||
|
||||
// Location state
|
||||
LatLng? _currentLocation;
|
||||
bool _hasLocation = false;
|
||||
|
||||
// Callbacks - set during initialization
|
||||
AnimatedMapController? _mapController;
|
||||
VoidCallback? _onLocationUpdated;
|
||||
FollowMeMode Function()? _getCurrentFollowMeMode;
|
||||
bool Function()? _getProximityAlertsEnabled;
|
||||
int Function()? _getProximityAlertDistance;
|
||||
List<OsmNode> Function()? _getNearbyNodes;
|
||||
List<NodeProfile> Function()? _getEnabledProfiles;
|
||||
VoidCallback? _onMapMovedProgrammatically;
|
||||
|
||||
/// Get the current GPS location (if available)
|
||||
LatLng? get currentLocation => _currentLatLng;
|
||||
LatLng? get currentLocation => _currentLocation;
|
||||
|
||||
/// Whether we currently have a valid GPS location
|
||||
bool get hasLocation => _hasLocation;
|
||||
|
||||
/// Initialize GPS location tracking
|
||||
Future<void> initializeLocation() async {
|
||||
final perm = await Geolocator.requestPermission();
|
||||
if (perm == LocationPermission.denied ||
|
||||
perm == LocationPermission.deniedForever) {
|
||||
debugPrint('[GpsController] Location permission denied');
|
||||
return;
|
||||
}
|
||||
|
||||
_positionSub = Geolocator.getPositionStream().listen((Position position) {
|
||||
final latLng = LatLng(position.latitude, position.longitude);
|
||||
_currentLatLng = latLng;
|
||||
debugPrint('[GpsController] GPS position updated: ${latLng.latitude}, ${latLng.longitude}');
|
||||
});
|
||||
}
|
||||
|
||||
/// Retry location initialization (e.g., after permission granted)
|
||||
Future<void> retryLocationInit() async {
|
||||
debugPrint('[GpsController] Retrying location initialization');
|
||||
await initializeLocation();
|
||||
}
|
||||
|
||||
/// Handle follow-me mode changes and animate map accordingly
|
||||
void handleFollowMeModeChange({
|
||||
required FollowMeMode newMode,
|
||||
required FollowMeMode oldMode,
|
||||
required AnimatedMapController controller,
|
||||
VoidCallback? onMapMovedProgrammatically,
|
||||
}) {
|
||||
debugPrint('[GpsController] Follow-me mode changed: $oldMode → $newMode');
|
||||
|
||||
// Only act when follow-me is first enabled and we have a current location
|
||||
if (newMode != FollowMeMode.off &&
|
||||
oldMode == FollowMeMode.off &&
|
||||
_currentLatLng != null) {
|
||||
|
||||
try {
|
||||
if (newMode == FollowMeMode.follow) {
|
||||
controller.animateTo(
|
||||
dest: _currentLatLng!,
|
||||
zoom: controller.mapController.camera.zoom,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
onMapMovedProgrammatically?.call();
|
||||
} else if (newMode == FollowMeMode.rotating) {
|
||||
// When switching to rotating mode, reset to north-up first
|
||||
controller.animateTo(
|
||||
dest: _currentLatLng!,
|
||||
zoom: controller.mapController.camera.zoom,
|
||||
rotation: 0.0,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
onMapMovedProgrammatically?.call();
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[GpsController] MapController not ready for follow-me change: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Process GPS position updates and handle follow-me animations
|
||||
void processPositionUpdate({
|
||||
required Position position,
|
||||
required FollowMeMode followMeMode,
|
||||
required AnimatedMapController controller,
|
||||
required VoidCallback onLocationUpdated,
|
||||
// Optional parameters for proximity alerts
|
||||
bool proximityAlertsEnabled = false,
|
||||
int proximityAlertDistance = 200,
|
||||
List<OsmNode> nearbyNodes = const [],
|
||||
List<NodeProfile> enabledProfiles = const [],
|
||||
// Optional callback when map is moved programmatically
|
||||
VoidCallback? onMapMovedProgrammatically,
|
||||
|
||||
}) {
|
||||
final latLng = LatLng(position.latitude, position.longitude);
|
||||
_currentLatLng = latLng;
|
||||
|
||||
// Notify that location was updated (for setState, etc.)
|
||||
onLocationUpdated();
|
||||
|
||||
// Check proximity alerts if enabled
|
||||
if (proximityAlertsEnabled && nearbyNodes.isNotEmpty) {
|
||||
ProximityAlertService().checkProximity(
|
||||
userLocation: latLng,
|
||||
nodes: nearbyNodes,
|
||||
enabledProfiles: enabledProfiles,
|
||||
alertDistance: proximityAlertDistance,
|
||||
);
|
||||
}
|
||||
|
||||
// Handle follow-me animations if enabled - use current mode from app state
|
||||
if (followMeMode != FollowMeMode.off) {
|
||||
debugPrint('[GpsController] GPS position update: ${latLng.latitude}, ${latLng.longitude}, follow-me: $followMeMode');
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
try {
|
||||
if (followMeMode == FollowMeMode.follow) {
|
||||
// Follow position only, keep current rotation
|
||||
controller.animateTo(
|
||||
dest: latLng,
|
||||
zoom: controller.mapController.camera.zoom,
|
||||
rotation: controller.mapController.camera.rotation,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
|
||||
// Notify that we moved the map programmatically (for node refresh)
|
||||
onMapMovedProgrammatically?.call();
|
||||
} else if (followMeMode == FollowMeMode.rotating) {
|
||||
// Follow position and rotation based on heading
|
||||
final heading = position.heading;
|
||||
final speed = position.speed; // Speed in m/s
|
||||
|
||||
// Only apply rotation if moving fast enough to avoid wild spinning when stationary
|
||||
final shouldRotate = !speed.isNaN && speed >= kMinSpeedForRotationMps && !heading.isNaN;
|
||||
final rotation = shouldRotate ? -heading : controller.mapController.camera.rotation;
|
||||
|
||||
controller.animateTo(
|
||||
dest: latLng,
|
||||
zoom: controller.mapController.camera.zoom,
|
||||
rotation: rotation,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
|
||||
// Notify that we moved the map programmatically (for node refresh)
|
||||
onMapMovedProgrammatically?.call();
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[GpsController] MapController not ready for position animation: $e');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize GPS with custom position processing callback
|
||||
Future<void> initializeWithCallback({
|
||||
required FollowMeMode followMeMode,
|
||||
required AnimatedMapController controller,
|
||||
/// Initialize GPS tracking with callbacks
|
||||
Future<void> initialize({
|
||||
required AnimatedMapController mapController,
|
||||
required VoidCallback onLocationUpdated,
|
||||
required FollowMeMode Function() getCurrentFollowMeMode,
|
||||
required bool Function() getProximityAlertsEnabled,
|
||||
@@ -167,40 +49,308 @@ class GpsController {
|
||||
required List<OsmNode> Function() getNearbyNodes,
|
||||
required List<NodeProfile> Function() getEnabledProfiles,
|
||||
VoidCallback? onMapMovedProgrammatically,
|
||||
|
||||
}) async {
|
||||
final perm = await Geolocator.requestPermission();
|
||||
if (perm == LocationPermission.denied ||
|
||||
perm == LocationPermission.deniedForever) {
|
||||
debugPrint('[GpsController] Location permission denied');
|
||||
debugPrint('[GpsController] Initializing GPS controller');
|
||||
|
||||
// Store callbacks
|
||||
_mapController = mapController;
|
||||
_onLocationUpdated = onLocationUpdated;
|
||||
_getCurrentFollowMeMode = getCurrentFollowMeMode;
|
||||
_getProximityAlertsEnabled = getProximityAlertsEnabled;
|
||||
_getProximityAlertDistance = getProximityAlertDistance;
|
||||
_getNearbyNodes = getNearbyNodes;
|
||||
_getEnabledProfiles = getEnabledProfiles;
|
||||
_onMapMovedProgrammatically = onMapMovedProgrammatically;
|
||||
|
||||
// Start location tracking
|
||||
await _startLocationTracking();
|
||||
}
|
||||
|
||||
/// Update follow-me mode and restart tracking with appropriate frequency
|
||||
void updateFollowMeMode({
|
||||
required FollowMeMode newMode,
|
||||
required FollowMeMode oldMode,
|
||||
}) {
|
||||
debugPrint('[GpsController] Follow-me mode changed: $oldMode → $newMode');
|
||||
|
||||
// Restart position stream with new frequency settings
|
||||
_restartPositionStream();
|
||||
|
||||
// Handle initial animation when follow-me is first enabled
|
||||
_handleInitialFollowMeAnimation(newMode, oldMode);
|
||||
}
|
||||
|
||||
/// Manual retry (e.g., user pressed follow-me button)
|
||||
Future<void> retryLocationInit() async {
|
||||
debugPrint('[GpsController] Manual retry of location initialization');
|
||||
_cancelRetry();
|
||||
await _startLocationTracking();
|
||||
}
|
||||
|
||||
/// Start location tracking - checks permissions and starts stream
|
||||
Future<void> _startLocationTracking() async {
|
||||
_stopLocationTracking(); // Clean slate
|
||||
|
||||
// Check if location services are enabled
|
||||
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
debugPrint('[GpsController] Location services disabled');
|
||||
_hasLocation = false;
|
||||
_notifyLocationChange();
|
||||
_scheduleRetry();
|
||||
return;
|
||||
}
|
||||
|
||||
_positionSub = Geolocator.getPositionStream().listen((Position position) {
|
||||
// Get the current follow-me mode from the app state each time
|
||||
final currentFollowMeMode = getCurrentFollowMeMode();
|
||||
final proximityAlertsEnabled = getProximityAlertsEnabled();
|
||||
final proximityAlertDistance = getProximityAlertDistance();
|
||||
final nearbyNodes = getNearbyNodes();
|
||||
final enabledProfiles = getEnabledProfiles();
|
||||
processPositionUpdate(
|
||||
position: position,
|
||||
followMeMode: currentFollowMeMode,
|
||||
controller: controller,
|
||||
onLocationUpdated: onLocationUpdated,
|
||||
proximityAlertsEnabled: proximityAlertsEnabled,
|
||||
proximityAlertDistance: proximityAlertDistance,
|
||||
nearbyNodes: nearbyNodes,
|
||||
enabledProfiles: enabledProfiles,
|
||||
onMapMovedProgrammatically: onMapMovedProgrammatically,
|
||||
// Check permissions
|
||||
final permission = await Geolocator.requestPermission();
|
||||
debugPrint('[GpsController] Location permission result: $permission');
|
||||
|
||||
switch (permission) {
|
||||
case LocationPermission.deniedForever:
|
||||
// User said "never" - respect that and stop trying
|
||||
debugPrint('[GpsController] Location denied forever - stopping attempts');
|
||||
_hasLocation = false;
|
||||
_notifyLocationChange();
|
||||
return;
|
||||
|
||||
case LocationPermission.denied:
|
||||
// User said "not now" - keep trying later
|
||||
debugPrint('[GpsController] Location denied - will retry later');
|
||||
_hasLocation = false;
|
||||
_notifyLocationChange();
|
||||
_scheduleRetry();
|
||||
return;
|
||||
|
||||
case LocationPermission.whileInUse:
|
||||
case LocationPermission.always:
|
||||
// Permission granted - start stream
|
||||
debugPrint('[GpsController] Location permission granted: $permission');
|
||||
_startPositionStream();
|
||||
return;
|
||||
|
||||
case LocationPermission.unableToDetermine:
|
||||
// Couldn't determine permission state - treat like denied and retry
|
||||
debugPrint('[GpsController] Unable to determine permission state - will retry');
|
||||
_hasLocation = false;
|
||||
_notifyLocationChange();
|
||||
_scheduleRetry();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the GPS position stream
|
||||
void _startPositionStream() {
|
||||
final followMeMode = _getCurrentFollowMeMode?.call() ?? FollowMeMode.off;
|
||||
final distanceFilter = followMeMode == FollowMeMode.off ? 5 : 1; // 5m normal, 1m follow-me
|
||||
|
||||
debugPrint('[GpsController] Starting GPS position stream (${distanceFilter}m filter)');
|
||||
|
||||
try {
|
||||
_positionSub = Geolocator.getPositionStream(
|
||||
locationSettings: LocationSettings(
|
||||
accuracy: LocationAccuracy.high, // Request best, accept what we get
|
||||
distanceFilter: distanceFilter,
|
||||
),
|
||||
).listen(
|
||||
_onPositionReceived,
|
||||
onError: _onPositionError,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[GpsController] Failed to start position stream: $e');
|
||||
_hasLocation = false;
|
||||
_notifyLocationChange();
|
||||
_scheduleRetry();
|
||||
}
|
||||
}
|
||||
|
||||
/// Restart position stream with current follow-me settings
|
||||
void _restartPositionStream() {
|
||||
if (_positionSub == null) {
|
||||
// No active stream, let retry logic handle it
|
||||
return;
|
||||
}
|
||||
|
||||
debugPrint('[GpsController] Restarting position stream for follow-me mode change');
|
||||
_stopLocationTracking();
|
||||
_startPositionStream();
|
||||
}
|
||||
|
||||
/// Handle incoming GPS position
|
||||
void _onPositionReceived(Position position) {
|
||||
final newLocation = LatLng(position.latitude, position.longitude);
|
||||
_currentLocation = newLocation;
|
||||
|
||||
if (!_hasLocation) {
|
||||
debugPrint('[GpsController] GPS location acquired');
|
||||
}
|
||||
_hasLocation = true;
|
||||
_cancelRetry(); // Got location, stop any retry attempts
|
||||
|
||||
debugPrint('[GpsController] GPS position: ${newLocation.latitude}, ${newLocation.longitude} (±${position.accuracy}m)');
|
||||
|
||||
// Notify UI
|
||||
_notifyLocationChange();
|
||||
|
||||
// Handle proximity alerts
|
||||
_checkProximityAlerts(newLocation);
|
||||
|
||||
// Handle follow-me animations
|
||||
_handleFollowMeUpdate(position, newLocation);
|
||||
}
|
||||
|
||||
/// Handle GPS stream errors
|
||||
void _onPositionError(dynamic error) {
|
||||
debugPrint('[GpsController] Position stream error: $error');
|
||||
if (_hasLocation) {
|
||||
debugPrint('[GpsController] Lost GPS location - will retry');
|
||||
}
|
||||
_hasLocation = false;
|
||||
_currentLocation = null;
|
||||
_notifyLocationChange();
|
||||
_scheduleRetry();
|
||||
}
|
||||
|
||||
/// Check proximity alerts if enabled
|
||||
void _checkProximityAlerts(LatLng userLocation) {
|
||||
final proximityEnabled = _getProximityAlertsEnabled?.call() ?? false;
|
||||
if (!proximityEnabled) return;
|
||||
|
||||
final nearbyNodes = _getNearbyNodes?.call() ?? [];
|
||||
if (nearbyNodes.isEmpty) return;
|
||||
|
||||
final alertDistance = _getProximityAlertDistance?.call() ?? 200;
|
||||
final enabledProfiles = _getEnabledProfiles?.call() ?? [];
|
||||
|
||||
ProximityAlertService().checkProximity(
|
||||
userLocation: userLocation,
|
||||
nodes: nearbyNodes,
|
||||
enabledProfiles: enabledProfiles,
|
||||
alertDistance: alertDistance,
|
||||
);
|
||||
}
|
||||
|
||||
/// Handle follow-me animations
|
||||
void _handleFollowMeUpdate(Position position, LatLng location) {
|
||||
final followMeMode = _getCurrentFollowMeMode?.call() ?? FollowMeMode.off;
|
||||
if (followMeMode == FollowMeMode.off || _mapController == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
try {
|
||||
if (followMeMode == FollowMeMode.follow) {
|
||||
// Follow position, preserve rotation
|
||||
_mapController!.animateTo(
|
||||
dest: location,
|
||||
zoom: _mapController!.mapController.camera.zoom,
|
||||
rotation: _mapController!.mapController.camera.rotation,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
} else if (followMeMode == FollowMeMode.rotating) {
|
||||
// Follow position and heading
|
||||
final heading = position.heading;
|
||||
final speed = position.speed;
|
||||
|
||||
// Only rotate if moving fast enough and heading is valid
|
||||
final shouldRotate = !speed.isNaN && speed >= kMinSpeedForRotationMps && !heading.isNaN;
|
||||
final rotation = shouldRotate ? -heading : _mapController!.mapController.camera.rotation;
|
||||
|
||||
_mapController!.animateTo(
|
||||
dest: location,
|
||||
zoom: _mapController!.mapController.camera.zoom,
|
||||
rotation: rotation,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
|
||||
// Notify that map was moved programmatically
|
||||
_onMapMovedProgrammatically?.call();
|
||||
} catch (e) {
|
||||
debugPrint('[GpsController] Map animation error: $e');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Dispose of GPS resources
|
||||
void dispose() {
|
||||
/// Handle initial animation when follow-me mode is enabled
|
||||
void _handleInitialFollowMeAnimation(FollowMeMode newMode, FollowMeMode oldMode) {
|
||||
if (newMode == FollowMeMode.off || oldMode != FollowMeMode.off) {
|
||||
return; // Not enabling follow-me, or already enabled
|
||||
}
|
||||
|
||||
if (_currentLocation == null || _mapController == null) {
|
||||
return; // No location or map controller
|
||||
}
|
||||
|
||||
try {
|
||||
if (newMode == FollowMeMode.follow) {
|
||||
_mapController!.animateTo(
|
||||
dest: _currentLocation!,
|
||||
zoom: _mapController!.mapController.camera.zoom,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
} else if (newMode == FollowMeMode.rotating) {
|
||||
// Reset to north-up when starting rotating mode
|
||||
_mapController!.animateTo(
|
||||
dest: _currentLocation!,
|
||||
zoom: _mapController!.mapController.camera.zoom,
|
||||
rotation: 0.0,
|
||||
duration: kFollowMeAnimationDuration,
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
|
||||
_onMapMovedProgrammatically?.call();
|
||||
} catch (e) {
|
||||
debugPrint('[GpsController] Initial follow-me animation error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Notify UI that location status changed
|
||||
void _notifyLocationChange() {
|
||||
_onLocationUpdated?.call();
|
||||
}
|
||||
|
||||
/// Schedule retry attempts for location access
|
||||
void _scheduleRetry() {
|
||||
_cancelRetry();
|
||||
_retryTimer = Timer.periodic(const Duration(seconds: 15), (timer) {
|
||||
debugPrint('[GpsController] Retry attempt ${timer.tick}');
|
||||
_startLocationTracking();
|
||||
});
|
||||
}
|
||||
|
||||
/// Cancel any pending retry attempts
|
||||
void _cancelRetry() {
|
||||
if (_retryTimer != null) {
|
||||
debugPrint('[GpsController] Canceling retry timer');
|
||||
_retryTimer?.cancel();
|
||||
_retryTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop the position stream
|
||||
void _stopLocationTracking() {
|
||||
_positionSub?.cancel();
|
||||
_positionSub = null;
|
||||
debugPrint('[GpsController] GPS controller disposed');
|
||||
}
|
||||
|
||||
/// Clean up all resources
|
||||
void dispose() {
|
||||
debugPrint('[GpsController] Disposing GPS controller');
|
||||
_stopLocationTracking();
|
||||
_cancelRetry();
|
||||
|
||||
// Clear callbacks
|
||||
_mapController = null;
|
||||
_onLocationUpdated = null;
|
||||
_getCurrentFollowMeMode = null;
|
||||
_getProximityAlertsEnabled = null;
|
||||
_getProximityAlertDistance = null;
|
||||
_getNearbyNodes = null;
|
||||
_getEnabledProfiles = null;
|
||||
_onMapMovedProgrammatically = null;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,21 @@ class MapDataManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Expand bounds by the given multiplier, maintaining center point.
|
||||
/// Used to expand rendering bounds to prevent nodes blinking at screen edges.
|
||||
LatLngBounds _expandBounds(LatLngBounds bounds, double multiplier) {
|
||||
final centerLat = (bounds.north + bounds.south) / 2;
|
||||
final centerLng = (bounds.east + bounds.west) / 2;
|
||||
|
||||
final latSpan = (bounds.north - bounds.south) * multiplier / 2;
|
||||
final lngSpan = (bounds.east - bounds.west) * multiplier / 2;
|
||||
|
||||
return LatLngBounds(
|
||||
LatLng(centerLat - latSpan, centerLng - lngSpan),
|
||||
LatLng(centerLat + latSpan, centerLng + lngSpan),
|
||||
);
|
||||
}
|
||||
|
||||
/// Get nodes to render based on current map state
|
||||
/// Returns a MapDataResult containing all relevant node data and limit state
|
||||
MapDataResult getNodesForRendering({
|
||||
@@ -39,10 +54,13 @@ class MapDataManager {
|
||||
bool isLimitActive = false;
|
||||
|
||||
if (currentZoom >= minZoom) {
|
||||
// Above minimum zoom - get cached nodes directly (no Provider needed)
|
||||
allNodes = (mapBounds != null)
|
||||
? NodeProviderWithCache.instance.getCachedNodesForBounds(mapBounds)
|
||||
: <OsmNode>[];
|
||||
// Above minimum zoom - get cached nodes with expanded bounds to prevent edge blinking
|
||||
if (mapBounds != null) {
|
||||
final expandedBounds = _expandBounds(mapBounds, kNodeRenderingBoundsExpansion);
|
||||
allNodes = NodeProviderWithCache.instance.getCachedNodesForBounds(expandedBounds);
|
||||
} else {
|
||||
allNodes = <OsmNode>[];
|
||||
}
|
||||
|
||||
// Filter out invalid coordinates before applying limit
|
||||
final validNodes = allNodes.where((node) {
|
||||
|
||||
@@ -62,23 +62,29 @@ class MarkerLayerBuilder {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
|
||||
// Determine if we should dim node markers (when suspected location is selected)
|
||||
final shouldDimNodes = appState.selectedSuspectedLocation != null;
|
||||
// Determine if nodes should be dimmed and/or disabled
|
||||
final shouldDimNodes = appState.selectedSuspectedLocation != null ||
|
||||
appState.isInSearchMode ||
|
||||
appState.showingOverview;
|
||||
|
||||
// Disable node interactions when navigation is in conflicting state
|
||||
final shouldDisableNodeTaps = appState.isInSearchMode || appState.showingOverview;
|
||||
|
||||
final markers = NodeMarkersBuilder.buildNodeMarkers(
|
||||
nodes: nodesToRender,
|
||||
mapController: mapController.mapController,
|
||||
userLocation: userLocation,
|
||||
selectedNodeId: selectedNodeId,
|
||||
onNodeTap: onNodeTap,
|
||||
onNodeTap: onNodeTap, // Keep the original callback
|
||||
shouldDim: shouldDimNodes,
|
||||
enabled: !shouldDisableNodeTaps, // Use enabled parameter instead
|
||||
);
|
||||
|
||||
// Build suspected location markers (respect same zoom and count limits as nodes)
|
||||
final suspectedLocationMarkers = <Marker>[];
|
||||
if (appState.suspectedLocationsEnabled && mapBounds != null &&
|
||||
currentZoom >= (appState.uploadMode == UploadMode.sandbox ? kOsmApiMinZoomLevel : kNodeMinZoomLevel)) {
|
||||
final suspectedLocations = appState.getSuspectedLocationsInBounds(
|
||||
final suspectedLocations = appState.getSuspectedLocationsInBoundsSync(
|
||||
north: mapBounds.north,
|
||||
south: mapBounds.south,
|
||||
east: mapBounds.east,
|
||||
@@ -101,7 +107,9 @@ class MarkerLayerBuilder {
|
||||
locations: filteredSuspectedLocations,
|
||||
mapController: mapController.mapController,
|
||||
selectedLocationId: appState.selectedSuspectedLocation?.ticketNo,
|
||||
onLocationTap: onSuspectedLocationTap,
|
||||
onLocationTap: onSuspectedLocationTap, // Keep the original callback
|
||||
shouldDimAll: shouldDisableNodeTaps,
|
||||
enabled: !shouldDisableNodeTaps, // Use enabled parameter instead
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -13,11 +13,13 @@ class NodeMapMarker extends StatefulWidget {
|
||||
final OsmNode node;
|
||||
final MapController mapController;
|
||||
final void Function(OsmNode)? onNodeTap;
|
||||
final bool enabled;
|
||||
|
||||
const NodeMapMarker({
|
||||
required this.node,
|
||||
required this.mapController,
|
||||
this.onNodeTap,
|
||||
this.enabled = true,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@@ -31,6 +33,8 @@ class _NodeMapMarkerState extends State<NodeMapMarker> {
|
||||
static const Duration tapTimeout = kMarkerTapTimeout;
|
||||
|
||||
void _onTap() {
|
||||
if (!widget.enabled) return; // Don't respond to taps when disabled
|
||||
|
||||
_tapTimer = Timer(tapTimeout, () {
|
||||
// Don't center immediately - let the sheet opening handle the coordinated animation
|
||||
|
||||
@@ -38,6 +42,9 @@ class _NodeMapMarkerState extends State<NodeMapMarker> {
|
||||
if (widget.onNodeTap != null) {
|
||||
widget.onNodeTap!(widget.node);
|
||||
} else {
|
||||
// Fallback: This should not happen if callbacks are properly provided,
|
||||
// but if it does, at least open the sheet (without map coordination)
|
||||
debugPrint('[NodeMapMarker] Warning: onNodeTap callback not provided, using fallback');
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (_) => NodeTagSheet(node: widget.node),
|
||||
@@ -48,6 +55,8 @@ class _NodeMapMarkerState extends State<NodeMapMarker> {
|
||||
}
|
||||
|
||||
void _onDoubleTap() {
|
||||
if (!widget.enabled) return; // Don't respond to double taps when disabled
|
||||
|
||||
_tapTimer?.cancel();
|
||||
widget.mapController.move(widget.node.coord, widget.mapController.camera.zoom + kNodeDoubleTapZoomDelta);
|
||||
}
|
||||
@@ -96,6 +105,7 @@ class NodeMarkersBuilder {
|
||||
int? selectedNodeId,
|
||||
void Function(OsmNode)? onNodeTap,
|
||||
bool shouldDim = false,
|
||||
bool enabled = true,
|
||||
}) {
|
||||
final markers = <Marker>[
|
||||
// Node markers
|
||||
@@ -116,6 +126,7 @@ class NodeMarkersBuilder {
|
||||
node: n,
|
||||
mapController: mapController,
|
||||
onNodeTap: onNodeTap,
|
||||
enabled: enabled,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -13,11 +13,13 @@ class SuspectedLocationMapMarker extends StatefulWidget {
|
||||
final SuspectedLocation location;
|
||||
final MapController mapController;
|
||||
final void Function(SuspectedLocation)? onLocationTap;
|
||||
final bool enabled;
|
||||
|
||||
const SuspectedLocationMapMarker({
|
||||
required this.location,
|
||||
required this.mapController,
|
||||
this.onLocationTap,
|
||||
this.enabled = true,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@@ -31,11 +33,16 @@ class _SuspectedLocationMapMarkerState extends State<SuspectedLocationMapMarker>
|
||||
static const Duration tapTimeout = kMarkerTapTimeout;
|
||||
|
||||
void _onTap() {
|
||||
if (!widget.enabled) return; // Don't respond to taps when disabled
|
||||
|
||||
_tapTimer = Timer(tapTimeout, () {
|
||||
// Use callback if provided, otherwise fallback to direct modal
|
||||
if (widget.onLocationTap != null) {
|
||||
widget.onLocationTap!(widget.location);
|
||||
} else {
|
||||
// Fallback: This should not happen if callbacks are properly provided,
|
||||
// but if it does, at least open the sheet (without map coordination)
|
||||
debugPrint('[SuspectedLocationMapMarker] Warning: onLocationTap callback not provided, using fallback');
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (_) => SuspectedLocationSheet(location: widget.location),
|
||||
@@ -46,6 +53,8 @@ class _SuspectedLocationMapMarkerState extends State<SuspectedLocationMapMarker>
|
||||
}
|
||||
|
||||
void _onDoubleTap() {
|
||||
if (!widget.enabled) return; // Don't respond to double taps when disabled
|
||||
|
||||
_tapTimer?.cancel();
|
||||
widget.mapController.move(widget.location.centroid, widget.mapController.camera.zoom + kNodeDoubleTapZoomDelta);
|
||||
}
|
||||
@@ -73,6 +82,8 @@ class SuspectedLocationMarkersBuilder {
|
||||
required MapController mapController,
|
||||
String? selectedLocationId,
|
||||
void Function(SuspectedLocation)? onLocationTap,
|
||||
bool shouldDimAll = false,
|
||||
bool enabled = true,
|
||||
}) {
|
||||
final markers = <Marker>[];
|
||||
|
||||
@@ -81,7 +92,7 @@ class SuspectedLocationMarkersBuilder {
|
||||
|
||||
// Check if this location should be highlighted (selected) or dimmed
|
||||
final isSelected = selectedLocationId == location.ticketNo;
|
||||
final shouldDim = selectedLocationId != null && !isSelected;
|
||||
final shouldDim = shouldDimAll || (selectedLocationId != null && !isSelected);
|
||||
|
||||
markers.add(
|
||||
Marker(
|
||||
@@ -94,6 +105,7 @@ class SuspectedLocationMarkersBuilder {
|
||||
location: location,
|
||||
mapController: mapController,
|
||||
onLocationTap: onLocationTap,
|
||||
enabled: enabled,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -4,7 +4,7 @@ import 'package:flutter_map_animations/flutter_map_animations.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../app_state.dart';
|
||||
import '../app_state.dart' show AppState, FollowMeMode, UploadMode;
|
||||
import '../services/offline_area_service.dart';
|
||||
import '../services/network_status.dart';
|
||||
import '../services/prefetch_area_service.dart';
|
||||
@@ -28,7 +28,6 @@ import 'network_status_indicator.dart';
|
||||
import 'node_limit_indicator.dart';
|
||||
import 'proximity_alert_banner.dart';
|
||||
import '../dev_config.dart';
|
||||
import '../app_state.dart' show FollowMeMode;
|
||||
import '../services/proximity_alert_service.dart';
|
||||
import 'sheet_aware_map.dart';
|
||||
|
||||
@@ -45,6 +44,7 @@ class MapView extends StatefulWidget {
|
||||
this.onSuspectedLocationTap,
|
||||
this.onSearchPressed,
|
||||
this.onNodeLimitChanged,
|
||||
this.onLocationStatusChanged,
|
||||
});
|
||||
|
||||
final FollowMeMode followMeMode;
|
||||
@@ -55,6 +55,7 @@ class MapView extends StatefulWidget {
|
||||
final void Function(SuspectedLocation)? onSuspectedLocationTap;
|
||||
final VoidCallback? onSearchPressed;
|
||||
final void Function(bool isLimited)? onNodeLimitChanged;
|
||||
final VoidCallback? onLocationStatusChanged;
|
||||
|
||||
@override
|
||||
State<MapView> createState() => MapViewState();
|
||||
@@ -119,10 +120,12 @@ class MapViewState extends State<MapView> {
|
||||
});
|
||||
|
||||
// Initialize GPS with callback for position updates and follow-me
|
||||
_gpsController.initializeWithCallback(
|
||||
followMeMode: widget.followMeMode,
|
||||
controller: _controller,
|
||||
onLocationUpdated: () => setState(() {}),
|
||||
_gpsController.initialize(
|
||||
mapController: _controller,
|
||||
onLocationUpdated: () {
|
||||
setState(() {});
|
||||
widget.onLocationStatusChanged?.call(); // Notify parent about location status change
|
||||
},
|
||||
getCurrentFollowMeMode: () {
|
||||
// Use mounted check to avoid calling context when widget is disposed
|
||||
if (mounted) {
|
||||
@@ -191,7 +194,6 @@ class MapViewState extends State<MapView> {
|
||||
// Refresh nodes when GPS controller moves the map
|
||||
_refreshNodesFromProvider();
|
||||
},
|
||||
|
||||
);
|
||||
|
||||
// Fetch initial cameras
|
||||
@@ -231,6 +233,9 @@ class MapViewState extends State<MapView> {
|
||||
LatLng? getUserLocation() {
|
||||
return _gpsController.currentLocation;
|
||||
}
|
||||
|
||||
/// Whether we currently have a valid GPS location
|
||||
bool get hasLocation => _gpsController.hasLocation;
|
||||
|
||||
/// Expose static methods from MapPositionManager for external access
|
||||
static Future<void> clearStoredMapPosition() =>
|
||||
@@ -249,19 +254,20 @@ class MapViewState extends State<MapView> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Calculate search bar offset for screen-positioned indicators
|
||||
double _calculateScreenIndicatorSearchOffset(AppState appState) {
|
||||
return (!appState.offlineMode && appState.isInSearchMode) ? 60.0 : 0.0;
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant MapView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
// Handle follow-me mode changes - only if it actually changed
|
||||
if (widget.followMeMode != oldWidget.followMeMode) {
|
||||
_gpsController.handleFollowMeModeChange(
|
||||
_gpsController.updateFollowMeMode(
|
||||
newMode: widget.followMeMode,
|
||||
oldMode: oldWidget.followMeMode,
|
||||
controller: _controller,
|
||||
onMapMovedProgrammatically: () {
|
||||
_refreshNodesFromProvider();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -370,23 +376,6 @@ class MapViewState extends State<MapView> {
|
||||
children: [
|
||||
...overlayLayers,
|
||||
markerLayer,
|
||||
|
||||
// Node limit indicator (top-left) - shown when limit is active
|
||||
Builder(
|
||||
builder: (context) {
|
||||
final appState = context.read<AppState>();
|
||||
// Add search bar offset when search bar is visible
|
||||
final searchBarOffset = (!appState.offlineMode && appState.isInSearchMode) ? 60.0 : 0.0;
|
||||
|
||||
return NodeLimitIndicator(
|
||||
isActive: nodeData.isLimitActive,
|
||||
renderedCount: nodeData.nodesToRender.length,
|
||||
totalCount: nodeData.validNodesCount,
|
||||
top: 8.0 + searchBarOffset,
|
||||
left: 8.0,
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
@@ -540,12 +529,28 @@ class MapViewState extends State<MapView> {
|
||||
onSearchPressed: widget.onSearchPressed,
|
||||
),
|
||||
|
||||
// Node limit indicator (top-left) - shown when limit is active
|
||||
Builder(
|
||||
builder: (context) {
|
||||
final appState = context.watch<AppState>();
|
||||
final searchBarOffset = _calculateScreenIndicatorSearchOffset(appState);
|
||||
|
||||
return NodeLimitIndicator(
|
||||
isActive: nodeData.isLimitActive,
|
||||
renderedCount: nodeData.nodesToRender.length,
|
||||
totalCount: nodeData.validNodesCount,
|
||||
top: 8.0 + searchBarOffset,
|
||||
left: 8.0,
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
// Network status indicator (top-left) - conditionally shown
|
||||
if (appState.networkStatusIndicatorEnabled)
|
||||
Builder(
|
||||
builder: (context) {
|
||||
// Calculate position based on node limit indicator presence and search bar
|
||||
final searchBarOffset = (!appState.offlineMode && appState.isInSearchMode) ? 60.0 : 0.0;
|
||||
final appState = context.watch<AppState>();
|
||||
final searchBarOffset = _calculateScreenIndicatorSearchOffset(appState);
|
||||
final nodeLimitOffset = nodeData.isLimitActive ? 48.0 : 0.0; // Height of node limit indicator + spacing
|
||||
|
||||
return NetworkStatusIndicator(
|
||||
|
||||
@@ -93,8 +93,14 @@ class NavigationSheet extends StatelessWidget {
|
||||
children: [
|
||||
_buildDragHandle(),
|
||||
|
||||
// SEARCH MODE: Initial location with route options
|
||||
if (navigationMode == AppNavigationMode.search && !appState.isSettingSecondPoint && !appState.isCalculating && !appState.showingOverview && provisionalLocation != null) ...[
|
||||
// SEARCH MODE: Initial location with route options (only when no route points are set yet)
|
||||
if (navigationMode == AppNavigationMode.search &&
|
||||
!appState.isSettingSecondPoint &&
|
||||
!appState.isCalculating &&
|
||||
!appState.showingOverview &&
|
||||
provisionalLocation != null &&
|
||||
appState.routeStart == null &&
|
||||
appState.routeEnd == null) ...[
|
||||
_buildLocationInfo(
|
||||
label: LocalizationService.instance.t('navigation.location'),
|
||||
coordinates: provisionalLocation,
|
||||
@@ -155,7 +161,50 @@ class NavigationSheet extends StatelessWidget {
|
||||
coordinates: provisionalLocation,
|
||||
address: provisionalAddress,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// Show distance from first point
|
||||
if (appState.distanceFromFirstPoint != null) ...[
|
||||
Text(
|
||||
'Distance: ${(appState.distanceFromFirstPoint! / 1000).toStringAsFixed(1)} km',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
|
||||
// Show distance warning if threshold exceeded
|
||||
if (appState.distanceExceedsWarningThreshold) ...[
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.amber.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.amber.withOpacity(0.3)),
|
||||
),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.warning_amber, color: Colors.amber[700], size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Trips longer than ${(kNavigationDistanceWarningThreshold / 1000).toStringAsFixed(0)} km are likely to time out. We are working to improve this; stay tuned.',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.amber[700],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
|
||||
|
||||
// Show warning message if locations are too close
|
||||
if (appState.areRoutePointsTooClose) ...[
|
||||
@@ -185,13 +234,27 @@ class NavigationSheet extends StatelessWidget {
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
ElevatedButton.icon(
|
||||
icon: const Icon(Icons.check),
|
||||
label: Text(LocalizationService.instance.t('navigation.selectLocation')),
|
||||
onPressed: appState.areRoutePointsTooClose ? null : () {
|
||||
debugPrint('[NavigationSheet] Select Location button pressed');
|
||||
appState.selectSecondRoutePoint();
|
||||
},
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
icon: const Icon(Icons.check),
|
||||
label: Text(LocalizationService.instance.t('navigation.selectLocation')),
|
||||
onPressed: appState.areRoutePointsTooClose ? null : () {
|
||||
debugPrint('[NavigationSheet] Select Location button pressed');
|
||||
appState.selectSecondRoutePoint();
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
icon: const Icon(Icons.close),
|
||||
label: Text(LocalizationService.instance.t('actions.cancel')),
|
||||
onPressed: () => appState.cancelNavigation(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ class NodeProviderWithCache extends ChangeNotifier {
|
||||
if (enabledProfiles.isEmpty) return [];
|
||||
|
||||
// Filter nodes to only show those matching enabled profiles
|
||||
// Note: This uses ALL enabled profiles for filtering, even though Overpass queries
|
||||
// may be deduplicated for efficiency (broader profiles capture nodes for specific ones)
|
||||
return allNodes.where((node) {
|
||||
return _matchesAnyProfile(node, enabledProfiles);
|
||||
}).toList();
|
||||
@@ -107,9 +109,12 @@ class NodeProviderWithCache extends ChangeNotifier {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Check if a node matches a specific profile (all profile tags must match)
|
||||
/// Check if a node matches a specific profile (all non-empty profile tags must match)
|
||||
bool _nodeMatchesProfile(OsmNode node, NodeProfile profile) {
|
||||
for (final entry in profile.tags.entries) {
|
||||
// Skip empty values - they are used for refinement UI, not matching
|
||||
if (entry.value.trim().isEmpty) continue;
|
||||
|
||||
if (node.tags[entry.key] != entry.value) return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
227
lib/widgets/nsi_tag_value_field.dart
Normal file
227
lib/widgets/nsi_tag_value_field.dart
Normal file
@@ -0,0 +1,227 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../services/nsi_service.dart';
|
||||
|
||||
/// A text field that provides NSI suggestions for OSM tag values
|
||||
class NSITagValueField extends StatefulWidget {
|
||||
const NSITagValueField({
|
||||
super.key,
|
||||
required this.tagKey,
|
||||
required this.initialValue,
|
||||
required this.onChanged,
|
||||
this.readOnly = false,
|
||||
this.hintText,
|
||||
});
|
||||
|
||||
final String tagKey;
|
||||
final String initialValue;
|
||||
final ValueChanged<String> onChanged;
|
||||
final bool readOnly;
|
||||
final String? hintText;
|
||||
|
||||
@override
|
||||
State<NSITagValueField> createState() => _NSITagValueFieldState();
|
||||
}
|
||||
|
||||
class _NSITagValueFieldState extends State<NSITagValueField> {
|
||||
late TextEditingController _controller;
|
||||
List<String> _suggestions = [];
|
||||
bool _showingSuggestions = false;
|
||||
final LayerLink _layerLink = LayerLink();
|
||||
late OverlayEntry _overlayEntry;
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = TextEditingController(text: widget.initialValue);
|
||||
_loadSuggestions();
|
||||
|
||||
_focusNode.addListener(_onFocusChanged);
|
||||
_controller.addListener(_onTextChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(NSITagValueField oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
|
||||
// If the tag key changed, reload suggestions
|
||||
if (oldWidget.tagKey != widget.tagKey) {
|
||||
_hideSuggestions(); // Hide old suggestions immediately
|
||||
_suggestions.clear();
|
||||
_loadSuggestions(); // Load new suggestions for new key
|
||||
}
|
||||
|
||||
// If the initial value changed, update the controller
|
||||
if (oldWidget.initialValue != widget.initialValue) {
|
||||
_controller.text = widget.initialValue;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
_focusNode.dispose();
|
||||
_hideSuggestions();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Get filtered suggestions based on current text input (case-sensitive)
|
||||
List<String> _getFilteredSuggestions() {
|
||||
final currentText = _controller.text;
|
||||
if (currentText.isEmpty) {
|
||||
return _suggestions;
|
||||
}
|
||||
|
||||
return _suggestions
|
||||
.where((suggestion) => suggestion.contains(currentText))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Handle text changes to update suggestion filtering
|
||||
void _onTextChanged() {
|
||||
if (_showingSuggestions) {
|
||||
// Update the overlay with filtered suggestions
|
||||
_updateSuggestionsOverlay();
|
||||
}
|
||||
}
|
||||
|
||||
void _loadSuggestions() async {
|
||||
if (widget.tagKey.trim().isEmpty) return;
|
||||
|
||||
try {
|
||||
final suggestions = await NSIService().getAllSuggestions(widget.tagKey);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_suggestions = suggestions.take(10).toList(); // Limit to 10 suggestions
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently fail - field still works as regular text field
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_suggestions = [];
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _onFocusChanged() {
|
||||
final filteredSuggestions = _getFilteredSuggestions();
|
||||
if (_focusNode.hasFocus && filteredSuggestions.isNotEmpty && !widget.readOnly) {
|
||||
_showSuggestions();
|
||||
} else {
|
||||
_hideSuggestions();
|
||||
}
|
||||
}
|
||||
|
||||
void _showSuggestions() {
|
||||
final filteredSuggestions = _getFilteredSuggestions();
|
||||
if (_showingSuggestions || filteredSuggestions.isEmpty) return;
|
||||
|
||||
_overlayEntry = _buildSuggestionsOverlay(filteredSuggestions);
|
||||
Overlay.of(context).insert(_overlayEntry);
|
||||
setState(() {
|
||||
_showingSuggestions = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// Update the suggestions overlay with current filtered suggestions
|
||||
void _updateSuggestionsOverlay() {
|
||||
final filteredSuggestions = _getFilteredSuggestions();
|
||||
|
||||
if (filteredSuggestions.isEmpty) {
|
||||
_hideSuggestions();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_showingSuggestions) {
|
||||
// Remove current overlay and create new one with filtered suggestions
|
||||
_overlayEntry.remove();
|
||||
_overlayEntry = _buildSuggestionsOverlay(filteredSuggestions);
|
||||
Overlay.of(context).insert(_overlayEntry);
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the suggestions overlay with the given suggestions list
|
||||
OverlayEntry _buildSuggestionsOverlay(List<String> suggestions) {
|
||||
return OverlayEntry(
|
||||
builder: (context) => Positioned(
|
||||
width: 250, // Slightly wider to fit more content in refine tags
|
||||
child: CompositedTransformFollower(
|
||||
link: _layerLink,
|
||||
showWhenUnlinked: false,
|
||||
offset: const Offset(0.0, 35.0), // Below the text field
|
||||
child: Material(
|
||||
elevation: 4.0,
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 200),
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.zero,
|
||||
shrinkWrap: true,
|
||||
itemCount: suggestions.length,
|
||||
itemBuilder: (context, index) {
|
||||
final suggestion = suggestions[index];
|
||||
return ListTile(
|
||||
dense: true,
|
||||
title: Text(suggestion, style: const TextStyle(fontSize: 14)),
|
||||
onTap: () => _selectSuggestion(suggestion),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _hideSuggestions() {
|
||||
if (!_showingSuggestions) return;
|
||||
|
||||
_overlayEntry.remove();
|
||||
setState(() {
|
||||
_showingSuggestions = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _selectSuggestion(String suggestion) {
|
||||
_controller.text = suggestion;
|
||||
widget.onChanged(suggestion);
|
||||
_hideSuggestions();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final filteredSuggestions = _getFilteredSuggestions();
|
||||
|
||||
return CompositedTransformTarget(
|
||||
link: _layerLink,
|
||||
child: TextField(
|
||||
controller: _controller,
|
||||
focusNode: _focusNode,
|
||||
readOnly: widget.readOnly,
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.hintText,
|
||||
border: const OutlineInputBorder(),
|
||||
isDense: true,
|
||||
suffixIcon: _suggestions.isNotEmpty && !widget.readOnly
|
||||
? Icon(
|
||||
Icons.arrow_drop_down,
|
||||
color: _showingSuggestions ? Theme.of(context).primaryColor : Colors.grey,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
onChanged: widget.readOnly ? null : (value) {
|
||||
widget.onChanged(value);
|
||||
},
|
||||
onTap: () {
|
||||
if (!widget.readOnly && filteredSuggestions.isNotEmpty) {
|
||||
_showSuggestions();
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
107
lib/widgets/nuclear_reset_dialog.dart
Normal file
107
lib/widgets/nuclear_reset_dialog.dart
Normal file
@@ -0,0 +1,107 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import '../services/nuclear_reset_service.dart';
|
||||
|
||||
/// Non-dismissible error dialog shown when migrations fail and nuclear reset is triggered.
|
||||
/// Forces user to restart the app by making it impossible to close this dialog.
|
||||
class NuclearResetDialog extends StatelessWidget {
|
||||
final String errorReport;
|
||||
|
||||
const NuclearResetDialog({
|
||||
Key? key,
|
||||
required this.errorReport,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
// Prevent back button from closing dialog
|
||||
onWillPop: () async => false,
|
||||
child: AlertDialog(
|
||||
title: const Row(
|
||||
children: [
|
||||
Icon(Icons.warning, color: Colors.red),
|
||||
SizedBox(width: 8),
|
||||
Text('Migration Error'),
|
||||
],
|
||||
),
|
||||
content: const Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Unfortunately we encountered an issue during the app update and had to clear your settings and data.',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
Text(
|
||||
'You will need to:',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
Text('• Log back into OpenStreetMap'),
|
||||
Text('• Recreate any custom profiles'),
|
||||
Text('• Re-download any offline areas'),
|
||||
SizedBox(height: 12),
|
||||
Text(
|
||||
'Please close and restart the app to continue.',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton.icon(
|
||||
onPressed: () => _copyErrorToClipboard(),
|
||||
icon: const Icon(Icons.copy),
|
||||
label: const Text('Copy Error'),
|
||||
),
|
||||
TextButton.icon(
|
||||
onPressed: () => _sendErrorToSupport(),
|
||||
icon: const Icon(Icons.email),
|
||||
label: const Text('Send to Support'),
|
||||
),
|
||||
],
|
||||
// No dismiss button - forces user to restart app
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _copyErrorToClipboard() async {
|
||||
await NuclearResetService.copyToClipboard(errorReport);
|
||||
}
|
||||
|
||||
Future<void> _sendErrorToSupport() async {
|
||||
const supportEmail = 'app@deflock.me';
|
||||
const subject = 'DeFlock App Migration Error Report';
|
||||
|
||||
// Create mailto URL with pre-filled error report
|
||||
final body = Uri.encodeComponent(errorReport);
|
||||
final mailtoUrl = 'mailto:$supportEmail?subject=${Uri.encodeComponent(subject)}&body=$body';
|
||||
|
||||
try {
|
||||
final uri = Uri.parse(mailtoUrl);
|
||||
if (await canLaunchUrl(uri)) {
|
||||
await launchUrl(uri);
|
||||
}
|
||||
} catch (e) {
|
||||
// If email fails, just copy to clipboard as fallback
|
||||
await _copyErrorToClipboard();
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the nuclear reset dialog (non-dismissible)
|
||||
static Future<void> show(BuildContext context, Object error, StackTrace? stackTrace) async {
|
||||
// Generate error report
|
||||
final errorReport = await NuclearResetService.generateErrorReport(error, stackTrace);
|
||||
|
||||
// Clear all app data
|
||||
await NuclearResetService.clearEverything();
|
||||
|
||||
// Show non-dismissible dialog
|
||||
await showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false, // Prevent tap-outside to dismiss
|
||||
builder: (context) => NuclearResetDialog(errorReport: errorReport),
|
||||
);
|
||||
}
|
||||
}
|
||||
92
lib/widgets/positioning_tutorial_overlay.dart
Normal file
92
lib/widgets/positioning_tutorial_overlay.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
import 'dart:ui';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../dev_config.dart';
|
||||
import '../services/localization_service.dart';
|
||||
|
||||
/// Overlay that appears over add/edit node sheets to guide users through
|
||||
/// the positioning tutorial. Shows a blurred background with tutorial text.
|
||||
class PositioningTutorialOverlay extends StatelessWidget {
|
||||
const PositioningTutorialOverlay({
|
||||
super.key,
|
||||
this.onFadeOutComplete,
|
||||
});
|
||||
|
||||
/// Called when the fade-out animation completes (if animated)
|
||||
final VoidCallback? onFadeOutComplete;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AnimatedBuilder(
|
||||
animation: LocalizationService.instance,
|
||||
builder: (context, child) {
|
||||
final locService = LocalizationService.instance;
|
||||
|
||||
return ClipRect(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(
|
||||
sigmaX: kPositioningTutorialBlurSigma,
|
||||
sigmaY: kPositioningTutorialBlurSigma,
|
||||
),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withOpacity(0.3), // Semi-transparent overlay
|
||||
),
|
||||
child: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Tutorial icon
|
||||
Icon(
|
||||
Icons.pan_tool_outlined,
|
||||
size: 48,
|
||||
color: Colors.white,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Tutorial title
|
||||
Text(
|
||||
locService.t('positioningTutorial.title'),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Tutorial instructions
|
||||
Text(
|
||||
locService.t('positioningTutorial.instructions'),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// Additional hint
|
||||
Text(
|
||||
locService.t('positioningTutorial.hint'),
|
||||
style: TextStyle(
|
||||
color: Colors.white.withOpacity(0.8),
|
||||
fontSize: 14,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,32 @@ import 'package:provider/provider.dart';
|
||||
|
||||
import '../app_state.dart';
|
||||
import '../models/operator_profile.dart';
|
||||
import '../models/node_profile.dart';
|
||||
import '../services/localization_service.dart';
|
||||
import 'nsi_tag_value_field.dart';
|
||||
|
||||
/// Result returned from RefineTagsSheet
|
||||
class RefineTagsResult {
|
||||
final OperatorProfile? operatorProfile;
|
||||
final Map<String, String> refinedTags;
|
||||
|
||||
RefineTagsResult({
|
||||
required this.operatorProfile,
|
||||
required this.refinedTags,
|
||||
});
|
||||
}
|
||||
|
||||
class RefineTagsSheet extends StatefulWidget {
|
||||
const RefineTagsSheet({
|
||||
super.key,
|
||||
this.selectedOperatorProfile,
|
||||
this.selectedProfile,
|
||||
this.currentRefinedTags,
|
||||
});
|
||||
|
||||
final OperatorProfile? selectedOperatorProfile;
|
||||
final NodeProfile? selectedProfile;
|
||||
final Map<String, String>? currentRefinedTags;
|
||||
|
||||
@override
|
||||
State<RefineTagsSheet> createState() => _RefineTagsSheetState();
|
||||
@@ -19,11 +36,23 @@ class RefineTagsSheet extends StatefulWidget {
|
||||
|
||||
class _RefineTagsSheetState extends State<RefineTagsSheet> {
|
||||
OperatorProfile? _selectedOperatorProfile;
|
||||
Map<String, String> _refinedTags = {};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_selectedOperatorProfile = widget.selectedOperatorProfile;
|
||||
_refinedTags = Map<String, String>.from(widget.currentRefinedTags ?? {});
|
||||
}
|
||||
|
||||
/// Get list of tag keys that have empty values and can be refined
|
||||
List<String> _getRefinableTags() {
|
||||
if (widget.selectedProfile == null) return [];
|
||||
|
||||
return widget.selectedProfile!.tags.entries
|
||||
.where((entry) => entry.value.trim().isEmpty)
|
||||
.map((entry) => entry.key)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -37,11 +66,17 @@ class _RefineTagsSheetState extends State<RefineTagsSheet> {
|
||||
title: Text(locService.t('refineTagsSheet.title')),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () => Navigator.pop(context, widget.selectedOperatorProfile),
|
||||
onPressed: () => Navigator.pop(context, RefineTagsResult(
|
||||
operatorProfile: widget.selectedOperatorProfile,
|
||||
refinedTags: widget.currentRefinedTags ?? {},
|
||||
)),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, _selectedOperatorProfile),
|
||||
onPressed: () => Navigator.pop(context, RefineTagsResult(
|
||||
operatorProfile: _selectedOperatorProfile,
|
||||
refinedTags: _refinedTags,
|
||||
)),
|
||||
child: Text(locService.t('refineTagsSheet.done')),
|
||||
),
|
||||
],
|
||||
@@ -152,6 +187,75 @@ class _RefineTagsSheetState extends State<RefineTagsSheet> {
|
||||
),
|
||||
],
|
||||
],
|
||||
// Add refineable tags section
|
||||
..._buildRefinableTagsSection(locService),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Build the section for refineable tags (empty-value profile tags)
|
||||
List<Widget> _buildRefinableTagsSection(LocalizationService locService) {
|
||||
final refinableTags = _getRefinableTags();
|
||||
if (refinableTags.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
locService.t('refineTagsSheet.profileTags'),
|
||||
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
locService.t('refineTagsSheet.profileTagsDescription'),
|
||||
style: const TextStyle(color: Colors.grey, fontSize: 14),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
...refinableTags.map((tagKey) => _buildTagDropdown(tagKey, locService)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/// Build a text field for a single refineable tag (similar to profile editor)
|
||||
Widget _buildTagDropdown(String tagKey, LocalizationService locService) {
|
||||
final currentValue = _refinedTags[tagKey] ?? '';
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
tagKey,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
NSITagValueField(
|
||||
key: ValueKey('${tagKey}_refine'),
|
||||
tagKey: tagKey,
|
||||
initialValue: currentValue,
|
||||
hintText: locService.t('refineTagsSheet.selectValue'),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
if (value.trim().isEmpty) {
|
||||
_refinedTags.remove(tagKey);
|
||||
} else {
|
||||
_refinedTags[tagKey] = value.trim();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -29,6 +29,8 @@ class SheetAwareMap extends StatelessWidget {
|
||||
// Use the actual available height from constraints, not full screen height
|
||||
final availableHeight = constraints.maxHeight;
|
||||
|
||||
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
AnimatedPositioned(
|
||||
|
||||
@@ -50,7 +50,7 @@ class _SubmissionGuideDialogState extends State<SubmissionGuideDialog> {
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pop(true); // Return true to indicate "proceed with submission"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +148,13 @@ class _SubmissionGuideDialogState extends State<SubmissionGuideDialog> {
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// Cancel - just close dialog without marking as seen, return false to cancel submission
|
||||
Navigator.of(context).pop(false);
|
||||
},
|
||||
child: Text(locService.cancel),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: _onClose,
|
||||
child: Text(locService.t('submissionGuide.gotIt')),
|
||||
|
||||
50
pubspec.lock
50
pubspec.lock
@@ -484,7 +484,7 @@ packages:
|
||||
source: hosted
|
||||
version: "3.2.1"
|
||||
path:
|
||||
dependency: transitive
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: path
|
||||
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
|
||||
@@ -680,6 +680,46 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "7.0.0"
|
||||
sqflite:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: sqflite
|
||||
sha256: e2297b1da52f127bc7a3da11439985d9b536f75070f3325e62ada69a5c585d03
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.2"
|
||||
sqflite_android:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sqflite_android
|
||||
sha256: "2b3070c5fa881839f8b402ee4a39c1b4d561704d4ebbbcfb808a119bc2a1701b"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.1"
|
||||
sqflite_common:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sqflite_common
|
||||
sha256: "6ef422a4525ecc601db6c0a2233ff448c731307906e92cabc9ba292afaae16a6"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.5.6"
|
||||
sqflite_darwin:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sqflite_darwin
|
||||
sha256: "279832e5cde3fe99e8571879498c9211f3ca6391b0d818df4e17d9fff5c6ccb3"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.2"
|
||||
sqflite_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sqflite_platform_interface
|
||||
sha256: "8dd4515c7bdcae0a785b0062859336de775e8c65db81ae33dd5445f35be61920"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.0"
|
||||
string_scanner:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -688,6 +728,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.1"
|
||||
synchronized:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: synchronized
|
||||
sha256: c254ade258ec8282947a0acbbc90b9575b4f19673533ee46f2f6e9b3aeefd7c0
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.4.0"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
name: deflockapp
|
||||
description: Map public surveillance infrastructure with OpenStreetMap
|
||||
publish_to: "none"
|
||||
version: 1.6.1+27 # The thing after the + is the version code, incremented with each release
|
||||
version: 2.3.0+38 # The thing after the + is the version code, incremented with each release
|
||||
|
||||
environment:
|
||||
sdk: ">=3.5.0 <4.0.0" # oauth2_client 4.x needs Dart 3.5+
|
||||
@@ -30,6 +30,8 @@ dependencies:
|
||||
|
||||
# Persistence
|
||||
shared_preferences: ^2.2.2
|
||||
sqflite: ^2.4.1
|
||||
path: ^1.8.3
|
||||
uuid: ^4.0.0
|
||||
package_info_plus: ^8.0.0
|
||||
csv: ^6.0.0
|
||||
|
||||
Reference in New Issue
Block a user