Skip to content

Commit

Permalink
Removed irrelevant SortTypes when not in the right Fragment
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Boulongne <[email protected]>
  • Loading branch information
KevinBoulongne committed Jan 7, 2022
1 parent a6e65cc commit afb856f
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 27 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/infomaniak/drive/data/models/File.kt
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,11 @@ open class File(
RECENT_TRASHED("desc", "deleted_at", R.string.sortRecent),
SMALLER("asc", "files.size", R.string.sortSmaller),
BIGGER("desc", "files.size", R.string.sortBigger),
//EXTENSION("asc", "extension", R.string.sortExtension); // TODO : Awaiting API
// EXTENSION("asc", "extension", R.string.sortExtension); // TODO: Awaiting API
}

enum class SortTypeUsage { FILE_LIST, TRASH }

@Parcelize
enum class FolderPermission(
override val icon: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.infomaniak.drive.data.api.ApiRepository
import com.infomaniak.drive.data.api.ErrorCode.Companion.translateError
import com.infomaniak.drive.data.cache.FileController
import com.infomaniak.drive.data.models.*
import com.infomaniak.drive.data.models.File.*
import com.infomaniak.drive.data.services.DownloadWorker
import com.infomaniak.drive.data.services.MqttClientWrapper
import com.infomaniak.drive.data.services.UploadWorker
Expand Down Expand Up @@ -109,6 +110,7 @@ open class FileListFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
protected open var hideBackButtonWhenRoot: Boolean = true
protected open var showPendingFiles = true
protected open var allowCancellation = true
protected var sortTypeUsage: SortTypeUsage? = null

protected var userDrive: UserDrive? = null

Expand Down Expand Up @@ -467,16 +469,25 @@ open class FileListFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
}
fileListViewModel.isListMode.value = UISettings(requireContext()).listMode

sortButton.setText(fileListViewModel.sortType.translation)
sortButton.setOnClickListener {
safeNavigate(R.id.sortFilesBottomSheetDialog, bundleOf("sortType" to fileListViewModel.sortType))
sortButton.apply {
setText(fileListViewModel.sortType.translation)
setOnClickListener { navigateToSortFilesDialog() }
}

uploadFileInProgress.setUploadFileInProgress(R.string.uploadInThisFolderTitle) {
goToUploadInProgress(folderID)
}
}

private fun navigateToSortFilesDialog() {
safeNavigate(
R.id.sortFilesBottomSheetDialog,
bundleOf("sortType" to fileListViewModel.sortType).apply {
sortTypeUsage?.let { putSerializable("sortTypeUsage", it) }
}
)
}

