|
|
|
|
@ -176,40 +176,59 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
if (sessionId != null) { |
|
|
|
|
|
|
|
|
|
val sessionRealm = realm.findById<Session>(sessionId) |
|
|
|
|
if (sessionRealm != null) { |
|
|
|
|
sessionRealm?.let { |
|
|
|
|
val copy = realm.copyFromRealm(it) |
|
|
|
|
|
|
|
|
|
if (this.model.duplicate) { // duplicate session |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
val session = sessionRealm.duplicate() |
|
|
|
|
// realm.executeTransaction { |
|
|
|
|
val session = copy.duplicate() |
|
|
|
|
currentSession = session |
|
|
|
|
} |
|
|
|
|
// } |
|
|
|
|
sessionHasBeenUserCustomized = false |
|
|
|
|
} else { // show existing session |
|
|
|
|
currentSession = sessionRealm |
|
|
|
|
currentSession = copy |
|
|
|
|
sessionHasBeenUserCustomized = true |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
throw PAIllegalStateException("Session cannot be null here, session id = $sessionId") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} ?: throw PAIllegalStateException("Session cannot be null here, session id = $sessionId") |
|
|
|
|
|
|
|
|
|
// if (sessionRealm != null) { |
|
|
|
|
// |
|
|
|
|
// if (this.model.duplicate) { // duplicate session |
|
|
|
|
// realm.executeTransaction { |
|
|
|
|
// val session = sessionRealm.duplicate() |
|
|
|
|
// currentSession = session |
|
|
|
|
// } |
|
|
|
|
// sessionHasBeenUserCustomized = false |
|
|
|
|
// } else { // show existing session |
|
|
|
|
// currentSession = sessionRealm |
|
|
|
|
// sessionHasBeenUserCustomized = true |
|
|
|
|
// } |
|
|
|
|
// } else { |
|
|
|
|
// throw PAIllegalStateException("Session cannot be null here, session id = $sessionId") |
|
|
|
|
// } |
|
|
|
|
} else { // create new session |
|
|
|
|
realm.executeTransaction { executeRealm -> |
|
|
|
|
currentSession = Session.newInstance(executeRealm, this.model.isTournament) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, null, requireContext()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
currentSession = Session.newInstance(realm, this.model.isTournament) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(realm, currentSession, null, requireContext()) |
|
|
|
|
|
|
|
|
|
// realm.executeTransaction { executeRealm -> |
|
|
|
|
// currentSession = Session.newInstance(executeRealm, this.model.isTournament) |
|
|
|
|
// FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, null, requireContext()) |
|
|
|
|
// } |
|
|
|
|
// Find the nearest location around the user |
|
|
|
|
parentActivity?.findNearestLocation { |
|
|
|
|
it?.let { location -> |
|
|
|
|
realm.executeTransaction { executeRealm -> |
|
|
|
|
val realmLocation = executeRealm.findById<Location>(location.id) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, realmLocation, requireContext()) |
|
|
|
|
|
|
|
|
|
// realm.executeTransaction { executeRealm -> |
|
|
|
|
val realmLocation = realm.findById<Location>(location.id) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(realm, currentSession, realmLocation, requireContext()) |
|
|
|
|
currentSession.location = realmLocation |
|
|
|
|
} |
|
|
|
|
// } |
|
|
|
|
this.updateSessionUI(true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sessionHasBeenUserCustomized = false |
|
|
|
|
this.sessionHasBeenUserCustomized = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -251,15 +270,23 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
|
|
|
|
|
|
|
|
|
Timber.d("value changed: $value, row: $row") |
|
|
|
|
this.sessionHasBeenUserCustomized = true |
|
|
|
|
try { |
|
|
|
|
getRealm().executeTransaction { |
|
|
|
|
this.currentSession.updateValue(value, row) |
|
|
|
|
} |
|
|
|
|
} catch (e: PAIllegalStateException) { |
|
|
|
|
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
updateSessionThenSaveAsynchronously { session -> |
|
|
|
|
session.updateValue(value, row) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// try { |
|
|
|
|
// this.currentSession.updateValue(value, row) |
|
|
|
|
// getRealm().executeTransactionAsync { realm -> |
|
|
|
|
// realm.copyToRealmOrUpdate(this.currentSession) |
|
|
|
|
// } |
|
|
|
|
// } catch (e: PAIllegalStateException) { |
|
|
|
|
// Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() |
|
|
|
|
// return |
|
|
|
|
// } |
|
|
|
|
this.sessionAdapter.refreshRow(row) |
|
|
|
|
when (row) { |
|
|
|
|
SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE, SessionPropertiesRow.NET_RESULT, |
|
|
|
|
@ -273,6 +300,18 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun updateSessionThenSaveAsynchronously(handler: (Session) -> (Unit)) { |
|
|
|
|
try { |
|
|
|
|
handler(this.currentSession) |
|
|
|
|
} catch (e: PAIllegalStateException) { |
|
|
|
|
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
getRealm().executeTransactionAsync { realm -> |
|
|
|
|
realm.copyToRealmOrUpdate(this.currentSession) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update the UI with the session data |
|
|
|
|
* Should be called after the initialization of the session |
|
|
|
|
@ -386,12 +425,20 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
currentSession.startOrContinue() |
|
|
|
|
updateSessionThenSaveAsynchronously { session -> |
|
|
|
|
session.startOrContinue() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// currentSession.startOrContinue() |
|
|
|
|
binding.recyclerView.smoothScrollToPosition(0) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
SessionState.STARTED -> { |
|
|
|
|
currentSession.pause() |
|
|
|
|
updateSessionThenSaveAsynchronously { session -> |
|
|
|
|
session.pause() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// currentSession.pause() |
|
|
|
|
} |
|
|
|
|
else -> { |
|
|
|
|
} |
|
|
|
|
@ -435,7 +482,9 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
* Stop the current session |
|
|
|
|
*/ |
|
|
|
|
private fun stopSession() { |
|
|
|
|
this.currentSession.stop(requireContext()) |
|
|
|
|
updateSessionThenSaveAsynchronously { session -> |
|
|
|
|
session.stop(requireContext()) |
|
|
|
|
} |
|
|
|
|
updateSessionUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -454,7 +503,9 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
* Restart timer |
|
|
|
|
*/ |
|
|
|
|
private fun restartTimer() { |
|
|
|
|
currentSession.restart() |
|
|
|
|
updateSessionThenSaveAsynchronously { session -> |
|
|
|
|
session.restart() |
|
|
|
|
} |
|
|
|
|
updateSessionUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -478,7 +529,14 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr |
|
|
|
|
|
|
|
|
|
val bankrollId = this.currentSession.bankroll?.id |
|
|
|
|
|
|
|
|
|
this.currentSession.delete() |
|
|
|
|
val sessionId = this.currentSession.id |
|
|
|
|
getRealm().executeTransactionAsync { realm -> |
|
|
|
|
realm.findById<Session>(sessionId)?.let { session -> |
|
|
|
|
session.delete() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// this.currentSession.delete() |
|
|
|
|
bankrollId?.let { |
|
|
|
|
BankrollReportManager.notifyBankrollReportImpact(bankrollId) |
|
|
|
|
} |
|
|
|
|
|