-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
edit tags #16672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tobiasKaminsky
wants to merge
2
commits into
master
Choose a base branch
from
editTags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,938
−28
Open
edit tags #16672
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
app/src/main/java/com/nextcloud/ui/tags/TagListAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only | ||
| */ | ||
| package com.nextcloud.ui.tags | ||
|
|
||
| import android.graphics.Color | ||
| import android.graphics.drawable.GradientDrawable | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.CheckBox | ||
| import android.widget.TextView | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.owncloud.android.R | ||
| import com.owncloud.android.lib.resources.tags.Tag | ||
|
|
||
| class TagListAdapter(private val onTagChecked: (Tag, Boolean) -> Unit, private val onCreateTag: (String) -> Unit) : | ||
| RecyclerView.Adapter<RecyclerView.ViewHolder>() { | ||
|
|
||
| private var tags: List<Tag> = emptyList() | ||
| private var assignedTagIds: Set<String> = emptySet() | ||
| private var query: String = "" | ||
| private var showCreateItem: Boolean = false | ||
|
|
||
| companion object { | ||
| private const val VIEW_TYPE_TAG = 0 | ||
| private const val VIEW_TYPE_CREATE = 1 | ||
| } | ||
|
|
||
| fun update(allTags: List<Tag>, assignedIds: Set<String>, searchQuery: String) { | ||
| this.assignedTagIds = assignedIds | ||
| this.query = searchQuery | ||
|
|
||
| tags = if (searchQuery.isBlank()) { | ||
| allTags | ||
| } else { | ||
| allTags.filter { it.name.contains(searchQuery, ignoreCase = true) } | ||
| } | ||
|
|
||
| showCreateItem = searchQuery.isNotBlank() && tags.none { it.name.equals(searchQuery, ignoreCase = true) } | ||
|
|
||
| notifyDataSetChanged() | ||
| } | ||
|
|
||
| override fun getItemCount(): Int = tags.size + if (showCreateItem) 1 else 0 | ||
|
|
||
| override fun getItemViewType(position: Int): Int = | ||
| if (showCreateItem && position == tags.size) VIEW_TYPE_CREATE else VIEW_TYPE_TAG | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
| val inflater = LayoutInflater.from(parent.context) | ||
| return if (viewType == VIEW_TYPE_CREATE) { | ||
| val view = inflater.inflate(R.layout.tag_list_item, parent, false) | ||
| CreateTagViewHolder(view) | ||
| } else { | ||
| val view = inflater.inflate(R.layout.tag_list_item, parent, false) | ||
| TagViewHolder(view) | ||
| } | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
| when (holder) { | ||
| is TagViewHolder -> { | ||
| val tag = tags[position] | ||
| holder.bind(tag, tag.id in assignedTagIds) | ||
| } | ||
|
|
||
| is CreateTagViewHolder -> { | ||
| holder.bind(query) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| inner class TagViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move inner classes inside |
||
| private val colorDot: View = itemView.findViewById(R.id.tag_color_dot) | ||
| private val tagName: TextView = itemView.findViewById(R.id.tag_name) | ||
| private val checkBox: CheckBox = itemView.findViewById(R.id.tag_checkbox) | ||
|
|
||
| fun bind(tag: Tag, isAssigned: Boolean) { | ||
| tagName.text = tag.name | ||
|
|
||
| if (tag.color != null) { | ||
| try { | ||
| val color = Color.parseColor(tag.color) | ||
| val background = colorDot.background | ||
| if (background is GradientDrawable) { | ||
| background.setColor(color) | ||
| } | ||
| colorDot.visibility = View.VISIBLE | ||
| } catch (e: IllegalArgumentException) { | ||
| colorDot.visibility = View.INVISIBLE | ||
| } | ||
| } else { | ||
| colorDot.visibility = View.INVISIBLE | ||
| } | ||
|
|
||
| checkBox.setOnCheckedChangeListener(null) | ||
| checkBox.isChecked = isAssigned | ||
| checkBox.setOnCheckedChangeListener { _, isChecked -> | ||
| onTagChecked(tag, isChecked) | ||
| } | ||
|
|
||
| itemView.setOnClickListener { | ||
| checkBox.isChecked = !checkBox.isChecked | ||
| } | ||
| } | ||
| } | ||
|
|
||
| inner class CreateTagViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
| private val colorDot: View = itemView.findViewById(R.id.tag_color_dot) | ||
| private val tagName: TextView = itemView.findViewById(R.id.tag_name) | ||
| private val checkBox: CheckBox = itemView.findViewById(R.id.tag_checkbox) | ||
|
|
||
| fun bind(name: String) { | ||
| colorDot.visibility = View.INVISIBLE | ||
| tagName.text = itemView.context.getString(R.string.create_tag_format, name) | ||
| checkBox.visibility = View.GONE | ||
|
|
||
| itemView.setOnClickListener { | ||
| onCreateTag(name) | ||
| } | ||
| } | ||
| } | ||
| } | ||
145 changes: 145 additions & 0 deletions
145
app/src/main/java/com/nextcloud/ui/tags/TagManagementBottomSheet.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only | ||
| */ | ||
| package com.nextcloud.ui.tags | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.core.os.bundleOf | ||
| import androidx.core.widget.doAfterTextChanged | ||
| import androidx.fragment.app.setFragmentResult | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.lifecycle.repeatOnLifecycle | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import com.google.android.material.bottomsheet.BottomSheetBehavior | ||
| import com.google.android.material.bottomsheet.BottomSheetDialog | ||
| import com.google.android.material.bottomsheet.BottomSheetDialogFragment | ||
| import com.nextcloud.android.common.ui.theme.utils.ColorRole | ||
| import com.nextcloud.client.di.Injectable | ||
| import com.nextcloud.client.di.ViewModelFactory | ||
| import com.owncloud.android.databinding.TagManagementBottomSheetBinding | ||
| import com.owncloud.android.lib.resources.tags.Tag | ||
| import com.owncloud.android.utils.theme.ViewThemeUtils | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| class TagManagementBottomSheet : | ||
| BottomSheetDialogFragment(), | ||
| Injectable { | ||
|
|
||
| @Inject | ||
| lateinit var vmFactory: ViewModelFactory | ||
|
|
||
| @Inject | ||
| lateinit var viewThemeUtils: ViewThemeUtils | ||
|
|
||
| private var _binding: TagManagementBottomSheetBinding? = null | ||
| private val binding get() = _binding!! | ||
|
|
||
| private lateinit var viewModel: TagManagementViewModel | ||
| private lateinit var tagAdapter: TagListAdapter | ||
|
|
||
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||
| viewModel = ViewModelProvider(this, vmFactory)[TagManagementViewModel::class.java] | ||
| _binding = TagManagementBottomSheetBinding.inflate(inflater, container, false) | ||
|
|
||
| val bottomSheetDialog = dialog as BottomSheetDialog | ||
| bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED | ||
| bottomSheetDialog.behavior.skipCollapsed = true | ||
|
|
||
| viewThemeUtils.platform.colorViewBackground(binding.bottomSheet, ColorRole.SURFACE) | ||
|
|
||
| setupAdapter() | ||
| setupSearch() | ||
| observeState() | ||
|
|
||
| val fileId = requireArguments().getLong(ARG_FILE_ID) | ||
| val currentTags = requireArguments().getParcelableArrayList<Tag>(ARG_CURRENT_TAGS) ?: arrayListOf() | ||
| viewModel.load(fileId, currentTags) | ||
|
|
||
| return binding.root | ||
| } | ||
|
|
||
| private fun setupAdapter() { | ||
| tagAdapter = TagListAdapter( | ||
| onTagChecked = { tag, isChecked -> | ||
| if (isChecked) { | ||
| viewModel.assignTag(tag) | ||
| } else { | ||
| viewModel.unassignTag(tag) | ||
| } | ||
| }, | ||
| onCreateTag = { name -> | ||
| viewModel.createAndAssignTag(name) | ||
| binding.searchEditText.text?.clear() | ||
| } | ||
| ) | ||
|
|
||
| binding.tagList.apply { | ||
| layoutManager = LinearLayoutManager(requireContext()) | ||
| adapter = tagAdapter | ||
| } | ||
| } | ||
|
|
||
| private fun setupSearch() { | ||
| binding.searchEditText.doAfterTextChanged { text -> | ||
| viewModel.setSearchQuery(text?.toString() ?: "") | ||
| } | ||
| } | ||
|
|
||
| private fun observeState() { | ||
| viewLifecycleOwner.lifecycleScope.launch { | ||
| viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
| viewModel.uiState.collect { state -> | ||
| when (state) { | ||
| is TagManagementViewModel.TagUiState.Loading -> { | ||
| binding.loadingIndicator.visibility = View.VISIBLE | ||
| binding.tagList.visibility = View.GONE | ||
| } | ||
|
|
||
| is TagManagementViewModel.TagUiState.Loaded -> { | ||
| binding.loadingIndicator.visibility = View.GONE | ||
| binding.tagList.visibility = View.VISIBLE | ||
| tagAdapter.update(state.allTags, state.assignedTagIds, state.query) | ||
| } | ||
|
|
||
| is TagManagementViewModel.TagUiState.Error -> { | ||
| binding.loadingIndicator.visibility = View.GONE | ||
| binding.tagList.visibility = View.GONE | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| val assignedTags = viewModel.getAssignedTags() | ||
| setFragmentResult(REQUEST_KEY, bundleOf(RESULT_KEY_TAGS to ArrayList(assignedTags))) | ||
|
|
||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
|
|
||
| companion object { | ||
| const val REQUEST_KEY = "TAG_MANAGEMENT_REQUEST" | ||
| const val RESULT_KEY_TAGS = "RESULT_TAGS" | ||
| private const val ARG_FILE_ID = "ARG_FILE_ID" | ||
| private const val ARG_CURRENT_TAGS = "ARG_CURRENT_TAGS" | ||
|
|
||
| fun newInstance(fileId: Long, currentTags: List<Tag>): TagManagementBottomSheet = | ||
| TagManagementBottomSheet().apply { | ||
| arguments = bundleOf( | ||
| ARG_FILE_ID to fileId, | ||
| ARG_CURRENT_TAGS to ArrayList(currentTags) | ||
| ) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move adapter inside
com.nextcloud.ui.tags.adapter