protected open fun setupFileAdapter() {
mainViewModel.isInternetAvailable.observe(viewLifecycleOwner) { isInternetAvailable ->
fileAdapter.toggleOfflineMode(requireContext(), !isInternetAvailable)
Expand Down Expand Up @@ -786,7 +797,7 @@ open class FileListFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {

private inner class SortFiles : () -> Unit {
override fun invoke() {
getBackNavigationResult<File.SortType>(SORT_TYPE_OPTION_KEY) { newSortType ->
getBackNavigationResult<SortType>(SORT_TYPE_OPTION_KEY) { newSortType ->
fileListViewModel.sortType = newSortType
sortButton?.setText(fileListViewModel.sortType.translation)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,45 @@ import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.File.SortType
import com.infomaniak.drive.data.models.File.SortTypeUsage
import com.infomaniak.lib.core.views.ViewHolder
import kotlinx.android.synthetic.main.item_select_bottom_sheet.view.*

class SortFilesBottomSheetAdapter(
private val selectedSortType: File.SortType,
private val onItemClicked: (sortType: File.SortType) -> Unit
private val selectedType: SortType,
private val usage: SortTypeUsage,
private val onItemClicked: (sortType: SortType) -> Unit
) : RecyclerView.Adapter<ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_select_bottom_sheet, parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
File.SortType.values()[position].let { sortType ->
usage.types()[position].let { sortType ->
holder.itemView.apply {
itemSelectText.setText(sortType.translation)
itemSelectActiveIcon.isVisible = selectedSortType == sortType
itemSelectActiveIcon.isVisible = selectedType == sortType
setOnClickListener { onItemClicked(sortType) }
}
}
}

override fun getItemCount() = File.SortType.values().size
}
override fun getItemCount() = usage.types().size

private fun SortTypeUsage.types(): Array<SortType> {
return when (this) {
SortTypeUsage.FILE_LIST -> SortType.values().fileListTypes()
SortTypeUsage.TRASH -> SortType.values().trashTypes()
}
}

private fun Array<SortType>.fileListTypes(): Array<SortType> {
return filter { it != SortType.OLDER_TRASHED && it != SortType.RECENT_TRASHED }.toTypedArray()
}

private fun Array<SortType>.trashTypes(): Array<SortType> {
return filter { it != SortType.OLDER && it != SortType.RECENT }.toTypedArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.navArgs
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.File.SortType
import com.infomaniak.drive.utils.setBackNavigationResult
import com.infomaniak.drive.views.SelectBottomSheetDialog
import kotlinx.android.synthetic.main.fragment_bottom_sheet_select.*
Expand All @@ -30,17 +30,13 @@ class SortFilesBottomSheetDialog : SelectBottomSheetDialog() {

private val navigationArgs: SortFilesBottomSheetDialogArgs by navArgs()

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

selectTitle.setText(R.string.sortTitle)

selectRecyclerView.adapter = SortFilesBottomSheetAdapter(navigationArgs.sortType) {
onSortTypeClicked(it)
}
selectRecyclerView.adapter = SortFilesBottomSheetAdapter(sortType, sortTypeUsage) { onSortTypeClicked(it) }
}

private fun onSortTypeClicked(sortType: File.SortType) {
private fun onSortTypeClicked(sortType: SortType) {
setBackNavigationResult(FileListFragment.SORT_TYPE_OPTION_KEY, sortType)
}
}
}
11 changes: 7 additions & 4 deletions app/src/main/java/com/infomaniak/drive/ui/menu/TrashFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import androidx.navigation.navGraphViewModels
import com.infomaniak.drive.R
import com.infomaniak.drive.data.api.ErrorCode.Companion.translateError
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.File.SortType
import com.infomaniak.drive.data.models.File.SortTypeUsage
import com.infomaniak.drive.utils.*
import com.infomaniak.drive.utils.Utils.ROOT_ID
import kotlinx.android.synthetic.main.fragment_file_list.*
Expand All @@ -33,7 +35,8 @@ class TrashFragment : FileSubTypeListFragment() {
val trashViewModel: TrashViewModel by navGraphViewModels(R.id.trashFragment)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
fileListViewModel.sortType = File.SortType.RECENT_TRASHED
sortTypeUsage = SortTypeUsage.TRASH
fileListViewModel.sortType = SortType.RECENT_TRASHED
sortFiles = SortFiles()
downloadFiles =
DownloadFiles(
Expand Down Expand Up @@ -98,10 +101,10 @@ class TrashFragment : FileSubTypeListFragment() {

private inner class SortFiles : () -> Unit {
override fun invoke() {
getBackNavigationResult<File.SortType>(SORT_TYPE_OPTION_KEY) { newSortType ->
getBackNavigationResult<SortType>(SORT_TYPE_OPTION_KEY) { newSortType ->
fileListViewModel.sortType = when (newSortType) {
File.SortType.OLDER -> File.SortType.OLDER_TRASHED
File.SortType.RECENT -> File.SortType.RECENT_TRASHED
SortType.OLDER -> SortType.OLDER_TRASHED
SortType.RECENT -> SortType.RECENT_TRASHED
else -> newSortType
}
sortButton.setText(fileListViewModel.sortType.translation)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/item_select_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="?selectableItemBackground">
android:background="?selectableItemBackground"
tools:context=".ui.fileList.SortFilesBottomSheetDialog">

<ImageView
android:id="@+id/itemSelectIcon"
Expand Down Expand Up @@ -61,4 +62,4 @@
app:tint="@color/iconColor"
tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/navigation/main_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@
android:name="sortType"
android:defaultValue="NAME_AZ"
app:argType="com.infomaniak.drive.data.models.File$SortType" />
<argument
android:name="sortTypeUsage"
android:defaultValue="FILE_LIST"
app:argType="com.infomaniak.drive.data.models.File$SortTypeUsage" />
</dialog>

<fragment
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/navigation/select_folder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
android:name="sortType"
android:defaultValue="NAME_AZ"
app:argType="com.infomaniak.drive.data.models.File$SortType" />
<argument
android:name="sortTypeUsage"
android:defaultValue="FILE_LIST"
app:argType="com.infomaniak.drive.data.models.File$SortTypeUsage" />
</dialog>

<dialog
Expand Down

0 comments on commit afb856f

Please sign in to comment.