From 56f92980ff7a9d4dd8fded0831fa7d66d2c129fa Mon Sep 17 00:00:00 2001 From: Melisa69 Date: Mon, 18 Jul 2022 09:33:26 +0200 Subject: [PATCH] Remove unnecessary delay causing TTFB to be longer then expected. --- sdk/config/gradle/versioning.gradle | 2 +- sdk/src/main/java/me/digi/sdk/Init.kt | 148 +++++++++++++------------- 2 files changed, 73 insertions(+), 77 deletions(-) diff --git a/sdk/config/gradle/versioning.gradle b/sdk/config/gradle/versioning.gradle index b067c38..292c32e 100644 --- a/sdk/config/gradle/versioning.gradle +++ b/sdk/config/gradle/versioning.gradle @@ -1,7 +1,7 @@ ext.versions = [ // Main SDK - 'sdk': '4.0.8', + 'sdk': '4.0.9', // Build time plugins 'androidgradleplugin': '3.4.2', diff --git a/sdk/src/main/java/me/digi/sdk/Init.kt b/sdk/src/main/java/me/digi/sdk/Init.kt index 3a0c832..0bc060e 100644 --- a/sdk/src/main/java/me/digi/sdk/Init.kt +++ b/sdk/src/main/java/me/digi/sdk/Init.kt @@ -648,60 +648,56 @@ class Init( DMELog.d("Session data poll scheduled.") - Handler(Looper.getMainLooper()).postDelayed({ + DMELog.d("Fetching file list.") + if (activeSyncStatus != FileList.SyncStatus.COMPLETED()) { + readFileList(userAccessToken) { fileList, listFetchError -> - DMELog.d("Fetching file list.") - if (activeSyncStatus != FileList.SyncStatus.COMPLETED()) { - readFileList(userAccessToken) { fileList, listFetchError -> + when { + fileList != null -> DMELog.d("File list obtained; Sync syncStatus is ${fileList.syncStatus.rawValue}.") + listFetchError != null -> DMELog.d("Error fetching file list: ${listFetchError.message}.") + } - when { - fileList != null -> DMELog.d("File list obtained; Sync syncStatus is ${fileList.syncStatus.rawValue}.") - listFetchError != null -> DMELog.d("Error fetching file list: ${listFetchError.message}.") - } + val syncStatus = fileList?.syncStatus ?: FileList.SyncStatus.RUNNING() - val syncStatus = fileList?.syncStatus ?: FileList.SyncStatus.RUNNING() + latestFileList = fileList + val updatedFileIds = fileListItemCache?.updateCacheWithItemsAndDeduceChanges( + fileList?.fileList.orEmpty() + ).orEmpty() - latestFileList = fileList - val updatedFileIds = fileListItemCache?.updateCacheWithItemsAndDeduceChanges( - fileList?.fileList.orEmpty() - ).orEmpty() + DMELog.i( + "${ + fileList?.fileList.orEmpty().count() + } files discovered. Of these, ${updatedFileIds.count()} have updates and need downloading." + ) - DMELog.i( - "${ - fileList?.fileList.orEmpty().count() - } files discovered. Of these, ${updatedFileIds.count()} have updates and need downloading." + if (updatedFileIds.count() > 0 && fileList != null) { + fileListUpdateHandler?.invoke(fileList, updatedFileIds) + stalePollCount = 0 + } else if (++stalePollCount == max(configuration.maxStalePolls, 20)) { + fileListCompletionHandler?.invoke( + fileList, + SDKError.FileListPollingTimeout() ) + return@readFileList + } - if (updatedFileIds.count() > 0 && fileList != null) { - fileListUpdateHandler?.invoke(fileList, updatedFileIds) - stalePollCount = 0 - } else if (++stalePollCount == max(configuration.maxStalePolls, 20)) { - fileListCompletionHandler?.invoke( - fileList, - SDKError.FileListPollingTimeout() - ) - return@readFileList + when (syncStatus) { + FileList.SyncStatus.PENDING(), + FileList.SyncStatus.RUNNING() -> { + DMELog.i("Sync still in progress, continuing to poll for updates.") + scheduleNextPoll(userAccessToken) } - - when (syncStatus) { - FileList.SyncStatus.PENDING(), - FileList.SyncStatus.RUNNING() -> { - DMELog.i("Sync still in progress, continuing to poll for updates.") - scheduleNextPoll(userAccessToken) - } - FileList.SyncStatus.COMPLETED(), - FileList.SyncStatus.PARTIAL() -> fileListCompletionHandler?.invoke( - fileList, - listFetchError - ) - else -> Unit - } - - activeSyncStatus = syncStatus + FileList.SyncStatus.COMPLETED(), + FileList.SyncStatus.PARTIAL() -> fileListCompletionHandler?.invoke( + fileList, + listFetchError + ) + else -> Unit } - } - }, 3000) + activeSyncStatus = syncStatus + } + } } private fun completeDeliveryOfSessionData(error: Error?) { @@ -1381,7 +1377,8 @@ class Init( Compressor.decompressData(contentBytes, compression) FileItem().copy( - fileContent = String(decompressedContentBytes)) + fileContent = String(decompressedContentBytes) + ) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -1474,7 +1471,8 @@ class Init( Compressor.decompressData(contentBytes, compression) FileItem().copy( - fileContent = String(decompressedContentBytes)) + fileContent = String(decompressedContentBytes) + ) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -1560,45 +1558,43 @@ class Init( downloadHandler: FileContentCompletion, completion: FileListCompletion ) { - Handler(Looper.getMainLooper()).postDelayed({ - DMELog.i(context.getString(R.string.labelReadingFiles)) + DMELog.i(context.getString(R.string.labelReadingFiles)) - activeFileDownloadHandler = downloadHandler - activeSessionDataFetchCompletionHandler = completion + activeFileDownloadHandler = downloadHandler + activeSessionDataFetchCompletionHandler = completion - getSessionFileList(userAccessToken, { _, updatedFileIds -> + getSessionFileList(userAccessToken, { _, updatedFileIds -> - updatedFileIds.forEach { + updatedFileIds.forEach { - activeDownloadCount++ - DMELog.i("Downloading file with ID: $it.") + activeDownloadCount++ + DMELog.i("Downloading file with ID: $it.") - getSessionData(it) { file, error -> + getSessionData(it) { file, error -> - when { - file != null -> DMELog.i("Successfully downloaded updates for file with ID: $it.") - else -> DMELog.e("Failed to download updates for file with ID: $it.") - } - - downloadHandler.invoke(file, error) - activeDownloadCount-- + when { + file != null -> DMELog.i("Successfully downloaded updates for file with ID: $it.") + else -> DMELog.e("Failed to download updates for file with ID: $it.") } - } - }) { fileList, error -> - if (fileList?.syncStatus == FileList.SyncStatus.COMPLETED() && error == null) { - completion( - fileList, - null - ) // We only want to push this if the error exists, else - // it'll cause a premature loop exit. - } else if (fileList?.syncStatus == FileList.SyncStatus.PARTIAL()) { - completion( - fileList, - error - ) + downloadHandler.invoke(file, error) + activeDownloadCount-- } } - }, 3000) + + }) { fileList, error -> + if (fileList?.syncStatus == FileList.SyncStatus.COMPLETED() && error == null) { + completion( + fileList, + null + ) // We only want to push this if the error exists, else + // it'll cause a premature loop exit. + } else if (fileList?.syncStatus == FileList.SyncStatus.PARTIAL()) { + completion( + fileList, + error + ) + } + } } } \ No newline at end of file