Created
May 29, 2024 06:40
-
-
Save prongbang/99c15b2d3880d70016bcadbcc68036cd to your computer and use it in GitHub Desktop.
This file contains 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
import 'package:google_api_availability/google_api_availability.dart'; | |
enum ServicesAvailability { | |
success, | |
serviceMissing, | |
serviceUpdating, | |
serviceVersionUpdateRequired, | |
serviceDisabled, | |
serviceInvalid, | |
notAvailableOnPlatform, | |
unknown, | |
} | |
abstract class FirebaseServiceUtility { | |
Future<ServicesAvailability> checkServiceAvailability(); | |
} | |
/// How to use: | |
/// final firebaseServiceUtility = GoogleFirebaseServiceUtility(..) | |
/// final result = await firebaseServiceUtility.checkServiceAvailability(); | |
/// switch (result) { | |
/// case ServicesAvailability.success: | |
/// case ServicesAvailability.serviceUpdating: | |
/// case ServicesAvailability.serviceVersionUpdateRequired: | |
/// yield* _processCheckNewVersionUpdate(); | |
/// break; | |
/// default: | |
/// // Support cases | |
/// // - ServicesAvailability.serviceMissing | |
/// // - ServicesAvailability.serviceInvalid | |
/// // - ServicesAvailability.notAvailableOnPlatform | |
/// } | |
class GoogleFirebaseServiceUtility implements FirebaseServiceUtility { | |
final GoogleApiAvailability _googleApiAvailability; | |
GoogleFirebaseServiceUtility(this._googleApiAvailability); | |
@override | |
Future<ServicesAvailability> checkServiceAvailability() async { | |
try { | |
final result = await _googleApiAvailability | |
.checkGooglePlayServicesAvailability(false); | |
switch (result) { | |
case GooglePlayServicesAvailability.success: | |
return ServicesAvailability.success; | |
case GooglePlayServicesAvailability.serviceMissing: | |
return ServicesAvailability.serviceMissing; | |
case GooglePlayServicesAvailability.serviceUpdating: | |
return ServicesAvailability.serviceUpdating; | |
case GooglePlayServicesAvailability.serviceVersionUpdateRequired: | |
return ServicesAvailability.serviceVersionUpdateRequired; | |
case GooglePlayServicesAvailability.serviceDisabled: | |
return ServicesAvailability.serviceDisabled; | |
case GooglePlayServicesAvailability.serviceInvalid: | |
return ServicesAvailability.serviceInvalid; | |
case GooglePlayServicesAvailability.notAvailableOnPlatform: | |
// TODO Note that on iOS this will always return notAvailableOnPlatform! | |
return ServicesAvailability.notAvailableOnPlatform; | |
default: | |
return ServicesAvailability.unknown; | |
} | |
} catch (e) { | |
return ServicesAvailability.unknown; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment