Skip to content

Commit

Permalink
[fix]: Fix Code Convention
Browse files Browse the repository at this point in the history
  • Loading branch information
kez-lab committed Jan 4, 2024
1 parent f3e4b47 commit e8a97ea
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ class UsageGoalsDataSource {
@Provides
@Singleton
fun getUsageGoals(): List<UsageGoalModel> {
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),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UsageGoalModel>,
) : UsageGoalsRepository {
override fun getUsageGoals(): List<UsageGoal> {
return usageGoalList.map { it ->
UsageGoal(it.packageName, it.goalTime)
}
class DefaultUsageGoalsRepository @Inject constructor(
private val usageGoalList: List<UsageGoalModel>
) : UsageGoalsRepository {
override fun getUsageGoals(): List<UsageGoal> {
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UsageStat> {
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<UsageStat> {
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<UsageStat> {
val usageStatsList = getUsageStats(startTime, endTime)
return usageStatsList.filter {
packageNames.contains(it.packageName)
}
}

override fun getUsageTimeForPackages(
startTime: Long,
endTime: Long,
vararg packageNames: String,
): List<UsageStat> {
val usageStatsList = getUsageStats(startTime, endTime)
return usageStatsList.filter {
override fun getUsageTimeForPackages(
startTime: Long,
endTime: Long,
packageNames: List<String>,
): List<UsageStat> {
val usageStatsList = getUsageStats(startTime, endTime)
val newUsageStatsList =
usageStatsList.filter {
packageNames.contains(it.packageName)
}
}

override fun getUsageTimeForPackages(
startTime: Long,
endTime: Long,
packageNames: List<String>,
): List<UsageStat> {
val usageStatsList = getUsageStats(startTime, endTime)
val newUsageStatsList =
usageStatsList.filter {
packageNames.contains(it.packageName)
}
return newUsageStatsList
}
return newUsageStatsList
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UsageStatAndGoal> {
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<UsageStatAndGoal> {
val appList = getSelectedAppList()
class StaticsUseCase @Inject constructor(
private val usageStatsRepository: UsageStatsRepository,
private val usageGoalsRepository: UsageGoalsRepository
) {
fun getUsageStatsAndGoals(
startTime: Long,
endTime: Long,
): List<UsageStatAndGoal> {
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<UsageStatAndGoal> {
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<String> = usageGoalsRepository.getUsageGoals().map { it.packageName }
private fun getTotalUsage(
startTime: Long,
endTime: Long,
): Long = usageStatsRepository.getUsageStats(startTime, endTime).sumOf {
it.totalTimeInForeground
}

private fun getSelectedAppList(): List<String> =
usageGoalsRepository.getUsageGoals().map { it.packageName }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<UsageStatAndGoal>>(emptyList())
class StaticsViewModel @Inject constructor(
private val staticsUseCase: StaticsUseCase
) : ViewModel() {
val usageStatAndGoalList = MutableStateFlow<List<UsageStatAndGoal>>(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)
}
}

0 comments on commit e8a97ea

Please sign in to comment.