[APIv2/Librus] Add Attendance types, Classrooms, Event types, Notices, Notice types, PT meetings. Simplify JSON array iteration.

This commit is contained in:
Kuba Szczodrzyński 2019-10-24 22:15:35 +02:00
parent 05ce790587
commit 35ed31f6b9
26 changed files with 464 additions and 114 deletions

View File

@ -14,8 +14,8 @@ import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import im.wangchao.mhttp.Response
import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher
import pl.szczodrzynski.edziennik.data.db.modules.teams.Team
import pl.szczodrzynski.navlib.R
import pl.szczodrzynski.navlib.crc16
@ -48,6 +48,8 @@ fun JsonObject?.getFloat(key: String, defaultValue: Float): Float = get(key)?.le
fun JsonObject?.getJsonObject(key: String, defaultValue: JsonObject): JsonObject = get(key)?.let { if (it.isJsonNull) defaultValue else it.asJsonObject } ?: defaultValue
fun JsonObject?.getJsonArray(key: String, defaultValue: JsonArray): JsonArray = get(key)?.let { if (it.isJsonNull) defaultValue else it.asJsonArray } ?: defaultValue
fun JsonArray?.asJsonObjectList() = this?.map { it.asJsonObject }
fun CharSequence?.isNotNullNorEmpty(): Boolean {
return this != null && this.isNotEmpty()
}

View File

