| 1 |
package io.oxygenhub.android.ui.backdrop |
| 2 |
|
| 3 |
import io.oxygenhub.android.api.HubApi |
| 4 |
import io.oxygenhub.android.api.UserSky |
| 5 |
import kotlinx.coroutines.CancellationException |
| 6 |
import uniffi.rust_core.SkyEvent |
| 7 |
import uniffi.rust_core.Backdrop as Scene |
| 8 |
|
| 9 |
/** Your sky's events for the scene: each aged by the hub's clock, in days; malformed ones left out. */ |
| 10 |
fun skyEvents(sky: UserSky): List<SkyEvent> = sky.events.mapNotNull { e -> |
| 11 |
if (e.size < 4 || e[0] !in sky.repos.indices || e[1] !in 0L..4L) return@mapNotNull null |
| 12 |
SkyEvent(e[0].toUInt(), e[1].toUInt(), ((sky.now - e[2]).coerceAtLeast(0) / 86_400_000.0).toFloat(), e[3].coerceIn(1, 10_000).toUInt()) |
| 13 |
} |
| 14 |
|
| 15 |
/** Your sky, as on the hub's dashboard; the quiet field when there is nothing of yours to draw. */ |
| 16 |
fun skyScene(sky: UserSky?, still: Boolean): Scene = |
| 17 |
if (sky == null || sky.repos.isEmpty()) Scene.adrift(still) else Scene.sky(sky.repos, skyEvents(sky), 0f, 0f, still) |
| 18 |
|
| 19 |
/** A repository's sky, or null when the hub cannot give it (one before #23 answers 404). */ |
| 20 |
suspend fun repoSkyOrNull(api: HubApi, owner: String, name: String): UserSky? = |
| 21 |
try { |
| 22 |
api.repoSky(owner, name) |
| 23 |
} catch (e: CancellationException) { |
| 24 |
throw e |
| 25 |
} catch (_: Exception) { |
| 26 |
null |
| 27 |
} |