Skip to content

Commit

Permalink
Bundle users
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 30, 2024
1 parent bb536cc commit ff7786f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ build/
app/src/main/assets/elements.json
app/src/main/assets/reports.json
app/src/main/assets/events.json
app/src/main/assets/areas.json
app/src/main/assets/areas.json
app/src/main/assets/users.json
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ tasks.register("bundleData") {

val areasSrc = URL("https://static.btcmap.org/api/v2/areas.json")
File(destDir, "areas.json").writeText(areasSrc.readText())

val usersSrc = URL("https://static.btcmap.org/api/v2/users.json")
File(destDir, "users.json").writeText(usersSrc.readText())
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class Sync(
areasRepo.fetchBundledAreas()
}

if (usersRepo.selectCount() == 0L && usersRepo.hasBundledUsers()) {
usersRepo.fetchBundledUsers()
}

val elementsReport = async { elementsRepo.sync() }
val reportsReport = async { reportsRepo.sync() }
val areasReport = async { areasRepo.sync() }
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/kotlin/user/UserQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,18 @@ data class UserQueries(private val db: SQLiteOpenHelper) {
cursor.getZonedDateTime(0)
}
}

suspend fun selectCount(): Long {
return withContext(Dispatchers.IO) {
val cursor = db.readableDatabase.query(
"""
SELECT count(*)
FROM user;
"""
)

cursor.moveToNext()
cursor.getLong(0)
}
}
}
21 changes: 21 additions & 0 deletions app/src/main/kotlin/user/UsersRepo.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
package user

import android.content.Context
import api.Api
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class UsersRepo(
private val api: Api,
private val queries: UserQueries,
private val context: Context,
) {

suspend fun selectAll() = queries.selectAll()

suspend fun selectById(id: Long) = queries.selectById(id)

suspend fun selectCount() = queries.selectCount()

suspend fun hasBundledUsers(): Boolean {
return withContext(Dispatchers.IO) {
context.resources.assets.list("")!!.contains("users.json")
}
}

suspend fun fetchBundledUsers() {
withContext(Dispatchers.IO) {
context.assets.open("users.json").use { bundledUsers ->
val users = bundledUsers.toUsersJson().map { it.toUser() }
queries.insertOrReplace(users)
}
}
}

suspend fun sync(): SyncReport {
val startMillis = System.currentTimeMillis()
var count = 0L
Expand Down

0 comments on commit ff7786f

Please sign in to comment.