@@ -490,8 +490,22 @@ class AppStateProvider extends ChangeNotifier {
490490 // Listen to GPS changes
491491 debugLog ('[INIT] Setting up GPS status listener...' );
492492 _gpsService.statusStream.listen ((status) {
493- debugLog ( '[GPS] Status changed: $ _gpsStatus → $ status ' ) ;
493+ final previousStatus = _gpsStatus;
494494 _gpsStatus = status;
495+ debugLog ('[GPS] Status changed: $previousStatus → $status ' );
496+ debugLog ('[GPS] Post-change state: inZone=$_inZone , isCheckingZone=$_isCheckingZone , '
497+ 'hasInternet=$_hasInternet , hasPosition=${_currentPosition != null }' );
498+
499+ // Log when we transition to locked state (permission granted + GPS available)
500+ if (status == GpsStatus .locked && previousStatus != GpsStatus .locked) {
501+ debugLog ('[GPS] GPS lock acquired - zone check should trigger on first position' );
502+ }
503+ // Log when permission is denied or GPS disabled
504+ if (status == GpsStatus .permissionDenied) {
505+ debugLog ('[GPS] Location permission denied - zone checks will be blocked' );
506+ } else if (status == GpsStatus .disabled) {
507+ debugLog ('[GPS] Location services disabled - zone checks will be blocked' );
508+ }
495509 notifyListeners ();
496510 });
497511 _gpsStatus = _gpsService.status; // Sync initial status
@@ -502,16 +516,23 @@ class AppStateProvider extends ChangeNotifier {
502516 _currentPosition = position;
503517 notifyListeners ();
504518
505- // Diagnostic: log every position received and zone check conditions
506- debugLog ('[GPS] Position received in AppState: ${position .latitude .toStringAsFixed (5 )}, ${position .longitude .toStringAsFixed (5 )}' );
519+ // Comprehensive diagnostic logging for debugging user issues
520+ debugLog ('[GPS] Position received: ${position .latitude .toStringAsFixed (5 )}, ${position .longitude .toStringAsFixed (5 )} '
521+ '(accuracy: ${position .accuracy .toStringAsFixed (1 )}m)' );
522+ debugLog ('[GPS] Current state: inZone=$_inZone , isCheckingZone=$_isCheckingZone , '
523+ 'hasInternet=$_hasInternet , offlineMode=${_preferences .offlineMode }, gpsStatus=$_gpsStatus ' );
507524
508525 // Check zone on first GPS lock (when _inZone is null)
509526 // Skip zone checks when offline mode is enabled
510527 if (_inZone == null && ! _preferences.offlineMode) {
511- debugLog ('[GEOFENCE] First GPS lock, checking zone status' );
512- await checkZoneStatus ();
513- } else if (_inZone == null ) {
514- debugLog ('[GEOFENCE] Zone check skipped: offlineMode=${_preferences .offlineMode }' );
528+ if (! _hasInternet) {
529+ debugLog ('[GEOFENCE] First GPS lock but no internet - zone check will wait for connectivity' );
530+ } else {
531+ debugLog ('[GEOFENCE] First GPS lock with internet, triggering zone check' );
532+ await checkZoneStatus ();
533+ }
534+ } else if (_inZone == null && _preferences.offlineMode) {
535+ debugLog ('[GEOFENCE] First GPS lock skipped: offline mode enabled' );
515536 }
516537
517538 // Check zone every 100m movement (while disconnected)
@@ -543,40 +564,64 @@ class AppStateProvider extends ChangeNotifier {
543564 _connectivityService = ConnectivityService ();
544565 await _connectivityService.initialize ();
545566 _connectivityService.internetStream.listen ((hasInternet) async {
567+ final previousState = _hasInternet;
546568 _hasInternet = hasInternet;
569+ debugLog ('[CONNECTIVITY] Internet status changed: $previousState → $hasInternet ' );
570+ debugLog ('[CONNECTIVITY] Current state: inZone=$_inZone , isCheckingZone=$_isCheckingZone , '
571+ 'gpsStatus=$_gpsStatus , hasPosition=${_currentPosition != null }, offlineMode=${_preferences .offlineMode }' );
547572 notifyListeners ();
548573
549574 // Re-check zone when internet becomes available
550575 if (hasInternet && _inZone == null && ! _preferences.offlineMode) {
551- debugLog ('[CONNECTIVITY] Internet restored, rechecking zone' );
576+ debugLog ('[CONNECTIVITY] Internet restored and zone unknown, will attempt zone check ' );
552577
553578 // If we have GPS position, check zone immediately
554579 if (_currentPosition != null ) {
580+ debugLog ('[CONNECTIVITY] GPS position available, triggering zone check' );
555581 checkZoneStatus ();
556582 } else {
557583 // GPS not providing positions - try to restart it
558584 debugLog ('[CONNECTIVITY] No GPS position available, restarting GPS service' );
559585 await _gpsService.startWatching ();
586+ debugLog ('[CONNECTIVITY] GPS service restarted, status: ${_gpsService .status }' );
560587 // Zone check will be triggered by GPS position listener when position arrives
561588 }
589+ } else if (! hasInternet) {
590+ debugLog ('[CONNECTIVITY] Internet lost - zone checks will be blocked until restored' );
562591 }
563592 });
564593 _hasInternet = _connectivityService.hasInternet;
594+ debugLog ('[INIT] Initial internet status: $_hasInternet ' );
565595
566596 // Initialize audio service for sound notifications
567597 await _audioService.initialize ();
568598
569599 debugLog ('[INIT] AppStateProvider initialization complete' );
600+ debugLog ('[INIT] Final init state: gpsStatus=$_gpsStatus , hasInternet=$_hasInternet , '
601+ 'inZone=$_inZone , isCheckingZone=$_isCheckingZone , hasPosition=${_currentPosition != null }, '
602+ 'offlineMode=${_preferences .offlineMode }' );
570603 notifyListeners ();
571604 }
572605
573606 /// Restart GPS service after permission disclosure is accepted
574607 /// Called from MainScaffold after user grants location permission
575608 Future <void > restartGpsAfterPermission () async {
576- debugLog ('[APP] Restarting GPS after permission granted' );
609+ debugLog ('[GPS] restartGpsAfterPermission() called' );
610+ debugLog ('[GPS] Pre-restart state: gpsStatus=$_gpsStatus , inZone=$_inZone , '
611+ 'isCheckingZone=$_isCheckingZone , hasInternet=$_hasInternet , hasPosition=${_currentPosition != null }' );
612+
577613 await _gpsService.startWatching ();
578614 _gpsStatus = _gpsService.status; // Sync after restart
579- debugLog ('[APP] GPS restarted, status: $_gpsStatus ' );
615+
616+ debugLog ('[GPS] GPS restarted, new status: $_gpsStatus ' );
617+ debugLog ('[GPS] Post-restart state: inZone=$_inZone , isCheckingZone=$_isCheckingZone , '
618+ 'hasInternet=$_hasInternet , hasPosition=${_currentPosition != null }' );
619+
620+ // If we now have a position and zone hasn't been checked, trigger check
621+ if (_currentPosition != null && _inZone == null && _hasInternet && ! _preferences.offlineMode) {
622+ debugLog ('[GPS] Permission granted with existing position - triggering zone check' );
623+ await checkZoneStatus ();
624+ }
580625 notifyListeners ();
581626 }
582627
@@ -881,6 +926,9 @@ class AppStateProvider extends ChangeNotifier {
881926 return _preferences.autoPowerSet || _preferences.powerLevelSet || _deviceModel != null ;
882927 };
883928
929+ // Get external antenna value for API payloads
930+ _pingService! .getExternalAntenna = () => _preferences.externalAntenna;
931+
884932 // Check if TX is allowed by API (zone capacity)
885933 _pingService! .checkTxAllowed = () => txAllowed;
886934
@@ -1261,6 +1309,7 @@ class AppStateProvider extends ChangeNotifier {
12611309 heardRepeats: heardRepeats,
12621310 timestamp: entry.timestamp.millisecondsSinceEpoch ~ / 1000 ,
12631311 repeaterId: entry.repeaterId,
1312+ externalAntenna: _preferences.externalAntenna,
12641313 noiseFloor: _meshCoreConnection? .lastNoiseFloor,
12651314 );
12661315
@@ -2167,27 +2216,33 @@ class AppStateProvider extends ChangeNotifier {
21672216 /// Check zone status via API
21682217 /// Should be called on app launch and every 100m of GPS movement while disconnected
21692218 Future <void > checkZoneStatus () async {
2219+ debugLog ('[GEOFENCE] checkZoneStatus() called' );
2220+ debugLog ('[GEOFENCE] Pre-check state: inZone=$_inZone , isCheckingZone=$_isCheckingZone , '
2221+ 'hasInternet=$_hasInternet , hasPosition=${_currentPosition != null }, gpsStatus=$_gpsStatus ' );
2222+
21702223 // Skip if no internet (avoids stuck "Checking..." state)
21712224 if (! _hasInternet) {
2172- debugLog ('[GEOFENCE] Skipping zone check: no internet' );
2225+ debugLog ('[GEOFENCE] Skipping zone check: no internet (hasInternet=$ _hasInternet ) ' );
21732226 return ;
21742227 }
21752228
21762229 if (_currentPosition == null ) {
2177- debugLog ('[GEOFENCE] Cannot check zone status: no GPS position' );
2230+ debugLog ('[GEOFENCE] Cannot check zone status: no GPS position (gpsStatus=$ _gpsStatus ) ' );
21782231 return ;
21792232 }
21802233
21812234 if (_isCheckingZone) {
2182- debugLog ('[GEOFENCE] Zone check already in progress' );
2235+ debugLog ('[GEOFENCE] Zone check already in progress, skipping duplicate call ' );
21832236 return ;
21842237 }
21852238
2239+ debugLog ('[GEOFENCE] Starting zone check - setting isCheckingZone=true (previous inZone=$_inZone )' );
21862240 _isCheckingZone = true ;
21872241 notifyListeners ();
21882242
21892243 try {
2190- debugLog ('[GEOFENCE] Checking zone status at ${_currentPosition !.latitude .toStringAsFixed (5 )}, ${_currentPosition !.longitude .toStringAsFixed (5 )}' );
2244+ debugLog ('[GEOFENCE] Making API call to check zone at ${_currentPosition !.latitude .toStringAsFixed (5 )}, '
2245+ '${_currentPosition !.longitude .toStringAsFixed (5 )} (accuracy: ${_currentPosition !.accuracy .toStringAsFixed (1 )}m)' );
21912246
21922247 final result = await _apiService.checkZoneStatus (
21932248 lat: _currentPosition! .latitude,
@@ -2196,8 +2251,10 @@ class AppStateProvider extends ChangeNotifier {
21962251 appVersion: _appVersion,
21972252 );
21982253
2254+ debugLog ('[GEOFENCE] API response received: ${result != null ? 'valid' : 'null' }' );
2255+
21992256 if (result == null ) {
2200- debugError ('[GEOFENCE] Zone status check failed: no response' );
2257+ debugError ('[GEOFENCE] Zone status check failed: no response from API ' );
22012258 return ;
22022259 }
22032260
@@ -2255,6 +2312,8 @@ class AppStateProvider extends ChangeNotifier {
22552312 debugError ('[GEOFENCE] Zone status check error: $e ' );
22562313 } finally {
22572314 _isCheckingZone = false ;
2315+ debugLog ('[GEOFENCE] Zone check complete - final state: inZone=$_inZone , isCheckingZone=$_isCheckingZone , '
2316+ 'zoneName=${_currentZone ?['name' ]}, zoneCode=${_currentZone ?['code' ]}' );
22582317 notifyListeners ();
22592318 }
22602319 }
0 commit comments