@@ -30,6 +30,7 @@ import android.view.MenuInflater
3030import android.view.View
3131import android.view.ViewGroup
3232import androidx.appcompat.widget.SearchView
33+ import androidx.core.content.res.ResourcesCompat
3334import androidx.core.os.bundleOf
3435import androidx.core.view.isVisible
3536import androidx.fragment.app.Fragment
@@ -42,13 +43,17 @@ import com.owncloud.android.databinding.FileOptionsBottomSheetFragmentBinding
4243import com.owncloud.android.databinding.SpacesListFragmentBinding
4344import com.owncloud.android.domain.files.model.FileListOption
4445import com.owncloud.android.domain.spaces.model.OCSpace
46+ import com.owncloud.android.domain.spaces.model.SpaceMenuOption
4547import com.owncloud.android.extensions.collectLatestLifecycleFlow
4648import com.owncloud.android.extensions.showErrorInSnackbar
4749import com.owncloud.android.extensions.showMessageInSnackbar
4850import com.owncloud.android.extensions.toDrawableRes
51+ import com.owncloud.android.extensions.toDrawableResId
52+ import com.owncloud.android.extensions.toStringResId
4953import com.owncloud.android.extensions.toSubtitleStringRes
5054import com.owncloud.android.extensions.toTitleStringRes
5155import com.owncloud.android.presentation.capabilities.CapabilityViewModel
56+ import com.owncloud.android.presentation.common.BottomSheetFragmentItemView
5257import com.owncloud.android.utils.DisplayUtils
5358import com.owncloud.android.presentation.common.UIResult
5459import com.owncloud.android.presentation.spaces.createspace.CreateSpaceDialogFragment
@@ -66,6 +71,8 @@ class SpacesListFragment :
6671 private val binding get() = _binding !!
6772
6873 private var isMultiPersonal = false
74+ private var editSpacesPermission: Boolean = false
75+ private lateinit var currentSpace: OCSpace
6976
7077 private val spacesListViewModel: SpacesListViewModel by viewModel {
7178 parametersOf(
@@ -166,7 +173,10 @@ class SpacesListFragment :
166173 when (val uiResult = event.peekContent()) {
167174 is UIResult .Success -> {
168175 Timber .d(" The permissions for $accountName are: ${uiResult.data} " )
169- uiResult.data?.let { binding.fabCreateSpace.isVisible = it.contains(DRIVES_CREATE_ALL_PERMISSION ) }
176+ uiResult.data?.let {
177+ binding.fabCreateSpace.isVisible = it.contains(DRIVES_CREATE_ALL_PERMISSION )
178+ editSpacesPermission = it.contains(DRIVES_READ_WRITE_ALL_PERMISSION )
179+ }
170180 }
171181 is UIResult .Loading -> { }
172182 is UIResult .Error -> {
@@ -187,6 +197,47 @@ class SpacesListFragment :
187197 }
188198 }
189199
200+ collectLatestLifecycleFlow(spacesListViewModel.menuOptions) { menuOptions ->
201+ val spaceOptionsBottomSheetBinding = FileOptionsBottomSheetFragmentBinding .inflate(layoutInflater)
202+ val dialog = BottomSheetDialog (requireContext())
203+ dialog.setContentView(spaceOptionsBottomSheetBinding.root)
204+
205+ val fileOptionsBottomSheetSingleFileBehavior: BottomSheetBehavior <* > = BottomSheetBehavior .from(
206+ spaceOptionsBottomSheetBinding.root.parent as View )
207+ val closeBottomSheetButton = spaceOptionsBottomSheetBinding.closeBottomSheet
208+ closeBottomSheetButton.setOnClickListener {
209+ dialog.dismiss()
210+ }
211+
212+ val thumbnailBottomSheet = spaceOptionsBottomSheetBinding.thumbnailBottomSheet
213+ thumbnailBottomSheet.setImageResource(R .drawable.ic_menu_space)
214+
215+ val spaceNameBottomSheet = spaceOptionsBottomSheetBinding.fileNameBottomSheet
216+ spaceNameBottomSheet.text = currentSpace.name
217+
218+ val spaceSizeBottomSheet = spaceOptionsBottomSheetBinding.fileSizeBottomSheet
219+ spaceSizeBottomSheet.text = DisplayUtils .bytesToHumanReadable(currentSpace.quota?.used ? : 0L , requireContext(), true )
220+
221+ val spaceSeparatorBottomSheet = spaceOptionsBottomSheetBinding.fileSeparatorBottomSheet
222+ spaceSeparatorBottomSheet.visibility = View .GONE
223+
224+ menuOptions.forEach { menuOption ->
225+ setMenuOption(menuOption, spaceOptionsBottomSheetBinding, dialog)
226+ }
227+
228+ fileOptionsBottomSheetSingleFileBehavior.addBottomSheetCallback(object : BottomSheetBehavior .BottomSheetCallback () {
229+ override fun onStateChanged (bottomSheet : View , newState : Int ) {
230+ if (newState == BottomSheetBehavior .STATE_DRAGGING ) {
231+ fileOptionsBottomSheetSingleFileBehavior.state = BottomSheetBehavior .STATE_EXPANDED
232+ }
233+ }
234+ override fun onSlide (bottomSheet : View , slideOffset : Float ) {}
235+ })
236+
237+ dialog.setOnShowListener { fileOptionsBottomSheetSingleFileBehavior.peekHeight = spaceOptionsBottomSheetBinding.root.measuredHeight }
238+ dialog.show()
239+ }
240+
190241 }
191242
192243 private fun showOrHideEmptyView (spacesList : List <OCSpace >) {
@@ -211,40 +262,8 @@ class SpacesListFragment :
211262 }
212263
213264 override fun onThreeDotButtonClick (ocSpace : OCSpace ) {
214- val spaceOptionsBottomSheetBinding = FileOptionsBottomSheetFragmentBinding .inflate(layoutInflater)
215- val dialog = BottomSheetDialog (requireContext())
216- dialog.setContentView(spaceOptionsBottomSheetBinding.root)
217-
218- val fileOptionsBottomSheetSingleFileBehavior: BottomSheetBehavior <* > = BottomSheetBehavior .from(
219- spaceOptionsBottomSheetBinding.root.parent as View )
220- val closeBottomSheetButton = spaceOptionsBottomSheetBinding.closeBottomSheet
221- closeBottomSheetButton.setOnClickListener {
222- dialog.dismiss()
223- }
224-
225- val thumbnailBottomSheet = spaceOptionsBottomSheetBinding.thumbnailBottomSheet
226- thumbnailBottomSheet.setImageResource(R .drawable.ic_menu_space)
227-
228- val spaceNameBottomSheet = spaceOptionsBottomSheetBinding.fileNameBottomSheet
229- spaceNameBottomSheet.text = ocSpace.name
230-
231- val spaceSizeBottomSheet = spaceOptionsBottomSheetBinding.fileSizeBottomSheet
232- spaceSizeBottomSheet.text = DisplayUtils .bytesToHumanReadable(ocSpace.quota?.used ? : 0L , requireContext(), true )
233-
234- val spaceSeparatorBottomSheet = spaceOptionsBottomSheetBinding.fileSeparatorBottomSheet
235- spaceSeparatorBottomSheet.visibility = View .GONE
236-
237- fileOptionsBottomSheetSingleFileBehavior.addBottomSheetCallback(object : BottomSheetBehavior .BottomSheetCallback () {
238- override fun onStateChanged (bottomSheet : View , newState : Int ) {
239- if (newState == BottomSheetBehavior .STATE_DRAGGING ) {
240- fileOptionsBottomSheetSingleFileBehavior.state = BottomSheetBehavior .STATE_EXPANDED
241- }
242- }
243- override fun onSlide (bottomSheet : View , slideOffset : Float ) {}
244- })
245-
246- dialog.setOnShowListener { fileOptionsBottomSheetSingleFileBehavior.peekHeight = spaceOptionsBottomSheetBinding.root.measuredHeight }
247- dialog.show()
265+ currentSpace = ocSpace
266+ spacesListViewModel.filterMenuOptions(ocSpace, editSpacesPermission)
248267 }
249268
250269 override fun onCreateOptionsMenu (menu : Menu , inflater : MenuInflater ) {
@@ -276,12 +295,32 @@ class SpacesListFragment :
276295 searchViewRootToolbar.queryHint = getString(R .string.actionbar_search_space)
277296 }
278297
298+ private fun setMenuOption (menuOption : SpaceMenuOption , binding : FileOptionsBottomSheetFragmentBinding , dialog : BottomSheetDialog ) {
299+ val fileOptionItemView = BottomSheetFragmentItemView (requireContext())
300+ fileOptionItemView.apply {
301+ itemIcon = ResourcesCompat .getDrawable(resources, menuOption.toDrawableResId(), null )
302+ title = getString(menuOption.toStringResId())
303+ setOnClickListener {
304+ dialog.dismiss()
305+ when (menuOption) {
306+ SpaceMenuOption .EDIT -> {
307+ val accountName = requireArguments().getString(BUNDLE_ACCOUNT_NAME )
308+ val editDialog = CreateSpaceDialogFragment .newInstance(accountName, this @SpacesListFragment)
309+ editDialog.show(requireActivity().supportFragmentManager, DIALOG_CREATE_SPACE )
310+ }
311+ }
312+ }
313+ }
314+ binding.fileOptionsBottomSheetLayout.addView(fileOptionItemView)
315+ }
316+
279317 companion object {
280318 const val REQUEST_KEY_CLICK_SPACE = " REQUEST_KEY_CLICK_SPACE"
281319 const val BUNDLE_KEY_CLICK_SPACE = " BUNDLE_KEY_CLICK_SPACE"
282320 const val BUNDLE_SHOW_PERSONAL_SPACE = " showPersonalSpace"
283321 const val BUNDLE_ACCOUNT_NAME = " accountName"
284322 const val DRIVES_CREATE_ALL_PERMISSION = " Drives.Create.all"
323+ const val DRIVES_READ_WRITE_ALL_PERMISSION = " Drives.ReadWrite.all"
285324
286325 private const val DIALOG_CREATE_SPACE = " DIALOG_CREATE_SPACE"
287326
0 commit comments