refactor(qrz): move the grid lookup out of the composable

LogTab read the QRZ cookie straight out of SharedPreferences through LocalContext,
inside composition, on every submission - disk access in a composable, around the
repository layer, with the client referenced by fully-qualified name inline. And it
did `if (grid != null)`, so a lookup that failed for any reason left the QSO without
a grid and told the operator nothing.

IQrzGridLookup lives in core:domain, QrzGridLookup in core:data owns the cookie read,
and the view model exposes lookupGrid. The composable now takes a callback and handles
each outcome: a locator is attached, no locator on file passes quietly because nothing
is wrong, an expired cookie says to paste a fresh one, and an unreachable QRZ says so.
That is what the four-outcome QrzGrid type from ce68f487 was for - until now nothing
consumed it and the old nullable client was still the one being called.

Note for anyone extending RadarScreen: the local holding the view model cannot be
referenced as `viewModel` inside a lambda, because that name also resolves to the
composable factory function. Hence the explicitly typed local.

The old QrzGridClient is now unused here but left in place; removing it belongs with
the settings screen, which still calls it to validate a pasted cookie.
This commit is contained in:
mckero committed 2026-08-25 15:18:40 +00:00
1 parent 0a67f74369
commit eb66a77cae
6 files changed
+129 -18

No files matched your search

