Merge remote-tracking branch 'origin/api-v2' into api-v2

This commit is contained in:
Kuba Szczodrzyński 2019-10-13 16:27:58 +02:00
commit 7561087c78
11 changed files with 296 additions and 28 deletions

1
.gitignore vendored
View File

@ -47,6 +47,7 @@ captures/
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml
.idea/copyright/profiles_settings.xml
# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.

View File

@ -1,3 +0,0 @@
<component name="CopyrightManager">
<settings default="kubasz" />
</component>

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

@ -5,15 +5,8 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_EVENTS
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ME
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADES
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_SCHOOLS
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiEvents
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiGrades
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiSchools
import pl.szczodrzynski.edziennik.api.v2.librus.*
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.*
import pl.szczodrzynski.edziennik.utils.Utils
class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
@ -43,21 +36,37 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
Utils.d(TAG, "Using endpoint $endpointId")
when (endpointId) {
ENDPOINT_LIBRUS_API_ME -> {
data.startProgress(R.string.edziennik_progress_endpoint_student_info)
data.startProgress(R.string.sync_action_getting_account)
LibrusApiMe(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_SCHOOLS -> {
data.startProgress(R.string.edziennik_progress_endpoint_school_info)
data.startProgress(R.string.sync_action_syncing_school_info)
LibrusApiSchools(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_NORMAL_GRADES -> {
data.startProgress(R.string.edziennik_progress_endpoint_grades)
data.startProgress(R.string.sync_action_syncing_grades)
LibrusApiGrades(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_EVENTS -> {
data.startProgress(R.string.sync_action_syncing_events)
LibrusApiEvents(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_HOMEWORK -> {
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() }
}
ENDPOINT_LIBRUS_API_ANNOUNCEMENTS -> {
data.startProgress(R.string.sync_action_syncing_announcements)
LibrusApiAnnouncements(data) { onSuccess() }
}
else -> onSuccess()
}
}

View File

@ -0,0 +1,68 @@
/*
* 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_ANNOUNCEMENTS
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiAnnouncements(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiAnnouncements"
}
init {
apiGet(TAG, "SchoolNotices") { json ->
val announcements = json.getJsonArray("SchoolNotices")
announcements?.forEach { announcementEl ->
val announcement = announcementEl.asJsonObject
val id = Utils.crc16(announcement.getString("Id")?.toByteArray()
?: return@forEach).toLong()
val subject = announcement.getString("Subject") ?: ""
val text = announcement.getString("Content") ?: ""
val startDate = Date.fromY_m_d(announcement.getString("StartDate")
?: return@forEach)
val endDate = Date.fromY_m_d(announcement.getString("EndDate") ?: return@forEach)
val teacherId = announcement.getJsonObject("AddedBy")?.getLong("Id") ?: -1
val announcementObject = Announcement(
profileId,
id,
subject,
text,
startDate,
endDate,
teacherId
)
val addedDate = Date.fromIso(announcement.getString("CreationDate")
?: return@forEach)
val read = announcement.getBoolean("WasRead") ?: false
data.announcementList.add(announcementObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_ANNOUNCEMENT,
id,
read,
read,
addedDate
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_ANNOUNCEMENTS, SYNC_ALWAYS)
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

@ -54,7 +54,7 @@ class LibrusApiEvents(override val data: DataLibrus,
teamId
)
val addedDate = Date.fromIso(event.get("AddDate").asString)
val addedDate = Date.fromIso(event.getString("AddDate") ?: return@forEach)
data.eventList.add(eventObject)
data.metadataList.add(

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-12.
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
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,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiHomework"
}
init {
apiGet(TAG, "HomeWorkAssignments") { json ->
val homeworkList = json.getJsonArray("HomeWorkAssignments")
homeworkList?.forEach { homeworkEl ->
val homework = homeworkEl.asJsonObject
val id = homework.getLong("Id") ?: return@forEach
val eventDate = Date.fromY_m_d(homework.getString("DueDate") ?: return@forEach)
val topic = (homework.getString("Topic") ?: "") + "\n" +
(homework.getString("Text") ?: "")
val teacherId = homework.getJsonObject("Teacher")?.getLong("Id") ?: -1
val eventObject = Event(
profileId,
id,
eventDate,
null,
topic,
-1,
-1,
false,
teacherId,
-1,
-1
)
val addedDate = Date.fromY_m_d(homework.getString("Date") ?: return@forEach)
data.eventList.add(eventObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_HOMEWORK,
id,
profile?.empty ?: false,
profile?.empty ?: false,
addedDate.inMillis
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_HOMEWORK, SYNC_ALWAYS)
onSuccess()
}
}
}

View File

@ -6,6 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
class LibrusApiTemplate(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
@ -14,13 +15,10 @@ class LibrusApiTemplate(override val data: DataLibrus,
}
init {
/*apiGet(LibrusApiMe.TAG, "") { json ->
/*apiGet(TAG, "") { json ->
// on error
data.error(TAG, ERROR_LIBRUS_API_, response, json)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 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)
}
}
}