Skip to content

Commit

Permalink
Fix app freezes
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 2, 2022
1 parent e2f2964 commit 74876d7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 94 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/db/Db.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun database(context: Context): Database {
val driver = AndroidSqliteDriver(
schema = Database.Schema,
context = context,
name = "btcmap-v25.db",
name = "btcmap-v26.db",
factory = RequerySQLiteOpenHelperFactory(),
)

Expand Down
102 changes: 59 additions & 43 deletions app/src/main/kotlin/elements/ElementsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import android.content.Context
import app.cash.sqldelight.coroutines.*
import db.*
import http.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
Expand Down Expand Up @@ -134,11 +131,14 @@ class ElementsRepo(
)
}
} else {
clustersCache.getOrPut(step) {
db.elementQueries.selectElementClusters(step = step).asFlow()
val clusters = clustersCache.getOrPut(step) {
db.elementQueries.selectElementClusters(step = step)
.asFlow()
.mapToList(Dispatchers.IO).first()
}.filter {
box.contains(it.lat!!, it.lon!!)
}

return withContext(Dispatchers.IO) {
clusters.filter { box.contains(it.lat!!, it.lon!!) }
}
}
}
Expand Down Expand Up @@ -180,23 +180,29 @@ class ElementsRepo(
bundledElementsInputStream,
)

db.transaction {
bundledElements.forEach {
val latLon = it.getLatLon()

db.elementQueries.insertOrReplace(
Element(
id = it.id,
lat = latLon.first,
lon = latLon.second,
osm_json = it.osm_json,
tags = it.tags,
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at,
bundledElements.chunked(500).forEach { element ->
val withLatLon = element.map { Pair(it, it.getLatLon()) }

db.transaction {
withLatLon.forEach {
val latLon = it.second

db.elementQueries.insertOrReplace(
Element(
id = it.first.id,
lat = latLon.first,
lon = latLon.second,
osm_json = it.first.osm_json,
tags = it.first.tags,
created_at = it.first.created_at,
updated_at = it.first.updated_at,
deleted_at = it.first.deleted_at,
)
)
)
}
}

delay(100)
}

SyncReport(
Expand Down Expand Up @@ -226,29 +232,39 @@ class ElementsRepo(
val json = Json { ignoreUnknownKeys = true }

val elements = runCatching {
json.decodeFromStream(
ListSerializer(ElementJson.serializer()),
response.body!!.byteStream(),
)
withContext(Dispatchers.IO) {
json.decodeFromStream(
ListSerializer(ElementJson.serializer()),
response.body!!.byteStream(),
)
}
}.getOrElse { return Result.failure(it) }

db.transaction {
elements.forEach {
val latLon = it.getLatLon()

db.elementQueries.insertOrReplace(
Element(
id = it.id,
lat = latLon.first,
lon = latLon.second,
osm_json = it.osm_json,
tags = it.tags,
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at,
)
)
elements.chunked(500).forEach { element ->
withContext(Dispatchers.IO) {
val withLatLon = element.map { Pair(it, it.getLatLon()) }

db.transaction {
withLatLon.forEach {
val latLon = it.second

db.elementQueries.insertOrReplace(
Element(
id = it.first.id,
lat = latLon.first,
lon = latLon.second,
osm_json = it.first.osm_json,
tags = it.first.tags,
created_at = it.first.created_at,
updated_at = it.first.updated_at,
deleted_at = it.first.deleted_at,
)
)
}
}
}

delay(100)
}

return Result.success(
Expand Down
47 changes: 29 additions & 18 deletions app/src/main/kotlin/events/EventsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import app.cash.sqldelight.coroutines.mapToOneNotNull
import db.*
import http.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
Expand All @@ -23,7 +25,8 @@ class EventsRepo(
) {

suspend fun selectAllNotDeletedAsListItems(limit: Long): List<SelectAllNotDeletedEventsAsListItems> {
return db.eventQueries.selectAllNotDeletedEventsAsListItems(limit).asFlow().mapToList(Dispatchers.IO)
return db.eventQueries.selectAllNotDeletedEventsAsListItems(limit).asFlow()
.mapToList(Dispatchers.IO)
.first()
}

Expand Down Expand Up @@ -51,26 +54,34 @@ class EventsRepo(
val json = Json { ignoreUnknownKeys = true }

val events = runCatching {
json.decodeFromStream(
ListSerializer(EventJson.serializer()),
response.body!!.byteStream(),
)
withContext(Dispatchers.IO) {
json.decodeFromStream(
ListSerializer(EventJson.serializer()),
response.body!!.byteStream(),
)
}
}.getOrElse { return Result.failure(it) }

db.transaction {
events.forEach {
db.eventQueries.insert(
Event(
id = it.id,
type = it.type,
element_id = it.element_id,
user_id = it.user_id,
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at,
)
)
events.chunked(1000).forEach {
withContext(Dispatchers.IO) {
db.transaction {
it.forEach {
db.eventQueries.insert(
Event(
id = it.id,
type = it.type,
element_id = it.element_id,
user_id = it.user_id,
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at,
)
)
}
}
}

delay(100)
}

return Result.success(
Expand Down
37 changes: 21 additions & 16 deletions app/src/main/kotlin/reports/ReportsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import db.Database
import http.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
Expand Down Expand Up @@ -34,26 +35,30 @@ class ReportsRepo(
val json = Json { ignoreUnknownKeys = true }

val reports = runCatching {
json.decodeFromStream(
ListSerializer(ReportJson.serializer()),
response.body!!.byteStream(),
).sortedBy { it.date }.toMutableList()
withContext(Dispatchers.IO) {
json.decodeFromStream(
ListSerializer(ReportJson.serializer()),
response.body!!.byteStream(),
)
}
}.getOrNull() ?: return

db.transaction {
db.reportQueries.deleteAll()
withContext(Dispatchers.IO) {
db.transaction {
db.reportQueries.deleteAll()

reports.forEach {
db.reportQueries.insertOrReplace(
Report(
area_id = it.area_id,
date = it.date,
tags = it.tags,
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at
reports.forEach {
db.reportQueries.insertOrReplace(
Report(
area_id = it.area_id,
date = it.date,
tags = it.tags,
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at
)
)
)
}
}
}
}
Expand Down
38 changes: 22 additions & 16 deletions app/src/main/kotlin/users/UsersRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import http.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
Expand All @@ -26,7 +27,8 @@ class UsersRepo(
) {

suspend fun selectAllUsersAsListItems(): List<SelectAllUsersAsListItems> {
return db.userQueries.selectAllUsersAsListItems().asFlow().mapToList(Dispatchers.IO).first().filter { it.changes > 0 }
return db.userQueries.selectAllUsersAsListItems().asFlow().mapToList(Dispatchers.IO).first()
.filter { it.changes > 0 }
}

suspend fun selectById(id: Long): User? {
Expand All @@ -52,26 +54,30 @@ class UsersRepo(
val json = Json { ignoreUnknownKeys = true }

val users = runCatching {
json.decodeFromStream(
ListSerializer(UserJson.serializer()),
response.body!!.byteStream(),
)
withContext(Dispatchers.IO) {
json.decodeFromStream(
ListSerializer(UserJson.serializer()),
response.body!!.byteStream(),
)
}
}.getOrElse { return Result.failure(it) }

db.transaction {
users.forEach {
db.userQueries.insertOrReplace(
User(
id = it.id,
osm_json = it.osm_json.toString(),
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at ?: "",
withContext(Dispatchers.IO) {
db.transaction {
users.forEach {
db.userQueries.insertOrReplace(
User(
id = it.id,
osm_json = it.osm_json.toString(),
created_at = it.created_at,
updated_at = it.updated_at,
deleted_at = it.deleted_at ?: "",
)
)
)
}
}
}

return Result.success(
SyncReport(
timeMillis = System.currentTimeMillis() - startMillis,
Expand Down

0 comments on commit 74876d7

Please sign in to comment.