|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 TSI-mc <surinder.kumar@t-systems.com> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.ui.albumItemActions |
| 9 | + |
| 10 | +import android.os.Bundle |
| 11 | +import android.view.LayoutInflater |
| 12 | +import android.view.View |
| 13 | +import android.view.ViewGroup |
| 14 | +import androidx.annotation.IdRes |
| 15 | +import androidx.appcompat.content.res.AppCompatResources |
| 16 | +import androidx.core.os.bundleOf |
| 17 | +import androidx.core.view.isEmpty |
| 18 | +import androidx.fragment.app.FragmentManager |
| 19 | +import androidx.fragment.app.setFragmentResult |
| 20 | +import androidx.lifecycle.LifecycleOwner |
| 21 | +import com.google.android.material.bottomsheet.BottomSheetBehavior |
| 22 | +import com.google.android.material.bottomsheet.BottomSheetDialog |
| 23 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment |
| 24 | +import com.nextcloud.android.common.ui.theme.utils.ColorRole |
| 25 | +import com.nextcloud.client.di.Injectable |
| 26 | +import com.owncloud.android.databinding.FileActionsBottomSheetBinding |
| 27 | +import com.owncloud.android.databinding.FileActionsBottomSheetItemBinding |
| 28 | +import com.owncloud.android.utils.theme.ViewThemeUtils |
| 29 | +import javax.inject.Inject |
| 30 | + |
| 31 | +class AlbumItemActionsBottomSheet : BottomSheetDialogFragment(), Injectable { |
| 32 | + |
| 33 | + @Inject |
| 34 | + lateinit var viewThemeUtils: ViewThemeUtils |
| 35 | + |
| 36 | + private var _binding: FileActionsBottomSheetBinding? = null |
| 37 | + val binding |
| 38 | + get() = _binding!! |
| 39 | + |
| 40 | + fun interface ResultListener { |
| 41 | + fun onResult(@IdRes actionId: Int) |
| 42 | + } |
| 43 | + |
| 44 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { |
| 45 | + _binding = FileActionsBottomSheetBinding.inflate(inflater, container, false) |
| 46 | + |
| 47 | + val bottomSheetDialog = dialog as BottomSheetDialog |
| 48 | + bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED |
| 49 | + bottomSheetDialog.behavior.skipCollapsed = true |
| 50 | + |
| 51 | + viewThemeUtils.platform.colorViewBackground(binding.bottomSheet, ColorRole.SURFACE) |
| 52 | + |
| 53 | + return binding.root |
| 54 | + } |
| 55 | + |
| 56 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 57 | + super.onViewCreated(view, savedInstanceState) |
| 58 | + binding.bottomSheetHeader.visibility = View.GONE |
| 59 | + binding.bottomSheetLoading.visibility = View.GONE |
| 60 | + displayActions() |
| 61 | + } |
| 62 | + |
| 63 | + override fun onDestroyView() { |
| 64 | + super.onDestroyView() |
| 65 | + _binding = null |
| 66 | + } |
| 67 | + |
| 68 | + fun setResultListener( |
| 69 | + fragmentManager: FragmentManager, |
| 70 | + lifecycleOwner: LifecycleOwner, |
| 71 | + listener: ResultListener |
| 72 | + ): AlbumItemActionsBottomSheet { |
| 73 | + fragmentManager.setFragmentResultListener(REQUEST_KEY, lifecycleOwner) { _, result -> |
| 74 | + @IdRes val actionId = result.getInt(RESULT_KEY_ACTION_ID, -1) |
| 75 | + if (actionId != -1) { |
| 76 | + listener.onResult(actionId) |
| 77 | + } |
| 78 | + } |
| 79 | + return this |
| 80 | + } |
| 81 | + |
| 82 | + private fun displayActions() { |
| 83 | + if (binding.fileActionsList.isEmpty()) { |
| 84 | + AlbumItemAction.SORTED_VALUES.forEach { action -> |
| 85 | + val view = inflateActionView(action) |
| 86 | + binding.fileActionsList.addView(view) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private fun inflateActionView(action: AlbumItemAction): View { |
| 92 | + val itemBinding = FileActionsBottomSheetItemBinding.inflate(layoutInflater, binding.fileActionsList, false) |
| 93 | + .apply { |
| 94 | + root.setOnClickListener { |
| 95 | + dispatchActionClick(action.id) |
| 96 | + } |
| 97 | + text.setText(action.title) |
| 98 | + if (action.icon != null) { |
| 99 | + val drawable = |
| 100 | + viewThemeUtils.platform.tintDrawable( |
| 101 | + requireContext(), |
| 102 | + AppCompatResources.getDrawable(requireContext(), action.icon)!! |
| 103 | + ) |
| 104 | + icon.setImageDrawable(drawable) |
| 105 | + } |
| 106 | + } |
| 107 | + return itemBinding.root |
| 108 | + } |
| 109 | + |
| 110 | + private fun dispatchActionClick(id: Int?) { |
| 111 | + if (id != null) { |
| 112 | + setFragmentResult(REQUEST_KEY, bundleOf(RESULT_KEY_ACTION_ID to id)) |
| 113 | + parentFragmentManager.clearFragmentResultListener(REQUEST_KEY) |
| 114 | + dismiss() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + companion object { |
| 119 | + private const val REQUEST_KEY = "REQUEST_KEY_ACTION" |
| 120 | + private const val RESULT_KEY_ACTION_ID = "RESULT_KEY_ACTION_ID" |
| 121 | + |
| 122 | + @JvmStatic |
| 123 | + fun newInstance(): AlbumItemActionsBottomSheet { |
| 124 | + return AlbumItemActionsBottomSheet() |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments