From ec90222e431fdde57ba4db4540cb1c8afb6199fb Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 2 Apr 2019 16:59:55 +0200 Subject: [PATCH] Fixing issue with favorite sessions --- .../model/utils/FavoriteSessionFinder.kt | 210 +++++++++--------- .../android/ui/fragment/SessionFragment.kt | 6 +- 2 files changed, 112 insertions(+), 104 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt b/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt index 0ddb035e..558a5f78 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt @@ -6,45 +6,46 @@ import io.realm.Sort import net.pokeranalytics.android.model.realm.Location import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow +import timber.log.Timber /** * Returns all significant parameters concatenated in a String * Not suitable for display */ -fun Session.parameterRepresentation(context: Context) : String { - var representation = "" +fun Session.parameterRepresentation(context: Context): String { + var representation = "" - this.significantFields().forEach { - representation += this.stringForRow(it, context) - } - return representation + this.significantFields().forEach { + representation += this.stringForRow(it, context) + } + return representation } /** * Returns a list of fields used to determine which kind of session is favorite */ -private fun Session.significantFields() : List { - when (this.type) { - Session.Type.CASH_GAME.ordinal -> { - return listOf( - SessionRow.GAME, - SessionRow.INITIAL_BUY_IN, - SessionRow.BANKROLL, - SessionRow.TABLE_SIZE, - SessionRow.TOURNAMENT_NAME - ) - } - Session.Type.TOURNAMENT.ordinal -> { - return listOf( - SessionRow.GAME, - SessionRow.BLINDS, - SessionRow.BANKROLL, - SessionRow.TABLE_SIZE - ) - } - } - throw Exception("A session should always have a type: tournament or CG") +private fun Session.significantFields(): List { + when (this.type) { + Session.Type.CASH_GAME.ordinal -> { + return listOf( + SessionRow.GAME, + SessionRow.INITIAL_BUY_IN, + SessionRow.BANKROLL, + SessionRow.TABLE_SIZE, + SessionRow.TOURNAMENT_NAME + ) + } + Session.Type.TOURNAMENT.ordinal -> { + return listOf( + SessionRow.GAME, + SessionRow.BLINDS, + SessionRow.BANKROLL, + SessionRow.TABLE_SIZE + ) + } + } + throw Exception("A session should always have a type: tournament or CG") } /** @@ -52,79 +53,86 @@ private fun Session.significantFields() : List { */ class FavoriteSessionFinder { - /** - * A counter convenience class - */ - class Counter(session: Session) { - - var session: Session = session - var counter: Int = 1 - - fun increment() { - this.counter++ - } - - } - - companion object { - - private const val FAVORITE_SIGNIFICANT_SESSIONS = 15L - - /** - * Copies the favorite session parameters on the [newSession] - */ - fun copyParametersFromFavoriteSession(newSession: Session, location: Location?, context: Context) { - - val favoriteSession = FavoriteSessionFinder.favoriteSession(newSession.type, location, newSession.realm, context) - - favoriteSession?.let { fav -> - - newSession.limit = fav.limit - newSession.game = fav.game - newSession.bankroll = fav.bankroll - newSession.tableSize = fav.tableSize - - when (newSession.type) { - Session.Type.CASH_GAME.ordinal -> { - newSession.cgSmallBlind = fav.cgSmallBlind - newSession.cgBigBlind = fav.cgBigBlind - } - Session.Type.TOURNAMENT.ordinal -> { - newSession.tournamentEntryFee = fav.tournamentEntryFee - } - } - } - } - - /** - * Determines the favorite session given a [sessionType] and an optional [location] - */ - 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) - } - val lastSessions = lastSessionsQuery - .sort("startDate", Sort.DESCENDING) - .limit(FAVORITE_SIGNIFICANT_SESSIONS) - .findAll() - - val counters= hashMapOf() - lastSessions.forEach { session -> - val representation = session.parameterRepresentation(context) - val counter = counters[representation] - if (counter != null) { - counter.increment() - } else { - counters[representation] = Counter(session) - } - } - - val sortedCounters = counters.values.sortedBy { it.counter } - return sortedCounters.firstOrNull()?.session - } - - } + /** + * A counter convenience class + */ + class Counter(session: Session) { + + var session: Session = session + var counter: Int = 1 + + fun increment() { + this.counter++ + } + + } + + companion object { + + private const val FAVORITE_SIGNIFICANT_SESSIONS = 15L + + /** + * Copies the favorite session parameters on the [newSession] + */ + fun copyParametersFromFavoriteSession(newSession: Session, location: Location?, context: Context) { + + val favoriteSession = + FavoriteSessionFinder.favoriteSession(newSession.type, location, newSession.realm, context) + + Timber.d(">>> fav null? = ${favoriteSession == null}") + + Timber.d(">>> game = ${favoriteSession?.game?.name}") + Timber.d(">>> bb = ${favoriteSession?.cgBigBlind?.toString()}") + Timber.d(">>> duration = ${favoriteSession?.netDuration}") + + favoriteSession?.let { fav -> + + newSession.limit = fav.limit + newSession.game = fav.game + newSession.bankroll = fav.bankroll + newSession.tableSize = fav.tableSize + + when (newSession.type) { + Session.Type.CASH_GAME.ordinal -> { + newSession.cgSmallBlind = fav.cgSmallBlind + newSession.cgBigBlind = fav.cgBigBlind + } + Session.Type.TOURNAMENT.ordinal -> { + newSession.tournamentEntryFee = fav.tournamentEntryFee + } + } + } + } + + /** + * Determines the favorite session given a [sessionType] and an optional [location] + */ + fun favoriteSession(sessionType: Int, location: Location?, realm: Realm, context: Context): Session? { + + val lastSessionsQuery = realm.where(Session::class.java).isNotNull("endDate").equalTo("type", sessionType) + if (location != null) { + lastSessionsQuery.equalTo("location.id", location.id) + } + val lastSessions = lastSessionsQuery + .sort("startDate", Sort.DESCENDING) + .limit(FAVORITE_SIGNIFICANT_SESSIONS) + .findAll() + + val counters = hashMapOf() + lastSessions.forEach { session -> + val representation = session.parameterRepresentation(context) + val counter = counters[representation] + if (counter != null) { + counter.increment() + } else { + counters[representation] = Counter(session) + } + } + + val sortedCounters = counters.values.sortedBy { it.counter } + return sortedCounters.firstOrNull()?.session + } + + } } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/SessionFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/SessionFragment.kt index 97e56d3d..fe2a7dc6 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/SessionFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/SessionFragment.kt @@ -285,10 +285,10 @@ class SessionFragment : PokerAnalyticsFragment(), RowRepresentableDelegate { parentActivity.findNearestLocation { location -> location?.let { realm.beginTransaction() - val realmLocation = realm.where().equalTo("id", it.id).findFirst() - FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, realmLocation, requireContext()) + val location = realm.where().equalTo("id", it.id).findFirst() + FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, location, requireContext()) - currentSession.location = realmLocation + currentSession.location = location realm.commitTransaction() updateSessionUI() }