[APIv2/Librus] Update endpoints: API/Schools, API/Events

This commit is contained in:
Kuba Szczodrzyński 2019-10-05 12:26:52 +02:00
parent 648699547e
commit fabacfdcca
7 changed files with 77 additions and 19 deletions

View File

@ -18,6 +18,12 @@ open class LibrusApi(open val data: DataLibrus) {
const val TAG = "LibrusApi"
}
val profileId
get() = data.profile?.id ?: -1
val profile
get() = data.profile
fun apiGet(tag: String, endpoint: String, method: Int = GET, payload: JsonObject? = null, onSuccess: (json: JsonObject?) -> Unit) {
val callback = object : JsonCallbackHandler() {

View File

@ -14,13 +14,13 @@ class LibrusApiEvents(override val data: DataLibrus,
}
init {
apiGet(LibrusApiMe.TAG, "") { json ->
/*apiGet(LibrusApiMe.TAG, "") { json ->
// on error
data.error(TAG, ERROR_LIBRUS_API_, response, json)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY)
onSuccess()
}
}*/
}
}

View File

@ -6,11 +6,11 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import android.util.Pair
import com.google.gson.JsonNull
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.getInt
import pl.szczodrzynski.edziennik.getJsonObject
import pl.szczodrzynski.edziennik.getString
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRange
import pl.szczodrzynski.edziennik.utils.models.Time
import java.util.*
@ -26,6 +26,8 @@ class LibrusApiSchools(override val data: DataLibrus,
val schoolId = school?.getInt("Id")
val schoolNameLong = school?.getString("Name")
// create the school's short name using first letters of each long name's word
// append the town name and save to student data
var schoolNameShort = ""
schoolNameLong?.split(" ")?.forEach {
if (it.isBlank())
@ -35,21 +37,24 @@ class LibrusApiSchools(override val data: DataLibrus,
val schoolTown = school?.getString("Town")?.toLowerCase(Locale.getDefault())
data.schoolName = schoolId.toString() + schoolNameShort + "_" + schoolTown
/*lessonRanges.clear()
for ((index, lessonRangeEl) in school.get("LessonsRange").getAsJsonArray().withIndex()) {
val lr = lessonRangeEl.getAsJsonObject()
val from = lr.get("From")
val to = lr.get("To")
if (from != null && to != null && from !is JsonNull && to !is JsonNull) {
lessonRanges.put(index, Pair<Time, Time>(Time.fromH_m(from!!.getAsString()), Time.fromH_m(to!!.getAsString())))
school?.getJsonArray("LessonsRange")?.let { ranges ->
data.lessonRanges.clear()
ranges.forEachIndexed { index, rangeEl ->
val range = rangeEl.asJsonObject
val from = range.getString("From") ?: return@forEachIndexed
val to = range.getString("To") ?: return@forEachIndexed
data.lessonRanges.add(
LessonRange(
profileId,
index,
Time.fromH_m(from),
Time.fromH_m(to)
))
}
}
profile.putStudentData("lessonRanges", app.gson.toJson(lessonRanges))
// on error
data.error(TAG, ERROR_LIBRUS_API_, response, json)
data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY)
onSuccess()*/
data.setSyncNext(ENDPOINT_LIBRUS_API_SCHOOLS, 4 * DAY)
onSuccess()
}
}
}

View File

@ -17,6 +17,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade
import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory
import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRange
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient
@ -90,6 +91,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
val teacherList = LongSparseArray<Teacher>()
val subjectList = LongSparseArray<Subject>()
val teamList = mutableListOf<Team>()
var lessonRanges = mutableListOf<LessonRange>()
var lessonsToRemove: DataRemoveModel? = null
val lessonList = mutableListOf<Lesson>()
@ -129,6 +131,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
db.endpointTimerDao().getAllNow(profile.id).forEach { endpointTimer ->
endpointTimers.add(endpointTimer)
}
lessonRanges = db.lessonRangeDao().getAllNow(profile.id).toMutableList()
db.teacherDao().getAllNow(profile.id).forEach { teacher ->
teacherList.put(teacher.id, teacher)
}
@ -151,6 +155,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
teacherList.clear()
subjectList.clear()
teamList.clear()
lessonRanges.clear()
lessonList.clear()
lessonChangeList.clear()
gradeCategoryList.clear()

View File

@ -35,6 +35,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson;
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange;
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChangeDao;
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonDao;
import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRangeDao;
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore;
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStoreDao;
import pl.szczodrzynski.edziennik.data.db.modules.luckynumber.LuckyNumber;
@ -84,7 +85,7 @@ import android.content.Context;
MessageRecipient.class,
DebugLog.class,
EndpointTimer.class,
Metadata.class}, version = 55)
Metadata.class}, version = 56)
@TypeConverters({
ConverterTime.class,
ConverterDate.class,
@ -115,6 +116,7 @@ public abstract class AppDb extends RoomDatabase {
public abstract MessageRecipientDao messageRecipientDao();
public abstract DebugLogDao debugLogDao();
public abstract EndpointTimerDao endpointTimerDao();
public abstract LessonRangeDao lessonRangeDao();
public abstract MetadataDao metadataDao();
private static volatile AppDb INSTANCE;
@ -575,6 +577,18 @@ public abstract class AppDb extends RoomDatabase {
")");
}
};
private static final Migration MIGRATION_55_56 = new Migration(55, 56) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE IF NOT EXISTS lessonRanges (" +
"profileId INTEGER NOT NULL," +
"lessonRangeNumber INTEGER NOT NULL," +
"lessonRangeStart TEXT NOT NULL," +
"lessonRangeEnd TEXT NOT NULL," +
"PRIMARY KEY(profileId, lessonRangeNumber)" +
")");
}
};
public static AppDb getDatabase(final Context context) {
@ -627,7 +641,8 @@ public abstract class AppDb extends RoomDatabase {
MIGRATION_51_52,
MIGRATION_52_53,
MIGRATION_53_54,
MIGRATION_54_55
MIGRATION_54_55,
MIGRATION_55_56
)
.allowMainThreadQueries()
//.fallbackToDestructiveMigration()

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) Kuba Szczodrzyński 2019-10-5.
*/
package pl.szczodrzynski.edziennik.data.db.modules.lessons
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@Dao
interface LessonRangeDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun add(lessonRange: LessonRange)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addAll(lessonRangeList: List<LessonRange>)
@Query("SELECT * FROM lessonRanges WHERE profileId = :profileId")
fun getAllNow(profileId: Int): List<LessonRange>
@Query("DELETE FROM lessonRanges WHERE profileId = :profileId")
fun clear(profileId: Int)
}

View File

@ -924,4 +924,5 @@
<string name="agenda_teacher_absence">Nieobecni nauczyciele</string>
<string name="sync_action_syncing_grade_comments">Pobieranie komentarzy ocen</string>
<string name="settings_register_show_teacher_absences_text">Pokazuj nieobecności nauczycieli w Terminarzu</string>
<string name="edziennik_progress_endpoint_school_info">Pobieram informacje o szkole...</string>
</resources>