Skip to content

Commit 8787ef0

Browse files
committed
Twelve: Album file types
Change-Id: Iecb01cfd3266949a7088354a98201c5101a1a59e
1 parent 7fb6426 commit 8787ef0

File tree

3 files changed

+107
-28
lines changed

3 files changed

+107
-28
lines changed

app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.navigation.ui.setupWithNavController
2626
import androidx.recyclerview.widget.RecyclerView
2727
import coil3.load
2828
import com.google.android.material.appbar.MaterialToolbar
29+
import com.google.android.material.card.MaterialCardView
2930
import com.google.android.material.progressindicator.LinearProgressIndicator
3031
import kotlinx.coroutines.coroutineScope
3132
import kotlinx.coroutines.flow.collectLatest
@@ -54,6 +55,8 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
5455
// Views
5556
private val albumTitleTextView by getViewProperty<TextView>(R.id.albumTitleTextView)
5657
private val artistNameTextView by getViewProperty<TextView>(R.id.artistNameTextView)
58+
private val fileTypeMaterialCardView by getViewProperty<MaterialCardView>(R.id.fileTypeMaterialCardView)
59+
private val fileTypeTextView by getViewProperty<TextView>(R.id.fileTypeTextView)
5760
private val infoNestedScrollView by getViewProperty<NestedScrollView?>(R.id.infoNestedScrollView)
5861
private val linearProgressIndicator by getViewProperty<LinearProgressIndicator>(R.id.linearProgressIndicator)
5962
private val noElementsNestedScrollView by getViewProperty<NestedScrollView>(R.id.noElementsNestedScrollView)
@@ -317,6 +320,13 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
317320
noElementsNestedScrollView.isVisible = isEmpty
318321
}
319322
}
323+
324+
launch {
325+
viewModel.albumFileTypes.collectLatest {
326+
fileTypeMaterialCardView.isVisible = it.isNotEmpty()
327+
fileTypeTextView.text = it.joinToString(" / ")
328+
}
329+
}
320330
}
321331
}
322332

app/src/main/java/org/lineageos/twelve/viewmodels/AlbumViewModel.kt

+28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.stateIn
2020
import org.lineageos.twelve.models.Audio
2121
import org.lineageos.twelve.models.RequestStatus
2222
import org.lineageos.twelve.models.UniqueItem
23+
import org.lineageos.twelve.utils.MimeUtils
2324
import kotlin.reflect.safeCast
2425

2526
class AlbumViewModel(application: Application) : TwelveViewModel(application) {
@@ -108,6 +109,33 @@ class AlbumViewModel(application: Application) : TwelveViewModel(application) {
108109
listOf()
109110
)
110111

112+
@OptIn(ExperimentalCoroutinesApi::class)
113+
val albumFileTypes = album
114+
.filterNotNull()
115+
.mapLatest {
116+
when (it) {
117+
is RequestStatus.Loading -> null
118+
119+
is RequestStatus.Success -> {
120+
it.data.second
121+
.map { audio -> audio.mimeType }
122+
.distinct()
123+
.takeIf { mimeTypes -> mimeTypes.size <= 2 }
124+
?.mapNotNull { mimeType -> MimeUtils.mimeTypeToDisplayName(mimeType) }
125+
.orEmpty()
126+
}
127+
128+
is RequestStatus.Error -> listOf()
129+
}
130+
}
131+
.filterNotNull()
132+
.flowOn(Dispatchers.IO)
133+
.stateIn(
134+
viewModelScope,
135+
SharingStarted.WhileSubscribed(),
136+
listOf()
137+
)
138+
111139
fun loadAlbum(albumUri: Uri) {
112140
this.albumUri.value = albumUri
113141
}

app/src/main/res/layout/album_labels.xml

+69-28
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,79 @@
1818
android:textAppearance="?attr/textAppearanceHeadlineMedium"
1919
tools:text="Sweet Revenge" />
2020

21-
<TextView
22-
android:id="@+id/artistNameTextView"
21+
<LinearLayout
2322
android:layout_width="match_parent"
2423
android:layout_height="wrap_content"
25-
android:drawablePadding="8dp"
2624
android:gravity="center_vertical"
27-
android:paddingVertical="4dp"
28-
android:paddingStart="0dp"
29-
android:paddingEnd="4dp"
30-
android:textAppearance="?attr/textAppearanceLabelLarge"
31-
android:textColor="@color/textview_pressed"
32-
app:backgroundTint="@android:color/transparent"
33-
app:drawableStartCompat="@drawable/ic_person"
34-
app:drawableTint="@color/textview_pressed"
35-
tools:text="Ryuichi Sakamoto" />
25+
android:orientation="horizontal">
3626

37-
<TextView
38-
android:id="@+id/yearTextView"
39-
android:layout_width="match_parent"
40-
android:layout_height="wrap_content"
41-
android:textAppearance="?attr/textAppearanceLabelLarge"
42-
android:textColor="?attr/colorOnSurfaceVariant"
43-
android:visibility="gone"
44-
tools:text="1994"
45-
tools:visibility="visible" />
27+
<LinearLayout
28+
android:layout_width="0dp"
29+
android:layout_height="wrap_content"
30+
android:layout_weight="1"
31+
android:orientation="vertical">
4632

47-
<TextView
48-
android:id="@+id/tracksInfoTextView"
49-
android:layout_width="match_parent"
50-
android:layout_height="wrap_content"
51-
android:textAppearance="?attr/textAppearanceLabelLarge"
52-
android:textColor="?attr/colorOnSurfaceVariant"
53-
tools:text="7 tracks, 51 minutes" />
33+
<TextView
34+
android:id="@+id/artistNameTextView"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:drawablePadding="8dp"
38+
android:gravity="center_vertical"
39+
android:paddingVertical="4dp"
40+
android:paddingStart="0dp"
41+
android:paddingEnd="4dp"
42+
android:textAppearance="?attr/textAppearanceLabelLarge"
43+
android:textColor="@color/textview_pressed"
44+
app:backgroundTint="@android:color/transparent"
45+
app:drawableStartCompat="@drawable/ic_person"
46+
app:drawableTint="@color/textview_pressed"
47+
tools:text="Ryuichi Sakamoto" />
48+
49+
<TextView
50+
android:id="@+id/yearTextView"
51+
android:layout_width="match_parent"
52+
android:layout_height="wrap_content"
53+
android:textAppearance="?attr/textAppearanceLabelLarge"
54+
android:textColor="?attr/colorOnSurfaceVariant"
55+
android:visibility="gone"
56+
tools:text="1994"
57+
tools:visibility="visible" />
58+
59+
<TextView
60+
android:id="@+id/tracksInfoTextView"
61+
android:layout_width="match_parent"
62+
android:layout_height="wrap_content"
63+
android:textAppearance="?attr/textAppearanceLabelLarge"
64+
android:textColor="?attr/colorOnSurfaceVariant"
65+
tools:text="7 tracks, 51 minutes" />
66+
67+
</LinearLayout>
68+
69+
<com.google.android.material.card.MaterialCardView
70+
android:id="@+id/fileTypeMaterialCardView"
71+
style="@style/Widget.Material3.CardView.Filled"
72+
android:layout_width="wrap_content"
73+
android:layout_height="wrap_content"
74+
android:layout_marginStart="16dp"
75+
android:visibility="gone"
76+
app:cardBackgroundColor="?attr/colorPrimaryContainer"
77+
app:cardCornerRadius="4dp"
78+
app:contentPaddingLeft="4dp"
79+
app:contentPaddingRight="4dp"
80+
tools:visibility="visible">
81+
82+
<TextView
83+
android:id="@+id/fileTypeTextView"
84+
android:layout_width="wrap_content"
85+
android:layout_height="24dp"
86+
android:gravity="center"
87+
android:textAlignment="gravity"
88+
android:textAppearance="?attr/textAppearanceLabelMedium"
89+
android:textColor="?attr/colorOnPrimaryContainer"
90+
tools:text="FLAC / MPEG" />
91+
92+
</com.google.android.material.card.MaterialCardView>
93+
94+
</LinearLayout>
5495

5596
</LinearLayout>

0 commit comments

Comments
 (0)