|
|
|
@ -12,19 +12,19 @@ import net.pokeranalytics.android.model.realm.handhistory.HandHistory |
|
|
|
import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
import net.pokeranalytics.android.util.NULL_TEXT |
|
|
|
import net.pokeranalytics.android.util.NULL_TEXT |
|
|
|
import net.pokeranalytics.android.util.extensions.getMonthAndYear |
|
|
|
import net.pokeranalytics.android.util.extensions.getMonthAndYear |
|
|
|
|
|
|
|
import timber.log.Timber |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
import kotlin.collections.HashMap |
|
|
|
import kotlin.collections.HashMap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* An adapter capable of displaying a list of RowRepresentables |
|
|
|
* An adapter capable of displaying a list of RowRepresentables |
|
|
|
* @param dataSource the datasource providing rows |
|
|
|
|
|
|
|
* @param delegate the delegate, notified of UI actions |
|
|
|
* @param delegate the delegate, notified of UI actions |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
var delegate: RowRepresentableDelegate? = null, |
|
|
|
var delegate: RowRepresentableDelegate? = null, |
|
|
|
var realmHandHistories: RealmResults<HandHistory>, |
|
|
|
private var realmHandHistories: RealmResults<HandHistory>, |
|
|
|
var distinctHandHistoryHeaders: RealmResults<HandHistory> |
|
|
|
private var distinctHandHistoryHeaders: RealmResults<HandHistory> |
|
|
|
) : |
|
|
|
) : |
|
|
|
RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
|
|
|
RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
|
|
|
|
|
|
|
|
|
|
|
@ -56,6 +56,7 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Display a header |
|
|
|
* Display a header |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -80,9 +81,8 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int { |
|
|
|
override fun getItemViewType(position: Int): Int { |
|
|
|
return if (sortedHeaders.containsKey(position)) { |
|
|
|
return if (this.sortedHeaders.containsKey(position)) { |
|
|
|
RowViewType.HEADER_TITLE.ordinal |
|
|
|
RowViewType.HEADER_TITLE.ordinal |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 1 |
|
|
|
// 1 |
|
|
|
@ -91,7 +91,7 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun getItemCount(): Int { |
|
|
|
override fun getItemCount(): Int { |
|
|
|
return realmHandHistories.size + distinctHandHistoryHeaders.size |
|
|
|
return this.realmHandHistories.size + this.distinctHandHistoryHeaders.size |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
|
|
|
@ -106,9 +106,9 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
* Return the header |
|
|
|
* Return the header |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun getHeaderForPosition(position: Int): String { |
|
|
|
private fun getHeaderForPosition(position: Int): String { |
|
|
|
if (sortedHeaders.containsKey(position)) { |
|
|
|
if (this.sortedHeaders.containsKey(position)) { |
|
|
|
val realmHeaderPosition = sortedHeaders.keys.indexOf(position) |
|
|
|
val realmHeaderPosition = this.sortedHeaders.keys.indexOf(position) |
|
|
|
return distinctHandHistoryHeaders[realmHeaderPosition]?.date?.getMonthAndYear() ?: "" |
|
|
|
return this.distinctHandHistoryHeaders[realmHeaderPosition]?.date?.getMonthAndYear() ?: "" |
|
|
|
} |
|
|
|
} |
|
|
|
return NULL_TEXT |
|
|
|
return NULL_TEXT |
|
|
|
} |
|
|
|
} |
|
|
|
@ -120,7 +120,7 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
|
|
|
|
|
|
|
|
// Row position |
|
|
|
// Row position |
|
|
|
var headersBefore = 0 |
|
|
|
var headersBefore = 0 |
|
|
|
for (key in sortedHeaders.keys) { |
|
|
|
for (key in this.sortedHeaders.keys) { |
|
|
|
if (position > key) { |
|
|
|
if (position > key) { |
|
|
|
headersBefore++ |
|
|
|
headersBefore++ |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
@ -128,7 +128,7 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return realmHandHistories[position - headersBefore] |
|
|
|
return this.realmHandHistories[position - headersBefore] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -136,7 +136,7 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun refreshData() { |
|
|
|
fun refreshData() { |
|
|
|
|
|
|
|
|
|
|
|
headersPositions.clear() |
|
|
|
this.headersPositions.clear() |
|
|
|
|
|
|
|
|
|
|
|
var previousYear = Int.MAX_VALUE |
|
|
|
var previousYear = Int.MAX_VALUE |
|
|
|
var previousMonth = Int.MAX_VALUE |
|
|
|
var previousMonth = Int.MAX_VALUE |
|
|
|
@ -144,22 +144,24 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
|
|
|
|
|
|
|
// Add headers if the date doesn't exist yet |
|
|
|
// Add headers if the date doesn't exist yet |
|
|
|
for ((index, handHistory) in realmHandHistories.withIndex()) { |
|
|
|
for ((index, handHistory) in this.realmHandHistories.withIndex()) { |
|
|
|
calendar.time = handHistory.date |
|
|
|
calendar.time = handHistory.date |
|
|
|
if (checkHeaderCondition(calendar, previousYear, previousMonth)) { |
|
|
|
if (checkHeaderCondition(calendar, previousYear, previousMonth)) { |
|
|
|
headersPositions[index + headersPositions.size] = handHistory.date |
|
|
|
this.headersPositions[index + this.headersPositions.size] = handHistory.date |
|
|
|
previousYear = calendar.get(Calendar.YEAR) |
|
|
|
previousYear = calendar.get(Calendar.YEAR) |
|
|
|
previousMonth = calendar.get(Calendar.MONTH) |
|
|
|
previousMonth = calendar.get(Calendar.MONTH) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sortedHeaders = headersPositions.toSortedMap() |
|
|
|
this.sortedHeaders = this.headersPositions.toSortedMap() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Timber.d("]]] this.sortedHeaders = ${this.sortedHeaders}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Check if we need to add a header |
|
|
|
* Check if we need to add a header |
|
|
|
* Can be change to manage different condition |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun checkHeaderCondition(currentCalendar: Calendar, previousYear: Int, previousMonth: Int): Boolean { |
|
|
|
private fun checkHeaderCondition(currentCalendar: Calendar, previousYear: Int, previousMonth: Int): Boolean { |
|
|
|
return currentCalendar.get(Calendar.YEAR) == previousYear && currentCalendar.get(Calendar.MONTH) < previousMonth || (currentCalendar.get( |
|
|
|
return currentCalendar.get(Calendar.YEAR) == previousYear && currentCalendar.get(Calendar.MONTH) < previousMonth || (currentCalendar.get( |
|
|
|
@ -167,5 +169,4 @@ class FeedHandHistoryRowRepresentableAdapter( |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |