import 'package:aves/model/device.dart'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:google_api_availability/google_api_availability.dart'; abstract class AvesAvailability { void onResume(); Future get isConnected; Future get hasPlayServices; Future get canLocatePlaces; Future get canUseGoogleMaps; } class LiveAvesAvailability implements AvesAvailability { bool? _isConnected, _hasPlayServices; LiveAvesAvailability() { Connectivity().onConnectivityChanged.listen(_updateConnectivityFromResult); } @override void onResume() => _isConnected = null; @override Future get isConnected async { if (_isConnected != null) return SynchronousFuture(_isConnected!); final result = await (Connectivity().checkConnectivity()); _updateConnectivityFromResult(result); return _isConnected!; } void _updateConnectivityFromResult(ConnectivityResult result) { final newValue = result != ConnectivityResult.none; if (_isConnected != newValue) { _isConnected = newValue; debugPrint('Device is connected=$_isConnected'); } } @override Future get hasPlayServices async { if (_hasPlayServices != null) return SynchronousFuture(_hasPlayServices!); final result = await GoogleApiAvailability.instance.checkGooglePlayServicesAvailability(); _hasPlayServices = result == GooglePlayServicesAvailability.success; debugPrint('Device has Play Services=$_hasPlayServices'); return _hasPlayServices!; } // local geocoding with `geocoder` requires Play Services @override Future get canLocatePlaces => Future.wait([isConnected, hasPlayServices]).then((results) => results.every((result) => result)); @override Future get canUseGoogleMaps async => device.canRenderGoogleMaps && await hasPlayServices; }