Compare commits

...

33 Commits

Author SHA1 Message Date
Laurent c08396b638 more work in progress 3 years ago
Laurent dc90960bcb Fixes duration calculation 3 years ago
Laurent 2aeb2bf48f Fix warning 3 years ago
Laurent 1ec2a438dc Fixes in duration calculus 3 years ago
Laurent 929365fc4c RealmWriteService + FlatTimeInterval for better performance 3 years ago
Laurent 526e50f8e4 Replace foreach by for loop 3 years ago
Laurent 897098499f Update 3 years ago
Laurent 7c64408749 Block stuff 3 years ago
Laurent 47b30f1788 enable listeners 3 years ago
Laurent d9638ce516 changes and improvements 3 years ago
Laurent 887dea91da Avoid notification for hourly rate when there is not much data 3 years ago
Laurent 9c99ef9c35 version bump to 6.0.4 3 years ago
Laurent 7fe851d072 cleanup 3 years ago
Laurent 54776dc6e7 Revert previous commit 3 years ago
Laurent af3c5ffe26 Removes useles primary key from Result 3 years ago
Laurent c95ab684d8 Fixes issue in CSV import 3 years ago
Laurent bd2c17909b Fixes filters 3 years ago
Laurent 6a1249a78c Fixes custom field entries on async writes 3 years ago
Laurent 3eeb8379bd cleanup 3 years ago
Laurent 1bd5876845 Add comment 3 years ago
Laurent 323a3e3bbc Fix crash 3 years ago
Laurent 35ef3047b7 Fix crash when adding transaction 3 years ago
Laurent 676bd0c94e cleanup 3 years ago
Laurent 3d8991baab Fixes various issues with async writes 3 years ago
Laurent 99cf2d465a Fixes missing ComputableResults in sessions 3 years ago
Laurent 0aac37b288 Fixes issue when creating subdata 3 years ago
Laurent e0279fcc85 Fixes crashes 3 years ago
Laurent e94a677cbb Fixes crash 3 years ago
Laurent 621357610b Put async transaction everywhere 3 years ago
Laurent 4ab1f561d8 fix 3 years ago
Laurent bc4bc3ca2e Improvement and cleanup 3 years ago
Laurent 56019357b4 Refactor executeTransaction into executeTransactionAsync 3 years ago
Laurent c99ef285af coroutines changes 3 years ago
  1. 25
      app/build.gradle
  2. 3
      app/src/androidTest/java/net/pokeranalytics/android/components/RealmInstrumentedUnitTest.kt
  3. 6
      app/src/androidTest/java/net/pokeranalytics/android/model/CriteriaTest.kt
  4. 3
      app/src/androidTest/java/net/pokeranalytics/android/performanceTests/PerfsInstrumentedUnitTest.kt
  5. 5
      app/src/androidTest/java/net/pokeranalytics/android/unitTests/BankrollInstrumentedUnitTest.kt
  6. 24
      app/src/androidTest/java/net/pokeranalytics/android/unitTests/StatsInstrumentedUnitTest.kt
  7. 1
      app/src/main/AndroidManifest.xml
  8. 76
      app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt
  9. 56
      app/src/main/java/net/pokeranalytics/android/RealmWriteService.kt
  10. 2
      app/src/main/java/net/pokeranalytics/android/api/CurrencyConverterApi.kt
  11. 268
      app/src/main/java/net/pokeranalytics/android/calculus/Calculator.kt
  12. 7
      app/src/main/java/net/pokeranalytics/android/calculus/ComputableGroup.kt
  13. 78
      app/src/main/java/net/pokeranalytics/android/calculus/ReportWhistleBlower.kt
  14. 14
      app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt
  15. 10
      app/src/main/java/net/pokeranalytics/android/calculus/bankroll/BankrollCalculator.kt
  16. 21
      app/src/main/java/net/pokeranalytics/android/calculus/bankroll/BankrollReportManager.kt
  17. 12
      app/src/main/java/net/pokeranalytics/android/calculus/optimalduration/CashGameOptimalDurationCalculator.kt
  18. 1
      app/src/main/java/net/pokeranalytics/android/exceptions/Exceptions.kt
  19. 20
      app/src/main/java/net/pokeranalytics/android/model/Criteria.kt
  20. 27
      app/src/main/java/net/pokeranalytics/android/model/LiveData.kt
  21. 2
      app/src/main/java/net/pokeranalytics/android/model/extensions/SessionExtensions.kt
  22. 7
      app/src/main/java/net/pokeranalytics/android/model/filter/Filterable.kt
  23. 10
      app/src/main/java/net/pokeranalytics/android/model/filter/Query.kt
  24. 2
      app/src/main/java/net/pokeranalytics/android/model/filter/QueryCondition.kt
  25. 57
      app/src/main/java/net/pokeranalytics/android/model/migrations/Patcher.kt
  26. 22
      app/src/main/java/net/pokeranalytics/android/model/migrations/PokerAnalyticsMigration.kt
  27. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/Comment.kt
  28. 10
      app/src/main/java/net/pokeranalytics/android/model/realm/ComputableResult.kt
  29. 26
      app/src/main/java/net/pokeranalytics/android/model/realm/CustomField.kt
  30. 10
      app/src/main/java/net/pokeranalytics/android/model/realm/CustomFieldEntry.kt
  31. 6
      app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt
  32. 10
      app/src/main/java/net/pokeranalytics/android/model/realm/FilterCondition.kt
  33. 61
      app/src/main/java/net/pokeranalytics/android/model/realm/FlatTimeInterval.kt
  34. 9
      app/src/main/java/net/pokeranalytics/android/model/realm/Game.kt
  35. 4
      app/src/main/java/net/pokeranalytics/android/model/realm/Player.kt
  36. 59
      app/src/main/java/net/pokeranalytics/android/model/realm/Result.kt
  37. 268
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  38. 27
      app/src/main/java/net/pokeranalytics/android/model/realm/SessionSet.kt
  39. 288
      app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt
  40. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/TournamentFeature.kt
  41. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/TournamentName.kt
  42. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/Transaction.kt
  43. 15
      app/src/main/java/net/pokeranalytics/android/model/realm/UserConfig.kt
  44. 10
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt
  45. 14
      app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt
  46. 209
      app/src/main/java/net/pokeranalytics/android/model/utils/SessionSetManager.kt
  47. 534
      app/src/main/java/net/pokeranalytics/android/model/utils/TimeManager.kt
  48. 10
      app/src/main/java/net/pokeranalytics/android/ui/activity/HomeActivity.kt
  49. 28
      app/src/main/java/net/pokeranalytics/android/ui/activity/components/BaseActivity.kt
  50. 13
      app/src/main/java/net/pokeranalytics/android/ui/activity/components/MediaActivity.kt
  51. 36
      app/src/main/java/net/pokeranalytics/android/ui/fragment/ImportFragment.kt
  52. 5
      app/src/main/java/net/pokeranalytics/android/ui/fragment/ReportCreationFragment.kt
  53. 29
      app/src/main/java/net/pokeranalytics/android/ui/fragment/ReportsFragment.kt
  54. 6
      app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt
  55. 30
      app/src/main/java/net/pokeranalytics/android/ui/fragment/StatisticsFragment.kt
  56. 5
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/BaseFragment.kt
  57. 24
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/DeletableItemFragment.kt
  58. 77
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/RealmFragment.kt
  59. 12
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetDataListFragment.kt
  60. 21
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetDataMultiSelectionFragment.kt
  61. 51
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt
  62. 9
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetListGameFragment.kt
  63. 2
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetStaticListFragment.kt
  64. 16
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/AbstractReportFragment.kt
  65. 27
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/ComposableTableReportFragment.kt
  66. 15
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/ProgressReportFragment.kt
  67. 2
      app/src/main/java/net/pokeranalytics/android/ui/helpers/AppReviewManager.kt
  68. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/bankroll/BankrollFragment.kt
  69. 3
      app/src/main/java/net/pokeranalytics/android/ui/modules/calendar/CalendarDetailsFragment.kt
  70. 163
      app/src/main/java/net/pokeranalytics/android/ui/modules/calendar/CalendarFragment.kt
  71. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/CustomFieldDataFragment.kt
  72. 55
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/DataManagerFragment.kt
  73. 13
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/EditableDataFragment.kt
  74. 24
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/TransactionDataFragment.kt
  75. 6
      app/src/main/java/net/pokeranalytics/android/ui/modules/datalist/DataListFragment.kt
  76. 96
      app/src/main/java/net/pokeranalytics/android/ui/modules/feed/FeedFragment.kt
  77. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/feed/FeedHandHistoryRowRepresentableAdapter.kt
  78. 12
      app/src/main/java/net/pokeranalytics/android/ui/modules/feed/FeedSessionRowRepresentableAdapter.kt
  79. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/feed/FeedTransactionRowRepresentableAdapter.kt
  80. 7
      app/src/main/java/net/pokeranalytics/android/ui/modules/feed/NewDataMenuActivity.kt
  81. 38
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterDetailsFragment.kt
  82. 11
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterDetailsViewModel.kt
  83. 4
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterHandler.kt
  84. 30
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterViewModel.kt
  85. 24
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FiltersFragment.kt
  86. 4
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FiltersListFragment.kt
  87. 16
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/editor/EditorFragment.kt
  88. 81
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/EditorViewModel.kt
  89. 111
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/replayer/ReplayExportService.kt
  90. 182
      app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt
  91. 10
      app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionViewModel.kt
  92. 12
      app/src/main/java/net/pokeranalytics/android/ui/modules/settings/DealtHandsPerHourFragment.kt
  93. 10
      app/src/main/java/net/pokeranalytics/android/ui/modules/settings/TransactionFilterFragment.kt
  94. 7
      app/src/main/java/net/pokeranalytics/android/ui/view/rows/FilterCategoryRow.kt
  95. 43
      app/src/main/java/net/pokeranalytics/android/ui/view/rows/FilterSectionRow.kt
  96. 8
      app/src/main/java/net/pokeranalytics/android/ui/view/rows/StaticReport.kt
  97. 34
      app/src/main/java/net/pokeranalytics/android/ui/viewmodel/BottomSheetViewModel.kt
  98. 12
      app/src/main/java/net/pokeranalytics/android/ui/viewmodel/DataManagerViewModel.kt
  99. 4
      app/src/main/java/net/pokeranalytics/android/util/FakeDataManager.kt
  100. 6
      app/src/main/java/net/pokeranalytics/android/util/Global.kt
  101. Some files were not shown because too many files have changed in this diff Show More

@ -12,7 +12,7 @@ apply plugin: "kotlinx-serialization"
repositories { repositories {
maven { url 'https://jitpack.io' } // required for MPAndroidChart maven { url 'https://jitpack.io' } // required for MPAndroidChart
jcenter() // for kotlin serialization mavenCentral() // for kotlin serialization
} }
android { android {
@ -37,8 +37,8 @@ android {
applicationId "net.pokeranalytics.android" applicationId "net.pokeranalytics.android"
minSdkVersion 23 minSdkVersion 23
targetSdkVersion 32 targetSdkVersion 32
versionCode 146 versionCode 147
versionName "6.0.3" versionName "6.0.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
@ -98,8 +98,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
// Kotlin // Kotlin
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-native-mt"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
// implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0") // JVM dependency // implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0") // JVM dependency
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
@ -141,16 +141,19 @@ dependencies {
// ffmpeg for encoding video (HH export) // ffmpeg for encoding video (HH export)
implementation 'com.arthenica:ffmpeg-kit-min-gpl:4.4.LTS' implementation 'com.arthenica:ffmpeg-kit-min-gpl:4.4.LTS'
// https://mvnrepository.com/artifact/com.android.volley/volley
implementation 'com.android.volley:volley:1.2.1'
// Instrumented Tests // Instrumented Tests
androidTestImplementation 'androidx.test:core:1.3.0' androidTestImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test:runner:1.3.0' androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.3.0' androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5'
// Test // Test
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
testImplementation 'com.android.support.test:runner:1.0.2' testImplementation 'androidx.test.ext:junit:1.1.5'
testImplementation 'com.android.support.test:rules:1.0.2' testImplementation 'androidx.test:rules:1.5.0'
// gross, somehow needed to make the stop notif work // gross, somehow needed to make the stop notif work
implementation 'com.google.guava:guava:27.0.1-android' implementation 'com.google.guava:guava:27.0.1-android'

@ -20,8 +20,9 @@ open class RealmInstrumentedUnitTest {
fun newSessionInstance(realm: Realm, isCashGame: Boolean = true) : Session { fun newSessionInstance(realm: Realm, isCashGame: Boolean = true) : Session {
val session = realm.createObject(Session::class.java, UUID.randomUUID().toString()) val session = realm.createObject(Session::class.java, UUID.randomUUID().toString())
session.startDate = Date() session.startDate = Date()
session.endDate = Date()
session.type = if (isCashGame) Session.Type.CASH_GAME.ordinal else Session.Type.TOURNAMENT.ordinal session.type = if (isCashGame) Session.Type.CASH_GAME.ordinal else Session.Type.TOURNAMENT.ordinal
session.result = realm.createObject(Result::class.java) session.result = realm.createObject(Result::class.java, UUID.randomUUID().toString())
return session return session
} }

@ -34,7 +34,7 @@ class CriteriaTest : BaseFilterInstrumentedUnitTest() {
realm.commitTransaction() realm.commitTransaction()
val yearQueries = Criteria.Years.queryConditions val yearQueries = Criteria.Years.queryConditions(realm)
assertEquals(16, yearQueries.size) assertEquals(16, yearQueries.size)
@ -57,7 +57,7 @@ class CriteriaTest : BaseFilterInstrumentedUnitTest() {
fun combined() { fun combined() {
val criterias = listOf(Criteria.MonthsOfYear, Criteria.DaysOfWeek) val criterias = listOf(Criteria.MonthsOfYear, Criteria.DaysOfWeek)
val combined = criterias.combined() val combined = criterias.combined(this.mockRealm)
val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context
combined.forEach { combined.forEach {
@ -92,7 +92,7 @@ class CriteriaTest : BaseFilterInstrumentedUnitTest() {
realm.commitTransaction() realm.commitTransaction()
val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context
val allMonths = Criteria.AllMonthsUpToNow.queries val allMonths = Criteria.AllMonthsUpToNow.queries(realm)
allMonths.forEach { allMonths.forEach {
it.conditions.forEach { qc-> it.conditions.forEach { qc->
println("<<<<< ${qc.getDisplayName(context)}") println("<<<<< ${qc.getDisplayName(context)}")

@ -39,8 +39,7 @@ class PerfsInstrumentedUnitTest : RealmInstrumentedUnitTest() {
Seed(app).execute(realm) Seed(app).execute(realm)
realm.commitTransaction() realm.commitTransaction()
FakeDataManager.createFakeSessions(5000) { success ->
FakeDataManager.createFakeSessions(5000) {success ->
if (success) { if (success) {

@ -16,7 +16,7 @@ import java.util.*
class BankrollInstrumentedUnitTest : SessionInstrumentedUnitTest() { class BankrollInstrumentedUnitTest : SessionInstrumentedUnitTest() {
private fun createDefaultTransactionTypes(realm: Realm) { private fun createDefaultTransactionTypes(realm: Realm) {
TransactionType.Value.values().forEachIndexed { index, value -> TransactionType.Value.values().forEachIndexed { _, value ->
val type = TransactionType() val type = TransactionType()
type.additive = value.additive type.additive = value.additive
type.kind = value.uniqueIdentifier type.kind = value.uniqueIdentifier
@ -64,10 +64,12 @@ class BankrollInstrumentedUnitTest : SessionInstrumentedUnitTest() {
val s1 = newSessionInstance(realm) val s1 = newSessionInstance(realm)
s1.bankroll = br1 s1.bankroll = br1
s1.result?.cashout = 200.0 s1.result?.cashout = 200.0
s1.preCompute()
val s2 = newSessionInstance(realm) val s2 = newSessionInstance(realm)
s2.bankroll = br2 s2.bankroll = br2
s2.result?.cashout = 500.0 s2.result?.cashout = 500.0
s2.preCompute()
} }
@ -113,6 +115,7 @@ class BankrollInstrumentedUnitTest : SessionInstrumentedUnitTest() {
val s1 = newSessionInstance(realm) val s1 = newSessionInstance(realm)
s1.bankroll = br1 s1.bankroll = br1
s1.result?.cashout = 200.0 s1.result?.cashout = 200.0
s1.endDate = Date()
} }

@ -65,6 +65,9 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
s1.location = l1 s1.location = l1
s2.location = l1 s2.location = l1
s1.preCompute()
s2.preCompute()
realm.commitTransaction() realm.commitTransaction()
assertEquals(2, computableResults.size) assertEquals(2, computableResults.size)
@ -186,13 +189,13 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
Assert.fail("No std100 stat") Assert.fail("No std100 stat")
} }
results.computedStat(Stat.MAXIMUM_NETRESULT)?.let { results.computedStat(Stat.MAXIMUM_NET_RESULT)?.let {
assertEquals(300.0, it.value, delta) assertEquals(300.0, it.value, delta)
} ?: run { } ?: run {
Assert.fail("No MAXIMUM_NETRESULT") Assert.fail("No MAXIMUM_NETRESULT")
} }
results.computedStat(Stat.MINIMUM_NETRESULT)?.let { results.computedStat(Stat.MINIMUM_NET_RESULT)?.let {
assertEquals(-100.0, it.value, delta) assertEquals(-100.0, it.value, delta)
} ?: run { } ?: run {
Assert.fail("No MINIMUM_NETRESULT") Assert.fail("No MINIMUM_NETRESULT")
@ -249,6 +252,9 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
// netDuration = 1h, hourly = -100, bb100 = -200bb / 25hands * 100 = -800 // netDuration = 1h, hourly = -100, bb100 = -200bb / 25hands * 100 = -800
// netDuration = 4h, hourly = 100, bb100 = 150 / 75 * 100 = +200 // netDuration = 4h, hourly = 100, bb100 = 150 / 75 * 100 = +200
s1.preCompute()
s2.preCompute()
} }
val stats: List<Stat> = listOf(Stat.NET_RESULT, Stat.AVERAGE) val stats: List<Stat> = listOf(Stat.NET_RESULT, Stat.AVERAGE)
@ -316,6 +322,10 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
realm.copyToRealmOrUpdate(s2) realm.copyToRealmOrUpdate(s2)
realm.copyToRealmOrUpdate(s3) realm.copyToRealmOrUpdate(s3)
s1.preCompute()
s2.preCompute()
s3.preCompute()
realm.commitTransaction() realm.commitTransaction()
val stats: List<Stat> = listOf(Stat.NET_RESULT, Stat.AVERAGE) val stats: List<Stat> = listOf(Stat.NET_RESULT, Stat.AVERAGE)
@ -454,6 +464,9 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
s1.endDate = ed1 s1.endDate = ed1
realm.copyToRealmOrUpdate(s1) realm.copyToRealmOrUpdate(s1)
s1.preCompute()
realm.commitTransaction() realm.commitTransaction()
val sets = realm.where(SessionSet::class.java).findAll() val sets = realm.where(SessionSet::class.java).findAll()
@ -498,6 +511,9 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
realm.copyToRealmOrUpdate(s1) realm.copyToRealmOrUpdate(s1)
realm.copyToRealmOrUpdate(s2) realm.copyToRealmOrUpdate(s2)
s1.preCompute()
s2.preCompute()
realm.commitTransaction() realm.commitTransaction()
val sets = realm.where(SessionSet::class.java).findAll() val sets = realm.where(SessionSet::class.java).findAll()
@ -731,6 +747,10 @@ class StatsInstrumentedUnitTest : SessionInstrumentedUnitTest() {
s2.startDate = sd2 s2.startDate = sd2
s2.endDate = ed2 s2.endDate = ed2
s1.preCompute()
s2.preCompute()
} }
val group = ComputableGroup(Query(), listOf()) val group = ComputableGroup(Query(), listOf())

@ -188,6 +188,7 @@
android:launchMode="singleTop"/> android:launchMode="singleTop"/>
<service android:name="net.pokeranalytics.android.ui.modules.handhistory.replayer.ReplayExportService" android:exported="false"/> <service android:name="net.pokeranalytics.android.ui.modules.handhistory.replayer.ReplayExportService" android:exported="false"/>
<service android:name="net.pokeranalytics.android.RealmWriteService" android:exported="false"/>
<meta-data <meta-data
android:name="preloaded_fonts" android:name="preloaded_fonts"

@ -1,19 +1,27 @@
package net.pokeranalytics.android package net.pokeranalytics.android
import android.app.Application import android.app.Application
import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Build import android.os.Build
import android.os.IBinder
import com.google.firebase.FirebaseApp import com.google.firebase.FirebaseApp
import io.realm.Realm import io.realm.Realm
import io.realm.RealmConfiguration import io.realm.RealmConfiguration
import io.realm.kotlin.where import io.realm.kotlin.where
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.calculus.ReportWhistleBlower import net.pokeranalytics.android.calculus.ReportWhistleBlower
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.migrations.Patcher import net.pokeranalytics.android.model.migrations.Patcher
import net.pokeranalytics.android.model.migrations.PokerAnalyticsMigration import net.pokeranalytics.android.model.migrations.PokerAnalyticsMigration
import net.pokeranalytics.android.model.realm.FlatTimeInterval
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.utils.Seed import net.pokeranalytics.android.model.utils.Seed
import net.pokeranalytics.android.model.utils.TimeManager
import net.pokeranalytics.android.util.CrashLogging import net.pokeranalytics.android.util.CrashLogging
import net.pokeranalytics.android.util.FakeDataManager import net.pokeranalytics.android.util.FakeDataManager
import net.pokeranalytics.android.util.PokerAnalyticsLogs import net.pokeranalytics.android.util.PokerAnalyticsLogs
@ -22,11 +30,16 @@ import net.pokeranalytics.android.util.billing.AppGuard
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
object AppState {
var isImporting = false
}
class PokerAnalyticsApplication : Application() { class PokerAnalyticsApplication : Application() {
var reportWhistleBlower: ReportWhistleBlower? = null var reportWhistleBlower: ReportWhistleBlower? = null
private var realmWriteService: RealmWriteService? = null
companion object { companion object {
fun timeSinceInstall(context: Context): Long { fun timeSinceInstall(context: Context): Long {
@ -52,13 +65,14 @@ class PokerAnalyticsApplication : Application() {
Realm.init(this) Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder() val realmConfiguration = RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME) .name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(14) .schemaVersion(15)
.allowWritesOnUiThread(true)
.migration(PokerAnalyticsMigration()) .migration(PokerAnalyticsMigration())
.initialData(Seed(this)) .initialData(Seed(this))
.build() .build()
Realm.setDefaultConfiguration(realmConfiguration) Realm.setDefaultConfiguration(realmConfiguration)
initRealmWriteService()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val locales = resources.configuration.locales val locales = resources.configuration.locales
CrashLogging.log("App onCreate. Locales = $locales") CrashLogging.log("App onCreate. Locales = $locales")
@ -80,17 +94,65 @@ class PokerAnalyticsApplication : Application() {
// Patch // Patch
Patcher.patchAll(this) Patcher.patchAll(this)
// Report // Reports
this.reportWhistleBlower = ReportWhistleBlower(this.applicationContext) this.reportWhistleBlower = ReportWhistleBlower(this.applicationContext)
TimeManager.configure()
// Infos // Infos
val locale = Locale.getDefault() val locale = Locale.getDefault()
CrashLogging.log("Country: ${locale.country}, language: ${locale.language}") CrashLogging.log("Country: ${locale.country}, language: ${locale.language}")
// Realm.getDefaultInstance().executeTransaction { // Debugging
// it.delete(Performance::class.java) val realm = Realm.getDefaultInstance()
val emptyFTI = realm.where(FlatTimeInterval::class.java).isEmpty("sessions").findAll()
if (emptyFTI.isNotEmpty()) {
Timber.w(">>> WARNING: There are ${emptyFTI.size} FTIs without sessions")
// Timber.w(">>> DELETING THE EMPTY FTIs")
// realm.executeTransactionAsync {
// it.where(FlatTimeInterval::class.java).isEmpty("sessions").findAll().deleteAllFromRealm()
// } // }
}
val ftis = realm.where(FlatTimeInterval::class.java).sort("startDate").findAll()
Timber.d(">>> Total FTIs count = ${ftis.size}")
ftis.forEach {
Timber.d("fti > ${it.startDate} / ${it.endDate}")
}
Timber.d("================")
val sessions = realm.where<Session>().findAll()
sessions.forEach {
Timber.d("Session FTI count = ${it.flatTimeIntervals.size}")
}
}
/** Defines callbacks for service binding, passed to bindService() */
private val connection = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
realmWriteService = null
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val binder = service as RealmWriteService.LocalBinder
realmWriteService = binder.getService()
}
}
private fun initRealmWriteService() {
val intent = Intent(this, RealmWriteService::class.java)
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
override fun onTerminate() {
super.onTerminate()
unbindService(connection)
}
fun executeRealmAsyncTransaction(handler: (Realm) -> (Unit)) {
this.realmWriteService?.executeRealmAsyncTransaction(handler) ?: throw PAIllegalStateException("no realmWriteService")
} }
/** /**
@ -103,7 +165,7 @@ class PokerAnalyticsApplication : Application() {
realm.close() realm.close()
if (sessionsCount < 10) { if (sessionsCount < 10) {
GlobalScope.launch { CoroutineScope(Dispatchers.Default).launch {
FakeDataManager.createFakeSessions(500) FakeDataManager.createFakeSessions(500)
} }
} }

@ -0,0 +1,56 @@
package net.pokeranalytics.android
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import io.realm.Realm
import timber.log.Timber
class RealmWriteService : Service() {
private lateinit var realm: Realm
private val binder = LocalBinder()
override fun onBind(intent: Intent?): IBinder {
return binder
}
inner class LocalBinder : Binder() {
fun getService(): RealmWriteService = this@RealmWriteService
}
override fun onCreate() {
super.onCreate()
this.realm = Realm.getDefaultInstance()
}
override fun onDestroy() {
super.onDestroy()
Timber.d(">>>> Service destroyed : realm close")
this.realm.close()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
fun executeRealmAsyncTransaction(handler: (Realm) -> (Unit)) {
Timber.d(">>>> Launch async transaction...")
this.realm.executeTransactionAsync({ asyncRealm ->
handler(asyncRealm)
Timber.d(">> transaction handler done")
}, {
Timber.d(">> onSuccess, refreshing...")
this.realm.refresh()
}, {
Timber.d(">> transaction failed: $it")
})
}
}

@ -18,7 +18,7 @@ class CurrencyConverterApi {
companion object { companion object {
val json = Json { ignoreUnknownKeys = true } private val json = Json { ignoreUnknownKeys = true }
fun currencyRate(fromCurrency: String, toCurrency: String, context: Context, callback: (Double?, VolleyError?) -> (Unit)) { fun currencyRate(fromCurrency: String, toCurrency: String, context: Context, callback: (Double?, VolleyError?) -> (Unit)) {

@ -6,10 +6,10 @@ import net.pokeranalytics.android.calculus.Stat.*
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.Criteria import net.pokeranalytics.android.model.Criteria
import net.pokeranalytics.android.model.combined import net.pokeranalytics.android.model.combined
import net.pokeranalytics.android.model.extensions.hourlyDuration
import net.pokeranalytics.android.model.filter.Query import net.pokeranalytics.android.model.filter.Query
import net.pokeranalytics.android.model.filter.filter import net.pokeranalytics.android.model.filter.filter
import net.pokeranalytics.android.model.realm.* import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.model.utils.SessionInterval
import net.pokeranalytics.android.util.extensions.startOfDay import net.pokeranalytics.android.util.extensions.startOfDay
import java.util.* import java.util.*
import kotlin.math.max import kotlin.math.max
@ -44,8 +44,18 @@ class Calculator {
filter: Filter? = null, filter: Filter? = null,
aggregationType: AggregationType? = null, aggregationType: AggregationType? = null,
userGenerated: Boolean = false, userGenerated: Boolean = false,
reportSetupId: String? = null) : reportSetupId: String? = null
this(progressValues, stats, criterias, filter?.query ?: Query(), filter?.id, aggregationType, userGenerated, reportSetupId) ) :
this(
progressValues,
stats,
criterias,
filter?.query ?: Query(),
filter?.id,
aggregationType,
userGenerated,
reportSetupId
)
/** /**
* Specifies whether progress values should be added and their kind * Specifies whether progress values should be added and their kind
@ -64,7 +74,6 @@ class Calculator {
} }
} }
/** /**
* The type of evolution numericValues * The type of evolution numericValues
*/ */
@ -110,6 +119,7 @@ class Calculator {
get() { get() {
return this.stats.contains(LOCATIONS_PLAYED) return this.stats.contains(LOCATIONS_PLAYED)
} }
/** /**
* Whether the number of days played should be computed * Whether the number of days played should be computed
*/ */
@ -117,6 +127,7 @@ class Calculator {
get() { get() {
return this.stats.contains(DAYS_PLAYED) return this.stats.contains(DAYS_PLAYED)
} }
/** /**
* Whether progress values should be managed at the group level * Whether progress values should be managed at the group level
*/ */
@ -166,7 +177,11 @@ class Calculator {
} }
return when (aggregationType) { return when (aggregationType) {
AggregationType.SESSION, AggregationType.DURATION -> this.computeGroups(realm, listOf(group), options) AggregationType.SESSION, AggregationType.DURATION -> this.computeGroups(
realm,
listOf(group),
options
)
AggregationType.MONTH, AggregationType.YEAR -> { AggregationType.MONTH, AggregationType.YEAR -> {
this.computeStats(realm, options) this.computeStats(realm, options)
} }
@ -177,7 +192,7 @@ class Calculator {
val computableGroups: MutableList<ComputableGroup> = mutableListOf() val computableGroups: MutableList<ComputableGroup> = mutableListOf()
val combinations = options.criterias.combined() val combinations = options.criterias.combined(realm)
// Timber.d("Combinations: ${ combinations.map { it.defaultName }}") // Timber.d("Combinations: ${ combinations.map { it.defaultName }}")
for (comparatorQuery in combinations) { for (comparatorQuery in combinations) {
@ -197,10 +212,14 @@ class Calculator {
/** /**
* Computes all statIds for list of Session sessionGroup * Computes all statIds for list of Session sessionGroup
*/ */
fun computeGroups(realm: Realm, groups: List<ComputableGroup>, options: Options = Options()): Report { fun computeGroups(
realm: Realm,
groups: List<ComputableGroup>,
options: Options = Options()
): Report {
val report = Report(options) val report = Report(options)
groups.forEach { group -> for (group in groups) {
// Clean existing computables / sessionSets if group is reused // Clean existing computables / sessionSets if group is reused
group.cleanup() group.cleanup()
@ -228,16 +247,20 @@ class Calculator {
// Timber.d(">>> group ${group.name} in $duration seconds") // Timber.d(">>> group ${group.name} in $duration seconds")
} }
return report return report
} }
/** /**
* Computes statIds for a SessionSet * Computes statIds for a SessionSet
*/ */
fun compute(realm: Realm, computableGroup: ComputableGroup, options: Options = Options()): ComputedResults { fun compute(
realm: Realm,
computableGroup: ComputableGroup,
options: Options = Options()
): ComputedResults {
val results = ComputedResults(computableGroup, options.shouldManageMultiGroupProgressValues) val results =
ComputedResults(computableGroup, options.shouldManageMultiGroupProgressValues)
val computables = computableGroup.computables(realm, options.shouldSortValues) val computables = computableGroup.computables(realm, options.shouldSortValues)
@ -251,30 +274,42 @@ class Calculator {
// Timber.d("$$$ buyin = ${it.ratedBuyin} $$$ net result = ${it.ratedNet}") // Timber.d("$$$ buyin = ${it.ratedBuyin} $$$ net result = ${it.ratedNet}")
// } // }
val ftis = realm.where(FlatTimeInterval::class.java).findAll()
results.addStat(FTI_COUNT, ftis.size.toDouble())
var ratedNet = computables.sum(ComputableResult.Field.RATED_NET.identifier).toDouble() var ratedNet = computables.sum(ComputableResult.Field.RATED_NET.identifier).toDouble()
if (options.includedTransactions.isNotEmpty()) { if (options.includedTransactions.isNotEmpty()) {
for (transactionType in options.includedTransactions) { for (transactionType in options.includedTransactions) {
val transactions = computableGroup.transactions(realm, transactionType, options.shouldSortValues) val transactions = computableGroup.transactions(
val transactionRatedAmount = transactions.sum(Transaction.Field.RATED_AMOUNT.identifier).toDouble() realm,
transactionType,
options.shouldSortValues
)
val transactionRatedAmount =
transactions.sum(Transaction.Field.RATED_AMOUNT.identifier).toDouble()
ratedNet += transactionRatedAmount ratedNet += transactionRatedAmount
} }
} }
results.addStat(NET_RESULT, ratedNet) results.addStat(NET_RESULT, ratedNet)
val totalHands = computables.sum(ComputableResult.Field.ESTIMATED_HANDS.identifier).toDouble() val totalHands =
computables.sum(ComputableResult.Field.ESTIMATED_HANDS.identifier).toDouble()
results.addStat(HANDS_PLAYED, totalHands) results.addStat(HANDS_PLAYED, totalHands)
val bbSum = computables.sum(ComputableResult.Field.BB_NET.identifier).toDouble() val bbSum = computables.sum(ComputableResult.Field.BB_NET.identifier).toDouble()
results.addStat(BB_NET_RESULT, bbSum) results.addStat(BB_NET_RESULT, bbSum)
val bbSessionCount = computables.sum(ComputableResult.Field.HAS_BIG_BLIND.identifier).toInt() val bbSessionCount =
computables.sum(ComputableResult.Field.HAS_BIG_BLIND.identifier).toInt()
results.addStat(BB_SESSION_COUNT, bbSessionCount.toDouble()) results.addStat(BB_SESSION_COUNT, bbSessionCount.toDouble())
val winningSessionCount = computables.sum(ComputableResult.Field.IS_POSITIVE.identifier).toInt() val winningSessionCount =
computables.sum(ComputableResult.Field.IS_POSITIVE.identifier).toInt()
results.addStat(WINNING_SESSION_COUNT, winningSessionCount.toDouble()) results.addStat(WINNING_SESSION_COUNT, winningSessionCount.toDouble())
val totalBuyin = computables.sum(ComputableResult.Field.RATED_BUYIN.identifier).toDouble() val totalBuyin =
computables.sum(ComputableResult.Field.RATED_BUYIN.identifier).toDouble()
results.addStat(TOTAL_BUYIN, totalBuyin) results.addStat(TOTAL_BUYIN, totalBuyin)
val totalTips = computables.sum(ComputableResult.Field.RATED_TIPS.identifier).toDouble() val totalTips = computables.sum(ComputableResult.Field.RATED_TIPS.identifier).toDouble()
@ -282,12 +317,14 @@ class Calculator {
// Timber.d("########## totalBuyin = ${totalBuyin} ### sum = ${sum}") // Timber.d("########## totalBuyin = ${totalBuyin} ### sum = ${sum}")
val maxNetResult = computables.max(ComputableResult.Field.RATED_NET.identifier)?.toDouble() val maxNetResult =
computables.max(ComputableResult.Field.RATED_NET.identifier)?.toDouble()
maxNetResult?.let { maxNetResult?.let {
results.addStat(MAXIMUM_NET_RESULT, it) results.addStat(MAXIMUM_NET_RESULT, it)
} }
val minNetResult = computables.min(ComputableResult.Field.RATED_NET.identifier)?.toDouble() val minNetResult =
computables.min(ComputableResult.Field.RATED_NET.identifier)?.toDouble()
minNetResult?.let { minNetResult?.let {
results.addStat(MINIMUM_NET_RESULT, it) results.addStat(MINIMUM_NET_RESULT, it)
} }
@ -307,7 +344,10 @@ class Calculator {
// } // }
if (options.computeLocationsPlayed) { if (options.computeLocationsPlayed) {
results.addStat(LOCATIONS_PLAYED, computables.distinctBy { it.session?.location?.id }.size.toDouble()) results.addStat(
LOCATIONS_PLAYED,
computables.distinctBy { it.session?.location?.id }.size.toDouble()
)
} }
var average = 0.0 // also used for standard deviation later var average = 0.0 // also used for standard deviation later
@ -350,7 +390,7 @@ class Calculator {
var currentStreak = 0 var currentStreak = 0
var tITMCount = 0 var tITMCount = 0
computables.forEach { computable -> for (computable in computables) {
index++ index++
tSum += computable.ratedNet tSum += computable.ratedNet
tBBSum += computable.bbNet tBBSum += computable.bbNet
@ -377,11 +417,20 @@ class Calculator {
} }
val session = val session =
computable.session ?: throw PAIllegalStateException("Computing lone ComputableResult") computable.session
?: throw PAIllegalStateException("Computing lone ComputableResult")
results.addEvolutionValue(tSum, stat = NET_RESULT, data = session) results.addEvolutionValue(tSum, stat = NET_RESULT, data = session)
results.addEvolutionValue(tSum / index, stat = AVERAGE, data = session) results.addEvolutionValue(tSum / index, stat = AVERAGE, data = session)
results.addEvolutionValue(index.toDouble(), stat = NUMBER_OF_GAMES, data = session) results.addEvolutionValue(
results.addEvolutionValue(tBBSum / tBBSessionCount, stat = AVERAGE_NET_BB, data = session) index.toDouble(),
stat = NUMBER_OF_GAMES,
data = session
)
results.addEvolutionValue(
tBBSum / tBBSessionCount,
stat = AVERAGE_NET_BB,
data = session
)
results.addEvolutionValue( results.addEvolutionValue(
(tWinningSessionCount.toDouble() / index.toDouble()), (tWinningSessionCount.toDouble() / index.toDouble()),
stat = WIN_RATIO, stat = WIN_RATIO,
@ -390,12 +439,25 @@ class Calculator {
results.addEvolutionValue( results.addEvolutionValue(
tITMCount.toDouble() / index.toDouble(), tITMCount.toDouble() / index.toDouble(),
stat = TOURNAMENT_ITM_RATIO, stat = TOURNAMENT_ITM_RATIO,
data = session) data = session
results.addEvolutionValue(tBuyinSum / index, stat = AVERAGE_BUYIN, data = session) )
results.addEvolutionValue(computable.ratedNet, stat = STANDARD_DEVIATION, data = session) results.addEvolutionValue(
tBuyinSum / index,
stat = AVERAGE_BUYIN,
data = session
)
results.addEvolutionValue(
computable.ratedNet,
stat = STANDARD_DEVIATION,
data = session
)
Stat.netBBPer100Hands(tBBSum, tHands)?.let { netBB100 -> Stat.netBBPer100Hands(tBBSum, tHands)?.let { netBB100 ->
results.addEvolutionValue(netBB100, stat = NET_BB_PER_100_HANDS, data = session) results.addEvolutionValue(
netBB100,
stat = NET_BB_PER_100_HANDS,
data = session
)
} }
Stat.returnOnInvestment(tSum, tBuyinSum)?.let { roi -> Stat.returnOnInvestment(tSum, tBuyinSum)?.let { roi ->
@ -411,30 +473,34 @@ class Calculator {
} }
// loseStreak is negative and we want it positive // loseStreak is negative and we want it positive
results.addStat(LONGEST_STREAKS, longestWinStreak.toDouble(), -longestLoseStreak.toDouble()) results.addStat(
LONGEST_STREAKS,
longestWinStreak.toDouble(),
-longestLoseStreak.toDouble()
)
} }
val sessionSets = computableGroup.sessionSets(realm, options.shouldSortValues) val sessionSets = computableGroup.sessionSets(realm, options.shouldSortValues)
results.addStat(NUMBER_OF_SETS, sessionSets.size.toDouble()) results.addStat(NUMBER_OF_SETS, sessionSets.size.toDouble())
var gHourlyDuration: Double? = null // var gHourlyDuration: Double? = null
var gBBSum: Double? = null //// var gBBSum: Double? = null
var maxDuration: Double? = null var maxDuration: Double? = null
if (computableGroup.conditions.isEmpty()) { // SessionSets are fine if (computableGroup.conditions.isEmpty()) { // SessionSets are fine
gHourlyDuration = // gHourlyDuration =
sessionSets.sum(SessionSet.Field.NET_DURATION.identifier).toDouble() / 3600000 // (milliseconds to hours) // sessionSets.sum(SessionSet.Field.NET_DURATION.identifier).toDouble() / 3600000 // (milliseconds to hours)
gBBSum = sessionSets.sum(SessionSet.Field.BB_NET.identifier).toDouble() // gBBSum = sessionSets.sum(SessionSet.Field.BB_NET.identifier).toDouble()
sessionSets.max(SessionSet.Field.NET_DURATION.identifier)?.let { sessionSets.max(SessionSet.Field.NET_DURATION.identifier)?.let {
maxDuration = it.toDouble() / 3600000 maxDuration = it.toDouble() / 3600000
} }
} }
val shouldIterateOverSets = computableGroup.conditions.isNotEmpty() val shouldIterateOverSets = options.progressValues != Options.ProgressValues.NONE
|| options.progressValues != Options.ProgressValues.NONE
|| options.computeDaysPlayed || options.computeDaysPlayed
// || computableGroup.conditions.isNotEmpty()
// Session Set // Session Set
if (shouldIterateOverSets) { if (shouldIterateOverSets) {
@ -449,7 +515,7 @@ class Calculator {
val daysSet = mutableSetOf<Date>() val daysSet = mutableSetOf<Date>()
var tMaxDuration = 0.0 var tMaxDuration = 0.0
sessionSets.forEach { sessionSet -> for (sessionSet in sessionSets) {
tIndex++ tIndex++
val setStats = SSStats(sessionSet, computableGroup.query) val setStats = SSStats(sessionSet, computableGroup.query)
@ -458,7 +524,7 @@ class Calculator {
tBBSum += setStats.bbSum tBBSum += setStats.bbSum
tHourlyDuration += setStats.hourlyDuration tHourlyDuration += setStats.hourlyDuration
tTotalHands += setStats.estimatedHands tTotalHands += setStats.estimatedHands
tMaxDuration = max(tMaxDuration, setStats.hourlyDuration) tMaxDuration = max(tMaxDuration, setStats.hourlyDuration.toDouble())
tHourlyRate = tRatedNetSum / tHourlyDuration tHourlyRate = tRatedNetSum / tHourlyDuration
tHourlyRateBB = tBBSum / tHourlyDuration tHourlyRateBB = tBBSum / tHourlyDuration
@ -466,8 +532,16 @@ class Calculator {
when (options.progressValues) { when (options.progressValues) {
Options.ProgressValues.STANDARD -> { Options.ProgressValues.STANDARD -> {
results.addEvolutionValue(tHourlyRate, stat = HOURLY_RATE, data = sessionSet) results.addEvolutionValue(
results.addEvolutionValue(tIndex.toDouble(), stat = NUMBER_OF_SETS, data = sessionSet) tHourlyRate,
stat = HOURLY_RATE,
data = sessionSet
)
results.addEvolutionValue(
tIndex.toDouble(),
stat = NUMBER_OF_SETS,
data = sessionSet
)
results.addEvolutionValue( results.addEvolutionValue(
sessionSet.hourlyDuration, sessionSet.hourlyDuration,
tHourlyDuration, tHourlyDuration,
@ -479,12 +553,26 @@ class Calculator {
stat = AVERAGE_HOURLY_DURATION, stat = AVERAGE_HOURLY_DURATION,
data = sessionSet data = sessionSet
) )
results.addEvolutionValue(tHourlyRateBB, stat = HOURLY_RATE_BB, data = sessionSet) results.addEvolutionValue(
tHourlyRateBB,
stat = HOURLY_RATE_BB,
data = sessionSet
)
} }
Options.ProgressValues.TIMED -> { Options.ProgressValues.TIMED -> {
results.addEvolutionValue(tRatedNetSum, tHourlyDuration, NET_RESULT, sessionSet) results.addEvolutionValue(
results.addEvolutionValue(tHourlyRate, tHourlyDuration, HOURLY_RATE, sessionSet) tRatedNetSum,
tHourlyDuration,
NET_RESULT,
sessionSet
)
results.addEvolutionValue(
tHourlyRate,
tHourlyDuration,
HOURLY_RATE,
sessionSet
)
results.addEvolutionValue( results.addEvolutionValue(
tIndex.toDouble(), tIndex.toDouble(),
tHourlyDuration, tHourlyDuration,
@ -503,7 +591,12 @@ class Calculator {
AVERAGE_HOURLY_DURATION, AVERAGE_HOURLY_DURATION,
sessionSet sessionSet
) )
results.addEvolutionValue(tHourlyRateBB, tHourlyDuration, HOURLY_RATE_BB, sessionSet) results.addEvolutionValue(
tHourlyRateBB,
tHourlyDuration,
HOURLY_RATE_BB,
sessionSet
)
Stat.netBBPer100Hands(tBBSum, tTotalHands)?.let { netBB100 -> Stat.netBBPer100Hands(tBBSum, tTotalHands)?.let { netBB100 ->
results.addEvolutionValue( results.addEvolutionValue(
@ -523,30 +616,44 @@ class Calculator {
} }
gHourlyDuration = tHourlyDuration // gHourlyDuration = tHourlyDuration
gBBSum = tBBSum // gBBSum = tBBSum
maxDuration = tMaxDuration maxDuration = tMaxDuration
} }
var hourlyRate = 0.0 // var hourlyRate = 0.0
if (gHourlyDuration != null) { // if (gHourlyDuration != null) {
//
// hourlyRate = ratedNet / gHourlyDuration
// if (sessionSets.size > 0) {
// val avgDuration = gHourlyDuration / sessionSets.size
// results.addStat(HOURLY_RATE, hourlyRate)
// results.addStat(AVERAGE_HOURLY_DURATION, avgDuration)
// }
// results.addStat(HOURLY_DURATION, gHourlyDuration)
// }
val timeIntervals = computableGroup.timeIntervals(realm)
val duration = timeIntervals.sum("duration").toDouble()
val breakDuration = sessionSets.sum("breakDuration").toDouble()
val netHourlyDuration = (duration - breakDuration) / 3600000
val hourlyRate = ratedNet / netHourlyDuration
hourlyRate = ratedNet / gHourlyDuration
if (sessionSets.size > 0) {
val avgDuration = gHourlyDuration / sessionSets.size
results.addStat(HOURLY_RATE, hourlyRate) results.addStat(HOURLY_RATE, hourlyRate)
results.addStat(AVERAGE_HOURLY_DURATION, avgDuration) results.addStat(AVERAGE_HOURLY_DURATION, netHourlyDuration / sessionSets.size)
} results.addStat(HOURLY_DURATION, netHourlyDuration)
results.addStat(HOURLY_DURATION, gHourlyDuration)
}
if (gBBSum != null) { results.addStat(HOURLY_RATE_BB, bbSum / netHourlyDuration)
if (gHourlyDuration != null) { results.addStat(AVERAGE_NET_BB, bbSum / bbSessionCount)
results.addStat(HOURLY_RATE_BB, gBBSum / gHourlyDuration)
} // if (gBBSum != null) {
results.addStat(AVERAGE_NET_BB, gBBSum / bbSessionCount) // if (gHourlyDuration != null) {
} // results.addStat(HOURLY_RATE_BB, gBBSum / gHourlyDuration)
// }
// results.addStat(AVERAGE_NET_BB, gBBSum / bbSessionCount)
// }
maxDuration?.let { maxd -> maxDuration?.let { maxd ->
results.addStat(MAXIMUM_DURATION, maxd) // (milliseconds to hours) results.addStat(MAXIMUM_DURATION, maxd) // (milliseconds to hours)
@ -560,35 +667,31 @@ class Calculator {
// Session // Session
var stdSum = 0.0 var stdSum = 0.0
var stdBBSum = 0.0 var stdBBSum = 0.0
var stdBBper100HandsSum = 0.0 var stdBBPer100HandsSum = 0.0
computables.forEach { session -> for (computable in computables) {
stdSum += (session.ratedNet - average).pow(2.0) stdSum += (computable.ratedNet - average).pow(2.0)
stdBBSum += (session.bbNet - averageBB).pow(2.0) stdBBSum += (computable.bbNet - averageBB).pow(2.0)
stdBBper100HandsSum += (session.bbPer100Hands - bbPer100Hands).pow(2.0) stdBBPer100HandsSum += (computable.bbPer100Hands - bbPer100Hands).pow(2.0)
} }
val standardDeviation = sqrt(stdSum / computables.size) val standardDeviation = sqrt(stdSum / computables.size)
val standardDeviationBB = sqrt(stdBBSum / computables.size) val standardDeviationBB = sqrt(stdBBSum / computables.size)
val standardDeviationBBper100Hands = sqrt(stdBBper100HandsSum / computables.size) val standardDeviationBBper100Hands = sqrt(stdBBPer100HandsSum / computables.size)
results.addStat(STANDARD_DEVIATION, standardDeviation) results.addStat(STANDARD_DEVIATION, standardDeviation)
results.addStat(STANDARD_DEVIATION_BB, standardDeviationBB) results.addStat(STANDARD_DEVIATION_BB, standardDeviationBB)
results.addStat(STANDARD_DEVIATION_BB_PER_100_HANDS, standardDeviationBBper100Hands) results.addStat(STANDARD_DEVIATION_BB_PER_100_HANDS, standardDeviationBBper100Hands)
// Session Set // Session Set
if (gHourlyDuration != null) {
var hourlyStdSum = 0.0 var hourlyStdSum = 0.0
sessionSets.forEach { set -> for (set in sessionSets) {
val ssStats = SSStats(set, computableGroup.query) val ssStats = SSStats(set, computableGroup.query)
val sHourlyRate = ssStats.hourlyRate val setHourlyRate = ssStats.hourlyRate
hourlyStdSum += (sHourlyRate - hourlyRate).pow(2.0) hourlyStdSum += (setHourlyRate - hourlyRate).pow(2.0)
} }
val hourlyStandardDeviation = sqrt(hourlyStdSum / sessionSets.size) val hourlyStandardDeviation = sqrt(hourlyStdSum / sessionSets.size)
results.addStat(STANDARD_DEVIATION_HOURLY, hourlyStandardDeviation) results.addStat(STANDARD_DEVIATION_HOURLY, hourlyStandardDeviation)
} }
}
return results return results
} }
@ -598,14 +701,18 @@ class Calculator {
class SSStats(sessionSet: SessionSet, query: Query) { // Session Set Stats class SSStats(sessionSet: SessionSet, query: Query) { // Session Set Stats
var hourlyDuration: Double = 0.0 var hourlyDuration: Long = 0L
var estimatedHands: Double = 0.0 var estimatedHands: Double = 0.0
var bbSum: Double = 0.0 var bbSum: Double = 0.0
var ratedNet: Double = 0.0 var ratedNet: Double = 0.0
val hourlyRate: Double val hourlyRate: Double
get() { get() {
return this.ratedNet / this.hourlyDuration return if (this.hourlyDuration > 0L) {
this.ratedNet / this.hourlyDuration.toDouble()
} else {
0.0
}
} }
init { init {
@ -618,10 +725,15 @@ class SSStats(sessionSet: SessionSet, query: Query) { // Session Set Stats
if (setSessions.size == filteredSessions.size) { if (setSessions.size == filteredSessions.size) {
this.initStatsWithSet(sessionSet) this.initStatsWithSet(sessionSet)
} else { } else {
ratedNet = filteredSessions.sumOf { it.computableResult?.ratedNet ?: 0.0 } ratedNet = filteredSessions.sumOf { it.computableResult?.ratedNet ?: 0.0 }
bbSum = filteredSessions.sumOf { it.bbNet } bbSum = filteredSessions.sumOf { it.bbNet }
hourlyDuration = filteredSessions.hourlyDuration
estimatedHands = filteredSessions.sumOf { it.estimatedHands } estimatedHands = filteredSessions.sumOf { it.estimatedHands }
val intervals = SessionInterval.intervalMap(filteredSessions.toSet())
val netDuration = intervals.sumOf { it.duration }
val estimatedBreak = if (netDuration > 0.0) sessionSet.breakDuration * sessionSet.netDuration / netDuration else 0L
hourlyDuration = netDuration - estimatedBreak
} }
} }
} }
@ -629,7 +741,7 @@ class SSStats(sessionSet: SessionSet, query: Query) { // Session Set Stats
private fun initStatsWithSet(sessionSet: SessionSet) { private fun initStatsWithSet(sessionSet: SessionSet) {
ratedNet = sessionSet.ratedNet ratedNet = sessionSet.ratedNet
bbSum = sessionSet.bbNet bbSum = sessionSet.bbNet
hourlyDuration = sessionSet.hourlyDuration hourlyDuration = sessionSet.hourlyDuration.toLong()
estimatedHands = sessionSet.estimatedHands estimatedHands = sessionSet.estimatedHands
} }

@ -53,6 +53,8 @@ class ComputableGroup(val query: Query, var displayedStats: List<Stat>? = null)
} }
} }
// Timber.d("QUERY = ${query.defaultName}")
val sortedField = if (sorted) "session.startDate" else null val sortedField = if (sorted) "session.startDate" else null
val computables = Filter.queryOn<ComputableResult>(realm, this.query, sortedField) val computables = Filter.queryOn<ComputableResult>(realm, this.query, sortedField)
@ -62,6 +64,11 @@ class ComputableGroup(val query: Query, var displayedStats: List<Stat>? = null)
return computables return computables
} }
fun timeIntervals(realm: Realm): RealmResults<FlatTimeInterval> {
return Filter.queryOn(realm, this.query)
}
/** /**
* The list of sets to compute * The list of sets to compute
*/ */

@ -1,21 +1,26 @@
package net.pokeranalytics.android.calculus package net.pokeranalytics.android.calculus
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.CountDownTimer import android.os.CountDownTimer
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import io.realm.Realm import io.realm.Realm
import io.realm.RealmQuery import io.realm.RealmQuery
import io.realm.RealmResults import io.realm.RealmResults
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.AppState
import net.pokeranalytics.android.calculus.optimalduration.CashGameOptimalDurationCalculator import net.pokeranalytics.android.calculus.optimalduration.CashGameOptimalDurationCalculator
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.LiveOnline import net.pokeranalytics.android.model.LiveOnline
import net.pokeranalytics.android.model.realm.* import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.ui.fragment.ImportBroadcast
import net.pokeranalytics.android.ui.view.rows.StaticReport import net.pokeranalytics.android.ui.view.rows.StaticReport
import net.pokeranalytics.android.util.extensions.formattedHourlyDuration import net.pokeranalytics.android.util.extensions.formattedHourlyDuration
import timber.log.Timber import timber.log.Timber
import kotlin.coroutines.CoroutineContext
interface NewPerformanceListener { interface NewPerformanceListener {
fun newBestPerformanceHandler() fun newBestPerformanceHandler()
@ -25,6 +30,7 @@ class ReportWhistleBlower(var context: Context) {
private var sessions: RealmResults<Session>? = null private var sessions: RealmResults<Session>? = null
private var results: RealmResults<Result>? = null private var results: RealmResults<Result>? = null
private var sessionSets: RealmResults<SessionSet>? = null
private var currentTask: ReportTask? = null private var currentTask: ReportTask? = null
@ -32,25 +38,42 @@ class ReportWhistleBlower(var context: Context) {
private val listeners: MutableList<NewPerformanceListener> = mutableListOf() private val listeners: MutableList<NewPerformanceListener> = mutableListOf()
private var paused: Boolean = false
private var timer: CountDownTimer? = null private var timer: CountDownTimer? = null
private val startImportReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
pause()
}
}
private val endImportReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
resume()
}
}
init { init {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
this.sessions = realm.where(Session::class.java).findAll() sessions = realm.where(Session::class.java).findAll()
this.sessions?.addChangeListener { _ -> sessions?.addChangeListener { _ ->
requestReportLaunch() requestReportLaunch()
} }
this.results = realm.where(Result::class.java).findAll() results = realm.where(Result::class.java).findAll()
this.results?.addChangeListener { _ -> results?.addChangeListener { _ ->
requestReportLaunch()
}
sessionSets = realm.where(SessionSet::class.java).findAll()
sessionSets?.addChangeListener { _ ->
requestReportLaunch() requestReportLaunch()
} }
realm.close() realm.close()
LocalBroadcastManager.getInstance(context).registerReceiver(startImportReceiver, IntentFilter(ImportBroadcast.START.identifier))
LocalBroadcastManager.getInstance(context).registerReceiver(endImportReceiver, IntentFilter(ImportBroadcast.END.identifier))
} }
fun addListener(newPerformanceListener: NewPerformanceListener) { fun addListener(newPerformanceListener: NewPerformanceListener) {
@ -62,9 +85,8 @@ class ReportWhistleBlower(var context: Context) {
} }
fun requestReportLaunch() { fun requestReportLaunch() {
Timber.d(">>> Launch report")
if (paused) { if (AppState.isImporting) {
return return
} }
@ -98,14 +120,12 @@ class ReportWhistleBlower(var context: Context) {
/** /**
* Pauses the whistleblower, for example when importing data * Pauses the whistleblower, for example when importing data
*/ */
fun pause() { private fun pause() {
this.paused = true
this.currentTask?.cancel() this.currentTask?.cancel()
this.currentTask = null this.currentTask = null
} }
fun resume() { private fun resume() {
this.paused = false
this.requestReportLaunch() this.requestReportLaunch()
} }
@ -131,19 +151,20 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
private var cancelled = false private var cancelled = false
private val coroutineContext: CoroutineContext
get() = Dispatchers.Default
fun start() { fun start() {
launchReports() launchReports()
} }
fun cancel() { fun cancel() {
// Timber.d("Reportwhistleblower task CANCEL")
this.cancelled = true this.cancelled = true
} }
private fun launchReports() { private fun launchReports() {
CoroutineScope(coroutineContext).launch {
// Timber.d("====== Report whistleblower launch batch...")
CoroutineScope(Dispatchers.Default).launch {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
@ -157,7 +178,8 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
// CustomField // CustomField
val customFields = realm.where(CustomField::class.java) val customFields = realm.where(CustomField::class.java)
.equalTo("type", CustomField.Type.LIST.uniqueIdentifier).findAll() .equalTo("type", CustomField.Type.LIST.uniqueIdentifier)
.findAll()
for (customField in customFields) { for (customField in customFields) {
if (cancelled) { if (cancelled) {
break break
@ -171,7 +193,7 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
private fun launchReport(realm: Realm, report: StaticReport) { private fun launchReport(realm: Realm, report: StaticReport) {
Timber.d(">>> launch report = $report") // Timber.d(">>> launch report = $report")
when (report) { when (report) {
StaticReport.OptimalDuration -> launchOptimalDuration(realm, report) StaticReport.OptimalDuration -> launchOptimalDuration(realm, report)
@ -200,9 +222,9 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
val nameSeparator = " " val nameSeparator = " "
for (stat in result.options.stats) { for (stat in staticReport.performanceStats) {
Timber.d("analyse stat: $stat for report: $staticReport") // Timber.d("analyse stat: $stat for report: $staticReport")
// Get current performance // Get current performance
var query = performancesQuery(realm, staticReport, stat) var query = performancesQuery(realm, staticReport, stat)
@ -221,7 +243,8 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
val performanceQuery = computedResults.group.query val performanceQuery = computedResults.group.query
val performanceName = performanceQuery.getName(this.context, nameSeparator) val performanceName = performanceQuery.getName(this.context, nameSeparator)
Timber.d("Best computed = $performanceName, ${computedResults.computedStat(Stat.NET_RESULT)?.value}") val count = computedResults.computedStat(Stat.NUMBER_OF_GAMES)?.value?.toInt() ?: throw PAIllegalStateException("Number of games not found")
val notify = !(stat.hasEarlyVariance && count < 10)
var storePerf = true var storePerf = true
currentPerf?.let { currentPerf?.let {
@ -242,8 +265,11 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
currentPerf.objectId = performanceQuery.objectId currentPerf.objectId = performanceQuery.objectId
currentPerf.customFieldId = customField?.id currentPerf.customFieldId = customField?.id
} }
if (notify) {
this.whistleBlower.notify(currentPerf) this.whistleBlower.notify(currentPerf)
} }
}
} }
@ -257,14 +283,16 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co
null null
) )
realm.executeTransaction { it.copyToRealm(performance) } realm.executeTransaction { it.copyToRealm(performance) }
if (notify) {
this.whistleBlower.notify(performance) this.whistleBlower.notify(performance)
} }
}
} ?: run { // if there is no max but a now irrelevant Performance, we delete it } ?: run { // if there is no max but a now irrelevant Performance, we delete it
Timber.d("NO best computed value, current perf = $currentPerf ") // Timber.d("NO best computed value, current perf = $currentPerf ")
currentPerf?.let { perf -> currentPerf?.let { perf ->
realm.executeTransaction { realm.executeTransaction {
Timber.d("Delete perf: stat = ${perf.stat}, report = ${perf.reportId}") // Timber.d("Delete perf: stat = ${perf.stat}, report = ${perf.reportId}")
perf.deleteFromRealm() perf.deleteFromRealm()
} }
} }

@ -55,7 +55,8 @@ enum class Stat(override var uniqueIdentifier: Int) : IntIdentifiable, RowRepres
RISK_OF_RUIN(28), RISK_OF_RUIN(28),
STANDARD_DEVIATION_BB(29), STANDARD_DEVIATION_BB(29),
TOURNAMENT_ITM_RATIO(30), TOURNAMENT_ITM_RATIO(30),
TOTAL_TIPS(31) TOTAL_TIPS(31),
FTI_COUNT(32)
; ;
companion object : IntSearchable<Stat> { companion object : IntSearchable<Stat> {
@ -136,6 +137,7 @@ enum class Stat(override var uniqueIdentifier: Int) : IntIdentifiable, RowRepres
TOTAL_BUYIN -> R.string.total_buyin TOTAL_BUYIN -> R.string.total_buyin
TOURNAMENT_ITM_RATIO -> R.string.itm_ratio TOURNAMENT_ITM_RATIO -> R.string.itm_ratio
TOTAL_TIPS -> R.string.total_tips TOTAL_TIPS -> R.string.total_tips
FTI_COUNT -> R.string.players_count
else -> throw PAIllegalStateException("Stat ${this.name} name required but undefined") else -> throw PAIllegalStateException("Stat ${this.name} name required but undefined")
} }
} }
@ -162,7 +164,7 @@ enum class Stat(override var uniqueIdentifier: Int) : IntIdentifiable, RowRepres
return TextFormat(value.formatted, color) return TextFormat(value.formatted, color)
} }
// white integers // white integers
NUMBER_OF_SETS, NUMBER_OF_GAMES, HANDS_PLAYED, LOCATIONS_PLAYED, DAYS_PLAYED -> { NUMBER_OF_SETS, NUMBER_OF_GAMES, HANDS_PLAYED, LOCATIONS_PLAYED, DAYS_PLAYED, FTI_COUNT -> {
return TextFormat("${value.toInt()}") return TextFormat("${value.toInt()}")
} // white durations } // white durations
HOURLY_DURATION, AVERAGE_HOURLY_DURATION, MAXIMUM_DURATION -> { HOURLY_DURATION, AVERAGE_HOURLY_DURATION, MAXIMUM_DURATION -> {
@ -319,6 +321,14 @@ enum class Stat(override var uniqueIdentifier: Int) : IntIdentifiable, RowRepres
} }
} }
val hasEarlyVariance: Boolean
get() {
return when (this) {
HOURLY_RATE, AVERAGE, AVERAGE_NET_BB, AVERAGE_BUYIN, AVERAGE_HOURLY_DURATION -> true
else -> false
}
}
private val hasProgressValues: Boolean private val hasProgressValues: Boolean
get() { get() {
return when (this) { return when (this) {

@ -31,7 +31,7 @@ class BankrollCalculator {
var initialValue = 0.0 var initialValue = 0.0
var transactionNet = 0.0 var transactionNet = 0.0
bankrolls.forEach { bankroll -> for (bankroll in bankrolls) {
val rate = if (setup.virtualBankroll) bankroll.rate else 1.0 val rate = if (setup.virtualBankroll) bankroll.rate else 1.0
@ -70,8 +70,8 @@ class BankrollCalculator {
val transactions = Filter.queryOn<Transaction>(realm, baseQuery) val transactions = Filter.queryOn<Transaction>(realm, baseQuery)
report.addDatedItems(transactions) report.addDatedItems(transactions)
transactions.forEach { for (transaction in transactions) {
report.addTransaction(it) report.addTransaction(transaction)
} }
val sessionQuery = Query(QueryCondition.DateNotNull).merge(baseQuery) val sessionQuery = Query(QueryCondition.DateNotNull).merge(baseQuery)
@ -83,8 +83,8 @@ class BankrollCalculator {
val options = Calculator.Options(stats = listOf(Stat.NET_RESULT, Stat.HOURLY_RATE, Stat.STANDARD_DEVIATION_HOURLY)) val options = Calculator.Options(stats = listOf(Stat.NET_RESULT, Stat.HOURLY_RATE, Stat.STANDARD_DEVIATION_HOURLY))
val group = ComputableGroup(baseQuery) val group = ComputableGroup(baseQuery)
val result = Calculator.compute(realm, group, options) val result = Calculator.compute(realm, group, options)
result.computedStat(Stat.NET_RESULT)?.let { result.computedStat(Stat.NET_RESULT)?.let { computedStat ->
report.netResult = it.value report.netResult = computedStat.value
} }
this.computeRiskOfRuin(report, result) this.computeRiskOfRuin(report, result)

@ -2,22 +2,17 @@ package net.pokeranalytics.android.calculus.bankroll
import io.realm.Realm import io.realm.Realm
import io.realm.RealmResults import io.realm.RealmResults
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.model.realm.Bankroll import net.pokeranalytics.android.model.realm.Bankroll
import net.pokeranalytics.android.model.realm.ComputableResult import net.pokeranalytics.android.model.realm.ComputableResult
import net.pokeranalytics.android.model.realm.Transaction import net.pokeranalytics.android.model.realm.Transaction
import timber.log.Timber import timber.log.Timber
import java.util.*
import kotlin.coroutines.CoroutineContext
object BankrollReportManager { object BankrollReportManager {
private val coroutineContext: CoroutineContext
get() = Dispatchers.Main
private var reports: MutableMap<String?, BankrollReport> = mutableMapOf() private var reports: MutableMap<String?, BankrollReport> = mutableMapOf()
private var computableResults: RealmResults<ComputableResult> private var computableResults: RealmResults<ComputableResult>
@ -68,12 +63,12 @@ object BankrollReportManager {
} }
// otherwise compute it // otherwise compute it
GlobalScope.launch(coroutineContext) { CoroutineScope(Dispatchers.Main).launch {
var report: BankrollReport? = null var report: BankrollReport? = null
val coroutine = GlobalScope.async { val coroutine = CoroutineScope(Dispatchers.Default).async {
val s = Date() // val s = Date()
Timber.d(">>>>> start computing bankroll...") // Timber.d(">>>>> start computing bankroll...")
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
@ -82,9 +77,9 @@ object BankrollReportManager {
realm.close() realm.close()
val e = Date() // val e = Date()
val duration = (e.time - s.time) / 1000.0 // val duration = (e.time - s.time) / 1000.0
Timber.d(">>>>> ended in $duration seconds") // Timber.d(">>>>> ended in $duration seconds")
} }
coroutine.await() coroutine.await()

@ -64,8 +64,8 @@ class CashGameOptimalDurationCalculator {
var end: Double? = null var end: Double? = null
var validBuckets = 0 var validBuckets = 0
val hkeys = sessionsByDuration.keys.map { it / 3600 / 1000.0 }.sorted() // val hkeys = sessionsByDuration.keys.map { it / 3600 / 1000.0 }.sorted()
Timber.d("Stop notif > keys: $hkeys ") // Timber.d("Stop notif > keys: $hkeys ")
for (key in sessionsByDuration.keys.sorted()) { for (key in sessionsByDuration.keys.sorted()) {
val sessionCount = sessionsByDuration[key]?.size ?: 0 val sessionCount = sessionsByDuration[key]?.size ?: 0
if (start == null && sessionCount >= minimumValidityCount) { if (start == null && sessionCount >= minimumValidityCount) {
@ -76,15 +76,15 @@ class CashGameOptimalDurationCalculator {
validBuckets++ validBuckets++
} }
} }
Timber.d("Stop notif > validBuckets: $validBuckets ") // Timber.d("Stop notif > validBuckets: $validBuckets ")
if (!(start != null && end != null && (end - start) >= intervalValidity)) { if (!(start != null && end != null && (end - start) >= intervalValidity)) {
Timber.d("Stop notif > invalid setup: $start / $end ") // Timber.d("Stop notif > invalid setup: $start / $end ")
return null return null
} }
// define if we have enough sessions // define if we have enough sessions
if (sessions.size < 50) { if (sessions.size < 50) {
Timber.d("Stop notif > not enough sessions: ${sessions.size} ") // Timber.d("Stop notif > not enough sessions: ${sessions.size} ")
return null return null
} }
@ -134,7 +134,7 @@ class CashGameOptimalDurationCalculator {
return bestDuration return bestDuration
} }
Timber.d("Stop notif > not found, best duration: $bestDuration") // Timber.d("Stop notif > not found, best duration: $bestDuration")
realm.close() realm.close()
return null return null
} }

@ -11,6 +11,7 @@ class ConfigurationException(message: String) : Exception(message)
class EnumIdentifierNotFoundException(message: String) : Exception(message) class EnumIdentifierNotFoundException(message: String) : Exception(message)
class MisconfiguredSavableEnumException(message: String) : Exception(message) class MisconfiguredSavableEnumException(message: String) : Exception(message)
class PAIllegalStateException(message: String) : Exception(message) class PAIllegalStateException(message: String) : Exception(message)
class PADataModelException(message: String) : Exception(message)
sealed class PokerAnalyticsException(message: String) : Exception(message) { sealed class PokerAnalyticsException(message: String) : Exception(message) {
object FilterElementUnknownName : PokerAnalyticsException(message = "No filterElement name was found to identify the queryCondition") object FilterElementUnknownName : PokerAnalyticsException(message = "No filterElement name was found to identify the queryCondition")

@ -25,11 +25,12 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.util.enumerations.IntIdentifiable import net.pokeranalytics.android.util.enumerations.IntIdentifiable
import net.pokeranalytics.android.util.enumerations.IntSearchable import net.pokeranalytics.android.util.enumerations.IntSearchable
import net.pokeranalytics.android.util.extensions.findById import net.pokeranalytics.android.util.extensions.findById
import timber.log.Timber
fun List<Criteria>.combined(): List<Query> { fun List<Criteria>.combined(realm: Realm): List<Query> {
val comparatorList = ArrayList<List<Query>>() val comparatorList = ArrayList<List<Query>>()
this.forEach { criteria -> this.forEach { criteria ->
comparatorList.add(criteria.queries) comparatorList.add(criteria.queries(realm))
} }
return getCombinations(comparatorList) return getCombinations(comparatorList)
} }
@ -68,7 +69,6 @@ sealed class Criteria(override var uniqueIdentifier: Int) : IntIdentifiable, Row
realm.findById(CustomField::class.java, this.customFieldId)?.entries?.forEach { realm.findById(CustomField::class.java, this.customFieldId)?.entries?.forEach {
objects.add(QueryCondition.CustomFieldListQuery(it)) objects.add(QueryCondition.CustomFieldListQuery(it))
} }
objects.sort()
realm.close() realm.close()
return objects.map { Query(it) } return objects.map { Query(it) }
} }
@ -166,14 +166,11 @@ sealed class Criteria(override var uniqueIdentifier: Int) : IntIdentifiable, Row
data class ValueCustomFields(override var customFieldId: String) : ListCriteria(22), CustomFieldCriteria data class ValueCustomFields(override var customFieldId: String) : ListCriteria(22), CustomFieldCriteria
object Duration : ListCriteria(23) object Duration : ListCriteria(23)
val queries: List<Query> fun queries(realm: Realm): List<Query> {
get() {
return when (this) { return when (this) {
is AllMonthsUpToNow -> { is AllMonthsUpToNow -> {
val realm = Realm.getDefaultInstance()
val firstSession = realm.where<Session>().isNotNull("startDate").sort("startDate", Sort.ASCENDING).findFirst() val firstSession = realm.where<Session>().isNotNull("startDate").sort("startDate", Sort.ASCENDING).findFirst()
val lastSession = realm.where<Session>().isNotNull("startDate").sort("startDate", Sort.DESCENDING).findFirst() val lastSession = realm.where<Session>().isNotNull("startDate").sort("startDate", Sort.DESCENDING).findFirst()
realm.close()
val years: ArrayList<Query> = arrayListOf() val years: ArrayList<Query> = arrayListOf()
@ -201,13 +198,12 @@ sealed class Criteria(override var uniqueIdentifier: Int) : IntIdentifiable, Row
years years
} }
else -> { else -> {
return this.queryConditions return this.queryConditions(realm)
} }
} }
} }
val queryConditions: List<Query> fun queryConditions(realm: Realm): List<Query> {
get() {
return when (this) { return when (this) {
is Bankrolls -> comparison<Bankroll>() is Bankrolls -> comparison<Bankroll>()
is Games -> comparison<Game>() is Games -> comparison<Game>()
@ -222,7 +218,6 @@ sealed class Criteria(override var uniqueIdentifier: Int) : IntIdentifiable, Row
is TournamentFees -> comparison<QueryCondition.TournamentFee, Double>() is TournamentFees -> comparison<QueryCondition.TournamentFee, Double>()
is Years -> { is Years -> {
val years = arrayListOf<Query>() val years = arrayListOf<Query>()
val realm = Realm.getDefaultInstance()
val lastSession = realm.where<Session>().isNotNull("startDate").sort("startDate", Sort.DESCENDING).findFirst() val lastSession = realm.where<Session>().isNotNull("startDate").sort("startDate", Sort.DESCENDING).findFirst()
val yearNow = lastSession?.year ?: return years val yearNow = lastSession?.year ?: return years
@ -234,19 +229,16 @@ sealed class Criteria(override var uniqueIdentifier: Int) : IntIdentifiable, Row
years.add(Query(yearCondition)) years.add(Query(yearCondition))
} }
} }
realm.close()
years years
} }
is Stakes -> comparison<QueryCondition.AnyStake, String>() is Stakes -> comparison<QueryCondition.AnyStake, String>()
is ListCustomFields -> comparison<CustomFieldEntry>() is ListCustomFields -> comparison<CustomFieldEntry>()
is ValueCustomFields -> { is ValueCustomFields -> {
val realm = Realm.getDefaultInstance()
val queries = when (this.customFieldType(realm)) { val queries = when (this.customFieldType(realm)) {
CustomField.Type.AMOUNT.uniqueIdentifier -> comparison<QueryCondition.CustomFieldAmountQuery, Double >() CustomField.Type.AMOUNT.uniqueIdentifier -> comparison<QueryCondition.CustomFieldAmountQuery, Double >()
CustomField.Type.NUMBER.uniqueIdentifier -> comparison<QueryCondition.CustomFieldNumberQuery, Double >() CustomField.Type.NUMBER.uniqueIdentifier -> comparison<QueryCondition.CustomFieldNumberQuery, Double >()
else -> throw PokerAnalyticsException.ComparisonCriteriaUnhandled(this) else -> throw PokerAnalyticsException.ComparisonCriteriaUnhandled(this)
} }
realm.close()
queries queries
} }
is Duration -> { is Duration -> {

@ -8,7 +8,7 @@ import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.Deletable import net.pokeranalytics.android.model.interfaces.Deletable
import net.pokeranalytics.android.model.realm.* import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.ui.modules.data.EditableDataActivity import net.pokeranalytics.android.ui.modules.data.*
import net.pokeranalytics.android.ui.modules.handhistory.HandHistoryActivity import net.pokeranalytics.android.ui.modules.handhistory.HandHistoryActivity
import net.pokeranalytics.android.ui.view.Localizable import net.pokeranalytics.android.ui.view.Localizable
import net.pokeranalytics.android.util.extensions.findById import net.pokeranalytics.android.util.extensions.findById
@ -30,7 +30,11 @@ enum class LiveData : Localizable {
PLAYER, PLAYER,
HAND_HISTORY; HAND_HISTORY;
var subType:Int? = null var subType: Int? = null
fun instanceFromOrdinal(ordinal: Int): LiveData {
return values()[ordinal]
}
val relatedEntity: Class<out Deletable> val relatedEntity: Class<out Deletable>
get() { get() {
@ -52,10 +56,10 @@ enum class LiveData : Localizable {
fun updateOrCreate(realm: Realm, primaryKey: String?): Deletable { fun updateOrCreate(realm: Realm, primaryKey: String?): Deletable {
val proxyItem: Deletable? = this.getData(realm, primaryKey) val proxyItem: Deletable? = this.getData(realm, primaryKey)
proxyItem?.let { return proxyItem?.let {
return realm.copyFromRealm(it) realm.copyFromRealm(it)
} ?: run { } ?: run {
return this.newEntity() this.newEntity()
} }
} }
@ -186,4 +190,17 @@ enum class LiveData : Localizable {
} }
} }
val dataFragment: EditableDataFragment
get() {
return when (this) {
BANKROLL -> BankrollDataFragment()
LOCATION -> LocationDataFragment()
TRANSACTION -> TransactionDataFragment()
CUSTOM_FIELD -> CustomFieldDataFragment()
TRANSACTION_TYPE -> TransactionTypeDataFragment()
PLAYER -> PlayerDataFragment()
else -> EditableDataFragment()
}
}
} }

@ -133,7 +133,7 @@ val AbstractList<Session>.hourlyDuration: Double
return intervals.sumOf { it.hourlyDuration } return intervals.sumOf { it.hourlyDuration }
} }
class TimeInterval(var start: Date, var end: Date, var breakDuration: Long) { class TimeInterval(var start: Date, var end: Date, var breakDuration: Long = 0L) {
val hourlyDuration: Double val hourlyDuration: Double
get() { get() {

@ -32,9 +32,9 @@ import net.pokeranalytics.android.util.CrashLogging
* *
*/ */
class UnmanagedFilterField(message: String) : Exception(message) { //class UnmanagedFilterField(message: String) : Exception(message) {
//
} //}
/** /**
* Companion-level Interface to indicate an RealmObject class can be filtered and to provide all the fieldNames (eg: parameter's path) needed to be query on. * Companion-level Interface to indicate an RealmObject class can be filtered and to provide all the fieldNames (eg: parameter's path) needed to be query on.
@ -64,6 +64,7 @@ class FilterHelper {
SessionSet::class.java -> SessionSet.fieldNameForQueryType(queryCondition) SessionSet::class.java -> SessionSet.fieldNameForQueryType(queryCondition)
Transaction::class.java -> Transaction.fieldNameForQueryType(queryCondition) Transaction::class.java -> Transaction.fieldNameForQueryType(queryCondition)
Result::class.java -> Result.fieldNameForQueryType(queryCondition) Result::class.java -> Result.fieldNameForQueryType(queryCondition)
FlatTimeInterval::class.java -> FlatTimeInterval.fieldNameForQueryType(queryCondition)
else -> { else -> {
CrashLogging.logException(PAIllegalStateException("Filterable type fields are not defined for condition ${queryCondition::class}, class ${T::class}")) CrashLogging.logException(PAIllegalStateException("Filterable type fields are not defined for condition ${queryCondition::class}, class ${T::class}"))
null null

@ -73,16 +73,16 @@ class Query {
it is QueryCondition.EndedToTime it is QueryCondition.EndedToTime
} }
this.conditions.forEach { for (condition in this.conditions) {
realmQuery = when (it) { realmQuery = when (condition) {
is QueryCondition.StartedFromTime -> { is QueryCondition.StartedFromTime -> {
it.queryWith(realmQuery, queryToTime) condition.queryWith(realmQuery, queryToTime)
} }
is QueryCondition.EndedToTime -> { is QueryCondition.EndedToTime -> {
it.queryWith(realmQuery, queryFromTime) condition.queryWith(realmQuery, queryFromTime)
} }
else -> { else -> {
it.queryWith(realmQuery) condition.queryWith(realmQuery)
} }
} }
} }

@ -723,7 +723,7 @@ sealed class QueryCondition : RowRepresentable {
constructor(customFieldEntry: CustomFieldEntry) : this() { constructor(customFieldEntry: CustomFieldEntry) : this() {
this.setObject(customFieldEntry) this.setObject(customFieldEntry)
this.customFieldId = customFieldEntry.customField?.id this.customFieldId = customFieldEntry.managedCustomField?.id
?: throw PokerAnalyticsException.QueryValueMapUnexpectedValue ?: throw PokerAnalyticsException.QueryValueMapUnexpectedValue
} }

@ -9,7 +9,7 @@ import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.realm.* import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.model.utils.Seed import net.pokeranalytics.android.model.utils.Seed
import net.pokeranalytics.android.model.utils.SessionSetManager import net.pokeranalytics.android.model.utils.TimeManager
import net.pokeranalytics.android.util.BLIND_SEPARATOR import net.pokeranalytics.android.util.BLIND_SEPARATOR
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import java.text.NumberFormat import java.text.NumberFormat
@ -68,10 +68,9 @@ class Patcher {
private fun patchMissingTransactionTypes(context: Context) { private fun patchMissingTransactionTypes(context: Context) {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransactionAsync { asyncRealm ->
val transactionTypes = TransactionType.Value.values() val transactionTypes = TransactionType.Value.values()
Seed.createDefaultTransactionTypes(transactionTypes, context, asyncRealm)
realm.executeTransaction {
Seed.createDefaultTransactionTypes(transactionTypes, context, realm)
} }
realm.close() realm.close()
@ -80,11 +79,12 @@ class Patcher {
private fun patchBreaks() { private fun patchBreaks() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val sets = realm.where(SessionSet::class.java).findAll() realm.executeTransactionAsync { asyncRealm ->
val sessions = Filter.queryOn<Session>(realm, Query(QueryCondition.IsCash))
val results = realm.where(Result::class.java).findAll() val sets = asyncRealm.where(SessionSet::class.java).findAll()
val sessions = Filter.queryOn<Session>(asyncRealm, Query(QueryCondition.IsCash))
val results = asyncRealm.where(Result::class.java).findAll()
realm.executeTransaction {
sets.forEach { sets.forEach {
it.computeStats() it.computeStats()
} }
@ -98,13 +98,12 @@ class Patcher {
} }
realm.close() realm.close()
} }
private fun patchDefaultTransactionTypes(context: Context) { private fun patchDefaultTransactionTypes(context: Context) {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
val tts = realm.where(TransactionType::class.java).findAll() val tts = asyncRealm.where(TransactionType::class.java).findAll()
tts.forEach { tt -> tts.forEach { tt ->
tt.kind?.let { kind -> tt.kind?.let { kind ->
val value = TransactionType.Value.values()[kind] val value = TransactionType.Value.values()[kind]
@ -117,8 +116,8 @@ class Patcher {
private fun patchStakes() { private fun patchStakes() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
val sessions = realm.where(Session::class.java).findAll() val sessions = asyncRealm.where(Session::class.java).findAll()
sessions.forEach { session -> sessions.forEach { session ->
val blinds = arrayListOf(session.cgOldSmallBlind, session.cgOldBigBlind).filterNotNull() val blinds = arrayListOf(session.cgOldSmallBlind, session.cgOldBigBlind).filterNotNull()
val blindsFormatted = blinds.map { NumberFormat.getInstance().format(it) } val blindsFormatted = blinds.map { NumberFormat.getInstance().format(it) }
@ -128,7 +127,7 @@ class Patcher {
} }
} }
val handHistories = realm.where(HandHistory::class.java).findAll() val handHistories = asyncRealm.where(HandHistory::class.java).findAll()
handHistories.forEach { hh -> handHistories.forEach { hh ->
val blinds = arrayListOf(hh.oldSmallBlind, hh.oldBigBlind).filterNotNull() val blinds = arrayListOf(hh.oldSmallBlind, hh.oldBigBlind).filterNotNull()
val blindsFormatted = blinds.map { NumberFormat.getInstance().format(it) } val blindsFormatted = blinds.map { NumberFormat.getInstance().format(it) }
@ -143,8 +142,8 @@ class Patcher {
private fun patchNegativeLimits() { private fun patchNegativeLimits() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
val sessions = realm.where(Session::class.java).lessThan("limit", 0).findAll() val sessions = asyncRealm.where(Session::class.java).lessThan("limit", 0).findAll()
sessions.forEach { session -> sessions.forEach { session ->
session.limit = null session.limit = null
} }
@ -154,10 +153,10 @@ class Patcher {
private fun cleanBlindsFilters() { private fun cleanBlindsFilters() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
val blindFilterConditions = realm.where(FilterCondition::class.java).equalTo("filterName", "AnyBlind").findAll() val blindFilterConditions = asyncRealm.where(FilterCondition::class.java).equalTo("filterName", "AnyBlind").findAll()
val filterIds = blindFilterConditions.mapNotNull { it.filters?.firstOrNull() }.map { it.id } val filterIds = blindFilterConditions.mapNotNull { it.filters?.firstOrNull() }.map { it.id }
val filters = realm.where(Filter::class.java).`in`("id", filterIds.toTypedArray()).findAll() val filters = asyncRealm.where(Filter::class.java).`in`("id", filterIds.toTypedArray()).findAll()
filters.deleteAllFromRealm() filters.deleteAllFromRealm()
} }
realm.close() realm.close()
@ -170,11 +169,11 @@ class Patcher {
private fun patchSessionSet() { private fun patchSessionSet() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
realm.where(SessionSet::class.java).findAll().deleteAllFromRealm() asyncRealm.where(SessionSet::class.java).findAll().deleteAllFromRealm()
val sessions = realm.where(Session::class.java).isNotNull("startDate").isNotNull("endDate").findAll() val sessions = asyncRealm.where(Session::class.java).isNotNull("startDate").isNotNull("endDate").findAll()
sessions.forEach { session -> sessions.forEach { session ->
SessionSetManager.updateTimeline(session) TimeManager.updateTimeline(session)
} }
} }
realm.close() realm.close()
@ -187,8 +186,8 @@ class Patcher {
*/ */
private fun patchComputableResults() { private fun patchComputableResults() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
val crs = realm.where(ComputableResult::class.java).findAll() val crs = asyncRealm.where(ComputableResult::class.java).findAll()
crs.forEach { cr -> crs.forEach { cr ->
cr.session?.let { cr.updateWith(it) } cr.session?.let { cr.updateWith(it) }
} }
@ -210,8 +209,8 @@ class Patcher {
private fun patchZeroTable() { private fun patchZeroTable() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val zero = 0 val zero = 0
val sessions = realm.where<Session>().equalTo("numberOfTables", zero).findAll() realm.executeTransactionAsync { asyncRealm ->
realm.executeTransaction { val sessions = asyncRealm.where<Session>().equalTo("numberOfTables", zero).findAll()
sessions.forEach { s -> sessions.forEach { s ->
s.numberOfTables = 1 s.numberOfTables = 1
} }
@ -221,8 +220,8 @@ class Patcher {
private fun patchRatedAmounts() { private fun patchRatedAmounts() {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val transactions = realm.where<Transaction>().findAll() realm.executeTransactionAsync { asyncRealm ->
realm.executeTransaction { val transactions = asyncRealm.where<Transaction>().findAll()
transactions.forEach { t -> transactions.forEach { t ->
t.computeRatedAmount() t.computeRatedAmount()
} }

@ -3,6 +3,7 @@ package net.pokeranalytics.android.model.migrations
import io.realm.DynamicRealm import io.realm.DynamicRealm
import io.realm.RealmMigration import io.realm.RealmMigration
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.realm.FlatTimeInterval
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
@ -335,6 +336,27 @@ class PokerAnalyticsMigration : RealmMigration {
currentVersion++ currentVersion++
} }
// Migrate to version 15
if (currentVersion == 14) {
schema.get("Result")?.let { crs ->
crs.addField("id", String::class.java).setRequired("id", true)
crs.addPrimaryKey("id")
}
schema.create("FlatTimeInterval")?.let { fs ->
fs.addField("id", String::class.java).setRequired("id", true)
fs.addPrimaryKey("id")
fs.addField("startDate", Date::class.java).setRequired("startDate", true)
fs.addField("endDate", Date::class.java).setRequired("endDate", true)
fs.addField("duration", Long::class.java)
schema.get("Session")?.let { ss ->
ss.addRealmSetField("flatTimeIntervals", fs)
}
}
currentVersion++
}
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {

@ -42,7 +42,7 @@ open class Comment : RealmObject(), Manageable, RowRepresentable, RowUpdatable {
} }
override fun getDisplayName(context: Context): String { override fun getDisplayName(context: Context): String {
return if (content.isNotEmpty()) content else NULL_TEXT return content.ifEmpty { NULL_TEXT }
} }
// override fun startEditing(dataSource: Any?, parent: Fragment?) { // override fun startEditing(dataSource: Any?, parent: Fragment?) {

@ -22,7 +22,7 @@ open class ComputableResult : RealmObject(), Filterable {
var session: Session? = null var session: Session? = null
var ratedTips: Double = 0.0 private var ratedTips: Double = 0.0
fun updateWith(session: Session) { fun updateWith(session: Session) {
@ -38,8 +38,12 @@ open class ComputableResult : RealmObject(), Filterable {
this.bbNet = session.bbNet this.bbNet = session.bbNet
this.hasBigBlind = if (session.cgBiggestBet != null) 1 else 0 this.hasBigBlind = if (session.cgBiggestBet != null) 1 else 0
this.estimatedHands = session.estimatedHands this.estimatedHands = session.estimatedHands
this.bbPer100Hands =
session.bbNet / (session.numberOfHandsPerHour * session.hourlyDuration) * 100 this.bbPer100Hands = if (this.estimatedHands > 0.0) {
session.bbNet / this.estimatedHands * 100
} else {
0.0
}
} }

@ -139,10 +139,6 @@ open class CustomField : RealmObject(), RowRepresentable, RowUpdatable, NameMana
} }
} }
override fun isValidForSave(): Boolean {
return super.isValidForSave()
}
override fun getFailedSaveMessage(status: SaveValidityStatus): Int { override fun getFailedSaveMessage(status: SaveValidityStatus): Int {
return when (status) { return when (status) {
SaveValidityStatus.DATA_INVALID -> R.string.cf_empty_field_error SaveValidityStatus.DATA_INVALID -> R.string.cf_empty_field_error
@ -250,7 +246,7 @@ open class CustomField : RealmObject(), RowRepresentable, RowUpdatable, NameMana
fun cleanupEntries() { // called when saving the custom field fun cleanupEntries() { // called when saving the custom field
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync {
this.entriesToDelete.forEach { // entries are out of realm this.entriesToDelete.forEach { // entries are out of realm
realm.where<CustomFieldEntry>().equalTo("id", it.id).findFirst()?.deleteFromRealm() realm.where<CustomFieldEntry>().equalTo("id", it.id).findFirst()?.deleteFromRealm()
} }
@ -272,26 +268,6 @@ open class CustomField : RealmObject(), RowRepresentable, RowUpdatable, NameMana
} }
} }
/**
* Clean the entries if the type is not a list & remove the deleted entries from realm
*/
// fun cleanEntries(realm: Realm) {
// realm.executeTransaction {
//
// if (!isListType) {
// entriesToDelete.addAll(entries)
// entries.clear()
// }
//
// // @TODO
// entriesToDelete.forEach {
// Timber.d("Delete entry: V=${it.value} N=${it.numericValue} / ID=${it.id}")
// realm.where<CustomFieldEntry>().equalTo("id", it.id).findFirst()?.deleteFromRealm()
// }
// entriesToDelete.clear()
// }
// }
/** /**
* Returns a comparison criteria based on this custom field * Returns a comparison criteria based on this custom field
*/ */

@ -22,6 +22,7 @@ import net.pokeranalytics.android.ui.view.RowUpdatable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.extensions.toCurrency import net.pokeranalytics.android.util.extensions.toCurrency
import timber.log.Timber
import java.text.NumberFormat import java.text.NumberFormat
import java.util.* import java.util.*
import java.util.Currency import java.util.Currency
@ -46,7 +47,7 @@ open class CustomFieldEntry : RealmObject(), NameManageable, RowRepresentable, R
@LinkingObjects("entries") @LinkingObjects("entries")
val customFields: RealmResults<CustomField>? = null val customFields: RealmResults<CustomField>? = null
val customField: CustomField? val managedCustomField: CustomField?
get() { get() {
return this.customFields?.first() return this.customFields?.first()
} }
@ -89,7 +90,7 @@ open class CustomFieldEntry : RealmObject(), NameManageable, RowRepresentable, R
} }
override fun getDisplayName(context: Context): String { override fun getDisplayName(context: Context): String {
return if (value.isNotEmpty()) value else NULL_TEXT return value.ifEmpty { NULL_TEXT }
} }
override fun editingDescriptors(map: Map<String, Any?>): ArrayList<RowRepresentableEditDescriptor>? { override fun editingDescriptors(map: Map<String, Any?>): ArrayList<RowRepresentableEditDescriptor>? {
@ -136,8 +137,8 @@ open class CustomFieldEntry : RealmObject(), NameManageable, RowRepresentable, R
/** /**
* Return the amount * Return the amount
*/ */
fun getFormattedValue(currency: Currency? = null): String { fun getFormattedValue(parentCustomField: CustomField, currency: Currency? = null): String {
return when (customField?.type) { return when (parentCustomField.type) {
CustomField.Type.AMOUNT.uniqueIdentifier -> { CustomField.Type.AMOUNT.uniqueIdentifier -> {
numericValue?.toCurrency(currency) ?: run { NULL_TEXT } numericValue?.toCurrency(currency) ?: run { NULL_TEXT }
} }
@ -145,6 +146,7 @@ open class CustomFieldEntry : RealmObject(), NameManageable, RowRepresentable, R
NumberFormat.getInstance().format(this.numericValue) NumberFormat.getInstance().format(this.numericValue)
} }
else -> { else -> {
Timber.d("FORMATTED = $value")
value value
} }
} }

@ -77,7 +77,7 @@ open class Filter : RealmObject(), RowRepresentable, RowUpdatable, Deletable, Us
var filterConditions: RealmList<FilterCondition> = RealmList() var filterConditions: RealmList<FilterCondition> = RealmList()
private set private set
private var filterableTypeUniqueIdentifier: Int? = null var filterableTypeUniqueIdentifier: Int? = null
val filterableType: FilterableType val filterableType: FilterableType
get() { get() {
@ -109,7 +109,7 @@ open class Filter : RealmObject(), RowRepresentable, RowUpdatable, Deletable, Us
val previousCondition = filterConditions.filter { val previousCondition = filterConditions.filter {
it.filterName == newFilterCondition.filterName && it.operator == newFilterCondition.operator it.filterName == newFilterCondition.filterName && it.operator == newFilterCondition.operator
} }
filterConditions.removeAll(previousCondition) filterConditions.removeAll(previousCondition.toSet())
filterConditions.add(newFilterCondition) filterConditions.add(newFilterCondition)
} }
} }
@ -118,7 +118,7 @@ open class Filter : RealmObject(), RowRepresentable, RowUpdatable, Deletable, Us
fun remove(filterCategoryRow: FilterCategoryRow) { fun remove(filterCategoryRow: FilterCategoryRow) {
val sections = filterCategoryRow.filterSectionRows.map { it.name } val sections = filterCategoryRow.filterSectionRows.map { it.name }
val savedSections = filterConditions.filter { sections.contains(it.sectionName) } val savedSections = filterConditions.filter { sections.contains(it.sectionName) }
this.filterConditions.removeAll(savedSections) this.filterConditions.removeAll(savedSections.toSet())
} }
fun countBy(filterCategoryRow: FilterCategoryRow): Int { fun countBy(filterCategoryRow: FilterCategoryRow): Int {

@ -63,16 +63,6 @@ open class FilterCondition() : RealmObject() {
} }
} }
fun <T> getv(clazz: Class<T>) : T {
return when (clazz) {
Int::class -> intValue ?: 0
Double::class -> doubleValue?: 0.0
Date::class -> dateValue ?: Date()
String::class -> stringValue ?: ""
else -> throw PokerAnalyticsException.QueryValueMapUnexpectedValue
} as T
}
inline fun <reified T> getValue(): T { inline fun <reified T> getValue(): T {
return when (T::class) { return when (T::class) {
Int::class -> intValue ?: 0 Int::class -> intValue ?: 0

@ -0,0 +1,61 @@
package net.pokeranalytics.android.model.realm
import io.realm.RealmObject
import io.realm.RealmResults
import io.realm.annotations.LinkingObjects
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass
import net.pokeranalytics.android.model.filter.Filterable
import net.pokeranalytics.android.model.filter.QueryCondition
import java.util.*
@RealmClass
open class FlatTimeInterval : RealmObject(), Filterable {
@PrimaryKey
var id = UUID.randomUUID().toString()
/**
* The start date of the session
*/
var startDate: Date = Date()
set(value) {
field = value
this.computeDuration()
}
/**
* The start date of the session
*/
var endDate: Date = Date()
set(value) {
field = value
this.computeDuration()
}
/**
* the net duration of the session, automatically calculated
*/
var duration: Long = 0L
@LinkingObjects("flatTimeIntervals")
val sessions: RealmResults<Session>? = null
private fun computeDuration() {
duration = endDate.time - startDate.time
}
companion object {
fun fieldNameForQueryType(queryCondition: Class <out QueryCondition>): String? {
Session.fieldNameForQueryType(queryCondition)?.let {
return "sessions.$it"
}
return null
}
}
}

@ -50,13 +50,6 @@ open class Game : RealmObject(), NameManageable, StaticRowRepresentableDataSourc
@Ignore @Ignore
override val ownerClass: Class<out RealmModel> = Session::class.java override val ownerClass: Class<out RealmModel> = Session::class.java
fun getNotNullShortName() : String {
this.shortName?.let {
return it
}
return this.name
}
override fun getDisplayName(context: Context): String { override fun getDisplayName(context: Context): String {
return this.name return this.name
} }
@ -71,7 +64,7 @@ open class Game : RealmObject(), NameManageable, StaticRowRepresentableDataSourc
tag: Int tag: Int
): CharSequence { ): CharSequence {
return when (row) { return when (row) {
SimpleRow.NAME -> if (this.name.isNotEmpty()) this.name else NULL_TEXT SimpleRow.NAME -> this.name.ifEmpty { NULL_TEXT }
GamePropertiesRow.SHORT_NAME -> this.shortName ?: NULL_TEXT GamePropertiesRow.SHORT_NAME -> this.shortName ?: NULL_TEXT
else -> return super.charSequenceForRow(row, context, 0) else -> return super.charSequenceForRow(row, context, 0)
} }

@ -83,7 +83,7 @@ open class Player : RealmObject(), NameManageable, Savable, Deletable, StaticRow
tag: Int tag: Int
): CharSequence { ): CharSequence {
return when (row) { return when (row) {
PlayerPropertiesRow.NAME -> if (this.name.isNotEmpty()) this.name else NULL_TEXT PlayerPropertiesRow.NAME -> this.name.ifEmpty { NULL_TEXT }
else -> return super.charSequenceForRow(row, context, 0) else -> return super.charSequenceForRow(row, context, 0)
} }
} }
@ -174,7 +174,7 @@ open class Player : RealmObject(), NameManageable, Savable, Deletable, StaticRow
*/ */
fun cleanupComments() { // called when saving the custom field fun cleanupComments() { // called when saving the custom field
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { realm.executeTransactionAsync {
this.commentsToDelete.forEach { // entries are out of realm this.commentsToDelete.forEach { // entries are out of realm
realm.where<Comment>().equalTo("id", it.id).findFirst()?.deleteFromRealm() realm.where<Comment>().equalTo("id", it.id).findFirst()?.deleteFromRealm()
} }

@ -3,18 +3,20 @@ package net.pokeranalytics.android.model.realm
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.RealmResults import io.realm.RealmResults
import io.realm.annotations.Ignore
import io.realm.annotations.LinkingObjects import io.realm.annotations.LinkingObjects
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass import io.realm.annotations.RealmClass
import net.pokeranalytics.android.exceptions.PADataModelException
import net.pokeranalytics.android.model.filter.Filterable import net.pokeranalytics.android.model.filter.Filterable
import net.pokeranalytics.android.model.filter.QueryCondition import net.pokeranalytics.android.model.filter.QueryCondition
import java.util.*
@RealmClass @RealmClass
open class Result : RealmObject(), Filterable { open class Result : RealmObject(), Filterable {
companion object { companion object {
fun fieldNameForQueryType(queryCondition: Class < out QueryCondition>): String? { fun fieldNameForQueryType(queryCondition: Class <out QueryCondition>): String? {
Session.fieldNameForQueryType(queryCondition)?.let { Session.fieldNameForQueryType(queryCondition)?.let {
return "sessions.$it" return "sessions.$it"
} }
@ -22,13 +24,17 @@ open class Result : RealmObject(), Filterable {
} }
} }
@PrimaryKey
var id = UUID.randomUUID().toString()
/** /**
* The buyin amount * The buyin amount
*/ */
var buyin: Double? = null var buyin: Double? = null
set(value) { set(value) {
field = value field = value
this.computeNumberOfRebuy() // this.computeNumberOfRebuy()
this.computeNet(true) this.computeNet(true)
} }
@ -39,9 +45,6 @@ open class Result : RealmObject(), Filterable {
set(value) { set(value) {
field = value field = value
this.computeNet(true) this.computeNet(true)
if (value != null) {
this.session?.end()
}
} }
/** /**
@ -49,20 +52,8 @@ open class Result : RealmObject(), Filterable {
*/ */
var netResult: Double? = null var netResult: Double? = null
set(value) { set(value) {
// this.session?.bankroll?.let { bankroll ->
// if (bankroll.live) {
// throw PAIllegalStateException("Can't set net result on a live bankroll")
// }
// } ?: run {
// throw PAIllegalStateException("Session doesn't have any bankroll")
// }
field = value field = value
this.computeNet(false) this.computeNet(false)
if (value != null) {
this.session?.end()
}
} }
/** /**
@ -75,10 +66,6 @@ open class Result : RealmObject(), Filterable {
* Tips * Tips
*/ */
var tips: Double? = null var tips: Double? = null
set(value) {
field = value
this.session?.computeStats()
}
// The transactions associated with the Result, impacting the result // The transactions associated with the Result, impacting the result
var transactions: RealmList<Transaction> = RealmList() var transactions: RealmList<Transaction> = RealmList()
@ -91,21 +78,23 @@ open class Result : RealmObject(), Filterable {
var tournamentFinalPosition: Int? = null var tournamentFinalPosition: Int? = null
// Number of rebuys // Number of rebuys
var numberOfRebuy: Double? = null private var numberOfRebuy: Double? = null
@LinkingObjects("result") @LinkingObjects("result")
private val sessions: RealmResults<Session>? = null private val sessions: RealmResults<Session>? = null
@Ignore private val managedSession: Session
val session: Session? = this.sessions?.firstOrNull() get() {
return this.sessions?.firstOrNull() ?: throw PADataModelException("Unmanaged Result")
}
/** /**
* Returns 1 if the session is positive * Returns 1 if the session is positive
*/ */
val isPositive: Int val isPositive: Int
get() { get() {
return if (session?.isTournament() == true) { return if (managedSession.isTournament()) {
if (this.cashout ?: -1.0 >= 0.0) 1 else 0 // if cashout is null we want to count a negative session if ((this.cashout ?: -1.0) >= 0.0) 1 else 0 // if cashout is null we want to count a negative session
} else { } else {
if (this.net >= 0.0) 1 else 0 if (this.net >= 0.0) 1 else 0
} }
@ -124,13 +113,11 @@ open class Result : RealmObject(), Filterable {
} else if (buyin != null || cashout != null) { } else if (buyin != null || cashout != null) {
useBuyin = true useBuyin = true
} else { } else {
this.session?.let { session -> if (this.managedSession.isCashGame() && !this.managedSession.isLive) {
if (session.isCashGame() && !session.isLive) {
useBuyin = false useBuyin = false
} }
} }
} }
}
if (useBuyin) { if (useBuyin) {
val buyin = this.buyin ?: 0.0 val buyin = this.buyin ?: 0.0
@ -142,15 +129,14 @@ open class Result : RealmObject(), Filterable {
} }
// Precompute results // Precompute results
this.session?.computeStats() // this.managedSession.computeStats()
this.session?.sessionSet?.computeStats() // this.managedSession.sessionSet?.computeStats()
} }
// Computes the number of rebuy // Computes the number of rebuy
fun computeNumberOfRebuy() { fun computeNumberOfRebuy() {
this.session?.let { if (this.managedSession.isCashGame()) {
if (it.isCashGame()) { this.managedSession.cgBiggestBet?.let { bb ->
it.cgBiggestBet?.let { bb ->
if (bb > 0.0) { if (bb > 0.0) {
this.numberOfRebuy = (this.buyin ?: 0.0) / (bb * 100.0) this.numberOfRebuy = (this.buyin ?: 0.0) / (bb * 100.0)
} else { } else {
@ -158,7 +144,7 @@ open class Result : RealmObject(), Filterable {
} }
} }
} else { } else {
it.tournamentEntryFee?.let { entryFee -> this.managedSession.tournamentEntryFee?.let { entryFee ->
if (entryFee > 0.0) { if (entryFee > 0.0) {
this.numberOfRebuy = (this.buyin ?: 0.0) / entryFee this.numberOfRebuy = (this.buyin ?: 0.0) / entryFee
} else { } else {
@ -167,7 +153,6 @@ open class Result : RealmObject(), Filterable {
} }
} }
} }
}
// @todo tips? // @todo tips?

@ -27,16 +27,13 @@ import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.filter.QueryCondition.* import net.pokeranalytics.android.model.filter.QueryCondition.*
import net.pokeranalytics.android.model.interfaces.* import net.pokeranalytics.android.model.interfaces.*
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.model.utils.SessionSetManager import net.pokeranalytics.android.model.utils.TimeManager
import net.pokeranalytics.android.ui.adapter.UnmanagedRowRepresentableException import net.pokeranalytics.android.ui.adapter.UnmanagedRowRepresentableException
import net.pokeranalytics.android.ui.graph.Graph import net.pokeranalytics.android.ui.graph.Graph
import net.pokeranalytics.android.ui.view.* import net.pokeranalytics.android.ui.view.*
import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow
import net.pokeranalytics.android.util.* import net.pokeranalytics.android.util.*
import net.pokeranalytics.android.util.extensions.hourMinute import net.pokeranalytics.android.util.extensions.*
import net.pokeranalytics.android.util.extensions.shortDateTime
import net.pokeranalytics.android.util.extensions.toCurrency
import net.pokeranalytics.android.util.extensions.toMinutes
import java.text.DateFormat import java.text.DateFormat
import java.text.NumberFormat import java.text.NumberFormat
import java.text.ParseException import java.text.ParseException
@ -67,24 +64,32 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
companion object { companion object {
fun newInstance(realm: Realm, isTournament: Boolean, bankroll: Bankroll? = null, managed: Boolean = true): Session { fun newInstance(realm: Realm, isTournament: Boolean, bankroll: Bankroll? = null): Session {
val session = Session() val session = Session()
session.result = Result() session.result = Result()
if (bankroll != null) { if (bankroll != null) {
session.bankroll = bankroll session.bankroll = realm.copyFromRealm(bankroll)
} else { } else {
session.bankroll = realm.where<Bankroll>().findFirst() realm.where<Bankroll>().findFirst()?.let { br ->
session.bankroll = realm.copyFromRealm(br)
}
} }
session.type = if (isTournament) Type.TOURNAMENT.ordinal else Type.CASH_GAME.ordinal session.type = if (isTournament) Type.TOURNAMENT.ordinal else Type.CASH_GAME.ordinal
session.limit = Limit.NO.ordinal session.limit = Limit.NO.ordinal
session.game = realm.where(Game::class.java).equalTo("shortName", "HE").findFirst()
return if (managed) { realm.where(Game::class.java)
realm.copyToRealm(session) .equalTo("shortName", "HE").findFirst()?.let {
} else { session.game = realm.copyFromRealm(it)
session
} }
return session
// return if (managed) {
// realm.copyToRealm(session)
// } else {
// session
// }
} }
fun fieldNameForQueryType(queryCondition: Class < out QueryCondition >): String? { fun fieldNameForQueryType(queryCondition: Class < out QueryCondition >): String? {
@ -157,6 +162,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
@Ignore @Ignore
val computableResult: ComputableResult? = this.computableResults?.firstOrNull() val computableResult: ComputableResult? = this.computableResults?.firstOrNull()
// Timed interface // Timed interface
override var dayOfWeek: Int? = null override var dayOfWeek: Int? = null
@ -193,6 +199,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
*/ */
var startDate: Date? = null var startDate: Date? = null
set(value) { set(value) {
val previous = this.startDate
field = value field = value
if (value == null) { if (value == null) {
startDateHourMinuteComponent = null startDateHourMinuteComponent = null
@ -204,12 +211,14 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
this.updateTimeParameter(field) this.updateTimeParameter(field)
this.computeNetDuration() this.computeNetDuration()
// nullifies enddate when setting the start date after the end date // nullifies the endDate when setting the start date after the end date
if (value != null && this.endDate != null && value.after(this.endDate)) { if (value != null && this.endDate != null && value.after(this.endDate)) {
this.endDate = null this.endDate = null
} }
this.dateChanged()
this.computeStats() TimeManager.startChanged(this, min(previous, value))
// this.computeStats()
} }
/** /**
@ -218,6 +227,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
@Index @Index
var endDate: Date? = null var endDate: Date? = null
set(value) { set(value) {
val previous = this.endDate
field = value field = value
if (value == null) { if (value == null) {
endDateHourMinuteComponent = null endDateHourMinuteComponent = null
@ -228,9 +238,9 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
} }
this.computeNetDuration() this.computeNetDuration()
this.dateChanged() TimeManager.endChanged(this, max(previous, value))
this.defineDefaultTournamentBuyinIfNecessary() this.defineDefaultTournamentBuyinIfNecessary()
this.computeStats() // this.computeStats()
} }
/** /**
@ -240,7 +250,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
set(value) { set(value) {
field = value field = value
this.computeNetDuration() this.computeNetDuration()
this.computeStats() // this.computeStats()
} }
/** /**
@ -252,10 +262,6 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
* The start date of the break * The start date of the break
*/ */
override var pauseDate: Date? = null override var pauseDate: Date? = null
set(value) {
field = value
// this.updateRowRepresentation()
}
// The session set containing the sessions, which can contain multiple endedSessions // The session set containing the sessions, which can contain multiple endedSessions
var sessionSet: SessionSet? = null var sessionSet: SessionSet? = null
@ -268,7 +274,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
set(value) { set(value) {
field = value field = value
this.generateStakes() this.generateStakes()
this.computeStats() // this.computeStats()
// this.updateRowRepresentation() // this.updateRowRepresentation()
} }
@ -296,7 +302,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
set(value) { set(value) {
if (value > 0) { if (value > 0) {
field = value field = value
this.computeStats() // this.computeStats()
} }
} }
@ -314,16 +320,13 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
// The small blind value // The small blind value
var cgOldSmallBlind: Double? = null var cgOldSmallBlind: Double? = null
set(value) {
field = value
}
// The big blind value // The big blind value
var cgOldBigBlind: Double? = null var cgOldBigBlind: Double? = null
set(value) { set(value) {
field = value field = value
this.computeStats() this.computeStats()
this.result?.computeNumberOfRebuy() // this.result?.computeNumberOfRebuy()
} }
// var blinds: String? = null // var blinds: String? = null
@ -334,8 +337,8 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
field = value field = value
this.generateStakes() this.generateStakes()
this.defineHighestBet() this.defineHighestBet()
this.computeStats() // this.computeStats()
this.result?.computeNumberOfRebuy() // this.result?.computeNumberOfRebuy()
} }
var cgBlinds: String? = null var cgBlinds: String? = null
@ -343,8 +346,8 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
field = cleanupBlinds(value) field = cleanupBlinds(value)
this.generateStakes() this.generateStakes()
this.defineHighestBet() this.defineHighestBet()
this.computeStats() // this.computeStats()
this.result?.computeNumberOfRebuy() // this.result?.computeNumberOfRebuy()
} }
var cgBiggestBet: Double? = null var cgBiggestBet: Double? = null
@ -355,10 +358,6 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
// The entry fee of the tournament // The entry fee of the tournament
var tournamentEntryFee: Double? = null var tournamentEntryFee: Double? = null
set(value) {
field = value
this.result?.computeNumberOfRebuy()
}
// The total number of players who participated in the tournament // The total number of players who participated in the tournament
var tournamentNumberOfPlayers: Int? = null var tournamentNumberOfPlayers: Int? = null
@ -375,29 +374,29 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
// The custom fields values // The custom fields values
var customFieldEntries: RealmList<CustomFieldEntry> = RealmList() var customFieldEntries: RealmList<CustomFieldEntry> = RealmList()
// The list of opponents who participated to the session
var flatTimeIntervals: RealmList<FlatTimeInterval> = RealmList()
// The number of hands played during the sessions // The number of hands played during the sessions
var handsCount: Int? = null var handsCount: Int? = null
set(value) {
field = value
this.computeStats()
}
fun bankrollHasBeenUpdated() { fun bankrollHasBeenUpdated() {
this.generateStakes() this.generateStakes()
} }
/** // /**
* Manages impacts on SessionSets // * Manages impacts on SessionSets
* Should be called when the start / end date are changed // * Should be called when the start / end date are changed
*/ // */
private fun dateChanged() { // private fun dateChanged() {
if (this.endDate != null) { // SessionSetManager.updatedSession(this)
SessionSetManager.updateTimeline(this) //// if (this.endDate != null) {
} else if (this.sessionSet != null) { //// SessionSetManager.updateTimeline(this)
SessionSetManager.removeFromTimeline(this) //// } else if (this.sessionSet != null) {
} //// SessionSetManager.removeFromTimeline(this)
// this.updateRowRepresentation() //// }
} //// this.updateRowRepresentation()
// }
/** /**
* Returns a non-null date for the session * Returns a non-null date for the session
@ -436,7 +435,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
get() { get() {
val bb = this.cgBiggestBet val bb = this.cgBiggestBet
val result = this.result val result = this.result
return if (bb != null && result != null) { return if (bb != null && bb > 0.0 && result != null) {
result.net / bb result.net / bb
} else { } else {
0.0 0.0
@ -471,6 +470,11 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
return this.result?.net ?: 0.0 return this.result?.net ?: 0.0
} }
fun preCompute() {
this.computeStats()
this.result?.computeNumberOfRebuy()
}
/** /**
* Pre-compute various statIds * Pre-compute various statIds
*/ */
@ -493,16 +497,21 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
} }
} }
this.sessionSet?.computeStats() this.sessionSet?.computeStats()
} }
/** /**
* Approximates the number of hands played per hour at the table * Approximates the number of hands played per hour at the table
*/ */
val numberOfHandsPerHour: Double private val numberOfHandsPerHour: Double
get() { get() {
val tableSize = this.tableSize ?: 9 // 9 is the default table size if null val tableSize = this.tableSize ?: 9 // 9 is the default table size if null
val config = UserConfig.getConfiguration(this.realm)
val playerHandsPerHour = if (this.isLive) config.liveDealtHandsPerHour else config.onlineDealtHandsPerHour var playerHandsPerHour = 0
UserConfig.getConfiguration(this.realm) { config ->
playerHandsPerHour = if (this.isLive) config.liveDealtHandsPerHour else config.onlineDealtHandsPerHour
}
return this.numberOfTables * playerHandsPerHour / tableSize.toDouble() return this.numberOfTables * playerHandsPerHour / tableSize.toDouble()
} }
@ -557,7 +566,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
* Start or continue a session * Start or continue a session
*/ */
fun startOrContinue() { fun startOrContinue() {
realm.executeTransaction { // realm.executeTransaction {
when (val state = getState()) { when (val state = getState()) {
SessionState.PENDING, SessionState.PLANNED -> { SessionState.PENDING, SessionState.PLANNED -> {
this.startDate = Date() this.startDate = Date()
@ -576,7 +585,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
throw PAIllegalStateException("unmanaged session state: $state") throw PAIllegalStateException("unmanaged session state: $state")
} }
} }
} // }
} }
private fun defineDefaultTournamentBuyinIfNecessary() { private fun defineDefaultTournamentBuyinIfNecessary() {
@ -589,28 +598,28 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
* Pause a session * Pause a session
*/ */
fun pause() { fun pause() {
realm.executeTransaction { // realm.executeTransaction {
when (val state = getState()) { when (val state = getState()) {
SessionState.STARTED -> { SessionState.STARTED -> {
this.pauseDate = Date() this.pauseDate = Date()
} }
else -> throw PAIllegalStateException("Pausing a session in an unmanaged state: $state") else -> throw PAIllegalStateException("Pausing a session in an unmanaged state: $state")
} }
} // }
} }
/** /**
* Stop a session * Stop a session
*/ */
fun stop(context: Context) { fun stop(context: Context) {
realm.executeTransaction { // realm.executeTransaction {
when (val state = getState()) { when (val state = getState()) {
SessionState.STARTED, SessionState.PAUSED -> { SessionState.STARTED, SessionState.PAUSED -> {
this.end() this.end()
} }
else -> throw Exception("Stopping session in unmanaged state: $state") else -> throw Exception("Stopping session in unmanaged state: $state")
} }
} // }
cancelStopNotification(context) cancelStopNotification(context)
} }
@ -618,12 +627,12 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
* Restart a session * Restart a session
*/ */
fun restart() { fun restart() {
realm.executeTransaction { // realm.executeTransaction {
this.pauseDate = null this.pauseDate = null
this.startDate = Date() this.startDate = Date()
this.endDate = null this.endDate = null
this.breakDuration = 0L this.breakDuration = 0L
} // }
} }
/** /**
@ -660,7 +669,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
/** /**
* Return the game titleResId * Return the game titleResId
* Example: NL Holdem * Example: NL Hold'em
*/ */
fun getFormattedGame(): String { fun getFormattedGame(): String {
var gameTitle = "" var gameTitle = ""
@ -672,46 +681,13 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
if (game != null) { if (game != null) {
gameTitle += game?.name gameTitle += game?.name
} }
return if (gameTitle.isNotBlank()) gameTitle else NULL_TEXT return gameTitle.ifBlank { NULL_TEXT }
} }
fun getFormattedStakes(): String { fun getFormattedStakes(): String {
return this.cgStakes?.let { StakesHolder.readableStakes(it) } ?: run { NULL_TEXT } return this.cgStakes?.let { StakesHolder.readableStakes(it) } ?: run { NULL_TEXT }
//
// val formattedBlinds = StakesHolder.formattedBlinds(this.cgBlinds, this.currency)
// val formattedAntes = StakesHolder.formattedAnte(this.cgAnte, this.currency)
//
// return StakesHolder.formattedStakes(formattedBlinds, formattedAntes)
//
//
// val components = arrayListOf<String>()
// this.formattedBlinds?.let { components.add(it) }
// this.formattedAnte?.let { components.add("($it)") }
//
// return if (components.isNotEmpty()) {
// components.joinToString(" ")
// } else {
// NULL_TEXT
// }
} }
// fun formatBlinds() {
// blinds = null
// if (cgBigBlind == null) return
// cgBigBlind?.let { bb ->
// val sb = cgSmallBlind ?: bb / 2.0
// val preFormattedBlinds = "${sb.formatted}/${bb.round()}"
// println("<<<<<< bb.toCurrency(currency) : ${bb.toCurrency(currency)}")
// println("<<<<<< preFormattedBlinds : $preFormattedBlinds")
// val regex = Regex("-?\\d+(\\.\\d+)?")
// blinds = bb.toCurrency(currency).replace(regex, preFormattedBlinds)
// println("<<<<<< blinds = $blinds")
// }
// }
// LifeCycle // LifeCycle
/** /**
@ -721,10 +697,8 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
CrashLogging.log("Deletes session. Id = ${this.id}") CrashLogging.log("Deletes session. Id = ${this.id}")
if (isValid) { if (isValid) {
realm.executeTransaction {
cleanup() cleanup()
deleteFromRealm() deleteFromRealm()
}
} else { } else {
CrashLogging.log("Attempt to delete an invalid session") CrashLogging.log("Attempt to delete an invalid session")
} }
@ -737,11 +711,15 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
// Updates the timeline // Updates the timeline
this.sessionSet?.let { this.sessionSet?.let {
SessionSetManager.removeFromTimeline(this) TimeManager.removeFromTimeline(this)
} }
TimeManager.sessionDateChanged(this)
// cleanup unnecessary related objects // cleanup unnecessary related objects
this.flatTimeIntervals.deleteAllFromRealm()
this.result?.deleteFromRealm() this.result?.deleteFromRealm()
this.computableResults?.deleteAllFromRealm() this.computableResult?.deleteFromRealm()
} }
@ -776,32 +754,12 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
when (row) { when (row) {
SessionPropertiesRow.BANKROLL -> bankroll = value as Bankroll? SessionPropertiesRow.BANKROLL -> bankroll = value as Bankroll?
SessionPropertiesRow.STAKES -> if (value is Stakes) { SessionPropertiesRow.STAKES -> if (value is Stakes) {
if (value.ante != null) { if (value.ante != null) {
this.cgAnte = value.ante this.cgAnte = value.ante
} }
if (value.blinds != null) { if (value.blinds != null) {
this.cgBlinds = value.blinds this.cgBlinds = value.blinds
} }
// cgSmallBlind = try {
// (value[0] as String? ?: "0").toDouble()
// } catch (e: Exception) {
// null
// }
//
// cgBigBlind = try {
// (value[1] as String? ?: "0").toDouble()
// } catch (e: Exception) {
// null
// }
//
// cgBigBlind?.let {
// if (cgSmallBlind == null || cgSmallBlind == 0.0) {
// cgSmallBlind = it / 2.0
// }
// }
} else if (value == null) { } else if (value == null) {
this.cgBlinds = null this.cgBlinds = null
this.cgAnte = null this.cgAnte = null
@ -812,15 +770,20 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
SessionPropertiesRow.BUY_IN -> { SessionPropertiesRow.BUY_IN -> {
val localResult = getOrCreateResult() val localResult = getOrCreateResult()
localResult.buyin = value as Double? localResult.buyin = value as Double?
// this.updateRowRepresentation()
} }
SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE -> { SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE -> {
val localResult = getOrCreateResult() val localResult = getOrCreateResult()
localResult.cashout = value as Double? localResult.cashout = value as Double?
if (value != null) {
this.end()
}
} }
SessionPropertiesRow.NET_RESULT -> { SessionPropertiesRow.NET_RESULT -> {
val localResult = getOrCreateResult() val localResult = getOrCreateResult()
localResult.netResult = value as Double? localResult.netResult = value as Double?
if (value != null) {
this.end()
}
} }
SessionPropertiesRow.COMMENT -> comment = value as String? ?: "" SessionPropertiesRow.COMMENT -> comment = value as String? ?: ""
SessionPropertiesRow.END_DATE -> if (value is Date?) { SessionPropertiesRow.END_DATE -> if (value is Date?) {
@ -876,20 +839,23 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
SessionPropertiesRow.TOURNAMENT_NAME -> tournamentName = value as TournamentName? SessionPropertiesRow.TOURNAMENT_NAME -> tournamentName = value as TournamentName?
SessionPropertiesRow.TOURNAMENT_TYPE -> tournamentType = (value as TournamentType?)?.ordinal SessionPropertiesRow.TOURNAMENT_TYPE -> tournamentType = (value as TournamentType?)?.ordinal
SessionPropertiesRow.TOURNAMENT_FEATURE -> { SessionPropertiesRow.TOURNAMENT_FEATURE -> {
this.tournamentFeatures.clear()
value?.let { value?.let {
tournamentFeatures = RealmList() tournamentFeatures = RealmList()
tournamentFeatures.addAll((it as ArrayList<TournamentFeature>)) tournamentFeatures.addAll((it as List<TournamentFeature>))
} ?: run {
tournamentFeatures.removeAll(this.tournamentFeatures)
} }
} }
SessionPropertiesRow.HANDS_COUNT -> handsCount = (value as Double?)?.toInt() SessionPropertiesRow.HANDS_COUNT -> handsCount = (value as Double?)?.toInt()
SessionPropertiesRow.NUMBER_OF_TABLES -> this.numberOfTables = (value as Double?)?.toInt() ?: 1 SessionPropertiesRow.NUMBER_OF_TABLES -> this.numberOfTables = (value as Double?)?.toInt() ?: 1
is CustomField -> { is CustomField -> {
customFieldEntries.filter { it.customField?.id == row.id }.let {
customFieldEntries.removeAll(it) val entryIds = row.entries.map { it.id }
} val entries = this.customFieldEntries.intersectBy(entryIds) { it.id }
this.customFieldEntries.removeAll(entries)
// customFieldEntries.filter { it.customField?.id == row.id }.let {
// customFieldEntries.removeAll(it.toSet())
// }
when (row.type) { when (row.type) {
CustomField.Type.AMOUNT.uniqueIdentifier, CustomField.Type.AMOUNT.uniqueIdentifier,
CustomField.Type.NUMBER.uniqueIdentifier -> { CustomField.Type.NUMBER.uniqueIdentifier -> {
@ -914,7 +880,8 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
private fun getOrCreateResult(): Result { private fun getOrCreateResult(): Result {
return this.result return this.result
?: run { ?: run {
val result = realm.createObject(Result::class.java) val result = Result()
// result.inverseSession = WeakReference(this)
this.result = result this.result = result
result result
} }
@ -1028,7 +995,7 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
SessionPropertiesRow.BUY_IN -> this.result?.buyin?.toCurrency(currency) ?: NULL_TEXT SessionPropertiesRow.BUY_IN -> this.result?.buyin?.toCurrency(currency) ?: NULL_TEXT
SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE -> this.result?.cashout?.toCurrency(currency) ?: NULL_TEXT SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE -> this.result?.cashout?.toCurrency(currency) ?: NULL_TEXT
SessionPropertiesRow.NET_RESULT -> this.result?.netResult?.toCurrency(currency) ?: NULL_TEXT SessionPropertiesRow.NET_RESULT -> this.result?.netResult?.toCurrency(currency) ?: NULL_TEXT
SessionPropertiesRow.COMMENT -> if (this.comment.isNotEmpty()) this.comment else NULL_TEXT SessionPropertiesRow.COMMENT -> this.comment.ifEmpty { NULL_TEXT }
SessionPropertiesRow.END_DATE -> this.endDate?.shortDateTime() ?: NULL_TEXT SessionPropertiesRow.END_DATE -> this.endDate?.shortDateTime() ?: NULL_TEXT
SessionPropertiesRow.GAME -> getFormattedGame() SessionPropertiesRow.GAME -> getFormattedGame()
SessionPropertiesRow.INITIAL_BUY_IN -> tournamentEntryFee?.toCurrency(currency) ?: NULL_TEXT SessionPropertiesRow.INITIAL_BUY_IN -> tournamentEntryFee?.toCurrency(currency) ?: NULL_TEXT
@ -1063,8 +1030,10 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
SessionPropertiesRow.HANDS_COUNT -> this.handsCountFormatted(context) SessionPropertiesRow.HANDS_COUNT -> this.handsCountFormatted(context)
SessionPropertiesRow.NUMBER_OF_TABLES -> this.numberOfTables.toString() SessionPropertiesRow.NUMBER_OF_TABLES -> this.numberOfTables.toString()
is CustomField -> { is CustomField -> {
customFieldEntries.find { it.customField?.id == row.id }?.let { customFieldEntry -> val entryIds = this.customFieldEntries.map { it.id }
return customFieldEntry.getFormattedValue(currency) val entries = row.entries.intersectBy(entryIds) { it.id }
entries.firstOrNull()?.let { customFieldEntry ->
return customFieldEntry.getFormattedValue(row, currency)
} }
return NULL_TEXT return NULL_TEXT
} }
@ -1086,33 +1055,6 @@ open class Session : RealmObject(), Savable, RowUpdatable, RowRepresentable, Tim
this.result?.netResult = null this.result?.netResult = null
} }
/// Stakes
// fun generateStakes() {
//
// if (this.cgAnte == null && this.cgAnte == null) {
// this.cgStakes = null
// return
// }
//
// val components = arrayListOf<String>()
//
// this.cgBlinds?.let { components.add("${cbBlinds}${it}") }
// this.cgAnte?.let { components.add("${cbAnte}${it.formatted}") }
//
// val code = this.bankroll?.currency?.code ?: UserDefaults.currency.currencyCode
// components.add("${cbCode}${code}")
//
// this.cgStakes = components.joinToString(cbSeparator)
// }
//
// fun defineHighestBet() {
// val bets = arrayListOf<Double>()
// this.cgAnte?.let { bets.add(it) }
// bets.addAll(this.blindValues)
// this.cgBiggestBet = bets.maxOrNull()
// }
private fun cleanupBlinds(blinds: String?): String? { private fun cleanupBlinds(blinds: String?): String? {
if (blinds == null) { if (blinds == null) {

@ -15,11 +15,12 @@ import net.pokeranalytics.android.model.interfaces.Identifiable
import net.pokeranalytics.android.model.interfaces.Timed import net.pokeranalytics.android.model.interfaces.Timed
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.TextFormat import net.pokeranalytics.android.util.TextFormat
import kotlin.math.min
import java.text.DateFormat import java.text.DateFormat
import java.util.* import java.util.*
import kotlin.math.max
open class SessionSet : RealmObject(), Timed, Filterable {
open class SessionSet() : RealmObject(), Timed, Filterable {
@PrimaryKey @PrimaryKey
override var id = UUID.randomUUID().toString() override var id = UUID.randomUUID().toString()
@ -64,7 +65,7 @@ open class SessionSet() : RealmObject(), Timed, Filterable {
this.ratedNet = this.sessions?.sumOf { it.computableResult?.ratedNet ?: 0.0 } ?: 0.0 this.ratedNet = this.sessions?.sumOf { it.computableResult?.ratedNet ?: 0.0 } ?: 0.0
this.estimatedHands = this.sessions?.sumOf { it.estimatedHands } ?: 0.0 this.estimatedHands = this.sessions?.sumOf { it.estimatedHands } ?: 0.0
this.bbNet = this.sessions?.sumOf { it.bbNet } ?: 0.0 this.bbNet = this.sessions?.sumOf { it.bbNet } ?: 0.0
this.breakDuration = this.sessions?.max("breakDuration")?.toLong() ?: 0L updateBreakDuration()
} }
/** /**
@ -75,7 +76,7 @@ open class SessionSet() : RealmObject(), Timed, Filterable {
var ratedNet: Double = 0.0 var ratedNet: Double = 0.0
val hourlyRate: Double private val hourlyRate: Double
get() { get() {
return this.ratedNet / this.hourlyDuration return this.ratedNet / this.hourlyDuration
} }
@ -84,7 +85,7 @@ open class SessionSet() : RealmObject(), Timed, Filterable {
var bbNet: BB = 0.0 var bbNet: BB = 0.0
val bbHourlyRate: BB private val bbHourlyRate: BB
get() { get() {
return this.bbNet / this.hourlyDuration return this.bbNet / this.hourlyDuration
} }
@ -142,5 +143,21 @@ open class SessionSet() : RealmObject(), Timed, Filterable {
@Ignore @Ignore
override val realmObjectClass: Class<out Identifiable> = SessionSet::class.java override val realmObjectClass: Class<out Identifiable> = SessionSet::class.java
private fun updateBreakDuration() {
var longestNetDuration = 0L
var maxBreakDuration = 0L
this.sessions?.let { sessions ->
for (session in sessions) {
longestNetDuration = max(longestNetDuration, session.netDuration)
maxBreakDuration = max(session.breakDuration, maxBreakDuration)
}
}
val maxSetBreak = endDate.time - startDate.time - longestNetDuration
this.breakDuration = min(maxBreakDuration, maxSetBreak)
}
} }

@ -1,288 +0,0 @@
//package net.pokeranalytics.android.model.realm
//
//import io.realm.RealmObject
//import io.realm.RealmQuery
//import io.realm.RealmResults
//import io.realm.annotations.Ignore
//import io.realm.annotations.LinkingObjects
//import net.pokeranalytics.android.exceptions.ModelException
//import timber.log.Timber
//import java.util.*
//
//open class TimeFrame : RealmObject() {
//
// // A start date
// var startDate: Date = Date()
// private set(value) {
// field = value
// this.computeNetDuration()
// }
//
// // An end date
// var endDate: Date? = null
// private set(value) {
// field = value
// this.computeNetDuration()
// }
//
// // 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.computeNetDuration()
// }
//
// // The break netDuration
// var breakDuration: Long = 0L
// set(value) {
// field = value
// this.computeNetDuration()
// }
//
// // the total netDuration
// var netDuration: Long = 0L
// private set
//
// var hourlyDuration: Double = 0.0
// get() {
// return this.netDuration / 3600000.0 // 3.6 millions of milliseconds
// }
//
// // Session
// @LinkingObjects("timeFrame")
// private val endedSessions: RealmResults<Session>? = null // we should have only one session
//
// @Ignore
// var session: Session? = null
// get() = if (this.endedSessions != null && this.endedSessions.isEmpty()) null else this.endedSessions?.first()
//
// // Group
// @LinkingObjects("timeFrame")
// private val sets: RealmResults<SessionSet>? = null // we should have only one sessionGroup
//
// @Ignore
// var set: SessionSet? = null
// get() = this.sets?.first()
//
// fun setStart(startDate: Date) {
// this.startDate = startDate
// this.session?.let {
// this.notifySessionDateChange(it)
// }
// }
//
// fun setEnd(endDate: Date?) {
// this.endDate = endDate
// this.session?.let {
// this.notifySessionDateChange(it)
// }
// }
//
// fun setDate(startDate: Date, endDate: Date?) {
// this.startDate = startDate
// this.endDate = endDate
//
// this.session?.let {
// this.notifySessionDateChange(it)
// }
// }
//
// /**
// * Computes the net netDuration of the session
// */
// private fun computeNetDuration() {
// var endDate: Date = this.endDate ?: Date()
// this.netDuration = endDate.time - this.startDate.time - this.breakDuration
// }
//
// /**
// * Queries all time frames that might be impacted by the date change
// * Makes all necessary changes to keep sequential time frames
// */
// fun notifySessionDateChange(owner: Session) {
//
// var query: RealmQuery<SessionSet> = this.realm.where(SessionSet::class.java)
// query.isNotNull("timeFrame")
//
//// Timber.d("this> sd = : ${this.startDate}, ed = ${this.endDate}")
//
// val sets = realm.where(SessionSet::class.java).findAll()
//// Timber.d("set count = ${sets.size}")
//
// if (this.endDate == null) {
// query.greaterThanOrEqualTo("timeFrame.startDate", this.startDate)
// .or()
// .greaterThanOrEqualTo("timeFrame.endDate", this.startDate)
// .or()
// .isNull("timeFrame.endDate")
// } else {
// val endDate = this.endDate!!
// query
// .lessThanOrEqualTo("timeFrame.startDate", this.startDate)
// .greaterThanOrEqualTo("timeFrame.endDate", this.startDate)
// .or()
// .lessThanOrEqualTo("timeFrame.startDate", endDate)
// .greaterThanOrEqualTo("timeFrame.endDate", endDate)
// .or()
// .greaterThanOrEqualTo("timeFrame.startDate", this.startDate)
// .lessThanOrEqualTo("timeFrame.endDate", endDate)
// .or()
// .isNull("timeFrame.endDate")
// .lessThanOrEqualTo("timeFrame.startDate", endDate)
// }
//
// val sessionGroups = query.findAll()
//
// this.updateTimeFrames(sessionGroups, owner)
//
// }
//
// /**
// * Update Time frames from sets
// */
// private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, owner: Session) {
//
// when (sessionSets.size) {
// 0 -> this.createOrUpdateSessionSet(owner)
// 1 -> this.updateSessionGroup(owner, sessionSets.first()!!)
// else -> this.mergeSessionGroups(owner, sessionSets)
// }
//
// }
//
// /**
// * Creates the session sessionGroup when the session has none
// */
// private fun createOrUpdateSessionSet(owner: Session) {
//
// val set = owner.sessionSet
// if (set != null) {
// set.timeFrame?.startDate = this.startDate
// set.timeFrame?.endDate = this.endDate
// } else {
// this.createSessionSet(owner)
// }
//
//// Timber.d("sd = : ${set.timeFrame?.startDate}, ed = ${set.timeFrame?.endDate}")
// Timber.d("netDuration 1 = : ${set?.timeFrame?.netDuration}")
//
// }
//
// fun createSessionSet(owner: Session) {
// val set: SessionSet = SessionSet.newInstanceForResult(this.realm)
// set.timeFrame?.let {
// it.startDate = this.startDate
// it.endDate = this.endDate
// } ?: run {
// throw ModelException("TimeFrame should never be null here")
// }
//
// owner.sessionSet = set
// }
//
//
// /**
// * Single SessionSet update, the session might be the owner
// * Changes the sessionGroup timeframe using the current timeframe dates
// */
// private fun updateSessionGroup(owner: Session, sessionSet: SessionSet) {
//
// var timeFrame: TimeFrame = sessionSet.timeFrame!! // tested in the query
//// timeFrame.setDate(this.startDate, this.endDate)
//
// val sisterSessions = sessionSet.endedSessions!! // shouldn't crash ever
//
// // if we have only one session in the set and that it corresponds to the set
// if (sessionSet.endedSessions?.size == 1 && sessionSet.endedSessions?.first() == owner) {
// timeFrame.setDate(this.startDate, this.endDate)
// } else { // there are 2+ endedSessions to manage and possible splits
//
// val endDate = this.endDate
//
// // case where all endedSessions are over but the set is not, we might have a split, so we delete the set and save everything again
// if (endDate != null && sisterSessions.all { it.timeFrame?.endDate != null } && timeFrame.endDate == null) {
// var endedSessions = mutableListOf<Session>(owner)
// sessionSet.endedSessions?.forEach { endedSessions.add(it) }
// sessionSet.deleteFromRealm()
// endedSessions.forEach { it.timeFrame?.notifySessionDateChange(it) }
// } else {
//
// if (this.startDate.before(timeFrame.startDate)) {
// timeFrame.startDate = this.startDate
// }
// if (endDate != null && timeFrame.endDate != null && endDate.after(timeFrame.endDate)) {
// timeFrame.endDate = endDate
// } else if (endDate == null) {
// timeFrame.endDate = null
// }
//
// owner.sessionSet = sessionSet
//
//// Timber.d("sd = : ${sessionSet.timeFrame?.startDate}, ed = ${sessionSet.timeFrame?.endDate}")
// Timber.d("netDuration 2 = : ${sessionSet.timeFrame?.netDuration}")
// }
//
// }
//
// }
//
// /**
// * Multiple session sets update:
// * Merges all sets into one (delete all then create a new one)
// */
// private fun mergeSessionGroups(owner: Session, sessionSets: RealmResults<SessionSet>) {
//
// var startDate: Date = this.startDate
// var endDate: Date? = this.endDate
//
// // find earlier and later dates from all sets
// val timeFrames = sessionSets.mapNotNull { it.timeFrame }
// timeFrames.forEach { tf ->
// if (tf.startDate.before(startDate)) {
// startDate = tf.startDate
// }
//
// endDate?.let { ed ->
// tf.endDate?.let { tfed ->
// if (tfed.after(ed)) {
// endDate = tfed
// }
// }
// } ?: run {
// endDate = tf.endDate
// }
//
// }
//
// // get all endedSessions from sets
// var endedSessions = mutableSetOf<Session>()
// sessionSets.forEach { set ->
// set.endedSessions?.asIterable()?.let { endedSessions.addAll(it) }
// }
//
// // delete all sets
// sessionSets.deleteAllFromRealm()
//
// // Create a new sets
// val set: SessionSet = SessionSet.newInstanceForResult(this.realm)
// set.timeFrame?.let {
// it.setDate(startDate, endDate)
// } ?: run {
// throw ModelException("TimeFrame should never be null here")
// }
//
// // Add the session linked to this timeframe to the new sessionGroup
// owner.sessionSet = set
//
// // Add all orphan endedSessions
// endedSessions.forEach { it.sessionSet = set }
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}")
//
// }
//
//}

@ -62,7 +62,7 @@ open class TournamentFeature : RealmObject(), RowRepresentable, RowUpdatable, Na
tag: Int tag: Int
): CharSequence { ): CharSequence {
return when (row) { return when (row) {
SimpleRow.NAME -> if (this.name.isNotEmpty()) this.name else NULL_TEXT SimpleRow.NAME -> this.name.ifEmpty { NULL_TEXT }
else -> return super.charSequenceForRow(row, context, 0) else -> return super.charSequenceForRow(row, context, 0)
} }
} }

@ -61,7 +61,7 @@ open class TournamentName : RealmObject(), NameManageable, StaticRowRepresentabl
tag: Int tag: Int
): CharSequence { ): CharSequence {
return when (row) { return when (row) {
SimpleRow.NAME -> if (this.name.isNotEmpty()) this.name else NULL_TEXT SimpleRow.NAME -> this.name.ifEmpty { NULL_TEXT }
else -> return super.charSequenceForRow(row, context,0) else -> return super.charSequenceForRow(row, context,0)
} }
} }

@ -79,7 +79,7 @@ open class Transaction : RealmObject(), RowRepresentable, RowUpdatable, Manageab
} }
// The amount of the transaction // The amount of the transaction
var ratedAmount: Double = 0.0 private var ratedAmount: Double = 0.0
// The date of the transaction // The date of the transaction
override var date: Date = Date() override var date: Date = Date()

@ -5,13 +5,26 @@ import io.realm.RealmObject
import io.realm.annotations.PrimaryKey import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.util.UUID_SEPARATOR import net.pokeranalytics.android.util.UUID_SEPARATOR
import net.pokeranalytics.android.util.extensions.findById import net.pokeranalytics.android.util.extensions.findById
import timber.log.Timber
import java.util.* import java.util.*
open class UserConfig : RealmObject() { open class UserConfig : RealmObject() {
companion object { companion object {
fun getConfiguration(realm: Realm): UserConfig { fun getConfiguration(realm: Realm?, handler: (UserConfig) -> (Unit)) {
if (realm != null) {
handler(userConfiguration(realm))
} else {
val r = Realm.getDefaultInstance()
handler(userConfiguration(r))
r.close()
}
}
private fun userConfiguration(realm: Realm): UserConfig {
realm.where(UserConfig::class.java).findFirst()?.let { config -> realm.where(UserConfig::class.java).findFirst()?.let { config ->
return config return config
} }

@ -313,13 +313,7 @@ open class HandHistory : RealmObject(), Deletable, RowRepresentable, Filterable,
* Creates and affect a PlayerSetup at the given [positionIndex] * Creates and affect a PlayerSetup at the given [positionIndex]
*/ */
fun createPlayerSetup(positionIndex: Int): PlayerSetup { fun createPlayerSetup(positionIndex: Int): PlayerSetup {
val playerSetup = PlayerSetup()
val playerSetup = if (this.realm != null) {
this.realm.createObject(PlayerSetup::class.java) }
else {
PlayerSetup()
}
playerSetup.position = positionIndex playerSetup.position = positionIndex
this.playerSetups.add(playerSetup) this.playerSetups.add(playerSetup)
return playerSetup return playerSetup
@ -562,7 +556,7 @@ open class HandHistory : RealmObject(), Deletable, RowRepresentable, Filterable,
this.winnerPots.clear() this.winnerPots.clear()
this.winnerPots.addAll(wonPots) this.winnerPots.addAll(wonPots)
Timber.d("Pot won: ${this.winnerPots.size} for positions: ${this.winnerPots.map {it.position}} ") Timber.d("Pot won: ${this.winnerPots.size} for positions: ${this.winnerPots.map { it.position }} ")
} }
/*** /***

@ -73,15 +73,21 @@ class FavoriteSessionFinder {
/** /**
* Copies the favorite session parameters on the [session] * Copies the favorite session parameters on the [session]
*/ */
fun copyParametersFromFavoriteSession(session: Session, location: Location?, context: Context) { fun copyParametersFromFavoriteSession(realm: Realm, session: Session, location: Location?, context: Context) {
val favoriteSession = favoriteSession(session.type, location, session.realm, context) val favoriteSession = favoriteSession(session.type, location, realm, context)
favoriteSession?.let { fav -> favoriteSession?.let { fav ->
session.limit = fav.limit session.limit = fav.limit
session.game = fav.game
session.bankroll = fav.bankroll fav.game?.let {
session.game = realm.copyFromRealm(it)
}
fav.bankroll?.let {
session.bankroll = realm.copyFromRealm(it)
}
session.tableSize = fav.tableSize session.tableSize = fav.tableSize
when (session.type) { when (session.type) {

@ -1,209 +0,0 @@
package net.pokeranalytics.android.model.utils
import io.realm.RealmQuery
import io.realm.RealmResults
import net.pokeranalytics.android.exceptions.ModelException
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.SessionSet
import kotlin.math.max
class CorruptSessionSetException(message: String) : Exception(message)
/**
* The manager is in charge of updating the abstract concept of timeline,
* representing the sequenced time frames where the user plays.
*/
class SessionSetManager {
companion object {
/**
* Updates the global timeline using the updated [session]
*/
fun updateTimeline(session: Session) {
if (!session.realm.isInTransaction) {
throw PAIllegalStateException("realm should be in transaction at this point")
}
if (session.startDate == null) {
throw ModelException("Start date should never be null here")
}
if (session.endDate == null) {
throw ModelException("End date should never be null here")
}
val sessionSets = this.matchingSets(session)
cleanupSessionSets(session, sessionSets)
}
private fun matchingSets(session: Session) : RealmResults<SessionSet> {
val realm = session.realm
val endDate = session.endDate!! // tested above
val startDate = session.startDate!!
val query: RealmQuery<SessionSet> = realm.where(SessionSet::class.java)
query
.lessThanOrEqualTo("startDate", startDate)
.greaterThanOrEqualTo("endDate", startDate)
.or()
.lessThanOrEqualTo("startDate", endDate)
.greaterThanOrEqualTo("endDate", endDate)
.or()
.greaterThanOrEqualTo("startDate", startDate)
.lessThanOrEqualTo("endDate", endDate)
return query.findAll()
}
/**
* Multiple session sets update:
* Merges or splits session sets
* Does that by deleting then recreating
*/
private fun cleanupSessionSets(session: Session, sessionSets: RealmResults<SessionSet>) {
// get all endedSessions from sets
val allImpactedSessions = mutableSetOf<Session>()
sessionSets.forEach { set ->
set.sessions?.asIterable()?.let { allImpactedSessions.addAll(it) }
}
allImpactedSessions.add(session)
// delete all sets
sessionSets.deleteAllFromRealm()
allImpactedSessions.forEach { impactedSession ->
val sets = matchingSets(impactedSession)
this.updateTimeFrames(sets, impactedSession)
}
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}")
}
/**
* Update the global timeline using the impacted [sessionSets] and the updated [session]
*/
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, session: Session) {
when (sessionSets.size) {
0 -> this.createOrUpdateSessionSet(session)
else -> this.mergeSessionGroups(session, sessionSets)
}
}
/**
* Creates or update the session set for the [session]
*/
private fun createOrUpdateSessionSet(session: Session) {
val set = session.sessionSet
if (set != null) {
set.startDate = session.startDate!! // tested above
set.endDate = session.endDate!!
} else {
this.createSessionSet(session)
}
}
/**
* Create a set and affect it to the [session]
*/
private fun createSessionSet(session: Session) {
val set: SessionSet = SessionSet.newInstance(session.realm)
set.startDate = session.startDate!!
set.endDate = session.endDate!!
set.breakDuration = session.breakDuration
session.sessionSet = set
set.computeStats()
}
/**
* Multiple session sets update:
* Merges all sets into one (delete all then create a new one)
*/
private fun mergeSessionGroups(session: Session, sessionSets: RealmResults<SessionSet>) {
var startDate = session.startDate!!
var endDate = session.endDate!!
// get all endedSessions from sets
val sessions = mutableSetOf<Session>()
sessionSets.forEach { set ->
set.sessions?.asIterable()?.let { sessions.addAll(it) }
}
// find earlier and later dates from all sets
sessions.forEach { s ->
if (s.startDate != null && s.endDate != null) {
val start = s.startDate!!
val end = s.endDate!!
if (start.before(startDate)) {
startDate = start
}
if (end.after(endDate)) {
endDate = end
}
} else {
throw CorruptSessionSetException("Set contains unfinished sessions!")
}
}
// delete all sets
sessionSets.deleteAllFromRealm()
// Create a new set
val set: SessionSet = SessionSet.newInstance(session.realm)
set.startDate = startDate
set.endDate = endDate
// Add the session linked to this timeframe to the new sessionGroup
session.sessionSet = set
// Add all orphan endedSessions
sessions.forEach { s ->
s.sessionSet = set
set.breakDuration = max(set.breakDuration, s.breakDuration)
}
set.computeStats()
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}")
}
/**
* Removes the [session] from the timeline
*/
fun removeFromTimeline(session: Session) {
if (!session.realm.isInTransaction) {
throw PAIllegalStateException("realm should be in transaction at this point")
}
val sessionSet = session.sessionSet
if (sessionSet != null) {
val sessions = mutableSetOf<Session>()
sessionSet.sessions?.asIterable()?.let { sessions.addAll(it) }
sessions.remove(session)
sessionSet.deleteFromRealm()
sessions.forEach {
updateTimeline(it)
}
}
}
}
}

@ -0,0 +1,534 @@
package net.pokeranalytics.android.model.utils
import io.realm.Realm
import io.realm.RealmModel
import io.realm.RealmQuery
import io.realm.RealmResults
import net.pokeranalytics.android.exceptions.ModelException
import net.pokeranalytics.android.model.realm.FlatTimeInterval
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.SessionSet
import net.pokeranalytics.android.util.extensions.findById
import net.pokeranalytics.android.util.extensions.max
import net.pokeranalytics.android.util.extensions.min
import timber.log.Timber
import java.util.*
class CorruptSessionSetException(message: String) : Exception(message)
/**
* The TimeManager pre-computes time related data:
* - SessionSet: All overlapping sessions are grouped into a SessionSet,
* used to calculate the number of sessions and break durations
* - FlatTimeInterval: Sessions time intervals are breaked down into smaller intervals
* when overlapping occurs to get faster duration calculations
*/
object TimeManager {
var sessions: RealmResults<Session>? = null
private val sessionIdsToProcess = mutableSetOf<String>()
private var start: Date? = null
private var end: Date? = null
fun configure() {} // launch init
fun startChanged(session: Session, date: Date?) {
this.start = min(this.start, date)
this.end = max(this.end, session.endDate)
this.sessionIdsToProcess.add(session.id)
}
fun endChanged(session: Session, date: Date?) {
this.end = max(this.end, date)
this.start = min(this.start, session.startDate)
this.sessionIdsToProcess.add(session.id)
}
fun sessionDateChanged(session: Session) {
this.start = min(this.start, session.startDate)
this.end = max(this.end, session.endDate)
this.sessionIdsToProcess.add(session.id)
}
init {
val realm = Realm.getDefaultInstance()
sessions = realm.where(Session::class.java).findAllAsync()
sessions?.addChangeListener { _, _ ->
if (sessionIdsToProcess.isNotEmpty()) {
realm.executeTransactionAsync({ asyncRealm ->
val sessions = sessionIdsToProcess.mapNotNull { asyncRealm.findById<Session>(it) }
sessionIdsToProcess.clear()
for (session in sessions) {
Timber.d("Session id = ${session.id}")
Timber.d("Session time intervals count = ${session.flatTimeIntervals.size}")
session.flatTimeIntervals.deleteAllFromRealm()
val fti = FlatTimeInterval()
session.flatTimeIntervals.add(fti)
asyncRealm.insertOrUpdate(session)
}
}, {
Timber.d("executeTransactionAsync onSuccess listener...")
val timeIntervals = realm.where(FlatTimeInterval::class.java).findAll()
Timber.d("Total timeIntervals count = ${timeIntervals.size}")
timeIntervals.forEach {
Timber.d(">>> Time interval session count = ${it.sessions?.size}, session id = ${it.sessions?.firstOrNull()?.id}")
}
}, {})
}
}
// sessions?.addChangeListener { _, _ ->
//
// Timber.d("...sessions change at ${Date().time}")
//
// val start = this.start
// val end = this.end
// if (start != null && end != null) {
//
// Timber.d("...process date changes from $start to $end")
//
// this.start = null
// this.end = null
//
// realm.executeTransactionAsync ({ asyncRealm ->
// processSessions(asyncRealm, start, end)
// cleanUp()
// }, {
// Timber.d(">>>>> ON SUCCESS")
//
// realm.where(FlatTimeInterval::class.java).findAll().forEach {
// Timber.d("######## sessions count = ${it.sessions?.size}")
// }
//
// }, {
// Timber.d("Transaction failed : $it")
// })
// }
// }
realm.close()
}
private fun cleanUp() {
this.start = null
this.end = null
this.sessionIdsToProcess.clear()
}
private fun processSessions(realm: Realm, start: Date, end: Date) {
Timber.d("***** processSessions, process count = ${sessionIdsToProcess.size}")
// val start = this.start
// val end = this.end
val sessions = sessionIdsToProcess.mapNotNull { realm.findById<Session>(it) }
for (session in sessions) {
// Session Sets
val startDate = session.startDate
val endDate = session.endDate
if (startDate != null && endDate != null) {
updateTimeline(session)
} else if (session.sessionSet != null) {
removeFromTimeline(session)
}
}
// FlatTimeIntervals
processFlatTimeInterval(realm, sessions.toSet(), start, end)
val ftis = realm.where(FlatTimeInterval::class.java).findAll()
Timber.d("*** FTIs count = ${ftis.size}")
}
/**
* Updates the global timeline using the updated [session]
*/
fun updateTimeline(session: Session) {
// if (!session.realm.isInTransaction) {
// throw PAIllegalStateException("realm should be in transaction at this point")
// }
if (session.startDate == null) {
throw ModelException("Start date should never be null here")
}
if (session.endDate == null) {
throw ModelException("End date should never be null here")
}
val start = session.startDate!!
val end = session.endDate!!
val sessionSets = this.matchingData<SessionSet>(session.realm, start, end)
cleanupSessionSets(session, sessionSets)
}
// private fun matchingSets(session: Session): RealmResults<SessionSet> {
// val realm = session.realm
// val endDate = session.endDate!! // tested above
// val startDate = session.startDate!!
//
// val query: RealmQuery<SessionSet> = realm.where(SessionSet::class.java)
//
// query
// .lessThanOrEqualTo("startDate", startDate)
// .greaterThanOrEqualTo("endDate", startDate)
// .or()
// .lessThanOrEqualTo("startDate", endDate)
// .greaterThanOrEqualTo("endDate", endDate)
// .or()
// .greaterThanOrEqualTo("startDate", startDate)
// .lessThanOrEqualTo("endDate", endDate)
//
// return query.findAll()
// }
private inline fun <reified T : RealmModel> matchingData(realm: Realm, startDate: Date, endDate: Date): RealmResults<T> {
val query: RealmQuery<T> = realm.where(T::class.java)
query
.lessThanOrEqualTo("startDate", startDate)
.greaterThanOrEqualTo("endDate", startDate)
.or()
.lessThanOrEqualTo("startDate", endDate)
.greaterThanOrEqualTo("endDate", endDate)
.or()
.greaterThanOrEqualTo("startDate", startDate)
.lessThanOrEqualTo("endDate", endDate)
return query.findAll()
}
/**
* Multiple session sets update:
* Merges or splits session sets
* Does that by deleting then recreating
*/
private fun cleanupSessionSets(session: Session, sessionSets: RealmResults<SessionSet>) {
// get all endedSessions from sets
val allImpactedSessions = mutableSetOf<Session>()
sessionSets.forEach { set ->
set.sessions?.asIterable()?.let { allImpactedSessions.addAll(it) }
}
allImpactedSessions.add(session)
// delete all sets
sessionSets.deleteAllFromRealm()
allImpactedSessions.forEach { impactedSession ->
val sets = matchingData<SessionSet>(impactedSession.realm, impactedSession.startDate!!, impactedSession.endDate!!)
this.updateTimeFrames(sets, impactedSession)
}
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}")
}
/**
* Update the global timeline using the impacted [sessionSets] and the updated [session]
*/
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, session: Session) {
when (sessionSets.size) {
0 -> this.createOrUpdateSessionSet(session)
else -> this.mergeSessionGroups(session, sessionSets)
}
}
/**
* Creates or update the session set for the [session]
*/
private fun createOrUpdateSessionSet(session: Session) {
val set = session.sessionSet
if (set != null) {
set.startDate = session.startDate!! // tested above
set.endDate = session.endDate!!
} else {
this.createSessionSet(session)
}
}
/**
* Create a set and affect it to the [session]
*/
private fun createSessionSet(session: Session) {
val set = SessionSet.newInstance(session.realm)
set.startDate = session.startDate!!
set.endDate = session.endDate!!
set.breakDuration = session.breakDuration
session.sessionSet = set
set.computeStats()
}
/**
* Multiple session sets update:
* Merges all sets into one (delete all then create a new one)
*/
private fun mergeSessionGroups(session: Session, sessionSets: RealmResults<SessionSet>) {
var startDate = session.startDate!!
var endDate = session.endDate!!
// get all endedSessions from sets
val sessions = mutableSetOf<Session>()
sessionSets.forEach { set ->
set.sessions?.asIterable()?.let { sessions.addAll(it) }
}
// find earlier and later dates from all sets
sessions.forEach { s ->
if (s.startDate != null && s.endDate != null) {
val start = s.startDate!!
val end = s.endDate!!
if (start.before(startDate)) {
startDate = start
}
if (end.after(endDate)) {
endDate = end
}
} else {
throw CorruptSessionSetException("Set contains unfinished sessions!")
}
}
// delete all sets
sessionSets.deleteAllFromRealm()
// Create a new set
val set = SessionSet.newInstance(session.realm)
set.startDate = startDate
set.endDate = endDate
// Add the session linked to this timeframe to the new sessionGroup
session.sessionSet = set
// Add all orphan endedSessions
sessions.forEach { s ->
s.sessionSet = set
}
set.computeStats()
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}")
}
/**
* Removes the [session] from the timeline
*/
fun removeFromTimeline(session: Session) {
// if (!session.realm.isInTransaction) {
// throw PAIllegalStateException("realm should be in transaction at this point")
// }
val sessionSet = session.sessionSet
if (sessionSet != null) {
val sessions = mutableSetOf<Session>()
sessionSet.sessions?.asIterable()?.let { sessions.addAll(it) }
sessions.remove(session)
sessionSet.deleteFromRealm()
sessions.forEach {
updateTimeline(it)
}
}
}
private fun processFlatTimeInterval(realm: Realm, changedSessions: Set<Session>, start: Date, end: Date) {
// Timber.d("***************************************************")
// Timber.d("*** processFlatTimeInterval, from: $start, to $end")
// Timber.d("***************************************************")
val sessions = matchingData<Session>(realm, start, end)
val intervalsStore = IntervalsStore(sessions.toSet())
intervalsStore.processSessions(changedSessions)
Timber.d("*** sessions count = ${intervalsStore.sessions.size}")
Timber.d("*** ftis to delete: ${intervalsStore.intervals.size}")
for (fti in intervalsStore.intervals) {
fti.deleteFromRealm()
}
// intervalsStore.intervals.forEach { it.deleteFromRealm() }
val intervals = SessionInterval.intervalMap(intervalsStore.sessions)
for (interval in intervals) {
val sortedDates = interval.dates.sorted()
for (i in (0 until sortedDates.size - 1)) {
val s = sortedDates[i]
val e = sortedDates[i + 1]
val matchingSessions = interval.sessions.filter {
val sd = it.startDate
val ed = it.endDate
(sd != null && ed != null && sd <= s && ed >= e)
}
if (matchingSessions.isNotEmpty()) {
// Timber.d("**** Create FTI: $s - $e")
val fti = FlatTimeInterval()
fti.startDate = s
fti.endDate = e
for (session in matchingSessions) {
session.flatTimeIntervals.add(fti)
realm.insertOrUpdate(session)
}
realm.insertOrUpdate(fti)
} else {
Timber.w("The FTI has no sessions")
}
}
}
sessions.forEach {
Timber.d("ending process...session FTI count = ${it.flatTimeIntervals.size}")
}
}
}
class IntervalsStore(sessionSet: Set<Session>) {
var start: Date = Date()
var end: Date = Date(0L)
val intervals = mutableSetOf<FlatTimeInterval>()
val sessions = mutableSetOf<Session>()
private val sessionIds: MutableSet<String> = mutableSetOf()
init {
processSessions(sessionSet)
}
fun processSessions(sessions: Set<Session>) {
this.sessions.addAll(sessions)
for (session in sessions) {
// Timber.d("PROCESS > s = ${session.startDate} / e = ${session.endDate} ")
loadIntervals(session)
}
}
private fun loadIntervals(session: Session) {
if (sessionIds.contains(session.id)) {
return
}
session.startDate?.let { this.start = min(this.start, it) }
session.endDate?.let { this.end = max(this.end, it) }
this.sessionIds.add(session.id)
Timber.d("session FTI count = ${session.flatTimeIntervals.size}")
for (fti in session.flatTimeIntervals) {
this.intervals.add(fti)
fti.sessions?.let { sessions ->
processSessions(sessions.toSet())
}
}
}
}
class SessionInterval(session: Session) {
var start: Date
var end: Date?
var sessions: MutableSet<Session> = mutableSetOf()
val dates: MutableSet<Date> = mutableSetOf()
val duration: Long
get() {
val endDate = end ?: Date()
return endDate.time - start.time
}
init {
this.start = session.startDate!!
this.end = session.endDate
// Timber.d("INTERVAL init: s = $start, e = $end")
this.addSession(session)
}
private fun addSession(session: Session) {
this.sessions.add(session)
session.startDate?.let { this.dates.add(it) }
session.endDate?.let { endDate ->
this.dates.add(endDate)
if (endDate > end) {
end = endDate
}
}
}
companion object {
fun intervalMap(sessions: Set<Session>): List<SessionInterval> {
val sorted = sessions.sortedBy { it.startDate }
val intervals = mutableListOf<SessionInterval>()
sorted.firstOrNull()?.let { firstSession ->
var currentInterval = SessionInterval(firstSession)
intervals.add(currentInterval)
val remainingSessions = sorted.drop(1)
for (session in remainingSessions) {
val start = session.startDate!!
val currentEnd = currentInterval.end
if (currentEnd != null && start > currentEnd) {
val interval = SessionInterval(session)
currentInterval = interval
intervals.add(interval)
} else {
currentInterval.addSession(session)
}
}
}
// intervals.forEach {
// Timber.d("s = ${it.start}, e = ${it.end}")
// }
return intervals
}
}
}

@ -113,11 +113,11 @@ class HomeActivity : BaseActivity(), NewPerformanceListener {
val realm = getRealm() val realm = getRealm()
// observe currency changes // observe currency changes
this.currencies = realm.where(Currency::class.java).findAll() this.currencies = realm.where(Currency::class.java).findAllAsync()
this.currencies.addChangeListener { currencies, _ -> this.currencies.addChangeListener { _, _ ->
realm.executeTransaction { getRealm().executeTransactionAsync { asyncRealm ->
currencies.forEach { asyncRealm.where(Currency::class.java).findAll().forEach { currency ->
it.refreshRelatedRatedValues() currency.refreshRelatedRatedValues()
} }
} }
} }

@ -34,6 +34,7 @@ abstract class BaseActivity : AppCompatActivity() {
} }
private var realm: Realm? = null private var realm: Realm? = null
private var permissionCallback: ((granted: Boolean) -> Unit)? = null private var permissionCallback: ((granted: Boolean) -> Unit)? = null
private var permissionRequest: PermissionRequest? = null private var permissionRequest: PermissionRequest? = null
@ -86,7 +87,7 @@ abstract class BaseActivity : AppCompatActivity() {
override fun onBackPressed() { override fun onBackPressed() {
super.onBackPressed() super.onBackPressed()
AppReviewManager.showReviewManager(this) AppReviewManager.showReviewManagerIfNecessary(this)
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
@ -129,11 +130,19 @@ abstract class BaseActivity : AppCompatActivity() {
fragmentTransaction.commit() fragmentTransaction.commit()
} }
fun addFragmentWithBackStack(fragment: Fragment, containerId: Int) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(containerId, fragment)
fragmentTransaction.addToBackStack(fragment.javaClass.toString())
fragmentTransaction.commit()
}
/** /**
* Return the realm instance * Return the realm instance
*/ */
fun getRealm(): Realm { fun getRealm(): Realm {
// return this.realm
this.realm?.let { this.realm?.let {
return it return it
} ?: run { } ?: run {
@ -143,6 +152,23 @@ abstract class BaseActivity : AppCompatActivity() {
} }
} }
fun executeRealmAsyncTransaction(handler: (Realm) -> (Unit)) {
this.paApplication.executeRealmAsyncTransaction(handler)
// Timber.d(">>>> Launch async transaction")
//
// this.realm.executeTransactionAsync({ asyncRealm ->
// handler(asyncRealm)
// }, {
// Timber.d("YEAAAAAAAAAAAH !!!")
// this.realm.refresh()
// }, {
// Timber.d("NOOOOO error = $it")
// })
}
/** /**
* Return if the location permission has been granted by the user * Return if the location permission has been granted by the user
*/ */

@ -9,6 +9,7 @@ import android.provider.MediaStore
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -52,11 +53,11 @@ open class MediaActivity : BaseActivity() {
val filesList = ArrayList<File>() val filesList = ArrayList<File>()
GlobalScope.launch { CoroutineScope(Dispatchers.Default).launch {
if (tempFile != null) { if (tempFile != null) {
tempFile?.let { tempFile?.let {
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
filesList.add(it) filesList.add(it)
getPictures(filesList) getPictures(filesList)
} }
@ -65,7 +66,7 @@ open class MediaActivity : BaseActivity() {
data.clipData?.let { clipData -> data.clipData?.let { clipData ->
try { try {
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
isLoadingNewPictures() isLoadingNewPictures()
} }
@ -78,7 +79,7 @@ open class MediaActivity : BaseActivity() {
filesList.add(photoFile) filesList.add(photoFile)
} }
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
getPictures(filesList) getPictures(filesList)
} }
@ -90,7 +91,7 @@ open class MediaActivity : BaseActivity() {
data.data?.let { uri -> data.data?.let { uri ->
try { try {
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
isLoadingNewPictures() isLoadingNewPictures()
} }
@ -98,7 +99,7 @@ open class MediaActivity : BaseActivity() {
val photoFile = ImageUtils.createTempImageFile(this@MediaActivity) val photoFile = ImageUtils.createTempImageFile(this@MediaActivity)
ImageUtils.copyInputStreamToFile(inputStream!!, photoFile) ImageUtils.copyInputStreamToFile(inputStream!!, photoFile)
filesList.add(photoFile) filesList.add(photoFile)
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
getPictures(filesList) getPictures(filesList)
} }

@ -1,15 +1,15 @@
package net.pokeranalytics.android.ui.fragment package net.pokeranalytics.android.ui.fragment
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope import net.pokeranalytics.android.AppState
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.databinding.FragmentImportBinding import net.pokeranalytics.android.databinding.FragmentImportBinding
import net.pokeranalytics.android.ui.fragment.components.RealmFragment import net.pokeranalytics.android.ui.fragment.components.RealmFragment
@ -20,6 +20,11 @@ import java.io.InputStream
import java.text.NumberFormat import java.text.NumberFormat
import java.util.* import java.util.*
enum class ImportBroadcast(var identifier: String) {
START("start-import"),
END("end-import")
}
class ImportFragment : RealmFragment(), ImportDelegate { class ImportFragment : RealmFragment(), ImportDelegate {
private lateinit var filePath: String private lateinit var filePath: String
@ -84,14 +89,17 @@ class ImportFragment : RealmFragment(), ImportDelegate {
private fun startImport() { private fun startImport() {
this.parentActivity?.paApplication?.reportWhistleBlower?.pause() AppState.isImporting = true
LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(Intent(ImportBroadcast.START.identifier))
// this.parentActivity?.paApplication?.reportWhistleBlower?.pause()
this.importer = CSVImporter(inputStream) this.importer = CSVImporter(inputStream)
this.importer.delegate = this this.importer.delegate = this
CoroutineScope(coroutineContext).launch { CoroutineScope(Dispatchers.Main).launch {
val coroutine = GlobalScope.async { val coroutine = CoroutineScope(Dispatchers.Default).async {
val s = Date() val s = Date()
Timber.d(">>> Start Import...") Timber.d(">>> Start Import...")
@ -116,15 +124,6 @@ class ImportFragment : RealmFragment(), ImportDelegate {
snackBar.show() snackBar.show()
} }
// if (shouldDismissActivity) {
//
// activity?.let {
// it.setResult(ResultCode.IMPORT_UNRECOGNIZED_FORMAT.value)
// it.finish()
// }
//
// } else {
// }
importDidFinish() importDidFinish()
} }
@ -140,7 +139,10 @@ class ImportFragment : RealmFragment(), ImportDelegate {
} }
private fun end() { private fun end() {
this.parentActivity?.paApplication?.reportWhistleBlower?.resume()
AppState.isImporting = false
LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(Intent(ImportBroadcast.END.identifier))
// this.parentActivity?.paApplication?.reportWhistleBlower?.resume()
activity?.finish() activity?.finish()
} }

@ -118,11 +118,6 @@ class ReportCreationFragment : RealmFragment(), RowRepresentableDataSource, RowR
if (this.assistant.step == Assistant.Step.FINALIZE) { if (this.assistant.step == Assistant.Step.FINALIZE) {
// getRealm().executeTransaction {
// val rs = this.assistant.options.reportSetup("test")
// it.insert(rs)
// }
// launch report // launch report
this.finishActivityWithOptions(this.assistant.options, this.assistant.reportDisplay) this.finishActivityWithOptions(this.assistant.options, this.assistant.reportDisplay)

@ -41,7 +41,6 @@ import net.pokeranalytics.android.ui.view.rows.StaticReport
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import timber.log.Timber import timber.log.Timber
import java.util.*
data class ReportSection(val report: StaticReport, var performances: MutableList<PerformanceRow>) { data class ReportSection(val report: StaticReport, var performances: MutableList<PerformanceRow>) {
@ -60,10 +59,7 @@ data class ReportSection(val report: StaticReport, var performances: MutableList
} }
data class PerformanceRow(val performance: Performance, val report: StaticReport): RowRepresentable { data class PerformanceRow(val performance: Performance, val report: StaticReport): RowRepresentable {
override val resId: Int? = this.performance.resId override val resId: Int? = this.performance.resId
override val viewType: Int = RowViewType.TITLE_BADGE_VALUE.identifier override val viewType: Int = RowViewType.TITLE_BADGE_VALUE.identifier
} }
@ -71,6 +67,7 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
private lateinit var reportSetups: RealmResults<ReportSetup> private lateinit var reportSetups: RealmResults<ReportSetup>
private lateinit var performances: RealmResults<Performance> private lateinit var performances: RealmResults<Performance>
private var adapterRows = mutableListOf<RowRepresentable>() private var adapterRows = mutableListOf<RowRepresentable>()
override fun deletableItems(): List<Deletable> { override fun deletableItems(): List<Deletable> {
@ -131,7 +128,7 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
val itemToDeleteId = data?.getStringExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName) val itemToDeleteId = data?.getStringExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName)
itemToDeleteId?.let { id -> itemToDeleteId?.let { id ->
CoroutineScope(coroutineContext).launch { CoroutineScope(Dispatchers.Default).launch {
delay(300) delay(300)
deleteItem(dataListAdapter, reportSetups, id) deleteItem(dataListAdapter, reportSetups, id)
} }
@ -152,17 +149,23 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
* Init data * Init data
*/ */
private fun initData() { private fun initData() {
this.reportSetups = getRealm().where(ReportSetup::class.java).findAll().sort("name")
this.reportSetups = getRealm().where(ReportSetup::class.java).sort("name").findAllAsync()
this.performances = getRealm().where(Performance::class.java).findAllAsync()
this.reportSetups.addChangeListener { _, _ -> this.reportSetups.addChangeListener { _, _ ->
if (isAdded) {
this.updateRows() this.updateRows()
} }
}
this.performances = getRealm().where(Performance::class.java).findAll()
this.performances.addChangeListener { _, _ -> this.performances.addChangeListener { _, _ ->
if (isAdded) {
this.updateRows() this.updateRows()
} }
} }
}
/** /**
* Init UI * Init UI
*/ */
@ -291,7 +294,7 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
*/ */
private fun launchComputation(criteriaList: List<Criteria>, reportName: String, stat: Stat) { private fun launchComputation(criteriaList: List<Criteria>, reportName: String, stat: Stat) {
if (criteriaList.combined().size < 2) { if (criteriaList.combined(getRealm()).size < 2) {
Toast.makeText(context, R.string.less_then_2_values_for_display, Toast.LENGTH_LONG).show() Toast.makeText(context, R.string.less_then_2_values_for_display, Toast.LENGTH_LONG).show()
return return
} }
@ -311,19 +314,17 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
*/ */
private fun launchReportWithOptions(options: Calculator.Options, reportDisplay: ReportDisplay, reportName: String) { private fun launchReportWithOptions(options: Calculator.Options, reportDisplay: ReportDisplay, reportName: String) {
Timber.d("launchReportWithOptions")
showLoader() showLoader()
CoroutineScope(coroutineContext).launch { CoroutineScope(Dispatchers.Default).launch {
val startDate = Date()
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.refresh() realm.refresh()
val report = Calculator.computeStats(realm, options = options) val report = Calculator.computeStats(realm, options = options)
Timber.d("launchComputation: ${System.currentTimeMillis() - startDate.time}ms") CoroutineScope(Dispatchers.Main).launch {
launch(Dispatchers.Main) {
if (!isDetached) { if (!isDetached) {
hideLoader() hideLoader()
ReportActivity.newInstanceForResult(this@ReportsFragment, report, reportDisplay, reportName) ReportActivity.newInstanceForResult(this@ReportsFragment, report, reportDisplay, reportName)

@ -185,12 +185,12 @@ class SettingsFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRep
Preferences.setCurrencyCode(currencyCode, requireContext()) Preferences.setCurrencyCode(currencyCode, requireContext())
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { executeRealmAsyncTransaction { execRealm->
realm.where(Currency::class.java).findAll().forEach { currency -> execRealm.where(Currency::class.java).findAll().forEach { currency ->
currency.rate = (currency.rate ?: 1.0) * rate currency.rate = (currency.rate ?: 1.0) * rate
} }
realm.where(Session::class.java).findAll().forEach { session -> execRealm.where(Session::class.java).findAll().forEach { session ->
session.bankrollHasBeenUpdated() session.bankrollHasBeenUpdated()
} }
} }

@ -8,8 +8,9 @@ import android.os.Bundle
import android.view.* import android.view.*
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import io.realm.Realm import io.realm.Realm
import io.realm.RealmModel
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
@ -76,6 +77,7 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
addRealmChangeListener(this, UserConfig::class.java) addRealmChangeListener(this, UserConfig::class.java)
addRealmChangeListener(this, ComputableResult::class.java) addRealmChangeListener(this, ComputableResult::class.java)
addRealmChangeListener(this, Transaction::class.java) addRealmChangeListener(this, Transaction::class.java)
addRealmChangeListener(this, SessionSet::class.java)
} }
private fun initUI() { private fun initUI() {
@ -99,13 +101,14 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
private fun setTransactionFilterItemColor() { private fun setTransactionFilterItemColor() {
context?.let { context?.let {
val userConfig = UserConfig.getConfiguration(getRealm()) UserConfig.getConfiguration(getRealm()) { userConfig ->
val color = if (userConfig.transactionTypeIds.isNotEmpty()) R.color.red else R.color.white val color = if (userConfig.transactionTypeIds.isNotEmpty()) R.color.red else R.color.white
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.transactionFilterMenuItem?.iconTintList = ColorStateList.valueOf(it.getColor(color)) this.transactionFilterMenuItem?.iconTintList = ColorStateList.valueOf(it.getColor(color))
} }
} }
} }
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
@ -146,11 +149,13 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
// Business // Business
override fun asyncListenedEntityChange(realm: Realm) { override fun asyncListenedEntityChange(realm: Realm, clazz: Class<out RealmModel>) {
if (isAdded) { // Fixes: java.lang.IllegalStateException Fragment StatisticsFragment{9d3e5ec} not attached to a context. if (isAdded) { // Fixes: java.lang.IllegalStateException Fragment StatisticsFragment{9d3e5ec} not attached to a context.
launchStatComputation() launchStatComputation()
setTransactionFilterItemColor() setTransactionFilterItemColor()
} }
} }
/** /**
@ -158,11 +163,11 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
*/ */
private fun launchStatComputation() { private fun launchStatComputation() {
CoroutineScope(coroutineContext).launch { CoroutineScope(Dispatchers.Default).launch {
val async = GlobalScope.async { val async = async {
val s = Date() val s = Date()
Timber.d(">>> start...") // Timber.d(">>> start...")
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.refresh() realm.refresh()
@ -174,22 +179,26 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
val e = Date() val e = Date()
val duration = (e.time - s.time) / 1000.0 val duration = (e.time - s.time) / 1000.0
Timber.d(">>> ended in $duration seconds") // Timber.d(">>> computations took $duration seconds")
} }
async.await() async.await()
launch(Dispatchers.Main) {
if (isAdded && !isDetached) { if (isAdded && !isDetached) {
tableReportFragment.showResults() tableReportFragment.showResults()
} }
} }
} }
}
/** /**
* Create session groups and start computations * Create session groups and start computations
*/ */
private fun createSessionGroupsAndStartCompute(realm: Realm): Report { private fun createSessionGroupsAndStartCompute(realm: Realm): Report {
// Timber.d(">>> Launch statistics computations")
val filter: Filter? = this.currentFilter(this.requireContext(), realm)?.let { val filter: Filter? = this.currentFilter(this.requireContext(), realm)?.let {
if (it.filterableType == currentFilterable) { it } else { null } if (it.filterableType == currentFilterable) { it } else { null }
} }
@ -201,6 +210,7 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
Stat.NUMBER_OF_SETS, Stat.NUMBER_OF_SETS,
Stat.AVERAGE_HOURLY_DURATION, Stat.AVERAGE_HOURLY_DURATION,
Stat.HOURLY_DURATION, Stat.HOURLY_DURATION,
Stat.FTI_COUNT,
Stat.HANDS_PLAYED Stat.HANDS_PLAYED
) )
@ -238,8 +248,6 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
val tSessionGroup = ComputableGroup(Query(QueryCondition.IsTournament).merge(query), tStats) val tSessionGroup = ComputableGroup(Query(QueryCondition.IsTournament).merge(query), tStats)
Timber.d(">>>>> Start computations...")
val options = Calculator.Options() val options = Calculator.Options()
val computedStats = mutableListOf<Stat>() val computedStats = mutableListOf<Stat>()
computedStats.addAll(allStats) computedStats.addAll(allStats)
@ -247,7 +255,9 @@ class StatisticsFragment : FilterableFragment(), RealmAsyncListener {
computedStats.addAll(tStats) computedStats.addAll(tStats)
options.stats = computedStats options.stats = computedStats
options.includedTransactions = UserConfig.getConfiguration(realm).transactionTypes(realm) UserConfig.getConfiguration(realm) { userConfig ->
options.includedTransactions = userConfig.transactionTypes(realm)
}
return Calculator.computeGroups(realm, listOf(allSessionGroup, cgSessionGroup, tSessionGroup), options) return Calculator.computeGroups(realm, listOf(allSessionGroup, cgSessionGroup, tSessionGroup), options)
} }

@ -109,6 +109,11 @@ abstract class BaseFragment : Fragment() {
view?.findViewById<Toolbar>(R.id.toolbar)?.let { toolbar -> view?.findViewById<Toolbar>(R.id.toolbar)?.let { toolbar ->
parentActivity?.setSupportActionBar(toolbar) parentActivity?.setSupportActionBar(toolbar)
} }
context?.getColor(R.color.kaki_darkest)?.let { color ->
view?.setBackgroundColor(color)
}
} }
/** /**

@ -8,15 +8,13 @@ import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import io.realm.RealmObject import io.realm.RealmObject
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.interfaces.Deletable import net.pokeranalytics.android.model.interfaces.Deletable
import net.pokeranalytics.android.ui.modules.datalist.DataListActivity import net.pokeranalytics.android.ui.modules.datalist.DataListActivity
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.util.extensions.findById
/** /**
* Deletable Item Fragment * Deletable Item Fragment
@ -56,7 +54,7 @@ abstract class DeletableItemFragment : RealmFragment() {
val itemToDeleteId = data?.getStringExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName) val itemToDeleteId = data?.getStringExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName)
itemToDeleteId?.let { id -> itemToDeleteId?.let { id ->
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
delay(300) delay(300)
deleteItem(dataListAdapter, deletableItems(), id) deleteItem(dataListAdapter, deletableItems(), id)
} }
@ -84,14 +82,21 @@ abstract class DeletableItemFragment : RealmFragment() {
if (itemToDelete is RealmObject && itemPosition != -1) { if (itemToDelete is RealmObject && itemPosition != -1) {
val itemClass = itemToDelete.realmObjectClass
// Check if the object is valid for the deletion // Check if the object is valid for the deletion
if (itemToDelete.isValidForDelete(this.getRealm())) { if (itemToDelete.isValidForDelete(this.getRealm())) {
deletedItem = getRealm().copyFromRealm(itemToDelete) deletedItem = getRealm().copyFromRealm(itemToDelete)
lastDeletedItemPosition = itemPosition lastDeletedItemPosition = itemPosition
getRealm().executeTransaction {
itemToDelete.deleteDependencies(it) executeRealmAsyncTransaction { asyncRealm ->
itemToDelete.deleteFromRealm() val item = asyncRealm.findById(itemClass, itemId) as? Deletable
item?.let {
item.deleteDependencies(asyncRealm)
(item as RealmObject).deleteFromRealm()
}
} }
itemHasBeenReInserted = false itemHasBeenReInserted = false
updateUIAfterDeletion(itemId, itemPosition) updateUIAfterDeletion(itemId, itemPosition)
showUndoSnackBar() showUndoSnackBar()
@ -117,7 +122,8 @@ abstract class DeletableItemFragment : RealmFragment() {
snackBar?.setAction(R.string.cancel) { snackBar?.setAction(R.string.cancel) {
if (!itemHasBeenReInserted) { if (!itemHasBeenReInserted) {
itemHasBeenReInserted = true itemHasBeenReInserted = true
getRealm().executeTransaction { realm ->
executeRealmAsyncTransaction { realm ->
deletedItem?.let { deletedItem?.let {
val item = realm.copyToRealmOrUpdate(it) val item = realm.copyToRealmOrUpdate(it)
updateUIAfterUndoDeletion(item) updateUIAfterUndoDeletion(item)

@ -1,37 +1,45 @@
package net.pokeranalytics.android.ui.fragment.components package net.pokeranalytics.android.ui.fragment.components
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import io.realm.Realm import io.realm.Realm
import io.realm.RealmModel import io.realm.RealmModel
import io.realm.RealmResults import io.realm.RealmResults
import kotlinx.coroutines.Dispatchers import net.pokeranalytics.android.AppState
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import timber.log.Timber import net.pokeranalytics.android.ui.fragment.ImportBroadcast
import kotlin.coroutines.CoroutineContext
interface RealmAsyncListener { interface RealmAsyncListener {
fun asyncListenedEntityChange(realm: Realm) fun asyncListenedEntityChange(realm: Realm, clazz: Class<out RealmModel>)
} }
open class RealmFragment : BaseFragment() { open class RealmFragment : BaseFragment() {
val coroutineContext: CoroutineContext
get() = Dispatchers.Main
/** /**
* A realm instance * A realm instance
*/ */
private lateinit var realm: Realm // private lateinit var realm: Realm
/*** /***
* A listener to async updates * A listener to async updates
*/ */
private var changeListener: RealmAsyncListener? = null private var changeListener: RealmAsyncListener? = null
private var realmResults: RealmResults<out RealmModel>? = null private val endImportReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
sendRealmChange()
}
}
// private var realmResults: RealmResults<out RealmModel>? = null
/** /**
* A List of observed RealmResults * A List of observed RealmResults
@ -39,18 +47,8 @@ open class RealmFragment : BaseFragment() {
private var observedRealmResults: MutableList<RealmResults<*>> = mutableListOf() private var observedRealmResults: MutableList<RealmResults<*>> = mutableListOf()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
realm = Realm.getDefaultInstance() // realm = Realm.getDefaultInstance()
LocalBroadcastManager.getInstance(requireContext()).registerReceiver(endImportReceiver, IntentFilter(ImportBroadcast.END.identifier))
this.observedEntities.forEach {
val realmResults = realm.where(it).findAll()
realmResults.addChangeListener { t, _ ->
this.entitiesChanged(it, t)
}
this.observedRealmResults.add(realmResults)
}
return super.onCreateView(inflater, container, savedInstanceState) return super.onCreateView(inflater, container, savedInstanceState)
} }
@ -58,7 +56,11 @@ open class RealmFragment : BaseFragment() {
* Get the realm instance * Get the realm instance
*/ */
fun getRealm(): Realm { fun getRealm(): Realm {
return this.realm return this.parentActivity?.getRealm() ?: throw PAIllegalStateException("parent activity missing")
}
fun executeRealmAsyncTransaction(handler: (Realm) -> (Unit)) {
this.parentActivity?.executeRealmAsyncTransaction(handler) ?: throw PAIllegalStateException("parent activity missing")
} }
fun addRealmChangeListener(listener: RealmAsyncListener, clazz: Class<out RealmModel>) { fun addRealmChangeListener(listener: RealmAsyncListener, clazz: Class<out RealmModel>) {
@ -68,33 +70,48 @@ open class RealmFragment : BaseFragment() {
} }
this.changeListener = listener this.changeListener = listener
val results = this.realm.where(clazz).findAllAsync() val results = getRealm().where(clazz).findAllAsync()
results.addChangeListener { t, _ -> results.addChangeListener { res, _ ->
Timber.d("Realm changes: ${realmResults?.size}, $this") // Timber.d("Realm changes: ${realmResults?.size}, $this")
this.changeListener?.asyncListenedEntityChange(t.realm) if (!AppState.isImporting) {
this.changeListener?.asyncListenedEntityChange(res.realm, clazz)
} else {
this.changedClasses.add(clazz)
}
} }
this.observedRealmResults.add(results) this.observedRealmResults.add(results)
} }
private var changedClasses: MutableSet<Class<out RealmModel>> = mutableSetOf()
fun sendRealmChange() {
if (!AppState.isImporting) {
for (clazz in this.changedClasses) {
this.changeListener?.asyncListenedEntityChange(getRealm(), clazz)
}
this.changedClasses.clear()
}
}
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
LocalBroadcastManager.getInstance(requireContext()).unregisterReceiver(endImportReceiver)
this.observedRealmResults.forEach { this.observedRealmResults.forEach {
it.removeAllChangeListeners() it.removeAllChangeListeners()
} }
this.realm.close()
this.realmResults?.removeAllChangeListeners()
} }
/** /**
* A list of RealmModel classes to observe * A list of RealmModel classes to observe
*/ */
open val observedEntities: List<Class<out RealmModel>> = listOf() // open val observedEntities: List<Class<out RealmModel>> = listOf()
/** /**
* The method called when a change happened in any RealmResults * The method called when a change happened in any RealmResults
*/ */
open fun entitiesChanged(clazz: Class<out RealmModel>, results: RealmResults<out RealmModel>) {} // open fun entitiesChanged(clazz: Class<out RealmModel>, results: RealmResults<out RealmModel>) {}
} }

@ -13,7 +13,7 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
open class BottomSheetListFragment : BottomSheetFragment(), LiveRowRepresentableDataSource, RowRepresentableDelegate { open class BottomSheetDataListFragment : BottomSheetFragment(), LiveRowRepresentableDataSource, RowRepresentableDelegate {
private var _binding: BottomSheetListBinding? = null private var _binding: BottomSheetListBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
@ -62,18 +62,8 @@ open class BottomSheetListFragment : BottomSheetFragment(), LiveRowRepresentable
} }
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) { override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
this.onRowSelected(position) this.onRowSelected(position)
dismiss() dismiss()
// this.viewModel.realmData?.let {
// val selectedData = it[position]
// selectedData?.let {data ->
// this.viewModel.onRowValueChanged(data)
//// this.delegate.onRowValueChanged(data, this.row)
// dismiss()
// }
// }
// super.onRowSelected(position, row, tag)
} }
/** /**

@ -2,22 +2,16 @@ package net.pokeranalytics.android.ui.fragment.components.bottomsheet
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import io.realm.RealmModel
import net.pokeranalytics.android.databinding.BottomSheetDoubleEditTextBinding
import net.pokeranalytics.android.databinding.BottomSheetListBinding
import net.pokeranalytics.android.model.LiveData import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.ui.modules.data.EditableDataActivity
import net.pokeranalytics.android.ui.activity.components.BaseActivity import net.pokeranalytics.android.ui.activity.components.BaseActivity
import net.pokeranalytics.android.ui.modules.data.EditableDataActivity
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
/** /**
* Manage multiple items selection in a bottom sheet list * Manage multiple items selection in a bottom sheet list
*/ */
open class BottomSheetMultiSelectionFragment : BottomSheetListFragment() { open class BottomSheetDataMultiSelectionFragment : BottomSheetDataListFragment() {
override fun viewTypeForPosition(position: Int): Int { override fun viewTypeForPosition(position: Int): Int {
return RowViewType.TITLE_CHECK.ordinal return RowViewType.TITLE_CHECK.ordinal
@ -27,11 +21,14 @@ open class BottomSheetMultiSelectionFragment : BottomSheetListFragment() {
if (requestCode == REQUEST_CODE_ADD_NEW_OBJECT && resultCode == Activity.RESULT_OK && data != null) { if (requestCode == REQUEST_CODE_ADD_NEW_OBJECT && resultCode == Activity.RESULT_OK && data != null) {
val dataType = data.getIntExtra(EditableDataActivity.IntentKey.DATA_TYPE.keyName, 0) val dataType = data.getIntExtra(EditableDataActivity.IntentKey.DATA_TYPE.keyName, 0)
val primaryKey = data.getStringExtra(EditableDataActivity.IntentKey.PRIMARY_KEY.keyName) val primaryKey = data.getStringExtra(EditableDataActivity.IntentKey.PRIMARY_KEY.keyName)
val pokerAnalyticsActivity = activity as BaseActivity
val liveDataType = LiveData.values()[dataType] val liveDataType = LiveData.values()[dataType]
val proxyItem: RealmModel? = liveDataType.getData(pokerAnalyticsActivity.getRealm(), primaryKey) val realm = (activity as BaseActivity).getRealm()
this.model.selectedRows.add(proxyItem as RowRepresentable) liveDataType.getData(realm, primaryKey)?.let { proxyItem ->
this.refreshRow(proxyItem as RowRepresentable) val copy = realm.copyFromRealm(proxyItem)
this.model.selectedRows.add(copy as RowRepresentable)
this.refreshRow(copy as RowRepresentable)
}
// dataAdapter.refreshRow(proxyItem as RowRepresentable) // dataAdapter.refreshRow(proxyItem as RowRepresentable)
} }
} }

@ -18,13 +18,16 @@ import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.ui.activity.components.BaseActivity import net.pokeranalytics.android.ui.activity.components.BaseActivity
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.fragment.components.BaseFragment
import net.pokeranalytics.android.ui.modules.data.EditableDataActivity import net.pokeranalytics.android.ui.modules.data.EditableDataActivity
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow
import net.pokeranalytics.android.ui.view.rows.TransactionPropertiesRow import net.pokeranalytics.android.ui.view.rows.TransactionPropertiesRow
import net.pokeranalytics.android.ui.viewmodel.AddedDataViewModel
import net.pokeranalytics.android.ui.viewmodel.BottomSheetViewModel import net.pokeranalytics.android.ui.viewmodel.BottomSheetViewModel
import net.pokeranalytics.android.ui.viewmodel.BottomSheetViewModelFactory import net.pokeranalytics.android.ui.viewmodel.BottomSheetViewModelFactory
import timber.log.Timber
import java.util.* import java.util.*
class BottomSheetConfig(var row: RowRepresentable, class BottomSheetConfig(var row: RowRepresentable,
@ -48,6 +51,10 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
private var _binding: FragmentBottomSheetBinding? = null private var _binding: FragmentBottomSheetBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
protected open val addedDataViewModel: AddedDataViewModel by lazy {
ViewModelProvider(requireActivity()).get(AddedDataViewModel::class.java)
}
companion object { companion object {
private var config: BottomSheetConfig? = null private var config: BottomSheetConfig? = null
@ -75,11 +82,11 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
private fun newInstance(bottomSheetType: BottomSheetType): BottomSheetFragment { private fun newInstance(bottomSheetType: BottomSheetType): BottomSheetFragment {
return when (bottomSheetType) { return when (bottomSheetType) {
BottomSheetType.NONE -> BottomSheetFragment() BottomSheetType.NONE -> BottomSheetFragment()
BottomSheetType.LIST -> BottomSheetListFragment() BottomSheetType.LIST -> BottomSheetDataListFragment()
BottomSheetType.LIST_STATIC -> BottomSheetStaticListFragment() BottomSheetType.LIST_STATIC -> BottomSheetStaticListFragment()
BottomSheetType.LIST_GAME -> BottomSheetListGameFragment() BottomSheetType.LIST_GAME -> BottomSheetListGameFragment()
BottomSheetType.DOUBLE_LIST -> BottomSheetListGameFragment() BottomSheetType.DOUBLE_LIST -> BottomSheetListGameFragment()
BottomSheetType.MULTI_SELECTION -> BottomSheetMultiSelectionFragment() BottomSheetType.MULTI_SELECTION -> BottomSheetDataMultiSelectionFragment()
BottomSheetType.GRID -> BottomSheetTableSizeGridFragment() BottomSheetType.GRID -> BottomSheetTableSizeGridFragment()
BottomSheetType.EDIT_TEXT -> BottomSheetEditTextFragment() BottomSheetType.EDIT_TEXT -> BottomSheetEditTextFragment()
BottomSheetType.EDIT_TEXT_MULTI_LINES -> BottomSheetEditTextMultiLinesFragment() BottomSheetType.EDIT_TEXT_MULTI_LINES -> BottomSheetEditTextMultiLinesFragment()
@ -95,6 +102,7 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//TODO: When dependency 'com.google.android.material:material:1.1.0' will be available in stable version, upgrade and remove that //TODO: When dependency 'com.google.android.material:material:1.1.0' will be available in stable version, upgrade and remove that
activity?.setTheme(R.style.PokerAnalyticsTheme) activity?.setTheme(R.style.PokerAnalyticsTheme)
_binding = FragmentBottomSheetBinding.inflate(inflater, container, false) _binding = FragmentBottomSheetBinding.inflate(inflater, container, false)
inflateContentView(inflater, binding.root) inflateContentView(inflater, binding.root)
return binding.root return binding.root
@ -169,10 +177,18 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
val primaryKey = data.getStringExtra(EditableDataActivity.IntentKey.PRIMARY_KEY.keyName) val primaryKey = data.getStringExtra(EditableDataActivity.IntentKey.PRIMARY_KEY.keyName)
val pokerAnalyticsActivity = activity as BaseActivity val pokerAnalyticsActivity = activity as BaseActivity
val liveDataType = LiveData.values()[dataType] val liveDataType = LiveData.values()[dataType]
this.model.addedData = liveDataType.getData(pokerAnalyticsActivity.getRealm(), primaryKey)
val realm = pokerAnalyticsActivity.getRealm()
liveDataType.getData(realm, primaryKey)?.let {
this.model.addedData = realm.copyFromRealm(it)
this.onRowValueChanged() this.onRowValueChanged()
// this.delegate.onRowValueChanged(proxyItem, this.row)
dismiss() dismiss()
} ?: run {
Timber.w("Data not found with primary key = $primaryKey")
}
// this.model.addedData = liveDataType.getData(pokerAnalyticsActivity.getRealm(), primaryKey)
// this.delegate.onRowValueChanged(proxyItem, this.row)
} }
} }
@ -224,11 +240,22 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
else -> throw PAIllegalStateException("row $it does not have an associated LiveData value") else -> throw PAIllegalStateException("row $it does not have an associated LiveData value")
} }
EditableDataActivity.newInstanceForResult( val fragment = liveData.dataFragment
this, //
liveData, this.addedDataViewModel.dataForAdd = true
requestCode = REQUEST_CODE_ADD_NEW_OBJECT
) val bundle = Bundle()
bundle.putInt(BaseFragment.BundleKey.DATA_TYPE.value, liveData.ordinal)
fragment.arguments = bundle
(this.activity as BaseActivity).addFragmentWithBackStack(fragment, R.id.container)
dismiss()
// EditableDataActivity.newInstanceForResult(
// this,
// liveData,
// requestCode = REQUEST_CODE_ADD_NEW_OBJECT
// )
true true
} }
@ -251,9 +278,9 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
return this.model.rowRepresentableEditDescriptors return this.model.rowRepresentableEditDescriptors
} }
private fun getValue(): Any? { // private fun getValue(): Any? {
return this.model.getValue() // return this.model.getValue()
} // }
private fun onClear() { private fun onClear() {
this.delegate?.onRowValueChanged(null, this.model.row) this.delegate?.onRowValueChanged(null, this.model.row)

@ -7,6 +7,7 @@ import android.view.ViewGroup
import androidx.core.view.get import androidx.core.view.get
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.chip.Chip import com.google.android.material.chip.Chip
import io.realm.RealmModel
import net.pokeranalytics.android.databinding.BottomSheetGameListBinding import net.pokeranalytics.android.databinding.BottomSheetGameListBinding
import net.pokeranalytics.android.model.Limit import net.pokeranalytics.android.model.Limit
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
@ -17,7 +18,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
* Bottom Sheet List Game Fragment * Bottom Sheet List Game Fragment
* Display a list of game + chips to choose the game limit * Display a list of game + chips to choose the game limit
*/ */
class BottomSheetListGameFragment : BottomSheetListFragment() { class BottomSheetListGameFragment : BottomSheetDataListFragment() {
private var _binding: BottomSheetGameListBinding? = null private var _binding: BottomSheetGameListBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
@ -38,10 +39,10 @@ class BottomSheetListGameFragment : BottomSheetListFragment() {
} }
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) { override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
this.model.realmData?.let { this.model.realmData?.let { realmResults ->
val selectedData = it[position] val selectedData = realmResults[position]
selectedData?.let { data -> selectedData?.let { data ->
this.model.someValues[1] = data this.model.someValues[1] = realmResults.realm.copyFromRealm(data as RealmModel)
this.onRowValueChanged() this.onRowValueChanged()
// this.delegate.onRowValueChanged(values, this.row) // this.delegate.onRowValueChanged(values, this.row)
dismiss() dismiss()

@ -45,9 +45,7 @@ class BottomSheetStaticListFragment : BottomSheetFragment(), StaticRowRepresenta
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) { override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
this.model.selectedRows.add(row) this.model.selectedRows.add(row)
this.onRowValueChanged() this.onRowValueChanged()
// this.delegate.onRowValueChanged(row, this.row)
dismiss() dismiss()
// super.onRowSelected(position, row, tag)
} }
/** /**

@ -121,9 +121,12 @@ abstract class AbstractReportFragment : DataManagerFragment() {
this.reportViewModel.title = name this.reportViewModel.title = name
val rs = this.model.item as ReportSetup val rs = this.model.item as ReportSetup
getRealm().executeTransaction { realm -> val setupId = rs.id
val firstSave = (this.model.primaryKey == null) val firstSave = (this.model.primaryKey == null)
executeRealmAsyncTransaction { realm ->
if (firstSave) { if (firstSave) {
val options = this.selectedReport.options val options = this.selectedReport.options
rs.name = name rs.name = name
@ -141,15 +144,18 @@ abstract class AbstractReportFragment : DataManagerFragment() {
options.filterId?.let { id -> options.filterId?.let { id ->
rs.filter = realm.findById(id) rs.filter = realm.findById(id)
} }
realm.copyToRealmOrUpdate(rs)
} else {
rs.name = name
realm.insertOrUpdate(rs) realm.insertOrUpdate(rs)
// realm.copyToRealmOrUpdate(rs)
} else {
realm.findById<ReportSetup>(setupId)?.let { reportSetup ->
reportSetup.name = name
realm.insertOrUpdate(reportSetup)
}
} }
} }
this.model.primaryKey = rs.id this.model.primaryKey = setupId
this.deleteButtonShouldAppear = true this.deleteButtonShouldAppear = true
setToolbarTitle(this.reportViewModel.title) setToolbarTitle(this.reportViewModel.title)
} }

@ -7,16 +7,13 @@ import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.calcul.ReportDisplay
import net.pokeranalytics.android.calculus.Calculator import net.pokeranalytics.android.calculus.Calculator
import net.pokeranalytics.android.calculus.ComputableGroup import net.pokeranalytics.android.calculus.ComputableGroup
import net.pokeranalytics.android.calculus.Report import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.calculus.calcul.ReportDisplay
import net.pokeranalytics.android.databinding.FragmentComposableTableReportBinding import net.pokeranalytics.android.databinding.FragmentComposableTableReportBinding
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.ui.activity.components.ReportActivity import net.pokeranalytics.android.ui.activity.components.ReportActivity
@ -31,14 +28,10 @@ import net.pokeranalytics.android.ui.view.rows.StatRow
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.TextFormat import net.pokeranalytics.android.util.TextFormat
import timber.log.Timber import timber.log.Timber
import java.util.*
open class ComposableTableReportFragment : RealmFragment(), StaticRowRepresentableDataSource, CoroutineScope, open class ComposableTableReportFragment : RealmFragment(), StaticRowRepresentableDataSource,
RowRepresentableDelegate { RowRepresentableDelegate {
// override val coroutineContext: CoroutineContext
// get() = Dispatchers.Main
companion object { companion object {
/** /**
@ -211,12 +204,12 @@ open class ComposableTableReportFragment : RealmFragment(), StaticRowRepresentab
showLoader() showLoader()
GlobalScope.launch(coroutineContext) { CoroutineScope(Dispatchers.Default).launch {
var report: Report? = null var report: Report? = null
val test = GlobalScope.async { val test = async {
val s = Date() // val s = Date()
Timber.d(">>> start...") // Timber.d(">>> start...")
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.refresh() realm.refresh()
@ -227,9 +220,9 @@ open class ComposableTableReportFragment : RealmFragment(), StaticRowRepresentab
realm.close() realm.close()
val e = Date() // val e = Date()
val duration = (e.time - s.time) / 1000.0 // val duration = (e.time - s.time) / 1000.0
Timber.d(">>> ended in $duration seconds") // Timber.d(">>> ended in $duration seconds")
} }
test.await() test.await()

@ -11,6 +11,7 @@ import com.github.mikephil.charting.data.LineDataSet
import com.google.android.material.chip.Chip import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup import com.google.android.material.chip.ChipGroup
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -140,7 +141,7 @@ class ProgressReportFragment : AbstractReportFragment() {
when (aggregationType) { when (aggregationType) {
AggregationType.MONTH, AggregationType.YEAR -> { AggregationType.MONTH, AggregationType.YEAR -> {
if (aggregationType.criterias.combined().size < 2) { if (aggregationType.criterias.combined(getRealm()).size < 2) {
Toast.makeText(context, R.string.less_then_2_values_for_display, Toast.LENGTH_LONG).show() Toast.makeText(context, R.string.less_then_2_values_for_display, Toast.LENGTH_LONG).show()
return return
} }
@ -169,10 +170,10 @@ class ProgressReportFragment : AbstractReportFragment() {
graphContainer.hideWithAnimation() graphContainer.hideWithAnimation()
progressBar.showWithAnimation() progressBar.showWithAnimation()
GlobalScope.launch { CoroutineScope(Dispatchers.Default).launch {
val s = Date() // val s = Date()
Timber.d(">>> start...") // Timber.d(">>> start...")
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
@ -183,9 +184,9 @@ class ProgressReportFragment : AbstractReportFragment() {
realm.close() realm.close()
val e = Date() // val e = Date()
val duration = (e.time - s.time) / 1000.0 // val duration = (e.time - s.time) / 1000.0
Timber.d(">>> ended in $duration seconds") // Timber.d(">>> ended in $duration seconds")
launch(Dispatchers.Main) { launch(Dispatchers.Main) {
setGraphData(report, aggregationType) setGraphData(report, aggregationType)

@ -14,7 +14,7 @@ object AppReviewManager {
this.reviewRequested = true this.reviewRequested = true
} }
fun showReviewManager(activity: Activity) { fun showReviewManagerIfNecessary(activity: Activity) {
if (this.reviewRequested && this.shouldAskForReview(activity.baseContext)) { if (this.reviewRequested && this.shouldAskForReview(activity.baseContext)) {
this.reviewRequested = false this.reviewRequested = false

@ -35,8 +35,6 @@ import net.pokeranalytics.android.ui.view.rows.BankrollTotalRow
import net.pokeranalytics.android.ui.view.rows.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rows.CustomizableRowRepresentable
import net.pokeranalytics.android.util.extensions.sorted import net.pokeranalytics.android.util.extensions.sorted
import timber.log.Timber import timber.log.Timber
import java.util.*
import kotlin.collections.ArrayList
interface BankrollRowRepresentable : RowRepresentable { interface BankrollRowRepresentable : RowRepresentable {
var bankrollId: String? var bankrollId: String?

@ -13,6 +13,7 @@ import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.LineDataSet import com.github.mikephil.charting.data.LineDataSet
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -167,7 +168,7 @@ class CalendarDetailsFragment : BaseFragment(), StaticRowRepresentableDataSource
this.model.computedResults?.let { computedResults -> this.model.computedResults?.let { computedResults ->
GlobalScope.launch { CoroutineScope(Dispatchers.Default).launch {
val startDate = Date() val startDate = Date()

@ -11,11 +11,13 @@ import io.realm.Realm
import io.realm.RealmModel import io.realm.RealmModel
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.AppState
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Calculator import net.pokeranalytics.android.calculus.Calculator
import net.pokeranalytics.android.calculus.ComputedResults import net.pokeranalytics.android.calculus.ComputedResults
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.databinding.FragmentCalendarBinding import net.pokeranalytics.android.databinding.FragmentCalendarBinding
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
@ -23,9 +25,7 @@ import net.pokeranalytics.android.model.Criteria
import net.pokeranalytics.android.model.combined import net.pokeranalytics.android.model.combined
import net.pokeranalytics.android.model.filter.Query import net.pokeranalytics.android.model.filter.Query
import net.pokeranalytics.android.model.filter.QueryCondition import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.realm.ComputableResult import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.model.realm.Transaction
import net.pokeranalytics.android.model.realm.UserConfig
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
@ -43,8 +43,7 @@ import timber.log.Timber
import java.util.* import java.util.*
import kotlin.collections.set import kotlin.collections.set
class CalendarFragment : RealmFragment(), StaticRowRepresentableDataSource,
class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentableDataSource,
RowRepresentableDelegate, RealmAsyncListener { RowRepresentableDelegate, RealmAsyncListener {
enum class TimeFilter { enum class TimeFilter {
@ -87,6 +86,13 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
private var _binding: FragmentCalendarBinding? = null private var _binding: FragmentCalendarBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
private val requiredStats: List<Stat > = listOf(
Stat.LOCATIONS_PLAYED,
Stat.LONGEST_STREAKS,
Stat.DAYS_PLAYED,
Stat.STANDARD_DEVIATION_HOURLY
)
// Life Cycle // Life Cycle
override fun onCreateView( override fun onCreateView(
@ -101,13 +107,15 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
initData()
initUI() initUI()
initData()
addRealmChangeListener(this, UserConfig::class.java) addRealmChangeListener(this, UserConfig::class.java)
addRealmChangeListener(this, ComputableResult::class.java) addRealmChangeListener(this, ComputableResult::class.java)
addRealmChangeListener(this, Transaction::class.java) addRealmChangeListener(this, Transaction::class.java)
addRealmChangeListener(this, SessionSet::class.java)
} }
private var transactionFilterMenuItem: MenuItem? = null private var transactionFilterMenuItem: MenuItem? = null
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@ -133,11 +141,10 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
private fun setTransactionFilterItemColor() { private fun setTransactionFilterItemColor() {
context?.let { context?.let {
val userConfig = UserConfig.getConfiguration(getRealm()) UserConfig.getConfiguration(getRealm()) { userConfig ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.transactionFilterMenuItem?.let { item ->
val color = if (userConfig.transactionTypeIds.isNotEmpty()) R.color.red else R.color.white val color = if (userConfig.transactionTypeIds.isNotEmpty()) R.color.red else R.color.white
item.iconTintList = ColorStateList.valueOf(it.getColor(color)) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.transactionFilterMenuItem?.iconTintList = ColorStateList.valueOf(it.getColor(color))
} }
} }
} }
@ -161,14 +168,46 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
} }
private fun showDetails(computedResults: ComputedResults, title: String?) { private fun showDetails(computedResults: ComputedResults, title: String?) {
// start calculation with progress values
CoroutineScope(Dispatchers.Default).launch {
var report: Report? = null
val coroutine = async {
val realm = Realm.getDefaultInstance()
val options = Calculator.Options(
progressValues = Calculator.Options.ProgressValues.STANDARD,
stats = requiredStats,
query = computedResults.group.query,
includedTransactions = transactionTypes(realm)
)
report = Calculator.computeStats(realm, options = options)
realm.close()
}
coroutine.await()
launch(Dispatchers.Main) {
report?.results?.firstOrNull()?.let { cr ->
CalendarDetailsActivity.newInstance( CalendarDetailsActivity.newInstance(
requireContext(), requireContext(),
computedResults, cr,
sessionTypeCondition, sessionTypeCondition,
title title
) )
} }
}
}
}
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) { override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
when (this.currentTimeFilter) { when (this.currentTimeFilter) {
@ -211,9 +250,6 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
} }
} }
override val observedEntities: List<Class<out RealmModel>> = listOf(ComputableResult::class.java)
// Business // Business
/** /**
@ -348,7 +384,11 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
binding.progressBar.showWithAnimation() binding.progressBar.showWithAnimation()
binding.recyclerView.hideWithAnimation() binding.recyclerView.hideWithAnimation()
GlobalScope.launch { CoroutineScope(Dispatchers.Default).launch {
val async = async {
val s = Date()
// Timber.d(">>> start...")
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.refresh() realm.refresh()
@ -357,32 +397,58 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
realm.close() realm.close()
GlobalScope.launch(Dispatchers.Main) { val e = Date()
val duration = (e.time - s.time) / 1000.0
// Timber.d(">>> computations took $duration seconds")
}
async.await()
launch(Dispatchers.Main) {
if (isAdded && !isDetached) {
displayData() displayData()
} }
} }
} }
// CoroutineScope(Dispatchers.Default).launch {
//
// val realm = Realm.getDefaultInstance()
// realm.refresh()
//
// launchStatComputation(realm)
//
// realm.close()
//
// launch(Dispatchers.Main) {
// displayData()
// }
// }
}
private fun transactionTypes(realm: Realm): List<TransactionType> {
var transactionTypes = listOf<TransactionType>()
UserConfig.getConfiguration(realm) { userConfig ->
transactionTypes = userConfig.transactionTypes(realm)
}
return transactionTypes
}
private fun launchStatComputation(realm: Realm) { private fun launchStatComputation(realm: Realm) {
return
Timber.d(">>> Launch calendar computations")
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.time = Date().startOfMonth() calendar.time = Date().startOfMonth()
val startDate = Date() val startDate = Date()
val requiredStats: List<Stat> = val transactionTypes = this.transactionTypes(realm)
listOf(
Stat.LOCATIONS_PLAYED,
Stat.LONGEST_STREAKS,
Stat.DAYS_PLAYED,
Stat.STANDARD_DEVIATION_HOURLY
)
val transactionTypes = UserConfig.getConfiguration(realm).transactionTypes(realm)
// All // All
val allOptions = Calculator.Options( val allOptions = Calculator.Options(
progressValues = Calculator.Options.ProgressValues.STANDARD,
stats = requiredStats, stats = requiredStats,
query = Query(this.sessionTypeCondition), query = Query(this.sessionTypeCondition),
includedTransactions = transactionTypes includedTransactions = transactionTypes
@ -392,7 +458,6 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
// Sliding Month [sm] // Sliding Month [sm]
val smOptions = Calculator.Options( val smOptions = Calculator.Options(
progressValues = Calculator.Options.ProgressValues.STANDARD,
stats = requiredStats, stats = requiredStats,
query = Query(this.slidingMonthQueryCondition, this.sessionTypeCondition), query = Query(this.slidingMonthQueryCondition, this.sessionTypeCondition),
includedTransactions = transactionTypes includedTransactions = transactionTypes
@ -405,23 +470,24 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
val monthlyReports: HashMap<Date, ComputedResults> = HashMap() val monthlyReports: HashMap<Date, ComputedResults> = HashMap()
val monthlyQueries = when (sessionTypeCondition) { val monthlyQueries = when (sessionTypeCondition) {
QueryCondition.IsCash -> listOf(Criteria.AllMonthsUpToNow, Criteria.Cash).combined() QueryCondition.IsCash -> listOf(Criteria.AllMonthsUpToNow, Criteria.Cash).combined(realm)
QueryCondition.IsTournament -> listOf(Criteria.AllMonthsUpToNow, Criteria.Tournament).combined() QueryCondition.IsTournament -> listOf(Criteria.AllMonthsUpToNow, Criteria.Tournament).combined(realm)
else -> listOf(Criteria.Years, Criteria.MonthsOfYear).combined() else -> listOf(Criteria.Years, Criteria.MonthsOfYear).combined(realm)
} }
monthlyQueries.forEach { query -> for (monthlyQuery in monthlyQueries) {
val options = Calculator.Options( val options = Calculator.Options(
progressValues = Calculator.Options.ProgressValues.STANDARD,
stats = requiredStats, stats = requiredStats,
query = query, query = monthlyQuery,
includedTransactions = transactionTypes includedTransactions = transactionTypes
) )
val report = Calculator.computeStats(realm, options = options) val report = Calculator.computeStats(realm, options = options)
report.results.forEach { computedResults ->
for (computedResults in report.results) {
if (!computedResults.isEmpty) { if (!computedResults.isEmpty) {
// Set date data // Set date data
query.conditions.forEach { condition ->
for (condition in monthlyQuery.conditions) {
when (condition) { when (condition) {
is QueryCondition.AnyYear -> calendar.set( is QueryCondition.AnyYear -> calendar.set(
Calendar.YEAR, Calendar.YEAR,
@ -438,11 +504,11 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
monthlyReports[calendar.time] = computedResults monthlyReports[calendar.time] = computedResults
} }
} }
} }
// Sliding Year [sm] // Sliding Year [sm]
val syOptions = Calculator.Options( val syOptions = Calculator.Options(
progressValues = Calculator.Options.ProgressValues.STANDARD,
stats = requiredStats, stats = requiredStats,
query = Query(this.slidingYearQueryCondition, this.sessionTypeCondition), query = Query(this.slidingYearQueryCondition, this.sessionTypeCondition),
includedTransactions = transactionTypes includedTransactions = transactionTypes
@ -457,14 +523,13 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
val yearlyReports: HashMap<Date, ComputedResults> = HashMap() val yearlyReports: HashMap<Date, ComputedResults> = HashMap()
val yearConditions = when (sessionTypeCondition) { val yearConditions = when (sessionTypeCondition) {
QueryCondition.IsCash -> listOf(Criteria.Years, Criteria.Cash).combined() QueryCondition.IsCash -> listOf(Criteria.Years, Criteria.Cash).combined(realm)
QueryCondition.IsTournament -> listOf(Criteria.Years, Criteria.Tournament).combined() QueryCondition.IsTournament -> listOf(Criteria.Years, Criteria.Tournament).combined(realm)
else -> listOf(Criteria.Years).combined() else -> listOf(Criteria.Years).combined(realm)
} }
yearConditions.forEach { query -> yearConditions.forEach { query ->
val options = Calculator.Options( val options = Calculator.Options(
progressValues = Calculator.Options.ProgressValues.STANDARD,
stats = requiredStats, stats = requiredStats,
query = query, query = query,
includedTransactions = transactionTypes includedTransactions = transactionTypes
@ -498,7 +563,10 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
* Display data * Display data
*/ */
private fun displayData() { private fun displayData() {
Timber.d("displayData") // Timber.d("displayData")
this.binding.progressBar.hideWithAnimation()
this.binding.recyclerView.showWithAnimation()
if (context == null) { return } // required because of launchAsyncStatComputation if (context == null) { return } // required because of launchAsyncStatComputation
@ -594,21 +662,20 @@ class CalendarFragment : RealmFragment(), CoroutineScope, StaticRowRepresentable
} }
} }
Timber.d("Display data: ${System.currentTimeMillis() - startDate.time}ms") // Timber.d("Display data: ${System.currentTimeMillis() - startDate.time}ms")
Timber.d("Rows: ${rows.size}") // Timber.d("Rows: ${rows.size}")
this.calendarAdapter.notifyDataSetChanged() this.calendarAdapter.notifyDataSetChanged()
this.binding.progressBar.hideWithAnimation()
this.binding.recyclerView.showWithAnimation()
} }
override fun asyncListenedEntityChange(realm: Realm) { override fun asyncListenedEntityChange(realm: Realm, clazz: Class<out RealmModel>) {
if (isAdded) { // Fixes: java.lang.IllegalStateException Fragment StatisticsFragment{9d3e5ec} not attached to a context. if (isAdded) { // Fixes: java.lang.IllegalStateException Fragment StatisticsFragment{9d3e5ec} not attached to a context.
launchAsyncStatComputation() launchAsyncStatComputation()
setTransactionFilterItemColor() setTransactionFilterItemColor()
} }
} }
private fun showGridCalendar() { private fun showGridCalendar() {

@ -133,7 +133,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa
tag: Int tag: Int
): CharSequence { ): CharSequence {
return when (row) { return when (row) {
SimpleRow.NAME -> if (customField.name.isNotEmpty()) customField.name else NULL_TEXT SimpleRow.NAME -> customField.name.ifEmpty { NULL_TEXT }
else -> super.charSequenceForRow(row, context, 0) else -> super.charSequenceForRow(row, context, 0)
} }
} }

@ -5,7 +5,6 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@ -15,6 +14,7 @@ import net.pokeranalytics.android.model.interfaces.Savable
import net.pokeranalytics.android.model.interfaces.SaveValidityStatus import net.pokeranalytics.android.model.interfaces.SaveValidityStatus
import net.pokeranalytics.android.ui.fragment.components.RealmFragment import net.pokeranalytics.android.ui.fragment.components.RealmFragment
import net.pokeranalytics.android.ui.modules.datalist.DataListActivity import net.pokeranalytics.android.ui.modules.datalist.DataListActivity
import net.pokeranalytics.android.ui.viewmodel.AddedDataViewModel
import net.pokeranalytics.android.ui.viewmodel.DataManagerViewModel import net.pokeranalytics.android.ui.viewmodel.DataManagerViewModel
open class DataManagerFragment : RealmFragment() { open class DataManagerFragment : RealmFragment() {
@ -23,12 +23,11 @@ open class DataManagerFragment : RealmFragment() {
ViewModelProvider(this).get(modelClass) ViewModelProvider(this).get(modelClass)
} }
open val modelClass: Class<out DataManagerViewModel> = DataManagerViewModel::class.java protected open val addedDataViewModel: AddedDataViewModel by lazy {
ViewModelProvider(requireActivity()).get(AddedDataViewModel::class.java)
}
// lateinit var item: Deletable open val modelClass: Class<out DataManagerViewModel> = DataManagerViewModel::class.java
// protected lateinit var liveDataType: LiveData
// protected var primaryKey: String? = null
// protected var dataType: Int? = null
var deleteButtonShouldAppear = false var deleteButtonShouldAppear = false
set(value) { set(value) {
@ -53,10 +52,22 @@ open class DataManagerFragment : RealmFragment() {
menu.clear() menu.clear()
inflater.inflate(R.menu.toolbar_editable_data, menu) inflater.inflate(R.menu.toolbar_editable_data, menu)
this.editableMenu = menu this.editableMenu = menu
setMenuListeners()
updateMenuUI() updateMenuUI()
super.onCreateOptionsMenu(menu, inflater) super.onCreateOptionsMenu(menu, inflater)
} }
private fun setMenuListeners() {
editableMenu?.findItem(R.id.delete)?.setOnMenuItemClickListener {
deleteData()
return@setOnMenuItemClickListener true
}
editableMenu?.findItem(R.id.save)?.setOnMenuItemClickListener {
saveData()
return@setOnMenuItemClickListener true
}
}
/** /**
* Update menu UI * Update menu UI
*/ */
@ -65,13 +76,13 @@ open class DataManagerFragment : RealmFragment() {
editableMenu?.findItem(R.id.save)?.isVisible = this.saveButtonShouldAppear editableMenu?.findItem(R.id.save)?.isVisible = this.saveButtonShouldAppear
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { // override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { // when (item.itemId) {
R.id.save -> saveData() // R.id.save -> saveData()
R.id.delete -> deleteData() // R.id.delete -> deleteData()
} // }
return true // return true
} // }
/** /**
* Init data * Init data
@ -96,14 +107,20 @@ open class DataManagerFragment : RealmFragment() {
val status = savable.getSaveValidityStatus(realm = this.getRealm()) val status = savable.getSaveValidityStatus(realm = this.getRealm())
when (status) { when (status) {
SaveValidityStatus.VALID -> { SaveValidityStatus.VALID -> {
this.getRealm().executeTransaction {
val managedItem = it.copyToRealmOrUpdate(this.model.item) executeRealmAsyncTransaction { asyncRealm ->
if (managedItem is Savable) { asyncRealm.insertOrUpdate(savable)
val uniqueIdentifier = managedItem.id
finishActivityWithResult(uniqueIdentifier)
}
} }
onDataSaved() onDataSaved()
if (this.addedDataViewModel.dataForAdd) {
this.addedDataViewModel.data.value = savable
this.parentActivity?.supportFragmentManager?.popBackStack()
} else {
val uniqueIdentifier = savable.id
finishActivityWithResult(uniqueIdentifier)
}
} }
else -> { else -> {
val message = savable.getFailedSaveMessage(status) val message = savable.getFailedSaveMessage(status)

@ -18,7 +18,6 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowUpdatable import net.pokeranalytics.android.ui.view.RowUpdatable
open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegate { open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegate {
lateinit var rowRepresentableAdapter: RowRepresentableAdapter lateinit var rowRepresentableAdapter: RowRepresentableAdapter
@ -43,7 +42,6 @@ open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegat
this.model.primaryKey = this.arguments?.getString(BundleKey.PRIMARY_KEY.value) this.model.primaryKey = this.arguments?.getString(BundleKey.PRIMARY_KEY.value)
this.model.loadItemWithRealm(getRealm()) this.model.loadItemWithRealm(getRealm())
} }
open fun indexOfFirstRowToSelect(): Int { open fun indexOfFirstRowToSelect(): Int {
@ -67,15 +65,22 @@ open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegat
} }
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
getRealm().executeTransaction {
try {
(this.model.item as RowUpdatable).updateValue(value, row) (this.model.item as RowUpdatable).updateValue(value, row)
if (this.model.primaryKey != null) {
executeRealmAsyncTransaction { asyncRealm ->
try {
asyncRealm.insertOrUpdate(this.model.item)
} catch (e: Exception) { } catch (e: Exception) {
CrashLogging.log("Exception caught: row = $row, value=$value, class=${this.javaClass}") CrashLogging.log("Exception caught: row = $row, value=$value, class=${this.javaClass}")
throw e throw e
} }
}
} }
rowRepresentableAdapter.refreshRow(row) rowRepresentableAdapter.refreshRow(row)
} }

@ -2,8 +2,8 @@ package net.pokeranalytics.android.ui.modules.data
import android.content.Context import android.content.Context
import io.realm.kotlin.where import io.realm.kotlin.where
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.calculus.bankroll.BankrollReportManager import net.pokeranalytics.android.calculus.bankroll.BankrollReportManager
@ -33,6 +33,11 @@ class TransactionDataFragment : EditableDataFragment(), StaticRowRepresentableDa
return this.model.item as Transaction return this.model.item as Transaction
} }
override fun onDestroyView() {
super.onDestroyView()
this.addedDataViewModel.data.removeObservers(this)
}
override fun initData() { override fun initData() {
super.initData() super.initData()
@ -40,9 +45,19 @@ class TransactionDataFragment : EditableDataFragment(), StaticRowRepresentableDa
if (this.transaction.bankroll == null) { if (this.transaction.bankroll == null) {
val bankrolls = getRealm().where<Bankroll>().findAll() val bankrolls = getRealm().where<Bankroll>().findAll()
if (bankrolls.size == 1) { if (bankrolls.size == 1) {
this.transaction.bankroll = bankrolls.first() bankrolls.first()?.let { br ->
this.transaction.bankroll = getRealm().copyFromRealm(br)
}
} }
} }
this.addedDataViewModel.data.observeForever {
if (this.addedDataViewModel.dataForAdd) {
this.addedDataViewModel.dataForAdd = false
this.onRowValueChanged(it, this.addedDataViewModel.dataIdentifier)
}
}
} }
override fun indexOfFirstRowToSelect(): Int { override fun indexOfFirstRowToSelect(): Int {
@ -102,6 +117,9 @@ class TransactionDataFragment : EditableDataFragment(), StaticRowRepresentableDa
} }
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) { override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
this.addedDataViewModel.dataIdentifier = row
when (row) { when (row) {
TransactionPropertiesRow.DATE -> DateTimePickerManager.create( TransactionPropertiesRow.DATE -> DateTimePickerManager.create(
requireContext(), requireContext(),
@ -141,7 +159,7 @@ class TransactionDataFragment : EditableDataFragment(), StaticRowRepresentableDa
when (val next = rows[index + 1]) { when (val next = rows[index + 1]) {
TransactionPropertiesRow.DATE, TransactionPropertiesRow.COMMENT -> {} TransactionPropertiesRow.DATE, TransactionPropertiesRow.COMMENT -> {}
else -> { else -> {
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
delay(200) delay(200)
onRowSelected(0, next) onRowSelected(0, next)
} }

@ -81,10 +81,14 @@ open class DataListFragment : DeletableItemFragment(), RowRepresentableDelegate
getRealm().where(this.model.identifiableClass) getRealm().where(this.model.identifiableClass)
.`in`("id", itemIds) .`in`("id", itemIds)
.sort(this.model.dataType.sortFields, this.model.dataType.sortOrders) .sort(this.model.dataType.sortFields, this.model.dataType.sortOrders)
.findAll() .findAllAsync()
} else { } else {
this.retrieveItems(getRealm()) this.retrieveItems(getRealm())
} }
items.addChangeListener { _, _ ->
this.dataListAdapter.notifyDataSetChanged()
}
this.model.setItemsList(items) this.model.setItemsList(items)
} }

@ -12,6 +12,7 @@ import com.android.billingclient.api.Purchase
import com.google.android.material.badge.BadgeDrawable import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.badge.BadgeUtils import com.google.android.material.badge.BadgeUtils
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import io.realm.Realm
import io.realm.RealmModel import io.realm.RealmModel
import io.realm.RealmResults import io.realm.RealmResults
import io.realm.Sort import io.realm.Sort
@ -21,15 +22,14 @@ import net.pokeranalytics.android.api.BlogPostApi
import net.pokeranalytics.android.databinding.FragmentFeedBinding import net.pokeranalytics.android.databinding.FragmentFeedBinding
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.LiveData import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.realm.Filter import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.Transaction
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.model.utils.TimeManager
import net.pokeranalytics.android.ui.activity.BillingActivity import net.pokeranalytics.android.ui.activity.BillingActivity
import net.pokeranalytics.android.ui.activity.components.RequestCode import net.pokeranalytics.android.ui.activity.components.RequestCode
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.extensions.openUrl
import net.pokeranalytics.android.ui.fragment.components.FilterableFragment import net.pokeranalytics.android.ui.fragment.components.FilterableFragment
import net.pokeranalytics.android.ui.fragment.components.RealmAsyncListener
import net.pokeranalytics.android.ui.modules.data.EditableDataActivity import net.pokeranalytics.android.ui.modules.data.EditableDataActivity
import net.pokeranalytics.android.ui.modules.datalist.DataListActivity import net.pokeranalytics.android.ui.modules.datalist.DataListActivity
import net.pokeranalytics.android.ui.modules.filter.FilterActivityRequestCode import net.pokeranalytics.android.ui.modules.filter.FilterActivityRequestCode
@ -40,13 +40,13 @@ import net.pokeranalytics.android.ui.modules.session.SessionActivity
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.URL
import net.pokeranalytics.android.util.billing.AppGuard import net.pokeranalytics.android.util.billing.AppGuard
import net.pokeranalytics.android.util.billing.PurchaseListener import net.pokeranalytics.android.util.billing.PurchaseListener
import net.pokeranalytics.android.util.extensions.count import net.pokeranalytics.android.util.extensions.count
import net.pokeranalytics.android.util.extensions.findById
import java.util.* import java.util.*
class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseListener { class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseListener, RealmAsyncListener {
private enum class Tab { private enum class Tab {
SESSIONS, SESSIONS,
@ -75,6 +75,7 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
private lateinit var realmTransactions: RealmResults<Transaction> private lateinit var realmTransactions: RealmResults<Transaction>
private lateinit var realmHandHistories: RealmResults<HandHistory> private lateinit var realmHandHistories: RealmResults<HandHistory>
// private lateinit var realmSessions: RealmResults<Session>
private var newSessionCreated: Boolean = false private var newSessionCreated: Boolean = false
private var adapterHasBeenSet: Boolean = false private var adapterHasBeenSet: Boolean = false
@ -83,17 +84,12 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
private var badgeDrawable: BadgeDrawable? = null private var badgeDrawable: BadgeDrawable? = null
override val observedEntities: List<Class<out RealmModel>> = override fun asyncListenedEntityChange(realm: Realm, clazz: Class<out RealmModel>) {
listOf(Session::class.java, Transaction::class.java, HandHistory::class.java)
override fun entitiesChanged(
clazz: Class<out RealmModel>,
results: RealmResults<out RealmModel>
) {
super.entitiesChanged(clazz, results)
// Timber.d("asyncListenedEntityChange for $clazz")
when (clazz.kotlin) { when (clazz.kotlin) {
Session::class -> { Session::class -> {
// Timber.d("WOWOWOOWOOWOWOWOWOWOWOWOWO")
this.sessionAdapter.refreshData() this.sessionAdapter.refreshData()
this.sessionAdapter.notifyDataSetChanged() this.sessionAdapter.notifyDataSetChanged()
} }
@ -106,7 +102,6 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
this.handHistoryAdapter.notifyDataSetChanged() this.handHistoryAdapter.notifyDataSetChanged()
} }
} }
} }
private var _binding: FragmentFeedBinding? = null private var _binding: FragmentFeedBinding? = null
@ -141,11 +136,7 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
when (item.itemId) { when (item.itemId) {
R.id.duplicate -> { R.id.duplicate -> {
val session = this.sessionAdapter.sessionForPosition(menuPosition) val session = this.sessionAdapter.sessionForPosition(menuPosition)
if (session != null) {
createNewSession(true, sessionId = session.id, duplicate = true) createNewSession(true, sessionId = session.id, duplicate = true)
} else {
throw PAIllegalStateException("Session not found for duplicate at position: $menuPosition")
}
} }
} }
@ -157,12 +148,30 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
AppGuard.registerListener(this) AppGuard.registerListener(this)
// this.realmSessions = getRealm().where<Session>().findAllAsync()
// val listener = RealmChangeListener<RealmResults<Session>> {
// Timber.d("WOWOWOOWOOWOWOWOWOWOWOWOWO")
// sessionAdapter.refreshData()
// sessionAdapter.notifyDataSetChanged()
// }
// this.realmSessions.addChangeListener(listener)
// this.realmSessions.addChangeListener { t, changeSet ->
// Timber.d("WOWOWOOWOOWOWOWOWOWOWOWOWO")
// this.sessionAdapter.refreshData()
// this.sessionAdapter.notifyDataSetChanged()
// }
initUI() initUI()
initData() initData()
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
getRealm().refresh()
if (requestCode == RequestCode.FEED_MENU.value && resultCode == RESULT_OK && data != null) { if (requestCode == RequestCode.FEED_MENU.value && resultCode == RESULT_OK && data != null) {
when (data.getIntExtra(NewDataMenuActivity.IntentKey.CHOICE.keyName, -1)) { when (data.getIntExtra(NewDataMenuActivity.IntentKey.CHOICE.keyName, -1)) {
0 -> createNewSession(false) 0 -> createNewSession(false)
@ -276,9 +285,20 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
displayBlogPostButton() displayBlogPostButton()
binding.postButton.setOnClickListener { binding.postButton.setOnClickListener {
Preferences.setBlogTipsTapped(requireContext())
parentActivity?.openUrl(URL.BLOG_TIPS.value) getRealm().executeTransactionAsync { realm ->
displayBlogPostButton()
realm.where<Session>().findAll().deleteAllFromRealm()
realm.where<SessionSet>().findAll().deleteAllFromRealm()
realm.where<FlatTimeInterval>().findAll().deleteAllFromRealm()
realm.where<Result>().findAll().deleteAllFromRealm()
realm.where<ComputableResult>().findAll().deleteAllFromRealm()
}
// Preferences.setBlogTipsTapped(requireContext())
// parentActivity?.openUrl(URL.BLOG_TIPS.value)
// displayBlogPostButton()
} }
binding.postButton.viewTreeObserver.addOnGlobalLayoutListener { binding.postButton.viewTreeObserver.addOnGlobalLayoutListener {
@ -345,6 +365,10 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
*/ */
private fun initData() { private fun initData() {
addRealmChangeListener(this, Session::class.java)
addRealmChangeListener(this, Transaction::class.java)
addRealmChangeListener(this, HandHistory::class.java)
this.currentFilterable = FilterableType.SESSION this.currentFilterable = FilterableType.SESSION
applyFilter() applyFilter()
} }
@ -452,6 +476,18 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
*/ */
private fun createNewTransaction() { private fun createNewTransaction() {
val sessions = getRealm().where(Session::class.java).findAll().map { it.id }
getRealm().executeTransactionAsync { r ->
for (id in sessions) {
r.findById<Session>(id)?.let { s ->
s.tableSize = 6
TimeManager.sessionDateChanged(s)
}
}
}
return
AppGuard.endOfUse?.let { endDate -> AppGuard.endOfUse?.let { endDate ->
if (Date().after(endDate)) { if (Date().after(endDate)) {
this.showEndOfUseMessage() this.showEndOfUseMessage()
@ -481,9 +517,8 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
// gets the first session of the adapter - the last created - to preconfigure the HH // gets the first session of the adapter - the last created - to preconfigure the HH
if (this.sessionAdapter.itemCount > 0) { if (this.sessionAdapter.itemCount > 0) {
this.sessionAdapter.sessionForPosition(0)?.let { session -> val session = this.sessionAdapter.sessionForPosition(0)
HandHistoryActivity.newInstance(this, session, false) HandHistoryActivity.newInstance(this, session, false)
} ?: throw PAIllegalStateException("Cannot happen")
} else { } else {
HandHistoryActivity.newInstance(this) HandHistoryActivity.newInstance(this)
} }
@ -493,9 +528,16 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
* Delete selected transaction * Delete selected transaction
*/ */
private fun deleteSelectedTransaction() { private fun deleteSelectedTransaction() {
getRealm().executeTransaction {
selectedTransaction?.deleteFromRealm() selectedTransaction?.id?.let { id ->
executeRealmAsyncTransaction { asyncRealm ->
asyncRealm.findById<Transaction>(id)?.deleteFromRealm()
} }
}
// getRealm().executeTransaction {
// selectedTransaction?.deleteFromRealm()
// }
selectedTransactionPosition = -1 selectedTransactionPosition = -1
} }
@ -612,7 +654,7 @@ class FeedFragment : FilterableFragment(), RowRepresentableDelegate, PurchaseLis
show = true show = true
this.badgeDrawable?.number = newCount this.badgeDrawable?.number = newCount
} }
this.binding.postButton.isVisible = show this.binding.postButton.isVisible = true
this.badgeDrawable?.isVisible = show this.badgeDrawable?.isVisible = show
} }

@ -137,8 +137,6 @@ class FeedHandHistoryRowRepresentableAdapter(
this.sortedHeaders = this.headersPositions.toSortedMap() this.sortedHeaders = this.headersPositions.toSortedMap()
// Timber.d("]]] this.sortedHeaders = ${this.sortedHeaders}")
} }
/** /**

@ -100,7 +100,7 @@ class FeedSessionRowRepresentableAdapter(
BindableHolder { BindableHolder {
fun bind(title: String) { fun bind(title: String) {
// Title // Title
itemView.findViewById<AppCompatTextView>(net.pokeranalytics.android.R.id.title)?.let { itemView.findViewById<AppCompatTextView>(R.id.title)?.let {
it.text = title it.text = title
} }
} }
@ -144,7 +144,7 @@ class FeedSessionRowRepresentableAdapter(
// If the header has no date, it's a pending session // If the header has no date, it's a pending session
return if (sortedHeaders[position] == null) { return if (sortedHeaders[position] == null) {
context.getString(net.pokeranalytics.android.R.string.pending) context.getString(R.string.pending)
} else { } else {
// Else, return the formatted date // Else, return the formatted date
sortedHeaders[position]?.getMonthAndYear() ?: throw PAIllegalStateException("Null date should not happen there") sortedHeaders[position]?.getMonthAndYear() ?: throw PAIllegalStateException("Null date should not happen there")
@ -153,14 +153,14 @@ class FeedSessionRowRepresentableAdapter(
throw PAIllegalStateException("Any position should always have a header, position = $position") throw PAIllegalStateException("Any position should always have a header, position = $position")
} }
fun sessionForPosition(position: Int): Session? { fun sessionForPosition(position: Int): Session {
return this.getSessionForPosition(position) return this.getSessionForPosition(position)
} }
/** /**
* Get real index * Get real index
*/ */
private fun getSessionForPosition(position: Int): Session? { private fun getSessionForPosition(position: Int): Session {
// Row position // Row position
var headersBefore = 0 var headersBefore = 0
@ -182,7 +182,7 @@ class FeedSessionRowRepresentableAdapter(
allSessions.clear() allSessions.clear()
allSessions.addAll(this.pendingSessions) allSessions.addAll(this.pendingSessions)
allSessions.addAll(this.startedSessions) allSessions.addAll(this.startedSessions)
Timber.d("Update session list, total count = ${allSessions.size}") // Timber.d("Update session list, total count = ${allSessions.size}")
val headersPositions = HashMap<Int, Date?>() val headersPositions = HashMap<Int, Date?>()
@ -210,7 +210,7 @@ class FeedSessionRowRepresentableAdapter(
} }
sortedHeaders = headersPositions.toSortedMap() sortedHeaders = headersPositions.toSortedMap()
Timber.d("Create viewTypesPositions in: ${System.currentTimeMillis() - start}ms") // Timber.d("Create viewTypesPositions in: ${System.currentTimeMillis() - start}ms")
} }
/** /**

@ -160,8 +160,6 @@ class FeedTransactionRowRepresentableAdapter(
*/ */
private fun checkHeaderCondition(currentCalendar: Calendar, previousYear: Int, previousMonth: Int): Boolean { private fun checkHeaderCondition(currentCalendar: Calendar, previousYear: Int, previousMonth: Int): Boolean {
return currentCalendar.get(Calendar.YEAR) == previousYear && currentCalendar.get(Calendar.MONTH) < previousMonth || (currentCalendar.get(Calendar.YEAR) < previousYear) return currentCalendar.get(Calendar.YEAR) == previousYear && currentCalendar.get(Calendar.MONTH) < previousMonth || (currentCalendar.get(Calendar.YEAR) < previousYear)
} }
} }

@ -9,10 +9,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.view.ViewAnimationUtils import android.view.ViewAnimationUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.pokeranalytics.android.databinding.ActivityNewDataBinding import net.pokeranalytics.android.databinding.ActivityNewDataBinding
import net.pokeranalytics.android.ui.activity.components.BaseActivity import net.pokeranalytics.android.ui.activity.components.BaseActivity
import net.pokeranalytics.android.ui.extensions.px import net.pokeranalytics.android.ui.extensions.px
@ -105,7 +102,7 @@ class NewDataMenuActivity : BaseActivity() {
val intent = Intent() val intent = Intent()
intent.putExtra(IntentKey.CHOICE.keyName, choice) intent.putExtra(IntentKey.CHOICE.keyName, choice)
setResult(RESULT_OK, intent) setResult(RESULT_OK, intent)
GlobalScope.launch(Dispatchers.Main) { CoroutineScope(Dispatchers.Main).launch {
delay(200) delay(200)
hideMenu() hideMenu()
} }

@ -90,17 +90,17 @@ open class FilterDetailsFragment : RealmFragment(), RowRepresentableDelegate {
this.arguments?.let { bundle -> this.arguments?.let { bundle ->
val filter = this.activityModel.currentFilter ?: throw PAIllegalStateException("Filter is null") val filter = this.activityModel.currentFilter
val category = bundle.getInt(BundleKey.DATA_TYPE.value) val category = bundle.getInt(BundleKey.DATA_TYPE.value)
val categoryRow = FilterCategoryRow.values()[category] val categoryRow = FilterCategoryRow.values()[category]
Timber.d("Category row = $categoryRow") Timber.d("Category row = $categoryRow")
val factory = FilterDetailsViewModelFactory(filter, categoryRow) val factory = FilterDetailsViewModelFactory(getRealm(), filter, categoryRow)
this.model = ViewModelProvider(this, factory).get(FilterDetailsViewModel::class.java) this.model = ViewModelProvider(this, factory).get(FilterDetailsViewModel::class.java)
} ?: throw PAIllegalStateException("Missing bundle") } ?: throw PAIllegalStateException("Missing bundle")
Timber.d(">> Filter = ${this.activityModel.currentFilter}") // Timber.d(">> Filter = ${this.activityModel.currentFilter}")
Timber.d("selectedRow = ${this.activityModel.selectedCategoryRow}") Timber.d("selectedRow = ${this.activityModel.selectedCategoryRow}")
this.binding.toolbar.title = this.activityModel.selectedCategoryRow?.localizedTitle(requireContext()) this.binding.toolbar.title = this.activityModel.selectedCategoryRow?.localizedTitle(requireContext())
@ -230,26 +230,32 @@ open class FilterDetailsFragment : RealmFragment(), RowRepresentableDelegate {
*/ */
private fun saveData() { private fun saveData() {
val currentFilter = this.activityModel.currentFilter val filter = this.activityModel.currentFilter
//TODO: Save currentFilter details data // this.activityModel.currentFilter?.id?.let { filterId ->
Timber.d("Save data for queryWith: ${currentFilter?.id}")
this.model.selectedRows.forEach {
Timber.d("Selected rows: $it")
}
this.activityModel.selectedCategoryRow?.let { category -> this.activityModel.selectedCategoryRow?.let { category ->
getRealm().executeTransaction { // getRealm().executeTransactionAsync { asyncRealm ->
currentFilter?.remove(category) // asyncRealm.findById<Filter>(filterId)?.let { filter ->
filter.remove(category)
val validConditions = this.model.selectedRows.filter { it.queryCondition != null } val validConditions = this.model.selectedRows.filter { it.queryCondition != null }
currentFilter?.createOrUpdateFilterConditions(validConditions) filter.createOrUpdateFilterConditions(validConditions)
}
} }
// }
// }
currentFilter?.filterConditions?.forEach { // }
Timber.d("Condition: $it")
} // //TODO: Save currentFilter details data
// Timber.d("Save data for queryWith: ${currentFilter?.id}")
// this.model.selectedRows.forEach {
// Timber.d("Selected rows: $it")
// }
// currentFilter?.filterConditions?.forEach {
// Timber.d("Condition: $it")
// }
// finishActivityWithResult(currentFilter?.id) // finishActivityWithResult(currentFilter?.id)
} }

@ -3,6 +3,7 @@ package net.pokeranalytics.android.ui.modules.filter
import android.content.Context import android.content.Context
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import io.realm.Realm
import net.pokeranalytics.android.model.realm.Filter import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
@ -13,25 +14,25 @@ import net.pokeranalytics.android.ui.view.rows.FilterItemRow
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import timber.log.Timber import timber.log.Timber
class FilterDetailsViewModelFactory(var filter: Filter, private var categoryRow: FilterCategoryRow): ViewModelProvider.Factory { class FilterDetailsViewModelFactory(var realm: Realm, var filter: Filter, private var categoryRow: FilterCategoryRow): ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return FilterDetailsViewModel(categoryRow, filter) as T return FilterDetailsViewModel(realm, categoryRow, filter) as T
} }
} }
class FilterDetailsViewModel(categoryRow: FilterCategoryRow, var filter: Filter) : ViewModel(), StaticRowRepresentableDataSource { class FilterDetailsViewModel(var realm: Realm, categoryRow: FilterCategoryRow, var filter: Filter) : ViewModel(), StaticRowRepresentableDataSource {
private var rows: ArrayList<RowRepresentable> = ArrayList() private var rows: ArrayList<RowRepresentable> = ArrayList()
val selectedRows = ArrayList<FilterItemRow>() val selectedRows = ArrayList<FilterItemRow>()
init { init {
this.rows.addAll(categoryRow.filterElements) this.rows.addAll(categoryRow.filterElements(realm))
this.defineSelectedItems() this.defineSelectedItems()
} }
override fun adapterRows(): List<RowRepresentable>? { override fun adapterRows(): List<RowRepresentable> {
return this.rows return this.rows
} }

@ -9,6 +9,7 @@ import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.enumerations.IntIdentifiable import net.pokeranalytics.android.util.enumerations.IntIdentifiable
import net.pokeranalytics.android.util.enumerations.IntSearchable import net.pokeranalytics.android.util.enumerations.IntSearchable
import timber.log.Timber
enum class FilterActivityRequestCode { enum class FilterActivityRequestCode {
SELECT_FILTER, SELECT_FILTER,
@ -45,9 +46,8 @@ interface FilterHandler {
fun saveFilter(context: Context, filterId: String) { fun saveFilter(context: Context, filterId: String) {
Preferences.setActiveFilterId(filterId, context) Preferences.setActiveFilterId(filterId, context)
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.executeTransaction { executeRealm -> realm.executeTransactionAsync { executeRealm ->
currentFilter(context, executeRealm)?.let { currentFilter(context, executeRealm)?.let {
it.useCount++ it.useCount++
} }

@ -12,7 +12,7 @@ import net.pokeranalytics.android.util.extensions.findById
class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource { class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource {
var currentFilter: Filter? = null var currentFilter: Filter = Filter()
// Main // Main
var filterableType: FilterableType? = null var filterableType: FilterableType? = null
@ -27,27 +27,27 @@ class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource {
fun init(realm: Realm) { fun init(realm: Realm) {
if (this.currentFilter != null) { // can be called twice and we don't want that // if (this.currentFilter != null) { // can be called twice and we don't want that
return // return
} // }
this.primaryKey?.let { this.primaryKey?.let {
val filter = realm.findById<Filter>(it) ?: throw PAIllegalStateException("Can't find filter with id=$it") val filter = realm.findById<Filter>(it) ?: throw PAIllegalStateException("Can't find filter with id=$it")
this.currentFilter = realm.copyFromRealm(filter) this.currentFilter = realm.copyFromRealm(filter)
this.isUpdating = true this.isUpdating = true
} ?: run { } ?: run {
this.filterableType?.uniqueIdentifier?.let { this.currentFilter.filterableTypeUniqueIdentifier = this.filterableType?.uniqueIdentifier
this.currentFilter = Filter.newInstance(it) //realm.copyFromRealm(Filter.newInstanceForResult(realm, this.filterableType.ordinal)) // this.filterableType?.uniqueIdentifier?.let {
} // this.currentFilter = Filter.newInstance(it) //realm.copyFromRealm(Filter.newInstanceForResult(realm, this.filterableType.ordinal))
// }
} }
// Create a copy if the user cancels the updates // Create a copy if the user cancels the updates
this.currentFilter?.let { // this.currentFilter?.let {
if (it.isValid && it.isManaged) { // if (it.isValid && it.isManaged) {
this.filterCopy = realm.copyFromRealm(it) // this.filterCopy = realm.copyFromRealm(it)
} // }
} // }
this.categoryRows.clear() this.categoryRows.clear()
@ -59,13 +59,13 @@ class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource {
// Data source // Data source
override fun adapterRows(): List<RowRepresentable>? { override fun adapterRows(): List<RowRepresentable> {
return this.categoryRows return this.categoryRows
} }
override fun charSequenceForRow(row: RowRepresentable, context: Context, tag: Int): CharSequence { override fun charSequenceForRow(row: RowRepresentable, context: Context, tag: Int): CharSequence {
// Return the number of selected filters for this category // Return the number of selected filters for this category
val count = this.currentFilter?.countBy(row as FilterCategoryRow) ?: 0 val count = this.currentFilter.countBy(row as FilterCategoryRow)
return if (count > 0) count.toString() else "" return if (count > 0) count.toString() else ""
} }

@ -15,7 +15,6 @@ import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.realm.Filter import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.extensions.px
import net.pokeranalytics.android.ui.fragment.components.RealmFragment import net.pokeranalytics.android.ui.fragment.components.RealmFragment
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.rows.FilterCategoryRow import net.pokeranalytics.android.ui.view.rows.FilterCategoryRow
@ -105,7 +104,7 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.save -> validateUpdates() R.id.save -> save()
} }
return true return true
} }
@ -156,10 +155,8 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
val categoryRow = row as FilterCategoryRow val categoryRow = row as FilterCategoryRow
this.model.selectedCategoryRow = categoryRow this.model.selectedCategoryRow = categoryRow
this.model.currentFilter?.let { _ ->
(activity as FiltersActivity).showDetailsFragment(categoryRow) (activity as FiltersActivity).showDetailsFragment(categoryRow)
} }
}
/** /**
* Update the most used filters chips * Update the most used filters chips
@ -217,18 +214,15 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
/** /**
* Validate the updates of the queryWith * Validate the updates of the queryWith
*/ */
private fun validateUpdates() { private fun save() {
val currentFilter = this.model.currentFilter val filter = this.model.currentFilter
getRealm().executeTransaction { realm -> executeRealmAsyncTransaction { asyncRealm ->
currentFilter?.let { filter.name = filter.query.getName(requireContext())
it.name = it.query.getName(requireContext()) asyncRealm.insertOrUpdate(filter)
realm.copyToRealmOrUpdate(it)
}
} }
val filterId = currentFilter?.id ?: "" finishActivityWithResult(filter.id)
finishActivityWithResult(filterId)
} }
/** /**
@ -239,9 +233,9 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
val filterCopy = this.model.filterCopy val filterCopy = this.model.filterCopy
val filterId = filterCopy?.id ?: "" val filterId = filterCopy?.id ?: ""
getRealm().executeTransaction { realm -> executeRealmAsyncTransaction { realm ->
filterCopy?.let { filterCopy?.let {
realm.copyToRealmOrUpdate(it) realm.insertOrUpdate(it)
} }
} }
finishActivityWithResult(filterId) finishActivityWithResult(filterId)

@ -15,8 +15,10 @@ open class FiltersListFragment : DataListFragment() {
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
when (row) { when (row) {
is Filter -> { is Filter -> {
getRealm().executeTransaction {
row.updateValue(value, row) row.updateValue(value, row)
executeRealmAsyncTransaction { asyncRealm ->
asyncRealm.insertOrUpdate(row)
} }
val index = this.model.items.indexOf(row) val index = this.model.items.indexOf(row)
this.dataListAdapter.notifyItemChanged(index) this.dataListAdapter.notifyItemChanged(index)

@ -202,12 +202,15 @@ class EditorFragment : RealmFragment(), RowRepresentableDelegate, KeyboardListen
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
val playerId = data?.getStringExtra(BaseFragment.BundleKey.PRIMARY_KEY.value) val playerId = data?.getStringExtra(BaseFragment.BundleKey.PRIMARY_KEY.value)
?: throw PAIllegalStateException("Primary key not set where as activity has finished") ?: throw PAIllegalStateException("Primary key not set where as activity has finished")
getRealm().findById<Player>(playerId)?.let { player ->
this.model.playerSelected(player) val realm = getRealm()
realm.findById<Player>(playerId)?.let { player ->
val p = realm.copyFromRealm(player)
this.model.playerSelected(p)
} ?: throw PAIllegalStateException("Player (id=$playerId) not found") } ?: throw PAIllegalStateException("Player (id=$playerId) not found")
this.editorAdapter.notifyDataSetChanged() this.editorAdapter.notifyDataSetChanged()
} }
} }
} }
@ -684,10 +687,9 @@ class EditorFragment : RealmFragment(), RowRepresentableDelegate, KeyboardListen
*/ */
private fun deleteHand() { private fun deleteHand() {
getRealm().findById<HandHistory>(this.model.handHistory.id)?.let { hh -> val hhId = this.model.handHistory.id
getRealm().executeTransaction { executeRealmAsyncTransaction { asyncRealm ->
hh.deleteFromRealm() asyncRealm.findById<HandHistory>(hhId)?.deleteFromRealm()
}
} }
this.activity?.finish() this.activity?.finish()

@ -5,10 +5,7 @@ import android.text.InputType
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.handhistory.HandSetup import net.pokeranalytics.android.model.handhistory.HandSetup
@ -27,12 +24,10 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.rows.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rows.CustomizableRowRepresentable
import net.pokeranalytics.android.util.CrashLogging import net.pokeranalytics.android.util.CrashLogging
import net.pokeranalytics.android.util.extensions.findById
import net.pokeranalytics.android.util.extensions.formatted import net.pokeranalytics.android.util.extensions.formatted
import timber.log.Timber import timber.log.Timber
import java.text.DecimalFormat import java.text.DecimalFormat
import java.text.ParseException import java.text.ParseException
import kotlin.coroutines.CoroutineContext
import kotlin.reflect.KClass import kotlin.reflect.KClass
enum class HHKeyboard { enum class HHKeyboard {
@ -49,9 +44,6 @@ interface PlayerSetupCreationListener {
class EditorViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer, ActionListListener { class EditorViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer, ActionListListener {
private val coroutineContext: CoroutineContext
get() = Dispatchers.Main
/*** /***
* The hand history * The hand history
*/ */
@ -641,33 +633,30 @@ class EditorViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer
* Saves the current hand state in the database * Saves the current hand state in the database
*/ */
fun save(realm: Realm) { fun save(realm: Realm) {
realm.executeTransaction {
this.handHistory.actions.clear() this.handHistory.actions.clear()
val actions = this.sortedActions.map { it.action } val actions = this.sortedActions.map { it.action }
this.handHistory.actions.addAll(actions) this.handHistory.actions.addAll(actions)
if (!this.handHistory.isManaged) { realm.executeTransactionAsync { asyncRealm ->
realm.copyToRealmOrUpdate(this.handHistory) this.handHistory.defineWinnerPositions()
} asyncRealm.insertOrUpdate(this.handHistory)
}
this.defineWinnerPositions()
}
private fun defineWinnerPositions() {
val hhId = this.handHistory.id
GlobalScope.launch(coroutineContext) {
val c = GlobalScope.async {
val realm = Realm.getDefaultInstance()
realm.executeTransaction {
realm.findById<HandHistory>(hhId)?.defineWinnerPositions()
}
realm.close()
} }
c.await()
} }
} // private fun defineWinnerPositions() {
//
// val hhId = this.handHistory.id
// CoroutineScope(Dispatchers.Default).launch {
// val realm = Realm.getDefaultInstance()
//
// realm.findById<HandHistory>(hhId)?.defineWinnerPositions()
// realm.executeTransactionAsync { execRealm ->
// execRealm.copyToRealmOrUpdate(handHistory)
// }
// realm.close()
// }
//
// }
// Card Centralizer // Card Centralizer
@ -1013,16 +1002,16 @@ class EditorViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer
fun playerSelected(player: Player) { fun playerSelected(player: Player) {
// Remove all use of the selected player // Remove all use of the selected player
this.handHistory.playerSetups.filter { it.player == player }.forEach { this.handHistory.playerSetups.filter { it.player?.id == player.id }.forEach {
it.player = null it.player = null
} }
// Affects the player to the selected position // Affects the player to the selected position
this.tappedPlayerPositionIndex?.let { positionIndex -> this.tappedPlayerPositionIndex?.let { positionIndex ->
player.realm.executeTransaction { // player.realm.executeTransaction {
val ps = this.handHistory.playerSetupForPosition(positionIndex) ?: this.handHistory.createPlayerSetup(positionIndex) val ps = this.handHistory.playerSetupForPosition(positionIndex) ?: this.handHistory.createPlayerSetup(positionIndex)
ps.player = player ps.player = player
} // }
} ?: throw PAIllegalStateException("Click position not set for player selection") } ?: throw PAIllegalStateException("Click position not set for player selection")
} }
@ -1036,20 +1025,20 @@ class EditorViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer
/*** /***
* Tries to deletes the row at the given [position] * Tries to deletes the row at the given [position]
*/ */
fun deleteIfPossible(position: Int) { // fun deleteIfPossible(position: Int) {
when (val row = this.rowRepresentables[position]) { // when (val row = this.rowRepresentables[position]) {
is PlayerSetupRow -> { // is PlayerSetupRow -> {
val playerSetup = this.handHistory.playerSetupForPosition(row.positionIndex) ?: throw PAIllegalStateException("Attempt to delete an null object") // val playerSetup = this.handHistory.playerSetupForPosition(row.positionIndex) ?: throw PAIllegalStateException("Attempt to delete an null object")
this.handHistory.playerSetups.remove(playerSetup) // this.handHistory.playerSetups.remove(playerSetup)
//
this.handHistory.realm?.let { // this.handHistory.realm?.let {
it.executeTransaction { // it.executeTransaction {
playerSetup.deleteFromRealm() // playerSetup.deleteFromRealm()
} // }
} // }
} // }
} // }
} // }
fun removePlayerSetup(positionIndex: Int) { fun removePlayerSetup(positionIndex: Int) {
val ps = this.handHistory.playerSetupForPosition(positionIndex) val ps = this.handHistory.playerSetupForPosition(positionIndex)

@ -13,7 +13,9 @@ import android.provider.MediaStore
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import com.arthenica.ffmpegkit.FFmpegKit import com.arthenica.ffmpegkit.FFmpegKit
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
@ -26,7 +28,6 @@ import timber.log.Timber
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.util.* import java.util.*
import kotlin.coroutines.CoroutineContext
enum class FileType(var value: String) { enum class FileType(var value: String) {
IMAGE_GIF("image/gif"), IMAGE_GIF("image/gif"),
@ -39,9 +40,6 @@ class ReplayExportService : Service() {
private val binder = LocalBinder() private val binder = LocalBinder()
private val coroutineContext: CoroutineContext
get() = Dispatchers.Main
override fun onBind(intent: Intent?): IBinder { override fun onBind(intent: Intent?): IBinder {
return binder return binder
} }
@ -70,13 +68,13 @@ class ReplayExportService : Service() {
private fun startGIFExport() { private fun startGIFExport() {
GlobalScope.launch(coroutineContext) { CoroutineScope(Dispatchers.Default).launch {
val c = GlobalScope.async {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.refresh() realm.refresh()
val handHistory = realm.findById<HandHistory>(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") val handHistory = realm.findById<HandHistory>(handHistoryId)
?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
val context = this@ReplayExportService val context = this@ReplayExportService
@ -96,7 +94,8 @@ class ReplayExportService : Service() {
val resolver = applicationContext.contentResolver val resolver = applicationContext.contentResolver
// Q version tested before calling the function // Q version tested before calling the function
val imageCollection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) val imageCollection =
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val gifDetails = ContentValues().apply { val gifDetails = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, fileName) put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
@ -139,18 +138,16 @@ class ReplayExportService : Service() {
Timber.w("Resolver insert ended without uri...") Timber.w("Resolver insert ended without uri...")
} }
} }
c.await()
}
} }
private fun startFFMPEGVideoExport() { private fun startFFMPEGVideoExport() {
GlobalScope.launch(coroutineContext) { CoroutineScope(Dispatchers.Default).launch {
val async = GlobalScope.async {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val handHistory = realm.findById<HandHistory>(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") val handHistory = realm.findById<HandHistory>(handHistoryId)
?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
val context = this@ReplayExportService val context = this@ReplayExportService
@ -173,12 +170,14 @@ class ReplayExportService : Service() {
val formattedDate = Date().dateTimeFileFormatted val formattedDate = Date().dateTimeFileFormatted
val fileName = "hand_${formattedDate}.mp4" val fileName = "hand_${formattedDate}.mp4"
val outputDirectory = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES) ?: throw PAIllegalStateException("File is invalid") val outputDirectory = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES)
?: throw PAIllegalStateException("File is invalid")
val output = "${outputDirectory.path}/$fileName" val output = "${outputDirectory.path}/$fileName"
Timber.d("Assembling images for video...") Timber.d("Assembling images for video...")
val command = "-f concat -safe 0 -i $dpath -vb 20M -vsync vfr -s ${width}x${height} -vf fps=20 -pix_fmt yuv420p $output" val command =
"-f concat -safe 0 -i $dpath -vb 20M -vsync vfr -s ${width}x${height} -vf fps=20 -pix_fmt yuv420p $output"
FFmpegKit.executeAsync(command) { FFmpegKit.executeAsync(command) {
when { when {
@ -189,7 +188,7 @@ class ReplayExportService : Service() {
Timber.d("Command execution cancelled by user.") Timber.d("Command execution cancelled by user.")
} }
else -> { else -> {
Timber.d(String.format("Command execution failed with rc=%d and the output below.", it.returnCode.value)) Timber.d("Command execution failed with rc=${it.returnCode.value} and the output below.")
} }
} }
@ -201,7 +200,8 @@ class ReplayExportService : Service() {
val resolver = applicationContext.contentResolver val resolver = applicationContext.contentResolver
// Q version tested before calling the function // Q version tested before calling the function
val videoCollection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) val videoCollection =
MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val fileDetails = ContentValues().apply { val fileDetails = ContentValues().apply {
Timber.d("set file details = $fileName") Timber.d("set file details = $fileName")
@ -229,66 +229,18 @@ class ReplayExportService : Service() {
} }
} }
async.await()
}
} }
// private fun startVideoExport() {
//
// GlobalScope.launch(coroutineContext) {
// val c = GlobalScope.async {
//
// val realm = Realm.getDefaultInstance()
// val handHistory = realm.findById<HandHistory>(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
//
// val context = this@ReplayExportService
//
// val animator = ReplayerAnimator(handHistory, true)
//
// val square = 1024
//
// val width = square
// val height = square
//
// animator.setDimension(width.toFloat(), height.toFloat())
// TableDrawer.configurePaints(context, animator)
//
// val muxer = MMediaMuxer()
// muxer.init(null, width, height, "hhVideo", "YES!")
//
// animator.frames(context) { bitmap, count ->
//
// try {
// val byteArray = bitmap.toByteArray()
// muxer.addFrame(byteArray, count, false)
// } catch (e: Exception) {
// Timber.e("error = ${e.message}")
// }
//
// }
//
// realm.close()
//
// muxer.createVideo { path ->
// notifyUser(path)
// }
//
// }
// c.await()
// }
//
// }
private fun startGIFExportPreQ() { private fun startGIFExportPreQ() {
GlobalScope.launch(coroutineContext) { CoroutineScope(Dispatchers.Default).launch {
val c = GlobalScope.async {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.refresh() realm.refresh()
val handHistory = realm.findById<HandHistory>(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") val handHistory = realm.findById<HandHistory>(handHistoryId)
?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
val context = this@ReplayExportService val context = this@ReplayExportService
@ -337,18 +289,18 @@ class ReplayExportService : Service() {
notifyUser(path) notifyUser(path)
} }
c.await()
}
} }
private fun startFFMPEGVideoExportPreQ() { private fun startFFMPEGVideoExportPreQ() {
GlobalScope.launch(coroutineContext) { CoroutineScope(Dispatchers.Default).launch {
val async = GlobalScope.async {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val handHistory = realm.findById<HandHistory>(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") realm.refresh() // Fixes crash right below
val handHistory = realm.findById<HandHistory>(handHistoryId)
?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
val context = this@ReplayExportService val context = this@ReplayExportService
@ -378,8 +330,8 @@ class ReplayExportService : Service() {
Timber.d("Assembling images for video...") Timber.d("Assembling images for video...")
val command =
val command = "-f concat -safe 0 -i $dpath -vb 20M -vsync vfr -s ${width}x${height} -vf fps=20 -pix_fmt yuv420p $output" "-f concat -safe 0 -i $dpath -vb 20M -vsync vfr -s ${width}x${height} -vf fps=20 -pix_fmt yuv420p $output"
FFmpegKit.executeAsync(command) { FFmpegKit.executeAsync(command) {
when { when {
@ -390,7 +342,7 @@ class ReplayExportService : Service() {
Timber.d("Command execution cancelled by user.") Timber.d("Command execution cancelled by user.")
} }
else -> { else -> {
Timber.d(String.format("Command execution failed with rc=%d and the output below.", it.returnCode.value)) Timber.d("Command execution failed with rc=${it.returnCode.value} and the output below.")
} }
} }
@ -411,8 +363,6 @@ class ReplayExportService : Service() {
} }
} }
async.await()
}
} }
@ -454,7 +404,8 @@ class ReplayExportService : Service() {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooser = Intent.createChooser(intent, getString(R.string.open_file_with)) val chooser = Intent.createChooser(intent, getString(R.string.open_file_with))
val pendingIntent = PendingIntent.getActivity(this, 0, chooser, PendingIntent.FLAG_CANCEL_CURRENT) val pendingIntent =
PendingIntent.getActivity(this, 0, chooser, PendingIntent.FLAG_CANCEL_CURRENT)
TriggerNotification(this, title, body, pendingIntent) TriggerNotification(this, title, body, pendingIntent)

@ -12,8 +12,7 @@ import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.bankroll.BankrollReportManager import net.pokeranalytics.android.calculus.bankroll.BankrollReportManager
@ -38,6 +37,7 @@ import net.pokeranalytics.android.ui.modules.datalist.DataListActivity
import net.pokeranalytics.android.ui.modules.handhistory.HandHistoryActivity import net.pokeranalytics.android.ui.modules.handhistory.HandHistoryActivity
import net.pokeranalytics.android.ui.view.* import net.pokeranalytics.android.ui.view.*
import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow
import net.pokeranalytics.android.ui.viewmodel.AddedDataViewModel
import net.pokeranalytics.android.util.CrashLogging import net.pokeranalytics.android.util.CrashLogging
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.extensions.* import net.pokeranalytics.android.util.extensions.*
@ -48,6 +48,10 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
private lateinit var model: SessionViewModel private lateinit var model: SessionViewModel
private val addedDataViewModel: AddedDataViewModel by lazy {
ViewModelProvider(requireActivity()).get(AddedDataViewModel::class.java)
}
companion object { companion object {
const val TIMER_DELAY = 5000L const val TIMER_DELAY = 5000L
const val REQUEST_CODE_NEW_CUSTOM_FIELD = 1000 const val REQUEST_CODE_NEW_CUSTOM_FIELD = 1000
@ -58,7 +62,6 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
private var sessionMenu: Menu? = null private var sessionMenu: Menu? = null
private val oldRows: ArrayList<RowRepresentable> = ArrayList() private val oldRows: ArrayList<RowRepresentable> = ArrayList()
private var sessionHasBeenUserCustomized = false
private val handler: Handler = Handler() private val handler: Handler = Handler()
private val timer: Runnable = object : Runnable { private val timer: Runnable = object : Runnable {
@ -104,13 +107,21 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
loadOrCreateSession() loadOrCreateSession()
initUI() initUI()
initData()
} }
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
this.addedDataViewModel.data.removeObservers(this)
_binding = null _binding = null
} }
private fun initData() {
this.addedDataViewModel.data.observeForever {
this.onRowValueChanged(it, this.addedDataViewModel.dataIdentifier)
}
}
/** /**
* Init UI * Init UI
*/ */
@ -134,7 +145,6 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
binding.floatingActionButton.setOnClickListener { binding.floatingActionButton.setOnClickListener {
if (this.currentSession.isValidForSave()) { if (this.currentSession.isValidForSave()) {
sessionHasBeenUserCustomized = true
startPauseResumeSession() startPauseResumeSession()
} else { } else {
val builder = AlertDialog.Builder(requireContext()) val builder = AlertDialog.Builder(requireContext())
@ -178,40 +188,41 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
if (sessionId != null) { if (sessionId != null) {
val sessionRealm = realm.findById<Session>(sessionId) val sessionRealm = realm.findById<Session>(sessionId)
if (sessionRealm != null) { sessionRealm?.let {
val copy = realm.copyFromRealm(it)
if (this.model.duplicate) { // duplicate session if (this.model.duplicate) { // duplicate session
realm.executeTransaction { // realm.executeTransaction {
val session = sessionRealm.duplicate() val session = copy.duplicate()
currentSession = session currentSession = session
} // }
sessionHasBeenUserCustomized = false
} else { // show existing session } 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")
} else { // create new session } else { // create new session
realm.executeTransaction { executeRealm ->
currentSession = Session.newInstance(executeRealm, this.model.isTournament) currentSession = Session.newInstance(realm, this.model.isTournament)
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, null, requireContext()) 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 // Find the nearest location around the user
parentActivity?.findNearestLocation { parentActivity?.findNearestLocation {
it?.let { location -> it?.let { location ->
realm.executeTransaction { executeRealm -> // realm.executeTransaction { executeRealm ->
val realmLocation = executeRealm.findById<Location>(location.id) val realmLocation = realm.findById<Location>(location.id)
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, realmLocation, requireContext()) FavoriteSessionFinder.copyParametersFromFavoriteSession(realm, currentSession, realmLocation, requireContext())
currentSession.location = realmLocation currentSession.location = realmLocation
} // }
this.updateSessionUI(true) this.updateSessionUI(true)
} }
} }
sessionHasBeenUserCustomized = false
} }
} }
@ -221,6 +232,8 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
return return
} }
this.addedDataViewModel.dataIdentifier = row
val session = this.currentSession val session = this.currentSession
val data = this.editDescriptors(row) val data = this.editDescriptors(row)
@ -253,15 +266,24 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
} }
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
this.sessionHasBeenUserCustomized = true
try { Timber.d("value changed: $value, row: $row")
getRealm().executeTransaction {
this.currentSession.updateValue(value, row) updateSessionThenSaveAsynchronously { session ->
} session.updateValue(value, row)
} catch (e: PAIllegalStateException) {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
return
} }
// Timber.d("val = ${this.currentSession.customFieldEntries}")
// 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) this.sessionAdapter.refreshRow(row)
when (row) { when (row) {
SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE, SessionPropertiesRow.NET_RESULT, SessionPropertiesRow.CASHED_OUT, SessionPropertiesRow.PRIZE, SessionPropertiesRow.NET_RESULT,
@ -275,6 +297,25 @@ 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
}
executeRealmAsyncTransaction { realm ->
val session = realm.copyToRealmOrUpdate(this.currentSession)
session.preCompute()
for (customField in this.model.customFields) {
realm.insertOrUpdate(customField)
}
}
}
/** /**
* Update the UI with the session data * Update the UI with the session data
* Should be called after the initialization of the session * Should be called after the initialization of the session
@ -388,12 +429,20 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
} }
} }
currentSession.startOrContinue() updateSessionThenSaveAsynchronously { session ->
session.startOrContinue()
}
// currentSession.startOrContinue()
binding.recyclerView.smoothScrollToPosition(0) binding.recyclerView.smoothScrollToPosition(0)
} }
SessionState.STARTED -> { SessionState.STARTED -> {
currentSession.pause() updateSessionThenSaveAsynchronously { session ->
session.pause()
}
// currentSession.pause()
} }
else -> { else -> {
} }
@ -406,14 +455,13 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
Timber.d("Start optimal duration finding attempt...") Timber.d("Start optimal duration finding attempt...")
val isLive = this.currentSession.isLive val isLive = this.currentSession.isLive
CoroutineScope(coroutineContext).launch { CoroutineScope(Dispatchers.Default).launch {
var optimalDuration: Double? = null
val cr = GlobalScope.async { val optimalDuration: Double? = CashGameOptimalDurationCalculator.start(isLive)
optimalDuration = CashGameOptimalDurationCalculator.start(isLive) // optimalDuration =
} // val cr = GlobalScope.async {
cr.await() // }
// cr.await()
if (!isDetached) { if (!isDetached) {
optimalDuration?.let { optimalDuration?.let {
@ -421,11 +469,14 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
val delay = it.toLong() //5000L val delay = it.toLong() //5000L
currentSession.scheduleStopNotification(requireContext(), delay) currentSession.scheduleStopNotification(requireContext(), delay)
launch(Dispatchers.Main) {
val formattedDuration = (it / 3600 / 1000).formattedHourlyDuration() val formattedDuration = (it / 3600 / 1000).formattedHourlyDuration()
Timber.d("Setting stop notification in: $formattedDuration") Timber.d("Setting stop notification in: $formattedDuration")
val message = requireContext().getString(R.string.stop_notification_in_, formattedDuration) val message = requireContext().getString(R.string.stop_notification_in_, formattedDuration)
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show() Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
} }
}
} }
} }
@ -435,7 +486,9 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
* Stop the current session * Stop the current session
*/ */
private fun stopSession() { private fun stopSession() {
this.currentSession.stop(requireContext()) updateSessionThenSaveAsynchronously { session ->
session.stop(requireContext())
}
updateSessionUI() updateSessionUI()
} }
@ -454,7 +507,9 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
* Restart timer * Restart timer
*/ */
private fun restartTimer() { private fun restartTimer() {
currentSession.restart() updateSessionThenSaveAsynchronously { session ->
session.restart()
}
updateSessionUI() updateSessionUI()
} }
@ -478,22 +533,30 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
val bankrollId = this.currentSession.bankroll?.id val bankrollId = this.currentSession.bankroll?.id
this.currentSession.delete() val sessionId = this.currentSession.id
executeRealmAsyncTransaction { asyncRealm ->
asyncRealm.findById<Session>(sessionId)?.delete()
}
// this.currentSession.delete()
bankrollId?.let { bankrollId?.let {
BankrollReportManager.notifyBankrollReportImpact(bankrollId) BankrollReportManager.notifyBankrollReportImpact(bankrollId)
} }
activity?.finish() activity?.finish()
} }
/** // /**
* Called when the user pressed back on the parent activity // * Called when the user pressed back on the parent activity
*/ // */
override fun onBackPressed() { // override fun onBackPressed() {
super.onBackPressed() // super.onBackPressed()
if (!sessionHasBeenUserCustomized) { // if (!sessionHasBeenUserCustomized) {
currentSession.delete() // val sessionId = currentSession.id
} // getRealm().executeTransactionAsync { asyncRealm ->
} // asyncRealm.findById<Session>(sessionId)?.delete()
// }
// }
// }
//// Static Data Source //// Static Data Source
@ -641,15 +704,16 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
) )
) )
is CustomField -> { is CustomField -> {
val entry = row.entries.intersect(session.customFieldEntries).firstOrNull()
row.editingDescriptors( row.editingDescriptors(
when (row.type) { when (row.type) {
CustomField.Type.LIST.uniqueIdentifier -> mapOf( CustomField.Type.LIST.uniqueIdentifier -> {
"defaultValue" to session.customFieldEntries.find { it.customField?.id == row.id }?.value, mapOf(
"defaultValue" to entry?.value,
"data" to row.entries "data" to row.entries
) )
else -> mapOf( }
"defaultValue" to session.customFieldEntries.find { it.customField?.id == row.id }?.numericValue else -> mapOf("defaultValue" to entry?.numericValue)
)
} }
) )
} }
@ -659,7 +723,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr
override fun resultCaptureTypeSelected(resultCaptureType: ResultCaptureType, applyBankroll: Boolean) { override fun resultCaptureTypeSelected(resultCaptureType: ResultCaptureType, applyBankroll: Boolean) {
getRealm().executeTransaction { // cleanup existing results updateSessionThenSaveAsynchronously {
when (resultCaptureType) { when (resultCaptureType) {
ResultCaptureType.NET_RESULT -> { ResultCaptureType.NET_RESULT -> {
this.currentSession.clearBuyinCashedOut() this.currentSession.clearBuyinCashedOut()

@ -46,6 +46,11 @@ class SessionViewModel : ViewModel() {
*/ */
var rows: List<RowRepresentable> = mutableListOf() var rows: List<RowRepresentable> = mutableListOf()
/**
* The list of customFields
*/
var customFields: List<CustomField> = listOf()
fun rowForPosition(position: Int): RowRepresentable { fun rowForPosition(position: Int): RowRepresentable {
return this.rows[position] return this.rows[position]
} }
@ -104,7 +109,10 @@ class SessionViewModel : ViewModel() {
// Add custom fields // Add custom fields
rows.add(SeparatorRow()) rows.add(SeparatorRow())
rows.addAll(realm.sorted<CustomField>())
val customFields = realm.sorted<CustomField>()
this.customFields = customFields.map { realm.copyFromRealm(it) }
rows.addAll(this.customFields)
this.rows = rows this.rows = rows
} }

@ -49,24 +49,28 @@ class DealtHandsPerHourFragment : RealmFragment() {
setDisplayHomeAsUpEnabled(true) setDisplayHomeAsUpEnabled(true)
val userConfig = UserConfig.getConfiguration(this.getRealm()) UserConfig.getConfiguration(this.getRealm()) { userConfig ->
this.liveValue.hint = "${userConfig.liveDealtHandsPerHour}" this.liveValue.hint = "${userConfig.liveDealtHandsPerHour}"
this.onlineValue.hint = "${userConfig.onlineDealtHandsPerHour}" this.onlineValue.hint = "${userConfig.onlineDealtHandsPerHour}"
}
} }
private fun save() { private fun save() {
getRealm().executeTransaction { realm -> executeRealmAsyncTransaction { realm ->
val userConfig = UserConfig.getConfiguration(realm) UserConfig.getConfiguration(realm) { userConfig ->
this.liveValue.text.toString().toIntOrNull()?.let { liveDealtHandsPerHour -> this.liveValue.text.toString().toIntOrNull()?.let { liveDealtHandsPerHour ->
userConfig.liveDealtHandsPerHour = liveDealtHandsPerHour userConfig.liveDealtHandsPerHour = liveDealtHandsPerHour
} }
this.onlineValue.text.toString().toIntOrNull()?.let { onlineDealtHandsPerHour -> this.onlineValue.text.toString().toIntOrNull()?.let { onlineDealtHandsPerHour ->
userConfig.onlineDealtHandsPerHour = onlineDealtHandsPerHour userConfig.onlineDealtHandsPerHour = onlineDealtHandsPerHour
} }
realm.copyToRealmOrUpdate(userConfig) realm.insertOrUpdate(userConfig)
}
// val userConfig = UserConfig.getConfiguration(realm)
// update all precomputed hand counts // update all precomputed hand counts
realm.where(ComputableResult::class.java).findAll().forEach { cr -> realm.where(ComputableResult::class.java).findAll().forEach { cr ->

@ -90,16 +90,18 @@ class TransactionFilterFragment : RealmFragment(), StaticRowRepresentableDataSou
.findAll() .findAll()
this.model.transactionTypes = transactionTypes this.model.transactionTypes = transactionTypes
val userConfig = UserConfig.getConfiguration(this.getRealm()) UserConfig.getConfiguration(this.getRealm()) { userConfig ->
this.model.selectedTransactionTypes = userConfig.transactionTypes(getRealm()).toMutableSet() this.model.selectedTransactionTypes = userConfig.transactionTypes(getRealm()).toMutableSet()
}
} }
private fun save() { private fun save() {
getRealm().executeTransaction { realm -> executeRealmAsyncTransaction { asyncRealm ->
val userConfig = UserConfig.getConfiguration(realm) UserConfig.getConfiguration(asyncRealm) { userConfig ->
userConfig.setTransactionTypeIds(this.model.selectedTransactionTypes) userConfig.setTransactionTypeIds(this.model.selectedTransactionTypes)
realm.copyToRealmOrUpdate(userConfig) asyncRealm.insertOrUpdate(userConfig)
}
} }
this.activity?.finish() this.activity?.finish()
} }

@ -60,14 +60,13 @@ enum class FilterCategoryRow(override val resId: Int?, override val viewType: In
} }
} }
val filterElements: List<RowRepresentable> fun filterElements(realm: Realm): List<RowRepresentable> {
get() {
return filterSectionRows.flatMap { return filterSectionRows.flatMap {
val items = it.filterItems val items = it.filterItems(realm)
val list = mutableListOf<RowRepresentable>() val list = mutableListOf<RowRepresentable>()
if (items.isNotEmpty()) { if (items.isNotEmpty()) {
list.add(it) list.add(it)
list.addAll(it.filterItems) list.addAll(it.filterItems(realm))
} }
list list
} }

@ -1,6 +1,7 @@
package net.pokeranalytics.android.ui.view.rows package net.pokeranalytics.android.ui.view.rows
import android.content.Context import android.content.Context
import io.realm.Realm
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PokerAnalyticsException import net.pokeranalytics.android.exceptions.PokerAnalyticsException
import net.pokeranalytics.android.model.Criteria import net.pokeranalytics.android.model.Criteria
@ -71,9 +72,8 @@ sealed class FilterSectionRow(override val resId: Int?) : RowRepresentable {
val allowMultiSelection: Boolean val allowMultiSelection: Boolean
get() = (this.selectionType == SelectionType.MULTIPLE) get() = (this.selectionType == SelectionType.MULTIPLE)
val filterItems: List<FilterItemRow> fun filterItems(realm: Realm): List<FilterItemRow> {
get() { return this.queryConditions(realm).map {
return this.queryConditions.map {
rowWrapper(it, this@FilterSectionRow) rowWrapper(it, this@FilterSectionRow)
// it.toRowWrapper(this@FilterSectionRow) // it.toRowWrapper(this@FilterSectionRow)
} }
@ -101,15 +101,14 @@ sealed class FilterSectionRow(override val resId: Int?) : RowRepresentable {
} }
} }
private val queryConditions: List<QueryCondition> private fun queryConditions(realm: Realm): List<QueryCondition> {
get() {
return when (this@FilterSectionRow) { return when (this@FilterSectionRow) {
// General // General
CashOrTournament -> Criteria.SessionTypes.queryConditions.mapFirstCondition() CashOrTournament -> Criteria.SessionTypes.queryConditions(realm).mapFirstCondition()
LiveOrOnline -> Criteria.BankrollTypes.queryConditions.mapFirstCondition() LiveOrOnline -> Criteria.BankrollTypes.queryConditions(realm).mapFirstCondition()
Game -> Criteria.Games.queryConditions.mapFirstCondition() Game -> Criteria.Games.queryConditions(realm).mapFirstCondition()
LimitType -> Criteria.Limits.queryConditions.mapFirstCondition() LimitType -> Criteria.Limits.queryConditions(realm).mapFirstCondition()
TableSize -> Criteria.TableSizes.queryConditions.mapFirstCondition() TableSize -> Criteria.TableSizes.queryConditions(realm).mapFirstCondition()
// Date // Date
DynamicDate -> arrayListOf( DynamicDate -> arrayListOf(
QueryCondition.IsToday, QueryCondition.IsToday,
@ -120,9 +119,9 @@ sealed class FilterSectionRow(override val resId: Int?) : RowRepresentable {
QueryCondition.DuringThisYear QueryCondition.DuringThisYear
) )
WeekdayOrWeekend -> arrayListOf(QueryCondition.IsWeekDay, QueryCondition.IsWeekEnd) WeekdayOrWeekend -> arrayListOf(QueryCondition.IsWeekDay, QueryCondition.IsWeekEnd)
Year -> Criteria.Years.queryConditions.mapFirstCondition() Year -> Criteria.Years.queryConditions(realm).mapFirstCondition()
DayOfWeek -> Criteria.DaysOfWeek.queryConditions.mapFirstCondition() DayOfWeek -> Criteria.DaysOfWeek.queryConditions(realm).mapFirstCondition()
MonthOfYear -> Criteria.MonthsOfYear.queryConditions.mapFirstCondition() MonthOfYear -> Criteria.MonthsOfYear.queryConditions(realm).mapFirstCondition()
// Duration // Duration
SessionDuration -> QueryCondition.moreEqualOrLessEqual<QueryCondition.Duration>() SessionDuration -> QueryCondition.moreEqualOrLessEqual<QueryCondition.Duration>()
@ -135,20 +134,20 @@ sealed class FilterSectionRow(override val resId: Int?) : RowRepresentable {
//Sessions -> arrayListOf(QueryCondition.LastGame(), QueryCondition.LastSession()) //Sessions -> arrayListOf(QueryCondition.LastGame(), QueryCondition.LastSession())
// Cash // Cash
Stakes -> Criteria.Stakes.queryConditions.mapFirstCondition() Stakes -> Criteria.Stakes.queryConditions(realm).mapFirstCondition()
// CashRebuyCount -> QueryCondition.moreOrLess<QueryCondition.Rebuy>() // CashRebuyCount -> QueryCondition.moreOrLess<QueryCondition.Rebuy>()
// Tournament // Tournament
TournamentType -> Criteria.TournamentTypes.queryConditions.mapFirstCondition() TournamentType -> Criteria.TournamentTypes.queryConditions(realm).mapFirstCondition()
// CompletionPercentage -> arrayListOf() // CompletionPercentage -> arrayListOf()
TournamentFinalPosition -> QueryCondition.moreEqualOrLessEqual<QueryCondition.TournamentFinalPosition>() TournamentFinalPosition -> QueryCondition.moreEqualOrLessEqual<QueryCondition.TournamentFinalPosition>()
TournamentNumberOfPlayer -> QueryCondition.moreEqualOrLessEqual<QueryCondition.TournamentNumberOfPlayer>() TournamentNumberOfPlayer -> QueryCondition.moreEqualOrLessEqual<QueryCondition.TournamentNumberOfPlayer>()
TournamentEntryFee -> Criteria.TournamentFees.queryConditions.mapFirstCondition() TournamentEntryFee -> Criteria.TournamentFees.queryConditions(realm).mapFirstCondition()
TournamentName -> Criteria.TournamentNames.queryConditions.mapFirstCondition() TournamentName -> Criteria.TournamentNames.queryConditions(realm).mapFirstCondition()
TournamentFeature -> Criteria.TournamentFeatures.queryConditions.mapFirstCondition() TournamentFeature -> Criteria.TournamentFeatures.queryConditions(realm).mapFirstCondition()
Location -> Criteria.Locations.queryConditions.mapFirstCondition() Location -> Criteria.Locations.queryConditions(realm).mapFirstCondition()
Bankroll -> Criteria.Bankrolls.queryConditions.mapFirstCondition() Bankroll -> Criteria.Bankrolls.queryConditions(realm).mapFirstCondition()
MultiTabling -> QueryCondition.moreEqualOrLessEqual<QueryCondition.NumberOfTable>() MultiTabling -> QueryCondition.moreEqualOrLessEqual<QueryCondition.NumberOfTable>()
//NumberOfPlayers -> QueryCondition.moreOrLess<QueryCondition.TournamentNumberOfPlayer>() //NumberOfPlayers -> QueryCondition.moreOrLess<QueryCondition.TournamentNumberOfPlayer>()
NumberOfRebuy -> QueryCondition.moreEqualOrLessEqual<QueryCondition.NumberOfRebuy>() NumberOfRebuy -> QueryCondition.moreEqualOrLessEqual<QueryCondition.NumberOfRebuy>()
@ -158,12 +157,12 @@ sealed class FilterSectionRow(override val resId: Int?) : RowRepresentable {
addAll(QueryCondition.moreEqualOrLessEqual<QueryCondition.NetAmountWon>()) addAll(QueryCondition.moreEqualOrLessEqual<QueryCondition.NetAmountWon>())
addAll(QueryCondition.moreEqualOrLessEqual<QueryCondition.NetAmountLost>()) addAll(QueryCondition.moreEqualOrLessEqual<QueryCondition.NetAmountLost>())
} }
TransactionType -> Criteria.TransactionTypes.queryConditions.mapFirstCondition() TransactionType -> Criteria.TransactionTypes.queryConditions(realm).mapFirstCondition()
is CustomField -> { is CustomField -> {
val cf = this@FilterSectionRow.customField val cf = this@FilterSectionRow.customField
when { when {
cf.isListType -> { cf.isListType -> {
Criteria.ListCustomFields(cf.id).queryConditions.mapFirstCondition() Criteria.ListCustomFields(cf.id).queryConditions(realm).mapFirstCondition()
} }
cf.isAmountType -> { cf.isAmountType -> {
QueryCondition.moreEqualOrLessEqual<QueryCondition.CustomFieldAmountQuery>().apply { QueryCondition.moreEqualOrLessEqual<QueryCondition.CustomFieldAmountQuery>().apply {

@ -107,6 +107,14 @@ sealed class StaticReport(override var uniqueIdentifier: Int) : RowRepresentable
} }
val stats: List<Stat> val stats: List<Stat>
get() {
return when (this) {
OptimalDuration -> listOf(Stat.AVERAGE_NET_BB)
else -> listOf(Stat.NET_RESULT, Stat.HOURLY_RATE, Stat.NUMBER_OF_GAMES)
}
}
val performanceStats: List<Stat>
get() { get() {
return when (this) { return when (this) {
OptimalDuration -> listOf(Stat.AVERAGE_NET_BB) OptimalDuration -> listOf(Stat.AVERAGE_NET_BB)

@ -3,15 +3,18 @@ package net.pokeranalytics.android.ui.viewmodel
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmModel
import io.realm.RealmResults import io.realm.RealmResults
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.exceptions.RowRepresentableEditDescriptorException import net.pokeranalytics.android.exceptions.RowRepresentableEditDescriptorException
import net.pokeranalytics.android.model.Stakes import net.pokeranalytics.android.model.Stakes
import net.pokeranalytics.android.model.interfaces.Identifiable
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow
import net.pokeranalytics.android.util.extensions.findById
import java.util.* import java.util.*
class BottomSheetViewModelFactory(var row: RowRepresentable, var delegate: RowRepresentableDelegate): ViewModelProvider.Factory { class BottomSheetViewModelFactory(var row: RowRepresentable, var delegate: RowRepresentableDelegate): ViewModelProvider.Factory {
@ -84,7 +87,7 @@ class BottomSheetViewModel(var row: RowRepresentable) : ViewModel() {
var alternativeLabels: Boolean = false var alternativeLabels: Boolean = false
/** /**
* Multiselection * Multi-selection
*/ */
val selectedRows: ArrayList<RowRepresentable> = ArrayList() val selectedRows: ArrayList<RowRepresentable> = ArrayList()
@ -224,12 +227,31 @@ class BottomSheetViewModel(var row: RowRepresentable) : ViewModel() {
fun isSelected(row: RowRepresentable): Boolean { fun isSelected(row: RowRepresentable): Boolean {
return this.selectedRows.contains(row) return this.selectedRows.contains(row)
// return when(row.bottomSheetType) {
// BottomSheetType.MULTI_SELECTION -> {
// val identifiables = this.selectedRows as List<Identifiable>
// val ids = identifiables.map { it.id }
// val id = (row as Identifiable).id
// ids.contains(id)
// }
// else -> this.selectedRows.contains(row)
// }
} }
fun changedValue(): Any? { fun changedValue(): Any? {
return when(row.bottomSheetType) { return when(row.bottomSheetType) {
BottomSheetType.DOUBLE_EDIT_TEXT -> arrayListOf(this.stringValue, this.secondStringValue) BottomSheetType.DOUBLE_EDIT_TEXT -> arrayListOf(this.stringValue, this.secondStringValue)
BottomSheetType.DOUBLE_LIST, BottomSheetType.LIST_GAME -> arrayListOf(this.someValues[0], this.someValues[1]) BottomSheetType.DOUBLE_LIST, BottomSheetType.LIST_GAME -> arrayListOf(this.someValues[0], this.someValues[1])
BottomSheetType.MULTI_SELECTION -> {
this.realmData?.realm?.let { realm ->
val identifiables = this.selectedRows as List<Identifiable>
identifiables.firstOrNull()?.realmObjectClass?.let { clazz ->
val ids = identifiables.map { it.id }
val objects = ids.mapNotNull { realm.findById(clazz, it) }
objects.map { realm.copyFromRealm(it) }
} ?: kotlin.run { this.selectedRows }
} ?: kotlin.run { this.selectedRows }
}
else -> getValue() else -> getValue()
} }
} }
@ -242,9 +264,15 @@ class BottomSheetViewModel(var row: RowRepresentable) : ViewModel() {
} }
} }
fun rowSelected(position: Int): RowRepresentable? { fun rowSelected(position: Int): RowRepresentable {
return when(this.row.bottomSheetType) { return when(this.row.bottomSheetType) {
BottomSheetType.LIST -> this.realmData?.get(position) BottomSheetType.LIST -> {
realmData?.realm?.let { realm ->
this.realmData?.get(position)?.let {
realm.copyFromRealm(it as RealmModel) as? RowRepresentable
} ?: throw PAIllegalStateException("item not found at $position")
} ?: throw PAIllegalStateException("realm not found")
}
BottomSheetType.LIST_STATIC -> this.staticRows[position] BottomSheetType.LIST_STATIC -> this.staticRows[position]
else -> throw PAIllegalStateException("row selected for unmanaged bottom sheet type") else -> throw PAIllegalStateException("row selected for unmanaged bottom sheet type")
} }

@ -1,9 +1,21 @@
package net.pokeranalytics.android.ui.viewmodel package net.pokeranalytics.android.ui.viewmodel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import io.realm.Realm import io.realm.Realm
import net.pokeranalytics.android.model.LiveData import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.interfaces.Deletable import net.pokeranalytics.android.model.interfaces.Deletable
import net.pokeranalytics.android.model.interfaces.Identifiable
import net.pokeranalytics.android.ui.view.RowRepresentable
open class AddedDataViewModel : ViewModel() {
var dataForAdd = false
lateinit var dataIdentifier: RowRepresentable
var data: MutableLiveData<Identifiable> = MutableLiveData()
}
open class DataManagerViewModel : ViewModel() { open class DataManagerViewModel : ViewModel() {

@ -35,9 +35,9 @@ class FakeDataManager {
val locations = realm.where<Location>().findAll() val locations = realm.where<Location>().findAll()
if (locations.size == 0) { if (locations.size == 0) {
realm.executeTransaction { realm.executeTransactionAsync { asyncRealm ->
listOf("Bellagio", "Aria", "Borgata").map { listOf("Bellagio", "Aria", "Borgata").map {
realm.getOrCreate<Location>(it) asyncRealm.getOrCreate<Location>(it)
} }
} }
} }

@ -5,3 +5,9 @@ const val RANDOM_PLAYER: String = "☺"
const val FFMPEG_DESCRIPTOR_FILE = "descriptor.txt" const val FFMPEG_DESCRIPTOR_FILE = "descriptor.txt"
const val BLIND_SEPARATOR: String = "/" const val BLIND_SEPARATOR: String = "/"
const val UUID_SEPARATOR: String = "," const val UUID_SEPARATOR: String = ","
//class Global {
// companion object {
// const val LAUNCH_ASYNC_LISTENERS = false
// }
//}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save