diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index e95854e7..2443679b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -1245,3 +1245,5 @@ val SwipeRefreshLayout.onScrollListener: RecyclerView.OnScrollListener operator fun Iterable>.get(key: K): V? { return firstOrNull { it.first == key }?.second } + +fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt index c2c5e47e..ee52cf1a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt @@ -118,3 +118,7 @@ const val VULCAN_WEB_ENDPOINT_LUCKY_NUMBER = "Start.mvc/GetKidsLuckyNumbers" const val VULCAN_WEB_ENDPOINT_REGISTER_DEVICE = "RejestracjaUrzadzeniaToken.mvc/Get" const val EDUDZIENNIK_USER_AGENT = "Szkolny.eu/${BuildConfig.VERSION_NAME}" + +const val PODLASIE_API_VERSION = "1.0.31" +const val PODLASIE_API_URL = "https://cpdklaser.zeto.bialystok.pl/api" +const val PODLASIE_API_USER_ENDPOINT = "/pobierzDaneUcznia" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt index 55d50c11..8301f62f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt @@ -200,6 +200,12 @@ const val ERROR_EDUDZIENNIK_WEB_LIMITED_ACCESS = 521 const val ERROR_EDUDZIENNIK_WEB_SESSION_EXPIRED = 522 const val ERROR_EDUDZIENNIK_WEB_TEAM_MISSING = 530 +const val ERROR_LOGIN_PODLASIE_API_INVALID_TOKEN = 601 +const val ERROR_LOGIN_PODLASIE_API_DEVICE_LIMIT = 602 +const val ERROR_PODLASIE_API_NO_TOKEN = 630 +const val ERROR_PODLASIE_API_OTHER = 631 +const val ERROR_PODLASIE_API_DATA_MISSING = 632 + const val ERROR_TEMPLATE_WEB_OTHER = 801 const val EXCEPTION_API_TASK = 900 @@ -222,5 +228,6 @@ const val EXCEPTION_EDUDZIENNIK_FILE_REQUEST = 921 const val ERROR_ONEDRIVE_DOWNLOAD = 930 const val EXCEPTION_VULCAN_WEB_LOGIN = 931 const val EXCEPTION_VULCAN_WEB_REQUEST = 932 +const val EXCEPTION_PODLASIE_API_REQUEST = 940 const val LOGIN_NO_ARGUMENTS = 1201 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/LoginMethods.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/LoginMethods.kt index 8ac92274..24444a58 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/LoginMethods.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/LoginMethods.kt @@ -13,6 +13,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.librus.login.LibrusLoginPor import pl.szczodrzynski.edziennik.data.api.edziennik.librus.login.LibrusLoginSynergia import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.login.MobidziennikLoginApi2 import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.login.MobidziennikLoginWeb +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.login.PodlasieLoginApi import pl.szczodrzynski.edziennik.data.api.edziennik.template.login.TemplateLoginApi import pl.szczodrzynski.edziennik.data.api.edziennik.template.login.TemplateLoginWeb import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.login.VulcanLoginApi @@ -28,7 +29,6 @@ import pl.szczodrzynski.edziennik.data.api.models.LoginMethod const val SYNERGIA_API_ENABLED = false - const val LOGIN_TYPE_IDZIENNIK = 3 const val LOGIN_TYPE_TEMPLATE = 21 @@ -142,6 +142,15 @@ val edudziennikLoginMethods = listOf( .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED } ) +const val LOGIN_TYPE_PODLASIE = 6 +const val LOGIN_MODE_PODLASIE_API = 0 +const val LOGIN_METHOD_PODLASIE_API = 100 +val podlasieLoginMethods = listOf( + LoginMethod(LOGIN_TYPE_PODLASIE, LOGIN_METHOD_PODLASIE_API, PodlasieLoginApi::class.java) + .withIsPossible { _, _ -> true } + .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED } +) + val templateLoginMethods = listOf( LoginMethod(LOGIN_TYPE_TEMPLATE, LOGIN_METHOD_TEMPLATE_WEB, TemplateLoginWeb::class.java) .withIsPossible { _, _ -> true } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt index 2b0498a7..4d3f0aca 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt @@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.Edudziennik import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.Idziennik import pl.szczodrzynski.edziennik.data.api.edziennik.librus.Librus import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.Mobidziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.Podlasie import pl.szczodrzynski.edziennik.data.api.edziennik.template.Template import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.Vulcan import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback @@ -80,6 +81,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa LOGIN_TYPE_VULCAN -> Vulcan(app, profile, loginStore, taskCallback) LOGIN_TYPE_IDZIENNIK -> Idziennik(app, profile, loginStore, taskCallback) LOGIN_TYPE_EDUDZIENNIK -> Edudziennik(app, profile, loginStore, taskCallback) + LOGIN_TYPE_PODLASIE -> Podlasie(app, profile, loginStore, taskCallback) LOGIN_TYPE_TEMPLATE -> Template(app, profile, loginStore, taskCallback) else -> null } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt new file mode 100644 index 00000000..d117c139 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt @@ -0,0 +1,119 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_PODLASIE_API +import pl.szczodrzynski.edziennik.data.api.models.Data +import pl.szczodrzynski.edziennik.data.db.entity.* +import kotlin.text.replace + +class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { + + fun isApiLoginValid() = apiToken.isNotNullNorEmpty() + + override fun satisfyLoginMethods() { + loginMethods.clear() + if (isApiLoginValid()) + loginMethods += LOGIN_METHOD_PODLASIE_API + } + + override fun generateUserCode(): String = "$schoolShortName:$loginShort:${studentId?.crc32()}" + + /* _ + /\ (_) + / \ _ __ _ + / /\ \ | '_ \| | + / ____ \| |_) | | + /_/ \_\ .__/|_| + | | + |*/ + private var mApiToken: String? = null + var apiToken: String? + get() { mApiToken = mApiToken ?: loginStore.getLoginData("apiToken", null); return mApiToken } + set(value) { loginStore.putLoginData("apiToken", value); mApiToken = value } + + private var mApiUrl: String? = null + var apiUrl: String? + get() { mApiUrl = mApiUrl ?: profile?.getStudentData("apiUrl", null); return mApiUrl } + set(value) { profile?.putStudentData("apiUrl", value) ?: return; mApiUrl = value } + + /* ____ _ _ + / __ \| | | | + | | | | |_| |__ ___ _ __ + | | | | __| '_ \ / _ \ '__| + | |__| | |_| | | | __/ | + \____/ \__|_| |_|\___|*/ + private var mStudentId: String? = null + var studentId: String? + get() { mStudentId = mStudentId ?: profile?.getStudentData("studentId", null); return mStudentId } + set(value) { profile?.putStudentData("studentId", value) ?: return; mStudentId = value } + + private var mStudentLogin: String? = null + var studentLogin: String? + get() { mStudentLogin = mStudentLogin ?: profile?.getStudentData("studentLogin", null); return mStudentLogin } + set(value) { profile?.putStudentData("studentLogin", value) ?: return; mStudentLogin = 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 } + + private var mClassName: String? = null + var className: String? + get() { mClassName = mClassName ?: profile?.getStudentData("className", null); return mClassName } + set(value) { profile?.putStudentData("className", value) ?: return; mClassName = value } + + private var mSchoolYear: String? = null + var schoolYear: String? + get() { mSchoolYear = mSchoolYear ?: profile?.getStudentData("schoolYear", null); return mSchoolYear } + set(value) { profile?.putStudentData("schoolYear", value) ?: return; mSchoolYear = value } + + private var mCurrentSemester: Int? = null + var currentSemester: Int + get() { mCurrentSemester = mCurrentSemester ?: profile?.getStudentData("currentSemester", 0); return mCurrentSemester ?: 0 } + set(value) { profile?.putStudentData("currentSemester", value) ?: return; mCurrentSemester = value } + + val schoolShortName: String? + get() = studentLogin?.split('@')?.get(1)?.replace(".podlaskie.pl", "") + + val loginShort: String? + get() = studentLogin?.split('@')?.get(0) + + fun getSubject(name: String): Subject { + val id = name.crc32() + return subjectList.singleOrNull { it.id == id } ?: run { + val subject = Subject(profileId, id, name, name) + subjectList.put(id, subject) + subject + } + } + + fun getTeacher(firstName: String, lastName: String): Teacher { + val name = "$firstName $lastName".fixName() + return teacherList.singleOrNull { it.fullName == name } ?: run { + val id = name.crc32() + val teacher = Teacher(profileId, id, firstName, lastName) + teacherList.put(id, teacher) + teacher + } + } + + fun getTeam(name: String? = null): Team { + if (name == "cała klasa" || name == null) return teamClass ?: run { + val id = className!!.crc32() + val teamCode = "$schoolShortName:$className" + val team = Team(profileId, id, className, Team.TYPE_CLASS, teamCode, -1) + teamList.put(id, team) + return team + } else { + val id = name.crc32() + val teamCode = "$schoolShortName:$name" + val team = Team(profileId, id, name, Team.TYPE_VIRTUAL, teamCode, -1) + teamList.put(id, team) + return team + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/Podlasie.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/Podlasie.kt new file mode 100644 index 00000000..b2b5cf31 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/Podlasie.kt @@ -0,0 +1,132 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.PodlasieData +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.firstlogin.PodlasieFirstLogin +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.login.PodlasieLogin +import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback +import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface +import pl.szczodrzynski.edziennik.data.api.models.ApiError +import pl.szczodrzynski.edziennik.data.api.podlasieLoginMethods +import pl.szczodrzynski.edziennik.data.api.prepare +import pl.szczodrzynski.edziennik.data.db.entity.LoginStore +import pl.szczodrzynski.edziennik.data.db.entity.Profile +import pl.szczodrzynski.edziennik.data.db.entity.Teacher +import pl.szczodrzynski.edziennik.data.db.full.AnnouncementFull +import pl.szczodrzynski.edziennik.data.db.full.EventFull +import pl.szczodrzynski.edziennik.data.db.full.MessageFull +import pl.szczodrzynski.edziennik.utils.Utils + +class Podlasie(val app: App, val profile: Profile?, val loginStore: LoginStore, val callback: EdziennikCallback) : EdziennikInterface { + companion object { + const val TAG = "Podlasie" + } + + val internalErrorList = mutableListOf() + val data: DataPodlasie + + init { + data = DataPodlasie(app, profile, loginStore).apply { + callback = wrapCallback(this@Podlasie.callback) + satisfyLoginMethods() + } + } + + private fun completed() { + data.saveData() + callback.onCompleted() + } + + /* _______ _ _ _ _ _ + |__ __| | /\ | | (_) | | | + | | | |__ ___ / \ | | __ _ ___ _ __ _| |_| |__ _ __ ___ + | | | '_ \ / _ \ / /\ \ | |/ _` |/ _ \| '__| | __| '_ \| '_ ` _ \ + | | | | | | __/ / ____ \| | (_| | (_) | | | | |_| | | | | | | | | + |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| + __/ | + |__*/ + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { + data.arguments = arguments + data.prepare(podlasieLoginMethods, PodlasieFeatures, featureIds, viewId, onlyEndpoints) + Utils.d(TAG, "LoginMethod IDs: ${data.targetLoginMethodIds}") + Utils.d(TAG, "Endpoint IDs: ${data.targetEndpointIds}") + PodlasieLogin(data) { + PodlasieData(data) { + completed() + } + } + } + + override fun getMessage(message: MessageFull) { + + } + + override fun sendMessage(recipients: List, subject: String, text: String) { + + } + + override fun markAllAnnouncementsAsRead() { + + } + + override fun getAnnouncement(announcement: AnnouncementFull) { + + } + + override fun getAttachment(owner: Any, attachmentId: Long, attachmentName: String) { + + } + + override fun getRecipientList() { + + } + + override fun getEvent(eventFull: EventFull) { + + } + + override fun firstLogin() { + PodlasieFirstLogin(data) { + completed() + } + } + + override fun cancel() { + Utils.d(TAG, "Cancelled") + data.cancel() + callback.onCompleted() + } + + private fun wrapCallback(callback: EdziennikCallback): EdziennikCallback { + return object : EdziennikCallback { + override fun onCompleted() { + callback.onCompleted() + } + + override fun onProgress(step: Float) { + callback.onProgress(step) + } + + override fun onStartProgress(stringRes: Int) { + callback.onStartProgress(stringRes) + } + + override fun onError(apiError: ApiError) { + // TODO Error handling + when (apiError.errorCode) { + in internalErrorList -> { + // finish immediately if the same error occurs twice during the same sync + callback.onError(apiError) + } + else -> callback.onError(apiError) + } + } + + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt new file mode 100644 index 00000000..82c6f659 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt @@ -0,0 +1,18 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie + +import pl.szczodrzynski.edziennik.data.api.FEATURE_ALWAYS_NEEDED +import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_PODLASIE_API +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_PODLASIE +import pl.szczodrzynski.edziennik.data.api.models.Feature + +const val ENDPOINT_PODLASIE_API_MAIN = 1001 + +val PodlasieFeatures = listOf( + Feature(LOGIN_TYPE_PODLASIE, FEATURE_ALWAYS_NEEDED, listOf( + ENDPOINT_PODLASIE_API_MAIN to LOGIN_METHOD_PODLASIE_API + ), listOf(LOGIN_METHOD_PODLASIE_API)) +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt new file mode 100644 index 00000000..af5ff9d7 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt @@ -0,0 +1,108 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data + +import com.google.gson.JsonObject +import im.wangchao.mhttp.Request +import im.wangchao.mhttp.RequestParams +import im.wangchao.mhttp.Response +import im.wangchao.mhttp.callback.JsonCallbackHandler +import pl.szczodrzynski.edziennik.data.api.* +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.ApiError +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getJsonObject +import pl.szczodrzynski.edziennik.toHexString +import pl.szczodrzynski.edziennik.utils.Utils +import java.security.MessageDigest +import java.text.SimpleDateFormat +import java.util.* + +open class PodlasieApi(open val data: DataPodlasie, open val lastSync: Long?) { + companion object { + const val TAG = "PodlasieApi" + } + + val profileId + get() = data.profile?.id ?: -1 + + val profile + get() = data.profile + + fun apiGet(tag: String, endpoint: String, onSuccess: (json: JsonObject) -> Unit) { + val url = PODLASIE_API_URL + endpoint + + Utils.d(tag, "Request: Podlasie/Api - $url") + + if (data.apiToken == null) { + data.error(tag, ERROR_PODLASIE_API_NO_TOKEN) + return + } + + val callback = object : JsonCallbackHandler() { + override fun onSuccess(json: JsonObject?, response: Response?) { + if (json == null || response == null) { + data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) + .withResponse(response)) + return + } + + val error = json.getJsonObject("system_message")?.getInt("code") + + error?.let { code -> + when (code) { + 0 -> ERROR_PODLASIE_API_DATA_MISSING + 4 -> ERROR_LOGIN_PODLASIE_API_DEVICE_LIMIT + 5 -> ERROR_LOGIN_PODLASIE_API_INVALID_TOKEN + 200 -> null // Not an error + else -> ERROR_PODLASIE_API_OTHER + }?.let { errorCode -> + data.error(ApiError(tag, errorCode) + .withApiResponse(json) + .withResponse(response)) + return@onSuccess + } + } + + try { + onSuccess(json) + } catch (e: Exception) { + data.error(ApiError(tag, EXCEPTION_PODLASIE_API_REQUEST) + .withResponse(response) + .withThrowable(e) + .withApiResponse(json)) + } + } + + override fun onFailure(response: Response?, throwable: Throwable?) { + data.error(ApiError(tag, ERROR_REQUEST_FAILURE) + .withResponse(response) + .withThrowable(throwable)) + } + } + + Request.builder() + .url(url) + .userAgent(SYSTEM_USER_AGENT) + .requestParams(RequestParams(mapOf( + "token" to data.apiToken, + "securityToken" to getSecurityToken(), + "mobileId" to data.app.deviceId, + "ver" to PODLASIE_API_VERSION + ))) + .callback(callback) + .build() + .enqueue() + } + + private fun getSecurityToken(): String { + val format = SimpleDateFormat("yyyy-MM-dd HH", Locale.ENGLISH) + .also { it.timeZone = TimeZone.getTimeZone("Europe/Warsaw") }.format(System.currentTimeMillis()) + val instance = MessageDigest.getInstance("SHA-256") + val digest = instance.digest("-EYlwYu8u16miVd8tT?oO7cvoUVQrQN0vr!$format".toByteArray()).toHexString() + val digest2 = instance.digest(data.apiToken!!.toByteArray()).toHexString() + return instance.digest("$digest$digest2".toByteArray()).toHexString() + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt new file mode 100644 index 00000000..ce1197b8 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data + +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.ENDPOINT_PODLASIE_API_MAIN +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api.PodlasieApiMain +import pl.szczodrzynski.edziennik.utils.Utils + +class PodlasieData(val data: DataPodlasie, val onSuccess: () -> Unit) { + companion object { + const val TAG = "PodlasieData" + } + + init { + nextEndpoint(onSuccess) + } + + private fun nextEndpoint(onSuccess: () -> Unit) { + if (data.targetEndpointIds.isEmpty()) { + onSuccess() + return + } + if (data.cancelled) { + onSuccess() + return + } + val id = data.targetEndpointIds.firstKey() + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { + data.progress(data.progressStep) + nextEndpoint(onSuccess) + } + } + + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { + Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") + when (endpointId) { + ENDPOINT_PODLASIE_API_MAIN -> { + data.startProgress(R.string.edziennik_progress_endpoint_data) + PodlasieApiMain(data, lastSync, onSuccess) + } + else -> onSuccess(endpointId) + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt new file mode 100644 index 00000000..b98440ea --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt @@ -0,0 +1,75 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Event +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time +import java.util.* + +class PodlasieApiEvents(val data: DataPodlasie, val rows: List) { + init { + rows.forEach { event -> + val id = event.getLong("ExternalId") ?: return@forEach + val date = event.getString("DateFrom")?.let { Date.fromY_m_d(it) } ?: return@forEach + val time = event.getString("DateFrom")?.let { Time.fromY_m_d_H_m_s(it) } + ?: return@forEach + + val name = event.getString("Name")?.replace(""", "\"") ?: "" + val description = event.getString("Description")?.replace(""", "\"") ?: "" + + val type = when (event.getString("Category")?.toLowerCase(Locale.getDefault())) { + "klasówka" -> Event.TYPE_EXAM + "praca domowa" -> Event.TYPE_HOMEWORK + "wycieczka" -> Event.TYPE_EXCURSION + else -> Event.TYPE_DEFAULT + } + + val teacherFirstName = event.getString("PersonEnteringDataFirstName") ?: return@forEach + val teacherLastName = event.getString("PersonEnteringDataLastName") ?: return@forEach + val teacher = data.getTeacher(teacherFirstName, teacherLastName) + + val lessonList = data.db.timetableDao().getAllForDateNow(data.profileId, date) + val lesson = lessonList.firstOrNull { it.startTime == time } + + val addedDate = event.getString("CreateDate")?.let { Date.fromIso(it) } + ?: System.currentTimeMillis() + + val eventObject = Event( + profileId = data.profileId, + id = id, + date = date, + time = time, + topic = name, + color = null, + type = type, + teacherId = teacher.id, + subjectId = lesson?.subjectId ?: -1, + teamId = data.teamClass?.id ?: -1, + addedDate = addedDate + ).apply { + homeworkBody = description + } + + data.eventList.add(eventObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_EVENT, + id, + data.profile?.empty ?: false, + data.profile?.empty ?: false + )) + } + + data.toRemove.add(DataRemoveModel.Events.future()) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt new file mode 100644 index 00000000..7c4575aa --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt @@ -0,0 +1,121 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Grade +import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_SEMESTER1_FINAL +import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_SEMESTER1_PROPOSED +import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_SEMESTER2_FINAL +import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_SEMESTER2_PROPOSED +import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_YEAR_FINAL +import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_YEAR_PROPOSED +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString + +class PodlasieApiFinalGrades(val data: DataPodlasie, val rows: List) { + init { data.profile?.also { profile -> + rows.forEach { grade -> + val id = grade.getLong("ExternalId") ?: return@forEach + val mark = grade.getString("Mark") ?: return@forEach + val proposedMark = grade.getString("ProposedMark") ?: "0" + val name = data.app.gradesManager.getGradeNumberName(mark) + val value = data.app.gradesManager.getGradeValue(name) + val semester = grade.getString("TermShortcut")?.length ?: return@forEach + + val typeName = grade.getString("Type") ?: return@forEach + val type = when (typeName) { + "S" -> if (semester == 1) TYPE_SEMESTER1_FINAL else TYPE_SEMESTER2_FINAL + "Y", "R" -> TYPE_YEAR_FINAL + else -> return@forEach + } + + val subjectName = grade.getString("SchoolSubject") ?: return@forEach + val subject = data.getSubject(subjectName) + + val addedDate = if (profile.empty) profile.getSemesterStart(semester).inMillis + else System.currentTimeMillis() + + val gradeObject = Grade( + profileId = data.profileId, + id = id, + name = name, + type = type, + value = value, + weight = 0f, + color = -1, + category = null, + description = null, + comment = null, + semester = semester, + teacherId = -1, + subjectId = subject.id, + addedDate = addedDate + ) + + data.gradeList.add(gradeObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_GRADE, + id, + profile.empty, + profile.empty + )) + + if (proposedMark != "0") { + val proposedName = data.app.gradesManager.getGradeNumberName(proposedMark) + val proposedValue = data.app.gradesManager.getGradeValue(proposedName) + + val proposedType = when (typeName) { + "S" -> if (semester == 1) TYPE_SEMESTER1_PROPOSED else TYPE_SEMESTER2_PROPOSED + "Y", "R" -> TYPE_YEAR_PROPOSED + else -> return@forEach + } + + val proposedGradeObject = Grade( + profileId = data.profileId, + id = id * (-1), + name = proposedName, + type = proposedType, + value = proposedValue, + weight = 0f, + color = -1, + category = null, + description = null, + comment = null, + semester = semester, + teacherId = -1, + subjectId = subject.id, + addedDate = addedDate + ) + + data.gradeList.add(proposedGradeObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_GRADE, + proposedGradeObject.id, + profile.empty, + profile.empty + )) + } + } + + data.toRemove.addAll(listOf( + TYPE_SEMESTER1_FINAL, + TYPE_SEMESTER1_PROPOSED, + TYPE_SEMESTER2_FINAL, + TYPE_SEMESTER2_PROPOSED, + TYPE_YEAR_FINAL, + TYPE_YEAR_PROPOSED + ).map { + DataRemoveModel.Grades.allWithType(it) + }) + }} +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt new file mode 100644 index 00000000..d5696567 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import android.graphics.Color +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Grade +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.getFloat +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date + +class PodlasieApiGrades(val data: DataPodlasie, val rows: List) { + init { + rows.forEach { grade -> + val id = grade.getLong("ExternalId") ?: return@forEach + val name = grade.getString("Mark") ?: return@forEach + val value = data.app.gradesManager.getGradeValue(name) + val weight = grade.getFloat("Weight") ?: 0f + val includeToAverage = grade.getInt("IncludeToAverage") != 0 + val color = grade.getString("Color")?.let { Color.parseColor(it) } ?: -1 + val category = grade.getString("Category") ?: "" + val comment = grade.getString("Comment") ?: "" + val semester = grade.getString("TermShortcut")?.length ?: data.currentSemester + + val teacherFirstName = grade.getString("TeacherFirstName") ?: return@forEach + val teacherLastName = grade.getString("TeacherLastName") ?: return@forEach + val teacher = data.getTeacher(teacherFirstName, teacherLastName) + + val subjectName = grade.getString("SchoolSubject") ?: return@forEach + val subject = data.getSubject(subjectName) + + val addedDate = grade.getString("ReceivedDate")?.let { Date.fromY_m_d(it).inMillis } + ?: System.currentTimeMillis() + + val gradeObject = Grade( + profileId = data.profileId, + id = id, + name = name, + type = Grade.TYPE_NORMAL, + value = value, + weight = if (includeToAverage) weight else 0f, + color = color, + category = category, + description = null, + comment = comment, + semester = semester, + teacherId = teacher.id, + subjectId = subject.id, + addedDate = addedDate + ) + + data.gradeList.add(gradeObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_GRADE, + id, + data.profile?.empty ?: false, + data.profile?.empty ?: false + )) + } + + data.toRemove.add(DataRemoveModel.Grades.allWithType(Grade.TYPE_NORMAL)) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiLuckyNumber.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiLuckyNumber.kt new file mode 100644 index 00000000..725ec727 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiLuckyNumber.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.db.entity.LuckyNumber +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.utils.models.Date + +class PodlasieApiLuckyNumber(val data: DataPodlasie, val luckyNumber: Int) { + init { + val luckyNumberObject = LuckyNumber( + profileId = data.profileId, + date = Date.getToday(), + number = luckyNumber + ) + + data.luckyNumberList.add(luckyNumberObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_LUCKY_NUMBER, + luckyNumberObject.date.value.toLong(), + true, + data.profile?.empty ?: false + )) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt new file mode 100644 index 00000000..d73e8d60 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import pl.szczodrzynski.edziennik.asJsonObjectList +import pl.szczodrzynski.edziennik.data.api.PODLASIE_API_USER_ENDPOINT +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.ENDPOINT_PODLASIE_API_MAIN +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.PodlasieApi +import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getJsonArray + +class PodlasieApiMain(override val data: DataPodlasie, + override val lastSync: Long?, + val onSuccess: (endpointId: Int) -> Unit) : PodlasieApi(data, lastSync) { + companion object { + const val TAG = "PodlasieApiTimetable" + } + + init { + apiGet(TAG, PODLASIE_API_USER_ENDPOINT) { json -> + data.getTeam() // Save the class team when it doesn't exist. + + json.getInt("LuckyNumber")?.let { PodlasieApiLuckyNumber(data, it) } + json.getJsonArray("Teacher")?.asJsonObjectList()?.let { PodlasieApiTeachers(data, it) } + json.getJsonArray("Timetable")?.asJsonObjectList()?.let { PodlasieApiTimetable(data, it) } + json.getJsonArray("Marks")?.asJsonObjectList()?.let { PodlasieApiGrades(data, it) } + json.getJsonArray("MarkFinal")?.asJsonObjectList()?.let { PodlasieApiFinalGrades(data, it) } + json.getJsonArray("News")?.asJsonObjectList()?.let { PodlasieApiEvents(data, it) } + + data.setSyncNext(ENDPOINT_PODLASIE_API_MAIN, SYNC_ALWAYS) + onSuccess(ENDPOINT_PODLASIE_API_MAIN) + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt new file mode 100644 index 00000000..ca1fa7b4 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.db.entity.Teacher +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString + +class PodlasieApiTeachers(val data: DataPodlasie, val rows: List) { + init { + rows.forEach { teacher -> + val id = teacher.getLong("ExternalId") ?: return@forEach + val firstName = teacher.getString("FirstName") ?: return@forEach + val lastName = teacher.getString("LastName") ?: return@forEach + val isEducator = teacher.getInt("Educator") == 1 + + val teacherObject = Teacher( + profileId = data.profileId, + id = id, + name = firstName, + surname = lastName, + loginId = null + ) + + data.teacherList.put(id, teacherObject) + + val teamClass = data.teamClass + if (isEducator && teamClass != null) { + data.teamList.put(teamClass.id, teamClass.apply { + teacherId = id + }) + } + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.kt new file mode 100644 index 00000000..a9a12d5d --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.kt @@ -0,0 +1,89 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Lesson +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time +import pl.szczodrzynski.edziennik.utils.models.Week + +class PodlasieApiTimetable(val data: DataPodlasie, rows: List) { + init { + val currentWeekStart = Week.getWeekStart() + + if (Date.getToday().weekDay > 4) { + currentWeekStart.stepForward(0, 0, 7) + } + + val getDate = data.arguments?.getString("weekStart") ?: currentWeekStart.stringY_m_d + + val weekStart = Date.fromY_m_d(getDate) + val weekEnd = weekStart.clone().stepForward(0, 0, 6) + + val days = mutableListOf() + var startDate: Date? = null + var endDate: Date? = null + + rows.forEach { lesson -> + val date = lesson.getString("Date")?.let { Date.fromY_m_d(it) } ?: return@forEach + + if ((date > weekEnd || date < weekStart) && data.profile?.empty != true) return@forEach + if (startDate == null) startDate = date.clone() + endDate = date.clone() + if (date.value !in days) days += date.value + + val lessonNumber = lesson.getInt("LessonNumber") ?: return@forEach + val startTime = lesson.getString("TimeFrom")?.let { Time.fromH_m_s(it) } + ?: return@forEach + val endTime = lesson.getString("TimeTo")?.let { Time.fromH_m_s(it) } ?: return@forEach + val subject = lesson.getString("SchoolSubject")?.let { data.getSubject(it) } + ?: return@forEach + + val teacherFirstName = lesson.getString("TeacherFirstName") ?: return@forEach + val teacherLastName = lesson.getString("TeacherLastName") ?: return@forEach + val teacher = data.getTeacher(teacherFirstName, teacherLastName) + + val team = lesson.getString("Group")?.let { data.getTeam(it) } ?: return@forEach + val classroom = lesson.getString("Room") + + Lesson(data.profileId, -1).also { + it.type = Lesson.TYPE_NORMAL + it.date = date + it.lessonNumber = lessonNumber + it.startTime = startTime + it.endTime = endTime + it.subjectId = subject.id + it.teacherId = teacher.id + it.teamId = team.id + it.classroom = classroom + + it.id = it.buildId() + data.lessonList += it + } + } + + if (startDate != null && endDate != null) { + if (weekEnd > endDate!!) endDate = weekEnd + + while (startDate!! <= endDate!!) { + if (startDate!!.value !in days) { + val lessonDate = startDate!!.clone() + data.lessonList += Lesson(data.profileId, lessonDate.value.toLong()).apply { + type = Lesson.TYPE_NO_LESSONS + date = lessonDate + } + } + startDate!!.stepForward(0, 0, 1) + } + } + + data.toRemove.add(DataRemoveModel.Timetable.between(weekStart, weekEnd)) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt new file mode 100644 index 00000000..9b92d541 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt @@ -0,0 +1,71 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.firstlogin + +import org.greenrobot.eventbus.EventBus +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_PODLASIE +import pl.szczodrzynski.edziennik.data.api.PODLASIE_API_USER_ENDPOINT +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.PodlasieApi +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.login.PodlasieLoginApi +import pl.szczodrzynski.edziennik.data.api.events.FirstLoginFinishedEvent +import pl.szczodrzynski.edziennik.data.db.entity.Profile + +class PodlasieFirstLogin(val data: DataPodlasie, val onSuccess: () -> Unit) { + companion object { + const val TAG = "PodlasieFirstLogin" + } + + private val api = PodlasieApi(data, null) + + init { + val loginStoreId = data.loginStore.id + val loginStoreType = LOGIN_TYPE_PODLASIE + + PodlasieLoginApi(data) { + api.apiGet(TAG, PODLASIE_API_USER_ENDPOINT) { json -> + val uuid = json.getString("Uuid") + val login = json.getString("Login") + val firstName = json.getString("FirstName") + val lastName = json.getString("LastName") + val studentNameLong = "$firstName $lastName".fixName() + val studentNameShort = studentNameLong.getShortName() + val schoolName = json.getString("SchoolName") + val className = json.getString("SchoolClass") + val schoolYear = json.getString("ActualSchoolYear")?.replace(' ', '/') + val semester = json.getString("ActualTermShortcut")?.length + val apiUrl = json.getString("URL") + + val profile = Profile( + loginStoreId, + loginStoreId, + loginStoreType, + studentNameLong, + login, + studentNameLong, + studentNameShort, + null + ).apply { + studentData["studentId"] = uuid + studentData["studentLogin"] = login + studentData["schoolName"] = schoolName + studentData["className"] = className + studentData["schoolYear"] = schoolYear + studentData["currentSemester"] = semester ?: 1 + studentData["apiUrl"] = apiUrl + + schoolYear?.split('/')?.get(0)?.toInt()?.let { + studentSchoolYearStart = it + } + studentClassName = className + } + + EventBus.getDefault().postSticky(FirstLoginFinishedEvent(listOf(profile), data.loginStore)) + onSuccess() + } + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/login/PodlasieLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/login/PodlasieLogin.kt new file mode 100644 index 00000000..406bb997 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/login/PodlasieLogin.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.login + +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_PODLASIE_API +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.utils.Utils + +class PodlasieLogin(val data: DataPodlasie, val onSuccess: () -> Unit) { + companion object { + const val TAG = "PodlasieLogin" + } + + private var cancelled = false + + init { + nextLoginMethod(onSuccess) + } + + private fun nextLoginMethod(onSuccess: () -> Unit) { + if (data.targetLoginMethodIds.isEmpty()) { + onSuccess() + return + } + if (cancelled) { + onSuccess() + return + } + useLoginMethod(data.targetLoginMethodIds.removeAt(0)) { usedMethodId -> + data.progress(data.progressStep) + if (usedMethodId != -1) + data.loginMethods.add(usedMethodId) + nextLoginMethod(onSuccess) + } + } + + private fun useLoginMethod(loginMethodId: Int, onSuccess: (usedMethodId: Int) -> Unit) { + // this should never be true + if (data.loginMethods.contains(loginMethodId)) { + onSuccess(-1) + return + } + Utils.d(TAG, "Using login method $loginMethodId") + when (loginMethodId) { + LOGIN_METHOD_PODLASIE_API -> { + data.startProgress(R.string.edziennik_progress_login_podlasie_api) + PodlasieLoginApi(data) { onSuccess(loginMethodId) } + } + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/login/PodlasieLoginApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/login/PodlasieLoginApi.kt new file mode 100644 index 00000000..6eb70d06 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/login/PodlasieLoginApi.kt @@ -0,0 +1,23 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.login + +import pl.szczodrzynski.edziennik.data.api.ERROR_LOGIN_DATA_MISSING +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.ApiError + +class PodlasieLoginApi(val data: DataPodlasie, val onSuccess: () -> Unit) { + companion object { + const val TAG = "PodlasieLoginApi" + } + + init { run { + if (data.isApiLoginValid()) { + onSuccess() + } else { + data.error(ApiError(TAG, ERROR_LOGIN_DATA_MISSING)) + } + }} +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt index fabf5f39..2f9c8fd9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt @@ -53,12 +53,15 @@ open class DataRemoveModel { fun futureExceptType(exceptType: Long) = Events(null, exceptType, null) fun futureExceptTypes(exceptTypes: List) = Events(null, null, exceptTypes) fun futureWithType(type: Long) = Events(type, null, null) + fun future() = Events(null, null, null) } fun commit(profileId: Int, dao: EventDao) { type?.let { dao.dontKeepFutureWithType(profileId, Date.getToday(), it) } exceptType?.let { dao.dontKeepFutureExceptType(profileId, Date.getToday(), it) } exceptTypes?.let { dao.dontKeepFutureExceptTypes(profileId, Date.getToday(), it) } + if (type == null && exceptType == null && exceptTypes == null) + dao.dontKeepFuture(profileId, Date.getToday()) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt index 44b2962d..a532602b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt @@ -114,6 +114,9 @@ abstract class EventDao : BaseDao { " AND " + filter)) } + @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate") + abstract fun dontKeepFuture(profileId: Int, todayDate: Date) + @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") abstract fun dontKeepFutureWithType(profileId: Int, todayDate: Date, type: Long) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt index aa28faf7..50ad82a7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt @@ -17,6 +17,7 @@ import com.google.gson.JsonObject import pl.droidsonroids.gif.GifDrawable import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_EDUDZIENNIK +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_PODLASIE import pl.szczodrzynski.edziennik.utils.ProfileImageHolder import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.navlib.ImageHolder @@ -175,6 +176,12 @@ open class Profile( MainActivity.DRAWER_ITEM_ATTENDANCE, MainActivity.DRAWER_ITEM_ANNOUNCEMENTS ) + LOGIN_TYPE_PODLASIE -> listOf( + MainActivity.DRAWER_ITEM_TIMETABLE, + MainActivity.DRAWER_ITEM_AGENDA, + MainActivity.DRAWER_ITEM_GRADES, + MainActivity.DRAWER_ITEM_HOMEWORK + ) else -> listOf( MainActivity.DRAWER_ITEM_TIMETABLE, MainActivity.DRAWER_ITEM_AGENDA, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginInfo.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginInfo.kt index 679bcb68..d7230356 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginInfo.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginInfo.kt @@ -333,6 +333,34 @@ object LoginInfo { ) ) ) + ), + Register( + loginType = LOGIN_TYPE_PODLASIE, + internalName = "podlasie", + registerName = R.string.login_type_podlasie, + registerLogo = R.drawable.login_logo_podlasie, + loginModes = listOf( + Mode( + loginMode = LOGIN_MODE_PODLASIE_API, + name = R.string.login_mode_podlasie_api, + icon = R.drawable.login_mode_podlasie_api, + guideText = R.string.login_mode_podlasie_api_guide, + credentials = listOf( + Credential( + keyName = "apiToken", + name = R.string.login_hint_token, + icon = CommunityMaterial.Icon2.cmd_lock_outline, + emptyText = R.string.login_error_no_token, + invalidText = R.string.login_error_incorrect_token, + errorCodes = mapOf(), + isRequired = true, + validationRegex = "[a-zA-Z0-9]{10}", + caseMode = Credential.CaseMode.UNCHANGED + ) + ), + errorCodes = mapOf() + ) + ) ) ) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/GradesManager.kt b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/GradesManager.kt index 8a09df3d..61ea5ad2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/GradesManager.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/GradesManager.kt @@ -208,6 +208,30 @@ class GradesManager(val app: App) : CoroutineScope { } } + fun getGradeNumberName(name: String): String { + return when(name.toLowerCase()){ + "niedostateczny", "f" -> "1" + "niedostateczny plus", "f+" -> "1+" + "niedostateczny minus", "f-" -> "1-" + "dopuszczający", "e" -> "2" + "dopuszczający plus", "e+" -> "2+" + "dopuszczający minus", "e-" -> "2-" + "dostateczny", "d" -> "3" + "dostateczny plus", "d+" -> "3+" + "dostateczny minus", "d-" -> "3-" + "dobry", "c" -> "4" + "dobry plus", "c+" -> "4+" + "dobry minus", "c-" -> "4-" + "bardzo dobry", "b" -> "5" + "bardzo dobry plus", "b+" -> "5+" + "bardzo dobry minus", "b-" -> "5-" + "celujący", "a" -> "6" + "celujący plus", "a+" -> "6+" + "celujący minus", "a-" -> "6-" + else -> name + } + } + /* _ _ _____ _____ _ __ _ | | | |_ _| / ____| (_)/ _(_) | | | | | | | (___ _ __ ___ ___ _| |_ _ ___ diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java index d19e317f..04b0c8a3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java @@ -49,6 +49,16 @@ public class Time implements Comparable