refactor(android): the track-list heart buttons share LikesRepository.toggleTrackLike (DRY A8)

Album detail, playlist detail and the player each flipped a track's like
with the same lines (desired = id !in likedTrackIds; toggleLike(TRACK…)).
They call toggleTrackLike(trackId, likedIds) now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 10:19:54 -05:00
co-authored by Claude Opus 5.5
parent d2918aeeb0
commit 5853c52640
4 changed files with 11 additions and 12 deletions
@@ -133,9 +133,6 @@ class AlbumDetailViewModel @Inject constructor(
}
fun toggleLikeTrack(trackId: String) {
val desired = trackId !in likedTrackIds.value
viewModelScope.launch {
likes.toggleLike(LikesRepository.ENTITY_TRACK, trackId, desired)
}
viewModelScope.launch { likes.toggleTrackLike(trackId, likedTrackIds.value) }
}
}
@@ -148,6 +148,14 @@ class LikesRepository @Inject constructor(
// ── Writes (optimistic local → best-effort REST → enqueue on fail) ──
/**
* Flips a track's like, reading whether it is liked now from [likedIds],
* the set the screen is already showing. The heart buttons on track lists
* (an album, a playlist, the player) call this.
*/
suspend fun toggleTrackLike(trackId: String, likedIds: Set<String>): Boolean =
toggleLike(ENTITY_TRACK, trackId, desiredState = trackId !in likedIds)
/**
* Toggle like state for [entityType]/[entityId]. UI doesn't need to
* await this; the Flow re-emits immediately from the optimistic
@@ -61,10 +61,7 @@ class PlayerViewModel @Inject constructor(
fun clearQueue() = controller.clearQueue()
fun toggleLikeTrack(trackId: String) {
val desired = trackId !in likedTrackIds.value
viewModelScope.launch {
likes.toggleLike(LikesRepository.ENTITY_TRACK, trackId, desired)
}
viewModelScope.launch { likes.toggleTrackLike(trackId, likedTrackIds.value) }
}
}
@@ -228,10 +228,7 @@ class PlaylistDetailViewModel @Inject constructor(
}
fun toggleLikeTrack(trackId: String) {
val desired = trackId !in likedTrackIds.value
viewModelScope.launch {
likes.toggleLike(LikesRepository.ENTITY_TRACK, trackId, desired)
}
viewModelScope.launch { likes.toggleTrackLike(trackId, likedTrackIds.value) }
}
/**