Skip to content

Commit

Permalink
Merge pull request #571 from Infomaniak/refactor-fileInfoActionView
Browse files Browse the repository at this point in the history
add currentFile to the interface
  • Loading branch information
sirambd authored Feb 22, 2022
2 parents b7fcc32 + 149ed7b commit 53a3eca
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ import kotlinx.coroutines.withContext

class FileInfoActionsBottomSheetDialog : BottomSheetDialogFragment(), FileInfoActionsView.OnItemClickListener {

private lateinit var currentFile: File
private lateinit var drivePermissions: DrivePermissions
private val mainViewModel: MainViewModel by activityViewModels()
private val navigationArgs: FileInfoActionsBottomSheetDialogArgs by navArgs()

override lateinit var currentFile: File
override val ownerFragment = this

private val selectFolderResultLauncher = registerForActivityResult(StartActivityForResult()) {
Expand Down Expand Up @@ -136,9 +137,9 @@ class FileInfoActionsBottomSheetDialog : BottomSheetDialogFragment(), FileInfoAc
fileInfoActionsView.removeOfflineObservations(this)
}

override fun editDocumentClicked(currentFile: File) {
override fun editDocumentClicked() {
findNavController().popBackStack()
super.editDocumentClicked(currentFile)
super.editDocumentClicked()
}

override fun displayInfoClicked() {
Expand Down Expand Up @@ -215,8 +216,8 @@ class FileInfoActionsBottomSheetDialog : BottomSheetDialogFragment(), FileInfoAc
}
}

override fun addFavoritesClicked(currentFile: File) {
super.addFavoritesClicked(currentFile)
override fun addFavoritesClicked() {
super.addFavoritesClicked()
currentFile.apply {
val observer: Observer<ApiResponse<Boolean>> = Observer { apiResponse ->
if (apiResponse.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
private val previewSliderViewModel: PreviewSliderViewModel by navGraphViewModels(R.id.previewSliderFragment)

private lateinit var bottomSheetBehavior: BottomSheetBehavior<View>
private lateinit var currentPreviewFile: File
private lateinit var drivePermissions: DrivePermissions
private lateinit var previewSliderAdapter: PreviewSliderAdapter
private lateinit var userDrive: UserDrive

private var hideActions: Boolean = false
private var showUi = false

override val ownerFragment = this
override lateinit var currentFile: File

private val selectFolderResultLauncher = registerForActivityResult(StartActivityForResult()) {
it.whenResultIsOk { data -> onSelectFolderResult(data) }
Expand All @@ -92,15 +91,15 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
userDrive = UserDrive(driveId = driveId, sharedWithMe = isSharedWithMe)
hideActions = arguments?.getBoolean(PREVIEW_HIDE_ACTIONS, false) ?: false

currentPreviewFile = fileId?.let {
currentFile = fileId?.let {
FileController.getFileById(it, userDrive) ?: mainViewModel.currentPreviewFileList[it]
} ?: throw Exception("No current preview found")

previewSliderViewModel.currentPreview = currentPreviewFile
previewSliderViewModel.currentPreview = currentFile
previewSliderViewModel.userDrive = userDrive

} else {
previewSliderViewModel.currentPreview?.let { currentPreviewFile = it }
previewSliderViewModel.currentPreview?.let { currentFile = it }
userDrive = previewSliderViewModel.userDrive
}

Expand All @@ -125,7 +124,7 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
selectFolderResultLauncher = selectFolderResultLauncher,
isSharedWithMe = userDrive.sharedWithMe,
)
updateCurrentFile(currentPreviewFile)
updateCurrentFile(currentFile)
setOnTouchListener { _, _ -> true }
}

Expand All @@ -137,29 +136,30 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
childFragmentManager.findFragmentByTag("f${previewSliderAdapter.getItemId(position)}")?.trackScreen()
currentPreviewFile = previewSliderAdapter.getFile(position)
editButton.isVisible = currentPreviewFile.isOnlyOfficePreview()
openWithButton.isGone = currentPreviewFile.isOnlyOfficePreview()

currentFile = previewSliderAdapter.getFile(position)
editButton.isVisible = currentFile.isOnlyOfficePreview()
openWithButton.isGone = currentFile.isOnlyOfficePreview()
bottomSheetFileInfos.openWith.isVisible = true
lifecycleScope.launchWhenResumed {
withContext(Dispatchers.Main) { bottomSheetFileInfos.updateCurrentFile(currentPreviewFile) }
withContext(Dispatchers.Main) { bottomSheetFileInfos.updateCurrentFile(currentFile) }
}
}
})
}

previewSliderViewModel.pdfIsDownloading.observe(viewLifecycleOwner) { isDownloading ->
if (!currentPreviewFile.isOnlyOfficePreview()) openWithButton.isGone = isDownloading
if (!currentFile.isOnlyOfficePreview()) openWithButton.isGone = isDownloading
bottomSheetFileInfos.openWith.isGone = isDownloading
}

editButton.setOnClickListener { openOnlyOfficeDocument(currentPreviewFile) }
editButton.setOnClickListener { openOnlyOfficeDocument(currentFile) }
openWithButton.setOnClickListener { openWithClicked() }
backButton.setOnClickListener { findNavController().popBackStack() }

mainViewModel.currentPreviewFileList.let { files ->
previewSliderAdapter.setFiles(ArrayList(files.values))
val position = previewSliderAdapter.getPosition(currentPreviewFile)
val position = previewSliderAdapter.getPosition(currentFile)
viewPager.setCurrentItem(position, false)
}

Expand All @@ -168,11 +168,11 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene

private fun setBackActionHandlers() {
getBackNavigationResult<Int>(DownloadProgressDialog.OPEN_WITH) {
context?.openWith(currentPreviewFile, userDrive)
context?.openWith(currentFile, userDrive)
}

getBackNavigationResult<Any>(SelectCategoriesFragment.SELECT_CATEGORIES_NAV_KEY) {
bottomSheetFileInfos.refreshBottomSheetUi(currentPreviewFile)
bottomSheetFileInfos.refreshBottomSheetUi(currentFile)
}
}

Expand Down Expand Up @@ -231,7 +231,7 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene

override fun onPause() {
super.onPause()
previewSliderViewModel.currentPreview = currentPreviewFile
previewSliderViewModel.currentPreview = currentFile
bottomSheetFileInfos.removeOfflineObservations(this)
}

Expand All @@ -244,12 +244,12 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
}

override fun onSaveInstanceState(outState: Bundle) {
if (this::currentPreviewFile.isInitialized) outState.putInt(PREVIEW_FILE_ID_TAG, currentPreviewFile.id)
if (this::currentFile.isInitialized) outState.putInt(PREVIEW_FILE_ID_TAG, currentFile.id)
super.onSaveInstanceState(outState)
}

override fun displayInfoClicked() {
currentPreviewFile.apply {
currentFile.apply {
safeNavigate(
PreviewSliderFragmentDirections.actionPreviewSliderFragmentToFileDetailsFragment(
fileId = id,
Expand All @@ -260,12 +260,12 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
}

override fun fileRightsClicked() {
safeNavigate(PreviewSliderFragmentDirections.actionPreviewSliderFragmentToFileShareDetailsFragment(currentPreviewFile.id))
safeNavigate(PreviewSliderFragmentDirections.actionPreviewSliderFragmentToFileShareDetailsFragment(currentFile.id))
}

override fun copyPublicLink() {
bottomSheetFileInfos.createPublicCopyLink(onSuccess = { file ->
previewSliderAdapter.updateFile(currentPreviewFile.id) { it.shareLink = file?.shareLink }
previewSliderAdapter.updateFile(currentFile.id) { it.shareLink = file?.shareLink }
requireActivity().showSnackbar(title = R.string.fileInfoLinkCopiedToClipboard)
toggleBottomSheet(true)
}, onError = { translatedError ->
Expand All @@ -274,9 +274,9 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
})
}

override fun addFavoritesClicked(currentFile: File) {
override fun addFavoritesClicked() {
super.addFavoritesClicked()
currentFile.apply {
super.addFavoritesClicked(this)
val observer: Observer<ApiResponse<Boolean>> = Observer { apiResponse ->
if (apiResponse.isSuccess()) {
isFavorite = !isFavorite
Expand Down Expand Up @@ -312,26 +312,26 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene

override fun removeOfflineFile(offlineLocalPath: java.io.File, cacheFile: java.io.File) {
lifecycleScope.launch {
mainViewModel.removeOfflineFile(currentPreviewFile, offlineLocalPath, cacheFile, userDrive)
previewSliderAdapter.updateFile(currentPreviewFile.id) { file -> file.isOffline = false }
mainViewModel.removeOfflineFile(currentFile, offlineLocalPath, cacheFile, userDrive)
previewSliderAdapter.updateFile(currentFile.id) { file -> file.isOffline = false }

withContext(Dispatchers.Main) {
currentPreviewFile.isOffline = false
bottomSheetFileInfos.refreshBottomSheetUi(currentPreviewFile)
currentFile.isOffline = false
bottomSheetFileInfos.refreshBottomSheetUi(currentFile)
}
}
}

override fun onLeaveShare(onApiResponse: () -> Unit) {
mainViewModel.deleteFile(currentPreviewFile).observe(viewLifecycleOwner) { apiResponse ->
mainViewModel.deleteFile(currentFile).observe(viewLifecycleOwner) { apiResponse ->
onApiResponse()
if (apiResponse.isSuccess()) {
if (previewSliderAdapter.deleteFile(currentPreviewFile)) {
if (previewSliderAdapter.deleteFile(currentFile)) {
findNavController().popBackStack()
} else {
toggleBottomSheet(true)
}
mainViewModel.currentPreviewFileList.remove(currentPreviewFile.id)
mainViewModel.currentPreviewFileList.remove(currentFile.id)
requireActivity().showSnackbar(R.string.snackbarLeaveShareConfirmation)
} else {
requireActivity().showSnackbar(apiResponse.translatedError)
Expand All @@ -357,12 +357,12 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene

override fun onDuplicateFile(result: String, onApiResponse: () -> Unit) {
val folderId = mainViewModel.currentFolder.value?.id
mainViewModel.duplicateFile(currentPreviewFile, folderId, result).observe(viewLifecycleOwner) { apiResponse ->
mainViewModel.duplicateFile(currentFile, folderId, result).observe(viewLifecycleOwner) { apiResponse ->
if (apiResponse.isSuccess()) {
apiResponse.data?.let { file ->
mainViewModel.currentPreviewFileList[file.id] = file
previewSliderAdapter.addFile(file)
requireActivity().showSnackbar(getString(R.string.allFileDuplicate, currentPreviewFile.name))
requireActivity().showSnackbar(getString(R.string.allFileDuplicate, currentFile.name))
toggleBottomSheet(true)
}
} else {
Expand All @@ -377,7 +377,7 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
bottomSheetFileInfos.onRenameFile(mainViewModel, newName,
onSuccess = {
toggleBottomSheet(true)
requireActivity().showSnackbar(getString(R.string.allFileRename, currentPreviewFile.name))
requireActivity().showSnackbar(getString(R.string.allFileRename, currentFile.name))
onApiResponse()
}, onError = { translatedError ->
toggleBottomSheet(true)
Expand All @@ -387,19 +387,19 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
}

override fun onDeleteFile(onApiResponse: () -> Unit) {
mainViewModel.deleteFile(currentPreviewFile).observe(viewLifecycleOwner) { apiResponse ->
mainViewModel.deleteFile(currentFile).observe(viewLifecycleOwner) { apiResponse ->
onApiResponse()
if (apiResponse.isSuccess()) {
mainViewModel.currentPreviewFileList.remove(currentPreviewFile.id)
if (previewSliderAdapter.deleteFile(currentPreviewFile)) {
mainViewModel.currentPreviewFileList.remove(currentFile.id)
if (previewSliderAdapter.deleteFile(currentFile)) {
findNavController().popBackStack()
} else {
toggleBottomSheet(true)
}
val title = resources.getQuantityString(
R.plurals.snackbarMoveTrashConfirmation,
1,
currentPreviewFile.name
currentFile.name
)
requireActivity().showSnackbar(title)
mainViewModel.deleteFileFromHome.value = true
Expand All @@ -412,28 +412,28 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
override fun openWithClicked() {
super.openWithClicked()
val packageManager = requireContext().packageManager
if (requireContext().openWithIntent(currentPreviewFile, userDrive).resolveActivity(packageManager) == null) {
if (requireContext().openWithIntent(currentFile, userDrive).resolveActivity(packageManager) == null) {
requireActivity().showSnackbar(R.string.allActivityNotFoundError)
} else {
safeNavigate(
PreviewSliderFragmentDirections.actionPreviewSliderFragmentToDownloadProgressDialog(
fileId = currentPreviewFile.id,
fileName = currentPreviewFile.name,
fileId = currentFile.id,
fileName = currentFile.name,
userDrive = userDrive
)
)
}
}

override fun onMoveFile(destinationFolder: File) {
mainViewModel.moveFile(currentPreviewFile, destinationFolder)
mainViewModel.moveFile(currentFile, destinationFolder)
.observe(viewLifecycleOwner) { apiResponse ->
if (apiResponse.isSuccess()) {
mainViewModel.refreshActivities.value = true
requireActivity().showSnackbar(
getString(
R.string.allFileMove,
currentPreviewFile.name,
currentFile.name,
destinationFolder.name
)
)
Expand Down
Loading

0 comments on commit 53a3eca

Please sign in to comment.