[APIv2] Fix receivers and other weird things

This commit is contained in:
Kuba Szczodrzyński 2019-10-04 20:24:47 +02:00
parent c8c933fb20
commit 648699547e
10 changed files with 155 additions and 6 deletions

View File

@ -36,6 +36,7 @@ class ApiService : Service() {
private val errorList = mutableListOf<ApiError>()
private var queueHasErrorReportTask = false
private var serviceClosed = false
private var taskCancelled = false
private var taskRunning = false
private var taskRunningId = -1
@ -110,7 +111,8 @@ class ApiService : Service() {
private fun sync() {
if (taskRunning)
return
if (taskQueue.size <= 0) {
if (taskQueue.size <= 0 || serviceClosed) {
serviceClosed = false
allCompleted()
return
}
@ -124,6 +126,7 @@ class ApiService : Service() {
notification
.setCurrentTask(taskRunningId, null)
.setProgressRes(R.string.edziennik_notification_api_error_report_title)
.post()
return
}
@ -155,7 +158,7 @@ class ApiService : Service() {
when (task) {
is SyncProfileRequest -> edziennikInterface?.sync(task.featureIds ?: Features.getAllIds())
is SyncViewRequest -> edziennikInterface?.sync(Features.getIdsByView(task.targetId))
is SyncViewRequest -> edziennikInterface?.sync(Features.getIdsByView(task.targetId), task.targetId)
is MessageGetRequest -> edziennikInterface?.getMessage(task.messageId)
}
}
@ -213,6 +216,13 @@ class ApiService : Service() {
taskCancelled = true
edziennikInterface?.cancel()
}
@Subscribe(threadMode = ThreadMode.ASYNC)
fun onServiceCloseRequest(serviceCloseRequest: ServiceCloseRequest) {
serviceClosed = true
taskCancelled = true
edziennikInterface?.cancel()
stopSelf()
}
/* _____ _ _ _
/ ____| (_) (_) | |

View File

@ -6,7 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.events.requests
import pl.szczodrzynski.edziennik.api.v2.models.ApiTask
data class SyncProfileRequest(override val profileId: Int, val featureIds: List<Int>?) : ApiTask(profileId) {
data class SyncProfileRequest(override val profileId: Int, val featureIds: List<Int>? = null) : ApiTask(profileId) {
override fun toString(): String {
return "SyncProfileRequest(profileId=$profileId, featureIds=$featureIds)"
}

View File

@ -5,7 +5,7 @@
package pl.szczodrzynski.edziennik.api.v2.interfaces
interface EdziennikInterface {
fun sync(featureIds: List<Int>)
fun sync(featureIds: List<Int>, viewId: Int? = null)
fun getMessage(messageId: Int)
fun cancel()
}

View File

@ -49,7 +49,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
|_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_|
__/ |
|__*/
override fun sync(featureIds: List<Int>) {
override fun sync(featureIds: List<Int>, viewId: Int?) {
val possibleLoginMethods = data.loginMethods.toMutableList()
for (loginMethod in librusLoginMethods) {
@ -75,7 +75,6 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
}
val timestamp = System.currentTimeMillis()
val viewId = 0
endpointList = endpointList
// sort the endpoint list by feature ID and priority

View File

@ -206,4 +206,9 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app
get() = profile?.getStudentData("isPremium", false) ?: false
set(value) { profile?.putStudentData("isPremium", value) }
private var mSchoolName: String? = null
var schoolName: String?
get() { mSchoolName = mSchoolName ?: profile?.getStudentData("schoolName", null); return mSchoolName }
set(value) { profile?.putStudentData("schoolName", value) ?: return; mSchoolName = value }
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-4.
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
class LibrusApiEvents(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiEvents"
}
init {
apiGet(LibrusApiMe.TAG, "") { json ->
// on error
data.error(TAG, ERROR_LIBRUS_API_, response, json)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY)
onSuccess()
}
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-4.
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import android.util.Pair
import com.google.gson.JsonNull
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.getInt
import pl.szczodrzynski.edziennik.getJsonObject
import pl.szczodrzynski.edziennik.getString
import pl.szczodrzynski.edziennik.utils.models.Time
import java.util.*
class LibrusApiSchools(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiSchools"
}
init {
apiGet(LibrusApiMe.TAG, "") { json ->
val school = json?.getJsonObject("School")
val schoolId = school?.getInt("Id")
val schoolNameLong = school?.getString("Name")
var schoolNameShort = ""
schoolNameLong?.split(" ")?.forEach {
if (it.isBlank())
return@forEach
schoolNameShort += it[0].toLowerCase()
}
val schoolTown = school?.getString("Town")?.toLowerCase(Locale.getDefault())
data.schoolName = schoolId.toString() + schoolNameShort + "_" + schoolTown
/*lessonRanges.clear()
for ((index, lessonRangeEl) in school.get("LessonsRange").getAsJsonArray().withIndex()) {
val lr = lessonRangeEl.getAsJsonObject()
val from = lr.get("From")
val to = lr.get("To")
if (from != null && to != null && from !is JsonNull && to !is JsonNull) {
lessonRanges.put(index, Pair<Time, Time>(Time.fromH_m(from!!.getAsString()), Time.fromH_m(to!!.getAsString())))
}
}
profile.putStudentData("lessonRanges", app.gson.toJson(lessonRanges))
// on error
data.error(TAG, ERROR_LIBRUS_API_, response, json)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY)
onSuccess()*/
}
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-4.
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.DAY
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
class LibrusApiTemplate(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApi"
}
init {
/*apiGet(LibrusApiMe.TAG, "") { json ->
// on error
data.error(TAG, ERROR_LIBRUS_API_, response, json)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY)
onSuccess()
}*/
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-4.
*/
package pl.szczodrzynski.edziennik.data.db.modules.lessons
import androidx.room.ColumnInfo
import androidx.room.Entity
import pl.szczodrzynski.edziennik.utils.models.Time
@Entity(tableName = "lessonRanges",
primaryKeys = ["profileId", "lessonRangeNumber"])
class LessonRange (
val profileId: Int,
@ColumnInfo(name = "lessonRangeNumber")
val lessonNumber: Int,
@ColumnInfo(name = "lessonRangeStart")
val startTime: Time,
@ColumnInfo(name = "lessonRangeEnd")
val endTime: Time
)

View File

@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent
import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.api.v2.events.requests.ServiceCloseRequest
import pl.szczodrzynski.edziennik.api.v2.events.requests.SyncProfileRequest
import pl.szczodrzynski.edziennik.api.v2.events.requests.SyncRequest
import pl.szczodrzynski.edziennik.api.v2.events.requests.TaskCancelRequest
@ -18,6 +19,7 @@ class SzkolnyReceiver : BroadcastReceiver() {
"ServiceCloseRequest" -> EventBus.getDefault().post(ServiceCloseRequest())
"TaskCancelRequest" -> EventBus.getDefault().post(TaskCancelRequest(intent.extras?.getInt("taskId", -1) ?: return))
"SyncRequest" -> EventBus.getDefault().post(SyncRequest())
"SyncProfileRequest" -> EventBus.getDefault().post(SyncProfileRequest(intent.extras?.getInt("profileId", -1) ?: return))
}
}
}