Artist/track/album form with a "played" time chip (now to yesterday) that submits through ScrobbleRepository. On success the form clears but keeps the time choice, and History reloads when revisited so the new play shows. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e4c19cc9ac
commit
16f701c855
7 files changed
+252
-4
No files matched your search
@@ -26,11 +26,11 @@ class MainActivity : ComponentActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
val history = (application as ScrobbledApplication).history
|
val app = application as ScrobbledApplication
|
||||||
setContent {
|
setContent {
|
||||||
ScrobbledTheme {
|
ScrobbledTheme {
|
||||||
val state by connectVm.state.collectAsState()
|
val state by connectVm.state.collectAsState()
|
||||||
if (state.connected) ScrobbledApp(history, onSignOut = connectVm::disconnect) else ConnectScreen(connectVm)
|
if (state.connected) ScrobbledApp(app.history, app.scrobbles, onSignOut = connectVm::disconnect) else ConnectScreen(connectVm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import dev.daubney.scrobbled.data.auth.DefaultAuthRepository
|
|||||||
import dev.daubney.scrobbled.data.auth.KeystoreCredentialStore
|
import dev.daubney.scrobbled.data.auth.KeystoreCredentialStore
|
||||||
import dev.daubney.scrobbled.data.history.DefaultHistoryRepository
|
import dev.daubney.scrobbled.data.history.DefaultHistoryRepository
|
||||||
import dev.daubney.scrobbled.data.history.HistoryRepository
|
import dev.daubney.scrobbled.data.history.HistoryRepository
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.DefaultScrobbleRepository
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||||
|
|
||||||
/** Hand-rolled service locator; small enough that a DI framework isn't worth it yet. */
|
/** Hand-rolled service locator; small enough that a DI framework isn't worth it yet. */
|
||||||
class ScrobbledApplication : Application() {
|
class ScrobbledApplication : Application() {
|
||||||
val auth: AuthRepository by lazy { DefaultAuthRepository(KeystoreCredentialStore(this)) }
|
val auth: AuthRepository by lazy { DefaultAuthRepository(KeystoreCredentialStore(this)) }
|
||||||
val history: HistoryRepository by lazy { DefaultHistoryRepository(auth) }
|
val history: HistoryRepository by lazy { DefaultHistoryRepository(auth) }
|
||||||
|
val scrobbles: ScrobbleRepository by lazy { DefaultScrobbleRepository(auth) }
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,9 @@ import androidx.lifecycle.viewmodel.initializer
|
|||||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import dev.daubney.scrobbled.data.history.HistoryRepository
|
import dev.daubney.scrobbled.data.history.HistoryRepository
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||||
|
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.HistoryScreen
|
||||||
import dev.daubney.scrobbled.ui.history.HistoryViewModel
|
import dev.daubney.scrobbled.ui.history.HistoryViewModel
|
||||||
import dev.daubney.scrobbled.ui.home.HomeScreen
|
import dev.daubney.scrobbled.ui.home.HomeScreen
|
||||||
@@ -34,7 +37,7 @@ enum class Destination(val route: String, val label: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ScrobbledApp(history: HistoryRepository, onSignOut: () -> Unit) {
|
fun ScrobbledApp(history: HistoryRepository, scrobbles: ScrobbleRepository, onSignOut: () -> Unit) {
|
||||||
val nav = rememberNavController()
|
val nav = rememberNavController()
|
||||||
val current by nav.currentBackStackEntryAsState()
|
val current by nav.currentBackStackEntryAsState()
|
||||||
Scaffold(
|
Scaffold(
|
||||||
@@ -61,7 +64,10 @@ fun ScrobbledApp(history: HistoryRepository, onSignOut: () -> Unit) {
|
|||||||
val vm = viewModel<HistoryViewModel>(factory = viewModelFactory { initializer { HistoryViewModel(history) } })
|
val vm = viewModel<HistoryViewModel>(factory = viewModelFactory { initializer { HistoryViewModel(history) } })
|
||||||
HistoryScreen(vm)
|
HistoryScreen(vm)
|
||||||
}
|
}
|
||||||
composable(Destination.Add.route) { Placeholder(Destination.Add.label) }
|
composable(Destination.Add.route) {
|
||||||
|
val vm = viewModel<AddViewModel>(factory = viewModelFactory { initializer { AddViewModel(scrobbles) } })
|
||||||
|
AddScreen(vm)
|
||||||
|
}
|
||||||
composable(Destination.Listen.route) { Placeholder(Destination.Listen.label) }
|
composable(Destination.Listen.route) { Placeholder(Destination.Listen.label) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package dev.daubney.scrobbled.ui.add
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
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.statusBarsPadding
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.horizontalScroll
|
||||||
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material3.Button
|
||||||
|
import androidx.compose.material3.FilterChip
|
||||||
|
import androidx.compose.material3.FilterChipDefaults
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
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
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AddScreen(vm: AddViewModel) {
|
||||||
|
val s by vm.state.collectAsState()
|
||||||
|
Column(
|
||||||
|
Modifier.fillMaxSize().statusBarsPadding().imePadding().verticalScroll(rememberScrollState()).padding(16.dp),
|
||||||
|
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||||
|
) {
|
||||||
|
Text("Add scrobble", style = MaterialTheme.typography.headlineSmall)
|
||||||
|
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)
|
||||||
|
|
||||||
|
Text("Played", style = MaterialTheme.typography.labelLarge)
|
||||||
|
Row(Modifier.horizontalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||||
|
WhenOption.entries.forEach { o ->
|
||||||
|
FilterChip(
|
||||||
|
selected = s.whenOption == o, onClick = { vm.onWhen(o) }, label = { Text(o.label) },
|
||||||
|
colors = FilterChipDefaults.filterChipColors(selectedContainerColor = MaterialTheme.colorScheme.outline),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s.error?.let { Text(it, color = MaterialTheme.colorScheme.error) }
|
||||||
|
s.message?.let { Text(it, color = MaterialTheme.colorScheme.primary) }
|
||||||
|
Button(vm::submit, Modifier.fillMaxWidth(), enabled = s.canSubmit) {
|
||||||
|
Text(if (s.submitting) "Scrobbling…" else "Scrobble")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package dev.daubney.scrobbled.ui.add
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.TrackDraft
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import java.time.Duration
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
/** When the track was played, relative to the moment of submitting. */
|
||||||
|
enum class WhenOption(val label: String, val ago: Duration) {
|
||||||
|
Now("Now", Duration.ZERO),
|
||||||
|
Minutes30("30 min ago", Duration.ofMinutes(30)),
|
||||||
|
Hour1("1 h ago", Duration.ofHours(1)),
|
||||||
|
Hours3("3 h ago", Duration.ofHours(3)),
|
||||||
|
Yesterday("Yesterday", Duration.ofHours(24)),
|
||||||
|
}
|
||||||
|
|
||||||
|
data class AddState(
|
||||||
|
val artist: String = "",
|
||||||
|
val track: String = "",
|
||||||
|
val album: String = "",
|
||||||
|
val whenOption: WhenOption = WhenOption.Now,
|
||||||
|
val submitting: Boolean = false,
|
||||||
|
val error: String? = null,
|
||||||
|
/** Set after a successful submit, e.g. "Scrobbled Reckoner". */
|
||||||
|
val message: String? = null,
|
||||||
|
) {
|
||||||
|
val canSubmit get() = !submitting && artist.isNotBlank() && track.isNotBlank()
|
||||||
|
}
|
||||||
|
|
||||||
|
class AddViewModel(
|
||||||
|
private val scrobbles: ScrobbleRepository,
|
||||||
|
private val clock: () -> Instant = Instant::now,
|
||||||
|
) : ViewModel() {
|
||||||
|
private val _state = MutableStateFlow(AddState())
|
||||||
|
val state: StateFlow<AddState> = _state.asStateFlow()
|
||||||
|
|
||||||
|
private fun edit(f: AddState.() -> AddState) = _state.update { it.f().copy(error = null, message = null) }
|
||||||
|
|
||||||
|
fun onArtist(v: String) = edit { copy(artist = v) }
|
||||||
|
fun onTrack(v: String) = edit { copy(track = v) }
|
||||||
|
fun onAlbum(v: String) = edit { copy(album = v) }
|
||||||
|
fun onWhen(v: WhenOption) = edit { copy(whenOption = v) }
|
||||||
|
|
||||||
|
fun submit() {
|
||||||
|
val s = _state.value
|
||||||
|
if (!s.canSubmit) return
|
||||||
|
val draft = TrackDraft(
|
||||||
|
artist = s.artist, track = s.track, album = s.album,
|
||||||
|
playedAt = clock().minus(s.whenOption.ago),
|
||||||
|
)
|
||||||
|
draft.validate(clock())?.let { err -> _state.update { it.copy(error = err) }; return }
|
||||||
|
_state.update { it.copy(submitting = true, error = null, message = null) }
|
||||||
|
viewModelScope.launch {
|
||||||
|
try {
|
||||||
|
scrobbles.submit(draft)
|
||||||
|
// Keep the time choice (adding several older tracks in a row is common), clear the rest.
|
||||||
|
_state.value = AddState(whenOption = s.whenOption, message = "Scrobbled ${draft.track.trim()}")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
_state.update { it.copy(submitting = false, error = e.message ?: "Something went wrong") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,9 @@ import androidx.compose.runtime.snapshotFlow
|
|||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||||
|
import androidx.lifecycle.repeatOnLifecycle
|
||||||
import dev.daubney.scrobbled.ui.ScrobbleRow
|
import dev.daubney.scrobbled.ui.ScrobbleRow
|
||||||
|
|
||||||
private const val PREFETCH = 10
|
private const val PREFETCH = 10
|
||||||
@@ -34,6 +37,12 @@ fun HistoryScreen(vm: HistoryViewModel) {
|
|||||||
val state by vm.state.collectAsState()
|
val state by vm.state.collectAsState()
|
||||||
val listState = rememberLazyListState()
|
val listState = rememberLazyListState()
|
||||||
|
|
||||||
|
// Returning to the tab (e.g. after adding a scrobble) reloads from the top; the first visit is handled below.
|
||||||
|
val lifecycle = LocalLifecycleOwner.current.lifecycle
|
||||||
|
LaunchedEffect(lifecycle, vm) {
|
||||||
|
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { if (vm.state.value.items.isNotEmpty()) vm.refresh() }
|
||||||
|
}
|
||||||
|
|
||||||
// Load more when within PREFETCH rows of the end (also covers the very first load).
|
// Load more when within PREFETCH rows of the end (also covers the very first load).
|
||||||
LaunchedEffect(listState, vm) {
|
LaunchedEffect(listState, vm) {
|
||||||
snapshotFlow {
|
snapshotFlow {
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package dev.daubney.scrobbled.ui.add
|
||||||
|
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.ScrobbleRepository
|
||||||
|
import dev.daubney.scrobbled.data.scrobble.TrackDraft
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||||
|
import kotlinx.coroutines.test.resetMain
|
||||||
|
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
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
private class FakeScrobbles : ScrobbleRepository {
|
||||||
|
val submitted = mutableListOf<TrackDraft>()
|
||||||
|
var failure: Exception? = null
|
||||||
|
override suspend fun submit(draft: TrackDraft) {
|
||||||
|
failure?.let { throw it }
|
||||||
|
submitted += draft
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
class AddViewModelTest {
|
||||||
|
private val now = Instant.parse("2026-10-12T12:00:00Z")
|
||||||
|
private val repo = FakeScrobbles()
|
||||||
|
private lateinit var vm: AddViewModel
|
||||||
|
|
||||||
|
@Before fun setUp() {
|
||||||
|
Dispatchers.setMain(UnconfinedTestDispatcher())
|
||||||
|
vm = AddViewModel(repo) { now }
|
||||||
|
}
|
||||||
|
@After fun tearDown() = Dispatchers.resetMain()
|
||||||
|
|
||||||
|
private fun fill() { vm.onArtist("Radiohead"); vm.onTrack("Reckoner") }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun cannotSubmitWithoutArtistAndTrack() {
|
||||||
|
assertFalse(vm.state.value.canSubmit)
|
||||||
|
vm.onArtist("A")
|
||||||
|
assertFalse(vm.state.value.canSubmit)
|
||||||
|
vm.onTrack("T")
|
||||||
|
assertTrue(vm.state.value.canSubmit)
|
||||||
|
vm.submit()
|
||||||
|
assertEquals(1, repo.submitted.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun submitSendsDraftWithTimeRelativeToNow() {
|
||||||
|
fill(); vm.onAlbum("In Rainbows"); vm.onWhen(WhenOption.Hour1)
|
||||||
|
vm.submit()
|
||||||
|
val d = repo.submitted.single()
|
||||||
|
assertEquals("Radiohead", d.artist)
|
||||||
|
assertEquals("In Rainbows", d.album)
|
||||||
|
assertEquals(now.minusSeconds(3600), d.playedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun successClearsFormKeepsTimeChoiceAndShowsMessage() {
|
||||||
|
fill(); vm.onWhen(WhenOption.Hours3)
|
||||||
|
vm.submit()
|
||||||
|
val s = vm.state.value
|
||||||
|
assertEquals("", s.artist)
|
||||||
|
assertEquals("", s.track)
|
||||||
|
assertEquals(WhenOption.Hours3, s.whenOption)
|
||||||
|
assertEquals("Scrobbled Reckoner", s.message)
|
||||||
|
assertFalse(s.submitting)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun failureKeepsFormAndShowsError() {
|
||||||
|
fill()
|
||||||
|
repo.failure = RuntimeException("offline")
|
||||||
|
vm.submit()
|
||||||
|
val s = vm.state.value
|
||||||
|
assertEquals("offline", s.error)
|
||||||
|
assertEquals("Radiohead", s.artist)
|
||||||
|
assertFalse(s.submitting)
|
||||||
|
assertTrue(s.canSubmit)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun editingClearsMessageAndError() {
|
||||||
|
fill(); vm.submit()
|
||||||
|
assertTrue(vm.state.value.message != null)
|
||||||
|
vm.onArtist("x")
|
||||||
|
assertNull(vm.state.value.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun yesterdayIsWithinServerWindow() {
|
||||||
|
fill(); vm.onWhen(WhenOption.Yesterday)
|
||||||
|
vm.submit()
|
||||||
|
assertEquals(1, repo.submitted.size)
|
||||||
|
assertNull(vm.state.value.error)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user