Skip to content

Commit 2cb435a

Browse files
committed
Optimize area image loading
1 parent 293f394 commit 2cb435a

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

app/src/main/kotlin/org/btcmap/api/Api.kt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ data class GetAreasItem(
3131
val name: String,
3232
val type: String,
3333
val urlAlias: String,
34-
val icon: String?,
3534
val websiteUrl: String,
3635
)
3736

@@ -249,7 +248,9 @@ class Api(private val httpClient: OkHttpClient, private val url: HttpUrl) {
249248
endsAt = if (!it.has("ends_at") || it.get("ends_at").isJsonNull) null else ZonedDateTime.parse(
250249
it.get("ends_at").asString
251250
),
252-
cronSchedule = if (!it.has("cron_schedule") || it.get("cron_schedule").isJsonNull) null else it.get("cron_schedule").asString,
251+
cronSchedule = if (!it.has("cron_schedule") || it.get("cron_schedule").isJsonNull) null else it.get(
252+
"cron_schedule"
253+
).asString,
253254
)
254255
}
255256
}
@@ -274,8 +275,6 @@ class Api(private val httpClient: OkHttpClient, private val url: HttpUrl) {
274275
name = it.get("name").asString,
275276
type = it.get("type").asString,
276277
urlAlias = it.get("url_alias").asString,
277-
icon = if (!it.has("icon") || it.get("icon").isJsonNull) null else it.get("icon").asString
278-
.ifBlank { null },
279278
websiteUrl = it.get("website_url").asString,
280279
)
281280
}
@@ -325,10 +324,16 @@ class Api(private val httpClient: OkHttpClient, private val url: HttpUrl) {
325324
name = body.get("name").asString,
326325
type = body.get("type").asString,
327326
urlAlias = body.get("url_alias").asString,
328-
icon = if (!body.has("icon") || body.get("icon").isJsonNull) null else body.get("icon").asString.ifBlank { null },
329-
iconWide = if (!body.has("icon_wide") || body.get("icon_wide").isJsonNull) null else body.get("icon_wide").asString.ifBlank { null },
327+
icon = if (!body.has("icon") || body.get("icon").isJsonNull) null else body.get(
328+
"icon"
329+
).asString.ifBlank { null },
330+
iconWide = if (!body.has("icon_wide") || body.get("icon_wide").isJsonNull) null else body.get(
331+
"icon_wide"
332+
).asString.ifBlank { null },
330333
websiteUrl = body.get("website_url").asString,
331-
description = if (!body.has("description") || body.get("description").isJsonNull) null else body.get("description").asString.ifBlank { null },
334+
description = if (!body.has("description") || body.get("description").isJsonNull) null else body.get(
335+
"description"
336+
).asString.ifBlank { null },
332337
)
333338
}
334339
}
@@ -340,12 +345,20 @@ class Api(private val httpClient: OkHttpClient, private val url: HttpUrl) {
340345
type = it.get("type").asString,
341346
placeId = it.get("place_id").asLong,
342347
placeName = it.get("place_name").asString,
343-
osmUserId = if (!it.has("osm_user_id") || it.get("osm_user_id").isJsonNull) null else it.get("osm_user_id").asLong,
344-
osmUserName = if (!it.has("osm_user_name") || it.get("osm_user_name").isJsonNull) null else it.get("osm_user_name").asString.ifBlank { null },
345-
osmUserTip = if (!it.has("osm_user_tip") || it.get("osm_user_tip").isJsonNull) null else it.get("osm_user_tip").asString.ifBlank { null },
348+
osmUserId = if (!it.has("osm_user_id") || it.get("osm_user_id").isJsonNull) null else it.get(
349+
"osm_user_id"
350+
).asLong,
351+
osmUserName = if (!it.has("osm_user_name") || it.get("osm_user_name").isJsonNull) null else it.get(
352+
"osm_user_name"
353+
).asString.ifBlank { null },
354+
osmUserTip = if (!it.has("osm_user_tip") || it.get("osm_user_tip").isJsonNull) null else it.get(
355+
"osm_user_tip"
356+
).asString.ifBlank { null },
346357
image = if (!it.has("image") || it.get("image").isJsonNull) null else it.get("image").asString.ifBlank { null },
347358
date = it.get("date").asString,
348-
durationDays = if (!it.has("duration_days") || it.get("duration_days").isJsonNull) null else it.get("duration_days").asLong,
359+
durationDays = if (!it.has("duration_days") || it.get("duration_days").isJsonNull) null else it.get(
360+
"duration_days"
361+
).asLong,
349362
comment = if (!it.has("comment") || it.get("comment").isJsonNull) null else it.get("comment").asString.ifBlank { null },
350363
)
351364
}

app/src/main/kotlin/org/btcmap/map/AreasAdapter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import androidx.recyclerview.widget.DiffUtil
66
import androidx.recyclerview.widget.ListAdapter
77
import androidx.recyclerview.widget.RecyclerView
88
import coil3.load
9+
import okhttp3.HttpUrl
910
import org.btcmap.api.GetAreasItem
1011
import org.btcmap.databinding.AreaItemBinding
1112

1213
class AreasAdapter(
14+
private val apiUrl: HttpUrl,
1315
private val onItemClick: (GetAreasItem) -> Unit,
1416
) : ListAdapter<GetAreasItem, AreasAdapter.ItemViewHolder>(DiffCallback()) {
1517

@@ -23,20 +25,20 @@ class AreasAdapter(
2325
false,
2426
)
2527

26-
return ItemViewHolder(binding)
28+
return ItemViewHolder(apiUrl, binding)
2729
}
2830

2931
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
3032
holder.bind(getItem(position), onItemClick)
3133
}
3234

3335
class ItemViewHolder(
36+
private val apiUrl: HttpUrl,
3437
private val binding: AreaItemBinding,
3538
) : RecyclerView.ViewHolder(binding.root) {
36-
3739
fun bind(area: GetAreasItem, onItemClick: (GetAreasItem) -> Unit) {
3840
binding.apply {
39-
icon.load(area.icon)
41+
icon.load("$apiUrl/v4/areas/${area.id}/image?type=square&w=256&h=256")
4042
root.setOnClickListener { onItemClick(area) }
4143
}
4244
}

app/src/main/kotlin/org/btcmap/map/MapFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import org.btcmap.map.layer.createEventLayers
7676
import org.btcmap.map.layer.createExchangeLayers
7777
import org.btcmap.map.layer.createMerchantLayers
7878
import org.btcmap.search.SearchAdapterItem
79+
import org.btcmap.settings.apiUrl
7980
import org.btcmap.settings.badgeBackgroundColor
8081
import org.btcmap.settings.badgeTextColor
8182
import org.btcmap.settings.boostedMarkerBackgroundColor
@@ -374,7 +375,7 @@ class MapFragment : Fragment() {
374375
}
375376
}.launchIn(viewLifecycleOwner.lifecycleScope)
376377

377-
areasAdapter = AreasAdapter { area ->
378+
areasAdapter = AreasAdapter(apiUrl = prefs.apiUrl) { area ->
378379
parentFragmentManager.commit {
379380
setReorderingAllowed(true)
380381
replace<AreaFragment>(

0 commit comments

Comments
 (0)