Skip to content

Commit

Permalink
Synchronize periodically only if there are changes to be synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Jun 7, 2023
1 parent 5295871 commit d4b6de4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1293,10 +1293,12 @@ class MainBibleActivity : CustomTitlebarActivityBase() {
Log.i(TAG, "Periodic sync starting")
try {
while (true) {
Log.i(TAG, "Performing periodic sync")
windowRepository.saveIntoDb(false)
DeviceSynchronize.start()
DeviceSynchronize.waitUntilFinished()
if(DeviceSynchronize.hasChanges()) {
Log.i(TAG, "Performing periodic sync")
DeviceSynchronize.start()
DeviceSynchronize.waitUntilFinished()
}
delay(CommonUtils.settings.getLong("gdrive_sync_interval", 60L) * 1000)
}
} catch (e: StopSync) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class SyncableDatabaseDefinition<T: SyncableRoomDatabase>(
}
}

val hasChanges: Boolean get() {
val lastSynchronized = dao.getLong(LAST_SYNCHRONIZED_KEY)?: 0
return dao.countNewLogEntries(lastSynchronized, deviceId) > 0
}

val categoryName get() = category.name.lowercase()
val dao get() = localDb.syncDao()
val writableDb get() = localDb.openHelper.writableDatabase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,11 @@ object DeviceSynchronize {
file.delete()
}

suspend fun hasChanges(): Boolean =
DatabaseContainer.dbDefFactories.asyncMap {
val dbDef = it.invoke()
dbDef.category.enabled && dbDef.hasChanges
}.any { it }

const val TAG = "GoogleDrive"
}

0 comments on commit d4b6de4

Please sign in to comment.