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
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {

defaultConfig {
applicationId = "dev.typetype.android"
versionCode = 11123
versionName = "1.14.0-beta.3"
versionCode = 11124
versionName = "1.14.0-beta.4"
}

signingConfigs {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/dev/typetype/android/AppNavHost.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.typetype.android

import dev.typetype.android.core.ui.components.LocalPlayOnTv
import dev.typetype.android.core.ui.navigation.LiveRoute
import dev.typetype.android.core.ui.navigation.TwitchChannelRoute
import dev.typetype.android.feature.live.TwitchChannelRouteScreen
Expand Down Expand Up @@ -93,7 +92,6 @@ fun AppNavHost(
val appPreferences by mainViewModel.preferences.collectAsStateWithLifecycle()
val navigationTransitionMillis = appPreferences.appearanceMotion.navigationTransitionMillis()
val notificationBadge by rememberNotificationBadge()
val playOnTv = rememberPlayOnTv(navController, notificationBadge.isAvailable, playerHostController)
val deArrowViewModel = hiltViewModel<DeArrowBrandingViewModel>()
val playerViewModel = hiltViewModel<PlayerViewModel>()
val deArrowSettings by deArrowViewModel.settings.collectAsStateWithLifecycle()
Expand All @@ -117,7 +115,6 @@ fun AppNavHost(
LocalPlaybackPrewarm provides playbackPrewarm,
LocalSettledCardPrefetch provides settledCardPrefetch,
LocalPlaybackRecovery provides (playbackRecovery ?: LocalPlaybackRecovery.current),
LocalPlayOnTv provides playOnTv,
) {
AppShell(
navController = navController,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/dev/typetype/android/AppShell.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import dev.typetype.android.core.ui.components.LocalAppSnackbarHost
import dev.typetype.android.core.ui.components.LocalPlayOnTv
import dev.typetype.android.core.ui.navigation.ChannelRoute
import dev.typetype.android.core.ui.navigation.NotificationsRoute
import dev.typetype.android.core.ui.navigation.PlaylistRoute
Expand Down Expand Up @@ -135,6 +136,7 @@ fun AppShell(
CompositionLocalProvider(
LocalMediaController provides mediaController,
LocalAppSnackbarHost provides snackbarHostState,
LocalPlayOnTv provides rememberPlayOnTv(navController, notificationsAvailable, playerHostController, mediaController),
) {
BoxWithConstraints(modifier = modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)) {
val tabletLayout =
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/dev/typetype/android/RemoteNavigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.typetype.android

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.media3.common.Player
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
Expand All @@ -16,17 +17,20 @@ internal fun NavGraphBuilder.remoteDestination(navController: NavHostController)
}
}

// Sending a video to the TV stops it on the phone, so the two do not play over each other.
@Composable
internal fun rememberPlayOnTv(
navController: NavHostController,
available: Boolean,
player: PlayerHostController,
): ((String) -> Unit)? = remember(navController, available, player) {
host: PlayerHostController,
playback: Player?,
): ((String) -> Unit)? = remember(navController, available, host, playback) {
if (!available) {
null
} else {
{ url ->
if (player.state.value.target == PlayerHostTarget.Expanded) player.minimize()
playback?.pause()
if (host.state.value.target == PlayerHostTarget.Expanded) host.minimize()
navController.navigate(RemoteRoute(url))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ enum class RemoteTvScreen(val wire: String) {
}
}

enum class RemoteKey(val wire: String) { Up("up"), Down("down"), Left("left"), Right("right"), Ok("ok"), Back("back"), Home("home") }
enum class RemoteKey(val wire: String) {
Up("up"),
Down("down"),
Left("left"),
Right("right"),
Ok("ok"),
Back("back"),
Home("home"),
VolumeUp("volume_up"),
VolumeDown("volume_down"),
Mute("mute"),
}

sealed interface RemoteCommand {
data object Play : RemoteCommand
Expand Down
3 changes: 3 additions & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,9 @@
<string name="remote_key_ok">Select</string>
<string name="remote_key_back">Back</string>
<string name="remote_key_home">Home</string>
<string name="remote_volume_up">Volume up</string>
<string name="remote_volume_down">Volume down</string>
<string name="remote_mute">Mute</string>
<string name="remote_touchpad">Swipe to move, tap to select</string>
<string name="remote_play">Play</string>
<string name="remote_pause">Pause</string>
Expand Down
1 change: 1 addition & 0 deletions feature/remote/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ android {
dependencies {
implementation(project(":core:data"))
implementation(libs.androidx.navigation.compose)
testImplementation(libs.kotlinx.coroutines.test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.typetype.android.R
import dev.typetype.android.domain.remote.RemoteCommand
import dev.typetype.android.domain.remote.RemoteTv
import dev.typetype.android.domain.remote.RemoteTvScreen

Expand Down Expand Up @@ -124,7 +125,11 @@ private fun TvList(state: RemoteUiState, onAction: (RemoteAction) -> Unit, onRen
@Composable
private fun Controller(state: RemoteUiState, tv: RemoteTv, onAction: (RemoteAction) -> Unit, modifier: Modifier) {
Column(
modifier = modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 20.dp, vertical = 12.dp),
modifier = modifier
.fillMaxSize()
.volumeKeys { onAction(RemoteAction.Send(RemoteCommand.Press(it))) }
.verticalScroll(rememberScrollState())
.padding(horizontal = 20.dp, vertical = 12.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Expand All @@ -137,6 +142,7 @@ private fun Controller(state: RemoteUiState, tv: RemoteTv, onAction: (RemoteActi
else -> {
if (state.sendFailed) Hint(stringResource(R.string.remote_send_failed))
RemotePanel(tv, onCommand = { onAction(RemoteAction.Send(it)) }, onSearch = { onAction(RemoteAction.Search(it)) })
if (tv.screen != RemoteTvScreen.Screensaver) VolumeRow(onKey = { onAction(RemoteAction.Send(RemoteCommand.Press(it))) })
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.typetype.android.feature.remote
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.toRoute
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.typetype.android.core.ui.navigation.RemoteRoute
import dev.typetype.android.domain.remote.RemoteCommand
Expand Down Expand Up @@ -44,7 +43,7 @@ class RemoteViewModel @Inject constructor(
private val repository: RemoteControlRepository,
savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val _state = MutableStateFlow(RemoteUiState(pendingVideoUrl = savedStateHandle.toRoute<RemoteRoute>().videoUrl))
private val _state = MutableStateFlow(RemoteUiState(pendingVideoUrl = savedStateHandle.get<String>(RemoteRoute::videoUrl.name)))
val state = _state.asStateFlow()
private var followJob: Job? = null
private var searchJob: Job? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.typetype.android.feature.remote

import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.VolumeDown
import androidx.compose.material.icons.automirrored.filled.VolumeOff
import androidx.compose.material.icons.automirrored.filled.VolumeUp
import androidx.compose.material3.FilledTonalIconButton
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import dev.typetype.android.R
import dev.typetype.android.domain.remote.RemoteKey

@Composable
internal fun VolumeRow(onKey: (RemoteKey) -> Unit, modifier: Modifier = Modifier) {
val haptics = LocalHapticFeedback.current
val press: (RemoteKey) -> Unit = { key ->
haptics.performHapticFeedback(HapticFeedbackType.TextHandleMove)
onKey(key)
}
Row(modifier = modifier, horizontalArrangement = Arrangement.spacedBy(24.dp), verticalAlignment = Alignment.CenterVertically) {
VolumeKey(Icons.AutoMirrored.Filled.VolumeDown, R.string.remote_volume_down) { press(RemoteKey.VolumeDown) }
VolumeKey(Icons.AutoMirrored.Filled.VolumeOff, R.string.remote_mute) { press(RemoteKey.Mute) }
VolumeKey(Icons.AutoMirrored.Filled.VolumeUp, R.string.remote_volume_up) { press(RemoteKey.VolumeUp) }
}
}

@Composable
private fun VolumeKey(icon: ImageVector, label: Int, onClick: () -> Unit) {
FilledTonalIconButton(onClick = onClick, modifier = Modifier.size(52.dp)) {
Icon(icon, contentDescription = stringResource(label))
}
}

// The phone's own volume buttons turn the TV up and down while its remote shows. Compose only hands key events to a
// focused node, so the controller holds the focus whenever the search field does not.
@Composable
internal fun Modifier.volumeKeys(onKey: (RemoteKey) -> Unit): Modifier {
val focus = remember { FocusRequester() }
LaunchedEffect(focus) { focus.requestFocus() }
return this
.onPreviewKeyEvent { event ->
val key = when (event.key) {
Key.VolumeUp -> RemoteKey.VolumeUp
Key.VolumeDown -> RemoteKey.VolumeDown
else -> return@onPreviewKeyEvent false
}
if (event.type == KeyEventType.KeyDown) onKey(key)
true
}
.focusRequester(focus)
.focusable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package dev.typetype.android.feature.remote

import androidx.lifecycle.SavedStateHandle
import dev.typetype.android.domain.remote.RemoteCommand
import dev.typetype.android.domain.remote.RemoteControlRepository
import dev.typetype.android.domain.remote.RemoteKey
import dev.typetype.android.domain.remote.RemoteSendResult
import dev.typetype.android.domain.remote.RemoteTv
import dev.typetype.android.domain.remote.RemoteTvEvent
import dev.typetype.android.domain.remote.RemoteTvScreen
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class RemoteViewModelTest {
private val dispatcher = StandardTestDispatcher()
private val salon = RemoteTv("tv-salon-0001", "Salon", RemoteTvScreen.Home, null, 0, 0, false)
private val chambre = RemoteTv("tv-chambre-01", "Chambre", RemoteTvScreen.Home, null, 0, 0, false)

private class FakeRepository(private val tvs: List<RemoteTv>) : RemoteControlRepository {
val sent = mutableListOf<Pair<String, RemoteCommand>>()
val events = MutableSharedFlow<RemoteTvEvent>()
var result = RemoteSendResult.Sent

override suspend fun tvs(): Result<List<RemoteTv>> = Result.success(tvs)

override suspend fun send(deviceId: String, command: RemoteCommand): RemoteSendResult {
sent += deviceId to command
return result
}

override fun follow(deviceId: String): Flow<RemoteTvEvent> = events
}

private fun viewModel(repository: FakeRepository, videoUrl: String? = null) =
RemoteViewModel(repository, SavedStateHandle(if (videoUrl == null) emptyMap() else mapOf("videoUrl" to videoUrl)))

@Before
fun setUp() {
Dispatchers.setMain(dispatcher)
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun aSingleTvIsTakenAndTheVideoSentFromTheMenuOpensOnIt() = runTest(dispatcher) {
val repository = FakeRepository(listOf(salon))
val model = viewModel(repository, "https://www.youtube.com/watch?v=x")
advanceUntilIdle()
assertEquals(salon, model.state.value.selected)
assertNull(model.state.value.pendingVideoUrl)
assertEquals(listOf(salon.deviceId to RemoteCommand.Open("https://www.youtube.com/watch?v=x")), repository.sent)
}

@Test
fun withTwoTvsTheVideoWaitsForTheChosenOne() = runTest(dispatcher) {
val repository = FakeRepository(listOf(salon, chambre))
val model = viewModel(repository, "https://www.youtube.com/watch?v=x")
advanceUntilIdle()
assertNull(model.state.value.selected)
assertTrue(repository.sent.isEmpty())
model.onAction(RemoteAction.Select(chambre))
advanceUntilIdle()
assertEquals(listOf(chambre.deviceId to RemoteCommand.Open("https://www.youtube.com/watch?v=x")), repository.sent)
}

@Test
fun typingSendsTheTextOnceTheFingersPause() = runTest(dispatcher) {
val repository = FakeRepository(listOf(salon))
val model = viewModel(repository)
advanceUntilIdle()
model.onAction(RemoteAction.Search("lo"))
advanceTimeBy(200)
model.onAction(RemoteAction.Search("lofi"))
advanceUntilIdle()
assertEquals(listOf(salon.deviceId to RemoteCommand.Type("lofi")), repository.sent)
}

@Test
fun theControllerFollowsTheScreenOfTheTvUntilItLeaves() = runTest(dispatcher) {
val repository = FakeRepository(listOf(salon))
val model = viewModel(repository)
advanceUntilIdle()
repository.events.emit(RemoteTvEvent.State(salon.copy(screen = RemoteTvScreen.Player, playing = true)))
advanceUntilIdle()
assertEquals(RemoteTvScreen.Player, model.state.value.selected?.screen)
assertFalse(model.state.value.gone)
repository.events.emit(RemoteTvEvent.Gone)
advanceUntilIdle()
assertTrue(model.state.value.gone)
}

@Test
fun anOfflineTvIsGoneAndAFailedSendIsShown() = runTest(dispatcher) {
val repository = FakeRepository(listOf(salon))
val model = viewModel(repository)
advanceUntilIdle()
repository.result = RemoteSendResult.Failed
model.onAction(RemoteAction.Send(RemoteCommand.Press(RemoteKey.VolumeUp)))
advanceUntilIdle()
assertTrue(model.state.value.sendFailed)
repository.result = RemoteSendResult.Offline
model.onAction(RemoteAction.Send(RemoteCommand.Pause))
advanceUntilIdle()
assertTrue(model.state.value.gone)
}

@Test
fun aRenameIsSentTrimmedAndABlankOneIsDropped() = runTest(dispatcher) {
val repository = FakeRepository(listOf(salon, chambre))
val model = viewModel(repository)
advanceUntilIdle()
model.onAction(RemoteAction.Rename(chambre, " "))
model.onAction(RemoteAction.Rename(chambre, " Bureau "))
advanceUntilIdle()
assertEquals(listOf(chambre.deviceId to RemoteCommand.Rename("Bureau")), repository.sent)
assertEquals(listOf("Salon", "Bureau"), model.state.value.tvs.map { it.name })
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ core = "1.19.1"
documentfile = "1.1.0"
gradle = "9.5.1"
junit = "4.13.2"
kotlinxCoroutines = "1.10.2"
kotlin = "2.4.21"
ksp = "2.3.13"
lifecycle = "2.11.0"
Expand Down Expand Up @@ -80,6 +81,7 @@ androidx-constraintlayout = { group = "androidx.constraintlayout", name = "const
androidx-documentfile = { module = "androidx.documentfile:documentfile", version.ref = "documentfile" }
androidx-fragment = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "fragment" }
junit = { module = "junit:junit", version.ref = "junit" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation" }
androidx-navigation-ui = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation" }
Expand Down
6 changes: 6 additions & 0 deletions release-notes/v1.14.0-beta.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
TypeType Android 1.14.0-beta.4

- Turn the TV volume up and down or mute it from the phone remote, with the new buttons or the phone's own volume keys while the remote is open.
- Pause the video on the phone when you send it to the TV with Play on TV.

The TV remote needs TypeType server 1.21.0-rc.2 or newer and TypeType TV 0.9.0-beta.3.