From b920cc47bff67184d821d92153ceb24832e80640 Mon Sep 17 00:00:00 2001 From: lsakee Date: Fri, 24 Jan 2025 01:27:13 +0900 Subject: [PATCH] [refactor]: qa [refactor]: fix --- .../com/record/network/TokenRefreshService.kt | 5 +- .../authenticator/RecordyAuthenticator.kt | 53 +++++++++------ .../java/com/record/mypage/MypageViewModel.kt | 67 +++++++++---------- .../record/mypage/screen/BookmarkScreen.kt | 26 +++---- .../java/com/record/upload/UploadViewModel.kt | 2 + 5 files changed, 82 insertions(+), 71 deletions(-) diff --git a/core/network/src/main/java/com/record/network/TokenRefreshService.kt b/core/network/src/main/java/com/record/network/TokenRefreshService.kt index 086b0da2..7b077d41 100644 --- a/core/network/src/main/java/com/record/network/TokenRefreshService.kt +++ b/core/network/src/main/java/com/record/network/TokenRefreshService.kt @@ -1,6 +1,5 @@ package com.record.network -import com.record.network.model.BaseResponse import com.record.network.model.ResponsePostAuthRefreshDto import retrofit2.http.Header import retrofit2.http.POST @@ -15,6 +14,6 @@ interface TokenRefreshService { @POST("/$API/$VERSION/$USER/$TOKEN") suspend fun postAuthRefresh( - @Header("refreshToken") refreshToken: String, - ): BaseResponse + @Header("Authorization") refreshToken: String, + ): ResponsePostAuthRefreshDto } diff --git a/core/network/src/main/java/com/record/network/authenticator/RecordyAuthenticator.kt b/core/network/src/main/java/com/record/network/authenticator/RecordyAuthenticator.kt index eb72e7b5..fccdd902 100644 --- a/core/network/src/main/java/com/record/network/authenticator/RecordyAuthenticator.kt +++ b/core/network/src/main/java/com/record/network/authenticator/RecordyAuthenticator.kt @@ -7,6 +7,8 @@ import com.record.network.TokenRefreshService import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import okhttp3.Authenticator import okhttp3.Request import okhttp3.Response @@ -18,30 +20,39 @@ class RecordyAuthenticator @Inject constructor( private val tokenRefreshService: TokenRefreshService, @ApplicationContext private val context: Context, ) : Authenticator { + private val mutex = Mutex() + override fun authenticate(route: Route?, response: Response): Request? { - if (response.code == CODE_TOKEN_EXPIRED) { - val newTokens = runCatching { - runBlocking { - tokenRefreshService.postAuthRefresh(dataStore.token.first().refreshToken) - } - }.onSuccess { - runBlocking { - dataStore.apply { - setAccessToken(it.data?.accessToken ?: "") - } + return if (response.code == CODE_TOKEN_EXPIRED) { + runBlocking { + mutex.withLock { + processTokenRefresh(response) } - }.onFailure { - runBlocking { - dataStore.setAutoLogin(false) - } - ProcessPhoenix.triggerRebirth(context) - }.getOrThrow() - - return response.request.newBuilder() - .header("accessToken", newTokens.data?.accessToken ?: "") - .build() + } + } else { + null } - return null + } + + private fun processTokenRefresh(response: Response): Request? { + val newTokens = runCatching { + runBlocking { + tokenRefreshService.postAuthRefresh(dataStore.token.first().refreshToken) + } + }.onSuccess { + runBlocking { + dataStore.setAccessToken(it.accessToken ?: "") + } + }.onFailure { + runBlocking { + dataStore.setAutoLogin(false) + } + ProcessPhoenix.triggerRebirth(context) + }.getOrThrow() + + return response.request.newBuilder() + .header("accessToken", newTokens.accessToken ?: "") + .build() } companion object { diff --git a/feature/mypage/src/main/java/com/record/mypage/MypageViewModel.kt b/feature/mypage/src/main/java/com/record/mypage/MypageViewModel.kt index 5761085e..916a5c8d 100644 --- a/feature/mypage/src/main/java/com/record/mypage/MypageViewModel.kt +++ b/feature/mypage/src/main/java/com/record/mypage/MypageViewModel.kt @@ -9,7 +9,6 @@ import com.record.user.repository.UserRepository import com.record.video.repository.VideoRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.collections.immutable.toImmutableList -import kotlinx.coroutines.async import kotlinx.coroutines.launch import javax.inject.Inject @@ -64,42 +63,40 @@ class MypageViewModel @Inject constructor( } } } - fun initialData() = viewModelScope.launch { - val myVideosResult = async { videoRepository.getMyVideos(0, 10) } - val bookmarkVideosResult = async { videoRepository.getBookmarkVideos(0, 10) } + getInitialMyVideos() + getInitialBookMark() + } - myVideosResult.await() - .onSuccess { myVideo -> - bookmarkVideosResult.await() - .onSuccess { bookmarkVideo -> - Log.e("북마크", bookmarkVideo.data.toString()) - intent { - copy( - myRecordList = myVideo.data.toImmutableList(), - myBookmarkList = bookmarkVideo.data.toImmutableList(), - recordCursor = myVideo.nextCursor?.toLong() ?: 0, - bookmarkCursor = bookmarkVideo.nextCursor?.toLong() ?: 0, - recordIsEnd = false, - bookmarkIsEnd = false, - ) - } - intent { - copy( - recordVideoCount = uiState.value.myRecordList.size, - bookmarkVideoCount = uiState.value.myBookmarkList.size, - ) - } - } - .onFailure { error -> - Log.e("오류 발생ㅇㅇ", error.message.toString()) - } - } - .onFailure { error -> - Log.e("오류 발생ㅇㅇ", error.message.toString()) + private suspend fun getInitialMyVideos() = videoRepository.getMyVideos(0, 10) + .onSuccess { myVideo -> + intent { + copy( + myRecordList = myVideo.data.toImmutableList(), + recordCursor = myVideo.nextCursor?.toLong() ?: 0, + recordIsEnd = false, + recordVideoCount = myVideo.data.size, + ) } - } + } + .onFailure { error -> + Log.e("오류 발생", error.message.toString()) + } + private suspend fun getInitialBookMark() = videoRepository.getBookmarkVideos(0, 10) + .onSuccess { bookmarkVideo -> + intent { + copy( + myBookmarkList = bookmarkVideo.data.toImmutableList(), + bookmarkCursor = bookmarkVideo.nextCursor?.toLong() ?: 0, + bookmarkIsEnd = false, + bookmarkVideoCount = bookmarkVideo.data.size, + ) + } + } + .onFailure { error -> + Log.e("오류 발생", error.message.toString()) + } fun loadMoreUserVideos() = viewModelScope.launch { val list = uiState.value.myRecordList.toList() if (uiState.value.recordIsEnd) return@launch @@ -168,7 +165,6 @@ class MypageViewModel @Inject constructor( video } } - val updatedMyBookmarkList = uiState.value.myBookmarkList.map { video -> if (video.id == id) { video.copy(isBookmark = it) @@ -176,13 +172,14 @@ class MypageViewModel @Inject constructor( video } } - intent { copy( myRecordList = updatedMyRecordList.toImmutableList(), myBookmarkList = updatedMyBookmarkList.toImmutableList(), ) } + + getInitialBookMark() }.onFailure { } } diff --git a/feature/mypage/src/main/java/com/record/mypage/screen/BookmarkScreen.kt b/feature/mypage/src/main/java/com/record/mypage/screen/BookmarkScreen.kt index 4a2b6d52..82da4d72 100644 --- a/feature/mypage/src/main/java/com/record/mypage/screen/BookmarkScreen.kt +++ b/feature/mypage/src/main/java/com/record/mypage/screen/BookmarkScreen.kt @@ -92,18 +92,20 @@ fun BookmarkScreen( } } items(videoItems) { item -> - RecordyVideoThumbnail( - imageUri = item.previewUrl, - isBookmarkable = true, - isBookmark = item.isBookmark, - onBookmarkClick = { - onBookmarkClick(item.id) - }, - location = item.exhibitionName, - onClick = { - onItemClick(VideoType.BOOKMARK, item.id) - }, - ) + if (item.isBookmark) { + RecordyVideoThumbnail( + imageUri = item.previewUrl, + isBookmarkable = true, + isBookmark = item.isBookmark, + onBookmarkClick = { + onBookmarkClick(item.id) + }, + location = item.exhibitionName, + onClick = { + onItemClick(VideoType.BOOKMARK, item.id) + }, + ) + } } } } diff --git a/feature/upload/src/main/java/com/record/upload/UploadViewModel.kt b/feature/upload/src/main/java/com/record/upload/UploadViewModel.kt index b5c9338c..56ab6dba 100644 --- a/feature/upload/src/main/java/com/record/upload/UploadViewModel.kt +++ b/feature/upload/src/main/java/com/record/upload/UploadViewModel.kt @@ -32,6 +32,8 @@ class UploadViewModel @Inject constructor( } } fun setSelectedPlace(id: String, name: String, address: String) { + if (uiState.value.selectPlace.name != name) updateLocationTextField("") + if (id.isNotEmpty() && name.isNotEmpty() && address.isNotEmpty()) { intent { copy(