-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
286 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/main/java/me/vanpetegem/accentor/ui/albums/AlbumView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package me.vanpetegem.accentor.ui.albums | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.aspectRatio | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.ContentAlpha | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.IconButton | ||
import androidx.compose.material.LocalContentColor | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.livedata.observeAsState | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import coil.compose.rememberImagePainter | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.launch | ||
import me.vanpetegem.accentor.R | ||
import me.vanpetegem.accentor.media.MediaSessionConnection | ||
import me.vanpetegem.accentor.ui.tracks.TrackRow | ||
|
||
@Composable | ||
fun AlbumView(id: Int, albumViewModel: AlbumViewModel = viewModel(), mediaSessionConnection: MediaSessionConnection = viewModel()) { | ||
val scope = rememberCoroutineScope() | ||
val albumState by albumViewModel.getAlbum(id).observeAsState() | ||
if (albumState != null) { | ||
val album = albumState!! | ||
val tracks by albumViewModel.tracksForAlbum(album).observeAsState() | ||
LazyColumn(modifier = Modifier.fillMaxSize()) { | ||
item { | ||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(bottom = 8.dp)) { | ||
Image( | ||
painter = if (album.image500 != null) { | ||
rememberImagePainter(album.image500) { | ||
placeholder(R.drawable.ic_album) | ||
} | ||
} else { | ||
painterResource(R.drawable.ic_album) | ||
}, | ||
contentDescription = stringResource(R.string.artist_image), | ||
modifier = Modifier.width(128.dp).aspectRatio(1f), | ||
) | ||
Column { | ||
Text(album.title, style = MaterialTheme.typography.h4, modifier = Modifier.padding(start = 8.dp)) | ||
Text( | ||
album.stringifyAlbumArtists(), | ||
style = MaterialTheme.typography.subtitle1, | ||
color = LocalContentColor.current.copy(alpha = ContentAlpha.medium), | ||
modifier = Modifier.padding(start = 8.dp), | ||
) | ||
Row(modifier = Modifier.padding(8.dp)) { | ||
IconButton(onClick = { scope.launch(IO) { mediaSessionConnection.play(album) } }) { | ||
Icon(painterResource(R.drawable.ic_play), contentDescription = stringResource(R.string.play_now)) | ||
} | ||
IconButton(onClick = { scope.launch(IO) { mediaSessionConnection.addTracksToQueue(album) } }) { | ||
Icon(painterResource(R.drawable.ic_queue_add), contentDescription = stringResource(R.string.play_last)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (tracks != null && tracks!!.size > 0) { | ||
items(tracks!!.size) { i -> TrackRow(tracks!![i]) } | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/me/vanpetegem/accentor/ui/albums/AlbumViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.vanpetegem.accentor.ui.albums | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Transformations.map | ||
import me.vanpetegem.accentor.data.AccentorDatabase | ||
import me.vanpetegem.accentor.data.albums.Album | ||
import me.vanpetegem.accentor.data.albums.AlbumRepository | ||
import me.vanpetegem.accentor.data.authentication.AuthenticationDataSource | ||
import me.vanpetegem.accentor.data.authentication.AuthenticationRepository | ||
import me.vanpetegem.accentor.data.tracks.Track | ||
import me.vanpetegem.accentor.data.tracks.TrackRepository | ||
|
||
class AlbumViewModel(application: Application) : AndroidViewModel(application) { | ||
private val database = AccentorDatabase.getDatabase(application) | ||
private val authenticationRepository = AuthenticationRepository(AuthenticationDataSource(application)) | ||
private val albumRepository = AlbumRepository(database.albumDao(), authenticationRepository) | ||
private val trackRepository = TrackRepository(database.trackDao(), authenticationRepository) | ||
|
||
fun getAlbum(id: Int): LiveData<Album> = map(albumRepository.allAlbumsById) { albums -> | ||
albums[id] | ||
} | ||
|
||
fun tracksForAlbum(album: Album): LiveData<List<Track>> = map(trackRepository.findByAlbum(album)) { tracks -> | ||
val copy = tracks.toMutableList() | ||
copy.sortWith({ t1, t2 -> t1.number.compareTo(t2.number) }) | ||
copy | ||
} | ||
} |
Oops, something went wrong.