databinding: simplify and address warnings

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-03-27 03:58:35 -06:00 committed by Harsh Shandilya
parent 536a6f3f83
commit 918d9b8b1f
4 changed files with 9 additions and 30 deletions

View File

@ -90,7 +90,7 @@ object BindingAdapters {
@BindingAdapter(requireAll = false, value = ["items", "layout", "configurationHandler"]) @BindingAdapter(requireAll = false, value = ["items", "layout", "configurationHandler"])
fun <K, E : Keyed<out K>> setItems(view: RecyclerView, fun <K, E : Keyed<out K>> setItems(view: RecyclerView,
oldList: ObservableKeyedArrayList<K, E>?, oldLayoutId: Int, oldList: ObservableKeyedArrayList<K, E>?, oldLayoutId: Int,
oldRowConfigurationHandler: RowConfigurationHandler<*, *>?, @Suppress("UNUSED_PARAMETER") oldRowConfigurationHandler: RowConfigurationHandler<*, *>?,
newList: ObservableKeyedArrayList<K, E>?, newLayoutId: Int, newList: ObservableKeyedArrayList<K, E>?, newLayoutId: Int,
newRowConfigurationHandler: RowConfigurationHandler<*, *>?) { newRowConfigurationHandler: RowConfigurationHandler<*, *>?) {
if (view.layoutManager == null) if (view.layoutManager == null)
@ -98,7 +98,7 @@ object BindingAdapters {
if (oldList === newList && oldLayoutId == newLayoutId) if (oldList === newList && oldLayoutId == newLayoutId)
return return
// The ListAdapter interface is not generic, so this cannot be checked. // The ListAdapter interface is not generic, so this cannot be checked.
var adapter = view.adapter as ObservableKeyedRecyclerViewAdapter<K, E>? @Suppress("UNCHECKED_CAST") var adapter = view.adapter as? ObservableKeyedRecyclerViewAdapter<K, E>?
// If the layout changes, any existing adapter must be replaced. // If the layout changes, any existing adapter must be replaced.
if (adapter != null && oldList != null && oldLayoutId != newLayoutId) { if (adapter != null && oldList != null && oldLayoutId != newLayoutId) {
adapter.setList(null) adapter.setList(null)

View File

@ -4,6 +4,7 @@
*/ */
package com.wireguard.android.databinding package com.wireguard.android.databinding
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
@ -55,6 +56,7 @@ class ObservableKeyedRecyclerViewAdapter<K, E : Keyed<out K>> internal construct
} }
fun setRowConfigurationHandler(rowConfigurationHandler: RowConfigurationHandler<*, *>?) { fun setRowConfigurationHandler(rowConfigurationHandler: RowConfigurationHandler<*, *>?) {
@Suppress("UNCHECKED_CAST")
this.rowConfigurationHandler = rowConfigurationHandler as? RowConfigurationHandler<ViewDataBinding, Any> this.rowConfigurationHandler = rowConfigurationHandler as? RowConfigurationHandler<ViewDataBinding, Any>
} }

View File

@ -16,7 +16,7 @@ import java.util.Spliterator
* array-based nature of this class, insertion and removal of elements with anything but the largest * array-based nature of this class, insertion and removal of elements with anything but the largest
* key still require O(n) time. * key still require O(n) time.
*/ */
class ObservableSortedKeyedArrayList<K, E : Keyed<out K>>(private val comparator: Comparator<in K>?) : ObservableKeyedArrayList<K, E>() { class ObservableSortedKeyedArrayList<K, E : Keyed<out K>>(private val comparator: Comparator<in K>) : ObservableKeyedArrayList<K, E>() {
@Transient @Transient
private val keyList = KeyList(this) private val keyList = KeyList(this)
@ -54,34 +54,15 @@ class ObservableSortedKeyedArrayList<K, E : Keyed<out K>>(private val comparator
return true return true
} }
private fun getInsertionPoint(e: E): Int { private fun getInsertionPoint(e: E) = -Collections.binarySearch(keyList, e.key, comparator) - 1
return if (comparator != null) {
-Collections.binarySearch(keyList, e.key, comparator) - 1
} else {
val list = keyList as List<Comparable<K>>
-Collections.binarySearch(list, e.key) - 1
}
}
override fun indexOfKey(key: K): Int { override fun indexOfKey(key: K): Int {
val index: Int val index = Collections.binarySearch(keyList, key, comparator)
index = if (comparator != null) {
Collections.binarySearch(keyList, key, comparator)
} else {
val list = keyList as List<Comparable<K>>
Collections.binarySearch(list, key)
}
return if (index >= 0) index else -1 return if (index >= 0) index else -1
} }
override fun set(index: Int, element: E): E { override fun set(index: Int, element: E): E {
val order: Int val order = comparator.compare(element.key, get(index).key)
order = if (comparator != null) {
comparator.compare(element.key, get(index).key)
} else {
val key = element.key as Comparable<K>
key.compareTo(get(index).key)
}
if (order != 0) { if (order != 0) {
// Allow replacement if the new key would be inserted adjacent to the replaced element. // Allow replacement if the new key would be inserted adjacent to the replaced element.
val insertionPoint = getInsertionPoint(element) val insertionPoint = getInsertionPoint(element)
@ -91,10 +72,6 @@ class ObservableSortedKeyedArrayList<K, E : Keyed<out K>>(private val comparator
return super.set(index, element) return super.set(index, element)
} }
fun values(): Collection<E> {
return this
}
private class KeyList<K, E : Keyed<out K>>(private val list: ObservableSortedKeyedArrayList<K, E>) : AbstractList<K>(), Set<K> { private class KeyList<K, E : Keyed<out K>>(private val list: ObservableSortedKeyedArrayList<K, E>) : AbstractList<K>(), Set<K> {
override fun get(index: Int): K = list[index].key override fun get(index: Int): K = list[index].key

View File

@ -35,7 +35,7 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P
} }
Application.getAsyncWorker().runAsync { Application.getAsyncWorker().runAsync {
Application.getTunnelManager().tunnels.thenApply { observableTunnels -> Application.getTunnelManager().tunnels.thenApply { observableTunnels ->
val downings = observableTunnels.values().map { it.setStateAsync(Tunnel.State.DOWN).toCompletableFuture() }.toTypedArray() val downings = observableTunnels.map { it.setStateAsync(Tunnel.State.DOWN).toCompletableFuture() }.toTypedArray()
CompletableFuture.allOf(*downings).thenRun { CompletableFuture.allOf(*downings).thenRun {
val restartIntent = Intent(context, SettingsActivity::class.java) val restartIntent = Intent(context, SettingsActivity::class.java)
restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)