Skip to content

Commit

Permalink
feat: updated options menu in audio and video preview
Browse files Browse the repository at this point in the history
  • Loading branch information
joragua committed Oct 2, 2024
1 parent 339ef62 commit 3deecbd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<List<FileMenuOption>> = MutableStateFlow(emptyList())
val menuOptions: StateFlow<List<FileMenuOption>> = _menuOptions

private val currentFile: Flow<OCFile?> = getFileByIdAsStreamUseCase(GetFileByIdAsStreamUseCase.Params(ocFile.id!!))

fun getCurrentFile(): Flow<OCFile?> = 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<List<FileMenuOption>> = MutableStateFlow(emptyList())
val menuOptions: StateFlow<List<FileMenuOption>> = _menuOptions

private val currentFile: Flow<OCFile?> = getFileByIdAsStreamUseCase(GetFileByIdAsStreamUseCase.Params(ocFile.id!!))

fun getCurrentFile(): Flow<OCFile?> = 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -86,7 +87,10 @@ class PreviewAudioFragment : FileFragment() {
private var mediaServiceConnection: MediaServiceConnection? = null
private var autoplay = true

private val previewAudioViewModel by viewModel<PreviewAudioViewModel>()
private val previewAudioViewModel by viewModel<PreviewAudioViewModel>() {
parametersOf(requireArguments().getParcelable(EXTRA_FILE))
}

private val fileOperationsViewModel: FileOperationsViewModel by inject()

/**
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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" }
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3deecbd

Please sign in to comment.