| /** |
| * Copyright (C) 2023 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| * in compliance with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software distributed under the License |
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| * or implied. See the License for the specific language governing permissions and limitations under |
| * the License. |
| */ |
| package com.android.healthconnect.controller.datasources.appsources |
| |
| import android.graphics.Canvas |
| import androidx.recyclerview.widget.ItemTouchHelper |
| import androidx.recyclerview.widget.RecyclerView |
| |
| class AppSourcesItemMoveCallback(private val adapter: AppSourcesAdapter) : |
| ItemTouchHelper.Callback() { |
| |
| override fun isLongPressDragEnabled(): Boolean = false |
| |
| override fun isItemViewSwipeEnabled(): Boolean = false |
| |
| override fun getMovementFlags( |
| recyclerView: RecyclerView, |
| viewHolder: RecyclerView.ViewHolder |
| ): Int { |
| val dragFlags: Int = ItemTouchHelper.UP or ItemTouchHelper.DOWN |
| return makeMovementFlags(dragFlags, /* swipeFlags= */ 0) |
| } |
| |
| override fun onMove( |
| recyclerView: RecyclerView, |
| viewHolder: RecyclerView.ViewHolder, |
| target: RecyclerView.ViewHolder |
| ): Boolean { |
| return adapter.onItemMove(viewHolder.bindingAdapterPosition, target.bindingAdapterPosition) |
| } |
| |
| override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {} |
| |
| override fun onChildDraw( |
| c: Canvas, |
| recyclerView: RecyclerView, |
| viewHolder: RecyclerView.ViewHolder, |
| dX: Float, |
| dY: Float, |
| actionState: Int, |
| isCurrentlyActive: Boolean |
| ) { |
| val topY: Float = viewHolder.itemView.top + dY |
| val bottomY: Float = topY + viewHolder.itemView.height |
| |
| // Only redraw child if it is inbounds of view |
| if (topY > 0 && bottomY < recyclerView.height) { |
| super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive) |
| } |
| } |
| } |