Add song durations
Add song durations to the SongItem layout
This commit is contained in:
parent
dc45e2973a
commit
16038567dd
5 changed files with 51 additions and 5 deletions
|
@ -8,6 +8,10 @@ import android.view.ViewGroup
|
||||||
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.DataBindingUtil
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||||
import org.oxycblt.auxio.recycler.adapters.AlbumAdapter
|
import org.oxycblt.auxio.recycler.adapters.AlbumAdapter
|
||||||
|
@ -19,6 +23,11 @@ class LibraryFragment : Fragment() {
|
||||||
ViewModelProvider(this).get(LibraryViewModel::class.java)
|
ViewModelProvider(this).get(LibraryViewModel::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val inflateJob = Job()
|
||||||
|
private val mainScope = CoroutineScope(
|
||||||
|
inflateJob + Dispatchers.Main
|
||||||
|
)
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
|
@ -28,9 +37,14 @@ class LibraryFragment : Fragment() {
|
||||||
inflater, R.layout.fragment_library, container, false
|
inflater, R.layout.fragment_library, container, false
|
||||||
)
|
)
|
||||||
|
|
||||||
val adapter = AlbumAdapter(libraryModel.albums.value!!)
|
// Offload the initial layout creation to a coroutine so that it doesn't hold up
|
||||||
|
// the UI thread. Hacky but it results in a smoother experience.
|
||||||
|
mainScope.launch {
|
||||||
|
binding.libraryRecycler.adapter = AlbumAdapter(libraryModel.albums.value!!)
|
||||||
|
binding.libraryRecycler.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
|
||||||
binding.libraryRecycler.adapter = adapter
|
// binding.libraryRecycler.adapter = adapter
|
||||||
binding.libraryRecycler.applyDivider()
|
binding.libraryRecycler.applyDivider()
|
||||||
binding.libraryRecycler.setHasFixedSize(true)
|
binding.libraryRecycler.setHasFixedSize(true)
|
||||||
|
|
||||||
|
@ -38,4 +52,10 @@ class LibraryFragment : Fragment() {
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
|
||||||
|
inflateJob.cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ class LoadingViewModel(private val app: Application) : ViewModel() {
|
||||||
|
|
||||||
private val loadingJob = Job()
|
private val loadingJob = Job()
|
||||||
private val ioScope = CoroutineScope(
|
private val ioScope = CoroutineScope(
|
||||||
Dispatchers.IO
|
loadingJob + Dispatchers.IO
|
||||||
)
|
)
|
||||||
|
|
||||||
private val mMusicRepoResponse = MutableLiveData<MusicLoaderResponse>()
|
private val mMusicRepoResponse = MutableLiveData<MusicLoaderResponse>()
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.oxycblt.auxio.music.models
|
package org.oxycblt.auxio.music.models
|
||||||
|
|
||||||
|
import android.text.format.DateUtils
|
||||||
|
|
||||||
// Class containing all relevant values for a song.
|
// Class containing all relevant values for a song.
|
||||||
data class Song(
|
data class Song(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
|
@ -9,4 +11,7 @@ data class Song(
|
||||||
val duration: Long
|
val duration: Long
|
||||||
) {
|
) {
|
||||||
lateinit var album: Album
|
lateinit var album: Album
|
||||||
|
|
||||||
|
val seconds = duration / 1000
|
||||||
|
val formattedDuration = DateUtils.formatElapsedTime(seconds)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,9 @@ import android.view.ViewGroup
|
||||||
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.DataBindingUtil
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
||||||
import org.oxycblt.auxio.recycler.adapters.SongAdapter
|
import org.oxycblt.auxio.recycler.adapters.SongAdapter
|
||||||
|
@ -15,6 +18,11 @@ import org.oxycblt.auxio.recycler.applyDivider
|
||||||
|
|
||||||
class SongsFragment : Fragment() {
|
class SongsFragment : Fragment() {
|
||||||
|
|
||||||
|
private val inflateJob = Job()
|
||||||
|
private val mainScope = CoroutineScope(
|
||||||
|
inflateJob + Dispatchers.Main
|
||||||
|
)
|
||||||
|
|
||||||
private val songsModel: SongsViewModel by lazy {
|
private val songsModel: SongsViewModel by lazy {
|
||||||
ViewModelProvider(this).get(SongsViewModel::class.java)
|
ViewModelProvider(this).get(SongsViewModel::class.java)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
android:textAppearance="?android:attr/textAppearanceListItem"
|
android:textAppearance="?android:attr/textAppearanceListItem"
|
||||||
android:textColor="?android:attr/textColorPrimary"
|
android:textColor="?android:attr/textColorPrimary"
|
||||||
app:layout_constraintBottom_toTopOf="@+id/song_info"
|
app:layout_constraintBottom_toTopOf="@+id/song_info"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toStartOf="@+id/duration"
|
||||||
app:layout_constraintStart_toEndOf="@+id/cover"
|
app:layout_constraintStart_toEndOf="@+id/cover"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:layout_constraintVertical_chainStyle="packed"
|
app:layout_constraintVertical_chainStyle="packed"
|
||||||
|
@ -54,10 +54,23 @@
|
||||||
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
|
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
|
||||||
android:textColor="?android:attr/textColorSecondary"
|
android:textColor="?android:attr/textColorSecondary"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toStartOf="@+id/duration"
|
||||||
app:layout_constraintStart_toEndOf="@+id/cover"
|
app:layout_constraintStart_toEndOf="@+id/cover"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/song_name"
|
app:layout_constraintTop_toBottomOf="@+id/song_name"
|
||||||
tools:text="Artist / Album" />
|
tools:text="Artist / Album" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/duration"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="@{song.formattedDuration}"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
|
||||||
|
android:textColor="?android:attr/textColorTertiary"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
tools:text="16:16" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
</layout>
|
</layout>
|
Loading…
Reference in a new issue