parent
fe43a537a6
commit
29ef32cced
@ -0,0 +1,214 @@ |
|||||||
|
package net.pokeranalytics.android.ui.adapter |
||||||
|
|
||||||
|
import android.view.LayoutInflater |
||||||
|
import android.view.View |
||||||
|
import android.view.ViewGroup |
||||||
|
import androidx.appcompat.widget.AppCompatTextView |
||||||
|
import androidx.recyclerview.widget.DiffUtil |
||||||
|
import androidx.recyclerview.widget.RecyclerView |
||||||
|
import io.realm.RealmResults |
||||||
|
import kotlinx.android.synthetic.main.row_history_session.view.* |
||||||
|
import net.pokeranalytics.android.R |
||||||
|
import net.pokeranalytics.android.model.realm.Session |
||||||
|
import net.pokeranalytics.android.ui.view.BindableHolder |
||||||
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||||
|
import net.pokeranalytics.android.ui.view.RowViewType |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* An adapter capable of displaying a list of RowRepresentables |
||||||
|
* @param dataSource the datasource providing rows |
||||||
|
* @param delegate the delegate, notified of UI actions |
||||||
|
*/ |
||||||
|
class HistoryRowRepresentableAdapter( |
||||||
|
var realmResults: RealmResults<Session>, |
||||||
|
var delegate: RowRepresentableDelegate? = null, |
||||||
|
var headers: ArrayList<String> |
||||||
|
) : |
||||||
|
RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
||||||
|
|
||||||
|
private var headersPositions: HashMap<String, Int> = HashMap() |
||||||
|
private var headersDisplayed: ArrayList<String> = ArrayList() |
||||||
|
private var headersPositions2: HashMap<Int, String> = HashMap() |
||||||
|
|
||||||
|
private var viewTypesPositions: ArrayList<Int> = ArrayList() |
||||||
|
|
||||||
|
init { |
||||||
|
|
||||||
|
val start = System.currentTimeMillis() |
||||||
|
|
||||||
|
headersPositions2.clear() |
||||||
|
viewTypesPositions.clear() |
||||||
|
|
||||||
|
/* |
||||||
|
for (session in realmResults) { |
||||||
|
if (!headersPositions2.containsValue(session.groupByMonth)) { |
||||||
|
headersPositions2[viewTypesPositions.size] = session.groupByMonth |
||||||
|
viewTypesPositions.add(RowViewType.HEADER_TITLE.ordinal) |
||||||
|
} |
||||||
|
viewTypesPositions.add(RowViewType.ROW_SESSION.ordinal) |
||||||
|
} |
||||||
|
|
||||||
|
Timber.d("Create viewTypesPositions in: ${System.currentTimeMillis() - start}ms") |
||||||
|
*/ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Display a session view |
||||||
|
*/ |
||||||
|
inner class RowSessionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), BindableHolder { |
||||||
|
fun bind(position: Int, row: Session?, adapter: HistoryRowRepresentableAdapter) { |
||||||
|
|
||||||
|
itemView.sessionRow.setData(row as Session) |
||||||
|
val listener = View.OnClickListener { |
||||||
|
adapter.delegate?.onRowSelected(position, row) |
||||||
|
} |
||||||
|
itemView.sessionRow.setOnClickListener(listener) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Display a session view |
||||||
|
*/ |
||||||
|
inner class HeaderTitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), BindableHolder { |
||||||
|
fun bind(position: Int, value: String?, adapter: HistoryRowRepresentableAdapter) { |
||||||
|
|
||||||
|
// Title |
||||||
|
itemView.findViewById<AppCompatTextView>(R.id.title)?.let { |
||||||
|
it.text = value ?: "" |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun getItemViewType(position: Int): Int { |
||||||
|
|
||||||
|
return viewTypesPositions[position] |
||||||
|
|
||||||
|
/* |
||||||
|
if (position < headers.size) { |
||||||
|
return RowViewType.HEADER_TITLE.ordinal |
||||||
|
} else { |
||||||
|
return RowViewType.ROW_SESSION.ordinal |
||||||
|
}*/ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* |
||||||
|
|
||||||
|
var nbHeaders = 0 |
||||||
|
for (key in headersPositions2.keys) { |
||||||
|
if (position < key) { |
||||||
|
nbHeaders++ |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
val realIndex = position - nbHeaders |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//val realIndex = getRealIndex(position)//position - headersPositions.size |
||||||
|
val month = realmResults[realIndex]?.groupByMonth ?: "" |
||||||
|
|
||||||
|
if (headersPositions.containsKey(month) || headersPositions[month] == position) { |
||||||
|
return RowViewType.ROW_SESSION.ordinal//this.dataSource.viewTypeForPosition(position) |
||||||
|
} else { |
||||||
|
headersPositions[month] = position |
||||||
|
return RowViewType.HEADER_TITLE.ordinal |
||||||
|
} |
||||||
|
*/ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { |
||||||
|
//val rowViewType: RowViewType = RowViewType.values()[viewType] |
||||||
|
//return rowViewType.viewHolder(parent) |
||||||
|
|
||||||
|
if (viewType == RowViewType.ROW_SESSION.ordinal) { |
||||||
|
val layout = LayoutInflater.from(parent.context).inflate(R.layout.row_history_session, parent, false) |
||||||
|
return RowSessionViewHolder(layout) |
||||||
|
} else { |
||||||
|
val layout = LayoutInflater.from(parent.context).inflate(R.layout.row_header_title, parent, false) |
||||||
|
return HeaderTitleViewHolder(layout) |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
var size = 0 |
||||||
|
|
||||||
|
override fun getItemCount(): Int { |
||||||
|
size++ |
||||||
|
|
||||||
|
|
||||||
|
return realmResults.size + headers.size + size |
||||||
|
} |
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
||||||
|
if (holder is RowSessionViewHolder) { |
||||||
|
holder.bind(position, realmResults[getRealIndex(position)], this) |
||||||
|
|
||||||
|
} else if (holder is HeaderTitleViewHolder) { |
||||||
|
holder.bind(position, headers[getRealIndex(position)], this) |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
this.dataSource.rowRepresentableForPosition(position)?.let { |
||||||
|
(holder as RowSessionViewHolder).bind(position, getItem(position), this) |
||||||
|
} |
||||||
|
*/ |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get real index |
||||||
|
*/ |
||||||
|
private fun getRealIndex(position: Int) : Int { |
||||||
|
|
||||||
|
val sorted = headersPositions2.toSortedMap() |
||||||
|
|
||||||
|
if (sorted.containsKey(position)) { |
||||||
|
return sorted.keys.indexOf(position) |
||||||
|
} else { |
||||||
|
var nbHeadersBefore = 0 |
||||||
|
for (key in sorted.keys) { |
||||||
|
if (key < position) { |
||||||
|
nbHeadersBefore++ |
||||||
|
} else { |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
return position - nbHeadersBefore |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Refresh the row in the adapter |
||||||
|
*/ |
||||||
|
fun refreshRow(row: RowRepresentable) { |
||||||
|
|
||||||
|
/* |
||||||
|
if (row.viewType == RowViewType.TITLE_SWITCH.ordinal) { |
||||||
|
// Avoid to refresh the view because it will refresh itself |
||||||
|
// Caution if we want to update the title for example |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
val index = this.dataSource.indexForRow(row) |
||||||
|
if (index >= 0) { |
||||||
|
Timber.d("refreshRow: $index") |
||||||
|
notifyItemChanged(index) |
||||||
|
} |
||||||
|
*/ |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update UI |
||||||
|
*/ |
||||||
|
fun updateRows(diffResult: DiffUtil.DiffResult) { |
||||||
|
diffResult.dispatchUpdatesTo(this) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue