Commit 92d30e0c authored by Joey's avatar Joey

core: update lifecycle to 2.1.0-beta01

Use viewModelScope from the lifecycle -ktx libray
instead of ours
Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
Change-Id: I1ba7763b6fe20bd46def144992d79760a71618d3
parent fdc869bb
......@@ -26,7 +26,6 @@ import it.diab.holders.GlucoseHolder
import it.diab.holders.GlucoseHolderCallbacks
import it.diab.util.UIUtils
import it.diab.viewmodels.glucose.GlucoseListViewModel
import kotlinx.coroutines.CoroutineScope
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
......@@ -62,9 +61,8 @@ class GlucoseListAdapter(
}
}
override fun fetchHourText(date: Date, onFetch: (String, CoroutineScope) -> Unit) {
val text = hourFormat.format(date)
onFetch(text, viewModel.viewModelScope)
override fun fetchHourText(date: Date, onFetch: (String) -> Unit) {
onFetch(hourFormat.format(date))
}
override fun getIndicator(value: Int) = when {
......
......@@ -42,8 +42,8 @@ class GlucoseHolder(
private fun bindValue(glucose: Glucose) {
val title = "${glucose.value} (%1\$s)"
callbacks.fetchHourText(glucose.date) { text, scope ->
titleView.setPrecomputedText(title.format(text), scope)
callbacks.fetchHourText(glucose.date) { text ->
titleView.setPrecomputedText(title.format(text))
}
val indicatorDrawable = callbacks.getIndicator(glucose.value)
......
......@@ -9,7 +9,6 @@
package it.diab.holders
import android.graphics.drawable.Drawable
import kotlinx.coroutines.CoroutineScope
import java.util.Date
interface GlucoseHolderCallbacks {
......@@ -22,7 +21,7 @@ interface GlucoseHolderCallbacks {
*/
fun fetchHourText(
date: Date,
onFetch: (String, CoroutineScope) -> Unit
onFetch: (String) -> Unit
)
/**
......
......@@ -9,18 +9,20 @@
package it.diab.viewmodels.glucose
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.LivePagedListBuilder
import it.diab.core.viewmodels.ScopedViewModel
import it.diab.data.entities.Insulin
import it.diab.data.repositories.GlucoseRepository
import it.diab.data.repositories.InsulinRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class GlucoseListViewModel internal constructor(
glucoseRepository: GlucoseRepository,
private val insulinRepository: InsulinRepository
) : ScopedViewModel() {
) : ViewModel() {
val pagedList = LivePagedListBuilder(glucoseRepository.pagedList, 5).build()
val liveList = glucoseRepository.all
......@@ -29,14 +31,14 @@ class GlucoseListViewModel internal constructor(
fun prepare(block: () -> Unit) {
viewModelScope.launch {
runPrepare()
launch(Dispatchers.Main) { block() }
block()
}
}
fun getInsulin(uid: Long) = insulins.firstOrNull { it.uid == uid } ?: Insulin()
@VisibleForTesting
suspend fun runPrepare() {
suspend fun runPrepare() = withContext(IO) {
insulins = insulinRepository.getInsulins()
}
}
\ No newline at end of file
......@@ -8,13 +8,13 @@
*/
package it.diab.viewmodels.insulin
import androidx.lifecycle.ViewModel
import androidx.paging.LivePagedListBuilder
import it.diab.data.repositories.InsulinRepository
import it.diab.core.viewmodels.ScopedViewModel
class InsulinViewModel internal constructor(
insulinRepository: InsulinRepository
) : ScopedViewModel() {
) : ViewModel() {
val list = LivePagedListBuilder(insulinRepository.all, 5).build()
}
\ No newline at end of file
......@@ -10,25 +10,27 @@ package it.diab.viewmodels.overview
import android.annotation.SuppressLint
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.mikephil.charting.data.Entry
import it.diab.data.entities.Glucose
import it.diab.data.entities.TimeFrame
import it.diab.data.repositories.GlucoseRepository
import it.diab.core.override.BaseFitHandler
import it.diab.core.util.DateUtils
import it.diab.data.entities.Glucose
import it.diab.data.entities.TimeFrame
import it.diab.data.extensions.toTimeFrame
import it.diab.core.viewmodels.ScopedViewModel
import it.diab.data.repositories.GlucoseRepository
import it.diab.util.extensions.getAsMinutes
import it.diab.util.extensions.isToday
import it.diab.util.extensions.isZeroOrNan
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.collections.set
class OverviewViewModel internal constructor(
private val glucoseRepository: GlucoseRepository
) : ScopedViewModel() {
) : ViewModel() {
val list = glucoseRepository.all
val last = glucoseRepository.last
......@@ -46,15 +48,13 @@ class OverviewViewModel internal constructor(
val data = glucoseRepository.getInDateRange(start, end)
val result = runGetDataSets(data)
launch(Dispatchers.Main) {
block(result.first, result.second)
}
block(result.first, result.second)
}
}
@SuppressLint("UseSparseArrays")
@VisibleForTesting
suspend fun runGetDataSets(data: List<Glucose>): Pair<List<Entry>, List<Entry>> {
suspend fun runGetDataSets(data: List<Glucose>) = withContext(Default) {
val averageDef = async {
val avg = HashMap<Int, Float>()
......@@ -84,6 +84,6 @@ class OverviewViewModel internal constructor(
val today = todayDef.await()
val average = averageDef.await()
return Pair(today, average)
Pair(today, average)
}
}
\ No newline at end of file
......@@ -36,8 +36,9 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
// Architecture
api "androidx.lifecycle:lifecycle-extensions:2.0.0"
kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"
api "androidx.lifecycle:lifecycle-extensions:2.1.0-beta01"
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01"
kapt "androidx.lifecycle:lifecycle-compiler:2.1.0-beta01"
}
// Testing dependencies
......
......@@ -11,15 +11,15 @@ package it.diab.core.util.extensions
import android.widget.TextView
import androidx.core.text.PrecomputedTextCompat
import androidx.core.widget.TextViewCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
fun TextView.setPrecomputedText(text: CharSequence, coroutineScope: CoroutineScope) {
fun TextView.setPrecomputedText(text: CharSequence) {
val params = TextViewCompat.getTextMetricsParams(this)
coroutineScope.launch(Dispatchers.IO) {
GlobalScope.launch(Dispatchers.Default) {
val textDef = async { PrecomputedTextCompat.getTextFuture(text, params, null).get() }
launch(Dispatchers.Main) { this@setPrecomputedText.text = textDef.await() }
}
......
/*
* Copyright (c) 2018 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.viewmodels
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
abstract class ScopedViewModel : ViewModel(), CoroutineScope {
private val job = Job()
val viewModelScope = CoroutineScope(IO + job)
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
override fun onCleared() {
super.onCleared()
job.cancel()
}
}
\ No newline at end of file
......@@ -21,17 +21,17 @@ import androidx.lifecycle.ViewModelProviders
import androidx.transition.TransitionManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import it.diab.data.entities.Insulin
import it.diab.data.repositories.GlucoseRepository
import it.diab.data.repositories.InsulinRepository
import it.diab.core.override.BaseFitHandler
import it.diab.core.util.Activities
import it.diab.data.plugin.PluginManager
import it.diab.core.util.PreferencesUtil
import it.diab.core.util.SystemUtil
import it.diab.data.extensions.insulin
import it.diab.core.util.extensions.setPrecomputedText
import it.diab.data.entities.Insulin
import it.diab.data.extensions.asTimeFrame
import it.diab.data.extensions.insulin
import it.diab.data.plugin.PluginManager
import it.diab.data.repositories.GlucoseRepository
import it.diab.data.repositories.InsulinRepository
import it.diab.glucose.R
import it.diab.glucose.suggestion.CheckAgainSuggestion
import it.diab.glucose.suggestion.InsulinSuggestion
......@@ -107,7 +107,7 @@ class EditorActivity : AppCompatActivity() {
private fun setupCommon() {
closeView.setOnClickListener { finish() }
valueView.text = viewModel.glucose.value.toString()
timeView.setPrecomputedText(viewModel.glucose.date.getDetailedString(), viewModel.viewModelScope)
timeView.setPrecomputedText(viewModel.glucose.date.getDetailedString())
fabView.setOnClickListener { onFabClick() }
eatView.progress = viewModel.glucose.eatLevel
}
......
......@@ -9,7 +9,8 @@
package it.diab.glucose.viewmodels
import androidx.annotation.VisibleForTesting
import it.diab.core.viewmodels.ScopedViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import it.diab.data.entities.Glucose
import it.diab.data.entities.Insulin
import it.diab.data.plugin.PluginManager
......@@ -17,14 +18,16 @@ import it.diab.data.repositories.GlucoseRepository
import it.diab.data.repositories.InsulinRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class EditorViewModel internal constructor(
private val glucoseRepository: GlucoseRepository,
private val insulinRepository: InsulinRepository
) : ScopedViewModel() {
) : ViewModel() {
var glucose = Glucose()
private set
......@@ -39,14 +42,14 @@ class EditorViewModel internal constructor(
fun prepare(pManager: PluginManager, block: () -> Unit) {
viewModelScope.launch {
runPrepare(this, pManager)
GlobalScope.launch(Dispatchers.Main) { block() }
block()
}
}
fun setGlucose(uid: Long, block: () -> Unit) {
viewModelScope.launch {
runSetGlucose(uid)
GlobalScope.launch(Dispatchers.Main) { block() }
block()
}
}
......@@ -90,8 +93,8 @@ class EditorViewModel internal constructor(
@VisibleForTesting
suspend fun runPrepare(scope: CoroutineScope, pManager: PluginManager) {
val defAll = scope.async { insulinRepository.getInsulins() }
val defBasal = scope.async { insulinRepository.getBasals() }
val defAll = scope.async(IO) { insulinRepository.getInsulins() }
val defBasal = scope.async(IO) { insulinRepository.getBasals() }
pluginManager = pManager
insulins = defAll.await()
......@@ -99,17 +102,17 @@ class EditorViewModel internal constructor(
}
@VisibleForTesting
suspend fun runSetGlucose(uid: Long) {
suspend fun runSetGlucose(uid: Long) = withContext(IO) {
glucose = glucoseRepository.getById(uid)
}
@VisibleForTesting
suspend fun runSave() {
suspend fun runSave() = withContext(IO) {
glucoseRepository.insert(glucose)
}
@VisibleForTesting
suspend fun runApplySuggestion(value: Float, insulin: Insulin) {
suspend fun runApplySuggestion(value: Float, insulin: Insulin) = withContext(IO) {
glucose.insulinId0 = insulin.uid
glucose.insulinValue0 = value
glucoseRepository.insert(glucose)
......
......@@ -9,23 +9,24 @@
package it.diab.insulin.viewmodels
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import it.diab.data.entities.Insulin
import it.diab.data.repositories.InsulinRepository
import it.diab.core.viewmodels.ScopedViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class EditorViewModel internal constructor(
private val insulinRepository: InsulinRepository
) : ScopedViewModel() {
) : ViewModel() {
var insulin = Insulin()
fun setInsulin(uid: Long, block: (Insulin) -> Unit) {
viewModelScope.launch {
insulin = runSetInsulin(uid)
launch(Dispatchers.Main) { block(insulin) }
block(insulin)
}
}
......@@ -38,17 +39,17 @@ class EditorViewModel internal constructor(
}
@VisibleForTesting
suspend fun runSetInsulin(uid: Long): Insulin {
return insulinRepository.getById(uid)
suspend fun runSetInsulin(uid: Long) = withContext(IO) {
insulinRepository.getById(uid)
}
@VisibleForTesting
suspend fun runDelete() {
suspend fun runDelete() = withContext(IO) {
insulinRepository.delete(insulin)
}
@VisibleForTesting
suspend fun runSave() {
suspend fun runSave() = withContext(IO) {
insulinRepository.insert(insulin)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment