diff --git a/app/build.gradle b/app/build.gradle index 69666800..00491e63 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,7 +58,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.30.25' + implementation 'com.simplemobiletools:commons:5.30.26' implementation 'org.greenrobot:eventbus:3.2.0' implementation 'com.squareup.picasso:picasso:2.71828' implementation 'androidx.media:media:1.2.0' diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt index 752ddb8e..f011b0d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt @@ -4,15 +4,14 @@ import android.content.Intent import android.os.Bundle import com.google.gson.Gson import com.google.gson.reflect.TypeToken +import com.simplemobiletools.commons.extensions.getFormattedDuration import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.adapters.AlbumsAdapter import com.simplemobiletools.musicplayer.extensions.getAlbums import com.simplemobiletools.musicplayer.extensions.getSongsSync import com.simplemobiletools.musicplayer.helpers.ALBUM import com.simplemobiletools.musicplayer.helpers.ARTIST -import com.simplemobiletools.musicplayer.models.Album -import com.simplemobiletools.musicplayer.models.Artist -import com.simplemobiletools.musicplayer.models.ListItem +import com.simplemobiletools.musicplayer.models.* import kotlinx.android.synthetic.main.activity_albums.* // Artists -> Albums -> Songs @@ -26,15 +25,26 @@ class AlbumsActivity : SimpleActivity() { title = artist.title getAlbums(artist) { albums -> - val items = albums.toMutableList() as ArrayList + val listItems = ArrayList() + val albumsSectionLabel = resources.getQuantityString(R.plurals.albums, albums.size, albums.size) + listItems.add(AlbumSection(albumsSectionLabel)) + listItems.addAll(albums) + var trackFullDuration = 0 + val tracksToAdd = ArrayList() albums.forEach { val tracks = getSongsSync(it.id) - items.addAll(tracks) + trackFullDuration += tracks.sumBy { it.duration } + tracksToAdd.addAll(tracks) } + var tracksSectionLabel = resources.getQuantityString(R.plurals.tracks, tracksToAdd.size, tracksToAdd.size) + tracksSectionLabel += " • ${trackFullDuration.getFormattedDuration(true)}" + listItems.add(AlbumSection(tracksSectionLabel)) + + listItems.addAll(tracksToAdd) runOnUiThread { - AlbumsAdapter(this, items, albums_list) { + AlbumsAdapter(this, listItems, albums_list) { if (it is Album) { Intent(this, SongsActivity::class.java).apply { putExtra(ALBUM, Gson().toJson(it)) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt index ba8d2905..4861aae9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt @@ -16,9 +16,11 @@ import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.activities.SimpleActivity import com.simplemobiletools.musicplayer.models.Album +import com.simplemobiletools.musicplayer.models.AlbumSection import com.simplemobiletools.musicplayer.models.ListItem import com.simplemobiletools.musicplayer.models.Song import kotlinx.android.synthetic.main.item_album.view.* +import kotlinx.android.synthetic.main.item_section.view.* import kotlinx.android.synthetic.main.item_song.view.* import java.util.* @@ -26,8 +28,9 @@ import java.util.* class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { - private val ITEM_ALBUM = 0 - private val ITEM_TRACK = 1 + private val ITEM_SECTION = 0 + private val ITEM_ALBUM = 1 + private val ITEM_TRACK = 2 init { setupDragListener(true) @@ -36,10 +39,10 @@ class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList, re override fun getActionMenuId() = R.menu.cab_albums override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val layout = if (viewType == ITEM_ALBUM) { - R.layout.item_album - } else { - R.layout.item_song + val layout = when (viewType) { + ITEM_SECTION -> R.layout.item_section + ITEM_ALBUM -> R.layout.item_album + else -> R.layout.item_song } return createViewHolder(layout, parent) @@ -48,10 +51,10 @@ class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList, re override fun onBindViewHolder(holder: ViewHolder, position: Int) { val item = items.getOrNull(position) ?: return holder.bindView(item, true, true) { itemView, layoutPosition -> - if (item is Album) { - setupAlbum(itemView, item) - } else { - setupTrack(itemView, item as Song) + when (item) { + is AlbumSection -> setupSection(itemView, item) + is Album -> setupAlbum(itemView, item) + else -> setupTrack(itemView, item as Song) } } bindViewHolder(holder) @@ -60,11 +63,10 @@ class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList, re override fun getItemCount() = items.size override fun getItemViewType(position: Int): Int { - val item = items[position] - return if (item is Album) { - ITEM_ALBUM - } else { - ITEM_TRACK + return when (items[position]) { + is AlbumSection -> ITEM_SECTION + is Album -> ITEM_ALBUM + else -> ITEM_TRACK } } @@ -116,4 +118,11 @@ class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList, re song_duration.setTextColor(textColor) } } + + private fun setupSection(view: View, section: AlbumSection) { + view.apply { + item_section.text = section.title + item_section.setTextColor(textColor) + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/AlbumSection.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/AlbumSection.kt new file mode 100644 index 00000000..6baa6351 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/AlbumSection.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.musicplayer.models + +data class AlbumSection(val title: String) : ListItem() diff --git a/app/src/main/res/layout/item_section.xml b/app/src/main/res/layout/item_section.xml new file mode 100644 index 00000000..be57403d --- /dev/null +++ b/app/src/main/res/layout/item_section.xml @@ -0,0 +1,10 @@ + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 274428b9..6d8fb9a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -49,13 +49,13 @@ Artists - %d album - %d albums + %d Album + %d Albums - %d track - %d tracks + %d Track + %d Tracks