Bundle shelter data in APK and add shared versioning

- Add scripts/fetch-shelters.sh: downloads Geonorge data, converts
  UTM33N→WGS84 via PWA script, copies to both Android assets and
  PWA public dirs
- Bundle pre-processed shelters.json (556 shelters) in APK assets
  so the app works immediately on first launch with no network
- ShelterRepository.seedFromAsset(): seeds Room DB from bundled
  JSON on first launch, marks as stale so network refresh is
  attempted in the background
- MainActivity.loadData(): seeds from asset before trying network,
  always attempts background refresh when data is stale
- Add version.properties (1.1.0, versionCode=2) as single source
  of truth for versioning
- build.gradle.kts reads version from properties file and exposes
  via BuildConfig
- Bump PWA version to 1.1.0 to match

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-03-08 18:33:38 +01:00
commit d0460cd686
7 changed files with 4584 additions and 9 deletions

View file

@ -1,9 +1,16 @@
import java.util.Properties
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp")
}
// Read version from shared version.properties
val versionProps = Properties().apply {
rootProject.file("version.properties").inputStream().use { load(it) }
}
android {
namespace = "no.naiv.tilfluktsrom"
compileSdk = 35
@ -12,8 +19,13 @@ android {
applicationId = "no.naiv.tilfluktsrom"
minSdk = 26
targetSdk = 35
versionCode = 1
versionName = "1.0.0"
versionCode = versionProps.getProperty("versionCode").toInt()
versionName = "${versionProps.getProperty("versionMajor")}." +
"${versionProps.getProperty("versionMinor")}." +
versionProps.getProperty("versionPatch")
// Make version available in BuildConfig
buildConfigField("String", "VERSION_DISPLAY", "\"$versionName\"")
}
buildTypes {
@ -29,6 +41,7 @@ android {
buildFeatures {
viewBinding = true
buildConfig = true
}
compileOptions {

File diff suppressed because it is too large Load diff

View file

@ -207,8 +207,15 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
private fun loadData() {
lifecycleScope.launch {
try {
val hasData = repository.hasCachedData()
var hasData = repository.hasCachedData()
// Seed from bundled asset if DB is empty (works offline)
if (!hasData) {
repository.seedFromAsset()
hasData = repository.hasCachedData()
}
// If seeding also failed (shouldn't happen), try network
if (!hasData) {
if (!isNetworkAvailable()) {
binding.statusText.text = getString(R.string.error_no_data_offline)
@ -244,13 +251,14 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
// Request location and start updates
requestLocationPermission()
// Check for stale data in background
if (hasData && repository.isDataStale() && isNetworkAvailable()) {
// Always try to refresh from network (seeded data has timestamp 0 = stale)
if (repository.isDataStale() && isNetworkAvailable()) {
launch {
val success = repository.refreshData()
if (success) {
Toast.makeText(this@MainActivity, R.string.update_success, Toast.LENGTH_SHORT).show()
} else {
} else if (!hasData) {
// Only warn if we had no data at all before
Toast.makeText(this@MainActivity, R.string.update_failed, Toast.LENGTH_SHORT).show()
}
}

View file

@ -9,19 +9,25 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import java.util.concurrent.TimeUnit
/**
* Repository managing shelter data: local Room cache + remote Geonorge download.
* Offline-first: always returns cached data when available, updates in background.
*
* Offline-first strategy:
* 1. On first launch, seed from bundled shelters.json asset (no network needed)
* 2. Try to download latest data from Geonorge in the background
* 3. Refresh automatically when data is older than 7 days
*/
class ShelterRepository(context: Context) {
class ShelterRepository(private val context: Context) {
companion object {
private const val TAG = "ShelterRepository"
private const val PREFS_NAME = "shelter_prefs"
private const val KEY_LAST_UPDATE = "last_update_ms"
private const val UPDATE_INTERVAL_MS = 7 * 24 * 60 * 60 * 1000L // 7 days
private const val BUNDLED_ASSET = "shelters.json"
// Geonorge GeoJSON download (ZIP containing all Norwegian shelters)
private const val SHELTER_DATA_URL =
@ -51,6 +57,34 @@ class ShelterRepository(context: Context) {
return System.currentTimeMillis() - lastUpdate > UPDATE_INTERVAL_MS
}
/**
* Seed the database from the bundled shelters.json asset.
* This is pre-processed at build time (UTM33N already converted to WGS84).
* Returns true if seeding succeeded.
*/
suspend fun seedFromAsset(): Boolean = withContext(Dispatchers.IO) {
try {
val json = context.assets.open(BUNDLED_ASSET).bufferedReader().use { it.readText() }
val shelters = parseBundledJson(json)
Log.d(TAG, "Seeding ${shelters.size} shelters from bundled asset")
db.withTransaction {
dao.deleteAll()
dao.insertAll(shelters)
}
// Mark as seeded but with timestamp 0 so it's considered stale
// and will be refreshed from network when possible
prefs.edit().putLong(KEY_LAST_UPDATE, 0).apply()
true
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e(TAG, "Failed to seed from bundled asset", e)
false
}
}
/**
* Download shelter data from Geonorge and cache it locally.
* Returns true on success, false on failure.
@ -99,4 +133,31 @@ class ShelterRepository(context: Context) {
false
}
}
/**
* Parse the pre-processed bundled JSON (already WGS84, no coordinate conversion needed).
*/
private fun parseBundledJson(json: String): List<Shelter> {
val array = JSONArray(json)
val shelters = mutableListOf<Shelter>()
for (i in 0 until array.length()) {
val obj = array.getJSONObject(i)
val lokalId: String? = obj.optString("lokalId", null)
if (lokalId.isNullOrBlank()) continue
shelters.add(
Shelter(
lokalId = lokalId,
romnr = obj.optInt("romnr", 0),
plasser = obj.optInt("plasser", 0),
adresse = obj.optString("adresse", ""),
latitude = obj.getDouble("latitude"),
longitude = obj.getDouble("longitude")
)
)
}
return shelters
}
}