Skip to content

[feat] #180 clip category drag and drop #181

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

Merged
merged 4 commits into from
Nov 6, 2024
Merged
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 @@ -9,45 +9,33 @@ import org.sopt.ui.view.ItemDiffCallback

class ClipEditAdapter(
private val itemClick: (Long, String, Long, String) -> Unit,
private val deleteClip: (Long) -> Unit,
private val onLongClick: (Long) -> Unit,
private val onLongClick2: (Long) -> Unit,
) : ListAdapter<Category, ClipEditViewHolder>(DiffUtil), ItemTouchHelperListener {
) : ListAdapter<Category, ClipEditViewHolder>(DiffUtil) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClipEditViewHolder {
return ClipEditViewHolder(
ItemClipEditClipBinding.inflate(LayoutInflater.from(parent.context), parent, false),
itemClick,
onLongClick,
)
}

override fun onBindViewHolder(holder: ClipEditViewHolder, position: Int) {
holder.onBind(getItem(position))
}

fun moveItem(fromPosition: Int, toPosition: Int) {
val currentList = currentList.toMutableList()
val item = currentList.removeAt(fromPosition)
currentList.add(toPosition, item)
submitList(currentList)
}

fun getCategoryId(position: Int): Long? {
return currentList[position].categoryId
}

companion object {
private val DiffUtil = ItemDiffCallback<Category>(
onItemsTheSame = { old, new -> old.categoryId == new.categoryId },
onContentsTheSame = { old, new -> old == new },
)
}

override fun onItemMove(from: Int, to: Int) {
val item: Category? = currentList[from]
val newList = ArrayList<Category>()
newList.addAll(currentList)
newList.removeAt(from)
if (item != null) {
newList.add(to, item)
}
notifyItemMoved(from, to)
onLongClick2(to.toLong())
}

