Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check for local availability using usecase
Browse files Browse the repository at this point in the history
Signed-off-by: parneet-guraya <[email protected]>
parneet-guraya committed Jan 26, 2024
1 parent a1603be commit 7f3e89c
Showing 5 changed files with 61 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ import com.owncloud.android.domain.files.usecases.GetFileByRemotePathUseCase
import com.owncloud.android.domain.files.usecases.GetFileWithSyncInfoByIdUseCase
import com.owncloud.android.domain.files.usecases.GetFolderContentAsStreamUseCase
import com.owncloud.android.domain.files.usecases.GetFolderContentUseCase
import com.owncloud.android.domain.files.usecases.GetIfFileFolderLocalUseCase
import com.owncloud.android.domain.files.usecases.GetFolderImagesUseCase
import com.owncloud.android.domain.files.usecases.GetPersonalRootFolderForAccountUseCase
import com.owncloud.android.domain.files.usecases.GetSearchFolderContentUseCase
@@ -164,6 +165,7 @@ val useCaseModule = module {
factoryOf(::GetFolderContentAsStreamUseCase)
factoryOf(::GetFolderContentUseCase)
factoryOf(::GetFolderImagesUseCase)
factoryOf(::GetIfFileFolderLocalUseCase)
factoryOf(::GetPersonalRootFolderForAccountUseCase)
factoryOf(::GetSearchFolderContentUseCase)
factoryOf(::GetSharedByLinkForAccountAsStreamUseCase)
Original file line number Diff line number Diff line change
@@ -473,8 +473,7 @@ class MainFileListFragment : Fragment(),
}

FileMenuOption.REMOVE -> {
val dialogRemove = RemoveFilesDialogFragment.newInstance(file, file.isAvailableLocally)
dialogRemove.show(requireActivity().supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION)
fileOperationsViewModel.showRemoveDialog(arrayListOf(file))
}

FileMenuOption.OPEN_WITH -> {
@@ -620,6 +619,22 @@ class MainFileListFragment : Fragment(),
}
}

fileOperationsViewModel.checkIfFileLocalLiveData.observe(viewLifecycleOwner, Event.EventObserver {
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) }
}

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

/* TransfersViewModel observables */
observeTransfers()

@@ -934,6 +949,13 @@ class MainFileListFragment : Fragment(),
}
}

private fun onShowRemoveDialog(filesToRemove: ArrayList<OCFile>, isLocal: Boolean) {
val dialog = RemoveFilesDialogFragment.newInstance(filesToRemove, isLocal)
dialog.show(requireActivity().supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION)
fileListAdapter.clearSelection()
updateActionModeAfterTogglingSelected()
}

