From 8787ef0fddf87c9dacab17083534f3e7e81e5402 Mon Sep 17 00:00:00 2001 From: Sebastiano Barezzi Date: Sun, 3 Nov 2024 20:14:47 +0100 Subject: [PATCH] Twelve: Album file types Change-Id: Iecb01cfd3266949a7088354a98201c5101a1a59e --- .../twelve/fragments/AlbumFragment.kt | 10 ++ .../twelve/viewmodels/AlbumViewModel.kt | 28 ++++++ app/src/main/res/layout/album_labels.xml | 97 +++++++++++++------ 3 files changed, 107 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt b/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt index fe8d0072..f2426712 100644 --- a/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt +++ b/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt @@ -26,6 +26,7 @@ import androidx.navigation.ui.setupWithNavController import androidx.recyclerview.widget.RecyclerView import coil3.load import com.google.android.material.appbar.MaterialToolbar +import com.google.android.material.card.MaterialCardView import com.google.android.material.progressindicator.LinearProgressIndicator import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.collectLatest @@ -54,6 +55,8 @@ class AlbumFragment : Fragment(R.layout.fragment_album) { // Views private val albumTitleTextView by getViewProperty(R.id.albumTitleTextView) private val artistNameTextView by getViewProperty(R.id.artistNameTextView) + private val fileTypeMaterialCardView by getViewProperty(R.id.fileTypeMaterialCardView) + private val fileTypeTextView by getViewProperty(R.id.fileTypeTextView) private val infoNestedScrollView by getViewProperty(R.id.infoNestedScrollView) private val linearProgressIndicator by getViewProperty(R.id.linearProgressIndicator) private val noElementsNestedScrollView by getViewProperty(R.id.noElementsNestedScrollView) @@ -317,6 +320,13 @@ class AlbumFragment : Fragment(R.layout.fragment_album) { noElementsNestedScrollView.isVisible = isEmpty } } + + launch { + viewModel.albumFileTypes.collectLatest { + fileTypeMaterialCardView.isVisible = it.isNotEmpty() + fileTypeTextView.text = it.joinToString(" / ") + } + } } } diff --git a/app/src/main/java/org/lineageos/twelve/viewmodels/AlbumViewModel.kt b/app/src/main/java/org/lineageos/twelve/viewmodels/AlbumViewModel.kt index 7e28fbc1..18010e6f 100644 --- a/app/src/main/java/org/lineageos/twelve/viewmodels/AlbumViewModel.kt +++ b/app/src/main/java/org/lineageos/twelve/viewmodels/AlbumViewModel.kt @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.stateIn import org.lineageos.twelve.models.Audio import org.lineageos.twelve.models.RequestStatus import org.lineageos.twelve.models.UniqueItem +import org.lineageos.twelve.utils.MimeUtils import kotlin.reflect.safeCast class AlbumViewModel(application: Application) : TwelveViewModel(application) { @@ -108,6 +109,33 @@ class AlbumViewModel(application: Application) : TwelveViewModel(application) { listOf() ) + @OptIn(ExperimentalCoroutinesApi::class) + val albumFileTypes = album + .filterNotNull() + .mapLatest { + when (it) { + is RequestStatus.Loading -> null + + is RequestStatus.Success -> { + it.data.second + .map { audio -> audio.mimeType } + .distinct() + .takeIf { mimeTypes -> mimeTypes.size <= 2 } + ?.mapNotNull { mimeType -> MimeUtils.mimeTypeToDisplayName(mimeType) } + .orEmpty() + } + + is RequestStatus.Error -> listOf() + } + } + .filterNotNull() + .flowOn(Dispatchers.IO) + .stateIn( + viewModelScope, + SharingStarted.WhileSubscribed(), + listOf() + ) + fun loadAlbum(albumUri: Uri) { this.albumUri.value = albumUri } diff --git a/app/src/main/res/layout/album_labels.xml b/app/src/main/res/layout/album_labels.xml index 18aa3de9..d5885758 100644 --- a/app/src/main/res/layout/album_labels.xml +++ b/app/src/main/res/layout/album_labels.xml @@ -18,38 +18,79 @@ android:textAppearance="?attr/textAppearanceHeadlineMedium" tools:text="Sweet Revenge" /> - + android:orientation="horizontal"> - + - + + + + + + + + + + + + + + +