Laurent 6 years ago
parent 88ebaba62f
commit e6d1160e35
  1. 35
      app/src/main/java/net/pokeranalytics/android/ui/adapter/FeedHandHistoryRowRepresentableAdapter.kt
  2. 5
      app/src/main/java/net/pokeranalytics/android/ui/fragment/FeedFragment.kt
  3. 24
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt
  4. 6
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt
  5. 2
      app/src/main/java/net/pokeranalytics/android/util/Global.kt

@ -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(
} }
} }

@ -37,6 +37,7 @@ import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.billing.AppGuard import net.pokeranalytics.android.util.billing.AppGuard
import net.pokeranalytics.android.util.extensions.count import net.pokeranalytics.android.util.extensions.count
import timber.log.Timber
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -391,6 +392,10 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate {
handHistoryFilter?.results() ?: run { getRealm().where<HandHistory>().findAll() } handHistoryFilter?.results() ?: run { getRealm().where<HandHistory>().findAll() }
this.realmHandHistories = this.realmHandHistories.sort("date", Sort.DESCENDING) this.realmHandHistories = this.realmHandHistories.sort("date", Sort.DESCENDING)
this.realmHandHistories.forEach {
Timber.d("date = ${it.date}, year=${it.year}, month = ${it.month}")
}
var distinctDates = handHistoryFilter?.results("year", "month") ?: run { var distinctDates = handHistoryFilter?.results("year", "month") ?: run {
getRealm().where<HandHistory>().distinct("year", "month").findAll() getRealm().where<HandHistory>().distinct("year", "month").findAll()
} }

@ -79,7 +79,10 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
initData() initData()
initUI() initUI()
this.edit()
if (this.model.isEdited) {
this.findNextActionToEdit(0)
}
} }
@ -88,10 +91,12 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
val handHistoryId = this.arguments?.getString(BundleKey.PRIMARY_KEY.value) val handHistoryId = this.arguments?.getString(BundleKey.PRIMARY_KEY.value)
handHistoryId?.let { handHistoryId?.let {
this.setEditing(false)
val handHistory = getRealm().findById<HandHistory>(it) val handHistory = getRealm().findById<HandHistory>(it)
?: throw PAIllegalStateException("HandHistory not found") ?: throw PAIllegalStateException("HandHistory not found")
this.model.setHandHistory(handHistory) this.model.setHandHistory(handHistory)
} ?: run { } ?: run {
this.setEditing(true)
getRealm().executeTransaction { getRealm().executeTransaction {
this.model.createNewHandHistory(it, HandSetup()) this.model.createNewHandHistory(it, HandSetup())
} }
@ -197,6 +202,14 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
return true return true
} }
/***
* Defines if the UI should be in edit mode, or read mode if [editing] false
*/
private fun setEditing(editing: Boolean) {
this.model.isEdited = editing
this.updateMenuUI()
}
private fun retrieveEditTextInputConnection(selection: HHSelection) { private fun retrieveEditTextInputConnection(selection: HHSelection) {
// throw PAIllegalStateException("I believe this should not happen any more") // throw PAIllegalStateException("I believe this should not happen any more")
@ -214,8 +227,8 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
} }
private fun saveOrEdit() { private fun saveOrEdit() {
this.model.isEdited = !this.model.isEdited this.setEditing(!this.model.isEdited)
updateMenuUI()
if (!this.model.isEdited) { if (!this.model.isEdited) {
this.save() this.save()
} }
@ -230,11 +243,6 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
this.model.save(getRealm()) this.model.save(getRealm())
} }
private fun edit() {
this.model.isEdited = true
this.findNextActionToEdit(0)
}
private fun findNextActionToEdit(index: Int? = null, userInitiated: Boolean = false) { private fun findNextActionToEdit(index: Int? = null, userInitiated: Boolean = false) {
val startIndex = index ?: this.model.currentSelection.index val startIndex = index ?: this.model.currentSelection.index

@ -48,14 +48,14 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
private var handSetup: HandSetup = HandSetup() private var handSetup: HandSetup = HandSetup()
/*** /***
* Indicats whether the HH is new or not * Indicates whether the HH is new or not
*/ */
private var isNew: Boolean = true var isNew: Boolean = true
/*** /***
* Indicates whether the hand history is being edited or not * Indicates whether the hand history is being edited or not
*/ */
var isEdited = true var isEdited = false
set(value) { set(value) {
field = value field = value
createRowRepresentation() createRowRepresentation()

@ -1,5 +1,3 @@
package net.pokeranalytics.android.util package net.pokeranalytics.android.util
import android.content.Context
const val NULL_TEXT: String = "--" const val NULL_TEXT: String = "--"

Loading…
Cancel
Save