[APIv2/Librus] Add getting attendances and their types

This commit is contained in:
Kacper Ziubryniewicz 2019-10-13 15:44:52 +02:00
parent 4f9b9c5f7b
commit fb945470c0
5 changed files with 140 additions and 6 deletions

View File

@ -35,7 +35,7 @@ const val ENDPOINT_LIBRUS_API_HOMEWORK = 1050
const val ENDPOINT_LIBRUS_API_LUCKY_NUMBER = 1060
const val ENDPOINT_LIBRUS_API_NOTICES = 1070
const val ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES = 1080
const val ENDPOINT_LIBRUS_API_ATTENDANCE = 1081
const val ENDPOINT_LIBRUS_API_ATTENDANCES = 1081
const val ENDPOINT_LIBRUS_API_ANNOUNCEMENTS = 1090
const val ENDPOINT_LIBRUS_API_PT_MEETINGS = 1100
const val ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS = 1110
@ -85,8 +85,8 @@ val LibrusFeatures = listOf(
ENDPOINT_LIBRUS_API_NOTICES to LOGIN_METHOD_LIBRUS_API
), listOf(LOGIN_METHOD_LIBRUS_API)),
Feature(LOGIN_TYPE_LIBRUS, FEATURE_ATTENDANCE, listOf(
ENDPOINT_LIBRUS_API_ATTENDANCE to LOGIN_METHOD_LIBRUS_API,
ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES to LOGIN_METHOD_LIBRUS_API
ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES to LOGIN_METHOD_LIBRUS_API,
ENDPOINT_LIBRUS_API_ATTENDANCES to LOGIN_METHOD_LIBRUS_API
), listOf(LOGIN_METHOD_LIBRUS_API)),
Feature(LOGIN_TYPE_LIBRUS, FEATURE_ANNOUNCEMENTS, listOf(
ENDPOINT_LIBRUS_API_ANNOUNCEMENTS to LOGIN_METHOD_LIBRUS_API
@ -140,4 +140,4 @@ val LibrusFeatures = listOf(
Feature(LOGIN_TYPE_LIBRUS, FEATURE_MESSAGES_SENT, listOf(
ENDPOINT_LIBRUS_MESSAGES_SENT to LOGIN_METHOD_LIBRUS_MESSAGES
), listOf(LOGIN_METHOD_LIBRUS_MESSAGES))
)
)

View File

@ -55,6 +55,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.sync_action_syncing_homework)
LibrusApiHomework(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES -> {
data.startProgress(R.string.sync_action_syncing_attendance_types)
LibrusApiAttendanceTypes(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_ATTENDANCES -> {
data.startProgress(R.string.sync_action_syncing_attendance)
LibrusApiAttendances(data) { onSuccess() }
}
else -> onSuccess()
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-10-13
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
class LibrusApiAttendanceTypes(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiAttendanceTypes"
}
init {
apiGet(TAG, "Attendances/Types") { json ->
val attendanceTypes = json.getJsonArray("Types")
attendanceTypes?.forEach { attendanceTypeEl ->
val attendanceType = attendanceTypeEl.asJsonObject
val id = attendanceType.getInt("Id") ?: return@forEach
val standardId = when (attendanceType.getBoolean("Standard") ?: false) {
true -> id
false -> attendanceType.getJsonObject("StandardType")?.getInt("Id")
?: return@forEach
}
val name = attendanceType.getString("Name") ?: ""
data.attendanceTypes.put(id, Pair(standardId, name))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES, SYNC_ALWAYS)
onSuccess()
}
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-10-13
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ATTENDANCES
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiAttendances(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiAttendances"
}
init {
apiGet(TAG, "Attendances") { json ->
val attendances = json.getJsonArray("Attendances")
attendances?.forEach { attendanceEl ->
val attendance = attendanceEl.asJsonObject
val teacherId = attendance.getJsonObject("AddedBy")?.getLong("Id") ?: -1
val lessonNo = attendance.getInt("LessonNo") ?: return@forEach
val startTime = data.lessonRanges.get(lessonNo).startTime
val lessonDate = Date.fromY_m_d(attendance.getString("Date") ?: return@forEach)
val semester = attendance.getInt("Semester") ?: return@forEach
var type = attendance.getJsonObject("Type")?.getInt("Id") ?: return@forEach
val attendanceType = data.attendanceTypes.get(type)
val topic = attendanceType.second
val id = Utils.strToInt((attendance.getString("Id") ?: return@forEach)
.replace("[^\\d.]".toRegex(), "")).toLong()
val subjectId = data.lessonList.singleOrNull {
it.weekDay == lessonDate.weekDay && it.startTime.value == startTime.value
}?.subjectId ?: -1
type = when(type) {
1 -> Attendance.TYPE_ABSENT
2 -> Attendance.TYPE_BELATED
3 -> Attendance.TYPE_ABSENT_EXCUSED
4 -> Attendance.TYPE_RELEASED
else -> Attendance.TYPE_PRESENT
}
val attendanceObject = Attendance(
profileId,
id,
teacherId,
subjectId,
semester,
topic,
lessonDate,
startTime,
type
)
val addedDate = Date.fromIso(attendance.getString("AddDate") ?: return@forEach)
data.attendanceList.add(attendanceObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_ATTENDANCE,
id,
profile?.empty ?: false,
profile?.empty ?: false,
addedDate
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_ATTENDANCES, SYNC_ALWAYS)
onSuccess()
}
}
}

View File

@ -103,6 +103,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
val teamList = LongSparseArray<Team>()
val lessonRanges = SparseArray<LessonRange>()
val gradeCategories = LongSparseArray<GradeCategory>()
val attendanceTypes = SparseArray<Pair<Int, String>>()
private var mTeamClass: Team? = null
var teamClass: Team?
@ -129,7 +130,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
var noticesToRemove: DataRemoveModel? = null
val noticeList = mutableListOf<Notice>()
var attendanceToRemove: DataRemoveModel? = null
var attendancesToRemove: DataRemoveModel? = null
val attendanceList = mutableListOf<Attendance>()
var announcementsToRemove: DataRemoveModel? = null
@ -304,4 +305,4 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
fun startProgress(stringRes: Int) {
callback.onStartProgress(stringRes)
}
}
}