override fun onFolderNameSet(newFolderName: String, parentFolder: OCFile) {
fileOperationsViewModel.performOperation(FileOperation.CreateFolder(newFolderName, parentFolder))
fileOperationsViewModel.createFolder.observe(viewLifecycleOwner, Event.EventObserver { uiResult: UIResult<Unit> ->
@@ -1095,11 +1117,7 @@ class MainFileListFragment : Fragment(),
}

R.id.action_remove_file -> {
// to handle using usecase
val dialog = RemoveFilesDialogFragment.newInstance(checkedFiles, false)
dialog.show(requireActivity().supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION)
fileListAdapter.clearSelection()
updateActionModeAfterTogglingSelected()
fileOperationsViewModel.showRemoveDialog(checkedFiles)
return true
}

Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ import com.owncloud.android.domain.exceptions.NoNetworkConnectionException
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.files.usecases.CopyFileUseCase
import com.owncloud.android.domain.files.usecases.CreateFolderAsyncUseCase
import com.owncloud.android.domain.files.usecases.GetIfFileFolderLocalUseCase
import com.owncloud.android.domain.files.usecases.ManageDeepLinkUseCase
import com.owncloud.android.domain.files.usecases.MoveFileUseCase
import com.owncloud.android.domain.files.usecases.RemoveFileUseCase
@@ -68,6 +69,7 @@ class FileOperationsViewModel(
private val unsetFilesAsAvailableOfflineUseCase: UnsetFilesAsAvailableOfflineUseCase,
private val manageDeepLinkUseCase: ManageDeepLinkUseCase,
private val setLastUsageFileUseCase: SetLastUsageFileUseCase,
private val getIfFileFolderLocalUseCase: GetIfFileFolderLocalUseCase,
private val contextProvider: ContextProvider,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
) : ViewModel() {
@@ -102,6 +104,9 @@ 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

val openDialogs = mutableListOf<FileAlreadyExistsDialog>()

// Used to save the last operation folder
@@ -123,6 +128,17 @@ class FileOperationsViewModel(
}
}

fun showRemoveDialog(filesToRemove: List<OCFile>) {
runUseCaseWithResult(
coroutineDispatcher = coroutinesDispatcherProvider.io,
showLoading = true,
liveData = _checkIfFileLocalLiveData,
useCase = getIfFileFolderLocalUseCase,
useCaseParams = GetIfFileFolderLocalUseCase.Params(filesToRemove),
requiresConnection = false
)
}

fun setLastUsageFile(file: OCFile) {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
setLastUsageFileUseCase(
Original file line number Diff line number Diff line change
@@ -77,22 +77,19 @@ class RemoveFilesDialogFragment : ConfirmationDialogFragment(), ConfirmationDial
* @return Dialog ready to show.
*/
@JvmStatic
fun newInstance(files: ArrayList<OCFile>): RemoveFilesDialogFragment {
fun newInstance(files: ArrayList<OCFile>, isLocal: Boolean): RemoveFilesDialogFragment {
val messageStringId: Int
var containsFolder = false
var containsDown = false
var containsAvailableOffline = false
for (file in files) {
if (file.isFolder) {
containsFolder = true
}
if (file.isAvailableLocally) {
containsDown = true
}
if (file.isAvailableOffline) {
containsAvailableOffline = true
}
}

messageStringId = if (files.size == 1) {
// choose message for a single file
val file = files.first()
@@ -109,7 +106,7 @@ class RemoveFilesDialogFragment : ConfirmationDialogFragment(), ConfirmationDial
R.string.confirmation_remove_files_alert
}
}
val localRemoveButton = if (!containsAvailableOffline && (containsFolder || containsDown)) {
val localRemoveButton = if (!containsAvailableOffline && isLocal) {
R.string.confirmation_remove_local
} else {
-1
@@ -139,8 +136,8 @@ class RemoveFilesDialogFragment : ConfirmationDialogFragment(), ConfirmationDial
*/
@JvmStatic
@JvmName("newInstanceForSingleFile")
fun newInstance(file: OCFile): RemoveFilesDialogFragment {
return newInstance(arrayListOf(file))
fun newInstance(file: OCFile, isLocal: Boolean): RemoveFilesDialogFragment {
return newInstance(arrayListOf(file), isLocal)
}
}
}
Original file line number Diff line number Diff line change
@@ -22,21 +22,24 @@ import com.owncloud.android.domain.BaseUseCaseWithResult
import com.owncloud.android.domain.files.FileRepository
import com.owncloud.android.domain.files.model.OCFile

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

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

private fun getIfFileFolderLocal(listOfFiles: List<OCFile>): Boolean {
listOfFiles.forEach { file ->
if (file.isFolder) {
if (getIfFileFolderLocal(fileRepository.getFolderContent(file.id!!))) return true
} else {
if (file.isAvailableLocally) return true
if (filesToRemove.any { it.isAvailableLocally }) {
return Pair(filesToRemove, true)
} else {
filesToRemove.filter { it.isFolder }.forEach { folder ->
if (getIfFileFolderLocal(fileRepository.getFolderContent(folder.id!!)).second) {
return Pair(filesToRemove, true)
}
}
}
return false
return Pair(filesToRemove, false)
}

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

}

0 comments on commit 7f3e89c

Please sign in to comment.