@@ -11,10 +11,13 @@ import com.example.util.simpletimetracker.domain.notifications.interactor.Update
1111import com.example.util.simpletimetracker.domain.recordType.model.RecordType
1212import com.example.util.simpletimetracker.domain.base.ResultContainer
1313import com.example.util.simpletimetracker.domain.base.CurrentTimestampProvider
14+ import com.example.util.simpletimetracker.domain.base.SuspendLazy
15+ import com.example.util.simpletimetracker.domain.base.suspendLazy
1416import com.example.util.simpletimetracker.domain.record.model.Record
1517import com.example.util.simpletimetracker.domain.record.model.RecordBase
1618import com.example.util.simpletimetracker.domain.record.model.RecordDataSelectionDialogResult
1719import com.example.util.simpletimetracker.domain.record.model.RunningRecord
20+ import kotlinx.coroutines.coroutineScope
1821import java.util.concurrent.TimeUnit
1922import javax.inject.Inject
2023
@@ -72,21 +75,16 @@ class AddRunningRecordMediator @Inject constructor(
7275 timeStarted : StartTime = StartTime .TakeCurrent ,
7376 updateNotificationSwitch : Boolean = true,
7477 checkDefaultDuration : Boolean = true,
75- ) {
78+ ) = coroutineScope {
7679 val currentTime = currentTimestampProvider.get()
7780 val actualTimeStarted = when (timeStarted) {
7881 is StartTime .Current -> timeStarted.currentTimeStampMs
7982 is StartTime .TakeCurrent -> currentTime
8083 is StartTime .Timestamp -> timeStarted.timestampMs
8184 }.coerceAtMost(currentTime)
8285 val retroactiveTrackingMode = prefsInteractor.getRetroactiveTrackingMode()
83- val actualPrevRecords = if (
84- retroactiveTrackingMode ||
85- complexRuleProcessActionInteractor.hasRules()
86- ) {
86+ val actualPrevRecords = suspendLazy {
8787 recordInteractor.getAllPrev(actualTimeStarted)
88- } else {
89- emptyList()
9088 }
9189 val rulesResult = if (
9290 retroactiveTrackingMode &&
@@ -184,7 +182,7 @@ class AddRunningRecordMediator @Inject constructor(
184182
185183 private suspend fun addRetroactiveModeInternal (
186184 params : StartParams ,
187- prevRecords : List <Record >,
185+ prevRecords : SuspendLazy < List <Record > >,
188186 ) {
189187 val type = recordTypeInteractor.get(params.typeId) ? : return
190188
@@ -247,7 +245,7 @@ class AddRunningRecordMediator @Inject constructor(
247245
248246 private suspend fun addRecordRetroactively (
249247 params : StartParams ,
250- prevRecords : List <Record >,
248+ prevRecords : SuspendLazy < List <Record > >,
251249 ) {
252250 val prevRecord = getPrevRecordToMergeWith(params.typeId, prevRecords)
253251 val sameTags = prevRecord?.tags.orEmpty().sortedBy { it.tagId } == params.tags.sortedBy { it.tagId }
@@ -265,7 +263,7 @@ class AddRunningRecordMediator @Inject constructor(
265263 ? : prevRecord.tags,
266264 )
267265 } else {
268- val newTimeStarted = prevRecords.firstOrNull()?.timeEnded
266+ val newTimeStarted = prevRecords() .firstOrNull()?.timeEnded
269267 ? : (params.timeStarted - TimeUnit .MINUTES .toMillis(5 ))
270268 Record (
271269 id = 0L , // Creates new record.
@@ -285,7 +283,7 @@ class AddRunningRecordMediator @Inject constructor(
285283 private suspend fun processRules (
286284 typeId : Long ,
287285 timeStarted : Long ,
288- prevRecords : List <Record >,
286+ prevRecords : SuspendLazy < List <Record > >,
289287 ): ComplexRuleProcessActionInteractor .Result {
290288 // If no rules - no need to check them.
291289 return if (complexRuleProcessActionInteractor.hasRules()) {
@@ -297,7 +295,7 @@ class AddRunningRecordMediator @Inject constructor(
297295 hasAnyRunningTimersOnTimeStarted
298296
299297 // If no current records - check closest previous.
300- val records = if (takeCurrentRecords) currentRecords else prevRecords
298+ val records = if (takeCurrentRecords) currentRecords else prevRecords()
301299
302300 val currentTypeIds = records
303301 .map { it.typeIds }
@@ -350,15 +348,15 @@ class AddRunningRecordMediator @Inject constructor(
350348
351349 private suspend fun processRetroactiveMultitasking (
352350 params : StartParams ,
353- prevRecords : List <Record >,
351+ prevRecords : SuspendLazy < List <Record > >,
354352 ) {
355353 if (! params.isMultitaskingAllowed) return
356354
357355 val recordTypesMap = recordTypeInteractor.getAll().associateBy(RecordType ::id)
358356 val mergedRecord = getPrevRecordToMergeWith(params.typeId, prevRecords)
359357
360358 // Extend prev records to current time.
361- prevRecords.filter {
359+ prevRecords() .filter {
362360 // Skip record that would be merge.
363361 it.id != mergedRecord?.id
364362 }.filter {
@@ -392,11 +390,11 @@ class AddRunningRecordMediator @Inject constructor(
392390 return result.distinctBy { it.tagId }
393391 }
394392
395- private fun getPrevRecordToMergeWith (
393+ private suspend fun getPrevRecordToMergeWith (
396394 typeId : Long ,
397- prevRecords : List <Record >,
395+ prevRecords : SuspendLazy < List <Record > >,
398396 ): Record ? {
399- return prevRecords.firstOrNull { it.typeId == typeId }
397+ return prevRecords() .firstOrNull { it.typeId == typeId }
400398 }
401399
402400 private data class StartParams (
0 commit comments