Skip to content

Native Android client for Spotify, built with Jetpack Compose

SpotiFLAC verification: hold the returned grant until the screen takes it

liliapk6e505a696d19
diff --git a/base/app/src/main/java/app/myspoti/MainActivity.kt b/base/app/src/main/java/app/myspoti/MainActivity.kt
index ddd6bcc9..dbb84829 100644
--- a/base/app/src/main/java/app/myspoti/MainActivity.kt
+++ b/base/app/src/main/java/app/myspoti/MainActivity.kt
@@ -174,7 +174,7 @@ class MainActivity : AppCompatActivity() {
intent ?: return
// The SpotiFLAC verification's return (spotiflac://session-grant?grant=…&state=…).
if (intent.data?.scheme == "spotiflac" && intent.data?.host == "session-grant") {
- com.music.spotui.lossless.ZarzSession.grants.tryEmit(intent.data.toString())
+ com.music.spotui.lossless.ZarzSession.grants.value = intent.data.toString()
return
}
when (intent.action) {
diff --git a/base/app/src/main/java/com/music/spotui/lossless/ZarzSession.kt b/base/app/src/main/java/com/music/spotui/lossless/ZarzSession.kt
index 3d6b8bb8..f04195d9 100644
--- a/base/app/src/main/java/com/music/spotui/lossless/ZarzSession.kt
+++ b/base/app/src/main/java/com/music/spotui/lossless/ZarzSession.kt
@@ -38,8 +38,13 @@ internal object ZarzSession {
private const val PREFIX = "X-Zarz-"
private const val WINDOW_SECONDS = 300L
- /** The grant redirects the verification page sends back to the app (spotiflac://session-grant…). */
- val grants = kotlinx.coroutines.flow.MutableSharedFlow<String>(extraBufferCapacity = 4)
+ /**
+ * The grant redirects the verification page sends back to the app
+ * (spotiflac://session-grant…). A StateFlow, not a SharedFlow: the value is
+ * held until the screen takes it, so a rotation (or a screen that starts
+ * collecting a moment later) cannot lose the return. The screen clears it.
+ */
+ val grants = kotlinx.coroutines.flow.MutableStateFlow<String?>(null)
/** A challenge page to open (the user solves it), or null when a session came back directly. */
class Bootstrap(val challengeUrl: String?, val state: String)
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/SpotiflacScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/SpotiflacScreen.kt
index df750d8e..911bc3fb 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/SpotiflacScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/SpotiflacScreen.kt
@@ -73,8 +73,9 @@ fun SpotiflacScreen(onClose: () -> Unit) {
// Reused so a retry keeps the same install; the service binds the session to it.
val installId = remember { com.music.spotui.lossless.ZarzSession.installId(context) }
- // The state the challenge page must send back with its grant.
- var state by remember { mutableStateOf("") }
+ // The state the challenge page must send back with its grant. Saveable: a
+ // rotation while the browser is open must not lose it (the grant is then rejected).
+ var state by androidx.compose.runtime.saveable.rememberSaveable { mutableStateOf("") }
fun finish(grant: String) = scope.launch {
message = res(R.string.spotiflac_session_creating)
@@ -133,6 +134,8 @@ fun SpotiflacScreen(onClose: () -> Unit) {
// The grant coming back from the browser tab.
LaunchedEffect(Unit) {
com.music.spotui.lossless.ZarzSession.grants.collect { link ->
+ if (link == null) return@collect
+ com.music.spotui.lossless.ZarzSession.grants.value = null // take it, once
val back = com.music.spotui.lossless.ZarzSession.parseGrant(link)
if (back == null || state.isBlank() || back.second != state) {
message = res(R.string.spotiflac_verify_rejected)