|
|
|
@ -1,23 +1,49 @@ |
|
|
|
package net.pokeranalytics.android.ui.adapter.components |
|
|
|
package net.pokeranalytics.android.ui.adapter.components |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.inputmethodservice.Keyboard |
|
|
|
import android.view.View |
|
|
|
import android.view.View |
|
|
|
import android.view.ViewGroup |
|
|
|
import android.view.ViewGroup |
|
|
|
import androidx.recyclerview.widget.DiffUtil |
|
|
|
import androidx.recyclerview.widget.DiffUtil |
|
|
|
import androidx.recyclerview.widget.RecyclerView |
|
|
|
import androidx.recyclerview.widget.RecyclerView |
|
|
|
import net.pokeranalytics.android.ui.view.BindableHolder |
|
|
|
import net.pokeranalytics.android.ui.view.* |
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback |
|
|
|
interface RowRepresentableDataSource : DisplayableDataSource { |
|
|
|
import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
fun rowRepresentableForPosition(position:Int): RowRepresentable { |
|
|
|
|
|
|
|
if (this.numberOfRows() > position) { |
|
|
|
|
|
|
|
return this.adapterRows()[position] |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw IllegalStateException("Need to implement Data Source") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun numberOfRows(): Int { |
|
|
|
|
|
|
|
return this.adapterRows().size |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun viewTypeForPosition(position:Int): Int { |
|
|
|
|
|
|
|
return this.rowRepresentableForPosition(position).viewType |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun indexForRow(row:RowRepresentable): Int { |
|
|
|
|
|
|
|
return this.adapterRows().indexOf(row) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface RowRepresentableDelegate : DisplayableDelegate { |
|
|
|
|
|
|
|
fun onIndexSelected(position: Int) {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* An interface used to provide RowRepresentableAdapter content and value in the form of rows |
|
|
|
* An interface used to provide RowRepresentableAdapter content and value in the form of rows |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
interface RowRepresentableDataSource { |
|
|
|
interface DisplayableDataSource { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns a list of rows |
|
|
|
* Returns a list of rows |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun adapterRows(): ArrayList<RowRepresentable> |
|
|
|
fun adapterRows(): ArrayList<RowRepresentable> { |
|
|
|
|
|
|
|
return ArrayList() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns a boolean for a specific row |
|
|
|
* Returns a boolean for a specific row |
|
|
|
@ -48,39 +74,29 @@ interface RowRepresentableDataSource { |
|
|
|
* - switch (bool) |
|
|
|
* - switch (bool) |
|
|
|
* - static content |
|
|
|
* - static content |
|
|
|
* */ |
|
|
|
* */ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A delegate used to propagate UI actions |
|
|
|
* A delegate used to propagate UI actions |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
interface RowRepresentableDelegate { |
|
|
|
interface DisplayableDelegate { |
|
|
|
fun onRowSelected(row: RowRepresentable) {} |
|
|
|
fun onRowSelected(row: RowRepresentable) {} |
|
|
|
fun onActionSelected(row: RowRepresentable) {} |
|
|
|
fun onActionSelected(row: RowRepresentable) {} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* An adapter capable of displaying a list of RowRepresentables |
|
|
|
* An adapter capable of displaying a list of RowRepresentables |
|
|
|
* @param rowRepresentableDataSource the datasource providing rows |
|
|
|
* @param dataSource the datasource providing rows |
|
|
|
* @param rowRepresentableDelegate the delegate, notified of UI actions |
|
|
|
* @param delegate the delegate, notified of UI actions |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class RowRepresentableAdapter( |
|
|
|
class RowRepresentableAdapter( |
|
|
|
var rowRepresentableDataSource: RowRepresentableDataSource, |
|
|
|
var dataSource: RowRepresentableDataSource, |
|
|
|
var rowRepresentableDelegate: RowRepresentableDelegate? = null |
|
|
|
var delegate: RowRepresentableDelegate? = null |
|
|
|
) : |
|
|
|
) : |
|
|
|
RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
|
|
|
RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The list of rows to display |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private var rows: ArrayList<RowRepresentable> = ArrayList() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
init { |
|
|
|
|
|
|
|
this.rows = rowRepresentableDataSource.adapterRows() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int { |
|
|
|
override fun getItemViewType(position: Int): Int { |
|
|
|
return this.rows[position].viewType |
|
|
|
return this.dataSource.viewTypeForPosition(position) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { |
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { |
|
|
|
@ -89,28 +105,29 @@ class RowRepresentableAdapter( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun getItemCount(): Int { |
|
|
|
override fun getItemCount(): Int { |
|
|
|
return this.rows.size |
|
|
|
return this.dataSource.numberOfRows() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
|
|
|
val dynamicRow = this.rows[position] |
|
|
|
val dynamicRow = this.dataSource.rowRepresentableForPosition(position) |
|
|
|
|
|
|
|
|
|
|
|
val listener = View.OnClickListener { |
|
|
|
val listener = View.OnClickListener { |
|
|
|
rowRepresentableDelegate?.onRowSelected(dynamicRow) |
|
|
|
delegate?.onRowSelected(dynamicRow) |
|
|
|
|
|
|
|
delegate?.onIndexSelected(position) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val actionListener = View.OnClickListener { |
|
|
|
val actionListener = View.OnClickListener { |
|
|
|
rowRepresentableDelegate?.onActionSelected(dynamicRow) |
|
|
|
delegate?.onActionSelected(dynamicRow) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
(holder as BindableHolder).bind(dynamicRow, this.rowRepresentableDataSource, listener, actionListener) |
|
|
|
(holder as BindableHolder).bind(dynamicRow, this.dataSource, listener, actionListener) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Refresh the row in the adapter |
|
|
|
* Refresh the row in the adapter |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun refreshRow(row: RowRepresentable) { |
|
|
|
fun refreshRow(row: RowRepresentable) { |
|
|
|
val index = rows.indexOf(row) |
|
|
|
val index = this.dataSource.indexForRow(row) |
|
|
|
if (index >= 0) { |
|
|
|
if (index >= 0) { |
|
|
|
notifyItemChanged(index) |
|
|
|
notifyItemChanged(index) |
|
|
|
} |
|
|
|
} |
|
|
|
@ -120,9 +137,11 @@ class RowRepresentableAdapter( |
|
|
|
* Update UI |
|
|
|
* Update UI |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun updateRows(newRows: ArrayList<RowRepresentable>) { |
|
|
|
fun updateRows(newRows: ArrayList<RowRepresentable>) { |
|
|
|
val diffResult = DiffUtil.calculateDiff(RowRepresentableDiffCallback(newRows, rows, rowRepresentableDataSource)) |
|
|
|
/* |
|
|
|
|
|
|
|
val diffResult = DiffUtil.calculateDiff(RowRepresentableDiffCallback(newRows, rows, dataSource)) |
|
|
|
this.rows = newRows |
|
|
|
this.rows = newRows |
|
|
|
diffResult.dispatchUpdatesTo(this) |
|
|
|
diffResult.dispatchUpdatesTo(this) |
|
|
|
|
|
|
|
*/ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |