0
0
mirror of https://github.com/mediathekview/zapp.git synced 2024-09-20 04:12:14 +02:00

Use same view for all downloads

This commit is contained in:
Christine Coenen 2022-11-10 20:40:05 +01:00
parent d0838f949c
commit 189bd308b2
2 changed files with 9 additions and 135 deletions

View File

@ -1,127 +0,0 @@
package de.christinecoenen.code.zapp.app.personal.adapter
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import de.christinecoenen.code.zapp.R
import de.christinecoenen.code.zapp.databinding.MediathekListFragmentItemBinding
import de.christinecoenen.code.zapp.models.shows.DownloadStatus
import de.christinecoenen.code.zapp.models.shows.MediathekShow
import de.christinecoenen.code.zapp.repositories.MediathekRepository
import de.christinecoenen.code.zapp.utils.system.ColorHelper.themeColor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class DownloadItemViewHolder(
private val binding: MediathekListFragmentItemBinding
) : RecyclerView.ViewHolder(binding.root), KoinComponent {
private val mediathekRepository: MediathekRepository by inject()
private val bgColorDefault = binding.root.context.themeColor(R.attr.backgroundColor)
private val bgColorHighlight by lazy { binding.root.context.themeColor(R.attr.colorSurface) }
private var isRelevantForUserJob: Job? = null
private var downloadProgressJob: Job? = null
private var downloadStatusJob: Job? = null
private var playbackPositionJob: Job? = null
suspend fun setShow(show: MediathekShow) = withContext(Dispatchers.Main) {
binding.root.visibility = View.GONE
isRelevantForUserJob?.cancel()
downloadProgressJob?.cancel()
downloadStatusJob?.cancel()
playbackPositionJob?.cancel()
binding.title.text = show.title
binding.topic.text = show.topic
// fix layout_constraintWidth_max not be applied correctly
binding.topic.requestLayout()
binding.duration.text = show.formattedDuration
binding.channel.text = show.channel
binding.time.text = show.formattedTimestamp
binding.subtitle.isVisible = show.hasSubtitle
binding.subtitleDivider.isVisible = show.hasSubtitle
binding.downloadProgress.isVisible = false
binding.downloadProgressIcon.isVisible = false
binding.downloadStatusIcon.isVisible = false
binding.viewingStatus.isVisible = false
binding.viewingProgress.isVisible = false
binding.root.setBackgroundColor(bgColorDefault)
binding.root.visibility = View.VISIBLE
isRelevantForUserJob = launch { getIsRelevantForUserFlow(show) }
downloadProgressJob = launch { updateDownloadProgressFlow(show) }
downloadStatusJob = launch { updateDownloadStatusFlow(show) }
playbackPositionJob = launch { updatePlaybackPositionPercent(show) }
}
private suspend fun getIsRelevantForUserFlow(show: MediathekShow) {
mediathekRepository
.getIsRelevantForUser(show.apiId)
.collectLatest(::updateIsRelevantForUser)
}
private suspend fun updateDownloadProgressFlow(show: MediathekShow) {
mediathekRepository
.getDownloadProgress(show.apiId)
.collectLatest(::updateDownloadProgress)
}
private suspend fun updateDownloadStatusFlow(show: MediathekShow) {
mediathekRepository
.getDownloadStatus(show.apiId)
.collectLatest(::updateDownloadStatus)
}
private suspend fun updatePlaybackPositionPercent(show: MediathekShow) {
mediathekRepository
.getPlaybackPositionPercent(show.apiId)
.collectLatest(::updatePlaybackPositionPercent)
}
private fun updateIsRelevantForUser(isRelevant: Boolean) {
binding.root.setBackgroundColor(if (isRelevant) bgColorHighlight else bgColorDefault)
}
private fun updateDownloadProgress(progress: Int) {
binding.downloadProgress.progress = progress
}
private fun updateDownloadStatus(status: DownloadStatus) {
binding.downloadStatusIcon.isVisible = status == DownloadStatus.FAILED ||
status == DownloadStatus.COMPLETED
binding.downloadStatusIcon.setImageResource(
when (status) {
DownloadStatus.COMPLETED -> R.drawable.ic_baseline_save_alt_24
DownloadStatus.FAILED -> R.drawable.ic_outline_warning_amber_24
else -> 0
}
)
binding.downloadProgress.isVisible = status == DownloadStatus.QUEUED ||
status == DownloadStatus.DOWNLOADING ||
status == DownloadStatus.PAUSED ||
status == DownloadStatus.ADDED
binding.downloadProgressIcon.isVisible = binding.downloadProgress.isVisible
binding.downloadProgress.isIndeterminate = status != DownloadStatus.DOWNLOADING
}
private fun updatePlaybackPositionPercent(percent: Float) {
binding.viewingStatus.isVisible = percent > 0
binding.viewingProgress.progress = (percent * binding.viewingProgress.max).toInt()
binding.viewingProgress.isVisible = percent > 0
}
}

View File

@ -5,7 +5,8 @@ import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.recyclerview.widget.RecyclerView
import de.christinecoenen.code.zapp.databinding.MediathekListFragmentItemBinding
import de.christinecoenen.code.zapp.app.downloads.ui.list.adapter.DownloadViewHolder
import de.christinecoenen.code.zapp.databinding.DownloadsFragmentListItemBinding
import de.christinecoenen.code.zapp.models.shows.PersistedMediathekShow
import kotlinx.coroutines.launch
@ -13,20 +14,20 @@ import kotlinx.coroutines.launch
class DownloadListAdapter(
private val scope: LifecycleCoroutineScope,
private val listener: Listener? = null
) : RecyclerView.Adapter<DownloadItemViewHolder>() {
) : RecyclerView.Adapter<DownloadViewHolder>() {
private var persistedShows = mutableListOf<PersistedMediathekShow>()
public fun setShows(shows: List<PersistedMediathekShow>) {
fun setShows(shows: List<PersistedMediathekShow>) {
persistedShows = shows.toMutableList()
// TODO: use diff util
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadItemViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = MediathekListFragmentItemBinding.inflate(layoutInflater, parent, false)
val holder = DownloadItemViewHolder(binding)
val binding = DownloadsFragmentListItemBinding.inflate(layoutInflater, parent, false)
val holder = DownloadViewHolder(binding)
binding.root.setOnClickListener {
listener?.onShowClicked(persistedShows[holder.bindingAdapterPosition])
@ -43,9 +44,9 @@ class DownloadListAdapter(
return holder
}
override fun onBindViewHolder(holder: DownloadItemViewHolder, position: Int) {
override fun onBindViewHolder(holder: DownloadViewHolder, position: Int) {
scope.launch {
holder.setShow(persistedShows[holder.bindingAdapterPosition].mediathekShow)
holder.bindItem(persistedShows[holder.bindingAdapterPosition])
}
}