diff --git a/app/src/main/java/com/erica/gamsung/login/data/remote/LoginApi.kt b/app/src/main/java/com/erica/gamsung/login/data/remote/LoginApi.kt index 3bbec6d..b972484 100644 --- a/app/src/main/java/com/erica/gamsung/login/data/remote/LoginApi.kt +++ b/app/src/main/java/com/erica/gamsung/login/data/remote/LoginApi.kt @@ -17,6 +17,6 @@ interface LoginApi { } data class TokenResponse( - val providerId: Long, + val id: Long, val accessToken: String, ) diff --git a/app/src/main/java/com/erica/gamsung/login/data/repository/LoginRepositoryImpl.kt b/app/src/main/java/com/erica/gamsung/login/data/repository/LoginRepositoryImpl.kt index 57fb485..d33cec0 100644 --- a/app/src/main/java/com/erica/gamsung/login/data/repository/LoginRepositoryImpl.kt +++ b/app/src/main/java/com/erica/gamsung/login/data/repository/LoginRepositoryImpl.kt @@ -3,6 +3,7 @@ package com.erica.gamsung.login.data.repository import android.content.Context import android.content.SharedPreferences import com.erica.gamsung.login.data.remote.LoginApi +import com.erica.gamsung.login.data.remote.TokenResponse import com.erica.gamsung.login.domain.LoginRepository import com.erica.gamsung.menu.data.remote.MenuApi import com.erica.gamsung.store.data.remote.StoreApi @@ -19,9 +20,9 @@ class LoginRepositoryImpl( private val sharedPreferences: SharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE) override suspend fun fetchAccessToken(uuid: String): String { - val accessToken = loginApi.getToken(uuid).accessToken - saveToken(accessToken) - return accessToken + val tokenResponse = loginApi.getToken(uuid) + saveToken(tokenResponse) + return tokenResponse.accessToken } override fun getSavedAccessToken(): String? = sharedPreferences.getString("access_token", null) @@ -40,8 +41,9 @@ class LoginRepositoryImpl( menuResult.await() && storeResult.await() } - private fun saveToken(token: String) { - sharedPreferences.edit().putString("access_token", token).apply() + private fun saveToken(token: TokenResponse) { + sharedPreferences.edit().putString("access_token", token.accessToken).apply() + sharedPreferences.edit().putLong("memberId", token.id).apply() } override fun clearSession() { @@ -54,4 +56,6 @@ class LoginRepositoryImpl( loginApi.deleteMember(bearerToken) } } + + override fun getMemberId(): Long = sharedPreferences.getLong("memberId", 1L) } diff --git a/app/src/main/java/com/erica/gamsung/login/domain/LoginRepository.kt b/app/src/main/java/com/erica/gamsung/login/domain/LoginRepository.kt index 5b07a11..5aee991 100644 --- a/app/src/main/java/com/erica/gamsung/login/domain/LoginRepository.kt +++ b/app/src/main/java/com/erica/gamsung/login/domain/LoginRepository.kt @@ -14,4 +14,6 @@ interface LoginRepository { fun clearSession() suspend fun withDraw() + + fun getMemberId(): Long } diff --git a/app/src/main/java/com/erica/gamsung/menu/data/repository/FakeMenuRepositoryImpl.kt b/app/src/main/java/com/erica/gamsung/menu/data/repository/FakeMenuRepositoryImpl.kt index ca6776c..5ea2eeb 100644 --- a/app/src/main/java/com/erica/gamsung/menu/data/repository/FakeMenuRepositoryImpl.kt +++ b/app/src/main/java/com/erica/gamsung/menu/data/repository/FakeMenuRepositoryImpl.kt @@ -8,9 +8,12 @@ import kotlinx.coroutines.flow.flowOf class FakeMenuRepositoryImpl : MenuRepository { private var db = listOf() - override suspend fun updateMenus(menus: List) { + override suspend fun updateMenus( + menus: List, + userId: Long, + ) { db = menus } - override suspend fun getMenus(): Flow> = flowOf(db) + override suspend fun getMenus(userId: Long): Flow> = flowOf(db) } diff --git a/app/src/main/java/com/erica/gamsung/menu/data/repository/MenuRepositoryImpl.kt b/app/src/main/java/com/erica/gamsung/menu/data/repository/MenuRepositoryImpl.kt index c71f61a..77c4924 100644 --- a/app/src/main/java/com/erica/gamsung/menu/data/repository/MenuRepositoryImpl.kt +++ b/app/src/main/java/com/erica/gamsung/menu/data/repository/MenuRepositoryImpl.kt @@ -13,7 +13,10 @@ class MenuRepositoryImpl( private val menuDao: MenuDao, private val menuApi: MenuApi, ) : MenuRepository { - override suspend fun updateMenus(menus: List) { + override suspend fun updateMenus( + menus: List, + userId: Long, + ) { // 1. 서버에 메뉴 수정 요청 val updatedMenus = menuApi.updateMenus( @@ -31,7 +34,7 @@ class MenuRepositoryImpl( ) } - override suspend fun getMenus(): Flow> = + override suspend fun getMenus(userId: Long): Flow> = flow { // 1. 로컬 DB에 있는 데이터 반환 val localData = menuDao.getAll().map { it.toDomainModel() } @@ -39,7 +42,7 @@ class MenuRepositoryImpl( // 2. 서버에서 받은 데이터 동기화 및 반환 runCatching { - menuApi.getMenus() + menuApi.getMenus(userId) }.onSuccess { remoteData -> menuDao.updateAll(remoteData.map { it.toMenuEntity() }) emit(remoteData.map { it.toDomainModel() }) diff --git a/app/src/main/java/com/erica/gamsung/menu/domain/MenuRepository.kt b/app/src/main/java/com/erica/gamsung/menu/domain/MenuRepository.kt index c594b3f..bf4bed6 100644 --- a/app/src/main/java/com/erica/gamsung/menu/domain/MenuRepository.kt +++ b/app/src/main/java/com/erica/gamsung/menu/domain/MenuRepository.kt @@ -3,7 +3,10 @@ package com.erica.gamsung.menu.domain import kotlinx.coroutines.flow.Flow interface MenuRepository { - suspend fun updateMenus(menus: List) + suspend fun updateMenus( + menus: List, + userId: Long, + ) - suspend fun getMenus(): Flow> + suspend fun getMenus(userId: Long): Flow> } diff --git a/app/src/main/java/com/erica/gamsung/menu/presentation/MenuViewModel.kt b/app/src/main/java/com/erica/gamsung/menu/presentation/MenuViewModel.kt index 010a62c..20400e9 100644 --- a/app/src/main/java/com/erica/gamsung/menu/presentation/MenuViewModel.kt +++ b/app/src/main/java/com/erica/gamsung/menu/presentation/MenuViewModel.kt @@ -3,6 +3,7 @@ package com.erica.gamsung.menu.presentation import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.erica.gamsung.login.domain.LoginRepository import com.erica.gamsung.menu.data.repository.FakeMenuRepositoryImpl import com.erica.gamsung.menu.domain.Menu import com.erica.gamsung.menu.domain.MenuRepository @@ -21,6 +22,7 @@ class MenuViewModel constructor( state: SavedStateHandle, private val menuRepository: MenuRepository = FakeMenuRepositoryImpl(), + private val loginRepository: LoginRepository, ) : ViewModel() { private var _menusState = MutableStateFlow(state.get>(MENUS) ?: emptyList()) val menusState = _menusState.asStateFlow() @@ -36,7 +38,7 @@ class MenuViewModel private fun loadMenus() { viewModelScope.launch(Dispatchers.IO) { - menuRepository.getMenus().collect { menus -> + menuRepository.getMenus(loginRepository.getMemberId()).collect { menus -> _menusState.update { menus } } } @@ -113,7 +115,7 @@ class MenuViewModel } } else { CoroutineScope(Dispatchers.IO).launch { - menuRepository.updateMenus(menus) + menuRepository.updateMenus(menus, loginRepository.getMemberId()) } } } diff --git a/app/src/main/java/com/erica/gamsung/post/data/remote/PostApi.kt b/app/src/main/java/com/erica/gamsung/post/data/remote/PostApi.kt index e7660fa..113e61f 100644 --- a/app/src/main/java/com/erica/gamsung/post/data/remote/PostApi.kt +++ b/app/src/main/java/com/erica/gamsung/post/data/remote/PostApi.kt @@ -18,8 +18,10 @@ interface PostApi { @Path("reservationId") reservationId: Int, ): Response - @GET("postings/state") - suspend fun fetchPostListData(): Response> + @GET("postings/{id}/state") + suspend fun fetchPostListData( + @Path("id") memberId: Long, + ): Response> @POST("postings/post/{reservationId}") suspend fun confirmPostData( diff --git a/app/src/main/java/com/erica/gamsung/post/presentation/PostViewModel.kt b/app/src/main/java/com/erica/gamsung/post/presentation/PostViewModel.kt index 04bfefd..1d9dca6 100644 --- a/app/src/main/java/com/erica/gamsung/post/presentation/PostViewModel.kt +++ b/app/src/main/java/com/erica/gamsung/post/presentation/PostViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.erica.gamsung.login.domain.LoginRepository import com.erica.gamsung.post.data.mock.Post import com.erica.gamsung.post.data.remote.PostApi import com.erica.gamsung.post.domain.Schedule @@ -26,6 +27,7 @@ class PostViewModel @Inject constructor( private val postApi: PostApi, + private val loginRepository: LoginRepository, ) : ViewModel() { private val _contents = MutableLiveData>() val contents: LiveData> = _contents @@ -78,7 +80,7 @@ class PostViewModel fun fetchPostListData() { viewModelScope.launch { try { - val response = postApi.fetchPostListData() + val response = postApi.fetchPostListData(loginRepository.getMemberId()) if (response.isSuccessful) { _postListData.value = response.body() } else {