Handle Jorge's remarks.

This commit is contained in:
Benoit Marty 2024-10-30 16:34:47 +01:00
parent a54eee042b
commit 9d8d960a98
3 changed files with 40 additions and 44 deletions

View file

@ -17,37 +17,32 @@ import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
interface FirebaseTokenDeleter { interface FirebaseTokenDeleter {
/**
* Deletes the current Firebase token.
*/
suspend fun delete() suspend fun delete()
} }
/**
* This class deletes the current Firebase token.
*/
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultFirebaseTokenDeleter @Inject constructor( class DefaultFirebaseTokenDeleter @Inject constructor(
private val isPlayServiceAvailable: IsPlayServiceAvailable, private val isPlayServiceAvailable: IsPlayServiceAvailable,
) : FirebaseTokenDeleter { ) : FirebaseTokenDeleter {
override suspend fun delete() { override suspend fun delete() {
// 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
isPlayServiceAvailable.checkAvailableOrThrow()
suspendCoroutine { continuation -> suspendCoroutine { continuation ->
// 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' try {
if (isPlayServiceAvailable.isAvailable()) { FirebaseMessaging.getInstance().deleteToken()
try { .addOnSuccessListener {
FirebaseMessaging.getInstance().deleteToken() continuation.resume(Unit)
.addOnSuccessListener { }
continuation.resume(Unit) .addOnFailureListener { e ->
} Timber.e(e, "## deleteFirebaseToken() : failed")
.addOnFailureListener { e -> continuation.resumeWithException(e)
Timber.e(e, "## deleteFirebaseToken() : failed") }
continuation.resumeWithException(e) } catch (e: Throwable) {
} Timber.e(e, "## deleteFirebaseToken() : failed")
} catch (e: Throwable) { continuation.resumeWithException(e)
Timber.e(e, "## deleteFirebaseToken() : failed")
continuation.resumeWithException(e)
}
} else {
val e = Exception("No valid Google Play Services found. Cannot use FCM.")
Timber.e(e)
throw e
} }
} }
} }

View file

@ -17,37 +17,32 @@ import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
interface FirebaseTokenGetter { interface FirebaseTokenGetter {
/**
* Read the current Firebase token from FirebaseMessaging.
* If the token does not exist, it will be generated.
*/
suspend fun get(): String suspend fun get(): String
} }
/**
* This class read the current Firebase token.
* If the token does not exist, it will be generated.
*/
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultFirebaseTokenGetter @Inject constructor( class DefaultFirebaseTokenGetter @Inject constructor(
private val isPlayServiceAvailable: IsPlayServiceAvailable, private val isPlayServiceAvailable: IsPlayServiceAvailable,
) : FirebaseTokenGetter { ) : FirebaseTokenGetter {
override suspend fun get(): String { override suspend fun get(): String {
// 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
isPlayServiceAvailable.checkAvailableOrThrow()
return suspendCoroutine { continuation -> return suspendCoroutine { continuation ->
// 'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' try {
if (isPlayServiceAvailable.isAvailable()) { FirebaseMessaging.getInstance().token
try { .addOnSuccessListener { token ->
FirebaseMessaging.getInstance().token continuation.resume(token)
.addOnSuccessListener { token -> }
continuation.resume(token) .addOnFailureListener { e ->
} Timber.e(e, "## retrievedFirebaseToken() : failed")
.addOnFailureListener { e -> continuation.resumeWithException(e)
Timber.e(e, "## retrievedFirebaseToken() : failed") }
continuation.resumeWithException(e) } catch (e: Throwable) {
} Timber.e(e, "## retrievedFirebaseToken() : failed")
} catch (e: Throwable) {
Timber.e(e, "## retrievedFirebaseToken() : failed")
continuation.resumeWithException(e)
}
} else {
val e = Exception("No valid Google Play Services found. Cannot use FCM.")
Timber.e(e)
continuation.resumeWithException(e) continuation.resumeWithException(e)
} }
} }

View file

@ -20,6 +20,12 @@ interface IsPlayServiceAvailable {
fun isAvailable(): Boolean fun isAvailable(): Boolean
} }
fun IsPlayServiceAvailable.checkAvailableOrThrow() {
if (!isAvailable()) {
throw Exception("No valid Google Play Services found. Cannot use FCM.").also(Timber::e)
}
}
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultIsPlayServiceAvailable @Inject constructor( class DefaultIsPlayServiceAvailable @Inject constructor(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,