|
|
|
|
@ -4,15 +4,17 @@ 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 |
|
|
|
|
import net.pokeranalytics.android.util.extensions.getMonthAndYear |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import java.util.* |
|
|
|
|
import kotlin.collections.HashMap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -23,35 +25,15 @@ import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
|
class HistoryRowRepresentableAdapter( |
|
|
|
|
var realmResults: RealmResults<Session>, |
|
|
|
|
var delegate: RowRepresentableDelegate? = null, |
|
|
|
|
var headers: ArrayList<String> |
|
|
|
|
var headers: RealmResults<Session> |
|
|
|
|
) : |
|
|
|
|
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() |
|
|
|
|
private var headersPositions = HashMap<Int, Date>() |
|
|
|
|
private lateinit var sortedHeaders: SortedMap<Int, Date> |
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
refreshData() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -72,58 +54,15 @@ class HistoryRowRepresentableAdapter( |
|
|
|
|
* Display a session view |
|
|
|
|
*/ |
|
|
|
|
inner class HeaderTitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), BindableHolder { |
|
|
|
|
fun bind(position: Int, value: String?, adapter: HistoryRowRepresentableAdapter) { |
|
|
|
|
|
|
|
|
|
fun bind(position: Int, session: Session?, adapter: HistoryRowRepresentableAdapter) { |
|
|
|
|
// Title |
|
|
|
|
itemView.findViewById<AppCompatTextView>(R.id.title)?.let { |
|
|
|
|
it.text = value ?: "" |
|
|
|
|
it.text = session?.creationDate?.getMonthAndYear() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
@ -134,28 +73,25 @@ class HistoryRowRepresentableAdapter( |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var size = 0 |
|
|
|
|
|
|
|
|
|
override fun getItemCount(): Int { |
|
|
|
|
size++ |
|
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int { |
|
|
|
|
if (sortedHeaders.containsKey(position)) { |
|
|
|
|
return RowViewType.HEADER_TITLE.ordinal |
|
|
|
|
} else { |
|
|
|
|
return RowViewType.ROW_SESSION.ordinal |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return realmResults.size + headers.size + size |
|
|
|
|
override fun getItemCount(): Int { |
|
|
|
|
return realmResults.size + headers.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) |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -163,52 +99,51 @@ class HistoryRowRepresentableAdapter( |
|
|
|
|
*/ |
|
|
|
|
private fun getRealIndex(position: Int): Int { |
|
|
|
|
|
|
|
|
|
val sorted = headersPositions2.toSortedMap() |
|
|
|
|
|
|
|
|
|
if (sorted.containsKey(position)) { |
|
|
|
|
return sorted.keys.indexOf(position) |
|
|
|
|
if (sortedHeaders.containsKey(position)) { |
|
|
|
|
// Header position |
|
|
|
|
//Timber.d("getRealIndex: Header: ${sortedHeaders.keys.indexOf(position)}") |
|
|
|
|
return sortedHeaders.keys.indexOf(position) |
|
|
|
|
} else { |
|
|
|
|
var nbHeadersBefore = 0 |
|
|
|
|
for (key in sorted.keys) { |
|
|
|
|
if (key < position) { |
|
|
|
|
nbHeadersBefore++ |
|
|
|
|
// Row position |
|
|
|
|
var headersBefore = 0 |
|
|
|
|
for (key in sortedHeaders.keys) { |
|
|
|
|
if (position > key) { |
|
|
|
|
headersBefore++ |
|
|
|
|
} else { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return position - nbHeadersBefore |
|
|
|
|
|
|
|
|
|
//Timber.d("getRealIndex: Row: $position $headersBefore") |
|
|
|
|
return position - headersBefore |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Refresh the row in the adapter |
|
|
|
|
* Refresh headers positions |
|
|
|
|
*/ |
|
|
|
|
fun refreshRow(row: RowRepresentable) { |
|
|
|
|
fun refreshData() { |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
headersPositions.clear() |
|
|
|
|
val start = System.currentTimeMillis() |
|
|
|
|
|
|
|
|
|
var previousYear = 0 |
|
|
|
|
var previousMonth = 0 |
|
|
|
|
|
|
|
|
|
val index = this.dataSource.indexForRow(row) |
|
|
|
|
if (index >= 0) { |
|
|
|
|
Timber.d("refreshRow: $index") |
|
|
|
|
notifyItemChanged(index) |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
|
|
|
|
|
for ((index, session) in realmResults.withIndex()) { |
|
|
|
|
calendar.time = session.creationDate |
|
|
|
|
if (calendar.get(Calendar.YEAR) == previousYear && calendar.get(Calendar.MONTH) < previousMonth || (calendar.get(Calendar.YEAR) < previousYear) || index == 0) { |
|
|
|
|
headersPositions[index + headersPositions.size] = session.creationDate |
|
|
|
|
previousYear = calendar.get(Calendar.YEAR) |
|
|
|
|
previousMonth = calendar.get(Calendar.MONTH) |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update UI |
|
|
|
|
*/ |
|
|
|
|
fun updateRows(diffResult: DiffUtil.DiffResult) { |
|
|
|
|
diffResult.dispatchUpdatesTo(this) |
|
|
|
|
} |
|
|
|
|
sortedHeaders = headersPositions.toSortedMap() |
|
|
|
|
|
|
|
|
|
Timber.d("Create viewTypesPositions in: ${System.currentTimeMillis() - start}ms") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |