File indexing completed on 2024-12-22 04:41:42
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.UserInterface; 0008 0009 import android.graphics.Canvas; 0010 import android.view.LayoutInflater; 0011 import android.view.View; 0012 import android.view.ViewGroup; 0013 0014 import androidx.annotation.NonNull; 0015 import androidx.annotation.Nullable; 0016 import androidx.recyclerview.widget.ItemTouchHelper; 0017 import androidx.recyclerview.widget.RecyclerView; 0018 0019 import org.kde.kdeconnect_tp.databinding.CustomDeviceItemBinding; 0020 0021 import java.util.ArrayList; 0022 0023 public class CustomDevicesAdapter extends RecyclerView.Adapter<CustomDevicesAdapter.ViewHolder> { 0024 private ArrayList<String> customDevices; 0025 private final Callback callback; 0026 0027 CustomDevicesAdapter(@NonNull Callback callback) { 0028 this.callback = callback; 0029 0030 customDevices = new ArrayList<>(); 0031 } 0032 0033 void setCustomDevices(ArrayList<String> customDevices) { 0034 this.customDevices = customDevices; 0035 0036 notifyDataSetChanged(); 0037 } 0038 0039 @Override 0040 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { 0041 super.onAttachedToRecyclerView(recyclerView); 0042 0043 ItemTouchHelper itemTouchHelper = new ItemTouchHelper( 0044 new ItemTouchHelperCallback(adapterPos -> callback.onCustomDeviceDismissed(customDevices.get(adapterPos)))); 0045 itemTouchHelper.attachToRecyclerView(recyclerView); 0046 } 0047 0048 @NonNull 0049 @Override 0050 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 0051 CustomDeviceItemBinding itemBinding = 0052 CustomDeviceItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false); 0053 0054 return new ViewHolder(itemBinding); 0055 } 0056 0057 @Override 0058 public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 0059 holder.bind(customDevices.get(position)); 0060 } 0061 0062 @Override 0063 public int getItemCount() { 0064 return customDevices.size(); 0065 } 0066 0067 class ViewHolder extends RecyclerView.ViewHolder implements SwipeableViewHolder { 0068 private final CustomDeviceItemBinding itemBinding; 0069 0070 ViewHolder(@NonNull CustomDeviceItemBinding itemBinding) { 0071 super(itemBinding.getRoot()); 0072 this.itemBinding = itemBinding; 0073 itemBinding.deviceNameOrIP.setOnClickListener(v -> callback.onCustomDeviceClicked(customDevices.get(getAdapterPosition()))); 0074 } 0075 0076 void bind(String customDevice) { 0077 itemBinding.deviceNameOrIP.setText(customDevice); 0078 } 0079 0080 @Override 0081 public View getSwipeableView() { 0082 return itemBinding.swipeableView; 0083 } 0084 } 0085 0086 private interface SwipeableViewHolder { 0087 View getSwipeableView(); 0088 } 0089 0090 private static class ItemTouchHelperCallback extends ItemTouchHelper.Callback { 0091 @NonNull private final Callback callback; 0092 0093 private ItemTouchHelperCallback(@NonNull Callback callback) { 0094 this.callback = callback; 0095 } 0096 0097 @Override 0098 public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { 0099 return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END); 0100 } 0101 0102 @Override 0103 public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { 0104 getDefaultUIUtil().clearView(((SwipeableViewHolder)viewHolder).getSwipeableView()); 0105 } 0106 0107 @Override 0108 public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) { 0109 super.onSelectedChanged(viewHolder, actionState); 0110 0111 if (viewHolder != null) { 0112 getDefaultUIUtil().onSelected(((SwipeableViewHolder) viewHolder).getSwipeableView()); 0113 } 0114 } 0115 0116 @Override 0117 public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { 0118 getDefaultUIUtil().onDraw(c, recyclerView, ((SwipeableViewHolder)viewHolder).getSwipeableView(), dX, dY, actionState, isCurrentlyActive); 0119 } 0120 0121 @Override 0122 public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { 0123 getDefaultUIUtil().onDrawOver(c, recyclerView, ((SwipeableViewHolder)viewHolder).getSwipeableView(), dX, dY, actionState, isCurrentlyActive); 0124 } 0125 0126 @Override 0127 public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { 0128 return 0.75f; 0129 } 0130 0131 @Override 0132 public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { 0133 return false; 0134 } 0135 0136 @Override 0137 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { 0138 callback.onItemDismissed(viewHolder.getAdapterPosition()); 0139 } 0140 0141 private interface Callback { 0142 void onItemDismissed(int adapterPosition); 0143 } 0144 } 0145 0146 public interface Callback { 0147 void onCustomDeviceClicked(String customDevice); 0148 void onCustomDeviceDismissed(String customDevice); 0149 } 0150 }