Repository navigation
Show user profile and dining barcode on Account page #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
36e7594
4650b80
cf5077b
e09a1a8
6056a76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.cornellappdev.android.eatery.data.models | ||
|
|
||
| import android.graphics.BitmapFactory | ||
| import android.util.Base64 | ||
| import androidx.compose.ui.graphics.ImageBitmap | ||
| import androidx.compose.ui.graphics.asImageBitmap | ||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
|
|
||
| /** | ||
| * Basic identity info for the logged-in user, displayed on the Account page. | ||
| * Any field may be null when the backend doesn't provide it. | ||
| * | ||
| * Held in memory only: the backend asks that profile data not be cached or logged. | ||
| */ | ||
| data class UserProfile( | ||
| val name: String? = null, | ||
| val email: String? = null, | ||
| val photo: ImageBitmap? = null, | ||
| ) | ||
|
|
||
| /** Response body of `POST /users/get-profile`. Every field may be null. */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class UserProfileResponse( | ||
| @Json(name = "firstName") val firstName: String? = null, | ||
| @Json(name = "middleName") val middleName: String? = null, | ||
| @Json(name = "lastName") val lastName: String? = null, | ||
| @Json(name = "email") val email: String? = null, | ||
| @Json(name = "photo") val photo: ProfilePhoto? = null, | ||
| ) | ||
|
|
||
| /** A profile photo as base64-encoded image bytes of type [mimeType] (e.g. `image/jpeg`). */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class ProfilePhoto( | ||
| @Json(name = "mimeType") val mimeType: String? = null, | ||
| @Json(name = "data") val data: String? = null, | ||
| ) | ||
|
|
||
| /** | ||
| * Converts the backend response into a [UserProfile], joining the name parts and decoding | ||
| * the photo. A photo that fails to decode is dropped rather than failing the whole profile. | ||
| */ | ||
| fun UserProfileResponse.toUserProfile(): UserProfile = UserProfile( | ||
| name = listOf(firstName, middleName, lastName) | ||
| .filterNot { it.isNullOrBlank() } | ||
| .joinToString(" ") | ||
| .ifEmpty { null }, | ||
| email = email?.takeIf { it.isNotBlank() }, | ||
| photo = photo?.toImageBitmap(), | ||
| ) | ||
|
|
||
| private fun ProfilePhoto.toImageBitmap(): ImageBitmap? { | ||
| if (mimeType?.startsWith("image/") == false) return null | ||
| val bytes = runCatching { Base64.decode(data ?: return null, Base64.DEFAULT) } | ||
| .getOrNull() ?: return null | ||
| return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)?.asImageBitmap() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,13 +10,20 @@ import com.cornellappdev.android.eatery.data.models.Result | |
| import com.cornellappdev.android.eatery.data.models.SessionID | ||
| import com.cornellappdev.android.eatery.data.models.Transaction | ||
| import com.cornellappdev.android.eatery.data.models.User | ||
| import com.cornellappdev.android.eatery.data.models.UserProfile | ||
| import com.cornellappdev.android.eatery.data.models.toTransactionAccountType | ||
| import com.cornellappdev.android.eatery.data.models.toUserProfile | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.firstOrNull | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import kotlinx.coroutines.withContext | ||
| import retrofit2.HttpException | ||
| import java.time.LocalDateTime | ||
| import java.time.ZonedDateTime | ||
| import java.time.format.DateTimeFormatter | ||
|
|
@@ -55,6 +62,17 @@ class UserRepository @Inject constructor( | |
| /** Expose whether a user is currently logged in. Delegates to UserPreferencesRepository. */ | ||
| val isLoggedInFlow: Flow<Boolean> = userPreferencesRepository.isLoggedInFlow | ||
|
|
||
| private val _userProfile: MutableStateFlow<UserProfile> = MutableStateFlow(UserProfile()) | ||
|
|
||
| /** | ||
| * The logged-in user's name, email, and profile picture. Kept in memory only, since the | ||
| * backend asks that profile data not be cached. | ||
| */ | ||
| val userProfileFlow: StateFlow<UserProfile> = _userProfile.asStateFlow() | ||
|
|
||
| /** Makes [getProfile]'s session check and write atomic with [logout]. */ | ||
| private val profileMutex = Mutex() | ||
|
|
||
| private val useLocalFavorites = BuildConfig.USE_LOCAL_FAVORITES | ||
|
|
||
| suspend fun updateFavorites(): Result<Unit> { | ||
|
|
@@ -205,6 +223,37 @@ class UserRepository @Inject constructor( | |
| financials | ||
| } | ||
|
|
||
| /** | ||
| * Fetches the user's name, email, and photo. If the GET session has expired (401), gets a | ||
| * new session with the saved PIN and tries once more, like [getFinancials]. | ||
| * | ||
| * The profile is dropped if the user logged out or linked another account while the request | ||
| * was in flight, so a late response can't show the old account's profile, like | ||
| * [GETAccountRepository.refreshBarcodeData]. | ||
| */ | ||
| suspend fun getProfile(): Result<UserProfile> = resultOfNetworkCall { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this to return anything if we already have the flow? Maybe change function name and return type so it's more of an update function.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^^ |
||
| var sessionId = getAccountRepository.getSessionId() | ||
| val response = try { | ||
| networkApi.getProfile(sessionId = SessionID(sessionId)) | ||
| } catch (e: HttpException) { | ||
| if (e.code() != 401) throw e | ||
| val pin = getAccountRepository.getPin() ?: throw e | ||
| val refreshResult = getAccountRepository.refreshLogin(pin = pin) | ||
| if (refreshResult is Result.Error) throw IllegalStateException("refreshLogin failed") | ||
|
Comment on lines
+240
to
+242
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this logic seems repeated. can we refactor it out
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also caleb's comment here is still open, could u pull the 401 retry into a shared helper that getFinancials and getProfile both use? |
||
| sessionId = getAccountRepository.getSessionId() | ||
| networkApi.getProfile(sessionId = SessionID(sessionId)) | ||
| } | ||
| val profile = withContext(Dispatchers.Default) { response.toUserProfile() } | ||
| // Under the lock, logout either already cleared the session, so the check fails, or | ||
| // hasn't started, so it resets the profile after this write. | ||
| profileMutex.withLock { | ||
| if (!sessionId.isNullOrEmpty() && getAccountRepository.getSessionId() == sessionId) { | ||
| _userProfile.value = profile | ||
| } | ||
| } | ||
| profile | ||
| } | ||
|
|
||
| private fun String.toLocalDateTime(): LocalDateTime? { | ||
| val inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX") | ||
| return runCatching { | ||
|
|
@@ -215,11 +264,12 @@ class UserRepository @Inject constructor( | |
| }.getOrNull() | ||
| } | ||
|
|
||
| suspend fun logout() { | ||
| suspend fun logout() = profileMutex.withLock { | ||
| _loadedUser.value = null | ||
| getAccountRepository.clearSessionId() | ||
| getAccountRepository.clearBarcodeData() | ||
| getAccountRepository.setIsLoggedIn(false) | ||
| _userProfile.value = UserProfile() | ||
| } | ||
|
|
||
| suspend fun hasOnboarded(): Boolean = | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.