From bd2b0c3a094d333623bee79426ac8e0ac5ca99c4 Mon Sep 17 00:00:00 2001 From: Aurelien Hubert Date: Thu, 21 Mar 2019 14:52:23 +0100 Subject: [PATCH] Remove background threat --- .../android/util/FakeDataManager.kt | 108 ++++++++---------- 1 file changed, 49 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/util/FakeDataManager.kt b/app/src/main/java/net/pokeranalytics/android/util/FakeDataManager.kt index 977285ed..d11daa9a 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/FakeDataManager.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/FakeDataManager.kt @@ -2,9 +2,6 @@ package net.pokeranalytics.android.util import io.realm.Realm import io.realm.kotlin.where -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch import net.pokeranalytics.android.model.Limit import net.pokeranalytics.android.model.realm.Bankroll import net.pokeranalytics.android.model.realm.Game @@ -28,82 +25,75 @@ class FakeDataManager { val commitFrequency = 100 - GlobalScope.launch { + try { - try { + val realm = Realm.getDefaultInstance() + val games = realm.where().findAll() + val bankroll = realm.where().findAll().firstOrNull() - val realm = Realm.getDefaultInstance() - val games = realm.where().findAll() - val bankroll = realm.where().findAll().firstOrNull() + // Test endedSessions - // Test endedSessions + Timber.d("*** Start creating ${numberOfSessions} fake computables...") - Timber.d("*** Start creating ${numberOfSessions} fake computables...") + val s = Date() - val s = Date() + realm.beginTransaction() - realm.beginTransaction() + for (index in 0..numberOfSessions) { - for (index in 0..numberOfSessions) { - - if (index % commitFrequency == 0) { - Timber.d("****** committing at ${index} computables...") - realm.commitTransaction() - realm.beginTransaction() - } - - val session = Session.newInstance(realm, Math.random() > 0.5, bankroll) + if (index % commitFrequency == 0) { + Timber.d("****** committing at ${index} computables...") + realm.commitTransaction() + realm.beginTransaction() + } - val bigBlind = arrayListOf(1.0, 2.0, 4.0).random() - session.cgBigBlind = bigBlind - session.cgSmallBlind = bigBlind / 2.0 + val session = Session.newInstance(realm, Math.random() > 0.5, bankroll) - val calendar = Calendar.getInstance() - calendar.set( - (2016..2018).random(), - (0..11).random(), - (0..28).random(), - (0..23).random(), - (0..59).random() - ) + val bigBlind = arrayListOf(1.0, 2.0, 4.0).random() + session.cgBigBlind = bigBlind + session.cgSmallBlind = bigBlind / 2.0 - val startDate = calendar.time - calendar.add(Calendar.HOUR_OF_DAY, (2..12).random()) - calendar.add(Calendar.MINUTE, (0..59).random()) - val endDate = calendar.time + val calendar = Calendar.getInstance() + calendar.set( + (2016..2018).random(), + (0..11).random(), + (0..28).random(), + (0..23).random(), + (0..59).random() + ) - session.startDate = startDate - session.endDate = endDate - session.creationDate = startDate + val startDate = calendar.time + calendar.add(Calendar.HOUR_OF_DAY, (2..12).random()) + calendar.add(Calendar.MINUTE, (0..59).random()) + val endDate = calendar.time - session.limit = Limit.values().random().ordinal - session.game = games.random() + session.startDate = startDate + session.endDate = endDate + session.creationDate = startDate - session.result?.let { result -> - result.buyin = buyinList.random() - result.netResult = resultsList.random() - } + session.limit = Limit.values().random().ordinal + session.game = games.random() + session.result?.let { result -> + result.buyin = buyinList.random() + result.netResult = resultsList.random() } - realm.commitTransaction() + } + + realm.commitTransaction() - val e = Date() - val duration = (e.time - s.time) / 1000.0 - Timber.d("*** ended in ${duration} seconds") + val e = Date() + val duration = (e.time - s.time) / 1000.0 + Timber.d("*** ended in ${duration} seconds") - realm.close() + realm.close() - launch(Dispatchers.Main) { - callback?.invoke(true) - } + callback?.invoke(true) - } catch (e: Exception) { - Timber.e(e) - launch(Dispatchers.Main) { - callback?.invoke(true) - } - } + } catch (e: Exception) { + Timber.e(e) + callback?.invoke(true) } } }