diff --git a/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/datasource/UsageGoalsDataSource.kt b/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/datasource/UsageGoalsDataSource.kt index d55c7efd..a1265981 100644 --- a/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/datasource/UsageGoalsDataSource.kt +++ b/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/datasource/UsageGoalsDataSource.kt @@ -13,15 +13,12 @@ class UsageGoalsDataSource { @Provides @Singleton fun getUsageGoals(): List { - val usageGoalList = - listOf( - UsageGoalModel("total", 201519990), - UsageGoalModel("com.kakao.talk", 15686 * 2), - UsageGoalModel("com.google.android.gms", 7134), - UsageGoalModel("com.google.android.youtube", 71349), - UsageGoalModel("com.android.chrome", 39445), - ) - - return usageGoalList + return listOf( + UsageGoalModel("total", 201519990), + UsageGoalModel("com.kakao.talk", 15686 * 2), + UsageGoalModel("com.google.android.gms", 7134), + UsageGoalModel("com.google.android.youtube", 71349), + UsageGoalModel("com.android.chrome", 39445), + ) } } diff --git a/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageGoalsRepository.kt b/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageGoalsRepository.kt index 29ae1c61..1c015813 100644 --- a/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageGoalsRepository.kt +++ b/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageGoalsRepository.kt @@ -4,18 +4,16 @@ import com.hmh.hamyeonham.usagestats.model.UsageGoal import com.hmh.hamyeonham.usagestats.model.UsageGoalModel import javax.inject.Inject -class DefaultUsageGoalsRepository - @Inject - constructor( - private val usageGoalList: List, - ) : UsageGoalsRepository { - override fun getUsageGoals(): List { - return usageGoalList.map { it -> - UsageGoal(it.packageName, it.goalTime) - } +class DefaultUsageGoalsRepository @Inject constructor( + private val usageGoalList: List +) : UsageGoalsRepository { + override fun getUsageGoals(): List { + return usageGoalList.map { + UsageGoal(it.packageName, it.goalTime) } + } - override fun getUsageGoalTime(packageName: String): Long { - return usageGoalList.firstOrNull { it.packageName == packageName }?.goalTime ?: 0 - } + override fun getUsageGoalTime(packageName: String): Long { + return usageGoalList.firstOrNull { it.packageName == packageName }?.goalTime ?: 0 } +} diff --git a/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageStatsRepository.kt b/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageStatsRepository.kt index d6bca023..f75d838f 100644 --- a/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageStatsRepository.kt +++ b/data/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/repository/DefaultUsageStatsRepository.kt @@ -4,52 +4,50 @@ import com.hmh.hamyeonham.usagestats.datasource.UsageStatsDataSource import com.hmh.hamyeonham.usagestats.model.UsageStat import javax.inject.Inject -class DefaultUsageStatsRepository - @Inject - constructor( - private val usageStatsDataSource: UsageStatsDataSource, - ) : UsageStatsRepository { - override fun getUsageStats( - startTime: Long, - endTime: Long, - ): List { - val usageStatsList = usageStatsDataSource.getUsageStats(startTime, endTime) - return usageStatsList.map { usageStatModel -> - UsageStat(usageStatModel.packageName, usageStatModel.totalTimeInForeground) - } +class DefaultUsageStatsRepository @Inject constructor( + private val usageStatsDataSource: UsageStatsDataSource +) : UsageStatsRepository { + override fun getUsageStats( + startTime: Long, + endTime: Long, + ): List { + val usageStatsList = usageStatsDataSource.getUsageStats(startTime, endTime) + return usageStatsList.map { usageStatModel -> + UsageStat(usageStatModel.packageName, usageStatModel.totalTimeInForeground) } + } + + override fun getUsageTimeForPackage( + startTime: Long, + endTime: Long, + packageName: String, + ): Long { + val usageStatsList = getUsageStats(startTime, endTime) + return usageStatsList.firstOrNull { it.packageName == packageName }?.totalTimeInForeground + ?: 0 + } - override fun getUsageTimeForPackage( - startTime: Long, - endTime: Long, - packageName: String, - ): Long { - val usageStatsList = getUsageStats(startTime, endTime) - return usageStatsList.firstOrNull { it.packageName == packageName }?.totalTimeInForeground - ?: 0 + override fun getUsageTimeForPackages( + startTime: Long, + endTime: Long, + vararg packageNames: String, + ): List { + val usageStatsList = getUsageStats(startTime, endTime) + return usageStatsList.filter { + packageNames.contains(it.packageName) } + } - override fun getUsageTimeForPackages( - startTime: Long, - endTime: Long, - vararg packageNames: String, - ): List { - val usageStatsList = getUsageStats(startTime, endTime) - return usageStatsList.filter { + override fun getUsageTimeForPackages( + startTime: Long, + endTime: Long, + packageNames: List, + ): List { + val usageStatsList = getUsageStats(startTime, endTime) + val newUsageStatsList = + usageStatsList.filter { packageNames.contains(it.packageName) } - } - - override fun getUsageTimeForPackages( - startTime: Long, - endTime: Long, - packageNames: List, - ): List { - val usageStatsList = getUsageStats(startTime, endTime) - val newUsageStatsList = - usageStatsList.filter { - packageNames.contains(it.packageName) - } - return newUsageStatsList - } + return newUsageStatsList } +} diff --git a/domain/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/usecase/StaticsUseCase.kt b/domain/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/usecase/StaticsUseCase.kt index bb358d24..f3e00f2b 100644 --- a/domain/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/usecase/StaticsUseCase.kt +++ b/domain/usagestats/src/main/java/com/hmh/hamyeonham/usagestats/usecase/StaticsUseCase.kt @@ -5,48 +5,42 @@ import com.hmh.hamyeonham.usagestats.repository.UsageGoalsRepository import com.hmh.hamyeonham.usagestats.repository.UsageStatsRepository import javax.inject.Inject -class StaticsUseCase - @Inject - constructor( - private val usageStatsRepository: UsageStatsRepository, - private val usageGoalsRepository: UsageGoalsRepository, - ) { - fun getUsageStatsAndGoals( - startTime: Long, - endTime: Long, - ): List { - val totalUsage = - getTotalUsage(startTime, endTime) - val usageForSelectedApps = - getUsageStatsAndGoalsForSelectedApps(startTime, endTime) - val totalUsageStatAndGoal = - UsageStatAndGoal("total", totalUsage, usageGoalsRepository.getUsageGoalTime("total")) - return listOf(totalUsageStatAndGoal) + usageForSelectedApps - } - - private fun getUsageStatsAndGoalsForSelectedApps( - startTime: Long, - endTime: Long, - ): List { - val appList = getSelectedAppList() +class StaticsUseCase @Inject constructor( + private val usageStatsRepository: UsageStatsRepository, + private val usageGoalsRepository: UsageGoalsRepository +) { + fun getUsageStatsAndGoals( + startTime: Long, + endTime: Long, + ): List { + val totalUsage = getTotalUsage(startTime, endTime) + val usageForSelectedApps = getUsageStatsAndGoalsForSelectedApps(startTime, endTime) + val totalUsageStatAndGoal = + UsageStatAndGoal("total", totalUsage, usageGoalsRepository.getUsageGoalTime("total")) + return listOf(totalUsageStatAndGoal) + usageForSelectedApps + } - val usageStatAndGoal = - usageStatsRepository.getUsageTimeForPackages(startTime, endTime, appList).map { - UsageStatAndGoal( - it.packageName, - it.totalTimeInForeground, - usageGoalsRepository.getUsageGoalTime(it.packageName), - ) - } - return usageStatAndGoal + private fun getUsageStatsAndGoalsForSelectedApps( + startTime: Long, + endTime: Long, + ): List { + val appList = getSelectedAppList() + return usageStatsRepository.getUsageTimeForPackages(startTime, endTime, appList).map { + UsageStatAndGoal( + it.packageName, + it.totalTimeInForeground, + usageGoalsRepository.getUsageGoalTime(it.packageName), + ) } + } - private fun getTotalUsage( - startTime: Long, - endTime: Long, - ): Long = - usageStatsRepository.getUsageStats(startTime, endTime) - .sumOf { it.totalTimeInForeground } - - private fun getSelectedAppList(): List = usageGoalsRepository.getUsageGoals().map { it.packageName } + private fun getTotalUsage( + startTime: Long, + endTime: Long, + ): Long = usageStatsRepository.getUsageStats(startTime, endTime).sumOf { + it.totalTimeInForeground } + + private fun getSelectedAppList(): List = + usageGoalsRepository.getUsageGoals().map { it.packageName } +} diff --git a/feature/statistics/src/main/java/com/hmh/hamyeonham/statistics/StaticsViewModel.kt b/feature/statistics/src/main/java/com/hmh/hamyeonham/statistics/StaticsViewModel.kt index 9871e8ae..1790bc67 100644 --- a/feature/statistics/src/main/java/com/hmh/hamyeonham/statistics/StaticsViewModel.kt +++ b/feature/statistics/src/main/java/com/hmh/hamyeonham/statistics/StaticsViewModel.kt @@ -9,22 +9,20 @@ import kotlinx.coroutines.flow.MutableStateFlow import javax.inject.Inject @HiltViewModel -class StaticsViewModel - @Inject - constructor( - private val staticsUseCase: StaticsUseCase, - ) : ViewModel() { - val usageStatAndGoalList = MutableStateFlow>(emptyList()) +class StaticsViewModel @Inject constructor( + private val staticsUseCase: StaticsUseCase +) : ViewModel() { + val usageStatAndGoalList = MutableStateFlow>(emptyList()) - init { - val (startTime, endTime) = getCurrentDayStartEndEpochMillis() - getUsageStatsAndGoals(startTime, endTime) - } + init { + val (startTime, endTime) = getCurrentDayStartEndEpochMillis() + getUsageStatsAndGoals(startTime, endTime) + } - private fun getUsageStatsAndGoals( - startTime: Long, - endTime: Long, - ) { - usageStatAndGoalList.value = staticsUseCase.getUsageStatsAndGoals(startTime, endTime) - } + private fun getUsageStatsAndGoals( + startTime: Long, + endTime: Long, + ) { + usageStatAndGoalList.value = staticsUseCase.getUsageStatsAndGoals(startTime, endTime) } +}