Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand Down Expand Up @@ -46,6 +48,8 @@ import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.layout.positionInParent
import androidx.compose.ui.input.InputMode
import androidx.compose.ui.platform.LocalInputModeManager
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -429,9 +433,14 @@ fun TvLoginScreen(
}
val activate = device.code?.activateText ?: state.serverActivateText.orEmpty()
TvMarqueeBody(
"Scan the code with your phone's camera, or go to " +
(activate.ifBlank { "your server's /activate page" }) +
" and enter it. Approve on your phone and this TV signs in by itself.",
// A decline withdraws the code, so there is nothing left to scan.
if (device.status == TvSignInStatus.Denied) {
stringResource(R.string.tv_signin_declined_lead)
} else {
"Scan the code with your phone's camera, or go to " +
(activate.ifBlank { "your server's /activate page" }) +
" and enter it. Approve on your phone and this TV signs in by itself."
},
modifier = Modifier.padding(top = if (network == null) 13.dp else 6.dp),
)
Row(
Expand Down Expand Up @@ -518,9 +527,11 @@ private class NetworkSignInAction(

/**
* The state's action ("Try again" / "Show a new code"), "Sign in with a
* password" and "Change server" on one row, as on Apple TV. Controls stay
* enabled while work runs so focus doesn't drop; the actions ignore repeats.
* password" and "Change server" on one row, as on Apple TV, wrapping when all
* three don't fit so no label is cut off. Controls stay enabled while work
* runs so focus doesn't drop; the actions ignore repeats.
*/
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun CodeActions(
status: TvSignInStatus,
Expand All @@ -540,7 +551,14 @@ private fun CodeActions(
val upTo = network?.focusRequester
// An update requirement hides the password button: no password can help.
val offersPassword = showPasswordAction && status != TvSignInStatus.UpdateRequired
Row(modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(11.dp)) {
// Wrapped onto its own line, Change server leaves Up and Left to the
// rows above it: the overrides below are for a one-row layout.
var changeServerWrapped by remember { mutableStateOf(false) }
FlowRow(
Comment thread
Quick104 marked this conversation as resolved.
Resolved
modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(11.dp),
verticalArrangement = Arrangement.spacedBy(11.dp),
) {
status.action()?.let { label ->
TvMarqueeButton(
text = stringResource(label),
Expand Down Expand Up @@ -573,8 +591,10 @@ private fun CodeActions(
kind = TvMarqueeButtonKind.Plain,
focusRequester = changeServerFocus,
modifier = Modifier
.onPlaced { changeServerWrapped = it.positionInParent().y > 0f }
Comment thread
Quick104 marked this conversation as resolved.
Resolved
.then(changeServerTracking)
.focusProperties {
if (changeServerWrapped) return@focusProperties
if (offersPassword) left = usePasswordFocus
upTo?.let { up = it }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,13 @@ class TvLoginViewModel(
private var machineObserver: Job? = null
private var deviceLoginJob: Job? = null
private var credentialLoginJob: Job? = null
private var newCodeJob: Job? = null
private var signInOptionsJob: Job? = null

/** Saves an approval's tokens and routes on; [onStop] never cancels it. */
private var approvalJob: Job? = null
private var authCompleted = false
private var visible = true

/** [onStop] ran since the last [onStart]: the next start re-reads the sign-in options. */
private var lastDeviceCode: String? = null

/** A device approval that arrived while a password (or network) save held the sign-in. */
private var heldApproval: Pair<DeviceLoginPollResponse, AccountSessionExpectation>? = null

Expand Down Expand Up @@ -612,13 +608,11 @@ class TvLoginViewModel(
_deviceSignIn.value = when (state) {
DeviceSignInState.GettingCode -> TvDeviceSignInUi(status = TvSignInStatus.GettingCode)
is DeviceSignInState.ShowingCode -> {
val renewedNow = state.renewed && lastDeviceCode != null && lastDeviceCode != state.session.deviceCode
lastDeviceCode = state.session.deviceCode
if (renewedNow) announceNewCode()
// A code that replaced an expired one says so for as long as
// it shows, as on Apple TV; asking for a new code starts over.
val status = when {
state.opened -> TvSignInStatus.Opened
renewedNow || (previous.status == TvSignInStatus.NewCode && newCodeJob?.isActive == true) ->
TvSignInStatus.NewCode
state.renewed -> TvSignInStatus.NewCode
else -> TvSignInStatus.Waiting
}
TvDeviceSignInUi(code = state.session.display(), status = status)
Expand All @@ -636,17 +630,6 @@ class TvLoginViewModel(
}
}

/** "New code" is a brief, polite announcement, then the status reads "Waiting" again. */
private fun announceNewCode() {
newCodeJob?.cancel()
newCodeJob = viewModelScope.launch {
delay(NEW_CODE_ANNOUNCEMENT_MS)
_deviceSignIn.update { ui ->
if (ui.status == TvSignInStatus.NewCode) ui.copy(status = TvSignInStatus.Waiting) else ui
}
}
}

/**
* Lift the approved tokens into [TokenManager] so everything downstream
* (start destination, authenticated calls) sees the same world as a
Expand Down Expand Up @@ -901,7 +884,6 @@ class TvLoginViewModel(
private companion object {
const val EXPECTATION_ATTEMPTS = 3
const val EXPECTATION_RETRY_MS = 250L
const val NEW_CODE_ANNOUNCEMENT_MS = 5_000L
const val SIGNED_IN_DWELL_MS = 1_000L

/** How long a nearby phone waits for a code that is still being fetched. */
Expand Down
9 changes: 6 additions & 3 deletions androidTvApp/src/androidMain/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<string name="tv_signin_status_signed_in_as">Signed in as %1$s</string>
<string name="tv_signin_status_signed_in">Signed in</string>
<string name="tv_signin_status_couldnt_finish">Couldn\'t finish signing in on this TV.</string>
<string name="tv_signin_status_denied">Sign-in was declined on your phone.</string>
<string name="tv_signin_status_denied">Sign-in was declined.</string>
<string name="tv_signin_status_paused">Sign-in paused.</string>
<string name="tv_signin_status_unreachable">Can\'t reach %1$s. Check this TV\'s connection.</string>
<string name="tv_signin_status_too_many">Too many sign-in attempts. Trying again shortly.</string>
Expand All @@ -70,6 +70,9 @@
<string name="tv_signin_action_change_server">Change server</string>
<string name="tv_signin_action_try_again">Try again</string>
<string name="tv_signin_action_new_code">Show a new code</string>
<!-- Replaces the scan-and-approve text once a sign-in is declined: the
code is gone, and the decline may have come from a phone or a browser. -->
<string name="tv_signin_declined_lead">Show a new code to try again.</string>

<!-- Pair a device: this TV approves another device\'s sign-in -->
<string name="tv_pair_error_enter_code">Enter the code shown on the other device.</string>
Expand All @@ -96,15 +99,15 @@
<string name="tv_signin_form_hide_password">Hide password</string>
<string name="tv_signin_error_username_required">Username is required</string>
<string name="tv_signin_error_password_required">Password is required</string>
<string name="tv_signin_error_invalid_credentials">Invalid username or password</string>
<string name="tv_signin_error_invalid_credentials">Incorrect username or password.</string>
<string name="tv_signin_error_account_disabled">Account is disabled</string>
<string name="tv_signin_error_network">Network error. Check your connection.</string>
<string name="tv_signin_error_local_login_disabled">Password sign-in is turned off on this server. Use your phone instead.</string>
<string name="tv_signin_error_not_permitted">Your account isn\'t allowed to use this server.</string>
<string name="tv_signin_error_password_expired">Your password has expired. Change it, then sign in again.</string>
<string name="tv_signin_error_provider_unavailable">The sign-in service can\'t be reached right now. Try again later.</string>
<string name="tv_signin_error_login_failed">Login failed</string>
<string name="tv_signin_error_invalid_credentials_provider">Invalid username or password. If you sign in with %1$s, use your phone instead.</string>
<string name="tv_signin_error_invalid_credentials_provider">Incorrect username or password. If you sign in with %1$s, use your phone instead.</string>
<string name="tv_signin_form_provider_hint">If you sign in with %1$s, use your phone instead.</string>
<string name="tv_signin_error_account_required">You don\'t have an account on this server yet. Ask an admin to add you.</string>
<string name="tv_signin_error_email_in_use">An account with this email already exists. Ask an admin to connect it to %1$s.</string>
Expand Down
Loading