@@ -71,6 +71,7 @@ import androidx.compose.ui.unit.sp
import com.rtbishop.look4sat.core.domain.model.SatRadio
import com.rtbishop.look4sat.core.domain.predict.OrbitalPos
import com.rtbishop.look4sat.core.domain.utility.DopplerFrequencyCalculator
import com.rtbishop.look4sat.core.domain.qrz.QrzGrid
import com.rtbishop.look4sat.core.domain.wavelog.CallsignEntry
import com.rtbishop.look4sat.core.domain.wavelog.WavelogQso
import com.rtbishop.look4sat.core.domain.wavelog.WavelogQueue
@@ -99,6 +100,7 @@ fun LogTab(
showToast: (String) -> Unit,
txBaseFrequencyHz: Long? = null,
aosTimeMs: Long = 0L,
onLookupGrid: (String, (QrzGrid) -> Unit) -> Unit,
modifier: Modifier = Modifier
) {
val context = androidx.compose.ui.platform.LocalContext.current
@@ -174,6 +176,7 @@ fun LogTab(
showToast = showToast,
txBaseFrequencyHz = txBaseFrequencyHz,
aosTimeMs = aosTimeMs,
onLookupGrid = onLookupGrid,
onSaved = { refreshTick++ }
)
}
@@ -261,6 +264,7 @@ private fun ExpandedLogInput(
showToast: (String) -> Unit,
txBaseFrequencyHz: Long? = null,
aosTimeMs: Long = 0L,
onLookupGrid: (String, (QrzGrid) -> Unit) -> Unit,
onSaved: () -> Unit
) {
val context = androidx.compose.ui.platform.LocalContext.current
@@ -280,6 +284,8 @@ private fun ExpandedLogInput(
val illegalMsg = stringResource(id = R.string.log_call_illegal)
val notACallMsg = stringResource(id = R.string.log_call_not_a_call)
val tooLongMsg = stringResource(id = R.string.log_call_too_long)
val gridSignedOutMsg = stringResource(id = R.string.log_grid_signed_out)
val gridUnreachableMsg = stringResource(id = R.string.log_grid_unreachable)
fun rejectionMessage(reason: CallsignEntry.Reason): String = when (reason) {
CallsignEntry.Reason.EMPTY, CallsignEntry.Reason.TOO_SHORT -> tooShortMsg
@@ -326,18 +332,19 @@ private fun ExpandedLogInput(
null -> savedMsg
}
)
// QRZ counterpart grid async backfill (4.5.5): only queried when Cookie is set; silent on failure
scope.launch {
val prefs = context.getSharedPreferences("qrz_cookie", android.content.Context.MODE_PRIVATE)
val rawCookie = prefs.getString("cookie", "") ?: ""
if (rawCookie.isNotBlank()) {
val grid = com.rtbishop.look4sat.core.domain.qrz.QrzGridClient.lookupGrid(
call, com.rtbishop.look4sat.core.domain.qrz.QrzGridClient.parseCookies(rawCookie)
)
if (grid != null) {
queue.updateGridsquare(qsoId, grid)
onSaved() // 触发 refreshTick++ 刷新列表
// Grid backfill. The view model owns the cookie and the request; this used to read
// SharedPreferences through LocalContext right here, inside composition. Failures now say
// something: an expired cookie was indistinguishable from a station with no grid filed.
onLookupGrid(call) { outcome ->
when (outcome) {
is QrzGrid.Found -> {
queue.updateGridsquare(qsoId, outcome.locator)
onSaved()
}
// Nothing to fetch and nothing wrong: the station has no locator on file.
QrzGrid.NotOnFile -> Unit
QrzGrid.SignedOut -> showToast(gridSignedOutMsg)
is QrzGrid.Unreachable -> showToast(gridUnreachableMsg)
}
}
}
@@ -58,6 +58,7 @@ import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.rtbishop.look4sat.core.domain.qrz.QrzGrid
import com.rtbishop.look4sat.core.domain.predict.OrbitalPos
import com.rtbishop.look4sat.core.domain.repository.IContainerProvider
import com.rtbishop.look4sat.core.domain.repository.MutualPassData
@@ -92,6 +93,9 @@ fun RadarDestination(navigateUp: () -> Unit) {
val container = (context.applicationContext as IContainerProvider).getMainContainer()
val viewModel: RadarViewModel = viewModel(factory = RadarViewModel.factory(container))
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
// Captured as a value because `viewModel` also names the composable factory function, so
// referring to it inside a lambda resolves to that instead of this local.
val lookupGrid: (String, (QrzGrid) -> Unit) -> Unit = viewModel::lookupGrid
val mutualData by container.mutualPassData.collectAsStateWithLifecycle()
val navigateUpAndClearMutual = {
if (container.mutualPassData.value.endTime > 0L) {
@@ -149,7 +153,8 @@ fun RadarDestination(navigateUp: () -> Unit) {
wavelogConfigured = container.settingsRepo.otherSettings.value.let {
it.wavelogUrl.isNotBlank() && it.wavelogApiKey.isNotBlank() && it.wavelogStationId.isNotBlank()
},
showToast = { msg -> container.provideShowToast()(msg) }
showToast = { msg -> container.provideShowToast()(msg) },
onLookupGrid = lookupGrid
)
}
@@ -162,7 +167,8 @@ private fun RadarScreen(
requestMicPermission: () -> Unit,
wavelogQueue: WavelogQueue,
wavelogConfigured: Boolean,
showToast: (String) -> Unit
showToast: (String) -> Unit,
onLookupGrid: (String, (QrzGrid) -> Unit) -> Unit
) {
val upcomingPass = uiState.currentPass ?: getDefaultPass()
val addToCalendar: () -> Unit = {
@@ -216,11 +222,11 @@ private fun RadarScreen(
}
if (isVertical) {
RadarCard(uiState, trackB, trackBPosition, Modifier.weight(1f))
PagerCard(uiState, onAction, requestMicPermission, wavelogQueue, wavelogConfigured, showToast, Modifier.weight(1f))
PagerCard(uiState, onAction, requestMicPermission, wavelogQueue, wavelogConfigured, showToast, onLookupGrid, Modifier.weight(1f))
} else {
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
RadarCard(uiState, trackB, trackBPosition, Modifier.weight(1f))
PagerCard(uiState, onAction, requestMicPermission, wavelogQueue, wavelogConfigured, showToast, Modifier.weight(1f))
PagerCard(uiState, onAction, requestMicPermission, wavelogQueue, wavelogConfigured, showToast, onLookupGrid, Modifier.weight(1f))
}
}
}
@@ -234,6 +240,7 @@ private fun PagerCard(
wavelogQueue: WavelogQueue,
wavelogConfigured: Boolean,
showToast: (String) -> Unit,
onLookupGrid: (String, (QrzGrid) -> Unit) -> Unit,
modifier: Modifier = Modifier
) {
val hasCalculatorPage = remember(uiState.transceivers.transmitters) {
@@ -302,7 +309,8 @@ private fun PagerCard(
wavelogConfigured = wavelogConfigured,
showToast = showToast,
txBaseFrequencyHz = uiState.radioControl.txBaseFrequencyHz,
aosTimeMs = uiState.currentPass?.aosTime ?: 0L
aosTimeMs = uiState.currentPass?.aosTime ?: 0L,
onLookupGrid = onLookupGrid
)
}
}
@@ -26,6 +26,8 @@ import com.rtbishop.look4sat.core.domain.predict.CelestialComputer
import com.rtbishop.look4sat.core.domain.predict.OrbitalObject
import com.rtbishop.look4sat.core.domain.predict.OrbitalPass
import com.rtbishop.look4sat.core.domain.predict.OrbitalPos
import com.rtbishop.look4sat.core.domain.qrz.IQrzGridLookup
import com.rtbishop.look4sat.core.domain.qrz.QrzGrid
import com.rtbishop.look4sat.core.domain.repository.IMainContainer
import com.rtbishop.look4sat.core.domain.repository.IRadioTrackingService
import com.rtbishop.look4sat.core.domain.repository.IReporter
@@ -64,7 +66,8 @@ class RadarViewModel(
private val audioCapture: IAudioCapture,
private val cwDecoderFactory: () -> ICwDecoder,
private val saveImage: ISaveImage,
private val showToast: IShowToast
private val showToast: IShowToast,
private val qrzGridLookup: IQrzGridLookup
) : ViewModel() {
private var stationPos = settingsRepo.stationPosition.value
@@ -544,6 +547,22 @@ class RadarViewModel(
_uiState.update { it.copy(cw = it.cw.copy(status = CwStatus.Idle)) }
}
/**
* Look up a station's grid and hand the outcome back.
*
* Lives here rather than in the composable, which read the QRZ cookie straight out of
* SharedPreferences through LocalContext - disk access inside composition, around the
* repository layer. Every outcome is returned, because a lookup that failed used to be
* indistinguishable from a station that simply has no grid on file.
*/
fun lookupGrid(callsign: String, onResult: (QrzGrid) -> Unit) {
viewModelScope.launch {
val outcome = runCatching { qrzGridLookup.lookup(callsign) }
.getOrElse { QrzGrid.Unreachable(1) }
onResult(outcome)
}
}
companion object {
// SSTV Decoder Tuning Parameters
// ==============================
@@ -599,7 +618,8 @@ class RadarViewModel(
audioCapture = container.provideAudioCapture(),
cwDecoderFactory = { container.provideCwDecoder() },
saveImage = container.provideSaveImage(),
showToast = container.provideShowToast()
showToast = container.provideShowToast(),
qrzGridLookup = container.provideQrzGridLookup()
)
}
}