Tap to listen (permission prompt on first use), progress while recording, then an editable confirmation with Scrobble / Set as now playing / Listen again. Shares the artist/track/album form with the Add screen and bundles screen dependencies into AppServices. Recognition currently uses FakeRecognizer until ACRCloud keys are available. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2ec12ced67
commit
f80494241b
8 files changed
+192
-19
No files matched your search
@@ -2,6 +2,9 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
|
||||
<uses-feature android:name="android.hardware.microphone" android:required="false" />
|
||||
|
||||
<application
|
||||
android:name=".ScrobbledApplication"
|
||||
|
||||
@@ -30,7 +30,7 @@ class MainActivity : ComponentActivity() {
|
||||
setContent {
|
||||
ScrobbledTheme {
|
||||
val state by connectVm.state.collectAsState()
|
||||
if (state.connected) ScrobbledApp(app.history, app.queue.observeCount(), app.scrobbles, app.trackSearch, onSignOut = connectVm::disconnect) else ConnectScreen(connectVm)
|
||||
if (state.connected) ScrobbledApp(app.services, onSignOut = connectVm::disconnect) else ConnectScreen(connectVm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,11 @@ import dev.daubney.scrobbled.data.scrobble.DefaultScrobbleRepository
|
||||
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||
import dev.daubney.scrobbled.data.search.MusicBrainzSearch
|
||||
import dev.daubney.scrobbled.data.search.TrackSearch
|
||||
import dev.daubney.scrobbled.data.recognition.AudioRecordRecorder
|
||||
import dev.daubney.scrobbled.data.recognition.AudioRecorder
|
||||
import dev.daubney.scrobbled.data.recognition.FakeRecognizer
|
||||
import dev.daubney.scrobbled.data.recognition.Recognizer
|
||||
import dev.daubney.scrobbled.ui.AppServices
|
||||
|
||||
/** Hand-rolled service locator; small enough that a DI framework isn't worth it yet. */
|
||||
class ScrobbledApplication : Application() {
|
||||
@@ -32,6 +37,12 @@ class ScrobbledApplication : Application() {
|
||||
val queueFlusher by lazy { QueueFlusher(queue, auth) }
|
||||
val scrobbles: ScrobbleRepository by lazy { DefaultScrobbleRepository(auth, queue, scheduler) }
|
||||
val trackSearch: TrackSearch by lazy { MusicBrainzSearch() }
|
||||
val recorder: AudioRecorder by lazy { AudioRecordRecorder() }
|
||||
|
||||
// TODO: swap for the ACRCloud recognizer once its host/keys are available.
|
||||
val recognizer: Recognizer by lazy { FakeRecognizer() }
|
||||
|
||||
val services by lazy { AppServices(history, queue.observeCount(), scrobbles, trackSearch, recorder, recognizer) }
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package dev.daubney.scrobbled.ui
|
||||
|
||||
import dev.daubney.scrobbled.data.history.HistoryRepository
|
||||
import dev.daubney.scrobbled.data.recognition.AudioRecorder
|
||||
import dev.daubney.scrobbled.data.recognition.Recognizer
|
||||
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||
import dev.daubney.scrobbled.data.search.TrackSearch
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/** Everything the screens need from the data layer, bundled so navigation doesn't take a parameter per service. */
|
||||
class AppServices(
|
||||
val history: HistoryRepository,
|
||||
val pendingScrobbles: Flow<Int>,
|
||||
val scrobbles: ScrobbleRepository,
|
||||
val trackSearch: TrackSearch,
|
||||
val recorder: AudioRecorder,
|
||||
val recognizer: Recognizer,
|
||||
)
|
||||
@@ -10,7 +10,6 @@ import androidx.compose.material3.NavigationBarItemDefaults
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -18,15 +17,14 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||
import androidx.navigation.compose.NavHost
|
||||
import dev.daubney.scrobbled.data.history.HistoryRepository
|
||||
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||
import dev.daubney.scrobbled.data.search.TrackSearch
|
||||
import dev.daubney.scrobbled.ui.add.AddScreen
|
||||
import dev.daubney.scrobbled.ui.add.AddViewModel
|
||||
import dev.daubney.scrobbled.ui.history.HistoryScreen
|
||||
import dev.daubney.scrobbled.ui.history.HistoryViewModel
|
||||
import dev.daubney.scrobbled.ui.home.HomeScreen
|
||||
import dev.daubney.scrobbled.ui.home.HomeViewModel
|
||||
import dev.daubney.scrobbled.ui.listen.ListenScreen
|
||||
import dev.daubney.scrobbled.ui.listen.ListenViewModel
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
@@ -39,7 +37,7 @@ enum class Destination(val route: String, val label: String) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ScrobbledApp(history: HistoryRepository, pending: Flow<Int>, scrobbles: ScrobbleRepository, trackSearch: TrackSearch, onSignOut: () -> Unit) {
|
||||
fun ScrobbledApp(services: AppServices, onSignOut: () -> Unit) {
|
||||
val nav = rememberNavController()
|
||||
val current by nav.currentBackStackEntryAsState()
|
||||
Scaffold(
|
||||
@@ -59,23 +57,23 @@ fun ScrobbledApp(history: HistoryRepository, pending: Flow<Int>, scrobbles: Scro
|
||||
) { padding ->
|
||||
NavHost(nav, startDestination = Destination.Home.route, modifier = Modifier.padding(bottom = padding.calculateBottomPadding())) {
|
||||
composable(Destination.Home.route) {
|
||||
val vm = viewModel<HomeViewModel>(factory = viewModelFactory { initializer { HomeViewModel(history, pending) } })
|
||||
val vm = viewModel<HomeViewModel>(factory = viewModelFactory { initializer { HomeViewModel(services.history, services.pendingScrobbles) } })
|
||||
HomeScreen(vm, onSignOut)
|
||||
}
|
||||
composable(Destination.History.route) {
|
||||
val vm = viewModel<HistoryViewModel>(factory = viewModelFactory { initializer { HistoryViewModel(history) } })
|
||||
val vm = viewModel<HistoryViewModel>(factory = viewModelFactory { initializer { HistoryViewModel(services.history) } })
|
||||
HistoryScreen(vm)
|
||||
}
|
||||
composable(Destination.Add.route) {
|
||||
val vm = viewModel<AddViewModel>(factory = viewModelFactory { initializer { AddViewModel(scrobbles, trackSearch) } })
|
||||
val vm = viewModel<AddViewModel>(factory = viewModelFactory { initializer { AddViewModel(services.scrobbles, services.trackSearch) } })
|
||||
AddScreen(vm)
|
||||
}
|
||||
composable(Destination.Listen.route) { Placeholder(Destination.Listen.label) }
|
||||
composable(Destination.Listen.route) {
|
||||
val vm = viewModel<ListenViewModel>(
|
||||
factory = viewModelFactory { initializer { ListenViewModel(services.recorder, services.recognizer, services.scrobbles) } },
|
||||
)
|
||||
ListenScreen(vm)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Placeholder(title: String) {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Text(title) }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.daubney.scrobbled.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
/** The artist / track / album inputs shared by every screen that lets the user edit a track before submitting. */
|
||||
@Composable
|
||||
fun TrackFields(
|
||||
artist: String, track: String, album: String,
|
||||
onArtist: (String) -> Unit, onTrack: (String) -> Unit, onAlbum: (String) -> Unit,
|
||||
) {
|
||||
val caps = KeyboardOptions(capitalization = KeyboardCapitalization.Words, imeAction = ImeAction.Next)
|
||||
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
OutlinedTextField(artist, onArtist, Modifier.fillMaxWidth(), singleLine = true, label = { Text("Artist") }, keyboardOptions = caps)
|
||||
OutlinedTextField(track, onTrack, Modifier.fillMaxWidth(), singleLine = true, label = { Text("Track") }, keyboardOptions = caps)
|
||||
OutlinedTextField(album, onAlbum, Modifier.fillMaxWidth(), singleLine = true, label = { Text("Album (optional)") }, keyboardOptions = caps)
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import dev.daubney.scrobbled.data.search.TrackResult
|
||||
import dev.daubney.scrobbled.ui.TrackFields
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -97,10 +98,7 @@ private fun ManualTab(s: AddState, vm: AddViewModel) {
|
||||
Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
val caps = KeyboardOptions(capitalization = KeyboardCapitalization.Words, imeAction = ImeAction.Next)
|
||||
OutlinedTextField(s.artist, vm::onArtist, Modifier.fillMaxWidth(), singleLine = true, label = { Text("Artist") }, keyboardOptions = caps)
|
||||
OutlinedTextField(s.track, vm::onTrack, Modifier.fillMaxWidth(), singleLine = true, label = { Text("Track") }, keyboardOptions = caps)
|
||||
OutlinedTextField(s.album, vm::onAlbum, Modifier.fillMaxWidth(), singleLine = true, label = { Text("Album (optional)") }, keyboardOptions = caps)
|
||||
TrackFields(s.artist, s.track, s.album, vm::onArtist, vm::onTrack, vm::onAlbum)
|
||||
if (s.mbid.isNotEmpty()) {
|
||||
Text("Matched on MusicBrainz", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package dev.daubney.scrobbled.ui.listen
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.imePadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.ContextCompat
|
||||
import dev.daubney.scrobbled.ui.TrackFields
|
||||
import dev.daubney.scrobbled.ui.theme.Glow
|
||||
import dev.daubney.scrobbled.ui.theme.Panel
|
||||
|
||||
@Composable
|
||||
fun ListenScreen(vm: ListenViewModel) {
|
||||
val s by vm.state.collectAsState()
|
||||
val context = LocalContext.current
|
||||
val permission = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
if (granted) vm.start() else vm.onPermissionDenied()
|
||||
}
|
||||
val listen = {
|
||||
val granted = ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
if (granted) vm.start() else permission.launch(Manifest.permission.RECORD_AUDIO)
|
||||
}
|
||||
|
||||
Column(
|
||||
Modifier.fillMaxSize().statusBarsPadding().imePadding().verticalScroll(rememberScrollState()).padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
Text("Listen", style = MaterialTheme.typography.headlineSmall)
|
||||
when (s.phase) {
|
||||
ListenPhase.Idle -> Idle(s, listen)
|
||||
ListenPhase.Recording -> Working("Listening…", s.progress, vm::cancel)
|
||||
ListenPhase.Identifying -> Working("Identifying…", null, vm::cancel)
|
||||
ListenPhase.Confirm -> Confirm(s, vm, listen)
|
||||
ListenPhase.NoMatch -> NoMatch(listen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Idle(s: ListenState, onListen: () -> Unit) {
|
||||
Text(
|
||||
"Hold your phone near the music, then tap to listen for about 10 seconds.",
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
ListenButton(onListen)
|
||||
s.message?.let { Text(it, color = MaterialTheme.colorScheme.primary, modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center) }
|
||||
s.error?.let { Text(it, color = MaterialTheme.colorScheme.error, modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center) }
|
||||
}
|
||||
|
||||
/** A big round button in the web UI's amber-on-dark panel style. */
|
||||
@Composable
|
||||
private fun ListenButton(onClick: () -> Unit) {
|
||||
Box(Modifier.fillMaxWidth().padding(vertical = 24.dp), contentAlignment = Alignment.Center) {
|
||||
Box(
|
||||
Modifier.size(168.dp).clip(CircleShape).background(Panel).clickable(onClick = onClick),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text("Listen", style = MaterialTheme.typography.titleLarge, color = Glow)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Working(label: String, progress: Float?, onCancel: () -> Unit) {
|
||||
Column(Modifier.fillMaxWidth().padding(vertical = 24.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(16.dp)) {
|
||||
Text(label, style = MaterialTheme.typography.titleLarge)
|
||||
if (progress != null) LinearProgressIndicator(progress = { progress }, Modifier.fillMaxWidth()) else CircularProgressIndicator()
|
||||
TextButton(onCancel) { Text("Cancel") }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Confirm(s: ListenState, vm: ListenViewModel, onListenAgain: () -> Unit) {
|
||||
Text("Is this right? Fix anything that's off before scrobbling.", color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
TrackFields(s.artist, s.track, s.album, vm::onArtist, vm::onTrack, vm::onAlbum)
|
||||
s.error?.let { Text(it, color = MaterialTheme.colorScheme.error) }
|
||||
Button(vm::scrobble, Modifier.fillMaxWidth(), enabled = s.canSubmit) { Text(if (s.busy) "Working…" else "Scrobble") }
|
||||
OutlinedButton(vm::setNowPlaying, Modifier.fillMaxWidth(), enabled = s.canSubmit) { Text("Set as now playing") }
|
||||
TextButton(onListenAgain, Modifier.fillMaxWidth()) { Text("Not right? Listen again") }
|
||||
TextButton(vm::cancel, Modifier.fillMaxWidth()) { Text("Discard") }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun NoMatch(onListen: () -> Unit) {
|
||||
Text("Couldn't identify that. Try again, closer to the speaker.", color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Button(onListen, Modifier.fillMaxWidth()) { Text("Try again") }
|
||||
}
|
||||
Reference in new issue
Block a user