Last active
October 26, 2020 16:50
-
-
Save gaetschwartz/31c8597bdc4db046122f34f79171842f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final _fetcher = Fetcher(); | |
final _futureRouteProvider = ChangeNotifierProvider<Fetcher>((ref) { | |
_fetcher.fetch(ref); | |
return _fetcher; | |
}); | |
class Fetcher extends ChangeNotifier { | |
RouteStates _state = const RouteStates.empty(); | |
RouteStates get state => _state; | |
set state(RouteStates state) { | |
_state = state; | |
notifyListeners(); | |
} | |
Future<void> fetch(ProviderReference ref) async { | |
final from = ref.watch(_fromTextfieldProvider).state; | |
final to = ref.watch(_toTextfieldProvider).state; | |
final _cff = ref.read(navigationAPIProvider); | |
final date = ref.watch(_dateProvider).state; | |
final timeType = ref.watch(_timeTypeProvider).state; | |
if (from is RouteTextfieldStateEmpty || to is RouteTextfieldStateEmpty) { | |
return; | |
} | |
state = const RouteStates.loading(); | |
final departure = from.when( | |
empty: () => null, text: (t) => t, currentLocation: (loc, lat, lon) => "$lat,$lon"); | |
final arrival = | |
to.when(empty: () => null, text: (t) => t, currentLocation: (loc, lat, lon) => "$lat,$lon"); | |
log("Fetching route from $departure to $arrival"); | |
try { | |
final CffRoute it = await _cff.route( | |
departure, | |
arrival, | |
date: date, | |
time: TimeOfDay.fromDateTime(date), | |
typeTime: timeType, | |
); | |
state = RouteStates.routes(it); | |
} on SocketException { | |
state = const RouteStates.network(); | |
} on Exception catch (e, s) { | |
state = RouteStates.exception(e); | |
report(e, s, name: "Fetch"); | |
// ignore: avoid_catching_errors | |
} on Error catch (e) { | |
state = RouteStates.exception(e); | |
report(e, e.stackTrace, name: "Fetch"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment