Fixing issue with favorite sessions

feature/top10
Laurent 7 years ago
parent 9da9bea414
commit ec90222e43
  1. 210
      app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt
  2. 6
      app/src/main/java/net/pokeranalytics/android/ui/fragment/SessionFragment.kt

@ -6,45 +6,46 @@ import io.realm.Sort
import net.pokeranalytics.android.model.realm.Location import net.pokeranalytics.android.model.realm.Location
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow
import timber.log.Timber
/** /**
* Returns all significant parameters concatenated in a String * Returns all significant parameters concatenated in a String
* Not suitable for display * Not suitable for display
*/ */
fun Session.parameterRepresentation(context: Context) : String { fun Session.parameterRepresentation(context: Context): String {
var representation = "" var representation = ""
this.significantFields().forEach { this.significantFields().forEach {
representation += this.stringForRow(it, context) representation += this.stringForRow(it, context)
} }
return representation return representation
} }
/** /**
* Returns a list of fields used to determine which kind of session is favorite * Returns a list of fields used to determine which kind of session is favorite
*/ */
private fun Session.significantFields() : List<SessionRow> { private fun Session.significantFields(): List<SessionRow> {
when (this.type) { when (this.type) {
Session.Type.CASH_GAME.ordinal -> { Session.Type.CASH_GAME.ordinal -> {
return listOf( return listOf(
SessionRow.GAME, SessionRow.GAME,
SessionRow.INITIAL_BUY_IN, SessionRow.INITIAL_BUY_IN,
SessionRow.BANKROLL, SessionRow.BANKROLL,
SessionRow.TABLE_SIZE, SessionRow.TABLE_SIZE,
SessionRow.TOURNAMENT_NAME SessionRow.TOURNAMENT_NAME
) )
} }
Session.Type.TOURNAMENT.ordinal -> { Session.Type.TOURNAMENT.ordinal -> {
return listOf( return listOf(
SessionRow.GAME, SessionRow.GAME,
SessionRow.BLINDS, SessionRow.BLINDS,
SessionRow.BANKROLL, SessionRow.BANKROLL,
SessionRow.TABLE_SIZE SessionRow.TABLE_SIZE
) )
} }
} }
throw Exception("A session should always have a type: tournament or CG") throw Exception("A session should always have a type: tournament or CG")
} }
/** /**
@ -52,79 +53,86 @@ private fun Session.significantFields() : List<SessionRow> {
*/ */
class FavoriteSessionFinder { class FavoriteSessionFinder {
/** /**
* A counter convenience class * A counter convenience class
*/ */
class Counter(session: Session) { class Counter(session: Session) {
var session: Session = session var session: Session = session
var counter: Int = 1 var counter: Int = 1
fun increment() { fun increment() {
this.counter++ this.counter++
} }
} }
companion object { companion object {
private const val FAVORITE_SIGNIFICANT_SESSIONS = 15L private const val FAVORITE_SIGNIFICANT_SESSIONS = 15L
/** /**
* Copies the favorite session parameters on the [newSession] * Copies the favorite session parameters on the [newSession]
*/ */
fun copyParametersFromFavoriteSession(newSession: Session, location: Location?, context: Context) { fun copyParametersFromFavoriteSession(newSession: Session, location: Location?, context: Context) {
val favoriteSession = FavoriteSessionFinder.favoriteSession(newSession.type, location, newSession.realm, context) val favoriteSession =
FavoriteSessionFinder.favoriteSession(newSession.type, location, newSession.realm, context)
favoriteSession?.let { fav ->
Timber.d(">>> fav null? = ${favoriteSession == null}")
newSession.limit = fav.limit
newSession.game = fav.game Timber.d(">>> game = ${favoriteSession?.game?.name}")
newSession.bankroll = fav.bankroll Timber.d(">>> bb = ${favoriteSession?.cgBigBlind?.toString()}")
newSession.tableSize = fav.tableSize Timber.d(">>> duration = ${favoriteSession?.netDuration}")
when (newSession.type) { favoriteSession?.let { fav ->
Session.Type.CASH_GAME.ordinal -> {
newSession.cgSmallBlind = fav.cgSmallBlind newSession.limit = fav.limit
newSession.cgBigBlind = fav.cgBigBlind newSession.game = fav.game
} newSession.bankroll = fav.bankroll
Session.Type.TOURNAMENT.ordinal -> { newSession.tableSize = fav.tableSize
newSession.tournamentEntryFee = fav.tournamentEntryFee
} when (newSession.type) {
} Session.Type.CASH_GAME.ordinal -> {
} newSession.cgSmallBlind = fav.cgSmallBlind
} newSession.cgBigBlind = fav.cgBigBlind
}
/** Session.Type.TOURNAMENT.ordinal -> {
* Determines the favorite session given a [sessionType] and an optional [location] newSession.tournamentEntryFee = fav.tournamentEntryFee
*/ }
fun favoriteSession(sessionType: Int, location: Location?, realm: Realm, context: Context) : Session? { }
}
val lastSessionsQuery = realm.where(Session::class.java).equalTo("type", sessionType) }
if (location != null) {
lastSessionsQuery.equalTo("location.id", location.id) /**
} * Determines the favorite session given a [sessionType] and an optional [location]
val lastSessions = lastSessionsQuery */
.sort("startDate", Sort.DESCENDING) fun favoriteSession(sessionType: Int, location: Location?, realm: Realm, context: Context): Session? {
.limit(FAVORITE_SIGNIFICANT_SESSIONS)
.findAll() val lastSessionsQuery = realm.where(Session::class.java).isNotNull("endDate").equalTo("type", sessionType)
if (location != null) {
val counters= hashMapOf<String, Counter>() lastSessionsQuery.equalTo("location.id", location.id)
lastSessions.forEach { session -> }
val representation = session.parameterRepresentation(context) val lastSessions = lastSessionsQuery
val counter = counters[representation] .sort("startDate", Sort.DESCENDING)
if (counter != null) { .limit(FAVORITE_SIGNIFICANT_SESSIONS)
counter.increment() .findAll()
} else {
counters[representation] = Counter(session) val counters = hashMapOf<String, Counter>()
} lastSessions.forEach { session ->
} val representation = session.parameterRepresentation(context)
val counter = counters[representation]
val sortedCounters = counters.values.sortedBy { it.counter } if (counter != null) {
return sortedCounters.firstOrNull()?.session counter.increment()
} } else {
counters[representation] = Counter(session)
} }
}
val sortedCounters = counters.values.sortedBy { it.counter }
return sortedCounters.firstOrNull()?.session
}
}
} }

@ -285,10 +285,10 @@ class SessionFragment : PokerAnalyticsFragment(), RowRepresentableDelegate {
parentActivity.findNearestLocation { location -> parentActivity.findNearestLocation { location ->
location?.let { location?.let {
realm.beginTransaction() realm.beginTransaction()
val realmLocation = realm.where<Location>().equalTo("id", it.id).findFirst() val location = realm.where<Location>().equalTo("id", it.id).findFirst()
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, realmLocation, requireContext()) FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, location, requireContext())
currentSession.location = realmLocation currentSession.location = location
realm.commitTransaction() realm.commitTransaction()
updateSessionUI() updateSessionUI()
} }

Loading…
Cancel
Save