From a25840776d837006c71bb92108206ebdb401cbf1 Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Tue, 17 Nov 2026 16:18:06 +0100 Subject: [PATCH] refactor(file-action): playback Signed-off-by: alperozturk96 --- .../player/ui/audio/AudioPlayerActivity.kt | 56 +++++++++---------- .../player/ui/audio/AudioPlayerScreenEvent.kt | 14 ----- .../player/ui/audio/AudioPlayerViewModel.kt | 55 ++---------------- .../preview/media/PreviewPlaybackFragment.kt | 21 +++---- 4 files changed, 39 insertions(+), 107 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerActivity.kt b/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerActivity.kt index eb9b8e00888c..adb525cc9fb6 100644 --- a/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerActivity.kt +++ b/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerActivity.kt @@ -14,25 +14,28 @@ import android.os.Bundle import androidx.activity.addCallback import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels +import androidx.annotation.IdRes import androidx.core.view.ViewCompat import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import com.nextcloud.client.di.Injectable import com.nextcloud.client.di.ViewModelFactory +import com.nextcloud.ui.fileactions.FileActionHandler +import com.nextcloud.ui.fileactions.FileActionHost import com.nextcloud.ui.fileactions.FileActionsBottomSheet import com.nextcloud.utils.SnackbarUtil import com.owncloud.android.R import com.owncloud.android.datamodel.OCFile import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.activity.FileDisplayActivity -import com.owncloud.android.ui.dialog.ConfirmationDialogFragment -import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment +import com.owncloud.android.ui.fragment.FileFragment import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import javax.inject.Inject class AudioPlayerActivity : FileActivity(), + FileFragment.ContainerActivity, Injectable { companion object { @@ -44,6 +47,9 @@ class AudioPlayerActivity : @Inject lateinit var viewModelFactory: ViewModelFactory + @Inject + lateinit var fileActionHandler: FileActionHandler + private val viewModel by viewModels { viewModelFactory } private lateinit var audioPlayerView: AudioPlayerView @@ -83,35 +89,29 @@ class AudioPlayerActivity : private fun handleEvent(event: AudioPlayerScreenEvent) { when (event) { is AudioPlayerScreenEvent.ShowFileActions -> showFileActions(event.file, event.actionsToHide) - - is AudioPlayerScreenEvent.ShowFileDetails -> showFileDetails(event.file) - is AudioPlayerScreenEvent.ShowFileExportStartedMessage -> showFileExportStartedMessage() - - is AudioPlayerScreenEvent.ShowShareFileDialog -> fileOperationsHelper.sendShareFile(event.file) - - is AudioPlayerScreenEvent.ShowRemoveFileDialog -> showRemoveFileDialog(event.file) - - is AudioPlayerScreenEvent.LaunchOpenFileIntent -> fileOperationsHelper.openFile(event.file) - - is AudioPlayerScreenEvent.LaunchStreamFileIntent -> fileOperationsHelper.streamMediaFile(event.file) - - is AudioPlayerScreenEvent.ToggleFileLock -> fileOperationsHelper.toggleFileLock( - event.file, - event.shouldBeLocked - ) - - is AudioPlayerScreenEvent.AddFileToAlbum -> fileOperationsHelper.addFileToAlbum(listOf(event.file)) } } private fun showFileActions(file: OCFile, actionsToHide: List) { FileActionsBottomSheet.newInstance(file, false, actionsToHide) - .setResultListener(supportFragmentManager, this) { viewModel.onFileActionChosen(file, it) } + .setResultListener(supportFragmentManager, this) { itemId -> onFileActionChosen(file, itemId) } .show(supportFragmentManager, "actions") } - private fun showFileDetails(file: OCFile) { + private fun onFileActionChosen(file: OCFile, @IdRes itemId: Int) { + viewModel.onFileActionChosen(itemId) + + val host = FileActionHost(this, supportFragmentManager, audioPlayerView, null) + fileActionHandler.handle(itemId, listOf(file), host) + } + + private fun showFileExportStartedMessage() { + val message = resources.getQuantityString(R.plurals.export_start, 1, 1) + SnackbarUtil.show(audioPlayerView, message) + } + + override fun showDetails(file: OCFile) { val intent = Intent(this, FileDisplayActivity::class.java).apply { action = FileDisplayActivity.ACTION_DETAILS putExtra(EXTRA_FILE, file) @@ -121,13 +121,9 @@ class AudioPlayerActivity : finish() } - private fun showFileExportStartedMessage() { - val message = resources.getQuantityString(R.plurals.export_start, 1, 1) - SnackbarUtil.show(audioPlayerView, message) - } + override fun showDetails(file: OCFile, activeTab: Int) = showDetails(file) - private fun showRemoveFileDialog(file: OCFile) { - RemoveFilesDialogFragment.newInstance(file) - .show(supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION) - } + override fun onBrowsedDownTo(folder: OCFile) = Unit + + override fun onTransferStateChanged(file: OCFile, downloading: Boolean, uploading: Boolean) = Unit } diff --git a/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerScreenEvent.kt b/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerScreenEvent.kt index d11f95c5fdcd..1fe3d17cbb8d 100644 --- a/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerScreenEvent.kt +++ b/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerScreenEvent.kt @@ -13,19 +13,5 @@ sealed interface AudioPlayerScreenEvent { data class ShowFileActions(val file: OCFile, val actionsToHide: List) : AudioPlayerScreenEvent - data class ShowFileDetails(val file: OCFile) : AudioPlayerScreenEvent - data object ShowFileExportStartedMessage : AudioPlayerScreenEvent - - data class ShowShareFileDialog(val file: OCFile) : AudioPlayerScreenEvent - - data class ShowRemoveFileDialog(val file: OCFile) : AudioPlayerScreenEvent - - data class LaunchOpenFileIntent(val file: OCFile) : AudioPlayerScreenEvent - - data class LaunchStreamFileIntent(val file: OCFile) : AudioPlayerScreenEvent - - data class ToggleFileLock(val file: OCFile, val shouldBeLocked: Boolean) : AudioPlayerScreenEvent - - data class AddFileToAlbum(val file: OCFile) : AudioPlayerScreenEvent } diff --git a/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerViewModel.kt b/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerViewModel.kt index 231ad3123f9f..134e225da07f 100644 --- a/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerViewModel.kt +++ b/app/src/main/java/com/nextcloud/client/player/ui/audio/AudioPlayerViewModel.kt @@ -7,12 +7,10 @@ package com.nextcloud.client.player.ui.audio +import androidx.annotation.IdRes import androidx.core.text.isDigitsOnly import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.nextcloud.client.account.UserAccountManager -import com.nextcloud.client.jobs.BackgroundJobManager -import com.nextcloud.client.jobs.download.FileDownloadHelper import com.nextcloud.client.logger.Logger import com.nextcloud.client.player.media3.PlaybackModel import com.nextcloud.ui.fileactions.FileAction @@ -31,8 +29,6 @@ import kotlin.coroutines.cancellation.CancellationException class AudioPlayerViewModel @Inject constructor( private val playbackModel: PlaybackModel, private val storageManager: FileDataStorageManager, - private val userAccountManager: UserAccountManager, - private val backgroundJobManager: BackgroundJobManager, private val logger: Logger ) : ViewModel() { @@ -47,31 +43,10 @@ class AudioPlayerViewModel @Inject constructor( } } - fun onFileActionChosen(file: OCFile, actionId: Int) { - when (actionId) { - R.id.action_see_details -> eventChannel.trySend(AudioPlayerScreenEvent.ShowFileDetails(file)) - - R.id.action_download_file -> startFileDownloading(file) - - R.id.action_export_file -> startFileExport(file) - - R.id.action_send_share_file -> eventChannel.trySend(AudioPlayerScreenEvent.ShowShareFileDialog(file)) - - R.id.action_remove_file -> eventChannel.trySend(AudioPlayerScreenEvent.ShowRemoveFileDialog(file)) - - R.id.action_open_file_with -> onOpenFileWithClick(file) - - R.id.action_stream_media -> onStreamFileClick(file) - - R.id.action_lock_file -> eventChannel.trySend( - AudioPlayerScreenEvent.ToggleFileLock(file, shouldBeLocked = true) - ) - - R.id.action_unlock_file -> eventChannel.trySend( - AudioPlayerScreenEvent.ToggleFileLock(file, shouldBeLocked = false) - ) - - R.id.action_add_to_album -> eventChannel.trySend(AudioPlayerScreenEvent.AddFileToAlbum(file)) + fun onFileActionChosen(@IdRes itemId: Int) { + when (itemId) { + R.id.action_open_file_with, R.id.action_stream_media -> playbackModel.pause() + R.id.action_export_file -> eventChannel.trySend(AudioPlayerScreenEvent.ShowFileExportStartedMessage) } } @@ -91,24 +66,4 @@ class AudioPlayerViewModel @Inject constructor( null } } - - private fun startFileDownloading(file: OCFile) { - val user = userAccountManager.user - FileDownloadHelper.instance().downloadFileIfNotStartedBefore(user, file) - } - - private fun startFileExport(file: OCFile) { - backgroundJobManager.startImmediateFilesExportJob(listOf(file)) - eventChannel.trySend(AudioPlayerScreenEvent.ShowFileExportStartedMessage) - } - - private fun onOpenFileWithClick(file: OCFile) { - playbackModel.pause() - eventChannel.trySend(AudioPlayerScreenEvent.LaunchOpenFileIntent(file)) - } - - private fun onStreamFileClick(file: OCFile) { - playbackModel.pause() - eventChannel.trySend(AudioPlayerScreenEvent.LaunchStreamFileIntent(file)) - } } diff --git a/app/src/main/java/com/owncloud/android/ui/preview/media/PreviewPlaybackFragment.kt b/app/src/main/java/com/owncloud/android/ui/preview/media/PreviewPlaybackFragment.kt index 2c30fb8b305b..9c6f610d91b0 100644 --- a/app/src/main/java/com/owncloud/android/ui/preview/media/PreviewPlaybackFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/preview/media/PreviewPlaybackFragment.kt @@ -16,6 +16,7 @@ import android.view.View import android.view.ViewGroup import androidx.activity.OnBackPressedCallback import androidx.activity.addCallback +import androidx.annotation.IdRes import androidx.core.content.ContextCompat import androidx.core.os.bundleOf import androidx.core.view.MenuProvider @@ -70,6 +71,7 @@ class PreviewPlaybackFragment : private const val ARGUMENT_AUTOPLAY = "ARGUMENT_AUTOPLAY" private const val SURFACE_ALPHA_VISIBLE = 1f private const val SURFACE_ALPHA_HIDDEN = 0f + private val PLAYBACK_PAUSING_ACTIONS = setOf(R.id.action_stream_media, R.id.action_remove_file) fun newInstance(file: OCFile, collection: PlaybackCollection, autoplay: Boolean = false) = PreviewPlaybackFragment().apply { @@ -218,21 +220,14 @@ class PreviewPlaybackFragment : } } - private fun onFileActionChosen(itemId: Int) { + private fun onFileActionChosen(@IdRes itemId: Int) { val previewActivity = previewActivity ?: return - - when (itemId) { - R.id.action_download_file -> previewActivity.requestForDownload(file) - - else -> { - if (itemId == R.id.action_stream_media || itemId == R.id.action_remove_file) { - playbackModel.pause() - } - - val host = FileActionHost(previewActivity, parentFragmentManager, view, null) - fileActionHandler.handle(itemId, listOf(file), host) - } + if (itemId in PLAYBACK_PAUSING_ACTIONS) { + playbackModel.pause() } + + val host = FileActionHost(previewActivity, parentFragmentManager, view, null) + fileActionHandler.handle(itemId, listOf(file), host) } private fun registerPictureInPictureOnBack() {