From e9471a8f70da240ecdc5da6d282962e7247b83eb Mon Sep 17 00:00:00 2001 From: zechs Date: Wed, 14 Jun 2023 16:23:25 +0530 Subject: [PATCH] `mpv` is default player, long press to change --- .../drive/stream/ui/files/FilesFragment.kt | 28 +++++++++++++++---- .../stream/ui/files/adapter/FilesAdapter.kt | 1 + .../ui/files/adapter/FilesViewHolder.kt | 8 ++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/zechs/drive/stream/ui/files/FilesFragment.kt b/app/src/main/java/zechs/drive/stream/ui/files/FilesFragment.kt index 4567aaf..012d46e 100644 --- a/app/src/main/java/zechs/drive/stream/ui/files/FilesFragment.kt +++ b/app/src/main/java/zechs/drive/stream/ui/files/FilesFragment.kt @@ -117,10 +117,12 @@ class FilesFragment : BaseFragment() { is Resource.Success -> response.data?.let { files -> onSuccess(files) } + is Resource.Error -> { showSnackBar(response.message) showError(response.message) } + is Resource.Loading -> { isLoading = true if (!viewModel.hasLoaded || viewModel.hasFailed) { @@ -197,12 +199,25 @@ class FilesFragment : BaseFragment() { private val filesAdapter by lazy { FilesAdapter( onClickListener = { handleFileOnClick(it) }, + onLongClickListener = { handleFileOnLongPress(it) }, onStarClickListener = { file, isStarred -> viewModel.starFile(file, isStarred) } ) } + private fun handleFileOnLongPress(file: DriveFile) { + Log.d(TAG, file.toString()) + if (file.isVideoFile) { + launchVideoPlayer(file) + } else if (file.isShortcut) { + if (file.isShortcutVideo) { + val videoShortcutFile = file.copy(id = file.shortcutDetails.targetId!!) + launchVideoPlayer(videoShortcutFile) + } + } + } + private fun handleFileOnClick(file: DriveFile) { Log.d(TAG, file.toString()) if (file.isFolder && !file.isShortcut) { @@ -212,7 +227,7 @@ class FilesFragment : BaseFragment() { ) findNavController().navigate(action) } else if (file.isVideoFile) { - launchVideoPlayer(file) + viewModel.fetchToken(file) } else if (file.isShortcut) { if (file.isShortcutFolder) { val action = FilesFragmentDirections.actionFilesFragmentSelf( @@ -220,10 +235,9 @@ class FilesFragment : BaseFragment() { query = "'${file.shortcutDetails.targetId}' in parents and trashed=false" ) findNavController().navigate(action) - } - else if (file.isShortcutVideo){ - val videoShortcutFile = file.copy(id=file.shortcutDetails.targetId!!) - launchVideoPlayer(videoShortcutFile) + } else if (file.isShortcutVideo) { + val videoShortcutFile = file.copy(id = file.shortcutDetails.targetId!!) + viewModel.fetchToken(videoShortcutFile) } } @@ -231,7 +245,7 @@ class FilesFragment : BaseFragment() { private fun launchVideoPlayer(file: DriveFile) { - val items = arrayOf("ExoPlayer", "MPV (Experimental)") + val items = arrayOf("ExoPlayer", "MPV") MaterialAlertDialogBuilder(requireContext()) .setTitle(getString(R.string.play_using)) @@ -251,9 +265,11 @@ class FilesFragment : BaseFragment() { is Resource.Success -> { launchMpv(res.data!!) } + is Resource.Error -> { showSnackBar(res.message!!) } + else -> {} } } diff --git a/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesAdapter.kt b/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesAdapter.kt index 2740ec0..85e12ef 100755 --- a/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesAdapter.kt +++ b/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesAdapter.kt @@ -10,6 +10,7 @@ import zechs.drive.stream.databinding.ItemLoadingBinding class FilesAdapter( val onClickListener: (DriveFile) -> Unit, + val onLongClickListener: (DriveFile) -> Unit, val onStarClickListener: (DriveFile, Boolean) -> Unit ) : ListAdapter(FilesItemDiffCallback()) { diff --git a/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesViewHolder.kt b/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesViewHolder.kt index 3dbdce3..5284c7c 100755 --- a/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesViewHolder.kt +++ b/app/src/main/java/zechs/drive/stream/ui/files/adapter/FilesViewHolder.kt @@ -34,6 +34,7 @@ sealed class FilesViewHolder( } } } + Starred.STARRED -> { itemBinding.apply { btnStar.isInvisible = false @@ -44,12 +45,14 @@ sealed class FilesViewHolder( } } } + Starred.LOADING -> { itemBinding.apply { btnStar.isInvisible = true starLoading.isGone = false } } + Starred.UNKNOWN -> { itemBinding.apply { btnStar.isGone = true @@ -87,6 +90,11 @@ sealed class FilesViewHolder( filesAdapter.onClickListener.invoke(item) } + root.setOnLongClickListener { + filesAdapter.onLongClickListener.invoke(item) + return@setOnLongClickListener true + } + setStarred(item, item.starred) } }