Skip to content

Commit

Permalink
WTA #71: Cleaned up use-case.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Dec 27, 2022
1 parent daefd0b commit fd76109
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jacob.wakatimeapp.navigation
import com.jacob.wakatimeapp.details.ui.DetailsPageNavigator
import com.jacob.wakatimeapp.home.ui.HomePageNavigator
import com.jacob.wakatimeapp.login.ui.LoginPageNavigator
import com.jacob.wakatimeapp.navigation.destinations.DetailsPageDestination
import com.jacob.wakatimeapp.navigation.destinations.HomePageDestination
import com.jacob.wakatimeapp.navigation.destinations.SearchProjectsDestination
import com.jacob.wakatimeapp.search.ui.SearchProjectsNavigator
Expand All @@ -19,7 +20,7 @@ class LoginPageNavigatorImpl(private val navigator: DestinationsNavigator) : Log
}

class HomePageNavigatorImpl(private val navigator: DestinationsNavigator) : HomePageNavigator {
override fun toDetailsPage() = navigator.navigate(HomePageDestination)
override fun toDetailsPage() = navigator.navigate(DetailsPageDestination)
override fun toSearchPage() = navigator.navigate(SearchProjectsDestination)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.jacob.wakatimeapp.core.models

import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDate
import kotlinx.datetime.toLocalDateTime
import kotlinx.serialization.Serializable

Expand All @@ -15,7 +18,14 @@ data class StatsRange(
)

constructor(startDate: String, endDate: String) : this(
startDate = startDate.toLocalDateTime().date,
endDate = endDate.toLocalDateTime().date,
startDate = startDate.takeWhile { it != 'T' }.toLocalDate(),
endDate = endDate.takeWhile { it != 'T' }.toLocalDate(),
)

companion object {
val ZERO = StatsRange(
startDate = Instant.DISTANT_PAST.toLocalDateTime(TimeZone.currentSystemDefault()).date,
endDate = Instant.DISTANT_PAST.toLocalDateTime(TimeZone.currentSystemDefault()).date,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class TotalTimeForProjectDTO(
val text: String,
val timeout: Int,
@SerialName("is_up_to_date") val isUpToDate: Boolean,
@SerialName("percent_calculated") val percentCalculated: Int,
@SerialName("percent_calculated") val percentCalculated: Int?,
@SerialName("total_seconds") val totalSeconds: Double,
) {
@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import com.jacob.wakatimeapp.details.data.dtos.TotalTimeForProjectDTO
import com.jacob.wakatimeapp.details.domain.models.TotalProjectTime
import kotlinx.datetime.toLocalDate

@Suppress("MagicNumber")
fun TotalTimeForProjectDTO.toModel() = TotalProjectTime(
totalTime = Time.fromDecimal(data.decimal.toFloat()),
startDate = data.range.startDate.toLocalDate(),
percentageCalculated = data.percentCalculated,
percentageCalculated = data.percentCalculated ?: 100,
message = data.text,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jacob.wakatimeapp.details.domain.models

import com.jacob.wakatimeapp.core.models.Time
import kotlinx.datetime.LocalDate

data class DetailedProjectStatsUiData(
val totalProjectTime: TotalProjectTime,
val averageTime: Time,
val dailyProjectStats: Map<LocalDate, Time>,
val languages: List<String>,
val operatingSystems: List<String>,
val editors: List<String>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class ProjectStats(
val ZERO = ProjectStats(
totalTime = Time.ZERO,
dailyProjectStats = mapOf(),
range = StatsRange(startDate = "", endDate = ""),
range = StatsRange.ZERO,
languages = listOf(),
operatingSystems = listOf(),
editors = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import com.jacob.wakatimeapp.core.common.toDate
import com.jacob.wakatimeapp.core.common.utils.InstantProvider
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.details.data.ProjectDetailsNetworkData
import com.jacob.wakatimeapp.details.domain.models.DetailedProjectStatsUiData
import com.jacob.wakatimeapp.details.domain.models.ProjectStats
import com.jacob.wakatimeapp.details.domain.models.TotalProjectTime
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate
import kotlinx.datetime.plus

@Singleton
Expand All @@ -25,7 +24,7 @@ internal class GetProjectStatsUC @Inject constructor(
val batchSize = DatePeriod(months = 6)
val now = instantProvider.now().toDate()

generateSequence(totalProjectTime.startDate) { it + batchSize }
val projectStats = generateSequence(totalProjectTime.startDate) { it + batchSize }
.takeWhile { it < now }
.plusElement(now)
.zipWithNext()
Expand All @@ -39,14 +38,14 @@ internal class GetProjectStatsUC @Inject constructor(
}
.map { it.bind() }
.fold(ProjectStats.ZERO, ProjectStats::plus)

DetailedProjectStatsUiData(
totalProjectTime = totalProjectTime,
averageTime = Time.ZERO,
dailyProjectStats = projectStats.dailyProjectStats,
languages = projectStats.languages,
operatingSystems = projectStats.operatingSystems,
editors = projectStats.editors,
)
}
}

data class Something(
val totalProjectTime: TotalProjectTime,
val averageTime: Time,
val dailyProjectStats: Map<LocalDate, Time>,
val languages: List<String>,
val operatingSystems: List<String>,
val editors: List<String>,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package com.jacob.wakatimeapp.details.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jacob.wakatimeapp.details.domain.usecases.GetProjectStatsUC
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import timber.log.Timber

@HiltViewModel
class DetailsPageViewModel @Inject constructor() : ViewModel()
internal class DetailsPageViewModel @Inject constructor(
private val getProjectStatsUC: GetProjectStatsUC,
) : ViewModel() {
private val _viewState = MutableStateFlow<DetailsPageViewState>(DetailsPageViewState.Loading)
val viewState = _viewState.asStateFlow()

init {
viewModelScope.launch {
val data = getProjectStatsUC("WakaTimeApp.Compose")
Timber.w(data.toString())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jacob.wakatimeapp.details.ui

sealed class DetailsPageViewState {
object Loading : DetailsPageViewState()

data class Loaded(val detUiDetailsPageViewState: DetailsPageViewState) : DetailsPageViewState()

data class Error(val error: com.jacob.wakatimeapp.core.models.Error) : DetailsPageViewState()
}

0 comments on commit fd76109

Please sign in to comment.