override fun onItemSwipe(position: Int) {
val newList = ArrayList(currentList)
deleteClip(newList[position].categoryId ?: 0)
newList.removeAt(position)
notifyItemRemoved(position)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,33 @@ import org.sopt.ui.view.onThrottleClick
class ClipEditFragment : BindingFragment<FragmentClipEditBinding>({ FragmentClipEditBinding.inflate(it) }) {
private val viewModel: ClipEditViewModel by viewModels()
private lateinit var clipEditAdapter: ClipEditAdapter
private val itemTouchHelper by lazy {
ItemTouchHelper(ItemTouchCallback(clipEditAdapter))
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

clipEditAdapter = ClipEditAdapter(
{ itemId, state, position, title ->
when (state) {
"delete" -> {
showDeleteDialog(itemId, title)
}
clipEditAdapter = ClipEditAdapter { itemId, state, position, title ->
when (state) {
"delete" -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

hard

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์ด๊ฑฐ ๋‚ด๊ฐ€ ํ•œ๊ฑฐ ์•„๋‹ˆ์—์š”

showDeleteDialog(itemId, title)
}

"edit" -> {
showHomeBottomSheet(itemId, title)
}
"edit" -> {
showHomeBottomSheet(itemId, title)
}
},
deleteClip = {
viewModel.deleteCategory(it)
},
onLongClick = {
viewModel.last2.flowWithLifecycle(viewLifeCycle).onEach { state ->
when (state) {
is UiState.Success -> {
viewModel.patchCategoryEditPriority(it, state.data + 1)
}

else -> {}
}
}.launchIn(viewLifeCycleScope)
},
onLongClick2 = {
viewModel.update2(it.toInt())
},
)
}
}
binding.rvClipEdit.adapter = clipEditAdapter
itemTouchHelper.attachToRecyclerView(binding.rvClipEdit)
val callback = ItemTouchCallback(
adapter = clipEditAdapter,
recyclerView = binding.rvClipEdit,
) { categoryId, newIndex ->
if (categoryId != null) viewModel.updateCategoryEditPriorityState(categoryId, newIndex)
}
ItemTouchHelper(callback).attachToRecyclerView(binding.rvClipEdit)
updateEditListView()
updateDelete()
onClickBackButton()
editCategoryPriority()
}

private fun updateEditListView() {
Expand Down Expand Up @@ -117,7 +102,7 @@ class ClipEditFragment : BindingFragment<FragmentClipEditBinding>({ FragmentClip
}
}

fun editCategoryTitle() {
private fun editCategoryTitle() {
viewModel.editTitleState.flowWithLifecycle(viewLifeCycle).onEach { state ->
when (state) {
is UiState.Success -> {
Expand All @@ -144,4 +129,16 @@ class ClipEditFragment : BindingFragment<FragmentClipEditBinding>({ FragmentClip
}
.show()
}

private fun editCategoryPriority() {
viewModel.categoryEditPriorityState.flowWithLifecycle(viewLifeCycle).onEach { state ->
when (state) {
is UiState.Success -> {
viewModel.patchCategoryEditPriority(state.data.first, state.data.second)
}

else -> {}
}
}.launchIn(viewLifeCycleScope)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.sopt.model.category.Category
class ClipEditViewHolder(
private val binding: ItemClipEditClipBinding,
private val onClickItemClip: (Long, String, Long, String) -> Unit,
private val onLongClick: (Long) -> Unit,
) : RecyclerView.ViewHolder(binding.root) {
fun onBind(clipData: Category) {
if (clipData == null) return
Expand All @@ -29,10 +28,6 @@ class ClipEditViewHolder(
onClickItemClip(clipData.categoryId!!.toLong(), "delete", itemId, clipData.categoryTitle ?: "")
}
}
root.setOnLongClickListener {
onLongClick(clipData.categoryId!!.toLong())
true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ class ClipEditViewModel @Inject constructor(
private val _editTitleState = MutableStateFlow<UiState<Unit>>(UiState.Empty)
val editTitleState: StateFlow<UiState<Unit>> = _editTitleState.asStateFlow()

private val _last2 = MutableStateFlow<UiState<Int>>(UiState.Empty)
val last2: StateFlow<UiState<Int>> = _last2.asStateFlow()

fun update2(a: Int) = viewModelScope.launch {
_last2.emit(UiState.Success(a))
}
private val _categoryEditPriorityState = MutableStateFlow<UiState<Pair<Long, Int>>>(UiState.Empty)
val categoryEditPriorityState: StateFlow<UiState<Pair<Long, Int>>> = _categoryEditPriorityState.asStateFlow()

init {
getCategoryAll()
}

fun updateCategoryEditPriorityState(clipId: Long, newIndex: Int) = viewModelScope.launch {
_categoryEditPriorityState.emit(UiState.Success(Pair(clipId, newIndex)))
}

fun getCategoryAll() = viewModelScope.launch {
getCategoryAll.invoke().onSuccess {
val allCategoryList = listOf<Category>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@ package org.sopt.clip.clipedit
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class ItemTouchCallback(private val listener: ItemTouchHelperListener) : ItemTouchHelper.Callback() {
class ItemTouchCallback(
private val adapter: ClipEditAdapter,
private val recyclerView: RecyclerView,
private val onMoveItem: (Long?, Int) -> Unit,
) : ItemTouchHelper.Callback() {

override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
val swipeFlags = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
return makeMovementFlags(dragFlags, swipeFlags)
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
): Int {
val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.START or ItemTouchHelper.END
return makeMovementFlags(dragFlags, 0)
}

override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
listener.onItemMove(viewHolder.adapterPosition, target.adapterPosition)
return false
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
val fromPosition = viewHolder.bindingAdapterPosition
val toPosition = target.bindingAdapterPosition
adapter.moveItem(fromPosition, toPosition)
return true
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener.onItemSwipe(viewHolder.layoutPosition)
}

override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
onMoveItem(adapter.getCategoryId(viewHolder.bindingAdapterPosition), viewHolder.bindingAdapterPosition)
viewHolder.itemView.animate().scaleX(1.0f).scaleY(1.0f).setDuration(200).start()
}

override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
viewHolder?.itemView?.animate()?.scaleX(1.1f)?.scaleY(1.1f)?.setDuration(200)?.start()
}
}

This file was deleted.

1 change: 1 addition & 0 deletions feature/clip/src/main/res/layout/fragment_clip_edit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@
android:layout_width="match_parent"
android:layout_height="20dp"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Loading