Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.nextcloud.ui.SetStatusMessageBottomSheet;
import com.nextcloud.ui.composeActivity.ComposeActivity;
import com.nextcloud.ui.fileactions.FileActionsBottomSheet;
import com.nextcloud.ui.tags.TagManagementBottomSheet;
import com.nextcloud.ui.trashbinFileActions.TrashbinFileActionsBottomSheet;
import com.nmc.android.ui.LauncherActivity;
import com.owncloud.android.MainApp;
Expand Down Expand Up @@ -512,4 +513,7 @@ abstract class ComponentsModule {

@ContributesAndroidInjector
abstract CommunityFragment communityFragment();

@ContributesAndroidInjector
abstract TagManagementBottomSheet tagManagementBottomSheet();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import com.nextcloud.client.documentscan.DocumentScanViewModel
import com.nextcloud.client.etm.EtmViewModel
import com.nextcloud.client.logger.ui.LogsViewModel
import com.nextcloud.ui.fileactions.FileActionsViewModel
import com.owncloud.android.ui.preview.pdf.PreviewPdfViewModel
import com.nextcloud.ui.trashbinFileActions.TrashbinFileActionsViewModel
import com.owncloud.android.ui.preview.pdf.PreviewPdfViewModel
import com.owncloud.android.ui.unifiedsearch.UnifiedSearchViewModel
import dagger.Binds
import dagger.Module
Expand Down
154 changes: 154 additions & 0 deletions app/src/main/java/com/nextcloud/ui/tags/TagManagementBottomSheet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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.BundleCompat
import androidx.core.os.bundleOf
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.Lifecycle
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.ui.tags.adapter.TagListAdapter
import com.nextcloud.ui.tags.model.TagUiState
import com.nextcloud.ui.tags.repository.TagManagementRepositoryImpl
import com.nextcloud.utils.extensions.getTypedActivity
import com.owncloud.android.databinding.TagManagementBottomSheetBinding
import com.owncloud.android.lib.resources.tags.Tag
import com.owncloud.android.ui.activity.BaseActivity
import com.owncloud.android.utils.theme.ViewThemeUtils
import kotlinx.coroutines.launch
import javax.inject.Inject

class TagManagementBottomSheet :
BottomSheetDialogFragment(),
Injectable {

@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 onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val clientRepository = getTypedActivity(BaseActivity::class.java)?.clientRepository
?: error("clientRepository not available")
val repository = TagManagementRepositoryImpl(clientRepository)
viewModel = TagManagementViewModel(repository)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_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 = BundleCompat.getParcelableArrayList(requireArguments(), ARG_CURRENT_TAGS, Tag::class.java)
?: arrayListOf()
viewModel.fetch(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 TagUiState.Loading -> {
binding.loadingIndicator.visibility = View.VISIBLE
binding.tagList.visibility = View.GONE
}

is TagUiState.Loaded -> {
binding.loadingIndicator.visibility = View.GONE
binding.tagList.visibility = View.VISIBLE
tagAdapter.update(state.allTags, state.assignedTagIds, state.query)
}

is 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)
)
}
}
}
110 changes: 110 additions & 0 deletions app/src/main/java/com/nextcloud/ui/tags/TagManagementViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.ui.tags

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nextcloud.ui.tags.model.TagUiState
import com.nextcloud.ui.tags.model.toLoaded
import com.nextcloud.ui.tags.repository.TagManagementRepository
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.tags.Tag
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class TagManagementViewModel(private val repository: TagManagementRepository) : ViewModel() {

companion object {
private const val TAG = "TagManagementViewModel"
}

private val _uiState = MutableStateFlow<TagUiState>(TagUiState.Loading)
val uiState: StateFlow<TagUiState> = _uiState

private var fileId: Long = -1

fun fetch(fileId: Long, currentTags: List<Tag>) {
this.fileId = fileId
viewModelScope.launch {
val tags = repository.fetch(fileId, currentTags)

// TODO: handle error ui state
_uiState.update {
tags.toLoaded(currentTags)
}
}
}

fun assignTag(tag: Tag) = setTagAssigned(tag, assign = true)

fun unassignTag(tag: Tag) = setTagAssigned(tag, assign = false)

// TODO: handle error ui state
private fun setTagAssigned(tag: Tag, assign: Boolean) {
val loaded = _uiState.value as? TagUiState.Loaded ?: return
if ((tag.id in loaded.assignedTagIds) == assign) return

fun apply(assigned: Boolean) = _uiState.update { state ->
if (state is TagUiState.Loaded) {
state.copy(
assignedTagIds = if (assigned) state.assignedTagIds + tag.id else state.assignedTagIds - tag.id
)
} else {
state
}
}

apply(assign)

viewModelScope.launch {
val success = if (assign) repository.assignTag(fileId, tag) else repository.unassignTag(fileId, tag)
if (!success) {
Log_OC.e(TAG, "cannot ${if (assign) "assign" else "unassign"} tag")
apply(!assign)
}
}
}

fun createAndAssignTag(name: String) {
viewModelScope.launch {
val (allTags, newTagId) = repository.createAndAssignTag(fileId, name) ?: return@launch
_uiState.update { state ->
if (state is TagUiState.Loaded) {
state.copy(
allTags = allTags,
assignedTagIds = state.assignedTagIds + newTagId
)
} else {
TagUiState.Loaded(
allTags = allTags,
assignedTagIds = setOf(newTagId)
)
}
}
}
}

fun setSearchQuery(query: String) {
_uiState.update { state ->
if (state is TagUiState.Loaded) {
state.copy(query = query)
} else {
state
}
}
}

fun getAssignedTags(): List<Tag> {
val state = _uiState.value
if (state is TagUiState.Loaded) {
return state.allTags.filter { it.id in state.assignedTagIds }
}
return emptyList()
}
}
86 changes: 86 additions & 0 deletions app/src/main/java/com/nextcloud/ui/tags/adapter/TagListAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.nextcloud.ui.tags.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.nextcloud.ui.tags.adapter.viewholder.CreateTagViewHolder
import com.nextcloud.ui.tags.adapter.viewholder.TagViewHolder
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
}

init {
setHasStableIds(true)
}

@SuppressLint("NotifyDataSetChanged")
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 getItemId(position: Int): Long = if (getItemViewType(position) == VIEW_TYPE_CREATE) {
Long.MIN_VALUE
} else {
tags[position].id.hashCode().toLong()
}

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, onTagChecked)
}

is CreateTagViewHolder -> {
holder.bind(query, onCreateTag)
}
}
}
}
Loading
Loading