@ -51,6 +51,43 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app
}
}
fun getColor(id: Int?): Int {
return when (id) {
1 -> 0xFFF0E68C
2 -> 0xFF87CEFA
3 -> 0xFFB0C4DE
4 -> 0xFFF0F8FF
5 -> 0xFFF0FFFF
6 -> 0xFFF5F5DC
7 -> 0xFFFFEBCD
8 -> 0xFFFFF8DC
9 -> 0xFFA9A9A9
10 -> 0xFFBDB76B
11 -> 0xFF8FBC8F
12 -> 0xFFDCDCDC
13 -> 0xFFDAA520
14 -> 0xFFE6E6FA
15 -> 0xFFFFA07A
16 -> 0xFF32CD32
17 -> 0xFF66CDAA
18 -> 0xFF66CDAA
19 -> 0xFFC0C0C0
20 -> 0xFFD2B48C
21 -> 0xFF3333FF
22 -> 0xFF7B68EE
23 -> 0xFFBA55D3
24 -> 0xFFFFB6C1
25 -> 0xFFFF1493
26 -> 0xFFDC143C
27 -> 0xFFFF0000
28 -> 0xFFFF8C00
29 -> 0xFFFFD700
30 -> 0xFFADFF2F
31 -> 0xFF7CFC00
else -> 0xff2196f3
}.toInt()
}
/* _____ _ _
| __ \ | | | |
| |__) |__ _ __| |_ __ _| |

View File

@ -30,11 +30,12 @@ const val ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES = 1033
const val ENDPOINT_LIBRUS_API_TEXT_GRADES = 1034
const val ENDPOINT_LIBRUS_API_DESCRIPTIVE_TEXT_GRADES = 1035
const val ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADES = 1036
const val ENDPOINT_LIBRUS_API_EVENTS = 1040
const val ENDPOINT_LIBRUS_API_EVENT_TYPES = 1041
const val ENDPOINT_LIBRUS_API_EVENT_TYPES = 1040
const val ENDPOINT_LIBRUS_API_EVENTS = 1041
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_NOTICE_TYPES = 1070
const val ENDPOINT_LIBRUS_API_NOTICES = 1071
const val ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES = 1080
const val ENDPOINT_LIBRUS_API_ATTENDANCES = 1081
const val ENDPOINT_LIBRUS_API_ANNOUNCEMENTS = 1090

View File

@ -45,10 +45,43 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_school_info)
LibrusApiSchools(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_CLASSES -> {
data.startProgress(R.string.edziennik_progress_endpoint_classes)
LibrusApiClasses(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_VIRTUAL_CLASSES -> {
data.startProgress(R.string.edziennik_progress_endpoint_teams)
LibrusApiVirtualClasses(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_UNITS -> {
data.startProgress(R.string.edziennik_progress_endpoint_units)
LibrusApiUnits(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_USERS -> {
data.startProgress(R.string.edziennik_progress_endpoint_teachers)
LibrusApiUsers(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_SUBJECTS -> {
data.startProgress(R.string.edziennik_progress_endpoint_subjects)
LibrusApiSubjects(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_CLASSROOMS -> {
data.startProgress(R.string.edziennik_progress_endpoint_classrooms)
LibrusApiClassrooms(data) { onSuccess() }
}
// TODO push config
// TODO timetable
ENDPOINT_LIBRUS_API_NORMAL_GRADES -> {
data.startProgress(R.string.edziennik_progress_endpoint_grades)
LibrusApiGrades(data) { onSuccess() }
}
// TODO grades
ENDPOINT_LIBRUS_API_EVENT_TYPES -> {
data.startProgress(R.string.edziennik_progress_endpoint_event_types)
LibrusApiEventTypes(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_EVENTS -> {
data.startProgress(R.string.edziennik_progress_endpoint_events)
LibrusApiEvents(data) { onSuccess() }
@ -57,6 +90,18 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_homework)
LibrusApiHomework(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_LUCKY_NUMBER -> {
data.startProgress(R.string.edziennik_progress_endpoint_lucky_number)
LibrusApiLuckyNumber(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_NOTICE_TYPES -> {
data.startProgress(R.string.edziennik_progress_endpoint_notice_types)
LibrusApiNoticeTypes(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_NOTICES -> {
data.startProgress(R.string.edziennik_progress_endpoint_notices)
LibrusApiNotices(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES -> {
data.startProgress(R.string.edziennik_progress_endpoint_attendance_types)
LibrusApiAttendanceTypes(data) { onSuccess() }
@ -69,13 +114,9 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_announcements)
LibrusApiAnnouncements(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_LUCKY_NUMBER -> {
data.startProgress(R.string.edziennik_progress_endpoint_lucky_number)
LibrusApiLuckyNumber(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_CLASSES -> {
data.startProgress(R.string.edziennik_progress_endpoint_classes)
LibrusApiClasses(data) { onSuccess() }
ENDPOINT_LIBRUS_API_PT_MEETINGS -> {
data.startProgress(R.string.edziennik_progress_endpoint_pt_meetings)
LibrusApiPtMeetings(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES -> {
data.startProgress(R.string.edziennik_progress_endpoint_teacher_free_day_types)
@ -85,6 +126,7 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_teacher_free_days)
LibrusApiTeacherFreeDays(data) { onSuccess() }
}
ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK -> {
data.startProgress(R.string.edziennik_progress_endpoint_homework)
LibrusSynergiaHomework(data) { onSuccess() }

View File

@ -22,11 +22,9 @@ class LibrusApiAnnouncements(override val data: DataLibrus,
init {
apiGet(TAG, "SchoolNotices") { json ->
val announcements = json.getJsonArray("SchoolNotices")
announcements?.forEach { announcementEl ->
val announcement = announcementEl.asJsonObject
val announcements = json.getJsonArray("SchoolNotices").asJsonObjectList()
announcements?.forEach { announcement ->
val id = Utils.crc16(announcement.getString("Id")?.toByteArray()
?: return@forEach).toLong()
val subject = announcement.getString("Subject") ?: ""

View File

@ -4,11 +4,13 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import android.graphics.Color
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
import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance
import pl.szczodrzynski.edziennik.data.db.modules.attendance.AttendanceType
class LibrusApiAttendanceTypes(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
@ -18,24 +20,29 @@ class LibrusApiAttendanceTypes(override val data: DataLibrus,
init {
apiGet(TAG, "Attendances/Types") { json ->
val attendanceTypes = json.getJsonArray("Types")
val attendanceTypes = json.getJsonArray("Types").asJsonObjectList()
attendanceTypes?.forEach { attendanceTypeEl ->
val attendanceType = attendanceTypeEl.asJsonObject
attendanceTypes?.forEach { attendanceType ->
val id = attendanceType.getLong("Id") ?: return@forEach
val name = attendanceType.getString("Name") ?: ""
val color = attendanceType.getString("ColorRGB")?.let { Color.parseColor("#$it") } ?: -1
val id = attendanceType.getInt("Id") ?: return@forEach
val standardId = when (attendanceType.getBoolean("Standard") ?: false) {
true -> id
false -> attendanceType.getJsonObject("StandardType")?.getInt("Id")
?: return@forEach
false -> attendanceType.getJsonObject("StandardType")?.getLong("Id") ?: id
}
val type = when (standardId) {
1L -> Attendance.TYPE_ABSENT
2L -> Attendance.TYPE_BELATED
3L -> Attendance.TYPE_ABSENT_EXCUSED
4L -> Attendance.TYPE_RELEASED
/*100*/else -> Attendance.TYPE_PRESENT
}
val name = attendanceType.getString("Name") ?: ""
data.attendanceTypes.put(id, Pair(standardId, name))
data.attendanceTypes.put(id, AttendanceType(profileId, id, name, type, color))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES, SYNC_ALWAYS)
data.setSyncNext(ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES, 4*DAY)
onSuccess()
}
}

