Skip to content

Commit

Permalink
Migrating to Proto-DataStore (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker authored Oct 22, 2023
1 parent 11feb14 commit b8ec34d
Show file tree
Hide file tree
Showing 33 changed files with 355 additions and 187 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.looker.android.application)
alias(libs.plugins.looker.hilt.work)
alias(libs.plugins.looker.lint)
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class AndroidApplicationPlugin : Plugin<Project> {
with(pluginManager) {
apply("com.android.application")
apply("org.jetbrains.kotlin.android")
apply("looker.lint")
}

extensions.configure<ApplicationExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AndroidLibraryPlugin : Plugin<Project> {
with(pluginManager) {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
apply("looker.lint")
}

extensions.configure<LibraryExtension> {
Expand Down
2 changes: 1 addition & 1 deletion build-logic/structure/src/main/kotlin/AndroidLintPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AndroidLintPlugin : Plugin<Project> {
tasks.getByPath("preBuild").dependsOn("ktlintFormat")
extensions.configure<KtlintExtension> {
android.set(true)
ignoreFailures.set(false)
ignoreFailures.set(true)
debug.set(true)
reporters {
reporter(ReporterType.HTML)
Expand Down
1 change: 1 addition & 0 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.android.build.gradle.internal.tasks.factory.dependsOn

plugins {
alias(libs.plugins.looker.android.library)
alias(libs.plugins.looker.lint)
}

android {
Expand Down
6 changes: 3 additions & 3 deletions core/common/src/main/java/com/looker/core/common/Text.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ fun <T : CharSequence> T.nullIfEmpty(): T? {
fun String.stripBetween(prefix: String, suffix: String = prefix): String {
val prefixIndex = indexOf(prefix)
val suffixIndex = lastIndexOf(suffix)
val isRangeValid = prefixIndex != -1
&& suffixIndex != -1
&& prefixIndex != suffixIndex
val isRangeValid = prefixIndex != -1 &&
suffixIndex != -1 &&
prefixIndex != suffixIndex
return if (isRangeValid) {
substring(0, prefixIndex + 1) + substring(suffixIndex + 1)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ fun Service.stopForegroundCompat(removeNotification: Boolean = true) {
@Suppress("DEPRECATION")
if (SdkCheck.isNougat) {
stopForeground(
if (removeNotification) Service.STOP_FOREGROUND_REMOVE
else Service.STOP_FOREGROUND_DETACH
if (removeNotification) {
Service.STOP_FOREGROUND_REMOVE
} else {
Service.STOP_FOREGROUND_DETACH
}
)
} else {
stopForeground(removeNotification)
Expand Down
1 change: 1 addition & 0 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.looker.android.library)
alias(libs.plugins.looker.hilt.work)
alias(libs.plugins.looker.lint)
}

android {
Expand Down
4 changes: 2 additions & 2 deletions core/data/src/main/java/com/looker/core/data/fdroid/Mapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ fun PackageV2.toEntity(
?: emptyMap(),
tenInchScreenshots = metadata.screenshots?.tenInch?.mapValues { it.value.map { it.name } }
?: emptyMap(),
sevenInchScreenshots = metadata.screenshots?.sevenInch?.mapValues { it.value.map { it.name } }
?: emptyMap(),
sevenInchScreenshots = metadata.screenshots?.sevenInch
?.mapValues { it.value.map { it.name } } ?: emptyMap(),
tvScreenshots = metadata.screenshots?.tv?.mapValues { it.value.map { it.name } }
?: emptyMap(),
wearScreenshots = metadata.screenshots?.wear?.mapValues { it.value.map { it.name } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class IndexDownloaderImpl @Inject constructor(
fileIndex = index
}
val (_, response) = downloadIndexFile(repo, INDEX_V1_FILE_NAME, validator)
if (repoFingerprint == null || fileIndex == null || repoFingerprint?.isBlank() == true || response is NetworkResponse.Error)
val isFingerprintAndIndexValid = repoFingerprint == null
|| fileIndex == null
|| repoFingerprint?.isBlank() == true
|| response is NetworkResponse.Error
if (isFingerprintAndIndexValid)
throw IllegalStateException("Fingerprint: $repoFingerprint, Index: $fileIndex")
IndexDownloadResponse(
index = fileIndex!!,
Expand Down Expand Up @@ -78,7 +82,11 @@ 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.Validation)
val isFingerprintAndIndexValid = repoFingerprint == null
|| fileEntry == null
|| repoFingerprint?.isBlank() == true
|| response is NetworkResponse.Error.Validation
if (isFingerprintAndIndexValid)
throw IllegalStateException("Empty Fingerprint")
IndexDownloadResponse(
index = fileEntry!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class IndexManager(
timestamp = response.lastModified,
etag = response.etag
)
if (response.lastModified == repo.versionInfo.timestamp) return@associate updatedRepo to null
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 @@ -34,7 +34,6 @@ class CollectionConverter {

@TypeConverter
fun stringToList(byteArray: ByteArray): List<String> = String(byteArray).split(STRING_DELIMITER)

}

class LocalizedConverter {
Expand All @@ -54,7 +53,6 @@ class LocalizedConverter {
@TypeConverter
fun jsonToLocalizedList(jsonObject: String): LocalizedList =
json.decodeFromString(localizedListSerializer, jsonObject)

}

class PackageEntityConverter {
Expand All @@ -74,7 +72,6 @@ class PackageEntityConverter {
@TypeConverter
fun stringToPackageList(jsonString: String): List<PackageEntity> =
json.decodeFromString(packageListSerializer, jsonString)

}

class RepoConverter {
Expand All @@ -94,5 +91,4 @@ class RepoConverter {
@TypeConverter
fun stringToCategory(string: String): Map<String, CategoryEntity> =
json.decodeFromString(categorySerializer, string)

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ abstract class DroidifyDatabase : RoomDatabase() {
abstract fun repoDao(): RepoDao

abstract fun installedDao(): InstalledDao

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ interface AppDao {
"""
)
suspend fun deleteApps(repoId: Long)

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ interface InstalledDao {

@Delete
suspend fun deleteInstalled(installedEntity: InstalledEntity)

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ interface RepoDao {
"""
)
suspend fun deleteRepo(id: Long)

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ object DaoModule {
fun provideInstalledDao(
database: DroidifyDatabase
): InstalledDao = database.installedDao()

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ object DatabaseModule {
DroidifyDatabase::class.java,
"droidify-database"
).createFromAsset("repo.db").build()

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fun List<AppEntity>.toExternal(
private fun AppEntity.author(): Author = Author(
name = authorName,
email = authorEmail,
web = authorWebSite,
web = authorWebSite
)

private fun AppEntity.donations(): Donation = Donation(
Expand All @@ -92,7 +92,7 @@ private fun AppEntity.donations(): Donation = Donation(
liteCoinAddress = litecoin.nullIfEmpty(),
openCollectiveId = openCollective.nullIfEmpty(),
librePayId = liberapayID.nullIfEmpty(),
librePayAddress = liberapay.nullIfEmpty(),
librePayAddress = liberapay.nullIfEmpty()
)

private fun AppEntity.graphics(locale: String): Graphics = Graphics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ data class InstalledEntity(
val packageName: String,
val versionCode: Long,
val signature: String
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun PackageEntity.toExternal(locale: String, installed: Boolean): Package = Pack
versionName = versionName,
usesSDKs = SDKs(minSdkVersion, targetSdkVersion),
signer = setOf(signer),
permissions = usesPermission.map(PermissionEntity::toExternalModel),
permissions = usesPermission.map(PermissionEntity::toExternalModel)
),
platforms = Platforms(nativeCode),
features = features,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import androidx.core.os.LocaleListCompat
import com.looker.core.common.stripBetween
import java.util.Locale

internal fun localeListCompat(tag: String): LocaleListCompat = LocaleListCompat.forLanguageTags(tag)
internal fun localeListCompat(tag: String): LocaleListCompat =
LocaleListCompat.forLanguageTags(tag)

/**
* Find the Localized value from [Map<String,T>] using [locale]
Expand All @@ -27,7 +28,6 @@ fun <T> Map<String, T>?.localizedValue(locale: String): T? {
*
* Returns null if none found
*/
@OptIn(ExperimentalStdlibApi::class)
internal fun LocaleListCompat.suitableLocale(keys: Set<String>): String? = (0..<size())
.asSequence()
.mapNotNull { get(it).suitableTag(keys) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.looker.core.database.utils
package com.looker.core.database

import androidx.core.os.LocaleListCompat
import androidx.core.os.LocaleListCompat.getEmptyLocaleList
import com.looker.core.database.utils.localeListCompat
import com.looker.core.database.utils.localizedValue
import com.looker.core.database.utils.suitableLocale
import com.looker.core.database.utils.suitableTag
import org.junit.Test
import java.util.Locale
import kotlin.test.assertEquals
Expand All @@ -14,7 +18,7 @@ import kotlin.test.assertNull
*
* https://developer.android.com/guide/topics/resources/multilingual-support#resource-resolution-examples
*/
class LocalizationKtTest {
class LocalizationTest {

@Test
fun `Get correct localeList`() {
Expand All @@ -34,27 +38,27 @@ class LocalizationKtTest {
fun `Fallback to english`() {
assertEquals(
"en",
getMap("de-AT", "de-DE", "en").localizedValue("fr-FR"),
getMap("de-AT", "de-DE", "en").localizedValue("fr-FR")
)
assertEquals(
"en-US",
getMap("en", "en-US").localizedValue("zh-Hant-TW,zh-Hans-CN"),
getMap("en", "en-US").localizedValue("zh-Hant-TW,zh-Hans-CN")
)
}

@Test
fun `Use the first selected locale, en_US`() {
assertEquals(
"en-US",
getMap("de-AT", "de-DE", "en-US").localizedValue("en-US,de-DE"),
getMap("de-AT", "de-DE", "en-US").localizedValue("en-US,de-DE")
)
}

@Test
fun `Use the first en translation`() {
assertEquals(
"en-US",
getMap("de-AT", "de-DE", "en-US").localizedValue("en-SE,de-DE"),
getMap("de-AT", "de-DE", "en-US").localizedValue("en-SE,de-DE")
)
}

Expand All @@ -67,11 +71,11 @@ class LocalizationKtTest {
"de-DE",
"en-GB",
"en-US"
).localizedValue("de-AT,de-DE"),
).localizedValue("de-AT,de-DE")
)
assertEquals(
"de",
getMap("de-AT", "de", "en-GB", "en-US").localizedValue("de-CH,en-US"),
getMap("de-AT", "de", "en-GB", "en-US").localizedValue("de-CH,en-US")
)
}

Expand All @@ -84,7 +88,7 @@ class LocalizationKtTest {
"zh-CN",
"zh-HK",
"zh-TW"
).localizedValue("zh-Hant-TW,zh-Hans-CN"),
).localizedValue("zh-Hant-TW,zh-Hans-CN")
)
}

Expand All @@ -93,13 +97,13 @@ class LocalizationKtTest {
assertEquals(
"fr-FR",
getMap("en-US", "de-DE", "es-ES", "fr-FR", "it-IT")
.localizedValue("fr-CH"),
.localizedValue("fr-CH")
)

assertEquals(
"it-IT",
getMap("en-US", "de-DE", "es-ES", "it-IT")
.localizedValue("fr-CH,it-CH"),
.localizedValue("fr-CH,it-CH")
)
}

Expand Down
6 changes: 5 additions & 1 deletion core/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
alias(libs.plugins.looker.android.library)
alias(libs.plugins.looker.hilt)
alias(libs.plugins.looker.lint)
alias(libs.plugins.kotlin.serialization)
}

android {
Expand All @@ -20,7 +22,9 @@ android {

dependencies {
modules(Modules.coreCommon)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.dataStore.core)
implementation(libs.androidx.dataStore.proto)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.datetime)
}
Loading

0 comments on commit b8ec34d

Please sign in to comment.