Skip to content

Commit

Permalink
check for local availability using usecase
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 3c59058 commit 271eb40
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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
Expand Down Expand Up @@ -166,6 +167,7 @@ val useCaseModule = module {
factoryOf(::GetFolderContentAsStreamUseCase)
factoryOf(::GetFolderContentUseCase)
factoryOf(::GetFolderImagesUseCase)
factoryOf(::GetIfFileFolderLocalUseCase)
factoryOf(::GetPersonalRootFolderForAccountUseCase)
factoryOf(::GetSearchFolderContentUseCase)
factoryOf(::GetSharedByLinkForAccountAsStreamUseCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,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 -> {
Expand Down Expand Up @@ -621,6 +620,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()

Expand Down Expand Up @@ -938,6 +953,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> ->
Expand Down Expand Up @@ -1099,11 +1121,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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Up @@ -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 271eb40

Please sign in to comment.