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 @@ -34,6 +34,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 @@ -510,6 +511,9 @@ abstract class ComponentsModule {

@ContributesAndroidInjector
abstract SetStatusMessageBottomSheet setStatusMessageBottomSheet();

@ContributesAndroidInjector
abstract TagManagementBottomSheet tagManagementBottomSheet();

@ContributesAndroidInjector
abstract NavigatorActivity navigatorActivity();
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/nextcloud/client/di/ViewModelModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ 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.tags.TagManagementViewModel
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 Expand Up @@ -57,6 +58,11 @@ abstract class ViewModelModule {
@ViewModelKey(TrashbinFileActionsViewModel::class)
abstract fun trashbinFileActionsViewModel(vm: TrashbinFileActionsViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(TagManagementViewModel::class)
abstract fun tagManagementViewModel(vm: TagManagementViewModel): ViewModel

@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}
147 changes: 147 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,147 @@
/*
* 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.nextcloud.ui.tags.adapter.TagListAdapter
import com.nextcloud.ui.tags.model.TagUiState
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 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)
)
}
}
}
166 changes: 166 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,166 @@
/*
* 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 androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nextcloud.client.account.CurrentAccountProvider
import com.nextcloud.ui.tags.model.TagUiState
import com.nextcloud.client.network.ClientFactory
import com.owncloud.android.R
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.tags.CreateTagRemoteOperation
import com.owncloud.android.lib.resources.tags.DeleteTagRemoteOperation
import com.owncloud.android.lib.resources.tags.GetTagsRemoteOperation
import com.owncloud.android.lib.resources.tags.PutTagRemoteOperation
import com.owncloud.android.lib.resources.tags.Tag
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

class TagManagementViewModel @Inject constructor(
private val clientFactory: ClientFactory,
private val currentAccountProvider: CurrentAccountProvider
) : ViewModel() {

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

private var fileId: Long = -1

fun load(fileId: Long, currentTags: List<Tag>) {
this.fileId = fileId
val assignedTagNames = currentTags.map { it.name }.toSet()

viewModelScope.launch(Dispatchers.IO) {
try {
val client = clientFactory.create(currentAccountProvider.user)
val result = GetTagsRemoteOperation().execute(client)

if (result.isSuccess) {
val assignedIds = result.resultData
.filter { it.name in assignedTagNames }
.map { it.id }
.toSet()

_uiState.update {
TagUiState.Loaded(
allTags = result.resultData,
assignedTagIds = assignedIds
)
}
} else {
_uiState.update { TagUiState.Error(R.string.failed_to_load_tags) }
}
} catch (e: ClientFactory.CreationException) {
_uiState.update { TagUiState.Error(R.string.failed_to_load_tags) }
}
}
}

fun assignTag(tag: Tag) {
viewModelScope.launch(Dispatchers.IO) {
try {
val client = clientFactory.createNextcloudClient(currentAccountProvider.user)
val result = PutTagRemoteOperation(tag.id, fileId).execute(client)
Comment thread
tobiasKaminsky marked this conversation as resolved.

if (result.isSuccess) {
_uiState.update { state ->
if (state is TagUiState.Loaded) {
state.copy(assignedTagIds = state.assignedTagIds + tag.id)
} else {
state
}
}
}
} catch (e: ClientFactory.CreationException) {
// ignore
}
}
}

fun unassignTag(tag: Tag) {
viewModelScope.launch(Dispatchers.IO) {
try {
val client = clientFactory.createNextcloudClient(currentAccountProvider.user)
val result = DeleteTagRemoteOperation(tag.id, fileId).execute(client)

if (result.isSuccess) {
_uiState.update { state ->
if (state is TagUiState.Loaded) {
state.copy(assignedTagIds = state.assignedTagIds - tag.id)
} else {
state
}
}
}
} catch (e: ClientFactory.CreationException) {
// ignore
}
}
}

fun createAndAssignTag(name: String) {
viewModelScope.launch(Dispatchers.IO) {
try {
val nextcloudClient = clientFactory.createNextcloudClient(currentAccountProvider.user)
val createResult = CreateTagRemoteOperation(name).execute(nextcloudClient)

if (createResult.isSuccess) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply fail fast princible instead of nested ifs.

val ownCloudClient = clientFactory.create(currentAccountProvider.user)
val tagsResult = GetTagsRemoteOperation().execute(ownCloudClient)

if (tagsResult.isSuccess) {
val allTags = tagsResult.resultData
val newTag = allTags.find { it.name == name }

if (newTag != null) {
PutTagRemoteOperation(newTag.id, fileId).execute(nextcloudClient)

_uiState.update { state ->
if (state is TagUiState.Loaded) {
state.copy(
allTags = allTags,
assignedTagIds = state.assignedTagIds + newTag.id
)
} else {
TagUiState.Loaded(
allTags = allTags,
assignedTagIds = setOf(newTag.id)
)
}
}
}
}
}
} catch (e: ClientFactory.CreationException) {
Log_OC.e("TagManagement", e.message)
}
}
}

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()
}
}
Loading
Loading