Skip to content

Commit

Permalink
add changelog
Browse files Browse the repository at this point in the history
Signed-off-by: parneet-guraya <[email protected]>
  • Loading branch information
parneet-guraya committed Mar 7, 2024
1 parent 271eb40 commit e756e89
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/4289
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Local-only option in file remove dialog is checks selected file/folder (files in folder recursively) and shown if at least one local file exists.

Now local-only option in remove dialog will check file/folder (files in folder) from selection and will show it if at least one local file (downloaded) exists.

https://github.com/owncloud/android/issues/3936
https://github.com/owncloud/android/pull/4289
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.owncloud.android.domain.utils.Event
import com.owncloud.android.presentation.common.UIResult
import com.owncloud.android.providers.ContextProvider
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -117,6 +118,43 @@ object ViewModelExt : KoinComponent {
}
}

fun <T, Params> ViewModel.runUseCaseWithResult(
coroutineDispatcher: CoroutineDispatcher,
requiresConnection: Boolean = true,
showLoading: Boolean = false,
sharedFlow: MutableSharedFlow<UIResult<T>>,
useCase: BaseUseCaseWithResult<T, Params>,
useCaseParams: Params,
postSuccess: Boolean = true,
postSuccessWithData: Boolean = true
) {
viewModelScope.launch(coroutineDispatcher) {
if (showLoading) {
sharedFlow.emit(UIResult.Loading())
}

// If use case requires connection and is not connected, it is not needed to execute use case
if (requiresConnection and !contextProvider.isConnected()) {
sharedFlow.emit(UIResult.Error(error = NoNetworkConnectionException()))
Timber.w("${useCase.javaClass.simpleName} will not be executed due to lack of network connection")
return@launch
}

val useCaseResult = useCase(useCaseParams)
Timber.d("Use case executed: ${useCase.javaClass.simpleName} with result: $useCaseResult")

if (useCaseResult.isSuccess && postSuccess) {
if (postSuccessWithData) {
sharedFlow.emit(UIResult.Success(useCaseResult.getDataOrNull()))
} else {
sharedFlow.emit(UIResult.Success())
}
} else if (useCaseResult.isError) {
sharedFlow.emit(UIResult.Error(error = useCaseResult.getThrowableOrNull()))
}
}
}

fun <T, U, Params> ViewModel.runUseCaseWithResultAndUseCachedData(
coroutineDispatcher: CoroutineDispatcher,
requiresConnection: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class MainFileListFragment : Fragment(),

private var menu: Menu? = null
private var checkedFiles: List<OCFile> = emptyList()
private var filesToRemove: List<OCFile> = emptyList()
private var fileSingleFile: OCFile? = null
private var fileOptionsBottomSheetSingleFileLayout: LinearLayout? = null
private var succeededTransfers: List<OCTransfer>? = null
Expand Down Expand Up @@ -474,7 +475,8 @@ class MainFileListFragment : Fragment(),
}

FileMenuOption.REMOVE -> {
fileOperationsViewModel.showRemoveDialog(arrayListOf(file))
filesToRemove = listOf(file)
fileOperationsViewModel.showRemoveDialog(filesToRemove)
}

FileMenuOption.OPEN_WITH -> {
Expand Down Expand Up @@ -620,21 +622,21 @@ class MainFileListFragment : Fragment(),
}
}

fileOperationsViewModel.checkIfFileLocalLiveData.observe(viewLifecycleOwner, Event.EventObserver {
collectLatestLifecycleFlow(fileOperationsViewModel.checkIfFileLocalSharedFlow) {
val fileActivity = (requireActivity() as FileActivity)
when (it) {
is UIResult.Loading -> fileActivity.showLoadingDialog(R.string.common_loading)
is UIResult.Success -> {
fileActivity.dismissLoadingDialog()
it.data?.let { result -> onShowRemoveDialog(result.first as ArrayList<OCFile>, result.second) }
it.data?.let { result -> onShowRemoveDialog(filesToRemove, result) }
}

is UIResult.Error -> {
fileActivity.dismissLoadingDialog()
showMessageInSnackbar(resources.getString(R.string.common_error_unknown))
}
}
})
}

