-
-
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
4
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.
Open
edit tags #16672
Changes from all commits
Commits
Show all changes
4 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
147 changes: 147 additions & 0 deletions
147
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,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
166
app/src/main/java/com/nextcloud/ui/tags/TagManagementViewModel.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,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) | ||
|
|
||
| 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) { | ||
|
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. 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() | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.