Skip to content
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

Feature/#96 바뀐 발행현황 API에 맞춰 요청한다 #97

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ interface LoginApi {
}

data class TokenResponse(
val providerId: Long,
val id: Long,
val accessToken: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -54,4 +56,6 @@ class LoginRepositoryImpl(
loginApi.deleteMember(bearerToken)
}
}

override fun getMemberId(): Long = sharedPreferences.getLong("memberId", 1L)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface LoginRepository {
fun clearSession()

suspend fun withDraw()

fun getMemberId(): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import kotlinx.coroutines.flow.flowOf
class FakeMenuRepositoryImpl : MenuRepository {
private var db = listOf<Menu>()

override suspend fun updateMenus(menus: List<Menu>) {
override suspend fun updateMenus(
menus: List<Menu>,
userId: Long,
) {
db = menus
}

override suspend fun getMenus(): Flow<List<Menu>> = flowOf(db)
override suspend fun getMenus(userId: Long): Flow<List<Menu>> = flowOf(db)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class MenuRepositoryImpl(
private val menuDao: MenuDao,
private val menuApi: MenuApi,
) : MenuRepository {
override suspend fun updateMenus(menus: List<Menu>) {
override suspend fun updateMenus(
menus: List<Menu>,
userId: Long,
) {
// 1. 서버에 메뉴 수정 요청
val updatedMenus =
menuApi.updateMenus(
Expand All @@ -31,15 +34,15 @@ class MenuRepositoryImpl(
)
}

override suspend fun getMenus(): Flow<List<Menu>> =
override suspend fun getMenus(userId: Long): Flow<List<Menu>> =
flow {
// 1. 로컬 DB에 있는 데이터 반환
val localData = menuDao.getAll().map { it.toDomainModel() }
emit(localData)

// 2. 서버에서 받은 데이터 동기화 및 반환
runCatching {
menuApi.getMenus()
menuApi.getMenus(userId)
}.onSuccess { remoteData ->
menuDao.updateAll(remoteData.map { it.toMenuEntity() })
emit(remoteData.map { it.toDomainModel() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.erica.gamsung.menu.domain
import kotlinx.coroutines.flow.Flow

interface MenuRepository {
suspend fun updateMenus(menus: List<Menu>)
suspend fun updateMenus(
menus: List<Menu>,
userId: Long,
)

suspend fun getMenus(): Flow<List<Menu>>
suspend fun getMenus(userId: Long): Flow<List<Menu>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<List<Menu>>(MENUS) ?: emptyList())
val menusState = _menusState.asStateFlow()
Expand All @@ -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 }
}
}
Expand Down Expand Up @@ -113,7 +115,7 @@ class MenuViewModel
}
} else {
CoroutineScope(Dispatchers.IO).launch {
menuRepository.updateMenus(menus)
menuRepository.updateMenus(menus, loginRepository.getMemberId())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ interface PostApi {
@Path("reservationId") reservationId: Int,
): Response<Schedule>

@GET("postings/state")
suspend fun fetchPostListData(): Response<List<ScheduleState>>
@GET("postings/{id}/state")
suspend fun fetchPostListData(
@Path("id") memberId: Long,
): Response<List<ScheduleState>>

@POST("postings/post/{reservationId}")
suspend fun confirmPostData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,7 @@ class PostViewModel
@Inject
constructor(
private val postApi: PostApi,
private val loginRepository: LoginRepository,
) : ViewModel() {
private val _contents = MutableLiveData<List<String>>()
val contents: LiveData<List<String>> = _contents
Expand Down Expand Up @@ -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 {
Expand Down
Loading