Commit 26e20389 authored by Habib Kazemi's avatar Habib Kazemi Committed by Geoffrey Métais

Refactor SubtitleAdapter events

Move duplicate code to parent fragment and Show toast on long press
Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent 405cd243
......@@ -647,6 +647,8 @@
<string name="subtitle_search_episode_hint">Episode</string>
<string name="subtitle_search_season_hint">Season</string>
<string name="language_to_download">Language</string>
<string name="delete_the_selected">Delete the selected</string>
<string name="download_the_selected">Download the selected</string>
<string name="next">Next</string>
<string name="download">Download</string>
</resources>
......@@ -3,21 +3,14 @@ package org.videolan.vlc.gui.dialogs
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import kotlinx.coroutines.experimental.channels.actor
import org.videolan.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.databinding.SubtitleDownloadFragmentBinding
import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.gui.helpers.UiTools.deleteSubtitleDialog
import org.videolan.vlc.util.AndroidDevices
import org.videolan.vlc.util.VLCDownloadManager
import org.videolan.vlc.viewmodels.SubtitlesModel
class SubtitleDownloadFragment : androidx.fragment.app.Fragment() {
......@@ -25,16 +18,6 @@ class SubtitleDownloadFragment : androidx.fragment.app.Fragment() {
private lateinit var adapter: SubtitlesAdapter
lateinit var mediaPath: String
private val listEventActor = coroutineScope.actor<SubtitleItem> {
for (subtitleItem in channel)
when (subtitleItem.state) {
State.NotDownloaded -> {
VLCDownloadManager.download(context!!, subtitleItem)
}
State.Downloaded -> deleteSubtitleDialog(context,
{ _, _ -> viewModel.deleteSubtitle(subtitleItem.mediaPath, subtitleItem.idSubtitle) }, { _, _ -> })
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -51,7 +34,7 @@ class SubtitleDownloadFragment : androidx.fragment.app.Fragment() {
//Prevent opening soft keyboard automatically
binding.constraintLayout.isFocusableInTouchMode = true
adapter = SubtitlesAdapter(listEventActor)
adapter = SubtitlesAdapter((parentFragment as SubtitleDownloaderDialogFragment).listEventActor)
val recyclerView = binding.subtitleList
recyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(activity, androidx.recyclerview.widget.DividerItemDecoration.VERTICAL))
recyclerView.adapter = adapter
......
......@@ -3,15 +3,25 @@ package org.videolan.vlc.gui.dialogs
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.isActive
import org.videolan.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.databinding.SubtitleDownloaderDialogBinding
import org.videolan.vlc.gui.helpers.UiTools.deleteSubtitleDialog
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.util.VLCDownloadManager
import org.videolan.vlc.viewmodels.SubtitlesModel
private const val MEDIA_PATHS = "MEDIA_PATHS"
const val MEDIA_PATH = "MEDIA_PATH"
......@@ -19,12 +29,44 @@ const val MEDIA_PATH = "MEDIA_PATH"
class SubtitleDownloaderDialogFragment: androidx.fragment.app.DialogFragment() {
private lateinit var adapter: ViewPagerAdapter
lateinit var paths: List<String>
private lateinit var viewModel: SubtitlesModel
private lateinit var toast: Toast
val listEventActor = coroutineScope.actor<SubtitleEvent> {
for (subtitleEvent in channel) if(isActive) when (subtitleEvent) {
is Click -> {
when(subtitleEvent.item.state) {
State.NotDownloaded -> {
VLCDownloadManager.download(context!!, subtitleEvent.item)
}
State.Downloaded -> deleteSubtitleDialog(context,
{ _, _ -> viewModel.deleteSubtitle(subtitleEvent.item.mediaPath, subtitleEvent.item.idSubtitle) }, { _, _ -> })
}
}
is LongClick -> {
@StringRes val message = when(subtitleEvent.item.state) {
State.NotDownloaded -> {R.string.download_the_selected}
State.Downloaded -> {R.string.delete_the_selected}
// Todo else -> {"Cancel download"}
else -> return@actor
}
if (::toast.isInitialized)
toast.cancel()
toast = Toast.makeText(activity, message, Toast.LENGTH_SHORT)
toast.setGravity(Gravity.TOP,0,100)
toast.show()
}
} else channel.close()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
paths = savedInstanceState?.getStringArrayList(MEDIA_PATHS)?.toList() ?: arguments?.getStringArrayList(MEDIA_PATHS)?.toList() ?: listOf()
if (paths.isEmpty()) dismiss()
viewModel = ViewModelProviders.of(requireActivity(), SubtitlesModel.Factory(requireContext(), paths[0])).get(paths[0], SubtitlesModel::class.java)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
......
......@@ -4,16 +4,10 @@ package org.videolan.vlc.gui.dialogs
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.coroutines.experimental.channels.actor
import org.videolan.tools.coroutineScope
import org.videolan.vlc.databinding.SubtitleHistoryFragmentBinding
import org.videolan.vlc.gui.helpers.UiTools.deleteSubtitleDialog
import org.videolan.vlc.viewmodels.SubtitlesModel
class SubtitleHistoryFragment : androidx.fragment.app.Fragment() {
......@@ -21,15 +15,6 @@ class SubtitleHistoryFragment : androidx.fragment.app.Fragment() {
private lateinit var adapter: SubtitlesAdapter
lateinit var mediaPath: String
private val listEventActor = coroutineScope.actor<SubtitleItem> {
for (event in channel)
if (event.state == State.Downloaded) {
deleteSubtitleDialog(context,
{ _, _ -> viewModel.deleteSubtitle(event.mediaPath, event.idSubtitle)
}, { _, _ -> })
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mediaPath = arguments?.getString(MEDIA_PATH, "") ?: ""
......@@ -40,7 +25,7 @@ class SubtitleHistoryFragment : androidx.fragment.app.Fragment() {
super.onCreateView(inflater, container, savedInstanceState)
val binding = SubtitleHistoryFragmentBinding.inflate(inflater, container, false)
adapter = SubtitlesAdapter(listEventActor)
adapter = SubtitlesAdapter((parentFragment as SubtitleDownloaderDialogFragment).listEventActor)
val recyclerView = binding.subtitleList
recyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(activity, androidx.recyclerview.widget.DividerItemDecoration.VERTICAL))
recyclerView.adapter = adapter
......@@ -59,6 +44,4 @@ class SubtitleHistoryFragment : androidx.fragment.app.Fragment() {
return subtitleHistoryFragment
}
}
}
package org.videolan.vlc.gui.dialogs
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.coroutines.experimental.channels.SendChannel
import org.videolan.vlc.api.OpenSubtitle
import org.videolan.vlc.databinding.SubtitleDownloadItemBinding
internal class SubtitlesAdapter(private val eventActor: SendChannel<SubtitleItem>) : androidx.recyclerview.widget.RecyclerView.Adapter<SubtitlesAdapter.ViewHolder>() {
internal class SubtitlesAdapter(private val eventActor: SendChannel<SubtitleEvent>) : androidx.recyclerview.widget.RecyclerView.Adapter<SubtitlesAdapter.ViewHolder>() {
private var dataset: List<SubtitleItem>? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubtitlesAdapter.ViewHolder {
......@@ -29,19 +27,35 @@ internal class SubtitlesAdapter(private val eventActor: SendChannel<SubtitleItem
override fun getItemCount() = dataset?.size ?: 0
inner class ViewHolder(val binding: SubtitleDownloadItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(binding.root), View.OnClickListener {
inner class ViewHolder(val binding: SubtitleDownloadItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(binding.root), View.OnClickListener, View.OnLongClickListener {
init {
itemView.setOnClickListener(this)
itemView.setOnLongClickListener(this)
}
override fun onClick(v: View) {
dataset?.get(layoutPosition)?.let { eventActor.offer(it) }
dataset?.get(layoutPosition)?.let {
if(!eventActor.isClosedForSend)
eventActor.offer(Click(it)) }
}
override fun onLongClick(v: View): Boolean {
dataset?.get(layoutPosition)?.let {
if(!eventActor.isClosedForSend)
eventActor.offer(LongClick(it))
}
return true
}
fun bind(subtitleItem: SubtitleItem?) {
binding.subtitleItem = subtitleItem
binding.executePendingBindings()
}
}
}
sealed class SubtitleEvent
class Click(val item: SubtitleItem) : SubtitleEvent()
class LongClick(val item: SubtitleItem) : SubtitleEvent()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment