1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.virtualization.terminal 17 18 import androidx.recyclerview.widget.RecyclerView 19 import androidx.recyclerview.widget.SortedList 20 import androidx.recyclerview.widget.SortedListAdapterCallback 21 22 abstract class SettingsPortForwardingBaseAdapter<T : RecyclerView.ViewHolder>() : 23 RecyclerView.Adapter<T>() { 24 var items: SortedList<SettingsPortForwardingItem> 25 26 init { 27 items = 28 SortedList( 29 SettingsPortForwardingItem::class.java, 30 object : SortedListAdapterCallback<SettingsPortForwardingItem>(this) { comparenull31 override fun compare( 32 o1: SettingsPortForwardingItem, 33 o2: SettingsPortForwardingItem, 34 ): Int { 35 return o1.port - o2.port 36 } 37 areContentsTheSamenull38 override fun areContentsTheSame( 39 o1: SettingsPortForwardingItem, 40 o2: SettingsPortForwardingItem, 41 ): Boolean { 42 return o1.port == o2.port && o1.enabled == o2.enabled 43 } 44 areItemsTheSamenull45 override fun areItemsTheSame( 46 o1: SettingsPortForwardingItem, 47 o2: SettingsPortForwardingItem, 48 ): Boolean { 49 return o1.port == o2.port 50 } 51 }, 52 ) 53 } 54 getItemCountnull55 override fun getItemCount() = items.size() 56 57 abstract fun getItems(): ArrayList<SettingsPortForwardingItem> 58 59 fun refreshItems() { 60 items.replaceAll(getItems()) 61 } 62 } 63