Skip to content

Commit

Permalink
Make code better readable
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Mayer <[email protected]>
  • Loading branch information
JonasMayerDev committed Apr 9, 2024
1 parent c64d4c8 commit ac9902f
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,35 @@ class FilesSyncWork(
setForegroundAsync(foregroundInfo)
}

private fun canExitEarly(changedFiles: Array<String>?): Boolean {
// If we are in power save mode better to postpone scan and upload
val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)
if ((powerManagementService.isPowerSavingEnabled && !overridePowerSaving)){
return true
}

// or sync worker already running and no changed files to be processed
val alreadyRunning = backgroundJobManager.bothFilesSyncJobsRunning()
if (alreadyRunning && changedFiles.isNullOrEmpty()) {
Log_OC.d(TAG, "Kill Sync Worker since another instance of the worker seems to be running already!")
return true
}

return false
}

@Suppress("MagicNumber")
override fun doWork(): Result {
backgroundJobManager.logStartOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class))

// If we are in power save mode or sync worker already running, better to postpone upload
val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)
val alreadyRunning = backgroundJobManager.bothFilesSyncJobsRunning()
if ((powerManagementService.isPowerSavingEnabled && !overridePowerSaving) || alreadyRunning) {
if (alreadyRunning) {
Log_OC.d(TAG, "Kill Sync Worker since another instance of the worker seems to be running already!")
}
val changedFiles = inputData.getStringArray(CHANGED_FILES)

if (canExitEarly(changedFiles)) {
val result = Result.success()
backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result)
return result
}

val resources = context.resources
val lightVersion = resources.getBoolean(R.bool.syncedFolder_light)
FilesSyncHelper.restartJobsIfNeeded(
Expand All @@ -117,7 +131,6 @@ class FilesSyncWork(
)

// Get changed files from ContentObserverWork (only images and videos) or by scanning filesystem
val changedFiles = inputData.getStringArray(CHANGED_FILES)
collectChangedFiles(changedFiles)

// Create all the providers we'll need
Expand Down

0 comments on commit ac9902f

Please sign in to comment.