diff --git a/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewAudioViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewAudioViewModel.kt index 968ecc438e1..686b827a193 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewAudioViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewAudioViewModel.kt @@ -25,9 +25,11 @@ import androidx.lifecycle.viewModelScope import com.owncloud.android.R import com.owncloud.android.domain.files.model.FileMenuOption import com.owncloud.android.domain.files.model.OCFile +import com.owncloud.android.domain.files.usecases.GetFileByIdAsStreamUseCase import com.owncloud.android.providers.ContextProvider import com.owncloud.android.providers.CoroutinesDispatcherProvider import com.owncloud.android.usecases.files.FilterFileMenuOptionsUseCase +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.update @@ -37,11 +39,17 @@ class PreviewAudioViewModel( private val filterFileMenuOptionsUseCase: FilterFileMenuOptionsUseCase, private val contextProvider: ContextProvider, private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider, + getFileByIdAsStreamUseCase: GetFileByIdAsStreamUseCase, + ocFile: OCFile ) : ViewModel() { private val _menuOptions: MutableStateFlow> = MutableStateFlow(emptyList()) val menuOptions: StateFlow> = _menuOptions + private val currentFile: Flow = getFileByIdAsStreamUseCase(GetFileByIdAsStreamUseCase.Params(ocFile.id!!)) + + fun getCurrentFile(): Flow = currentFile + fun filterMenuOptions(file: OCFile, accountName: String) { val shareViaLinkAllowed = contextProvider.getBoolean(R.bool.share_via_link_feature) val shareWithUsersAllowed = contextProvider.getBoolean(R.bool.share_with_users_feature) diff --git a/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewVideoViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewVideoViewModel.kt index d332dc02724..9ce93154f08 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewVideoViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/presentation/previews/PreviewVideoViewModel.kt @@ -25,9 +25,11 @@ import androidx.lifecycle.viewModelScope import com.owncloud.android.R import com.owncloud.android.domain.files.model.FileMenuOption import com.owncloud.android.domain.files.model.OCFile +import com.owncloud.android.domain.files.usecases.GetFileByIdAsStreamUseCase import com.owncloud.android.providers.ContextProvider import com.owncloud.android.providers.CoroutinesDispatcherProvider import com.owncloud.android.usecases.files.FilterFileMenuOptionsUseCase +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.update @@ -37,11 +39,17 @@ class PreviewVideoViewModel( private val filterFileMenuOptionsUseCase: FilterFileMenuOptionsUseCase, private val contextProvider: ContextProvider, private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider, + getFileByIdAsStreamUseCase: GetFileByIdAsStreamUseCase, + ocFile: OCFile ) : ViewModel() { private val _menuOptions: MutableStateFlow> = MutableStateFlow(emptyList()) val menuOptions: StateFlow> = _menuOptions + private val currentFile: Flow = getFileByIdAsStreamUseCase(GetFileByIdAsStreamUseCase.Params(ocFile.id!!)) + + fun getCurrentFile(): Flow = currentFile + fun filterMenuOptions(file: OCFile, accountName: String) { val shareViaLinkAllowed = contextProvider.getBoolean(R.bool.share_via_link_feature) val shareWithUsersAllowed = contextProvider.getBoolean(R.bool.share_with_users_feature) diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewAudioFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewAudioFragment.kt index 92547b13e29..8c430023b48 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewAudioFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewAudioFragment.kt @@ -60,6 +60,7 @@ import com.owncloud.android.ui.fragment.FileFragment import com.owncloud.android.utils.PreferenceUtils import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.viewModel +import org.koin.core.parameter.parametersOf import timber.log.Timber /** @@ -86,7 +87,10 @@ class PreviewAudioFragment : FileFragment() { private var mediaServiceConnection: MediaServiceConnection? = null private var autoplay = true - private val previewAudioViewModel by viewModel() + private val previewAudioViewModel by viewModel() { + parametersOf(requireArguments().getParcelable(EXTRA_FILE)) + } + private val fileOperationsViewModel: FileOperationsViewModel by inject() /** @@ -116,6 +120,16 @@ class PreviewAudioFragment : FileFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + collectLatestLifecycleFlow(previewAudioViewModel.getCurrentFile()) { currentFile -> + if (currentFile != null) { + file = currentFile + requireActivity().invalidateOptionsMenu() + } else { + requireActivity().onBackPressed() + } + + } + imagePreview = view.findViewById(R.id.image_preview) mediaController = view.findViewById(R.id.media_controller) } @@ -227,7 +241,7 @@ class PreviewAudioFragment : FileFragment() { super.onPrepareOptionsMenu(menu) val safeFile = file val accountName = account!!.name - previewAudioViewModel.filterMenuOptions(safeFile, accountName) + previewAudioViewModel.filterMenuOptions(safeFile,accountName) collectLatestLifecycleFlow(previewAudioViewModel.menuOptions) { menuOptions -> val hasWritePermission = safeFile.hasWritePermission diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewVideoActivity.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewVideoActivity.kt index 754f643ea6c..1046eb6a6df 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewVideoActivity.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewVideoActivity.kt @@ -37,6 +37,7 @@ import android.view.MenuItem import android.view.View import android.view.Window import androidx.annotation.OptIn +import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog import androidx.core.view.MenuProvider import androidx.core.view.WindowCompat @@ -61,6 +62,7 @@ import com.owncloud.android.R import com.owncloud.android.databinding.VideoPreviewBinding import com.owncloud.android.domain.files.model.OCFile import com.owncloud.android.domain.utils.Event +import com.owncloud.android.extensions.collectLatestLifecycleFlow import com.owncloud.android.extensions.filterMenuOptions import com.owncloud.android.extensions.sendDownloadedFilesByShareSheet import com.owncloud.android.extensions.showErrorInSnackbar @@ -81,8 +83,10 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import org.koin.androidx.viewmodel.ext.android.viewModel +import org.koin.core.parameter.parametersOf import timber.log.Timber +@RequiresApi(Build.VERSION_CODES.TIRAMISU) @OptIn(UnstableApi::class) class PreviewVideoActivity : FileActivity(), Player.Listener, OnPrepareVideoPlayerTaskListener, FileFragment.ContainerActivity, MenuProvider { private var account: Account? = null @@ -93,7 +97,7 @@ class PreviewVideoActivity : FileActivity(), Player.Listener, OnPrepareVideoPlay private var playWhenReady = true private var playbackPosition: Long = 0 private var windowInsetsController: WindowInsetsControllerCompat? = null - private val previewVideoViewModel: PreviewVideoViewModel by viewModel() + private val previewVideoViewModel: PreviewVideoViewModel by viewModel() { parametersOf(intent.getParcelableExtra(EXTRA_FILE, OCFile::class.java)) } private val fileOperationsViewModel: FileOperationsViewModel by viewModel() private val transfersViewModel: TransfersViewModel by viewModel() @@ -128,6 +132,16 @@ class PreviewVideoActivity : FileActivity(), Player.Listener, OnPrepareVideoPlay playbackPosition = savedInstanceState.getLong(EXTRA_PLAY_POSITION, 0) } + collectLatestLifecycleFlow(previewVideoViewModel.getCurrentFile()) { currentFile -> + if (currentFile != null) { + file = currentFile + this.invalidateOptionsMenu() + } else { + this.onBackPressed() + } + + } + checkNotNull(file) { "Instanced with a NULL OCFile" } checkNotNull(account) { "Instanced with a NULL ownCloud Account" } check(file.isVideo) { "Not a video file" } @@ -319,6 +333,8 @@ class PreviewVideoActivity : FileActivity(), Player.Listener, OnPrepareVideoPlay } override fun onPrepareMenu(menu: Menu) { + + val safeFile = file val accountName = account!!.name previewVideoViewModel.filterMenuOptions(safeFile, accountName)