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

File diff suppressed because it is too large Load diff

View file

@ -207,8 +207,15 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
private fun loadData() { private fun loadData() {
lifecycleScope.launch { lifecycleScope.launch {
try { 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 (!hasData) {
if (!isNetworkAvailable()) { if (!isNetworkAvailable()) {
binding.statusText.text = getString(R.string.error_no_data_offline) binding.statusText.text = getString(R.string.error_no_data_offline)
@ -244,13 +251,14 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
// Request location and start updates // Request location and start updates
requestLocationPermission() requestLocationPermission()
// Check for stale data in background // Always try to refresh from network (seeded data has timestamp 0 = stale)
if (hasData && repository.isDataStale() && isNetworkAvailable()) { if (repository.isDataStale() && isNetworkAvailable()) {
launch { launch {
val success = repository.refreshData() val success = repository.refreshData()
if (success) { if (success) {
Toast.makeText(this@MainActivity, R.string.update_success, Toast.LENGTH_SHORT).show() 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() 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 kotlinx.coroutines.withContext
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import org.json.JSONArray
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/** /**
* Repository managing shelter data: local Room cache + remote Geonorge download. * 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 { companion object {
private const val TAG = "ShelterRepository" private const val TAG = "ShelterRepository"
private const val PREFS_NAME = "shelter_prefs" private const val PREFS_NAME = "shelter_prefs"
private const val KEY_LAST_UPDATE = "last_update_ms" 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 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) // Geonorge GeoJSON download (ZIP containing all Norwegian shelters)
private const val SHELTER_DATA_URL = private const val SHELTER_DATA_URL =
@ -51,6 +57,34 @@ class ShelterRepository(context: Context) {
return System.currentTimeMillis() - lastUpdate > UPDATE_INTERVAL_MS 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. * Download shelter data from Geonorge and cache it locally.
* Returns true on success, false on failure. * Returns true on success, false on failure.
@ -99,4 +133,31 @@ class ShelterRepository(context: Context) {
false 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
}
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "tilfluktsrom-pwa", "name": "tilfluktsrom-pwa",
"version": "1.0.0", "version": "1.1.0",
"description": "Norwegian Emergency Shelter Finder - PWA", "description": "Norwegian Emergency Shelter Finder - PWA",
"type": "module", "type": "module",
"scripts": { "scripts": {

39
scripts/fetch-shelters.sh Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Downloads shelter data from Geonorge, converts UTM33N → WGS84 via the PWA
# script, and copies the result to both Android assets and PWA public dirs.
#
# Usage: ./scripts/fetch-shelters.sh
# Requires: bun (or node + tsx)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
PWA_SCRIPT="$PROJECT_ROOT/pwa/scripts/fetch-shelters.ts"
PWA_OUTPUT="$PROJECT_ROOT/pwa/public/data/shelters.json"
ANDROID_ASSETS="$PROJECT_ROOT/app/src/main/assets"
ANDROID_OUTPUT="$ANDROID_ASSETS/shelters.json"
# Ensure output dirs exist
mkdir -p "$(dirname "$PWA_OUTPUT")"
mkdir -p "$ANDROID_ASSETS"
echo "==> Fetching shelter data from Geonorge..."
# Run the PWA conversion script (download ZIP, convert UTM→WGS84, output JSON)
bun run "$PROJECT_ROOT/pwa/scripts/fetch-shelters.ts"
if [ ! -f "$PWA_OUTPUT" ]; then
echo "ERROR: PWA script did not produce $PWA_OUTPUT"
exit 1
fi
SHELTER_COUNT=$(python3 -c "import json; print(len(json.load(open('$PWA_OUTPUT'))))" 2>/dev/null || echo "?")
echo "==> Generated $SHELTER_COUNT shelters"
# Copy to Android assets
cp "$PWA_OUTPUT" "$ANDROID_OUTPUT"
echo "==> Copied to $ANDROID_OUTPUT"
echo "==> Done. Shelter data ready for both platforms."

4
version.properties Normal file
View file

@ -0,0 +1,4 @@
versionMajor=1
versionMinor=1
versionPatch=0
versionCode=2