From a7c3899ab32feadef9063e7cef5808aeec17ad37 Mon Sep 17 00:00:00 2001 From: Jacob Bosco Date: Tue, 27 Dec 2022 12:20:32 +0530 Subject: [PATCH] WTA #71: Updated GetProjectStatsUC to get data in parallel. --- .../domain/usecases/GetProjectStatsUC.kt | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/details/src/main/java/com/jacob/wakatimeapp/details/domain/usecases/GetProjectStatsUC.kt b/details/src/main/java/com/jacob/wakatimeapp/details/domain/usecases/GetProjectStatsUC.kt index 27fddbf7..a082500a 100644 --- a/details/src/main/java/com/jacob/wakatimeapp/details/domain/usecases/GetProjectStatsUC.kt +++ b/details/src/main/java/com/jacob/wakatimeapp/details/domain/usecases/GetProjectStatsUC.kt @@ -9,14 +9,21 @@ import com.jacob.wakatimeapp.details.domain.models.DetailedProjectStatsUiData import com.jacob.wakatimeapp.details.domain.models.ProjectStats import javax.inject.Inject import javax.inject.Singleton +import kotlin.coroutines.CoroutineContext +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.datetime.DatePeriod import kotlinx.datetime.plus @Singleton internal class GetProjectStatsUC @Inject constructor( + dispatcher: CoroutineContext = Dispatchers.IO, private val projectStatsNetworkData: ProjectDetailsNetworkData, private val instantProvider: InstantProvider, ) { + private val ioScope = CoroutineScope(dispatcher) suspend operator fun invoke(projectName: String) = either { val totalProjectTime = projectStatsNetworkData.getTotalTimeForProject(projectName).bind() @@ -28,14 +35,17 @@ internal class GetProjectStatsUC @Inject constructor( .takeWhile { it < now } .plusElement(now) .zipWithNext() - .toList() .map { (start, end) -> - projectStatsNetworkData.getStatsForProject( - projectName = projectName, - startDate = start.toString(), - endDate = end.toString(), - ) + ioScope.async { + projectStatsNetworkData.getStatsForProject( + projectName = projectName, + startDate = start.toString(), + endDate = end.toString(), + ) + } } + .toList() + .awaitAll() .map { it.bind() } .fold(ProjectStats.ZERO, ProjectStats::plus)