mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
- Add `LocalRecipeEntity` and database migration 3->4 - Update `LocalCustomFoodEntity` with category support - Include DAO methods for recipe persistence
96 lines
3.4 KiB
Kotlin
96 lines
3.4 KiB
Kotlin
package com.example.util
|
|
|
|
import android.accounts.Account
|
|
import android.content.Context
|
|
import com.google.android.gms.auth.GoogleAuthUtil
|
|
import com.google.android.gms.auth.UserRecoverableAuthException
|
|
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
|
import com.google.android.gms.auth.api.signin.GoogleSignInClient
|
|
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
|
|
import com.google.android.gms.common.api.Scope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
import kotlinx.coroutines.withContext
|
|
import kotlin.coroutines.resume
|
|
|
|
/**
|
|
* Lightweight representation of the Google account connected solely for
|
|
* personal Google Drive and Google Sheets backup/restore.
|
|
*/
|
|
data class GoogleDriveAccount(
|
|
val displayName: String?,
|
|
val email: String
|
|
)
|
|
|
|
object GoogleSheetsAuthHelper {
|
|
|
|
const val SCOPE_SPREADSHEETS_URL = "https://www.googleapis.com/auth/spreadsheets"
|
|
const val SCOPE_DRIVE_FILE_URL = "https://www.googleapis.com/auth/drive.file"
|
|
|
|
val SCOPE_SPREADSHEETS = Scope(SCOPE_SPREADSHEETS_URL)
|
|
val SCOPE_DRIVE_FILE = Scope(SCOPE_DRIVE_FILE_URL)
|
|
|
|
const val OAUTH_SCOPE_STRING = "oauth2:$SCOPE_SPREADSHEETS_URL $SCOPE_DRIVE_FILE_URL"
|
|
|
|
fun getGoogleSignInClient(context: Context): GoogleSignInClient {
|
|
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
|
.requestEmail()
|
|
.requestScopes(SCOPE_SPREADSHEETS, SCOPE_DRIVE_FILE)
|
|
.build()
|
|
|
|
return GoogleSignIn.getClient(context, gso)
|
|
}
|
|
|
|
fun getSignedInAccount(context: Context): GoogleDriveAccount? {
|
|
val account = GoogleSignIn.getLastSignedInAccount(context) ?: return null
|
|
val email = account.email?.takeIf { it.isNotBlank() } ?: return null
|
|
return GoogleDriveAccount(
|
|
displayName = account.displayName?.takeIf { it.isNotBlank() },
|
|
email = email
|
|
)
|
|
}
|
|
|
|
suspend fun getAccessToken(context: Context): Result<String> = withContext(Dispatchers.IO) {
|
|
try {
|
|
val email = GoogleSignIn.getLastSignedInAccount(context)?.email
|
|
|
|
if (email.isNullOrBlank()) {
|
|
return@withContext Result.failure(
|
|
IllegalStateException("Aucun compte Google connecté. Connectez votre compte pour sauvegarder sur Google Drive.")
|
|
)
|
|
}
|
|
|
|
val account = Account(email, "com.google")
|
|
val token = GoogleAuthUtil.getToken(context, account, OAUTH_SCOPE_STRING)
|
|
Result.success(token)
|
|
} catch (e: UserRecoverableAuthException) {
|
|
// Must be handled by prompting user via e.intent
|
|
Result.failure(e)
|
|
} catch (e: Exception) {
|
|
Result.failure(e)
|
|
}
|
|
}
|
|
|
|
fun invalidateToken(context: Context, token: String) {
|
|
try {
|
|
GoogleAuthUtil.clearToken(context.applicationContext, token)
|
|
} catch (_: Exception) {}
|
|
}
|
|
|
|
suspend fun signOut(context: Context) {
|
|
suspendCancellableCoroutine<Unit> { continuation ->
|
|
try {
|
|
getGoogleSignInClient(context).signOut().addOnCompleteListener {
|
|
if (continuation.isActive) {
|
|
continuation.resume(Unit)
|
|
}
|
|
}
|
|
} catch (_: Exception) {
|
|
if (continuation.isActive) {
|
|
continuation.resume(Unit)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|