Commit 347e3f55 authored by Filip Wiesner's avatar Filip Wiesner

Migrating to Kotlin 1.3-RC -> Coroutine migration

parent 5e66cff6
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JsCompilerArguments">
<option name="sourceMapEmbedSources" />
<option name="sourceMapPrefix" />
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
</project>
\ No newline at end of file
......@@ -53,8 +53,8 @@ dependencies {
//Tools
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation 'androidx.core:core-ktx:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.25.3'
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.2.71'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.27.0-eap13'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation 'com.github.magneticflux:kotlin-livedata-utils:0.3.3'
//Navigation
......@@ -102,8 +102,8 @@ dependencies {
debugImplementation 'com.willowtreeapps.hyperion:hyperion-measurement:0.9.24'
debugImplementation 'com.willowtreeapps.hyperion:hyperion-geiger-counter:0.9.24'
}
kotlin {
experimental {
coroutines "enable"
}
}
//kotlin {
// experimental {
// coroutines "enable"
// }
//}
@file:Suppress("unused")
package com.cvut.blackbird.extensions
import androidx.lifecycle.LiveData
......@@ -9,8 +11,10 @@ import com.cvut.blackbird.model.Result
import com.cvut.blackbird.model.Success
import com.cvut.blackbird.support.glue.Glue
import com.cvut.blackbird.support.glue.SuperGlue
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
infix fun<T> Glue.enabledToSuccessOf(source: LiveData<Result<T>>) {
source.observe(life, Observer { actor.isEnabled = it is Success })
......@@ -43,12 +47,12 @@ inline infix fun<T> MutableLiveData<Result<T>>.asProgressStatus(job: () -> Resul
postValue(Loading())
postValue(job.invoke())
}
inline infix fun<T> MutableLiveData<Result<T>>.fetchUsing(job: () -> Result<T>) {
inline infix fun<T> MutableLiveData<Result<T>>.asProgressTo(job: () -> Result<T>) {
postValue(Loading())
postValue(job.invoke())
}
inline infix fun<T> MutableLiveData<Result<T>>.fetchUsingJob(crossinline job: suspend () -> Result<T>) =
launch {
GlobalScope.launch {
postValue(Loading())
postValue(job.invoke())
}
......
......@@ -6,13 +6,17 @@ import androidx.lifecycle.ViewModel
import com.cvut.blackbird.extensions.JobStrategy
import com.cvut.blackbird.extensions.doWith
import com.cvut.blackbird.extensions.withDefault
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.*
import kotlinx.coroutines.android.Main
import kotlin.coroutines.CoroutineContext
sealed class LateInit<T>
class Initialized<T>(val data: T): LateInit<T>()
open class BlackBirdVM: ViewModel() {
open class BlackBirdVM: ViewModel(), CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default
private val jobs = hashMapOf<String, Job>()
private val _globalProgress = MutableLiveData<Boolean>() withDefault false
......@@ -49,4 +53,9 @@ open class BlackBirdVM: ViewModel() {
_globalProgress.postValue(false)
}
}
override fun onCleared() {
jobs.forEach { it.value.cancel() }
}
}
\ No newline at end of file
......@@ -10,8 +10,6 @@ import com.cvut.blackbird.model.Result
import com.cvut.blackbird.model.Success
import com.cvut.blackbird.model.flows.AuthModel
import com.cvut.blackbird.model.support.AuthResult
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.launch
class AuthViewModel : BlackBirdVM() {
private val model: AuthModel = AuthModel()
......@@ -45,24 +43,24 @@ class AuthViewModel : BlackBirdVM() {
}
fun initUser() { startJob("init") {
_userInitResult fetchUsing {
_userInitResult asProgressTo {
_userInitState.postValue("Looking up your account")
val user = model.loadUser()
val events = if (user is Success) {
_userInitState.postValue("Downloading your timetable")
model.loadEvents()
} else return@fetchUsing user as Failure
} else return@asProgressTo user as Failure
val teachers = if (events is Success) {
_userInitState.postValue("Downloading all your teachers")
model.loadTeachers()
} else return@fetchUsing events as Failure
} else return@asProgressTo events as Failure
val courses = if (teachers is Success) {
_userInitState.postValue("Downloading your courses")
model.loadCourses()
} else return@fetchUsing teachers as Failure
} else return@asProgressTo teachers as Failure
courses
}
......
......@@ -34,7 +34,7 @@ class EventDetailFragment : Fragment() {
private fun setupBinding() {
bind(viewModel.detailedEvent) toPassValueTo ::setupEvent
bind(detail_pin) clickToInvoke ::onPinnedStateChange
// bindText(detail_notePersonalContent) changeTo viewModel::changeEventNote
bindText(detail_notePersonalContent) changeTo viewModel::changeEventNote
}
private fun setupEvent(event: DetailedEvent) {
......@@ -83,10 +83,11 @@ class EventDetailFragment : Fragment() {
detail_teachersContent.text = teachersText.dropLast(1)
}
private fun onPinnedStateChange() = detailedEvent.event.apply {
isPinned = !isPinned
refreshPinTint()
}
private fun onPinnedStateChange() = detailedEvent.event
.apply {
isPinned = !isPinned
refreshPinTint()
}
private fun refreshPinTint() = detail_pin.apply {
imageTintList =
......
......@@ -39,7 +39,10 @@ class EventDetailViewModel: BlackBirdVM(), EventDetailVM {
}
@Suppress("UNREACHABLE_CODE")
override fun changeEventNote(text: String) {
return //TODO
startJob("note") {
if (detailedEvent.dontHaveValue()) return@startJob
......
......@@ -2,19 +2,19 @@ package com.cvut.blackbird.flows.profile
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.cvut.blackbird.extensions.JobStrategy
import com.cvut.blackbird.extensions.asProgressStatus
import com.cvut.blackbird.extensions.doWith
import com.cvut.blackbird.extensions.withDefault
import com.cvut.blackbird.flows.BlackBirdVM
import com.cvut.blackbird.model.NotYet
import com.cvut.blackbird.model.Result
import com.cvut.blackbird.model.Success
import com.cvut.blackbird.model.flows.ProfileModel
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
class ProfileViewModel : ViewModel() {
class ProfileViewModel : BlackBirdVM() {
private val model = ProfileModel()
private val _logoutStatus = MutableLiveData<Result<Unit>>() withDefault NotYet()
......
......@@ -7,9 +7,10 @@ import com.cvut.blackbird.model.*
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.News
import com.cvut.blackbird.model.flows.TasksModel
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.android.Main
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class TasksViewModel : BlackBirdVM() {
private val model: TasksModel = TasksModel()
......@@ -28,9 +29,8 @@ class TasksViewModel : BlackBirdVM() {
configTasks()
}
private var refreshJob: Job? = null
fun refresh() { startJob("refresh") {
_refreshResult fetchUsing {
_refreshResult asProgressTo {
val resultNews = model.getNews()
if (resultNews is Success) {
_news.postValue(resultNews.value)
......@@ -52,6 +52,6 @@ class TasksViewModel : BlackBirdVM() {
resultTasks as List<Task>
}
withContext(UI) { combined passTo _tasks}
withContext(Dispatchers.Main) { combined passTo _tasks}
} }
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import androidx.lifecycle.*
import com.cvut.blackbird.extensions.atEndOfTheDay
import com.cvut.blackbird.extensions.atStartOfTheDay
import com.cvut.blackbird.extensions.withDefault
import com.cvut.blackbird.flows.BlackBirdVM
import com.cvut.blackbird.model.NotYet
import com.cvut.blackbird.model.Result
import com.cvut.blackbird.model.Success
......@@ -12,13 +13,14 @@ import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.Teacher
import com.cvut.blackbird.model.flows.DateSpan
import com.cvut.blackbird.model.flows.TimetableModel
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.android.Main
import kotlinx.coroutines.launch
import org.joda.time.DateTime
import org.joda.time.DateTimeConstants
class TimetableViewModel : ViewModel() {
class TimetableViewModel : BlackBirdVM() {
private val model = TimetableModel()
private val _loadingStatus = MutableLiveData<Boolean>() withDefault false
......@@ -58,7 +60,7 @@ class TimetableViewModel : ViewModel() {
}
private fun requestTimetable(from: Long, to: Long) {
requestJob?.cancel()
requestJob = launch(UI) {
requestJob = launch(Dispatchers.Main) {
timetableData?.removeObserver(timetableObserver)
timetableData = model.getTimetable(DateSpan(from, to))
timetableData?.observeForever(timetableObserver)
......
......@@ -61,8 +61,8 @@ fun EventDao.getTeachers(): List<String> {
val raw = getRawTeachers()
val convertor = DBTypeConverters()
val result = ArrayList<String>()
for (json in raw) {
result.addAll(convertor.restoreList(json))
}
for (json in raw) result.addAll(convertor.restoreList(json))
return result.distinct()
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ data class DetailedEvent (
var course: Set<Course> = setOf()
@Relation(parentColumn = "id", entityColumn = "eventId")
var userNotes: List<EventNote> = listOf()
var userNotes: List<EventNote> = listOf(EventNote(event.id))
val courseEnt: Course? get() = course.firstOrNull()
......
......@@ -7,18 +7,18 @@ import com.cvut.blackbird.model.database.EventDao
import com.cvut.blackbird.model.database.TeacherDao
import com.cvut.blackbird.model.database.getTeachers
import com.cvut.blackbird.model.entities.Course
import com.cvut.blackbird.model.entities.Student
import com.cvut.blackbird.model.entities.Teacher
import com.cvut.blackbird.model.entities.User
import com.cvut.blackbird.model.services.AuthInfo
import com.cvut.blackbird.model.services.KosService
import com.cvut.blackbird.model.services.SiriusService
import com.cvut.blackbird.model.services.UserInfo
import com.cvut.blackbird.model.support.AuthResult
import kotlinx.coroutines.experimental.Deferred
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import javax.inject.Inject
@Suppress("RedundantSuspendModifier")
class AuthModel: BlackBirdModel() {
@Inject lateinit var kosService: KosService
@Inject lateinit var siriusService: SiriusService
......@@ -92,7 +92,7 @@ class AuthModel: BlackBirdModel() {
val teachers = eventDao.getTeachers()
for (teacher in teachers)
deferredTeacherList.add(async { fetch { kosService.getTeacher(teacher) } })
deferredTeacherList.add(GlobalScope.async { fetch { kosService.getTeacher(teacher) } })
deferredTeacherList.forEach { completedTeacherList.add(it.await()) }
val result = completedTeacherList.evaluateResult()
......@@ -107,7 +107,7 @@ class AuthModel: BlackBirdModel() {
val courses = fetch { kosService.getStudentsCourses() }
if (courses is Success) {
for (link in courses.value.courses)
deferredCourseList.add(async { fetch { kosService.getCourse(link.name) } })
deferredCourseList.add(GlobalScope.async { fetch { kosService.getCourse(link.name) } })
deferredCourseList.forEach { completedCourses.add(it.await()) }
} else return Failure((courses as Failure).message)
......
......@@ -12,13 +12,14 @@ import com.cvut.blackbird.model.entities.News
import com.cvut.blackbird.model.entities.NewsRoot
import com.cvut.blackbird.model.services.*
import com.tickaroo.tikxml.TikXml
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.Exception
import java.security.InvalidKeyException
import javax.inject.Inject
@Suppress("RedundantSuspendModifier")
class TasksModel: BlackBirdModel() {
@Inject lateinit var siriusService: SiriusService
@Inject lateinit var eventDao: EventDao
......@@ -29,17 +30,18 @@ class TasksModel: BlackBirdModel() {
init {
BlackBirdAC.graph.inject(this)
pinnedPref = EventsMeta.asLiveData(EventsMeta::pinned)
pinnedPref.observeForever { onPinnedChange(it)
pinnedPref.observeForever {
GlobalScope.launch { onPinnedChange(it) }
Log.d(BlackBirdAC.LOG_TAG, "PinnedTask changed in size: ${it.size}")}
}
suspend fun getExams() = eventDao.futureExams()
fun getPinnedEvents(): LiveData<List<Event>> = pinnedEvents
private fun onPinnedChange(set: Set<String>) { launch {
private suspend fun onPinnedChange(set: Set<String>) {
pinnedEvents.postValue(
eventDao.getEvents( set.map { it.toInt() }))
} }
}
suspend fun refreshEvents() = refreshEvents(siriusService, eventDao)
......
......@@ -24,7 +24,7 @@ enum class AuthResult(val critical: Boolean){
companion object {
fun resolveReturnJson(json: String?, errorMessage:String = "Unidentified error"): AuthResult {
val jsonObj = JsonParser().parse(json).asJsonObject
val result = AuthResult.valueOf(jsonObj.get("error").asString.toUpperCase() ?: AuthResult.UNEXPECTED_ERROR.name)
val result = AuthResult.valueOf(jsonObj.get("error")?.asString?.toUpperCase() ?: AuthResult.UNEXPECTED_ERROR.name)
if(jsonObj.has("error_description"))
result.errorDesc = jsonObj.get("error_description").asString
if(jsonObj.has("error_uri"))
......
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.71'
ext.kotlin_version = '1.3.0-rc-146'
repositories {
google()
jcenter()
maven {
url "http://dl.bintray.com/kotlin/kotlin-eap"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha13'
......@@ -20,6 +23,9 @@ allprojects {
repositories {
google()
jcenter()
maven {
url "http://dl.bintray.com/kotlin/kotlin-eap"
}
}
}
......
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