Skip to content

Commit 89c55f6

Browse files
committed
fix: add macOS location debug logs + NSLocationWhenInUseUsageDescription
Debug prints at each step of location flow: - [bitchat] Location service enabled: true/false - [bitchat] Location permission: whileInUse/denied/... - [bitchat] GPS location: lat, lon (or) - [bitchat] GPS failed: <error>, falling back to IP Also added NSLocationWhenInUseUsageDescription to macOS Info.plist (some geolocator versions need this specific key). Run macOS with: flutter run -d macos Check console for [bitchat] logs to diagnose.
1 parent 409fe88 commit 89c55f6

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

lib/ui/home_screen.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,36 +103,45 @@ class _HomeScreenState extends State<HomeScreen> {
103103

104104
(double, double)? _cachedLocation;
105105

106-
/// Get device location — GPS on iOS/Android, IP fallback on macOS.
106+
/// Get device location — GPS on iOS/Android/macOS, IP fallback.
107107
Future<(double, double)> _fetchLocation() async {
108108
if (_cachedLocation != null) return _cachedLocation!;
109109

110-
// Try GPS first (matches original bitchat behavior)
110+
// Try GPS/CoreLocation first (matches original bitchat behavior)
111111
try {
112112
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
113+
debugPrint('[bitchat] Location service enabled: $serviceEnabled');
113114
if (serviceEnabled) {
114115
LocationPermission perm = await Geolocator.checkPermission();
116+
debugPrint('[bitchat] Location permission: $perm');
115117
if (perm == LocationPermission.denied) {
116118
perm = await Geolocator.requestPermission();
119+
debugPrint('[bitchat] After request: $perm');
117120
}
118121
if (perm == LocationPermission.whileInUse ||
119122
perm == LocationPermission.always) {
120123
final pos = await Geolocator.getCurrentPosition(
121124
locationSettings: const LocationSettings(
122-
accuracy:
123-
LocationAccuracy.low, // City-level is enough for geohash
125+
accuracy: LocationAccuracy.low,
124126
timeLimit: Duration(seconds: 10),
125127
),
126128
);
129+
debugPrint(
130+
'[bitchat] GPS location: ${pos.latitude}, ${pos.longitude}',
131+
);
127132
_cachedLocation = (pos.latitude, pos.longitude);
128133
return _cachedLocation!;
134+
} else {
135+
debugPrint(
136+
'[bitchat] Location permission denied, falling back to IP',
137+
);
129138
}
130139
}
131-
} catch (_) {
132-
// GPS unavailable (e.g. macOS desktop), fall through to IP
140+
} catch (e) {
141+
debugPrint('[bitchat] GPS failed: $e, falling back to IP');
133142
}
134143

135-
// IP-based fallback for macOS/desktop
144+
// IP-based fallback
136145
try {
137146
final uri = Uri.parse('https://ipapi.co/json/');
138147
final client = HttpClient();
@@ -143,14 +152,15 @@ class _HomeScreenState extends State<HomeScreen> {
143152
final response = await request.close();
144153
final body = await response.transform(utf8.decoder).join();
145154
final json = jsonDecode(body) as Map<String, dynamic>;
146-
_cachedLocation = (
147-
(json['latitude'] as num).toDouble(),
148-
(json['longitude'] as num).toDouble(),
149-
);
155+
final lat = (json['latitude'] as num).toDouble();
156+
final lon = (json['longitude'] as num).toDouble();
157+
debugPrint('[bitchat] IP location: $lat, $lon');
158+
_cachedLocation = (lat, lon);
150159
client.close();
151160
return _cachedLocation!;
152-
} catch (_) {
153-
return (39.9, -77.0); // Final fallback
161+
} catch (e) {
162+
debugPrint('[bitchat] IP fallback also failed: $e');
163+
return (39.9, -77.0);
154164
}
155165
}
156166

macos/Runner/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@
3232
<string>BitChat uses Bluetooth to discover and communicate with nearby peers in the mesh network.</string>
3333
<key>NSLocationUsageDescription</key>
3434
<string>BitChat uses your location to join nearby geohash channels and connect with peers in your area.</string>
35+
<key>NSLocationWhenInUseUsageDescription</key>
36+
<string>BitChat uses your location to join nearby geohash channels and connect with peers in your area.</string>
3537
</dict>
3638
</plist>

0 commit comments

Comments
 (0)