[APIv2/Mobidziennik] Implement First login.

This commit is contained in:
Kuba Szczodrzyński 2019-10-14 15:44:02 +02:00
parent 0b7f9a08ef
commit e91b4fcf8b
3 changed files with 59 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import pl.szczodrzynski.edziennik.api.v2.CODE_INTERNAL_LIBRUS_ACCOUNT_410
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikData
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.firstlogin.MobidziennikFirstLogin
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.login.MobidziennikLogin
import pl.szczodrzynski.edziennik.api.v2.mobidziennikLoginMethods
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
@ -62,7 +63,9 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto
}
override fun firstLogin() {
// TODO
MobidziennikFirstLogin(data) {
completed()
}
}
override fun cancel() {

View File

@ -0,0 +1,55 @@
package pl.szczodrzynski.edziennik.api.v2.mobidziennik.firstlogin
import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.api.v2.events.FirstLoginFinishedEvent
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikWeb
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.login.MobidziennikLoginWeb
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
class MobidziennikFirstLogin(val data: DataMobidziennik, val onSuccess: () -> Unit) {
companion object {
private const val TAG = "MobidziennikFirstLogin"
}
private val web = MobidziennikWeb(data)
private val profileList = mutableListOf<Profile>()
init {
MobidziennikLoginWeb(data) {
web.webGet(TAG, "/api/zrzutbazy") { text ->
val tables = text.split("T@B#LA")
val studentIds = mutableListOf<Int>()
val studentNamesLong = mutableListOf<String>()
val studentNamesShort = mutableListOf<String>()
val student = tables[8].split("\n")
for (aStudent in student) {
if (aStudent.isEmpty())
continue
val student1 = aStudent.split("|")
if (student1.size == 2)
continue
studentIds += student1[0].toInt()
studentNamesLong.add(student1[2] + " " + student1[4])
studentNamesShort.add(student1[2] + " " + student1[4][0] + ".")
}
for (index in studentIds.indices) {
val profile = Profile()
profile.studentNameLong = studentNamesLong[index]
profile.studentNameShort = studentNamesShort[index]
profile.name = profile.studentNameLong + " (APIv2)"
profile.subname = data.loginUsername
profile.empty = true
profile.putStudentData("studentId", studentIds[index])
profileList.add(profile)
}
EventBus.getDefault().post(FirstLoginFinishedEvent(profileList, data.loginStore))
onSuccess()
}
}
}
}

View File

@ -12,7 +12,6 @@ import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.getUnixDate
import pl.szczodrzynski.edziennik.isNotNullNorEmpty
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.Utils.d
class MobidziennikLoginWeb(val data: DataMobidziennik, val onSuccess: () -> Unit) {
@ -21,11 +20,6 @@ class MobidziennikLoginWeb(val data: DataMobidziennik, val onSuccess: () -> Unit
}
init { run {
if (data.profile == null) {
data.error(ApiError(TAG, ERROR_PROFILE_MISSING))
return@run
}
if (data.isWebLoginValid()) {
onSuccess()
}