mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
feat: add recipe management support
- Add `LocalRecipeEntity` and database migration 3->4 - Update `LocalCustomFoodEntity` with category support - Include DAO methods for recipe persistence
This commit is contained in:
1 parent
097d4b521e
commit
d4d5d77d71
41 files changed
+5264
-1901
No files matched your search
@@ -2,17 +2,25 @@ package com.example.util
|
||||
|
||||
import android.accounts.Account
|
||||
import android.content.Context
|
||||
import com.example.R
|
||||
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 com.google.firebase.Firebase
|
||||
import com.google.firebase.auth.auth
|
||||
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 {
|
||||
|
||||
@@ -25,31 +33,30 @@ object GoogleSheetsAuthHelper {
|
||||
const val OAUTH_SCOPE_STRING = "oauth2:$SCOPE_SPREADSHEETS_URL $SCOPE_DRIVE_FILE_URL"
|
||||
|
||||
fun getGoogleSignInClient(context: Context): GoogleSignInClient {
|
||||
val serverClientId = try {
|
||||
context.getString(R.string.default_web_client_id)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
val gsoBuilder = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||
.requestEmail()
|
||||
.requestScopes(SCOPE_SPREADSHEETS, SCOPE_DRIVE_FILE)
|
||||
.build()
|
||||
|
||||
if (!serverClientId.isNullOrBlank()) {
|
||||
gsoBuilder.requestIdToken(serverClientId)
|
||||
}
|
||||
return GoogleSignIn.getClient(context, gso)
|
||||
}
|
||||
|
||||
return GoogleSignIn.getClient(context, gsoBuilder.build())
|
||||
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 = Firebase.auth.currentUser?.email
|
||||
?: GoogleSignIn.getLastSignedInAccount(context)?.email
|
||||
val email = GoogleSignIn.getLastSignedInAccount(context)?.email
|
||||
|
||||
if (email.isNullOrBlank()) {
|
||||
return@withContext Result.failure(
|
||||
IllegalStateException("Aucun compte Google connecté. Veuillez vous connecter pour synchroniser avec Google Sheets.")
|
||||
IllegalStateException("Aucun compte Google connecté. Connectez votre compte pour sauvegarder sur Google Drive.")
|
||||
)
|
||||
}
|
||||
|
||||
@@ -69,4 +76,20 @@ object GoogleSheetsAuthHelper {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user