Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.eatssu.android.R
import com.eatssu.android.data.MySharedPreferences
import com.eatssu.android.domain.repository.UserRepository
import com.eatssu.android.domain.usecase.auth.LogoutUseCase
import com.eatssu.android.domain.usecase.user.GetUserCollegeDepartmentUseCase
Expand Down Expand Up @@ -35,7 +36,11 @@ class MainViewModel @Inject constructor(
@ApplicationContext private val context: Context
) : ViewModel() {

private val _uiState: MutableStateFlow<UiState<MainState>> = MutableStateFlow(UiState.Init)
private val _uiState: MutableStateFlow<UiState<MainState>> = MutableStateFlow(
UiState.Success(
MainState.DepartmentState(MySharedPreferences.getUserDepartmentName(context))
)
)
val uiState: StateFlow<UiState<MainState>> = _uiState.asStateFlow()

private val _uiEvent = MutableSharedFlow<UiEvent>()
Expand Down Expand Up @@ -69,7 +74,6 @@ class MainViewModel @Inject constructor(
}

// 2) 정상 닉네임
_uiState.value = UiState.Success(MainState.NicknameExists(nickname))
_uiEvent.emit(
UiEvent.ShowToast(
String.format(
Expand All @@ -82,7 +86,7 @@ class MainViewModel @Inject constructor(
_uiState.value = UiState.Error
_uiEvent.emit(
UiEvent.ShowToast(
context.getString(R.string.not_found)
context.getString(R.string.not_found)
)
)
Timber.e(e)
Expand Down Expand Up @@ -138,7 +142,6 @@ class MainViewModel @Inject constructor(

sealed class MainState {
object NicknameNull : MainState()
data class NicknameExists(val nickname: String) : MainState()
object LoggedOut : MainState()
data class DepartmentState(
val departmentName: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.eatssu.android.R
import com.eatssu.android.data.MySharedPreferences
import com.eatssu.android.domain.model.RestaurantType
import com.eatssu.android.presentation.MainState
import com.eatssu.android.presentation.MainViewModel
Expand Down Expand Up @@ -103,8 +102,8 @@ fun MapFragmentComposeView(
val scope = rememberCoroutineScope()
var selectedFilter by remember { mutableStateOf(FilterType.All) }

val departmentId = MySharedPreferences.getUserDepartmentId(context).toLong()
val collegeId = MySharedPreferences.getUserCollegeId(context).toLong()
val departmentId = viewModel.departmentId
val collegeId = viewModel.collegeId

val cameraPositionState = rememberCameraPositionState {
position = CameraPosition(
Expand All @@ -128,15 +127,16 @@ fun MapFragmentComposeView(
}
}

// MainState
// MainState에서 학과 정보 가져오기
val (departmentName, showUserDepartmentBottomSheet) = when (val state = mainUiState) {
is UiState.Success -> {
when (val data = state.data) {
is MainState.DepartmentState -> data.departmentName to data.showUserDepartmentBottomSheet
else -> "" to false
else -> "학과" to false
}
}
else -> "" to false

else -> "학과" to false
}

LaunchedEffect(Unit) {
Expand Down Expand Up @@ -230,7 +230,6 @@ fun MapFragmentComposeView(
)
},
) { innerPadding ->
Timber.d("학과 정보 : ${MySharedPreferences.getUserDepartmentName(context)}")

// 학과 정보가 없을 때 보여줄 BottomSheet
if (sheetState.isVisible) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.eatssu.android.presentation.map

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.eatssu.android.data.MySharedPreferences
import com.eatssu.android.domain.model.Partnership
import com.eatssu.android.domain.model.PartnershipRestaurant
import com.eatssu.android.domain.repository.PartnershipRepository
import com.eatssu.android.domain.usecase.user.GetPartnershipDetailUseCase
import com.eatssu.android.domain.usecase.user.GetUserCollegeDepartmentUseCase
import com.eatssu.android.presentation.UiEvent
import com.eatssu.android.presentation.UiState
import com.eatssu.android.presentation.map.model.RestaurantInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -36,6 +37,7 @@ data class MapState(
class MapViewModel @Inject constructor(
private val partnershipRepository: PartnershipRepository,
private val getPartnershipDetailUseCase: GetPartnershipDetailUseCase,
@ApplicationContext private val context: Context,
) : ViewModel() {

private val _uiState: MutableStateFlow<UiState<MapState>> = MutableStateFlow(UiState.Init)
Expand All @@ -44,7 +46,11 @@ class MapViewModel @Inject constructor(
private val _uiEvent = MutableSharedFlow<UiEvent>()
val uiEvent: SharedFlow<UiEvent> = _uiEvent

val departmentId: Long = MySharedPreferences.getUserDepartmentId(context).toLong()
val collegeId: Long = MySharedPreferences.getUserCollegeId(context).toLong()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getUserCollegeDepartmentUseCase 이 usecase가 존재해서 MySharedPreferences값을 직접 가져오는 것보다 usecase를 활용하면 어떨까요!!

val departmentId: Long = getUserCollegeDepartmentUseCase().userDepartment.departmentId.toLong()
val collegeId: Long = getUserCollegeDepartmentUseCase().userCollege.collegeId.toLong()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 잊고 있었네요 수정하겠습니다!

init {
Timber.d("학과 정보 : ${MySharedPreferences.getUserDepartmentName(context)}")
loadPartnerships()
}

Expand All @@ -69,7 +75,6 @@ class MapViewModel @Inject constructor(
fun loadUserCollegePartnerships() {
viewModelScope.launch {
_uiState.value = UiState.Loading

runCatching { partnershipRepository.getUserCollegePartnerships() }
.onSuccess { data ->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.eatssu.android.presentation.mypage

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.eatssu.android.BuildConfig
import com.eatssu.android.data.MySharedPreferences
import com.eatssu.android.data.repository.PreferencesRepository
import com.eatssu.android.domain.usecase.alarm.AlarmUseCase
import com.eatssu.android.domain.usecase.alarm.SetDailyNotificationStatusUseCase
import com.eatssu.android.domain.usecase.user.GetUserNickNameUseCase
import com.eatssu.android.presentation.UiEvent
import com.eatssu.android.presentation.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -30,13 +33,20 @@ class MyPageViewModel @Inject constructor(
private val getUserNickNameUseCase: GetUserNickNameUseCase,
private val setNotificationStatusUseCase: SetDailyNotificationStatusUseCase,
private val alarmUseCase: AlarmUseCase,
private val preferencesRepository: PreferencesRepository
private val preferencesRepository: PreferencesRepository,
@ApplicationContext private val context: Context
) : ViewModel() {

// 내부는 항상 "값 그 자체"만 들고 있고,
// 화면엔 UiState로 감싸서 노출
// 로컬 저장소에서 닉네임을 먼저 읽어서 초기 상태 설정
private val _state = MutableStateFlow(
MyPageState(appVersion = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})")
MyPageState(
appVersion = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
nickname = MySharedPreferences.getUserName(context).takeIf {
it.isNotBlank()
}
Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

ViewModel이 초기화될 때 SharedPreferences에서 닉네임을 가져와 초기 상태를 설정하는 로직이 추가되었네요. 좋은 접근입니다.

다만, 현재 로직은 isNotBlank만 확인하고 있습니다. fetchMyInfo 메소드나 MyPageStatehasNickname 로직을 보면, user-로 시작하는 닉네임은 유효하지 않은 것으로 처리하고 있습니다.

일관성을 유지하고, 'user-xxxx'와 같은 닉네임이 잠시 보였다가 사라지는 UI 깜빡임 현상을 방지하기 위해, 초기화 시점에도 startsWith("user-") 체크를 추가하는 것이 좋겠습니다.

Suggested change
nickname = MySharedPreferences.getUserName(context).takeIf {
it.isNotBlank()
}
nickname = MySharedPreferences.getUserName(context).takeIf {
it.isNotBlank() && !it.startsWith("user-")
}

)
)
val uiState: StateFlow<UiState<MyPageState>> =
_state
Expand Down