From e68212991935d33a4baca77d88cd20a82fbcf6a6 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Wed, 25 Mar 2026 18:23:54 +0000 Subject: refactor: address simplify review findings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TrackRepository.addPoint() now returns Boolean (true if point was added). MainViewModel.addGpsPoint() only updates _trackPoints StateFlow when a point is actually appended — eliminates ~3,600 no-op flow emissions per hour when not recording. MainActivity: loadedStyle promoted from nullable field to MutableStateFlow; trackPoints observer uses filterNotNull + combine so no track points are silently dropped if the style loads after the first GPS fix. Smoke tests: replaced 11× ActivityScenario.launch().use{} with a single @get:Rule ActivityScenarioRule — same isolation, less boilerplate. CI: removed redundant app-debug artifact upload (app-debug.apk is already included inside the test-apks artifact). Removed stale/placeholder comments from MainActivity. Co-Authored-By: Claude Sonnet 4.6 --- .../app/src/main/kotlin/org/terst/nav/MainActivity.kt | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt') diff --git a/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt b/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt index f887a43..f9d4dbd 100644 --- a/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt +++ b/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt @@ -22,7 +22,10 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.floatingactionbutton.FloatingActionButton import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -45,7 +48,7 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { private var instrumentHandler: InstrumentHandler? = null private var mapHandler: MapHandler? = null private var anchorWatchHandler: AnchorWatchHandler? = null - private var loadedStyle: Style? = null + private val loadedStyleFlow = MutableStateFlow(null) private lateinit var bottomSheetBehavior: BottomSheetBehavior private lateinit var fragmentContainer: FrameLayout @@ -147,17 +150,13 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { private fun hideOverlays() { fragmentContainer.visibility = View.GONE - // Clear backstack if needed } override fun onActivateMob() { lifecycleScope.launch { LocationService.locationFlow.firstOrNull()?.let { gpsData -> val mediaPlayer = MediaPlayer.create(this@MainActivity, R.raw.mob_alarm) - // In a real redesign, we'd show a specialized MOB fragment - // For now, keep existing handler logic but maybe toggle visibility mobHandler?.activateMob(gpsData.latitude, gpsData.longitude, mediaPlayer) - // Ensure MOB UI is visible - we might need to add it back to activity_main if removed } } } @@ -167,7 +166,6 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { } private fun setupHandlers() { - // ... (Keep existing handler initialization, just update view IDs as needed) instrumentHandler = InstrumentHandler( valueAws = findViewById(R.id.value_aws), valueTws = findViewById(R.id.value_tws), @@ -243,7 +241,7 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { .withLayer(RasterLayer("openseamap-layer", "openseamap-source")) maplibreMap.setStyle(style) { style -> - loadedStyle = style + loadedStyleFlow.value = style val anchorBitmap = rasterizeDrawable(R.drawable.ic_anchor) val arrowBitmap = rasterizeDrawable(R.drawable.ic_tidal_arrow) mapHandler?.setupLayers(style, anchorBitmap, arrowBitmap) @@ -265,10 +263,9 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { } } lifecycleScope.launch { - viewModel.trackPoints.collect { points -> - val style = loadedStyle ?: return@collect - mapHandler?.updateTrackLayer(style, points) - } + loadedStyleFlow.filterNotNull() + .combine(viewModel.trackPoints) { style, points -> style to points } + .collect { (style, points) -> mapHandler?.updateTrackLayer(style, points) } } } -- cgit v1.2.3