Skip to content

Commit 9460b17

Browse files
authored
Merge pull request #30 from kdroidFilter/add-downloader-module
Add downloader module
2 parents 53f7cc2 + 4b5c7fe commit 9460b17

20 files changed

Lines changed: 768 additions & 96 deletions

File tree

README.MD

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,102 @@ dependencies {
364364
implementation("io.github.kdroidfilter.database:localization:<version>")
365365
}
366366
```
367+
368+
### Downloader Module
369+
370+
The downloader module provides functionality to download the latest store and policies databases from GitHub releases. It includes methods to download databases for multiple languages.
371+
372+
```kotlin
373+
dependencies {
374+
implementation("io.github.kdroidfilter.database.downloader:core:<version>")
375+
}
376+
```
377+
378+
#### Usage Example
379+
380+
```kotlin
381+
// Create a DatabaseDownloader instance
382+
val databaseDownloader = DatabaseDownloader()
383+
384+
// Download store databases for all languages (en, fr, he)
385+
val outputDir = "path/to/output/directory"
386+
val storeResults = databaseDownloader.downloadLatestStoreDatabases(outputDir)
387+
388+
// Check download results
389+
storeResults.forEach { (language, success) ->
390+
if (success) {
391+
println("Successfully downloaded store database for $language")
392+
} else {
393+
println("Failed to download store database for $language")
394+
}
395+
}
396+
397+
// Download policies database
398+
val policiesResult = databaseDownloader.downloadLatestPoliciesDatabase(outputDir)
399+
if (policiesResult) {
400+
println("Successfully downloaded policies database")
401+
} else {
402+
println("Failed to download policies database")
403+
}
404+
```
405+
406+
### DAO Module
407+
408+
The DAO (Data Access Object) module provides a clean interface for database operations, abstracting away direct SQL queries. It includes DAOs for accessing application data and version information, as well as utility classes for working with the data.
409+
410+
```kotlin
411+
dependencies {
412+
implementation("io.github.kdroidfilter.database.dao:core:<version>")
413+
}
414+
```
415+
416+
#### Key Components
417+
418+
- **ApplicationsDao**: Provides methods for loading and searching applications in the database
419+
- **VersionDao**: Handles database version management operations
420+
- **AppInfoWithExtras**: A data class that extends the GooglePlayApplicationInfo model with additional information
421+
422+
#### Usage Example
423+
424+
```kotlin
425+
// Load applications from the database
426+
val applications = ApplicationsDao.loadApplicationsFromDatabase(
427+
database = database,
428+
deviceLanguage = "en",
429+
creator = { id, categoryLocalizedName, appInfo ->
430+
AppInfoWithExtras(
431+
id = id,
432+
categoryLocalizedName = categoryLocalizedName,
433+
app = appInfo
434+
)
435+
}
436+
)
437+
438+
// Search for applications in the database
439+
val searchResults = ApplicationsDao.searchApplicationsInDatabase(
440+
database = database,
441+
query = "calculator",
442+
deviceLanguage = "en",
443+
creator = { id, categoryLocalizedName, appInfo ->
444+
AppInfoWithExtras(
445+
id = id,
446+
categoryLocalizedName = categoryLocalizedName,
447+
app = appInfo
448+
)
449+
}
450+
)
451+
452+
// Get the current database version
453+
val currentVersion = VersionDao.getCurrentVersion(database)
454+
455+
// Update the database version
456+
val updateSuccess = VersionDao.updateVersion(database, "NEWVERSION")
457+
```
458+
459+
#### Note on Sample Code
460+
461+
For a simplified overview of how to use the database, please consult the example in the `sample` directory. The sample demonstrates basic database operations including downloading, querying, and displaying data.
462+
463+
> ⚠️ **Important Warning**: The sample code uses `runBlocking` for database downloads, which is **prohibited** in production code. This is only done for demonstration purposes. In real applications, always use proper coroutine scopes and avoid blocking the main thread.
464+
465+
The DAO module is actively evolving to satisfy more needs and use cases. Contributions and pull requests are welcome to enhance its functionality and performance.

dao/src/commonMain/kotlin/io/github/kdroidfilter/database/dao/AppModels.kt renamed to dao/src/commonMain/kotlin/io/github/kdroidfilter/database/dao/AppInfoWithExtras.kt

File renamed without changes.

dao/src/commonMain/kotlin/io/github/kdroidfilter/database/dao/ApplicationsDao.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ object ApplicationsDao {
3333
score = app.score ?: 0.0,
3434
ratings = app.ratings ?: 0L,
3535
reviews = app.reviews ?: 0L,
36-
histogram = app.histogram?.removeSurrounding("[", "]")?.split(", ")?.map { it.toLongOrNull() ?: 0L } ?: emptyList(),
36+
histogram = app.histogram?.removeSurrounding("[", "]")?.split(", ")?.map {
37+
it.toLongOrNull() ?: 0L
38+
} ?: emptyList(),
3739
price = app.price ?: 0.0,
3840
free = app.free == 1L,
3941
currency = app.currency ?: "",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.kdroidfilter.database.dao
2+
3+
import co.touchlab.kermit.Logger
4+
import io.github.kdroidfilter.database.store.Database
5+
6+
/**
7+
* Data Access Object for Version
8+
* Contains functions for database operations related to version information
9+
*/
10+
object VersionDao {
11+
private val logger = Logger.withTag("VersionDao")
12+
13+
/**
14+
* Gets the current version from the database
15+
* @param database The database instance
16+
* @return The version string or null if no version is found
17+
*/
18+
fun getCurrentVersion(database: Database): String? {
19+
try {
20+
val versionQueries = database.versionQueries
21+
val version = versionQueries.getVersion().executeAsOneOrNull()
22+
return version?.release_name
23+
} catch (e: Exception) {
24+
logger.e(e) { "Failed to get current version: ${e.message}" }
25+
return null
26+
}
27+
}
28+
29+
/**
30+
* Updates the version in the database
31+
* @param database The database instance
32+
* @param versionName The new version name
33+
* @return Boolean indicating whether the update was successful
34+
*/
35+
fun updateVersion(database: Database, versionName: String): Boolean {
36+
return try {
37+
val versionQueries = database.versionQueries
38+
39+
// Clear existing versions
40+
versionQueries.clearVersions()
41+
42+
// Insert new version
43+
versionQueries.insertVersion(versionName)
44+
45+
logger.i { "Version updated to $versionName" }
46+
true
47+
} catch (e: Exception) {
48+
logger.e(e) { "Failed to update version: ${e.message}" }
49+
false
50+
}
51+
}
52+
}

downloader/build.gradle.kts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import com.vanniktech.maven.publish.SonatypeHost
2+
3+
plugins {
4+
alias(libs.plugins.multiplatform)
5+
alias(libs.plugins.android.library)
6+
alias(libs.plugins.vannitktech.maven.publish)
7+
}
8+
9+
val ref = System.getenv("GITHUB_REF") ?: ""
10+
version = if (ref.startsWith("refs/tags/")) {
11+
val tag = ref.removePrefix("refs/tags/")
12+
if (tag.startsWith("v")) tag.substring(1) else tag
13+
} else "dev"
14+
15+
16+
kotlin {
17+
jvmToolchain(17)
18+
androidTarget { publishLibraryVariants("release") }
19+
jvm()
20+
21+
22+
sourceSets {
23+
24+
androidMain.dependencies {
25+
implementation(libs.kotlinx.coroutines.android)
26+
}
27+
28+
jvmMain.dependencies {
29+
implementation(libs.maven.slf4j.provider)
30+
implementation(libs.kotlinx.coroutines.swing)
31+
}
32+
33+
jvmTest.dependencies {
34+
implementation(kotlin("test"))
35+
implementation(libs.kotlinx.coroutines.test)
36+
37+
}
38+
39+
commonMain.dependencies {
40+
api(project(":core"))
41+
api(libs.gplay.scrapper)
42+
implementation(libs.kotlinx.coroutines.core)
43+
implementation(libs.kermit)
44+
implementation(libs.platform.tools.release.fetcher)
45+
}
46+
47+
}
48+
49+
//https://kotlinlang.org/docs/native-objc-interop.html#export-of-kdoc-comments-to-generated-objective-c-headers
50+
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
51+
compilations["main"].compileTaskProvider.configure {
52+
compilerOptions {
53+
freeCompilerArgs.add("-Xexport-kdoc")
54+
}
55+
}
56+
}
57+
58+
}
59+
60+
android {
61+
namespace = "io.github.kdroidfilter.database.downloader"
62+
compileSdk = 35
63+
64+
defaultConfig {
65+
minSdk = 24
66+
}
67+
}
68+
69+
70+
mavenPublishing {
71+
coordinates(
72+
groupId = "io.github.kdroidfilter.database.downloader",
73+
artifactId = "core",
74+
version = version.toString()
75+
)
76+
77+
pom {
78+
name.set("KDroid Database Downloader")
79+
description.set("Downloader of the Kdroid Database")
80+
inceptionYear.set("2025")
81+
url.set("https://github.com/kdroidFilter/KDroidDatabase")
82+
83+
licenses {
84+
license {
85+
name.set("MIT License")
86+
url.set("https://opensource.org/licenses/MIT")
87+
}
88+
}
89+
90+
developers {
91+
developer {
92+
id.set("kdroidfilter")
93+
name.set("Elie Gambache")
94+
email.set("elyahou.hadass@gmail.com")
95+
}
96+
}
97+
98+
scm {
99+
connection.set("scm:git:git://github.com/kdroidFilter/KDroidDatabase.git")
100+
developerConnection.set("scm:git:ssh://git@github.com/kdroidFilter/KDroidDatabase.git")
101+
url.set("https://github.com/kdroidFilter/KDroidDatabase")
102+
}
103+
}
104+
105+
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
106+
107+
signAllPublications()
108+
}

0 commit comments

Comments
 (0)