|
|
|
@ -1,28 +1,33 @@ |
|
|
|
package net.pokeranalytics.android.model.realm |
|
|
|
package net.pokeranalytics.android.model.realm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
import android.text.InputType |
|
|
|
import android.text.InputType |
|
|
|
import io.realm.* |
|
|
|
import io.realm.RealmList |
|
|
|
|
|
|
|
import io.realm.RealmObject |
|
|
|
import io.realm.annotations.Ignore |
|
|
|
import io.realm.annotations.Ignore |
|
|
|
import io.realm.annotations.PrimaryKey |
|
|
|
import io.realm.annotations.PrimaryKey |
|
|
|
import net.pokeranalytics.android.R |
|
|
|
import net.pokeranalytics.android.R |
|
|
|
import net.pokeranalytics.android.calculus.SessionInterface |
|
|
|
import net.pokeranalytics.android.calculus.SessionInterface |
|
|
|
|
|
|
|
import net.pokeranalytics.android.model.Limit |
|
|
|
import net.pokeranalytics.android.model.LiveData |
|
|
|
import net.pokeranalytics.android.model.LiveData |
|
|
|
|
|
|
|
import net.pokeranalytics.android.model.ObjectSavable |
|
|
|
import net.pokeranalytics.android.model.extensions.SessionState |
|
|
|
import net.pokeranalytics.android.model.extensions.SessionState |
|
|
|
import net.pokeranalytics.android.model.extensions.getState |
|
|
|
import net.pokeranalytics.android.model.extensions.getState |
|
|
|
import net.pokeranalytics.android.ui.adapter.components.LiveDataDataSource |
|
|
|
|
|
|
|
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource |
|
|
|
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource |
|
|
|
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetData |
|
|
|
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetData |
|
|
|
import net.pokeranalytics.android.ui.view.RowEditable |
|
|
|
import net.pokeranalytics.android.ui.view.RowEditable |
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
import net.pokeranalytics.android.ui.view.SessionRow |
|
|
|
import net.pokeranalytics.android.ui.view.SessionRow |
|
|
|
import net.pokeranalytics.android.util.data.sessionDao |
|
|
|
import net.pokeranalytics.android.util.getDuration |
|
|
|
|
|
|
|
import net.pokeranalytics.android.util.round |
|
|
|
import net.pokeranalytics.android.util.short |
|
|
|
import net.pokeranalytics.android.util.short |
|
|
|
import net.pokeranalytics.android.util.toCurrency |
|
|
|
import net.pokeranalytics.android.util.toCurrency |
|
|
|
|
|
|
|
import timber.log.Timber |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
import kotlin.collections.ArrayList |
|
|
|
import kotlin.collections.ArrayList |
|
|
|
|
|
|
|
|
|
|
|
open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource, LiveDataDataSource, |
|
|
|
open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource, |
|
|
|
RowEditable { |
|
|
|
RowEditable, RowRepresentable, ObjectSavable { |
|
|
|
|
|
|
|
|
|
|
|
@PrimaryKey |
|
|
|
@PrimaryKey |
|
|
|
var id = UUID.randomUUID().toString() |
|
|
|
var id = UUID.randomUUID().toString() |
|
|
|
@ -138,6 +143,22 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Return the duration of the current session |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun getDuration(context: Context): String { |
|
|
|
|
|
|
|
val startDate = timeFrame?.startDate ?: Date() |
|
|
|
|
|
|
|
val enDate = timeFrame?.endDate ?: Date() |
|
|
|
|
|
|
|
return startDate.getDuration(context, enDate) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Return the formatted blinds |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun getBlinds(): String { |
|
|
|
|
|
|
|
return if (cgSmallBlind == null) "--" else "$${cgSmallBlind?.round()}/${cgBigBlind?.round()}" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Delete the object from realm |
|
|
|
* Delete the object from realm |
|
|
|
* TODO: Cascade delete? |
|
|
|
* TODO: Cascade delete? |
|
|
|
@ -179,13 +200,13 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun getGameTitle(): String { |
|
|
|
fun getGameTitle(): String { |
|
|
|
var gameTitle = "" |
|
|
|
var gameTitle = "" |
|
|
|
if (limit != null) { |
|
|
|
limit?.let { |
|
|
|
gameTitle += limit |
|
|
|
gameTitle += Limit.get(it) + " " |
|
|
|
} |
|
|
|
} |
|
|
|
if (game != null) { |
|
|
|
if (game != null) { |
|
|
|
gameTitle += game?.title |
|
|
|
gameTitle += game?.name |
|
|
|
} |
|
|
|
} |
|
|
|
return gameTitle |
|
|
|
return if (gameTitle.isNotBlank()) gameTitle else "--" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
companion object { |
|
|
|
@ -237,6 +258,14 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
return 0.0 |
|
|
|
return 0.0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun uniqueIdentifier(): String { |
|
|
|
|
|
|
|
return this.id |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun getDisplayName(): String { |
|
|
|
|
|
|
|
return "session ${this.creationDate}" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun adapterRows(): ArrayList<RowRepresentable> { |
|
|
|
override fun adapterRows(): ArrayList<RowRepresentable> { |
|
|
|
val rows = ArrayList<RowRepresentable>() |
|
|
|
val rows = ArrayList<RowRepresentable>() |
|
|
|
rows.addAll(SessionRow.getRowsForState(getState())) |
|
|
|
rows.addAll(SessionRow.getRowsForState(getState())) |
|
|
|
@ -251,9 +280,9 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
return when (row) { |
|
|
|
return when (row) { |
|
|
|
SessionRow.BUY_IN -> buyin.toCurrency() |
|
|
|
SessionRow.BUY_IN -> buyin.toCurrency() |
|
|
|
SessionRow.BLINDS -> if (cgSmallBlind != null && cgBigBlind != null) "$cgSmallBlind / $cgBigBlind" else "--" |
|
|
|
SessionRow.BLINDS -> if (cgSmallBlind != null && cgBigBlind != null) "$cgSmallBlind / $cgBigBlind" else "--" |
|
|
|
SessionRow.GAME -> game?.title ?: "--" |
|
|
|
SessionRow.GAME -> getGameTitle() |
|
|
|
SessionRow.LOCATION -> location?.title ?: "--" |
|
|
|
SessionRow.LOCATION -> location?.name ?: "--" |
|
|
|
SessionRow.BANKROLL -> bankroll?.title ?: "--" |
|
|
|
SessionRow.BANKROLL -> bankroll?.name ?: "--" |
|
|
|
SessionRow.TABLE_SIZE -> tableSize?.toString() ?: "--" |
|
|
|
SessionRow.TABLE_SIZE -> tableSize?.toString() ?: "--" |
|
|
|
SessionRow.START_DATE -> if (timeFrame != null) timeFrame?.startDate?.short() ?: "--" else "--" |
|
|
|
SessionRow.START_DATE -> if (timeFrame != null) timeFrame?.startDate?.short() ?: "--" else "--" |
|
|
|
SessionRow.END_DATE -> if (timeFrame != null) timeFrame?.endDate?.short() ?: "--" else "--" |
|
|
|
SessionRow.END_DATE -> if (timeFrame != null) timeFrame?.endDate?.short() ?: "--" else "--" |
|
|
|
@ -271,12 +300,6 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override var title: String = "Change that: $creationDate" |
|
|
|
|
|
|
|
override val primaryKey: String get() = this.id |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun getBottomSheetData(row: RowRepresentable): ArrayList<BottomSheetData> { |
|
|
|
override fun getBottomSheetData(row: RowRepresentable): ArrayList<BottomSheetData> { |
|
|
|
|
|
|
|
|
|
|
|
val data = ArrayList<BottomSheetData>() |
|
|
|
val data = ArrayList<BottomSheetData>() |
|
|
|
@ -288,8 +311,21 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
data.add(BottomSheetData(100.0 * (cgBigBlind ?: 0.0))) |
|
|
|
data.add(BottomSheetData(100.0 * (cgBigBlind ?: 0.0))) |
|
|
|
data.add(BottomSheetData(200.0 * (cgBigBlind ?: 0.0))) |
|
|
|
data.add(BottomSheetData(200.0 * (cgBigBlind ?: 0.0))) |
|
|
|
data.add(BottomSheetData(buyin)) |
|
|
|
data.add(BottomSheetData(buyin)) |
|
|
|
data.add(BottomSheetData("",inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL)) |
|
|
|
data.add( |
|
|
|
data.add(BottomSheetData("",inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL)) |
|
|
|
BottomSheetData( |
|
|
|
|
|
|
|
"", |
|
|
|
|
|
|
|
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
data.add( |
|
|
|
|
|
|
|
BottomSheetData( |
|
|
|
|
|
|
|
"", |
|
|
|
|
|
|
|
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
SessionRow.CASHED_OUT -> { |
|
|
|
|
|
|
|
data.add(BottomSheetData(result?.cashout, inputType = InputType.TYPE_CLASS_NUMBER)) |
|
|
|
} |
|
|
|
} |
|
|
|
SessionRow.TIPS -> { |
|
|
|
SessionRow.TIPS -> { |
|
|
|
// Disable the buttons with value = 0, add current value & set the 2 edit texts |
|
|
|
// Disable the buttons with value = 0, add current value & set the 2 edit texts |
|
|
|
@ -300,7 +336,9 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
data.add(BottomSheetData("", inputType = InputType.TYPE_CLASS_NUMBER)) |
|
|
|
data.add(BottomSheetData("", inputType = InputType.TYPE_CLASS_NUMBER)) |
|
|
|
data.add(BottomSheetData("", inputType = InputType.TYPE_CLASS_NUMBER)) |
|
|
|
data.add(BottomSheetData("", inputType = InputType.TYPE_CLASS_NUMBER)) |
|
|
|
} |
|
|
|
} |
|
|
|
SessionRow.TABLE_SIZE -> {data.add(BottomSheetData(tableSize))} |
|
|
|
SessionRow.TABLE_SIZE -> { |
|
|
|
|
|
|
|
data.add(BottomSheetData(tableSize)) |
|
|
|
|
|
|
|
} |
|
|
|
SessionRow.GAME -> { |
|
|
|
SessionRow.GAME -> { |
|
|
|
// Add current game & games list |
|
|
|
// Add current game & games list |
|
|
|
data.add(BottomSheetData(limit)) |
|
|
|
data.add(BottomSheetData(limit)) |
|
|
|
@ -329,14 +367,33 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
override fun updateValue(value: Any?, row: RowRepresentable) { |
|
|
|
override fun updateValue(value: Any?, row: RowRepresentable) { |
|
|
|
realm.beginTransaction() |
|
|
|
realm.beginTransaction() |
|
|
|
when (row) { |
|
|
|
when (row) { |
|
|
|
|
|
|
|
|
|
|
|
SessionRow.BUY_IN -> { |
|
|
|
SessionRow.BUY_IN -> { |
|
|
|
val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) |
|
|
|
val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) |
|
|
|
localResult.buyin = value as Double |
|
|
|
localResult.buyin = value as Double? |
|
|
|
|
|
|
|
result = localResult |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
SessionRow.CASHED_OUT -> { |
|
|
|
|
|
|
|
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 |
|
|
|
result = localResult |
|
|
|
} |
|
|
|
} |
|
|
|
SessionRow.TABLE_SIZE -> tableSize = value as Int? |
|
|
|
SessionRow.TABLE_SIZE -> tableSize = value as Int? |
|
|
|
SessionRow.GAME -> game = value as Game? |
|
|
|
SessionRow.GAME -> { |
|
|
|
|
|
|
|
if (value is ArrayList<*>) { |
|
|
|
|
|
|
|
Timber.d("${value[0]}") |
|
|
|
|
|
|
|
Timber.d("${value[1]}") |
|
|
|
|
|
|
|
limit = try { |
|
|
|
|
|
|
|
(value[0] as Int?) |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
|
|
null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
game = try { |
|
|
|
|
|
|
|
(value[1] as Game?) |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
|
|
null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
SessionRow.BANKROLL -> bankroll = value as Bankroll? |
|
|
|
SessionRow.BANKROLL -> bankroll = value as Bankroll? |
|
|
|
SessionRow.LOCATION -> location = value as Location? |
|
|
|
SessionRow.LOCATION -> location = value as Location? |
|
|
|
SessionRow.COMMENT -> comment = value as String? ?: "" |
|
|
|
SessionRow.COMMENT -> comment = value as String? ?: "" |
|
|
|
@ -351,20 +408,28 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource |
|
|
|
} catch (e: Exception) { |
|
|
|
} catch (e: Exception) { |
|
|
|
null |
|
|
|
null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cgBigBlind?.let { |
|
|
|
|
|
|
|
if (cgSmallBlind == null || cgSmallBlind == 0.0) { |
|
|
|
|
|
|
|
cgSmallBlind = it / 2.0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//TODO: Update |
|
|
|
//TODO: Update |
|
|
|
SessionRow.START_DATE -> if (value is Date?) { |
|
|
|
SessionRow.START_DATE -> if (value is Date?) { |
|
|
|
if (value == null) { |
|
|
|
if (value == null) { |
|
|
|
timeFrame = null |
|
|
|
timeFrame = null |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
val timeFrameToUpdate = if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) |
|
|
|
val timeFrameToUpdate = |
|
|
|
|
|
|
|
if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) |
|
|
|
timeFrameToUpdate.setDate(value, null) |
|
|
|
timeFrameToUpdate.setDate(value, null) |
|
|
|
timeFrame = timeFrameToUpdate |
|
|
|
timeFrame = timeFrameToUpdate |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//TODO: Update |
|
|
|
//TODO: Update |
|
|
|
SessionRow.END_DATE -> if (value is Date?) { |
|
|
|
SessionRow.END_DATE -> if (value is Date?) { |
|
|
|
val timeFrameToUpdate = if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) |
|
|
|
val timeFrameToUpdate = |
|
|
|
|
|
|
|
if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) |
|
|
|
timeFrameToUpdate.setDate(null, value) |
|
|
|
timeFrameToUpdate.setDate(null, value) |
|
|
|
timeFrame = timeFrameToUpdate |
|
|
|
timeFrame = timeFrameToUpdate |
|
|
|
} |
|
|
|
} |
|
|
|
@ -378,70 +443,3 @@ enum class TournamentKind { |
|
|
|
MTT, |
|
|
|
MTT, |
|
|
|
SNG |
|
|
|
SNG |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Session Dao |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
class SessionDao(realmDb: Realm) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var realm: Realm = realmDb |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Create or update session |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun createOrUpdateSession(session: Session): Session { |
|
|
|
|
|
|
|
realm.beginTransaction() |
|
|
|
|
|
|
|
val sessionToSave = realm.copyToRealmOrUpdate(session) |
|
|
|
|
|
|
|
realm.commitTransaction() |
|
|
|
|
|
|
|
return realm.copyFromRealm(sessionToSave) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Create or update sessions |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun createOrUpdateSessions(sessions: List<Session>): List<Session> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
realm.beginTransaction() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update |
|
|
|
|
|
|
|
val sessionsToSave = realm.copyToRealmOrUpdate(sessions) |
|
|
|
|
|
|
|
realm.commitTransaction() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return realm.copyFromRealm(sessionsToSave) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Find all sessions |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun findAllSessions(): RealmResults<Session> { |
|
|
|
|
|
|
|
return realm.where(Session::class.java).findAll().sort("creationDate", Sort.DESCENDING) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Find session by id |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun findSessionById(sessionId: Int): Session? { |
|
|
|
|
|
|
|
return realm.copyFromRealm(realm.where(Session::class.java).equalTo("id", sessionId).findFirst()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Delete session |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun deleteSession(sessionId: Int) { |
|
|
|
|
|
|
|
realm.beginTransaction() |
|
|
|
|
|
|
|
realm.sessionDao().findSessionById(sessionId)?.deleteFromRealm() |
|
|
|
|
|
|
|
realm.commitTransaction() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Delete all sessions |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun deleteAllSessions() { |
|
|
|
|
|
|
|
realm.beginTransaction() |
|
|
|
|
|
|
|
realm.sessionDao().findAllSessions().deleteAllFromRealm() |
|
|
|
|
|
|
|
realm.commitTransaction() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|