Skip to content

Commit

Permalink
#23 refactor: Pref의 gardenId 필요한 곳만 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungWoo-Ahn committed Apr 16, 2024
1 parent ad51f5f commit bcecb61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import io.tuttut.data.constant.CUSTOM_NAME
import io.tuttut.data.model.dto.Crops
import io.tuttut.data.model.dto.CropsInfo
import io.tuttut.data.model.response.Result
import io.tuttut.data.repository.auth.AuthRepository
import io.tuttut.data.repository.crops.CropsRepository
import io.tuttut.data.repository.cropsInfo.CropsInfoRepository
import io.tuttut.presentation.base.BaseViewModel
import io.tuttut.presentation.model.CropsModel
import io.tuttut.presentation.model.PreferenceUtil
import io.tuttut.presentation.util.getToday
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -26,10 +26,10 @@ import javax.inject.Inject
class AddCropsViewModel @Inject constructor(
private val cropsRepo: CropsRepository,
val cropsInfoRepo: CropsInfoRepository,
private val prefs: PreferenceUtil,
authRepo: AuthRepository,
private val cropsModel: CropsModel,
): BaseViewModel() {

private val gardenId = authRepo.currentUser.value.gardenId
private val crops = cropsModel.selectedCrops.value
val totalCrops = listOf(CropsInfo()) + cropsInfoRepo.cropsInfoList.value

Expand Down Expand Up @@ -167,7 +167,7 @@ class AddCropsViewModel @Inject constructor(
else typedGrowingDay.value.convertDayInt()
} else cropsInfo?.growingDay
)
cropsRepo.addCrops(prefs.gardenId, newCrops).collect {
cropsRepo.addCrops(gardenId, newCrops).collect {
when (it) {
is Result.Success -> {
cropsModel.refreshCropsList(newCrops)
Expand Down Expand Up @@ -195,7 +195,7 @@ class AddCropsViewModel @Inject constructor(
} else crops.growingDay,
needAlarm = crops.needAlarm
)
cropsRepo.updateCrops(prefs.gardenId, updatedCrops).collect {
cropsRepo.updateCrops(gardenId, updatedCrops).collect {
when (it) {
is Result.Success -> {
cropsModel.refreshCropsList(updatedCrops)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import io.tuttut.data.model.dto.CropsInfo
import io.tuttut.data.model.dto.Diary
import io.tuttut.data.model.response.Result
import io.tuttut.data.repository.auth.AuthRepository
import io.tuttut.data.repository.crops.CropsRepository
import io.tuttut.data.repository.cropsInfo.CropsInfoRepository
import io.tuttut.data.repository.diary.DiaryRepository
import io.tuttut.data.repository.garden.GardenRepository
import io.tuttut.presentation.base.BaseViewModel
import io.tuttut.presentation.model.CropsModel
import io.tuttut.presentation.model.DiaryModel
import io.tuttut.presentation.model.PreferenceUtil
import io.tuttut.presentation.util.getToday
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -27,20 +27,21 @@ import javax.inject.Inject
@HiltViewModel
class CropsDetailViewModel @Inject constructor(
private val cropsRepo: CropsRepository,
authRepo: AuthRepository,
gardenRepo: GardenRepository,
cropsInfoRepo: CropsInfoRepository,
diaryRepo: DiaryRepository,
private val cropsModel: CropsModel,
private val diaryModel: DiaryModel,
private val prefs: PreferenceUtil
): BaseViewModel() {
private val gardenId = authRepo.currentUser.value.gardenId
private val crops = cropsModel.observedCrops.value
val gardenMemberMap = gardenRepo.gardenMemberMap
val cropsInfoMap = cropsInfoRepo.cropsInfoMap

val uiState: StateFlow<CropsDetailUiState>
= cropsRepo.getCropsDetail(
gardenId = prefs.gardenId,
gardenId = gardenId,
cropsId = crops.id
).map(CropsDetailUiState::Success)
.stateIn(
Expand All @@ -51,7 +52,7 @@ class CropsDetailViewModel @Inject constructor(

val diaryUiState: StateFlow<CropsDiaryUiState>
= diaryRepo.getFourDiaryList(
gardenId = prefs.gardenId,
gardenId = gardenId,
cropsId = crops.id
).map(CropsDiaryUiState::Success)
.stateIn(
Expand Down Expand Up @@ -101,7 +102,7 @@ class CropsDetailViewModel @Inject constructor(

fun onHarvest(onShowSnackBar: suspend (String, String?) -> Boolean) {
viewModelScope.launch {
cropsRepo.harvestCrops(prefs.gardenId, crops.id, crops.harvestCnt).collect {
cropsRepo.harvestCrops(gardenId, crops.id, crops.harvestCnt).collect {
when (it) {
is Result.Success -> {
cropsModel.refreshHarvestedCropsList.value = true
Expand All @@ -125,7 +126,7 @@ class CropsDetailViewModel @Inject constructor(
onShowSnackBar("오늘 물을 줬어요", null)
} else {
cropsRepo.wateringCrops(
gardenId = prefs.gardenId,
gardenId = gardenId,
cropsId = crops.id,
today = getToday()
).collect {
Expand All @@ -144,7 +145,7 @@ class CropsDetailViewModel @Inject constructor(

fun onDelete(moveMain: () -> Unit, onShowSnackBar: suspend (String, String?) -> Boolean) {
viewModelScope.launch {
cropsRepo.deleteCrops(prefs.gardenId, crops.id).collect {
cropsRepo.deleteCrops(gardenId, crops.id).collect {
when (it) {
is Result.Success -> {
showDeleteDialog = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import io.tuttut.data.repository.storage.StorageRepository
import io.tuttut.presentation.base.BaseViewModel
import io.tuttut.presentation.model.CropsModel
import io.tuttut.presentation.model.DiaryModel
import io.tuttut.presentation.model.PreferenceUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
Expand All @@ -34,13 +33,12 @@ class DiaryListViewModel @Inject constructor(
gardenRepo: GardenRepository,
private val cropsModel: CropsModel,
private val diaryModel: DiaryModel,
prefs: PreferenceUtil,
) : BaseViewModel() {
val crops = cropsModel.observedCrops.value
val currentUser = authRepo.currentUser.value
val memberMap = gardenRepo.gardenMemberMap

val diaryList: Flow<PagingData<Diary>> = diaryRepo.getDiaryList(prefs.gardenId, crops.id).cachedIn(viewModelScope)
val diaryList: Flow<PagingData<Diary>> = diaryRepo.getDiaryList(currentUser.gardenId, crops.id).cachedIn(viewModelScope)

private var selectedDiary by mutableStateOf(Diary())
var showDeleteDialog by mutableStateOf(false)
Expand Down

0 comments on commit bcecb61

Please sign in to comment.