Skip to content

Commit

Permalink
Twelve: Album file types
Browse files Browse the repository at this point in the history
Change-Id: Iecb01cfd3266949a7088354a98201c5101a1a59e
  • Loading branch information
SebaUbuntu committed Nov 3, 2024
1 parent 7fb6426 commit 8787ef0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 28 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,6 +55,8 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
// Views
private val albumTitleTextView by getViewProperty<TextView>(R.id.albumTitleTextView)
private val artistNameTextView by getViewProperty<TextView>(R.id.artistNameTextView)
private val fileTypeMaterialCardView by getViewProperty<MaterialCardView>(R.id.fileTypeMaterialCardView)
private val fileTypeTextView by getViewProperty<TextView>(R.id.fileTypeTextView)
private val infoNestedScrollView by getViewProperty<NestedScrollView?>(R.id.infoNestedScrollView)
private val linearProgressIndicator by getViewProperty<LinearProgressIndicator>(R.id.linearProgressIndicator)
private val noElementsNestedScrollView by getViewProperty<NestedScrollView>(R.id.noElementsNestedScrollView)
Expand Down Expand Up @@ -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(" / ")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
97 changes: 69 additions & 28 deletions app/src/main/res/layout/album_labels.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,79 @@
android:textAppearance="?attr/textAppearanceHeadlineMedium"
tools:text="Sweet Revenge" />

<TextView
android:id="@+id/artistNameTextView"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:paddingVertical="4dp"
android:paddingStart="0dp"
android:paddingEnd="4dp"
android:textAppearance="?attr/textAppearanceLabelLarge"
android:textColor="@color/textview_pressed"
app:backgroundTint="@android:color/transparent"
app:drawableStartCompat="@drawable/ic_person"
app:drawableTint="@color/textview_pressed"
tools:text="Ryuichi Sakamoto" />
android:orientation="horizontal">

<TextView
android:id="@+id/yearTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceLabelLarge"
android:textColor="?attr/colorOnSurfaceVariant"
android:visibility="gone"
tools:text="1994"
tools:visibility="visible" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/tracksInfoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceLabelLarge"
android:textColor="?attr/colorOnSurfaceVariant"
tools:text="7 tracks, 51 minutes" />
<TextView
android:id="@+id/artistNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:paddingVertical="4dp"
android:paddingStart="0dp"
android:paddingEnd="4dp"
android:textAppearance="?attr/textAppearanceLabelLarge"
android:textColor="@color/textview_pressed"
app:backgroundTint="@android:color/transparent"
app:drawableStartCompat="@drawable/ic_person"
app:drawableTint="@color/textview_pressed"
tools:text="Ryuichi Sakamoto" />

<TextView
android:id="@+id/yearTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceLabelLarge"
android:textColor="?attr/colorOnSurfaceVariant"
android:visibility="gone"
tools:text="1994"
tools:visibility="visible" />

<TextView
android:id="@+id/tracksInfoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceLabelLarge"
android:textColor="?attr/colorOnSurfaceVariant"
tools:text="7 tracks, 51 minutes" />

</LinearLayout>

<com.google.android.material.card.MaterialCardView
android:id="@+id/fileTypeMaterialCardView"
style="@style/Widget.Material3.CardView.Filled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:visibility="gone"
app:cardBackgroundColor="?attr/colorPrimaryContainer"
app:cardCornerRadius="4dp"
app:contentPaddingLeft="4dp"
app:contentPaddingRight="4dp"
tools:visibility="visible">

<TextView
android:id="@+id/fileTypeTextView"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:gravity="center"
android:textAlignment="gravity"
android:textAppearance="?attr/textAppearanceLabelMedium"
android:textColor="?attr/colorOnPrimaryContainer"
tools:text="FLAC / MPEG" />

</com.google.android.material.card.MaterialCardView>

</LinearLayout>

</LinearLayout>

0 comments on commit 8787ef0

Please sign in to comment.