Use coroutines for graph processing

This commit is contained in:
Milos Kozak 2020-04-07 14:15:30 +02:00
parent 61acfb8c92
commit 4644159c82
2 changed files with 107 additions and 103 deletions

View file

@ -29,7 +29,8 @@ ext {
powermockVersion = "1.7.3" powermockVersion = "1.7.3"
dexmakerVersion = "1.2" dexmakerVersion = "1.2"
retrofit2Version = '2.8.1' retrofit2Version = '2.8.1'
okhttp3Version="4.4.1" okhttp3Version = '4.5.0'
coroutinesVersion = '1.3.5'
} }
@ -226,7 +227,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.android.gms:play-services-wearable:17.0.0' implementation 'com.google.android.gms:play-services-wearable:17.0.0'
implementation "com.google.android.gms:play-services-location:17.0.0" implementation "com.google.android.gms:play-services-location:17.0.0"
implementation 'com.google.firebase:firebase-core:17.2.3' implementation 'com.google.firebase:firebase-core:17.3.0'
implementation 'com.google.firebase:firebase-auth:19.3.0' implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-database:19.2.1' implementation 'com.google.firebase:firebase-database:19.2.1'
implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') { implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') {
@ -299,7 +300,8 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
// new for tidepool // new for tidepool
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version" implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"

View file

@ -91,6 +91,10 @@ import kotlinx.android.synthetic.main.overview_fragment.overview_temptarget
import kotlinx.android.synthetic.main.overview_fragment.overview_treatmentbutton import kotlinx.android.synthetic.main.overview_fragment.overview_treatmentbutton
import kotlinx.android.synthetic.main.overview_fragment.overview_wizardbutton import kotlinx.android.synthetic.main.overview_fragment.overview_wizardbutton
import kotlinx.android.synthetic.main.overview_fragment_nsclient_tablet.* import kotlinx.android.synthetic.main.overview_fragment_nsclient_tablet.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.* import java.util.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture import java.util.concurrent.ScheduledFuture
@ -741,8 +745,12 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
} }
// ****** GRAPH ******* // ****** GRAPH *******
Thread(Runnable { GlobalScope.launch(Dispatchers.Main) {
val graphData = GraphData(injector, overview_bggraph, iobCobCalculatorPlugin)
val secondaryGraphsData: ArrayList<GraphData> = ArrayList()
// do preparation in different thread
withContext(Dispatchers.Default) {
// align to hours // align to hours
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis() calendar.timeInMillis = System.currentTimeMillis()
@ -772,7 +780,6 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
// ------------------ 1st graph // ------------------ 1st graph
val graphData = GraphData(injector, overview_bggraph, iobCobCalculatorPlugin)
// **** In range Area **** // **** In range Area ****
graphData.addInRangeArea(fromTime, endTime, lowLine, highLine) graphData.addInRangeArea(fromTime, endTime, lowLine, highLine)
@ -801,7 +808,6 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
graphData.addNowLine(now) graphData.addNowLine(now)
// ------------------ 2nd graph // ------------------ 2nd graph
val secondaryGraphsData: ArrayList<GraphData> = ArrayList()
for (g in 0 until secondaryGraphs.size) { for (g in 0 until secondaryGraphs.size) {
val secondGraphData = GraphData(injector, secondaryGraphs[g], iobCobCalculatorPlugin) val secondGraphData = GraphData(injector, secondaryGraphs[g], iobCobCalculatorPlugin)
var useIobForScale = false var useIobForScale = false
@ -831,11 +837,8 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
secondGraphData.addNowLine(now) secondGraphData.addNowLine(now)
secondaryGraphsData.add(secondGraphData) secondaryGraphsData.add(secondGraphData)
} }
}
// do GUI update // finally enforce drawing of graphs in UI thread
val activity = activity
activity?.runOnUiThread {
// finally enforce drawing of graphs
graphData.performUpdate() graphData.performUpdate()
for (g in 0 until secondaryGraphs.size) { for (g in 0 until secondaryGraphs.size) {
secondaryGraphs[g].visibility = ( secondaryGraphs[g].visibility = (
@ -849,6 +852,5 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
secondaryGraphsData[g].performUpdate() secondaryGraphsData[g].performUpdate()
} }
} }
}).start()
} }
} }