Skip to content

Commit

Permalink
Bundle events
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 30, 2024
1 parent ecc0148 commit a4d02b1
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 @@ -17,4 +17,5 @@ build/

# App data
app/src/main/assets/elements.json
app/src/main/assets/reports.json
app/src/main/assets/reports.json
app/src/main/assets/events.json
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ tasks.register("bundleData") {

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

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

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/kotlin/event/EventQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ class EventQueries(private val db: SQLiteOpenHelper) {
}
}

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

cursor.moveToNext()
cursor.getLong(0)
}
}

private fun getLnUrl(description: String): String {
val pattern = Pattern.compile("\\(lightning:[^)]*\\)", Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(description)
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/kotlin/event/EventsRepo.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package event

import android.content.Context
import api.Api
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.time.Duration
import java.time.ZoneOffset
import java.time.ZonedDateTime

class EventsRepo(
private val api: Api,
private val queries: EventQueries,
private val context: Context,
) {

suspend fun selectAll(limit: Long) = queries.selectAll(limit)

suspend fun selectByUserIdAsListItems(userId: Long) = queries.selectByUserId(userId)

suspend fun selectCount() = queries.selectCount()

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

suspend fun fetchBundledEvents() {
withContext(Dispatchers.IO) {
context.assets.open("events.json").use { bundledEvents ->
val events = bundledEvents.toEventsJson().map { it.toEvent() }
queries.insertOrReplace(events)
}
}
}

suspend fun sync(): SyncReport {
val startedAt = ZonedDateTime.now(ZoneOffset.UTC)
val newEvents = mutableListOf<Event>()
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 @@ -46,6 +46,10 @@ class Sync(
reportsRepo.fetchBundledReports()
}

if (eventsRepo.selectCount() == 0L && eventsRepo.hasBundledEvents()) {
eventsRepo.fetchBundledEvents()
}

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

0 comments on commit a4d02b1

Please sign in to comment.