|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only |
| 6 | + */ |
| 7 | +package com.nextcloud.ui.tags |
| 8 | + |
| 9 | +import android.os.Bundle |
| 10 | +import android.view.LayoutInflater |
| 11 | +import android.view.View |
| 12 | +import android.view.ViewGroup |
| 13 | +import androidx.core.os.bundleOf |
| 14 | +import androidx.core.widget.doAfterTextChanged |
| 15 | +import androidx.fragment.app.setFragmentResult |
| 16 | +import androidx.lifecycle.Lifecycle |
| 17 | +import androidx.lifecycle.ViewModelProvider |
| 18 | +import androidx.lifecycle.lifecycleScope |
| 19 | +import androidx.lifecycle.repeatOnLifecycle |
| 20 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 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.nextcloud.client.di.ViewModelFactory |
| 27 | +import com.owncloud.android.databinding.TagManagementBottomSheetBinding |
| 28 | +import com.owncloud.android.lib.resources.tags.Tag |
| 29 | +import com.owncloud.android.utils.theme.ViewThemeUtils |
| 30 | +import kotlinx.coroutines.launch |
| 31 | +import javax.inject.Inject |
| 32 | + |
| 33 | +class TagManagementBottomSheet : BottomSheetDialogFragment(), Injectable { |
| 34 | + |
| 35 | + @Inject |
| 36 | + lateinit var vmFactory: ViewModelFactory |
| 37 | + |
| 38 | + @Inject |
| 39 | + lateinit var viewThemeUtils: ViewThemeUtils |
| 40 | + |
| 41 | + private var _binding: TagManagementBottomSheetBinding? = null |
| 42 | + private val binding get() = _binding!! |
| 43 | + |
| 44 | + private lateinit var viewModel: TagManagementViewModel |
| 45 | + private lateinit var tagAdapter: TagListAdapter |
| 46 | + |
| 47 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { |
| 48 | + viewModel = ViewModelProvider(this, vmFactory)[TagManagementViewModel::class.java] |
| 49 | + _binding = TagManagementBottomSheetBinding.inflate(inflater, container, false) |
| 50 | + |
| 51 | + val bottomSheetDialog = dialog as BottomSheetDialog |
| 52 | + bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED |
| 53 | + bottomSheetDialog.behavior.skipCollapsed = true |
| 54 | + |
| 55 | + viewThemeUtils.platform.colorViewBackground(binding.bottomSheet, ColorRole.SURFACE) |
| 56 | + |
| 57 | + setupAdapter() |
| 58 | + setupSearch() |
| 59 | + observeState() |
| 60 | + |
| 61 | + val fileId = requireArguments().getLong(ARG_FILE_ID) |
| 62 | + val currentTags = requireArguments().getParcelableArrayList<Tag>(ARG_CURRENT_TAGS) ?: arrayListOf() |
| 63 | + viewModel.load(fileId, currentTags) |
| 64 | + |
| 65 | + return binding.root |
| 66 | + } |
| 67 | + |
| 68 | + private fun setupAdapter() { |
| 69 | + tagAdapter = TagListAdapter( |
| 70 | + onTagChecked = { tag, isChecked -> |
| 71 | + if (isChecked) { |
| 72 | + viewModel.assignTag(tag) |
| 73 | + } else { |
| 74 | + viewModel.unassignTag(tag) |
| 75 | + } |
| 76 | + }, |
| 77 | + onCreateTag = { name -> |
| 78 | + viewModel.createAndAssignTag(name) |
| 79 | + binding.searchEditText.text?.clear() |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + binding.tagList.apply { |
| 84 | + layoutManager = LinearLayoutManager(requireContext()) |
| 85 | + adapter = tagAdapter |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private fun setupSearch() { |
| 90 | + binding.searchEditText.doAfterTextChanged { text -> |
| 91 | + viewModel.setSearchQuery(text?.toString() ?: "") |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private fun observeState() { |
| 96 | + viewLifecycleOwner.lifecycleScope.launch { |
| 97 | + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { |
| 98 | + viewModel.uiState.collect { state -> |
| 99 | + when (state) { |
| 100 | + is TagManagementViewModel.TagUiState.Loading -> { |
| 101 | + binding.loadingIndicator.visibility = View.VISIBLE |
| 102 | + binding.tagList.visibility = View.GONE |
| 103 | + } |
| 104 | + is TagManagementViewModel.TagUiState.Loaded -> { |
| 105 | + binding.loadingIndicator.visibility = View.GONE |
| 106 | + binding.tagList.visibility = View.VISIBLE |
| 107 | + tagAdapter.update(state.allTags, state.assignedTagIds, state.query) |
| 108 | + } |
| 109 | + is TagManagementViewModel.TagUiState.Error -> { |
| 110 | + binding.loadingIndicator.visibility = View.GONE |
| 111 | + binding.tagList.visibility = View.GONE |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + override fun onDestroyView() { |
| 120 | + val assignedTags = viewModel.getAssignedTags() |
| 121 | + setFragmentResult(REQUEST_KEY, bundleOf(RESULT_KEY_TAGS to ArrayList(assignedTags))) |
| 122 | + |
| 123 | + super.onDestroyView() |
| 124 | + _binding = null |
| 125 | + } |
| 126 | + |
| 127 | + companion object { |
| 128 | + const val REQUEST_KEY = "TAG_MANAGEMENT_REQUEST" |
| 129 | + const val RESULT_KEY_TAGS = "RESULT_TAGS" |
| 130 | + private const val ARG_FILE_ID = "ARG_FILE_ID" |
| 131 | + private const val ARG_CURRENT_TAGS = "ARG_CURRENT_TAGS" |
| 132 | + |
| 133 | + fun newInstance(fileId: Long, currentTags: List<Tag>): TagManagementBottomSheet { |
| 134 | + return TagManagementBottomSheet().apply { |
| 135 | + arguments = bundleOf( |
| 136 | + ARG_FILE_ID to fileId, |
| 137 | + ARG_CURRENT_TAGS to ArrayList(currentTags) |
| 138 | + ) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments