-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/example/howdroid/presentation/chart/ChartViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.example.howdroid.presentation.chart | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.example.howdroid.data.model.response.ResponseStatistic | ||
import com.example.howdroid.domain.repository.ChartRepository | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import java.time.LocalDate | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class ChartViewModel @Inject constructor( | ||
private val chartRepository: ChartRepository | ||
) : ViewModel() { | ||
private val _preToDoAchievementRate = MutableStateFlow(0) | ||
val preToDoAchievementRate get() = _preToDoAchievementRate.asStateFlow() | ||
private val _nowToDoAchievementRate = MutableStateFlow(0) | ||
val nowToDoAchievementRate get() = _nowToDoAchievementRate.asStateFlow() | ||
private val _rateOfChange = MutableStateFlow(0) | ||
val rateOfChange get() = _rateOfChange.asStateFlow() | ||
private val _nowCategoryRateList = | ||
MutableStateFlow<MutableList<ResponseStatistic.NowCategoryDate>>( | ||
mutableListOf() | ||
) | ||
val nowCategoryRateList get() = _nowCategoryRateList.asStateFlow() | ||
private val _nowBestCategory = MutableStateFlow("") | ||
val nowBestCategory get() = _nowBestCategory.asStateFlow() | ||
private val _nowFailTagList = MutableStateFlow<MutableList<ResponseStatistic.NowFailtag>>( | ||
mutableListOf() | ||
) | ||
val nowFailTagList get() = _nowFailTagList.asStateFlow() | ||
private val _nowWorstFailTag = MutableStateFlow("") | ||
val nowWorstFailTag get() = _nowWorstFailTag.asStateFlow() | ||
|
||
init { | ||
fetchStatistic() | ||
} | ||
|
||
// TODO 초기화 유사한 거 끼리 함수화 | ||
private fun fetchStatistic() { | ||
val today = LocalDate.now() | ||
viewModelScope.launch { | ||
chartRepository.fetchStatistic(today.toString()) | ||
.onSuccess { chartInfo -> | ||
val prePercent = | ||
chartInfo.prevTodoDoneCnt.toFloat() / chartInfo.prevTodoCnt.toFloat() * 100 | ||
val nowPercent = | ||
chartInfo.nowTodoDoneCnt.toFloat() / chartInfo.nowTodoCnt.toFloat() * 100 | ||
val totalPercent = prePercent + nowPercent | ||
_preToDoAchievementRate.value = | ||
(prePercent / totalPercent * 100).toInt() | ||
_nowToDoAchievementRate.value = (nowPercent / totalPercent * 100).toInt() | ||
_rateOfChange.value = chartInfo.rateOfChange | ||
_nowCategoryRateList.value = chartInfo.nowCategoryDate.toMutableList() | ||
_nowFailTagList.value = chartInfo.nowFailtagList.toMutableList() | ||
_nowBestCategory.value = chartInfo.nowBestCategory | ||
_nowWorstFailTag.value = chartInfo.nowWorstFailtag | ||
} | ||
.onFailure { throwable -> | ||
Timber.e(throwable.message) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters