Fix sync triggering

This commit is contained in:
Jonas Lochmann 2021-12-13 01:00:00 +01:00
parent 394adb2e97
commit d1401b664e
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
2 changed files with 29 additions and 19 deletions

View file

@ -384,11 +384,13 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
val category = handling.createdWithCategoryRelatedData.category
val categoryId = category.id
val timeToSubtractForCategory = timeToSubtractForCategory(categoryId)
val isRecentlyStartedCategory = recentlyStartedCategories.contains(categoryId)
val nowRemaining = handling.remainingTime ?: return@forEach // category is not limited anymore
val oldRemainingTime = nowRemaining.includingExtraTime - timeToSubtractForCategory
val newRemainingTime = oldRemainingTime - timeToSubtract
val commitedSessionDuration = handling.remainingSessionDuration
val oldSessionDuration = handling.remainingSessionDuration?.let { it - timeToSubtractForCategory }
// trigger time warnings
@ -451,7 +453,7 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
if (oldSessionDuration != null) {
val newSessionDuration = oldSessionDuration - timeToSubtract
if (oldSessionDuration > 0 && newSessionDuration <= 0) {
if (oldSessionDuration > 0 && newSessionDuration <= 0 && !isRecentlyStartedCategory) {
triggerSyncByTimeOver = true
}
}
@ -459,16 +461,15 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
// check if limit login triggered
val triggerSyncByLimitLoginCategoryForThisCategory = userRelatedData.preBlockSwitchPoints.let { switchPoints ->
if (switchPoints.isEmpty()) false else {
val limitLoginBySessionDuration = if (oldSessionDuration != null) {
val limitLoginBySessionDuration = if (commitedSessionDuration != null && oldSessionDuration != null) {
val newSessionDuration = oldSessionDuration - timeToSubtract
switchPoints.find { switchPoint -> oldSessionDuration >= switchPoint && newSessionDuration < switchPoint } != null
switchPoints.find { switchPoint -> switchPoint in newSessionDuration until commitedSessionDuration } != null
} else false
val limitLoginByRemainingTime = switchPoints.find { switchPoint -> oldRemainingTime >= switchPoint && newRemainingTime < switchPoint } != null
limitLoginBySessionDuration || limitLoginByRemainingTime
(limitLoginBySessionDuration && !isRecentlyStartedCategory) || limitLoginByRemainingTime
}
}
@ -524,7 +525,7 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
appLogic.platformIntegration.showOverlayMessage("sync forced")
}
commitUsedTimeUpdaters()
usedTimeUpdateHelper.doCommit()
ApplyActionUtil.applyAppLogicAction(
action = ForceSyncAction,

View file

@ -95,7 +95,7 @@ class UsedTimeUpdateHelper (private val appLogic: AppLogic) {
Log.d(LOG_TAG, "makeCommitByDifferntHandling = $makeCommitByDifferntHandling; makeCommitByDifferentBaseData = $makeCommitByDifferentBaseData; makeCommitByCountedTime = $makeCommitByCountedTime")
}
doCommitPrivate()
doCommit()
} else {
false
}
@ -109,14 +109,14 @@ class UsedTimeUpdateHelper (private val appLogic: AppLogic) {
}
suspend fun flush() {
doCommitPrivate()
doCommit()
lastCategoryHandlings = emptyList()
categoryIds = emptySet()
}
private suspend fun doCommitPrivate(): Boolean {
val makeCommit = lastCategoryHandlings.isNotEmpty() && countedTime > 0
suspend fun doCommit(): Boolean {
val makeCommit = lastCategoryHandlings.isNotEmpty()
if (makeCommit) {
val categoriesWithSessionDurationLimits = lastCategoryHandlings
@ -127,10 +127,14 @@ class UsedTimeUpdateHelper (private val appLogic: AppLogic) {
val categoriesWithSessionDurationLimitsWhichWereRecentlyStarted = categoriesWithSessionDurationLimits.intersect(recentlyStartedCategories)
if (categoriesWithSessionDurationLimitsWhichWereRecentlyStarted.isEmpty()) {
commitSendAction(
items = lastCategoryHandlings,
sendTimestamp = categoriesWithSessionDurationLimits.isNotEmpty()
)
val sendTimestamp = categoriesWithSessionDurationLimits.isNotEmpty()
if (countedTime > 0 || sendTimestamp) {
commitSendAction(
items = lastCategoryHandlings,
sendTimestamp = sendTimestamp
)
}
} else {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "skip updating the session duration last usage timestamps")
@ -138,10 +142,12 @@ class UsedTimeUpdateHelper (private val appLogic: AppLogic) {
if (categoriesWithSessionDurationLimits == categoriesWithSessionDurationLimitsWhichWereRecentlyStarted) {
// no category needs the timestamp
commitSendAction(
items = lastCategoryHandlings,
sendTimestamp = false
)
if (countedTime > 0) {
commitSendAction(
items = lastCategoryHandlings,
sendTimestamp = false
)
}
} else {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "... but only for some categories")
@ -155,7 +161,10 @@ class UsedTimeUpdateHelper (private val appLogic: AppLogic) {
categoriesWithSessionDurationLimitsWhichWereRecentlyStarted.contains(it.createdWithCategoryRelatedData.category.id)
}
commitSendAction(items = items1, sendTimestamp = false)
if (countedTime > 0) {
commitSendAction(items = items1, sendTimestamp = false)
}
commitSendAction(items = items2, sendTimestamp = true)
}
}