Skip to content

Commit e966f45

Browse files
committed
Fixed MapController after MVVM Refactor
1 parent 8d86e2d commit e966f45

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

lib/screens/map/map_view.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class _MapScreenState extends State<MapScreen> with TickerProviderStateMixin {
3131
@override
3232
Widget build(BuildContext context) {
3333
return ChangeNotifierProvider(
34-
create: (_) => MapViewModel()..initialize(),
34+
create: (_) => MapViewModel()..initialize(context, this),
3535
child: Consumer2<MapProvider, TrackingProvider>(
3636
builder: (context, mapProvider, trackingProvider, _) {
3737
return Consumer<MapViewModel>(
@@ -110,7 +110,7 @@ class _MapScreenState extends State<MapScreen> with TickerProviderStateMixin {
110110
_routeWaySwitcher(context),
111111

112112
// Update location button
113-
_updateLocationButton(context),
113+
_updateLocationButtons(context),
114114

115115
if (mapProvider.loadingProgress < 1)
116116
_loadingOverlay(context, mapProvider)
@@ -121,7 +121,7 @@ class _MapScreenState extends State<MapScreen> with TickerProviderStateMixin {
121121
);
122122
}
123123

124-
Widget _updateLocationButton(BuildContext context) {
124+
Widget _updateLocationButtons(BuildContext context) {
125125
final mapProvider = Provider.of<MapProvider>(context, listen: false);
126126
final trackingProvider =
127127
Provider.of<TrackingProvider>(context, listen: false);
@@ -132,16 +132,19 @@ class _MapScreenState extends State<MapScreen> with TickerProviderStateMixin {
132132
padding: const EdgeInsets.all(16.0),
133133
child: Row(
134134
mainAxisSize: MainAxisSize.min,
135+
spacing: 10,
135136
children: [
136137
if (trackingProvider.currentLocation != null)
138+
// Move to Bus
137139
FloatingActionButton(
138140
mini: true,
139141
onPressed: () {
140142
mapProvider.updateLocation(
141143
trackingProvider.currentLocation!, 16);
142144
},
143145
child: const Icon(Icons.directions_bus)),
144-
const SizedBox(width: 10),
146+
147+
// Move to Current Location
145148
FloatingActionButton(
146149
onPressed: () async =>
147150
mapProvider.moveToCurrentLocation(context),

lib/screens/map/map_viewmodel.dart

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
import 'dart:async';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_map_animations/flutter_map_animations.dart';
34
import 'package:mallorca_transit_services/mallorca_transit_services.dart';
5+
import 'package:provider/provider.dart';
46
import 'package:via_mallorca/cache/cache_manager.dart';
7+
import 'package:via_mallorca/providers/map_provider.dart';
58

69
class MapViewModel extends ChangeNotifier with WidgetsBindingObserver {
710
final bool _havePermission = false;
811
bool get havePermission => _havePermission;
912
List<Station> _cachedStations = [];
1013
List<Station> get cachedStations => _cachedStations;
1114

12-
Future<void> initialize() async {
13-
WidgetsBinding.instance.addObserver(this);
15+
Future<void> initialize(BuildContext context, TickerProvider vsync) async {
16+
context
17+
.read<MapProvider>()
18+
.setMapController(AnimatedMapController(vsync: vsync));
1419
_cachedStations = await CacheManager.getAllStations();
1520
if (_cachedStations.isEmpty) {
1621
_cachedStations = await Station.getAllStations();
1722
await CacheManager.setAllStations(_cachedStations);
1823
}
19-
notifyListeners();
20-
}
21-
22-
@override
23-
void didChangeAppLifecycleState(AppLifecycleState state) {
24-
if (state == AppLifecycleState.resumed) {
25-
// Resume tracking
26-
} else if (state == AppLifecycleState.paused) {
27-
// Pause tracking
28-
}
29-
}
3024

31-
@override
32-
void dispose() {
33-
WidgetsBinding.instance.removeObserver(this);
34-
super.dispose();
25+
notifyListeners();
3526
}
3627
}

0 commit comments

Comments
 (0)