Skip to content

Commit

Permalink
fix: #8 save lat lng on map move to restore view
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurBeaulieu committed Jul 4, 2024
1 parent 7c850ee commit 9d09162
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/src/map/map_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class MapViewState extends State<MapView> with TickerProviderStateMixin {
late AlignOnUpdate _alignPositionOnUpdate;
late final StreamController<double?> _alignPositionStreamController;
double restoredZoomed = 0; // To restore zoom level when unlocking position
// Widget internal utils
Timer? _debounce; // o debounce the saving of initial lat/lng on map move
// InitState main purpose is to async load spots/shops/bars
@override
void initState() {
Expand Down Expand Up @@ -321,9 +323,9 @@ class MapViewState extends State<MapView> with TickerProviderStateMixin {
body: FlutterMap(
mapController: _mapController,
options: MapOptions(
initialCenter: const LatLng(
48.8605277263,
2.34402407374,
initialCenter: LatLng(
widget.settingsController.initLat,
widget.settingsController.initLng,
),
initialZoom: 11.0,
interactionOptions: const InteractionOptions(
Expand All @@ -346,6 +348,19 @@ class MapViewState extends State<MapView> with TickerProviderStateMixin {
if (hasGesture && _alignPositionOnUpdate != AlignOnUpdate.never) {
setState(() => _alignPositionOnUpdate = AlignOnUpdate.never);
}
// Update center map to initial lat/lng
if (_debounce?.isActive ?? false) _debounce?.cancel();
_debounce = Timer(
const Duration(
milliseconds: 500,
),
() {
widget.settingsController.updateInitialPosition(
position.center.latitude,
position.center.longitude,
);
},
);
},
// Map clicked callback, add marker only if logged in
onTap: (widget.settingsController.isLoggedIn == true)
Expand Down
17 changes: 17 additions & 0 deletions lib/src/settings/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class SettingsController with ChangeNotifier {
late String ppPath;
late bool isUserActive;
late bool isUserStaff;
// Map saved settings
double initLat = 48.8605277263;
double initLng = 2.34402407374;
// Load settings from storage. Required before loading app
Future<void> loadSettings() async {
_themeMode = await _settingsService.themeMode();
Expand All @@ -44,6 +47,8 @@ class SettingsController with ChangeNotifier {
} else {
isLoggedIn = false;
}
initLat = await _settingsService.initialLat();
initLng = await _settingsService.initialLng();
// Finally notify listener that settings are loaded, app can be started
notifyListeners();
}
Expand Down Expand Up @@ -80,6 +85,18 @@ class SettingsController with ChangeNotifier {
await _settingsService.updateShowWelcomeScreen(showWelcomeScreen);
notifyListeners();
}
// Update initial lat/lng position
Future<void> updateInitialPosition(
double lat,
double lng,
) async {
if (lat < -90 || lat > 90) return;
if (lng < -180 || lng > 180) return;
initLat = lat;
initLng = lng;
await _settingsService.updateInitialPosition(lat, lng);
notifyListeners();
}

/* Auth related methods */

Expand Down
43 changes: 43 additions & 0 deletions lib/src/settings/settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ class SettingsService {
}
}
}
// Returns initial stored lat position
Future<double> initialLat() async {
final SharedPreferences appPreferences = await SharedPreferences.getInstance();
double? value = appPreferences.getDouble('bc-initial-pos-lat');
if (value == null) {
await appPreferences.setDouble(
'bc-initial-pos-lat',
48.8605277263,
);
return 48.8605277263;
} else {
return value;
}
}
// Returns initial stored lng position
Future<double> initialLng() async {
final SharedPreferences appPreferences = await SharedPreferences.getInstance();
double? value = appPreferences.getDouble('bc-initial-pos-lng');
if (value == null) {
await appPreferences.setDouble(
'bc-initial-pos-lng',
2.34402407374,
);
return 2.34402407374;
} else {
return value;
}
}
// Persists the user's preferred ThemeMode to device storage
Future<void> updateThemeMode(
ThemeMode theme,
Expand Down Expand Up @@ -146,6 +174,21 @@ class SettingsService {
);
}
}
// Update the saved initial position to restore map to latest view position on map
Future<void> updateInitialPosition(
double lat,
double lng,
) async {
final SharedPreferences appPreferences = await SharedPreferences.getInstance();
await appPreferences.setDouble(
'bc-initial-pos-lat',
lat,
);
await appPreferences.setDouble(
'bc-initial-pos-lng',
lng,
);
}
// Persists the user's JWT token in device secure storage
Future<void> updateAuthToken(
String expiry,
Expand Down

0 comments on commit 9d09162

Please sign in to comment.