View File

@ -4,6 +4,7 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import androidx.core.util.isEmpty
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ATTENDANCES
@ -21,12 +22,14 @@ class LibrusApiAttendances(override val data: DataLibrus,
}
init {
if (data.attendanceTypes.isEmpty()) {
data.db.attendanceTypeDao().getAllNow(profileId).toSparseArray(data.attendanceTypes) { it.id }
}
apiGet(TAG, "Attendances") { json ->
val attendances = json.getJsonArray("Attendances")
attendances?.forEach { attendanceEl ->
val attendance = attendanceEl.asJsonObject
val attendances = json.getJsonArray("Attendances").asJsonObjectList()
attendances?.forEach { attendance ->
val id = Utils.strToInt((attendance.getString("Id") ?: return@forEach)
.replace("[^\\d.]".toRegex(), "")).toLong()
val teacherId = attendance.getJsonObject("AddedBy")?.getLong("Id") ?: -1
@ -37,17 +40,9 @@ class LibrusApiAttendances(override val data: DataLibrus,
it.weekDay == lessonDate.weekDay && it.startTime.value == startTime.value
}?.subjectId ?: -1
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
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 type = attendance.getJsonObject("Type")?.getLong("Id") ?: return@forEach
val typeObject = data.attendanceTypes.get(type)
val topic = typeObject?.name ?: ""
val attendanceObject = Attendance(
profileId,
@ -58,13 +53,13 @@ class LibrusApiAttendances(override val data: DataLibrus,
topic,
lessonDate,
startTime,
type
typeObject.type
)
val addedDate = Date.fromIso(attendance.getString("AddDate") ?: return@forEach)
data.attendanceList.add(attendanceObject)
if(type != Attendance.TYPE_PRESENT) {
if(typeObject.type != Attendance.TYPE_PRESENT) {
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_ATTENDANCE,

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-24.
*/
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_CLASSROOMS
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.classrooms.Classroom
import java.util.*
class LibrusApiClassrooms(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiClassrooms"
}
init {
apiGet(TAG, "Classrooms") { json ->
val classrooms = json.getJsonArray("Classrooms").asJsonObjectList()
classrooms?.forEach { classroom ->
val id = classroom.getLong("Id") ?: return@forEach
val name = classroom.getString("Name")?.toLowerCase(Locale.getDefault()) ?: ""
val symbol = classroom.getString("Symbol")?.toLowerCase(Locale.getDefault()) ?: ""
val nameShort = name.split(" ").onEach { it[0] }.joinToString()
val friendlyName = if (name != symbol && !name.contains(symbol) && !nameShort.contains(symbol)) {
classroom.getString("Symbol") + " " + classroom.getString("Name")
}
else {
classroom.getString("Name") ?: ""
}
data.classrooms.put(id, Classroom(profileId, id, friendlyName))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_CLASSROOMS, 4*DAY)
onSuccess()
}
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-24.
*/
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_EVENT_TYPES
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.events.EventType
class LibrusApiEventTypes(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiEventTypes"
}
init {
apiGet(TAG, "HomeWorks/Categories") { json ->
val eventTypes = json.getJsonArray("Categories").asJsonObjectList()
eventTypes?.forEach { eventType ->
val id = eventType.getLong("Id") ?: return@forEach
val name = eventType.getString("Name") ?: ""
val color = data.getColor(eventType.getJsonObject("Color")?.getInt("Id"))
data.eventTypes.put(id, EventType(profileId, id, name, color))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_EVENT_TYPES, 4*DAY)
onSuccess()
}
}
}

View File

@ -4,9 +4,10 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import androidx.core.util.isEmpty
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_EVENTS
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_EVENTS
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.events.Event
@ -21,12 +22,14 @@ class LibrusApiEvents(override val data: DataLibrus,
}
init {
if (data.eventTypes.isEmpty()) {
data.db.eventTypeDao().getAllNow(profileId).toSparseArray(data.eventTypes) { it.id }
}
apiGet(TAG, "HomeWorks") { json ->
val events = json.getJsonArray("HomeWorks")
events?.forEach { eventEl ->
val event = eventEl.asJsonObject
val events = json.getJsonArray("HomeWorks").asJsonObjectList()
events?.forEach { event ->
val id = event.getLong("Id") ?: return@forEach
val eventDate = Date.fromY_m_d(event.getString("Date"))
val topic = event.getString("Content") ?: ""

View File

@ -1,8 +1,8 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADES
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADES
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.grades.Grade
@ -10,7 +10,6 @@ import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiGrades(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
@ -19,11 +18,9 @@ class LibrusApiGrades(override val data: DataLibrus,
init {
apiGet(TAG, "Grades") { json ->
val grades = json.getJsonArray("Grades")
grades?.forEach { gradeEl ->
val grade = gradeEl.asJsonObject
val grades = json.getJsonArray("Grades").asJsonObjectList()
grades?.forEach { grade ->
val id = grade.getLong("Id") ?: return@forEach
val categoryId = grade.getJsonObject("Category")?.getLong("Id") ?: -1
val name = grade.getString("Grade") ?: ""

View File

@ -4,16 +4,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_HOMEWORK
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.events.Event
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.getJsonArray
import pl.szczodrzynski.edziennik.getJsonObject
import pl.szczodrzynski.edziennik.getLong
import pl.szczodrzynski.edziennik.getString
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiHomework(override val data: DataLibrus,
@ -24,11 +21,9 @@ class LibrusApiHomework(override val data: DataLibrus,
init {
apiGet(TAG, "HomeWorkAssignments") { json ->
val homeworkList = json.getJsonArray("HomeWorkAssignments")
homeworkList?.forEach { homeworkEl ->
val homework = homeworkEl.asJsonObject
val homeworkList = json.getJsonArray("HomeWorkAssignments").asJsonObjectList()
homeworkList?.forEach { homework ->
val id = homework.getLong("Id") ?: return@forEach
val eventDate = Date.fromY_m_d(homework.getString("DueDate"))
val topic = homework.getString("Topic") + "\n" + homework.getString("Text")

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-24.
*/
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_NOTICE_TYPES
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.notices.NoticeType
class LibrusApiNoticeTypes(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiNoticeTypes"
}
init {
apiGet(TAG, "Notes/Categories") { json ->
val noticeTypes = json.getJsonArray("Categories").asJsonObjectList()
noticeTypes?.forEach { noticeType ->
val id = noticeType.getLong("Id") ?: return@forEach
val name = noticeType.getString("CategoryName") ?: ""
data.noticeTypes.put(id, NoticeType(profileId, id, name))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_NOTICE_TYPES, 4*DAY)
onSuccess()
}
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-24.
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import androidx.core.util.isEmpty
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_NOTICES
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.metadata.Metadata
import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiNotices(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiNotices"
}
init {
if (data.noticeTypes.isEmpty()) {
data.db.noticeTypeDao().getAllNow(profileId).toSparseArray(data.noticeTypes) { it.id }
}
apiGet(TAG, "Notes") { json ->
val notes = json.getJsonArray("Notes").asJsonObjectList()
notes?.forEach { note ->
val id = note.getLong("Id") ?: return@forEach
val text = note.getString("Text") ?: ""
val categoryId = note.getJsonObject("Category")?.getLong("Id") ?: -1
val teacherId = note.getJsonObject("AddedBy")?.getLong("Id") ?: -1
val addedDate = note.getString("Date")?.let { Date.fromY_m_d(it) } ?: return@forEach
val type = when (note.getInt("Positive")) {
0 -> Notice.TYPE_NEGATIVE
1 -> Notice.TYPE_POSITIVE
/*2*/else -> Notice.TYPE_NEUTRAL
}
val categoryText = data.noticeTypes[categoryId]?.name ?: ""
val semester = profile?.dateToSemester(addedDate) ?: 1
val noticeObject = Notice(
profileId,
id,
categoryText+"\n"+text,
semester,
type,
teacherId
)
data.noticeList.add(noticeObject)
data.metadataList.add(
Metadata(
profileId,
Metadata.TYPE_NOTICE,
id,
profile?.empty ?: false,
profile?.empty ?: false,
addedDate.inMillis
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_NOTICES, SYNC_ALWAYS)
onSuccess()
}
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-24.
*/
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_PT_MEETINGS
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.utils.models.Date
import pl.szczodrzynski.edziennik.utils.models.Time
class LibrusApiPtMeetings(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiPtMeetings"
}
init {
apiGet(TAG, "ParentTeacherConferences") { json ->
val ptMeetings = json.getJsonArray("ParentTeacherConferences").asJsonObjectList()
ptMeetings?.forEach { meeting ->
val id = meeting.getLong("Id") ?: return@forEach
val topic = meeting.getString("Topic") ?: ""
val teacherId = meeting.getJsonObject("Teacher")?.getLong("Id") ?: -1
val eventDate = meeting.getString("Date")?.let { Date.fromY_m_d(it) } ?: return@forEach
val startTime = meeting.getString("Time")?.let {
if (it == "00:00:00")
null
else
Time.fromH_m_s(it)
}
val eventObject = Event(
profileId,
id,
eventDate,
startTime,
topic,
-1,
Event.TYPE_PT_MEETING,
false,
teacherId,
-1,
data.teamClass?.id ?: -1
)
data.eventList.add(eventObject)
data.metadataList.add(
Metadata(
profileId,
Metadata.TYPE_EVENT,
id,
profile?.empty ?: false,
profile?.empty ?: false,
System.currentTimeMillis()
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_PT_MEETINGS, 12*HOUR)
onSuccess()
}
}
}

View File

@ -4,14 +4,11 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.DAY
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_SUBJECTS
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject
import pl.szczodrzynski.edziennik.getJsonArray
import pl.szczodrzynski.edziennik.getLong
import pl.szczodrzynski.edziennik.getString
class LibrusApiSubjects(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
@ -21,9 +18,9 @@ class LibrusApiSubjects(override val data: DataLibrus,
init {
apiGet(TAG, "Subjects") { json ->
json.getJsonArray("Subjects")?.forEach { subjectEl ->
val subject = subjectEl.asJsonObject
val subjects = json.getJsonArray("Subjects").asJsonObjectList()
subjects?.forEach { subject ->
val id = subject.getLong("Id") ?: return@forEach
val longName = subject.getString("Name") ?: ""
val shortName = subject.getString("Short") ?: ""

View File

@ -4,15 +4,11 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.DAY
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES
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.teachers.TeacherAbsenceType
import pl.szczodrzynski.edziennik.getJsonArray
import pl.szczodrzynski.edziennik.getLong
import pl.szczodrzynski.edziennik.getString
class LibrusApiTeacherFreeDayTypes(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
@ -22,11 +18,9 @@ class LibrusApiTeacherFreeDayTypes(override val data: DataLibrus,
init {
apiGet(TAG, "TeacherFreeDays/Types") { json ->
val teacherAbsenceTypes = json.getJsonArray("Types")
teacherAbsenceTypes?.forEach { teacherAbsenceTypeEl ->
val teacherAbsenceType = teacherAbsenceTypeEl.asJsonObject
val teacherAbsenceTypes = json.getJsonArray("Types").asJsonObjectList()
teacherAbsenceTypes?.forEach { teacherAbsenceType ->
val id = teacherAbsenceType.getLong("Id") ?: return@forEach
val name = teacherAbsenceType.getString("Name") ?: return@forEach

View File

@ -4,6 +4,7 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import androidx.core.util.isEmpty
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_AGENDA
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
@ -21,12 +22,14 @@ class LibrusApiTeacherFreeDays(override val data: DataLibrus,
}
init {
if (data.teacherAbsenceTypes.isEmpty()) {
data.db.teacherAbsenceTypeDao().getAllNow(profileId).toSparseArray(data.teacherAbsenceTypes) { it.id }
}
apiGet(TAG, "TeacherFreeDays") { json ->
val teacherAbsences = json.getJsonArray("TeacherFreeDays")
teacherAbsences?.forEach { teacherAbsenceEl ->
val teacherAbsence = teacherAbsenceEl.asJsonObject
val teacherAbsences = json.getJsonArray("TeacherFreeDays").asJsonObjectList()
teacherAbsences?.forEach { teacherAbsence ->
val id = teacherAbsence.getLong("Id") ?: return@forEach
val teacherId = teacherAbsence.getJsonObject("Teacher")?.getLong("Id")
?: return@forEach

View File

@ -23,9 +23,9 @@ class LibrusApiUnits(override val data: DataLibrus,
}
apiGet(TAG, "Units") { json ->
val units = json.getJsonArray("Units")
units?.singleOrNull { it.asJsonObject.getLong("Id") == data.unitId }?.also { unitEl ->
val unit = unitEl.asJsonObject
val units = json.getJsonArray("Units").asJsonObjectList()
units?.singleOrNull { it.getLong("Id") == data.unitId }?.also { unit ->
val startPoints = unit.getJsonObject("BehaviourGradesSettings")?.getJsonObject("StartPoints")
startPoints?.apply {
data.startPointsSemester1 = getInt("Semester1", defaultValue = 0)

View File

@ -18,9 +18,9 @@ class LibrusApiUsers(override val data: DataLibrus,
init {
apiGet(TAG, "Users") { json ->
json.getJsonArray("Users")?.forEach { userEl ->
val user = userEl.asJsonObject
val users = json.getJsonArray("Users").asJsonObjectList()
users?.forEach { user ->
val id = user.getLong("Id") ?: return@forEach
val firstName = user.getString("FirstName")?.fixWhiteSpaces() ?: ""
val lastName = user.getString("LastName")?.fixWhiteSpaces() ?: ""

View File

@ -18,9 +18,9 @@ class LibrusApiVirtualClasses(override val data: DataLibrus,
init {
apiGet(TAG, "VirtualClasses") { json ->
json.getJsonArray("VirtualClasses")?.forEach { virtualClassEl ->
val virtualClass = virtualClassEl.asJsonObject
val virtualClasses = json.getJsonArray("VirtualClasses").asJsonObjectList()
virtualClasses?.forEach { virtualClass ->
val id = virtualClass.getLong("Id") ?: return@forEach
val name = virtualClass.getString("Name") ?: ""
val teacherId = virtualClass.getJsonObject("Teacher")?.getLong("Id") ?: -1

View File

@ -2,6 +2,7 @@ package pl.szczodrzynski.edziennik.api.v2.models
import android.util.LongSparseArray
import android.util.SparseArray
import androidx.core.util.size
import com.google.gson.JsonObject
import im.wangchao.mhttp.Response
import pl.szczodrzynski.edziennik.App
@ -14,6 +15,8 @@ import pl.szczodrzynski.edziennik.data.db.AppDb
import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement
import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer
import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance
import pl.szczodrzynski.edziennik.data.db.modules.attendance.AttendanceType
import pl.szczodrzynski.edziennik.data.db.modules.classrooms.Classroom
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
import pl.szczodrzynski.edziennik.data.db.modules.events.EventType
import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade
@ -27,6 +30,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice
import pl.szczodrzynski.edziennik.data.db.modules.notices.NoticeType
import pl.szczodrzynski.edziennik.data.db.modules.notification.Notification
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject
@ -102,7 +106,11 @@ 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>>()
val classrooms = LongSparseArray<Classroom>()
val attendanceTypes = LongSparseArray<AttendanceType>()
val noticeTypes = LongSparseArray<NoticeType>()
val eventTypes = LongSparseArray<EventType>()
val teacherAbsenceTypes = LongSparseArray<TeacherAbsenceType>()
private var mTeamClass: Team? = null
@ -125,7 +133,6 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
var eventsToRemove: DataRemoveModel? = null
val eventList = mutableListOf<Event>()
val eventTypeList = mutableListOf<EventType>()
var noticesToRemove: DataRemoveModel? = null
val noticeList = mutableListOf<Notice>()
@ -158,7 +165,6 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
db.teamDao().getAllNow(profileId).toSparseArray(teamList) { it.id }
db.lessonRangeDao().getAllNow(profileId).toSparseArray(lessonRanges) { it.lessonNumber }
db.gradeCategoryDao().getAllNow(profileId).toSparseArray(gradeCategories) { it.categoryId }
db.teacherAbsenceTypeDao().getAllNow(profileId).toSparseArray(teacherAbsenceTypes) { it.id }
}
}
@ -170,12 +176,18 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
subjectList.clear()
teamList.clear()
lessonRanges.clear()
gradeCategories.clear()
classrooms.clear()
attendanceTypes.clear()
noticeTypes.clear()
eventTypes.clear()
teacherAbsenceTypes.clear()
lessonList.clear()
lessonChangeList.clear()
gradeCategories.clear()
gradeList.clear()
eventTypeList.clear()
noticeList.clear()
attendanceList.clear()
announcementList.clear()
@ -239,8 +251,17 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
db.lessonRangeDao().addAll(lessonRanges.values())
db.gradeCategoryDao().clear(profileId)
db.gradeCategoryDao().addAll(gradeCategories.values())
db.teacherAbsenceTypeDao().clear(profileId)
db.teacherAbsenceTypeDao().addAll(teacherAbsenceTypes.values())
if (classrooms.size > 0)
db.classroomDao().addAll(classrooms.values())
if (attendanceTypes.size > 0)
db.attendanceTypeDao().addAll(attendanceTypes.values())
if (noticeTypes.size > 0)
db.noticeTypeDao().addAll(noticeTypes.values())
if (eventTypes.size > 0)
db.eventTypeDao().addAll(eventTypes.values())
if (teacherAbsenceTypes.size > 0)
db.teacherAbsenceTypeDao().addAll(teacherAbsenceTypes.values())
gradesToRemove?.let { it ->
it.removeAll?.let { _ -> db.gradeDao().clear(profileId) }
@ -260,8 +281,6 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
db.eventDao().removeFuture(profile.id, Date.getToday())
db.eventDao().addAll(eventList)
}
if (eventTypeList.isNotEmpty())
db.eventTypeDao().addAll(eventTypeList)
if (noticeList.isNotEmpty()) {
db.noticeDao().clear(profile.id)
db.noticeDao().addAll(noticeList)

View File

@ -11,14 +11,14 @@ public class EventType {
public int profileId;
@ColumnInfo(name = "eventType")
public int id;
public long id;
@ColumnInfo(name = "eventTypeName")
public String name;
@ColumnInfo(name = "eventTypeColor")
public int color;
public EventType(int profileId, int id, String name, int color) {
public EventType(int profileId, long id, String name, int color) {
this.profileId = profileId;
this.id = id;
this.name = name;

View File

@ -34,8 +34,8 @@ import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;
import java.util.List;
import pl.szczodrzynski.edziennik.App;
import pl.szczodrzynski.edziennik.R;
import pl.szczodrzynski.edziennik.MainActivity;
import pl.szczodrzynski.edziennik.R;
import pl.szczodrzynski.edziennik.data.api.AppError;
import pl.szczodrzynski.edziennik.data.db.modules.events.Event;
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull;
@ -46,12 +46,12 @@ import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull;
import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject;
import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher;
import pl.szczodrzynski.edziennik.data.db.modules.teams.Team;
import pl.szczodrzynski.edziennik.utils.models.Date;
import pl.szczodrzynski.edziennik.utils.models.Time;
import pl.szczodrzynski.edziennik.utils.models.Week;
import pl.szczodrzynski.edziennik.network.ServerRequest;
import pl.szczodrzynski.edziennik.utils.TextInputDropDown;
import pl.szczodrzynski.edziennik.utils.Themes;
import pl.szczodrzynski.edziennik.utils.models.Date;
import pl.szczodrzynski.edziennik.utils.models.Time;
import pl.szczodrzynski.edziennik.utils.models.Week;
import static pl.szczodrzynski.edziennik.App.APP_URL;
import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OTHER;
@ -727,7 +727,7 @@ public class EventManualDialog {
popup.setOnMenuItemClickListener(item -> {
if (item.getGroupId() == 0 && eventTypes != null) {
EventType typeObj = eventTypes.get(item.getItemId());
eventType = typeObj.id;
eventType = (int)typeObj.id;
eventColor = -1; // set -1 as it's the event type's default color
registerEventManualColorPreview.setBackgroundColor(typeObj.color); // set event type's color here to show how will it look
}

View File

@ -127,17 +127,17 @@ class LoginSyncFragment : Fragment() {
profile.loginStoreId = loginStoreId
loginStore.id = loginStoreId
val typeList = listOf(
EventType(profileId, TYPE_HOMEWORK, getString(R.string.event_type_homework), COLOR_HOMEWORK),
EventType(profileId, TYPE_DEFAULT, getString(R.string.event_other), COLOR_DEFAULT),
EventType(profileId, TYPE_EXAM, getString(R.string.event_exam), COLOR_EXAM),
EventType(profileId, TYPE_SHORT_QUIZ, getString(R.string.event_short_quiz), COLOR_SHORT_QUIZ),
EventType(profileId, TYPE_ESSAY, getString(R.string.event_essay), COLOR_SHORT_QUIZ),
EventType(profileId, TYPE_PROJECT, getString(R.string.event_project), COLOR_PROJECT),
EventType(profileId, TYPE_PT_MEETING, getString(R.string.event_pt_meeting), COLOR_PT_MEETING),
EventType(profileId, TYPE_EXCURSION, getString(R.string.event_excursion), COLOR_EXCURSION),
EventType(profileId, TYPE_READING, getString(R.string.event_reading), COLOR_READING),
EventType(profileId, TYPE_CLASS_EVENT, getString(R.string.event_class_event), COLOR_CLASS_EVENT),
EventType(profileId, TYPE_INFORMATION, getString(R.string.event_information), COLOR_INFORMATION)
EventType(profileId, TYPE_HOMEWORK.toLong(), getString(R.string.event_type_homework), COLOR_HOMEWORK),
EventType(profileId, TYPE_DEFAULT.toLong(), getString(R.string.event_other), COLOR_DEFAULT),
EventType(profileId, TYPE_EXAM.toLong(), getString(R.string.event_exam), COLOR_EXAM),
EventType(profileId, TYPE_SHORT_QUIZ.toLong(), getString(R.string.event_short_quiz), COLOR_SHORT_QUIZ),
EventType(profileId, TYPE_ESSAY.toLong(), getString(R.string.event_essay), COLOR_SHORT_QUIZ),
EventType(profileId, TYPE_PROJECT.toLong(), getString(R.string.event_project), COLOR_PROJECT),
EventType(profileId, TYPE_PT_MEETING.toLong(), getString(R.string.event_pt_meeting), COLOR_PT_MEETING),
EventType(profileId, TYPE_EXCURSION.toLong(), getString(R.string.event_excursion), COLOR_EXCURSION),
EventType(profileId, TYPE_READING.toLong(), getString(R.string.event_reading), COLOR_READING),
EventType(profileId, TYPE_CLASS_EVENT.toLong(), getString(R.string.event_class_event), COLOR_CLASS_EVENT),
EventType(profileId, TYPE_INFORMATION.toLong(), getString(R.string.event_information), COLOR_INFORMATION)
)
app.db.eventTypeDao().addAll(typeList)
app.profileSaveFull(profile, loginStore)

View File

@ -955,4 +955,12 @@
<string name="toolbar_subtitle_syncing">Synchronizuję...</string>
<string name="toolbar_subtitle_syncing_format">[%d%%] %s</string>
<string name="edziennik_progress_endpoint_notices">Pobieranie uwag...</string>
<string name="edziennik_progress_endpoint_teams">Pobieranie grup klasowych...</string>
<string name="edziennik_progress_endpoint_units">Pobieranie informacji o jednostce...</string>
<string name="edziennik_progress_endpoint_teachers">Pobieranie listy nauczycieli...</string>
<string name="edziennik_progress_endpoint_subjects">Pobieranie listy przedmiotów...</string>
<string name="edziennik_progress_endpoint_classrooms">Pobieranie listy sal lekcyjnych...</string>
<string name="edziennik_progress_endpoint_event_types">Pobieranie kategorii wydarzeń...</string>
<string name="edziennik_progress_endpoint_notice_types">Pobieranie kategorii uwag...</string>
<string name="edziennik_progress_endpoint_pt_meetings">Pobieranie zebrań z rodzicami...</string>
</resources>