Skip to content

Commit

Permalink
Fix re-download of index on each try
Browse files Browse the repository at this point in the history
- Add notification to `SyncWorker`
- Use embedded kotlin version in plugins
  • Loading branch information
Iamlooker committed Oct 16, 2023
1 parent ca2b837 commit ebb7b4d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import com.android.build.api.dsl.ApplicationExtension
import com.looker.droidify.configureKotlinAndroid
import com.looker.droidify.getLibrary
import com.looker.droidify.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.*

class AndroidApplicationPlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand All @@ -32,9 +28,8 @@ class AndroidApplicationPlugin : Plugin<Project> {
}
}
dependencies {
add("implementation", platform(libs.getLibrary("kotlin.bom")))
add("implementation", kotlin("stdlib"))
add("implementation", kotlin("reflect"))
add("implementation", embeddedKotlin("stdlib"))
add("implementation", embeddedKotlin("reflect"))
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions build-logic/structure/src/main/kotlin/AndroidLibraryPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import com.looker.droidify.configureKotlinAndroid
import com.looker.droidify.getLibrary
import com.looker.droidify.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.embeddedKotlin

class AndroidLibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand All @@ -34,11 +32,10 @@ class AndroidLibraryPlugin : Plugin<Project> {
}
}
dependencies {
add("implementation", platform(libs.getLibrary("kotlin.bom")))
add("implementation", kotlin("stdlib"))
add("implementation", kotlin("reflect"))
add("testImplementation", kotlin("test"))
add("androidTestImplementation", kotlin("test"))
add("implementation", embeddedKotlin("stdlib"))
add("implementation", embeddedKotlin("reflect"))
add("testImplementation", embeddedKotlin("test"))
add("androidTestImplementation", embeddedKotlin("test"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.getByType
import org.gradle.plugin.use.PluginDependency

val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

fun VersionCatalog.getPlugin(alias: String): Provider<PluginDependency> =
findPlugin(alias).get()

fun VersionCatalog.getLibrary(alias: String): Provider<MinimalExternalModuleDependency> =
findLibrary(alias).get()
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class OfflineFirstRepoRepository @Inject constructor(

override suspend fun sync(repo: Repo): Boolean = coroutineScope {
val index = try {
indexManager.getIndex(listOf(repo))[repo]!!
indexManager.getIndex(listOf(repo))[repo] ?: throw Exception("Empty index returned")
} catch (e: Exception) {
e.exceptCancellation()
return@coroutineScope false
Expand All @@ -81,13 +81,15 @@ class OfflineFirstRepoRepository @Inject constructor(
override suspend fun syncAll(): Boolean = supervisorScope {
val repos = repoDao.getRepoStream().first().filter { it.enabled }
val indices = try {
indexManager.getIndex(repos.toExternal(locale))
indexManager
.getIndex(repos.toExternal(locale))
.filter { (_, index) -> index != null }
} catch (e: Exception) {
e.exceptCancellation()
return@supervisorScope false
}
indices.forEach { (repo, index) ->
val updatedRepo = index.repo.toEntity(
val updatedRepo = index!!.repo.toEntity(
id = repo.id,
fingerprint = repo.fingerprint,
username = repo.authentication.username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class IndexDownloaderImpl @Inject constructor(
fileEntry = entry
}
val (_, response) = downloadIndexFile(repo, ENTRY_FILE_NAME, validator)
if (repoFingerprint == null || fileEntry == null || repoFingerprint?.isBlank() == true || response is NetworkResponse.Error)
if (repoFingerprint == null || fileEntry == null || repoFingerprint?.isBlank() == true || response is NetworkResponse.Error.Validation)
throw IllegalStateException("Empty Fingerprint")
IndexDownloadResponse(
index = fileEntry!!,
Expand Down Expand Up @@ -109,8 +109,7 @@ class IndexDownloaderImpl @Inject constructor(
repo.authentication.username,
repo.authentication.password
)
if (repo.versionInfo.etag != null) etag(repo.versionInfo.etag!!)
else if (repo.versionInfo.timestamp > 0L) ifModifiedSince(Date(repo.versionInfo.timestamp))
if (repo.versionInfo.timestamp > 0L) ifModifiedSince(Date(repo.versionInfo.timestamp))
}
)
tempFile to response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IndexManager(

suspend fun getIndex(
repos: List<Repo>
): Map<Repo, IndexV2> = withContext(Dispatchers.Default) {
): Map<Repo, IndexV2?> = withContext(Dispatchers.Default) {
repos.associate { repo ->
when (indexDownloader.determineIndexType(repo)) {
IndexType.INDEX_V1 -> {
Expand All @@ -28,12 +28,14 @@ class IndexManager(

IndexType.ENTRY -> {
val response = indexDownloader.downloadEntry(repo)
val diff = response.index.getDiff(repo.versionInfo.timestamp)
repo.update(
val updatedRepo = repo.update(
fingerprint = response.fingerprint,
timestamp = response.lastModified,
etag = response.etag
) to downloadIndexBasedOnDiff(repo, diff)
)
if (response.lastModified == repo.versionInfo.timestamp) return@associate updatedRepo to null
val diff = response.index.getDiff(repo.versionInfo.timestamp)
updatedRepo to downloadIndexBasedOnDiff(repo, diff)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ class SyncWorker @AssistedInject constructor(

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
Log.i(SYNC_WORK, "Start Sync")
val isSuccess = repoRepository.syncAll()
setForegroundAsync(appContext.syncForegroundInfo())
val isSuccess = try {
repoRepository.syncAll()
} catch (e: Exception) {
e.printStackTrace()
return@withContext Result.failure()
}
if (isSuccess) Result.success() else Result.failure()
}

Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ hilt-ext-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.r
hilt-android-testing = { group = "com.google.dagger", name = "hilt-android-testing", version.ref = "hilt" }
junit4 = { group = "junit", name = "junit", version.ref = "junit4" }
jackson-core = { group = "com.fasterxml.jackson.core", name = "jackson-core", version.ref = "jackson" }
kotlin-bom = { group = "org.jetbrains.kotlin", name = "kotlin-bom", version.ref = "kotlin" }
kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinxCoroutines" }
kotlinx-coroutines-guava = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-guava", version.ref = "kotlinxCoroutines" }
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
Expand Down

0 comments on commit ebb7b4d

Please sign in to comment.