AutofillNavRepository.kt

1.5 KB · raw

1package com.peachpasswords.autofill23import kotlinx.coroutines.flow.MutableStateFlow4import kotlinx.coroutines.flow.StateFlow5import kotlinx.coroutines.flow.asStateFlow6import javax.inject.Inject7import javax.inject.Singleton89/**10 * Carries a one-shot "open the new-entry editor" request from the autofill picker11 * to the nav host. Mirrors [com.peachpasswords.updates.UpdateRepository]'s shape12 * so the host reads a [StateFlow] and calls a `consume` after navigating.13 *14 * Held at app scope so MainActivity's [android.app.Activity.onNewIntent] can set15 * the request before the NavHost subscribes (cold launches hit the same flow via16 * the picker→MainActivity launch path).17 */18@Singleton19class AutofillNavRepository @Inject constructor() {2021    private val _newEntryRequest = MutableStateFlow<NewEntryRequest?>(null)22    val newEntryRequest: StateFlow<NewEntryRequest?> = _newEntryRequest.asStateFlow()2324    fun requestNewEntry(url: String?, username: String?, email: String?) {25        _newEntryRequest.value = NewEntryRequest(26            url = url?.takeIf { it.isNotBlank() },27            username = username?.takeIf { it.isNotBlank() },28            email = email?.takeIf { it.isNotBlank() },29        )30    }3132    fun consumeNewEntryRequest() { _newEntryRequest.value = null }33}3435/**36 * Prefill payload for the new-entry editor. Every field is optional — the37 * minimum contract is [url], mirroring the inline picker's create form.38 */39data class NewEntryRequest(40    val url: String? = null,41    val username: String? = null,42    val email: String? = null,43)