Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Boulongne <[email protected]>
  • Loading branch information
KevinBoulongne committed Feb 22, 2022
1 parent ff925dd commit 714a222
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 83 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/drive/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ class MainViewModel(appContext: Application) : AndroidViewModel(appContext) {
}
}

fun deleteFileFromFavorites(file: File, userDrive: UserDrive? = null, callback: ((File) -> Unit)? = null) =
fun deleteFileFromFavorites(file: File, userDrive: UserDrive? = null, onFileUpdate: ((File) -> Unit)? = null) =
liveData(Dispatchers.IO) {
CoroutineScope(Dispatchers.IO).launch {
FileController.updateFile(file.id, userDrive = userDrive) {
it.isFavorite = false
callback?.invoke(it)
onFileUpdate?.invoke(it)
}
}
emit(ApiRepository.deleteFavoriteFile(file))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class ActionMultiSelectBottomSheetDialog : BottomSheetDialogFragment() {
private val actionMultiSelectModel by viewModels<ActionMultiSelectModel>()
private val navigationArgs: ActionMultiSelectBottomSheetDialogArgs by navArgs()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(R.layout.fragment_bottom_sheet_action_multi_select, container, false)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.fragment_bottom_sheet_action_multi_select, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down Expand Up @@ -98,8 +99,11 @@ class ActionMultiSelectBottomSheetDialog : BottomSheetDialogFragment() {
}

private fun configureDownloadFile() {
val drivePermissions = DrivePermissions()
drivePermissions.registerPermissions(this) { authorized -> if (authorized) downloadFileArchive() }
val drivePermissions = DrivePermissions().apply {
registerPermissions(this@ActionMultiSelectBottomSheetDialog) { authorized ->
if (authorized) downloadFileArchive()
}
}
downloadFile.apply {
setOnClickListener { if (drivePermissions.checkWriteStoragePermission()) downloadFileArchive() }
isVisible = navigationArgs.fileIds.isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class ActionPicturesMultiSelectBottomSheetDialog : BottomSheetDialogFragment() {
private val actionMultiSelectModel by viewModels<ActionMultiSelectModel>()
private val navigationArgs: ActionMultiSelectBottomSheetDialogArgs by navArgs()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(R.layout.fragment_bottom_sheet_action_multi_select, container, false)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.fragment_bottom_sheet_action_multi_select, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down Expand Up @@ -93,8 +94,11 @@ class ActionPicturesMultiSelectBottomSheetDialog : BottomSheetDialogFragment() {
}

private fun configureDownloadFile() {
val drivePermissions = DrivePermissions()
drivePermissions.registerPermissions(this) { authorized -> if (authorized) downloadFileArchive() }
val drivePermissions = DrivePermissions().apply {
registerPermissions(this@ActionPicturesMultiSelectBottomSheetDialog) { authorized ->
if (authorized) downloadFileArchive()
}
}
downloadFile.apply {
setOnClickListener { if (drivePermissions.checkWriteStoragePermission()) downloadFileArchive() }
isVisible = navigationArgs.fileIds.isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,15 @@ class MenuPicturesFragment : Fragment(), MultiSelectParent {
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentMenuPicturesBinding.inflate(inflater, container, false)
binding = FragmentMenuPicturesBinding.inflate(inflater, container, false).apply {
toolbar.setNavigationOnClickListener { findNavController().popBackStack() }

binding.toolbar.setNavigationOnClickListener {
findNavController().popBackStack()
}

binding.apply {
closeButtonMultiSelect.setOnClickListener { picturesFragment.onCloseMultiSelection() }
moveButtonMultiSelect.setOnClickListener { picturesFragment.onMove() }
deleteButtonMultiSelect.setOnClickListener { picturesFragment.onDelete() }
menuButtonMultiSelect.setOnClickListener { picturesFragment.onMenu() }
}

binding.swipeRefreshLayout.setOnRefreshListener {
picturesFragment.onRefreshPictures()
swipeRefreshLayout.setOnRefreshListener { picturesFragment.onRefreshPictures() }
}

return binding.root
Expand Down Expand Up @@ -103,15 +97,15 @@ class MenuPicturesFragment : Fragment(), MultiSelectParent {
menuButtonMultiSelect.isEnabled = false
}

override fun setTitleMultiSelect(title: String) {
binding.titleMultiSelect.text = title
override fun setTitleMultiSelect(title: String) = with(binding) {
titleMultiSelect.text = title
}

override fun disableSwipeRefresh() {
binding.swipeRefreshLayout.isEnabled = false
override fun disableSwipeRefresh() = with(binding) {
swipeRefreshLayout.isEnabled = false
}

override fun enableSwipeRefresh() {
binding.swipeRefreshLayout.isEnabled = true
override fun enableSwipeRefresh() = with(binding) {
swipeRefreshLayout.isEnabled = true
}
}
41 changes: 13 additions & 28 deletions app/src/main/java/com/infomaniak/drive/ui/menu/PicturesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import kotlinx.android.synthetic.main.cardview_picture.view.*
import kotlinx.android.synthetic.main.title_recycler_section.view.*
import java.util.*

class PicturesAdapter(
private val onItemClick: (file: File) -> Unit
) : LoaderAdapter<Any>() {
class PicturesAdapter(private val onItemClick: (file: File) -> Unit) : LoaderAdapter<Any>() {

var itemsSelected: OrderedRealmCollection<File> = RealmList()

Expand Down Expand Up @@ -80,10 +78,10 @@ class PicturesAdapter(
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }

if (newestSectionTitle != month) {
// This case can only be hit once when adding duplicated images
// If we need to add a month section title, it needs to be inserted a position 0 where as if we only insert the
// images without creating a new month section title, it needs to be inserted at position 1 right after the
// existing month title
// This case can only be hit once, when adding duplicated images.
// If we need to add a new month section title, it needs to be inserted at position 0.
// If we only insert images, without creating a new month section title,
// they need to be inserted at position 1, right after the existing month title.
index = 0
itemList.add(index++, month)
newestSectionTitle = month
Expand Down Expand Up @@ -170,16 +168,11 @@ class PicturesAdapter(

fun getValidItemsSelected() = itemsSelected.filter { it.isUsable() }

private fun isSelectedFile(file: File): Boolean {
return itemsSelected.any { it.isUsable() && it.id == file.id }
}
private fun isSelectedFile(file: File): Boolean = itemsSelected.any { it.isUsable() && it.id == file.id }

private fun onSelectedFile(file: File, isSelected: Boolean) {
if (file.isUsable()) {
when {
isSelected -> addSelectedFile(file)
else -> removeSelectedFile(file)
}
if (isSelected) addSelectedFile(file) else removeSelectedFile(file)
} else {
itemsSelected = RealmList()
}
Expand Down Expand Up @@ -212,32 +205,25 @@ class PicturesAdapter(

fun deleteByFileId(fileId: Int) {
indexOf(fileId)?.let(::deleteAt)
pictureList.indexOfFirstOrNull { (it as? File)?.id == fileId }
?.let(pictureList::removeAt)
pictureList.indexOfFirstOrNull { (it as? File)?.id == fileId }?.let(pictureList::removeAt)
}

fun updateFileProgressByFileId(fileId: Int, progress: Int, onComplete: ((position: Int, file: File) -> Unit)? = null) {
indexOf(fileId)?.let { position ->
updateFileProgress(position, progress, onComplete)
}
indexOf(fileId)?.let { position -> updateFileProgress(position, progress, onComplete) }
}

private fun updateFileProgress(position: Int, progress: Int, onComplete: ((position: Int, file: File) -> Unit)? = null) {
val file = getFile(position)
file.currentProgress = progress
notifyItemChanged(position, progress)

if (progress == 100) {
onComplete?.invoke(position, file)
}
if (progress == 100) onComplete?.invoke(position, file)
}

private fun getFile(position: Int) = itemList[position] as File

fun updateOfflineStatus(fileId: Int) {
indexOf(fileId)?.let { position ->
(itemList[position] as? File)?.isOffline = true
}
indexOf(fileId)?.let { position -> (itemList[position] as? File)?.isOffline = true }
}

fun notifyFileChanged(fileId: Int, onChange: ((file: File) -> Unit)? = null) {
Expand All @@ -247,14 +233,13 @@ class PicturesAdapter(
}
}


private fun List<Any>.indexOfFirstOrNull(predicate: (Any) -> Boolean): Int? {
val index = indexOfFirst(predicate)
return if (index >= 0) index else null
}

enum class DisplayType(val layout: Int) {
TITLE(R.layout.title_recycler_section),
PICTURE(R.layout.cardview_picture)
PICTURE(R.layout.cardview_picture),
}
}
}
57 changes: 27 additions & 30 deletions app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.*
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -60,18 +60,16 @@ import kotlin.math.min

class PicturesFragment(
private val multiSelectParent: MultiSelectParent? = null,
private val onFinish: (() -> Unit)? = null
private val onFinish: (() -> Unit)? = null,
) : Fragment(), MultiSelectListener {

private val mainViewModel: MainViewModel by activityViewModels()
private val picturesViewModel: PicturesViewModel by viewModels()
private val picturesAdapter: PicturesAdapter by lazy {
PicturesAdapter { file ->
Utils.displayFile(mainViewModel, findNavController(), file, picturesAdapter.pictureList)
}
PicturesAdapter { file -> Utils.displayFile(mainViewModel, findNavController(), file, picturesAdapter.pictureList) }
}

private val selectFolderResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
private val selectFolderResultLauncher = registerForActivityResult(StartActivityForResult()) {
it.whenResultIsOk { data ->
with(data?.extras!!) {
val folderName = getString(SelectFolderActivity.FOLDER_NAME_TAG).toString()
Expand All @@ -82,7 +80,7 @@ class PicturesFragment(

performBulkOperation(
type = bulkOperationType,
destinationFolder = File(id = folderId, name = folderName, driveId = AccountUtils.currentDriveId)
destinationFolder = File(id = folderId, name = folderName, driveId = AccountUtils.currentDriveId),
)
}
}
Expand All @@ -103,15 +101,11 @@ class PicturesFragment(
noPicturesLayout.setup(
icon = R.drawable.ic_images,
title = R.string.picturesNoFile,
initialListView = picturesRecyclerView
initialListView = picturesRecyclerView,
)

requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
if (picturesAdapter.multiSelectMode) {
closeMultiSelect()
} else {
findNavController().popBackStack()
}
if (picturesAdapter.multiSelectMode) closeMultiSelect() else findNavController().popBackStack()
}

picturesAdapter.apply {
Expand All @@ -136,8 +130,7 @@ class PicturesFragment(
if (fileId == 0) return@observe

val progress = workInfo.progress.getInt(DownloadWorker.PROGRESS, 100)

if(progress == 100) picturesAdapter.updateOfflineStatus(fileId)
if (progress == 100) picturesAdapter.updateOfflineStatus(fileId)
}

getPictures()
Expand Down Expand Up @@ -190,9 +183,7 @@ class PicturesFragment(
fun performBulkOperation(type: BulkOperationType, destinationFolder: File? = null, color: String? = null) {

val selectedFiles = picturesAdapter.getValidItemsSelected()

val fileCount = selectedFiles.size

val sendActions: (dialog: Dialog?) -> Unit = sendActions(fileCount, selectedFiles, type, destinationFolder, color)

if (type == BulkOperationType.TRASH) {
Expand Down Expand Up @@ -306,11 +297,11 @@ class PicturesFragment(
mediator.addSource(
mainViewModel.deleteFileFromFavorites(
file,
callback = {
onFileUpdate = {
lifecycleScope.launch(Dispatchers.Main) {
picturesAdapter.notifyFileChanged(file.id) { it.isFavorite = false }
}
}
},
),
mainViewModel.updateMultiSelectMediator(mediator),
)
Expand Down Expand Up @@ -403,8 +394,7 @@ class PicturesFragment(
}

private fun enableButtonMultiSelect(isEnabled: Boolean) {
if (isEnabled) multiSelectParent?.enableMultiSelectActionButtons()
else multiSelectParent?.disableMultiSelectActionButtons()
multiSelectParent?.run { if (isEnabled) enableMultiSelectActionButtons() else disableMultiSelectActionButtons() }
}

private fun configPicturesLayoutManager() {
Expand All @@ -427,10 +417,15 @@ class PicturesFragment(
}

private fun openMultiSelect() {
picturesAdapter.multiSelectMode = true
picturesAdapter.notifyItemRangeChanged(0, picturesAdapter.itemCount)
multiSelectParent?.openMultiSelectBar()
multiSelectParent?.disableSwipeRefresh()
picturesAdapter.apply {
multiSelectMode = true
notifyItemRangeChanged(0, itemCount)
}

multiSelectParent?.run {
openMultiSelectBar()
disableSwipeRefresh()
}
}

fun duplicateFiles() {
Expand All @@ -452,8 +447,10 @@ class PicturesFragment(
notifyItemRangeChanged(0, itemCount)
}

multiSelectParent?.closeMultiSelectBar()
multiSelectParent?.enableSwipeRefresh()
multiSelectParent?.run {
closeMultiSelectBar()
enableSwipeRefresh()
}
}

private fun getPictures() {
Expand Down Expand Up @@ -481,8 +478,8 @@ class PicturesFragment(
}
}

companion object {
private const val numberItemLoader = 12
private companion object {
const val numberItemLoader = 12
}
}

Expand All @@ -501,4 +498,4 @@ interface MultiSelectParent {
fun setTitleMultiSelect(title: String)
fun disableSwipeRefresh()
fun enableSwipeRefresh()
}
}

0 comments on commit 714a222

Please sign in to comment.