Skip to content

Commit

Permalink
Merge pull request #421 from Infomaniak/fix-remove-from-favorite-frag…
Browse files Browse the repository at this point in the history
…ment

Fix remove from favorite fragment
  • Loading branch information
sirambd authored Jan 7, 2022
2 parents 48d86a8 + 7e88bf3 commit b63aa8a
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,12 @@ object FileController {
parentId: Int,
realm: Realm,
order: File.SortType?,
withVisibilitySort: Boolean = true
withVisibilitySort: Boolean = true,
isFavorite: Boolean = false
): RealmResults<File> {
realm.refresh()
val realmLiveSortedFiles = getRealmLiveSortedFiles(getFileById(realm, parentId), order, withVisibilitySort)
return realmLiveSortedFiles ?: emptyList(realm)
return getRealmLiveSortedFiles(getFileById(realm, parentId), order, withVisibilitySort, isFavorite = isFavorite)
?: emptyList(realm)
}

fun getFilesFromCacheOrDownload(
Expand Down Expand Up @@ -692,13 +693,15 @@ object FileController {
localFolder: File?,
order: File.SortType?,
withVisibilitySort: Boolean = true,
localChildren: RealmResults<File>? = null
localChildren: RealmResults<File>? = null,
isFavorite: Boolean = false
): RealmResults<File>? {
val children = localChildren ?: localFolder?.children
return children?.where()
?.apply {
order?.let {
getSortQueryByOrder(it)
if (isFavorite) equalTo(File::isFavorite.name, true)
if (withVisibilitySort) sort(File::visibility.name, Sort.DESCENDING)
sort(File::type.name, Sort.ASCENDING)
distinct(File::id.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ class FavoritesFragment : FileListFragment() {
super.onViewCreated(view, savedInstanceState)

collapsingToolbarLayout.title = getString(R.string.favoritesTitle)
fileAdapter.onFileClicked = { file ->
if (file.isFolder()) {
fileListViewModel.cancelDownloadFiles()
safeNavigate(FavoritesFragmentDirections.actionFavoritesFragmentSelf(file.id, file.name))
} else {
val fileList = fileAdapter.getFileObjectsList(mainViewModel.realm)
Utils.displayFile(mainViewModel, findNavController(), file, fileList)

fileAdapter.apply {
onEmptyList = { changeNoFilesLayoutVisibility(hideFileList = true, changeControlsVisibility = false) }

onFileClicked = { file ->
if (file.isFolder()) {
fileListViewModel.cancelDownloadFiles()
safeNavigate(FavoritesFragmentDirections.actionFavoritesFragmentSelf(file.id, file.name))
} else {
val fileList = getFileObjectsList(mainViewModel.realm)
Utils.displayFile(mainViewModel, findNavController(), file, fileList)
}
}
}

}

private inner class SetNoFilesLayout : () -> Unit {
Expand All @@ -70,6 +76,7 @@ class FavoritesFragment : FileListFragment() {
it?.let { result ->
if (fileAdapter.itemCount == 0 || result.page == 1 || isNewSort) {
val realmFiles = FileController.getRealmLiveFiles(
isFavorite = true,
order = fileListViewModel.sortType,
parentId = FileController.FAVORITES_FILE_ID,
realm = mainViewModel.realm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ open class FileAdapter(

var itemsSelected: OrderedRealmCollection<File> = RealmList()

var onEmptyList: (() -> Unit)? = null
var onFileClicked: ((file: File) -> Unit)? = null
var onMenuClicked: ((selectedFile: File) -> Unit)? = null
var onStopUploadButtonClicked: ((index: Int, fileName: String) -> Unit)? = null
Expand Down Expand Up @@ -80,7 +81,9 @@ open class FileAdapter(
}

override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) {
if (fileList.isEmpty()) {
onEmptyList?.invoke()
} else if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) {
when {
positionStart == 0 -> notifyChanged(0)
positionStart >= fileList.count() -> notifyChanged(fileList.lastIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,33 +483,36 @@ open class FileListFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
noNetwork.isGone = isInternetAvailable
}

fileAdapter = FileAdapter(FileController.emptyList(mainViewModel.realm))
fileAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
fileAdapter.setHasStableIds(true)

fileAdapter.onFileClicked = { file ->
if (file.isUsable()) {
when {
file.isFolder() -> file.openFolder()
file.isBookmark() -> openBookmark(file)
else -> file.displayFile()
fileAdapter = FileAdapter(FileController.emptyList(mainViewModel.realm)).apply {
stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
setHasStableIds(true)

onEmptyList = { checkIfNoFiles() }

onFileClicked = { file ->
if (file.isUsable()) {
when {
file.isFolder() -> file.openFolder()
file.isBookmark() -> openBookmark(file)
else -> file.displayFile()
}
} else {
refreshActivities()
}
} else {
refreshActivities()
}
}

fileAdapter.openMultiSelectMode = { openMultiSelect() }
openMultiSelectMode = { openMultiSelect() }

fileAdapter.updateMultiSelectMode = { onUpdateMultiSelect() }
updateMultiSelectMode = { onUpdateMultiSelect() }

fileAdapter.onMenuClicked = { file ->
val fileObject = file.realm?.copyFromRealm(file, 1) ?: file
val bundle = bundleOf(
"fileId" to fileObject.id,
"userDrive" to UserDrive(driveId = file.driveId, sharedWithMe = fileListViewModel.isSharedWithMe)
)
safeNavigate(R.id.fileInfoActionsBottomSheetDialog, bundle, currentClassName = homeClassName())
onMenuClicked = { file ->
val fileObject = file.realm?.copyFromRealm(file, 1) ?: file
val bundle = bundleOf(
"fileId" to fileObject.id,
"userDrive" to UserDrive(driveId = file.driveId, sharedWithMe = fileListViewModel.isSharedWithMe)
)
safeNavigate(R.id.fileInfoActionsBottomSheetDialog, bundle, currentClassName = homeClassName())
}
}

onBackNavigationResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,19 @@ class SearchFragment : FileListFragment() {
}
})

fileAdapter.onFileClicked = { file ->
if (file.isFolder()) {
fileListViewModel.cancelDownloadFiles()
safeNavigate(
SearchFragmentDirections.actionSearchFragmentToFileListFragment(file.id, file.name)
)
} else {
val fileList = fileAdapter.getFileObjectsList(null)
Utils.displayFile(mainViewModel, findNavController(), file, fileList)
fileAdapter.apply {
onEmptyList = { changeNoFilesLayoutVisibility(hideFileList = true, changeControlsVisibility = false) }

onFileClicked = { file ->
if (file.isFolder()) {
fileListViewModel.cancelDownloadFiles()
safeNavigate(
SearchFragmentDirections.actionSearchFragmentToFileListFragment(file.id, file.name)
)
} else {
val fileList = getFileObjectsList(null)
Utils.displayFile(mainViewModel, findNavController(), file, fileList)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.infomaniak.drive.ui.menu

import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.findNavController
import com.infomaniak.drive.R
import com.infomaniak.drive.data.cache.FileController
Expand All @@ -33,6 +35,12 @@ open class FileSubTypeListFragment : FileListFragment() {
override var enabledMultiSelectMode: Boolean = false
override var hideBackButtonWhenRoot: Boolean = false

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

fileAdapter.onEmptyList = { changeNoFilesLayoutVisibility(hideFileList = true, changeControlsVisibility = true) }
}

protected fun populateFileList(
files: ArrayList<File>,
isComplete: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.content.Context
import android.content.DialogInterface
import android.content.DialogInterface.OnCancelListener
import android.content.DialogInterface.OnDismissListener
import android.content.res.Configuration
import android.text.SpannableStringBuilder
import android.text.format.DateFormat.is24HourFormat
import android.util.AttributeSet
Expand Down

0 comments on commit b63aa8a

Please sign in to comment.