@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip
import kotlinx.android.synthetic.main.row_hand_action.view.*
import kotlinx.android.synthetic.main.row_hand_cards.view.*
import kotlinx.android.synthetic.main.row_hhsettings_blinds.view.*
import kotlinx.android.synthetic.main.row_hhsettings_player_setup.view.*
import kotlinx.android.synthetic.main.row_hhsettings_straddle.view.*
@ -21,7 +22,6 @@ import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.handhistory.Position
import net.pokeranalytics.android.model.handhistory.Street
import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.model.realm.handhistory.formatted
import net.pokeranalytics.android.ui.adapter.BindableHolder
import net.pokeranalytics.android.ui.adapter.RecyclerAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource
@ -29,13 +29,10 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.extensions.px
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType
import net.pokeranalytics.android.ui.modules.handhistory.model.*
import net.pokeranalytics.android.ui.modules.handhistory.views.PlayerCardsRow
import net.pokeranalytics.android.ui.modules.handhistory.views.PositionAdapter
import net.pokeranalytics.android.ui.modules.handhistory.views.StreetCardsRow
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.holder.RowViewHolder
import net.pokeranalytics.android.ui.view.rowrepresentable.ViewIdentifier
import net.pokeranalytics.android.util.extensions.formatted
import timber.log.Timber
@ -70,7 +67,7 @@ enum class HandRowType(var layoutRes: Int) : ViewIdentifier, RowRepresentable, H
}
}
override fun keyboardForTag ( tag : Int ) : HHKeyboard {
override fun keyboardForTag ( tag : Int ) : HHKeyboard ? {
return when ( this ) {
BLINDS -> HHKeyboard . AMOUNT
else -> throw PAIllegalStateException ( " unmanaged tag $tag " )
@ -193,7 +190,30 @@ class HandHistoryAdapter(
this . currentPosition = position
}
protected fun toggleFocus ( editText : EditText , focused : Boolean ) {
protected fun configureEditTexts ( index : Int , position : Int , row : RowRepresentable , adapter : RecyclerAdapter ) {
this . configureEditTexts ( index .. index , position , row , adapter )
}
protected fun configureEditTexts ( range : IntRange , position : Int , row : RowRepresentable , adapter : RecyclerAdapter ) {
range . forEach { tag ->
val string = adapter . dataSource . stringForRow ( row , itemView . context , tag )
// Text
val editText = itemView . findViewWithTag < EditText > ( tag ) ?: throw PAIllegalStateException ( " Edit Text not found for tag: $tag , class: $this " )
editText . setText ( string )
// Focus
val selected = adapter . dataSource . isSelected ( position , row , tag )
toggleFocus ( editText , selected )
// Background
editText . setBackgroundColor ( color ( selected ) )
}
}
private fun toggleFocus ( editText : EditText , focused : Boolean ) {
if ( focused ) {
editText . requestFocus ( )
} else {
@ -202,41 +222,41 @@ class HandHistoryAdapter(
}
open fun editTextForTag ( tag : Int ) : EditText {
throw PAIllegalStateException ( " Should overriden by class : $this " )
return itemView . findViewWithTag ( tag )
// throw PAIllegalStateException("Field at tag: $tag requires to return an EditText in order to connect the keyboard to it. Should be overridden by class : $this")
}
}
inner class RowHandBlinds ( itemView : View ) : RowHandHolder ( itemView ) {
init {
itemView . smallBlindEditText . tag = 0
itemView . bigBlindEditText . tag = 1
itemView . anteEditText . tag = 2
}
// sb, bb, ante, bb ante
override fun onBind ( position : Int , row : RowRepresentable , adapter : RecyclerAdapter ) {
super . onBind ( position , row , adapter )
itemView . smallBlindEditText . setText ( adapter . dataSource . stringForRow ( row , itemView . context , 0 ) )
itemView . bigBlindEditText . setText ( adapter . dataSource . stringForRow ( row , itemView . context , 1 ) )
itemView . anteEditText . setText ( adapter . dataSource . stringForRow ( row , itemView . context , 2 ) )
itemView . bbAnteSwitch . isChecked = adapter . dataSource . isSelected ( position , row , 0 )
val sbSelected = adapter . dataSource . isSelected ( position , row , 0 )
toggleFocus ( itemView . smallBlindEditText , sbSelected )
configureEditTexts ( 0. . 2 , position , row , adapter )
val bbSelected = adapter . dataSource . isSelected ( position , row , 1 )
toggleFocus ( itemView . bigBlindEditText , bbSelected )
// itemView.smallBlindEditText.setText(adapter.dataSource.stringForRow(row, itemView.context, 0))
// itemView.bigBlindEditText.setText(adapter.dataSource.stringForRow(row, itemView.context, 1))
// itemView.anteEditText.setText(adapter.dataSource.stringForRow(row, itemView.context, 2))
// itemView.bbAnteSwitch.isChecked = adapter.dataSource.isSelected(position, row, 0)
//
// val sbSelected = adapter.dataSource.isSelected(position, row, 0)
// toggleFocus(itemView.smallBlindEditText, sbSelected)
//
// val bbSelected = adapter.dataSource.isSelected(position, row, 1)
// toggleFocus(itemView.bigBlindEditText, bbSelected)
//
// val anteSelected = adapter.dataSource.isSelected(position, row, 2)
// toggleFocus(itemView.anteEditText, anteSelected)
val anteSelected = adapter . dataSource . isSelected ( position , row , 2 )
toggleFocus ( itemView . anteEditText , anteSelected )
}
override fun editTextForTag ( tag : Int ) : EditText {
return when ( tag ) {
0 -> itemView . smallBlindEditText
1 -> itemView . bigBlindEditText
2 -> itemView . anteEditText
else -> throw PAIllegalStateException ( " unmanaged tag $tag " )
}
}
}
@ -271,9 +291,7 @@ class HandHistoryAdapter(
itemView . positionsChipGroup . addView ( chip )
}
}
}
/ * *
@ -282,6 +300,9 @@ class HandHistoryAdapter(
inner class RowHandAction ( itemView : View ) : RowHandHolder ( itemView ) {
init {
itemView . actionButton . tag = ComputedAction . Tag . ACTION . ordinal
itemView . amountEditText . tag = ComputedAction . Tag . AMOUNT . ordinal
// Action
itemView . findViewById < Button > ( R . id . actionButton ) ?. let { actionButton ->
actionButton . setOnClickListener {
@ -316,9 +337,9 @@ class HandHistoryAdapter(
}
}
override fun editTextForTag ( tag : Int ) : EditText {
return itemView . amountEditText
}
// override fun editTextForTag(tag: Int): EditText {
// return itemView. amountEditText
// }
override fun onBind ( position : Int , row : RowRepresentable , adapter : RecyclerAdapter ) {
super . onBind ( position , row , adapter )
@ -334,9 +355,7 @@ class HandHistoryAdapter(
itemView . findViewById < Button > ( R . id . actionButton ) ?. let { actionButton ->
actionButton . isEnabled = computedAction . actionTypeCanBeEdited
val tag = HHKeyboard . ACTION . ordinal
val selected = adapter . dataSource . isSelected ( position , row , tag )
val selected = adapter . dataSource . isSelected ( position , row , actionButton . tag as Int )
actionButton . backgroundTintList = ColorStateList . valueOf ( color ( selected ) )
computedAction . action . type ?. resId ?. let {
@ -351,22 +370,24 @@ class HandHistoryAdapter(
itemView . findViewById < EditText > ( R . id . amountEditText ) ?. let { amountEditText ->
amountEditText . isEnabled = computedAction . amountCanBeEdited
val tag = HHKeyboard . AMOUNT . ordinal
val tag = amountEditText . tag as Int
val selected = adapter . dataSource . isSelected ( position , row , tag )
amountEditText . setBackgroundColor ( color ( selected ) )
// amountEditText.setBackgroundColor(color(selected) )
// Useful to have the cursor disappear when the keyboard is closed
amountEditText . isFocusable = selected && computedAction . amountCanBeEdited
amountEditText . isFocusableInTouchMode = selected && computedAction . amountCanBeEdited
amountEditText . setText ( computedAction . action . formattedAmount )
configureEditTexts ( tag , position , row , adapter )
if ( selected ) {
amountEditText . requestFocus ( )
} else {
amountEditText . clearFocus ( )
}
// amountEditText.setText(computedAction.action.formattedAmount)
//
// if (selected) {
// amountEditText.requestFocus()
// } else {
// amountEditText.clearFocus()
// }
Timber . d ( " Amount at $position is selected: $selected , focusable = ${amountEditText.isFocusable} , isFocusableInTouchMode = ${amountEditText.isFocusableInTouchMode} , hasFocus = ${amountEditText.hasFocus()} , enabled = ${amountEditText.isEnabled} " )
}
@ -382,6 +403,10 @@ class HandHistoryAdapter(
init {
itemView . flopEditText . tag = Street . FLOP . ordinal
itemView . turnEditText . tag = Street . TURN . ordinal
itemView . riverEditText . tag = Street . RIVER . ordinal
// Flop
itemView . findViewById < EditText > ( R . id . flopEditText ) ?. let { flopEditText ->
@ -411,17 +436,19 @@ class HandHistoryAdapter(
val streetCardView = row as StreetCardsRow
val street = streetCardView . street
configureEditTexts ( 0. . 2 , position , row , adapter )
itemView . findViewById < EditText > ( R . id . flopEditText ) ?. let { flopEditText ->
flopEditText . isFocusable = ( street == Street . FLOP )
flopEditText . isVisible = true
val flop = streetCardView . cardHolder ?. cards ?. take ( 3 )
val text = flop ?. formatted ( itemView . context )
flopEditText . setText ( text )
val selected = adapter . dataSource . isSelected ( position , row , Street . FLOP . ordinal )
toggleFocus ( flopEditText , selected )
// val flop = streetCardView.cardHolder?.cards?.take(3)
// val text = flop?.formatted(itemView.context)
// flopEditText.setText(text)
//
// val selected = adapter.dataSource.isSelected(position, row, Street.FLOP.ordinal)
// toggleFocus(flopEditText, selected)
}
@ -429,30 +456,30 @@ class HandHistoryAdapter(
// turnEditText.isVisible = streetCardView.street.ordinal >= Street.TURN.ordinal
turnEditText . isFocusable = ( street == Street . TURN )
if ( streetCardView . cardCount > 3 ) {
val text = streetCardView . cardAtIndex ( 3 ) ?. formatted ( itemView . context )
turnEditText . setText ( text )
} else {
turnEditText . text = null
}
val selected = adapter . dataSource . isSelected ( position , row , Street . TURN . ordinal )
toggleFocus ( turnEditText , selected )
// if (streetCardView.cardCount > 3) {
// val text = streetCardView.cardAtIndex(3)?.formatted(itemView.context )
// turnEditText.setText(text )
// } else {
// turnEditText.text = null
// }
//
// val selected = adapter.dataSource.isSelected(position, row, Street.TURN.ordinal )
// toggleFocus(turnEditText, selected )
}
itemView . findViewById < EditText > ( R . id . riverEditText ) ?. let { riverEditText ->
// riverEditText.isVisible = streetCardView.street.ordinal >= Street.RIVER.ordinal
riverEditText . isFocusable = ( street == Street . RIVER )
if ( streetCardView . cardCount > 4 ) {
val text = streetCardView . cardAtIndex ( 4 ) ?. formatted ( itemView . context )
riverEditText . setText ( text )
} else {
riverEditText . text = null
}
val selected = adapter . dataSource . isSelected ( position , row , Street . RIVER . ordinal )
toggleFocus ( riverEditText , selected )
// if (streetCardView.cardCount > 4) {
// val text = streetCardView.cardAtIndex(4)?.formatted(itemView.context )
// riverEditText.setText(text )
// } else {
// riverEditText.text = null
// }
//
// val selected = adapter.dataSource.isSelected(position, row, Street.RIVER.ordinal )
// toggleFocus(riverEditText, selected )
}
}
@ -465,13 +492,13 @@ class HandHistoryAdapter(
* /
inner class RowHandPlayerSummary ( itemView : View ) : RowHandHolder ( itemView ) {
private val CARDS _TAG = 0
init {
// Cards
itemView . findViewById < EditText > ( R . id . cardsEditText ) ?. let { cardsEditText ->
cardsEditText . tag = PlayerCardsRow . Tag . CARDS . ordinal
cardsEditText . isFocusableInTouchMode = true
cardsEditText . setOnTouchListener { _ , event ->
@ -483,7 +510,7 @@ class HandHistoryAdapter(
cardsEditText . requestFocus ( )
editTextSelected ( cardsEditText , true , CARDS _TAG )
editTextSelected ( cardsEditText , true , PlayerCardsRow . Tag . CARDS . ordinal )
}
return @setOnTouchListener true
}
@ -499,23 +526,25 @@ class HandHistoryAdapter(
// Position
itemView . findViewById < Button > ( R . id . positionButton ) ?. let { positionButton ->
positionButton . text = playerCardView . position . value
positionButton . text = playerCardView . position ?. value ?: throw PAIllegalStateException ( " Should not happen " )
}
// Amount
itemView . findViewById < EditText > ( R . id . cardsEditText ) ?. let { cardsEditText ->
val selected = adapter . dataSource . isSelected ( position , row , CARDS _TAG )
val selected = adapter . dataSource . isSelected ( position , row , PlayerCardsRow . Tag . CARDS . ordinal )
// Both are required, otherwise requestFocus() fails
cardsEditText . isFocusable = selected
cardsEditText . isFocusableInTouchMode = selected
Timber . d ( " cardsEditText at $position is selected: $selected , focusable = ${cardsEditText.isFocusable} , isFocusableInTouchMode = ${cardsEditText.isFocusableInTouchMode} , hasFocus = ${cardsEditText.hasFocus()} , enabled = ${cardsEditText.isEnabled} " )
cardsEditText . setBackgroundColor ( color ( selected ) )
configureEditTexts ( PlayerCardsRow . Tag . CARDS . ordinal , position , row , adapter )
cardsEditText . setText ( playerCardView . cardHolder ?. cards ?. formatted ( itemView . context ) )
toggleFocus ( cardsEditText , selected )
// Timber.d("cardsEditText at $position is selected: $selected, focusable = ${cardsEditText.isFocusable}, isFocusableInTouchMode = ${cardsEditText.isFocusableInTouchMode}, hasFocus = ${cardsEditText.hasFocus()}, enabled = ${cardsEditText.isEnabled}")
// cardsEditText.setBackgroundColor(color(selected))
//
// cardsEditText.setText(playerCardView.cardHolder?.cards?.formatted(itemView.context))
// toggleFocus(cardsEditText, selected)
}
}
}
@ -527,6 +556,9 @@ class HandHistoryAdapter(
init {
itemView . handEditText . tag = PlayerSetupRow . Tag . CARDS . ordinal
itemView . stackEditText . tag = PlayerSetupRow . Tag . STACK . ordinal
// Position Recycler
val positionViewManager = LinearLayoutManager ( itemView . context , RecyclerView . HORIZONTAL , false )
@ -536,12 +568,34 @@ class HandHistoryAdapter(
adapter = positionAdapter
}
itemView . findViewById < EditText > ( R . id . stackEditText ) ?. let { stackEditText ->
stackEditText . inputType = InputType . TYPE _NUMBER _FLAG _DECIMAL
stackEditText . isFocusableInTouchMode = true
stackEditText . setOnTouchListener { _ , event ->
if ( event . action == MotionEvent . ACTION _UP ) {
// Both are required, otherwise requestFocus() fails
stackEditText . isFocusable = true
stackEditText . isFocusableInTouchMode = true
stackEditText . requestFocus ( )
editTextSelected ( stackEditText , true , stackEditText . tag as Int )
}
return @setOnTouchListener true
}
}
}
override fun onBind ( position : Int , row : RowRepresentable , adapter : RecyclerAdapter ) {
super . onBind ( position , row , adapter )
val setup = row as PlayerSetupRowRepresentable
val setup = row as PlayerSetupRow
val state = setup . state
this . delegate = adapter . delegate
@ -549,7 +603,7 @@ class HandHistoryAdapter(
itemView . title . text = setup . title ( itemView . context )
// Position recycler
val visibility = if ( state == PlayerSetupRowRepresentable . State . SETUP _ONLY ) View . GONE else View . VISIBLE
val visibility = if ( state == PlayerSetupRow . State . SETUP _ONLY ) View . GONE else View . VISIBLE
itemView . positionRecyclerView . visibility = visibility
this . positionAdapter . positions = adapter . dataSource . contentForRow ( row , itemView . context , Position :: class )
@ -565,21 +619,25 @@ class HandHistoryAdapter(
itemView . positionRecyclerView . visibility = View . VISIBLE
itemView . posButton . backgroundTintList = ColorStateList . valueOf ( color ( true ) )
}
val positionSelected = adapter . dataSource . isSelected ( position , row , PlayerSetupRowRepresentable . Tag . POSITION . ordinal )
val positionSelected = adapter . dataSource . isSelected ( position , row , PlayerSetupRow . Tag . POSITION . ordinal )
itemView . posButton . backgroundTintList = ColorStateList . valueOf ( color ( positionSelected ) )
// Settings
itemView . settings _container . visibility = if ( state == PlayerSetupRowRepresentable . State . POSITIONS _ONLY ) View . GONE else View . VISIBLE
itemView . settings _container . visibility = if ( state == PlayerSetupRow . State . POSITIONS _ONLY ) View . GONE else View . VISIBLE
// Hand
itemView . handEditText . setText ( setup . playerSetup ?. cards ?. formatted ( itemView . context ) )
val handSelected = adapter . dataSource . isSelected ( position , row , PlayerSetupRowRepresentable . Tag . CARDS . ordinal )
itemView . handEditText . setBackgroundColor ( color ( handSelected ) )
configureEditTexts ( 1. . 2 , position , row , adapter )
// Stack
itemView . stackEditText . setText ( setup . playerSetup ?. stack ?. formatted ( ) )
val stackSelected = adapter . dataSource . isSelected ( position , row , PlayerSetupRowRepresentable . Tag . STACK . ordinal )
itemView . stackEditText . setBackgroundColor ( color ( stackSelected ) )
// Hand
// itemView.handEditText.setText(setup.playerSetup?.cards?.formatted(itemView.context))
// val handSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRow.Tag.CARDS.ordinal)
// itemView.handEditText.setBackgroundColor(color(handSelected))
// toggleFocus(itemView.handEditText, handSelected)
//
// // Stack
// itemView.stackEditText.setText(setup.playerSetup?.stack?.formatted())
// val stackSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRow.Tag.STACK.ordinal)
// itemView.stackEditText.setBackgroundColor(color(stackSelected))
// toggleFocus(itemView.stackEditText, stackSelected)
// itemView.handEditText.setText(adapter.dataSource.stringForRow(row, itemView.context, 0))
@ -587,6 +645,13 @@ class HandHistoryAdapter(
}
// override fun editTextForTag(tag: Int): EditText {
// return when (tag) {
// PlayerSetupRow.Tag.STACK.ordinal -> itemView.stackEditText
// else -> throw PAIllegalStateException("unmanaged tag $tag")
// }
// }
}
}