Skip to content

Commit

Permalink
Bundle areas
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 30, 2024
1 parent a4d02b1 commit bb536cc
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 @@ -18,4 +18,5 @@ build/
# App data
app/src/main/assets/elements.json
app/src/main/assets/reports.json
app/src/main/assets/events.json
app/src/main/assets/events.json
app/src/main/assets/areas.json
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ tasks.register("bundleData") {

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

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

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/kotlin/area/AreaQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,18 @@ class AreaQueries(private val db: SQLiteOpenHelper) {
rows
}
}

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

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

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

class AreasRepo(
private val api: Api,
private val queries: AreaQueries,
private val context: Context,
) {

suspend fun selectById(id: String) = queries.selectById(id)
Expand All @@ -13,6 +17,23 @@ class AreasRepo(

suspend fun selectMeetups() = queries.selectMeetups()

suspend fun selectCount() = queries.selectCount()

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

suspend fun fetchBundledAreas() {
withContext(Dispatchers.IO) {
context.assets.open("areas.json").use { bundledAreas ->
val areas = bundledAreas.toAreasJson().map { it.toArea() }
queries.insertOrReplace(areas)
}
}
}

suspend fun sync(): SyncReport {
val startMillis = System.currentTimeMillis()
var count = 0L
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 @@ -50,6 +50,10 @@ class Sync(
eventsRepo.fetchBundledEvents()
}

if (areasRepo.selectCount() == 0L && areasRepo.hasBundledAreas()) {
areasRepo.fetchBundledAreas()
}

val elementsReport = async { elementsRepo.sync() }
val reportsReport = async { reportsRepo.sync() }
val areasReport = async { areasRepo.sync() }
Expand Down

0 comments on commit bb536cc

Please sign in to comment.