/* TransfersViewModel observables */
observeTransfers()
Expand Down Expand Up @@ -953,8 +955,8 @@ class MainFileListFragment : Fragment(),
}
}

private fun onShowRemoveDialog(filesToRemove: ArrayList<OCFile>, isLocal: Boolean) {
val dialog = RemoveFilesDialogFragment.newInstance(filesToRemove, isLocal)
private fun onShowRemoveDialog(filesToRemove: List<OCFile>, isLocal: Boolean) {
val dialog = RemoveFilesDialogFragment.newInstance(ArrayList(filesToRemove), isLocal)
dialog.show(requireActivity().supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION)
fileListAdapter.clearSelection()
updateActionModeAfterTogglingSelected()
Expand Down Expand Up @@ -1121,7 +1123,8 @@ class MainFileListFragment : Fragment(),
}

R.id.action_remove_file -> {
fileOperationsViewModel.showRemoveDialog(checkedFiles)
filesToRemove = checkedFiles
fileOperationsViewModel.showRemoveDialog(filesToRemove)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ import com.owncloud.android.providers.CoroutinesDispatcherProvider
import com.owncloud.android.ui.dialog.FileAlreadyExistsDialog
import com.owncloud.android.usecases.synchronization.SynchronizeFileUseCase
import com.owncloud.android.usecases.synchronization.SynchronizeFolderUseCase
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import timber.log.Timber
Expand Down Expand Up @@ -104,8 +106,8 @@ class FileOperationsViewModel(
private val _deepLinkFlow = MutableStateFlow<Event<UIResult<OCFile?>>?>(null)
val deepLinkFlow: StateFlow<Event<UIResult<OCFile?>>?> = _deepLinkFlow

private val _checkIfFileLocalLiveData = MediatorLiveData<Event<UIResult<Pair<List<OCFile>, Boolean>>>>()
val checkIfFileLocalLiveData: LiveData<Event<UIResult<Pair<List<OCFile>, Boolean>>>> = _checkIfFileLocalLiveData
private val _checkIfFileLocalSharedFlow = MutableSharedFlow<UIResult<Boolean>>()
val checkIfFileLocalSharedFlow: SharedFlow<UIResult<Boolean>> = _checkIfFileLocalSharedFlow

val openDialogs = mutableListOf<FileAlreadyExistsDialog>()

Expand All @@ -132,7 +134,7 @@ class FileOperationsViewModel(
runUseCaseWithResult(
coroutineDispatcher = coroutinesDispatcherProvider.io,
showLoading = true,
liveData = _checkIfFileLocalLiveData,
sharedFlow = _checkIfFileLocalSharedFlow,
useCase = getIfFileFolderLocalUseCase,
useCaseParams = GetIfFileFolderLocalUseCase.Params(filesToRemove),
requiresConnection = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ import com.owncloud.android.domain.files.FileRepository
import com.owncloud.android.domain.files.model.OCFile

class GetIfFileFolderLocalUseCase(private val fileRepository: FileRepository) :
BaseUseCaseWithResult<Pair<List<OCFile>, Boolean>, GetIfFileFolderLocalUseCase.Params>() {
BaseUseCaseWithResult<Boolean, GetIfFileFolderLocalUseCase.Params>() {

override fun run(params: Params): Pair<List<OCFile>, Boolean> = getIfFileFolderLocal(params.filesToRemove)
private fun getIfFileFolderLocal(filesToRemove: List<OCFile>): Pair<List<OCFile>, Boolean> {
override fun run(params: Params): Boolean = getIfFileFolderLocal(params.filesToRemove)
private fun getIfFileFolderLocal(filesToRemove: List<OCFile>): Boolean {

if (filesToRemove.any { it.isAvailableLocally }) {
return Pair(filesToRemove, true)
return true
} else {
filesToRemove.filter { it.isFolder }.forEach { folder ->
if (getIfFileFolderLocal(fileRepository.getFolderContent(folder.id!!)).second) {
return Pair(filesToRemove, true)
if (getIfFileFolderLocal(fileRepository.getFolderContent(folder.id!!))) {
return true
}
}
}
return Pair(filesToRemove, false)
return false
}

data class Params(val filesToRemove: List<OCFile>)
Expand Down

0 comments on commit e756e89

Please sign in to comment.