diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index 5258b44d..7424ced4 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -16,10 +16,7 @@ import net.pokeranalytics.android.model.extensions.getState import net.pokeranalytics.android.model.interfaces.Savable import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource import net.pokeranalytics.android.ui.view.* -import net.pokeranalytics.android.util.getDuration -import net.pokeranalytics.android.util.round -import net.pokeranalytics.android.util.shortDateTime -import net.pokeranalytics.android.util.toCurrency +import net.pokeranalytics.android.util.* import java.util.* import kotlin.collections.ArrayList @@ -131,6 +128,7 @@ open class Session : RealmObject(), SessionInterface, Savable, } SessionState.PAUSED -> { this.timeFrame?.paused = false + this.timeFrame?.pauseDate = null } else -> { } @@ -146,6 +144,7 @@ open class Session : RealmObject(), SessionInterface, Savable, when (getState()) { SessionState.STARTED -> { this.timeFrame?.paused = true + this.timeFrame?.pauseDate = Date() } } } @@ -159,12 +158,25 @@ open class Session : RealmObject(), SessionInterface, Savable, when (getState()) { SessionState.STARTED, SessionState.PAUSED -> { this.timeFrame?.paused = false + this.timeFrame?.pauseDate = null this.timeFrame?.setDate(null, Date()) } } } } + /** + * Restart a session + */ + fun restartSession() { + realm.executeTransaction { + this.timeFrame?.paused = false + this.timeFrame?.pauseDate = null + this.timeFrame?.setDate(Date(), null) + this.timeFrame?.breakDuration = 0L + } + } + /** * Return the duration of the current session */ @@ -339,16 +351,16 @@ open class Session : RealmObject(), SessionInterface, Savable, return when (row) { SessionRow.BANKROLL -> bankroll?.name ?: "--" SessionRow.BLINDS -> getBlinds() + SessionRow.BREAK_TIME -> timeFrame?.breakDuration?.toMinutes() ?: "--" SessionRow.BUY_IN -> buyin.toCurrency() - SessionRow.CASHED_OUT -> result?.cashout?.toCurrency() ?: "--" + SessionRow.CASHED_OUT, SessionRow.PRIZE -> result?.cashout?.toCurrency() ?: "--" SessionRow.COMMENT -> if (comment.isNotEmpty()) comment else "--" SessionRow.END_DATE -> if (timeFrame != null) timeFrame?.endDate?.shortDateTime() ?: "--" else "--" SessionRow.GAME -> getGameTitle() SessionRow.INITIAL_BUY_IN -> tournamentEntryFee?.toCurrency() ?: "--" SessionRow.LOCATION -> location?.name ?: "--" - SessionRow.PLAYERS -> "TODO" + SessionRow.PLAYERS -> tournamentNumberOfPlayers?.toString() ?: "--" SessionRow.POSITION -> result?.tournamentFinalPosition?.toString() ?: "--" - SessionRow.PRIZE -> "TODO" SessionRow.START_DATE -> if (timeFrame != null) timeFrame?.startDate?.shortDateTime() ?: "--" else "--" SessionRow.TABLE_SIZE -> this.tableSize?.let { TableSize(it).localizedTitle(context) } ?: "--" SessionRow.TIPS -> result?.tips?.toCurrency() ?: "--" @@ -387,6 +399,12 @@ open class Session : RealmObject(), SessionInterface, Savable, ) ) } + SessionRow.BREAK_TIME -> { + data.add( + RowRepresentableEditDescriptor("", + hint = R.string.in_minutes, inputType = InputType.TYPE_CLASS_NUMBER) + ) + } SessionRow.BUY_IN -> { // Add first & second buttons values, current value & set the 2 edit texts if (cgBigBlind != null) { @@ -395,6 +413,9 @@ open class Session : RealmObject(), SessionInterface, Savable, } else if (tournamentEntryFee != null) { data.add(RowRepresentableEditDescriptor((tournamentEntryFee ?: 0.0) * 1.0)) data.add(RowRepresentableEditDescriptor((tournamentEntryFee ?: 0.0) * 2.0)) + } else { + data.add(RowRepresentableEditDescriptor(0)) + data.add(RowRepresentableEditDescriptor(0)) } data.add(RowRepresentableEditDescriptor(buyin)) @@ -411,7 +432,7 @@ open class Session : RealmObject(), SessionInterface, Savable, ) ) } - SessionRow.CASHED_OUT -> { + SessionRow.CASHED_OUT, SessionRow.PRIZE -> { data.add( RowRepresentableEditDescriptor(result?.cashout?.round(), inputType = InputType.TYPE_CLASS_NUMBER) ) @@ -433,10 +454,9 @@ open class Session : RealmObject(), SessionInterface, Savable, // Add current location and locations list data.add(RowRepresentableEditDescriptor(location, data = LiveData.LOCATION.items(realm))) } - //TODO SessionRow.PLAYERS -> { data.add( - RowRepresentableEditDescriptor("", inputType = InputType.TYPE_CLASS_NUMBER) + RowRepresentableEditDescriptor(tournamentNumberOfPlayers?.toString(), inputType = InputType.TYPE_CLASS_NUMBER) ) } SessionRow.POSITION -> { @@ -447,12 +467,6 @@ open class Session : RealmObject(), SessionInterface, Savable, ) ) } - //TODO - SessionRow.PRIZE -> { - data.add( - RowRepresentableEditDescriptor("", inputType = InputType.TYPE_CLASS_NUMBER) - ) - } SessionRow.TABLE_SIZE -> { data.add(RowRepresentableEditDescriptor(tableSize)) } @@ -499,12 +513,18 @@ open class Session : RealmObject(), SessionInterface, Savable, cgSmallBlind = null cgBigBlind = null } + SessionRow.BREAK_TIME -> { + val timeFrameToUpdate = + if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) + timeFrameToUpdate.breakDuration = if (value != null) (value as String).toLong() * 60 * 1000 else 0 + timeFrame = timeFrameToUpdate + } SessionRow.BUY_IN -> { val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) localResult.buyin = value as Double? result = localResult } - SessionRow.CASHED_OUT -> { + SessionRow.CASHED_OUT, SessionRow.PRIZE -> { val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) localResult.cashout = if (value == null) null else (value as String).toDouble() result = localResult @@ -533,15 +553,12 @@ open class Session : RealmObject(), SessionInterface, Savable, } SessionRow.INITIAL_BUY_IN -> tournamentEntryFee = if (value == null) null else (value as String).toDouble() SessionRow.LOCATION -> location = value as Location? - SessionRow.PLAYERS -> { - }//TODO + SessionRow.PLAYERS -> tournamentNumberOfPlayers = if (value != null) (value as String).toInt() else null SessionRow.POSITION -> { val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) localResult.tournamentFinalPosition = if (value == null) null else (value as String).toInt() result = localResult } - SessionRow.PRIZE -> { - }//TODO SessionRow.START_DATE -> if (value is Date?) { if (value == null) { timeFrame = null diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt index 0fe3fa16..86596dad 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt @@ -26,6 +26,18 @@ open class TimeFrame : RealmObject() { this.computeDuration() } + // The latest pause date + var pauseDate: Date? = null + set(value) { + field?.let { + if (value == null && field != null) { + breakDuration += Date().time - it.time + } + } + field = value + this.computeDuration() + } + // The break duration var breakDuration: Long = 0L set(value) { 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 5a8a0847..7127aef4 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 @@ -27,7 +27,6 @@ import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback import net.pokeranalytics.android.ui.view.SessionRow import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager -import net.pokeranalytics.android.util.toast import java.util.* class SessionFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, BottomSheetDelegate { @@ -67,15 +66,9 @@ class SessionFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, Bott override fun onOptionsItemSelected(item: MenuItem?): Boolean { when (item!!.itemId) { - R.id.stop -> { - currentSession.stopSession() - updateSessionUI() - } - R.id.restart -> toast("Restart is clicked!") - R.id.delete -> { - currentSession.delete() - activity?.finish() - } + R.id.stop -> stopSession() + R.id.restart -> restartTimer() + R.id.delete -> deleteSession() } return true } @@ -231,6 +224,30 @@ class SessionFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, Bott updateSessionUI() } + /** + * Stop the current session + */ + private fun stopSession() { + currentSession.stopSession() + updateSessionUI() + } + + /** + * Restart timer + */ + private fun restartTimer() { + currentSession.restartSession() + updateSessionUI() + } + + /** + * Delete a session + */ + private fun deleteSession() { + currentSession.delete() + activity?.finish() + } + /** * Set fragment data */ diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt index 2528518c..a473d25c 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt @@ -5,10 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast -import androidx.core.content.ContextCompat -import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.fragment_settings.* import net.pokeranalytics.android.R import net.pokeranalytics.android.ui.activity.DataListActivity diff --git a/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt b/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt index f3e7551f..16388ccd 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt @@ -37,6 +37,19 @@ fun Double.toCurrency(): String { return format.format(this) } +// Return the time from milliseconds to hours:minutes +fun Long.toMinutes() : String { + val totalMinutes = this / (1000 * 60) + val hours = totalMinutes / 60 + val minutesLeft = totalMinutes % 60 + var duration = "" + duration += if (hours < 10) "0$hours" else hours.toString() + duration += ":" + duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString() + return duration + +} + // Calendar // Return if the calendar dates are in the same month