From 9535f5356339e1eb822aef1695362da61dfd9e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 25 Oct 2022 12:19:59 +0200 Subject: [PATCH] [App] Refactor profile methods as extensions. --- .../data/api/edziennik/EdziennikTask.kt | 2 + .../data/api/edziennik/ProfileArchiver.kt | 27 ++-- .../data/api/edziennik/librus/DataLibrus.kt | 42 +++--- .../data/synergia/LibrusSynergiaHomework.kt | 1 + .../mobidziennik/DataMobidziennik.kt | 12 +- .../data/api/MobidziennikApiAttendance.kt | 1 + .../data/web/MobidziennikWebAttendance.kt | 1 + .../data/web/MobidziennikWebGrades.kt | 1 + .../api/edziennik/podlasie/DataPodlasie.kt | 16 ++- .../data/api/PodlasieApiFinalGrades.kt | 1 + .../api/edziennik/template/DataTemplate.kt | 10 +- .../data/api/edziennik/usos/DataUsos.kt | 4 +- .../data/api/edziennik/vulcan/DataVulcan.kt | 36 ++--- .../edziennik/data/db/entity/Profile.kt | 118 ++-------------- .../data/firebase/SzkolnyLibrusFirebase.kt | 27 +++- .../firebase/SzkolnyMobidziennikFirebase.kt | 1 + .../data/firebase/SzkolnyVulcanFirebase.kt | 4 +- .../edziennik/ext/DataExtensions.kt | 24 ---- .../edziennik/ext/ProfileExtensions.kt | 127 ++++++++++++++++++ .../ui/settings/cards/SettingsRegisterCard.kt | 12 +- .../ui/timetable/TimetableFragment.kt | 8 +- .../edziennik/utils/models/Date.java | 1 + 22 files changed, 273 insertions(+), 203 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/ext/ProfileExtensions.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt index 160db7e8..e90813f2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt @@ -28,6 +28,8 @@ import pl.szczodrzynski.edziennik.data.db.enums.LoginType import pl.szczodrzynski.edziennik.data.db.full.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.full.EventFull import pl.szczodrzynski.edziennik.data.db.full.MessageFull +import pl.szczodrzynski.edziennik.ext.isBeforeYear +import pl.szczodrzynski.edziennik.ext.shouldArchive import pl.szczodrzynski.edziennik.utils.Utils.d import pl.szczodrzynski.edziennik.utils.managers.AvailabilityManager.Error.Type diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/ProfileArchiver.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/ProfileArchiver.kt index ab898447..f4f8db2a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/ProfileArchiver.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/ProfileArchiver.kt @@ -52,28 +52,29 @@ class ProfileArchiver(val app: App, val profile: Profile) { when (profile.loginStoreType) { LoginType.LIBRUS -> { - profile.removeStudentData("isPremium") - profile.removeStudentData("pushDeviceId") - profile.removeStudentData("startPointsSemester1") - profile.removeStudentData("startPointsSemester2") - profile.removeStudentData("enablePointGrades") - profile.removeStudentData("enableDescriptiveGrades") + profile.studentData.remove("isPremium") + profile.studentData.remove("pushDeviceId") + profile.studentData.remove("startPointsSemester1") + profile.studentData.remove("startPointsSemester2") + profile.studentData.remove("enablePointGrades") + profile.studentData.remove("enableDescriptiveGrades") } LoginType.MOBIDZIENNIK -> {} LoginType.VULCAN -> { // DataVulcan.isApiLoginValid() returns false so it will update the semester - profile.removeStudentData("currentSemesterEndDate") - profile.removeStudentData("studentSemesterId") - profile.removeStudentData("studentSemesterNumber") - profile.removeStudentData("semester1Id") - profile.removeStudentData("semester2Id") - profile.removeStudentData("studentClassId") + profile.studentData.remove("currentSemesterEndDate") + profile.studentData.remove("studentSemesterId") + profile.studentData.remove("studentSemesterNumber") + profile.studentData.remove("semester1Id") + profile.studentData.remove("semester2Id") + profile.studentData.remove("studentClassId") } LoginType.IDZIENNIK -> { - profile.removeStudentData("schoolYearId") + profile.studentData.remove("schoolYearId") } LoginType.EDUDZIENNIK -> {} LoginType.PODLASIE -> {} + LoginType.USOS -> {} LoginType.DEMO -> {} LoginType.TEMPLATE -> {} } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt index b5589832..0702c203 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt @@ -10,7 +10,9 @@ import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.LoginMethod import pl.szczodrzynski.edziennik.ext.currentTimeUnix +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.ext.set class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { @@ -117,7 +119,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiLogin: String? = null var apiLogin: String? get() { mApiLogin = mApiLogin ?: profile?.getStudentData("accountLogin", null); return mApiLogin } - set(value) { profile?.putStudentData("accountLogin", value); mApiLogin = value } + set(value) { profile["accountLogin"] = value; mApiLogin = value } /** * A Synergia password. * Used: for login (API Login Method) in Synergia mode. @@ -126,7 +128,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiPassword: String? = null var apiPassword: String? get() { mApiPassword = mApiPassword ?: profile?.getStudentData("accountPassword", null); return mApiPassword } - set(value) { profile?.putStudentData("accountPassword", value); mApiPassword = value } + set(value) { profile["accountPassword"] = value; mApiPassword = value } /** * A JST login Code. @@ -135,7 +137,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiCode: String? = null var apiCode: String? get() { mApiCode = mApiCode ?: loginStore.getLoginData("accountCode", null); return mApiCode } - set(value) { profile?.putStudentData("accountCode", value); mApiCode = value } + set(value) { profile["accountCode"] = value; mApiCode = value } /** * A JST login PIN. * Used only during first login in JST mode. @@ -143,7 +145,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiPin: String? = null var apiPin: String? get() { mApiPin = mApiPin ?: loginStore.getLoginData("accountPin", null); return mApiPin } - set(value) { profile?.putStudentData("accountPin", value); mApiPin = value } + set(value) { profile["accountPin"] = value; mApiPin = value } /** * A Synergia API access token. @@ -154,7 +156,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiAccessToken: String? = null var apiAccessToken: String? get() { mApiAccessToken = mApiAccessToken ?: profile?.getStudentData("accountToken", null); return mApiAccessToken } - set(value) { mApiAccessToken = value; profile?.putStudentData("accountToken", value) ?: return; } + set(value) { mApiAccessToken = value; profile["accountToken"] = value ?: return; } /** * A Synergia API refresh token. * Used when refreshing the [apiAccessToken] in JST, Synergia modes. @@ -162,7 +164,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiRefreshToken: String? = null var apiRefreshToken: String? get() { mApiRefreshToken = mApiRefreshToken ?: profile?.getStudentData("accountRefreshToken", null); return mApiRefreshToken } - set(value) { mApiRefreshToken = value; profile?.putStudentData("accountRefreshToken", value) ?: return; } + set(value) { mApiRefreshToken = value; profile["accountRefreshToken"] = value ?: return; } /** * The expiry time for [apiAccessToken], as a UNIX timestamp. * Used when refreshing the [apiAccessToken] in JST, Synergia modes. @@ -171,7 +173,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mApiTokenExpiryTime: Long? = null var apiTokenExpiryTime: Long get() { mApiTokenExpiryTime = mApiTokenExpiryTime ?: profile?.getStudentData("accountTokenTime", 0L); return mApiTokenExpiryTime ?: 0L } - set(value) { mApiTokenExpiryTime = value; profile?.putStudentData("accountTokenTime", value) ?: return; } + set(value) { mApiTokenExpiryTime = value; profile["accountTokenTime"] = value; } /** * A push device ID, generated by Librus when registering @@ -181,7 +183,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mPushDeviceId: Int? = null var pushDeviceId: Int get() { mPushDeviceId = mPushDeviceId ?: profile?.getStudentData("pushDeviceId", 0); return mPushDeviceId ?: 0 } - set(value) { mPushDeviceId = value; profile?.putStudentData("pushDeviceId", value) ?: return; } + set(value) { mPushDeviceId = value; profile["pushDeviceId"] = value; } /* _____ _ / ____| (_) @@ -198,7 +200,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mSynergiaSessionId: String? = null var synergiaSessionId: String? get() { mSynergiaSessionId = mSynergiaSessionId ?: profile?.getStudentData("accountSID", null); return mSynergiaSessionId } - set(value) { profile?.putStudentData("accountSID", value) ?: return; mSynergiaSessionId = value } + set(value) { profile["accountSID"] = value; mSynergiaSessionId = value } /** * The expiry time for [synergiaSessionId], as a UNIX timestamp. * Used in endpoints with Synergia login method. @@ -207,7 +209,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mSynergiaSessionIdExpiryTime: Long? = null var synergiaSessionIdExpiryTime: Long get() { mSynergiaSessionIdExpiryTime = mSynergiaSessionIdExpiryTime ?: profile?.getStudentData("accountSIDTime", 0L); return mSynergiaSessionIdExpiryTime ?: 0L } - set(value) { profile?.putStudentData("accountSIDTime", value) ?: return; mSynergiaSessionIdExpiryTime = value } + set(value) { profile["accountSIDTime"] = value; mSynergiaSessionIdExpiryTime = value } /** @@ -217,7 +219,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mMessagesSessionId: String? = null var messagesSessionId: String? get() { mMessagesSessionId = mMessagesSessionId ?: profile?.getStudentData("messagesSID", null); return mMessagesSessionId } - set(value) { profile?.putStudentData("messagesSID", value) ?: return; mMessagesSessionId = value } + set(value) { profile["messagesSID"] = value; mMessagesSessionId = value } /** * The expiry time for [messagesSessionId], as a UNIX timestamp. * Used in endpoints with Messages login method. @@ -226,7 +228,7 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mMessagesSessionIdExpiryTime: Long? = null var messagesSessionIdExpiryTime: Long get() { mMessagesSessionIdExpiryTime = mMessagesSessionIdExpiryTime ?: profile?.getStudentData("messagesSIDTime", 0L); return mMessagesSessionIdExpiryTime ?: 0L } - set(value) { profile?.putStudentData("messagesSIDTime", value) ?: return; mMessagesSessionIdExpiryTime = value } + set(value) { profile["messagesSIDTime"] = value; mMessagesSessionIdExpiryTime = value } /* ____ _ _ / __ \| | | | @@ -236,42 +238,42 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app \____/ \__|_| |_|\___|*/ var isPremium get() = profile?.getStudentData("isPremium", false) ?: false - set(value) { profile?.putStudentData("isPremium", value) } + set(value) { profile["isPremium"] = value } private var mSchoolName: String? = null var schoolName: String? get() { mSchoolName = mSchoolName ?: profile?.getStudentData("schoolName", null); return mSchoolName } - set(value) { profile?.putStudentData("schoolName", value) ?: return; mSchoolName = value } + set(value) { profile["schoolName"] = value; mSchoolName = value } private var mUnitId: Long? = null var unitId: Long get() { mUnitId = mUnitId ?: profile?.getStudentData("unitId", 0L); return mUnitId ?: 0L } - set(value) { profile?.putStudentData("unitId", value) ?: return; mUnitId = value } + set(value) { profile["unitId"] = value; mUnitId = value } private var mStartPointsSemester1: Int? = null var startPointsSemester1: Int get() { mStartPointsSemester1 = mStartPointsSemester1 ?: profile?.getStudentData("startPointsSemester1", 0); return mStartPointsSemester1 ?: 0 } - set(value) { profile?.putStudentData("startPointsSemester1", value) ?: return; mStartPointsSemester1 = value } + set(value) { profile["startPointsSemester1"] = value; mStartPointsSemester1 = value } private var mStartPointsSemester2: Int? = null var startPointsSemester2: Int get() { mStartPointsSemester2 = mStartPointsSemester2 ?: profile?.getStudentData("startPointsSemester2", 0); return mStartPointsSemester2 ?: 0 } - set(value) { profile?.putStudentData("startPointsSemester2", value) ?: return; mStartPointsSemester2 = value } + set(value) { profile["startPointsSemester2"] = value; mStartPointsSemester2 = value } private var mEnablePointGrades: Boolean? = null var enablePointGrades: Boolean get() { mEnablePointGrades = mEnablePointGrades ?: profile?.getStudentData("enablePointGrades", true); return mEnablePointGrades ?: true } - set(value) { profile?.putStudentData("enablePointGrades", value) ?: return; mEnablePointGrades = value } + set(value) { profile["enablePointGrades"] = value; mEnablePointGrades = value } private var mEnableDescriptiveGrades: Boolean? = null var enableDescriptiveGrades: Boolean get() { mEnableDescriptiveGrades = mEnableDescriptiveGrades ?: profile?.getStudentData("enableDescriptiveGrades", true); return mEnableDescriptiveGrades ?: true } - set(value) { profile?.putStudentData("enableDescriptiveGrades", value) ?: return; mEnableDescriptiveGrades = value } + set(value) { profile["enableDescriptiveGrades"] = value; mEnableDescriptiveGrades = value } private var mTimetableNotPublic: Boolean? = null var timetableNotPublic: Boolean get() { mTimetableNotPublic = mTimetableNotPublic ?: profile?.getStudentData("timetableNotPublic", false); return mTimetableNotPublic ?: false } - set(value) { profile?.putStudentData("timetableNotPublic", value) ?: return; mTimetableNotPublic = value } + set(value) { profile["timetableNotPublic"] = value; mTimetableNotPublic = value } /** * Set to false when Recaptcha helper doesn't provide a working token. diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt index e5706613..be9b4efc 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt @@ -16,6 +16,7 @@ import pl.szczodrzynski.edziennik.data.db.enums.FeatureType import pl.szczodrzynski.edziennik.data.db.enums.MetadataType import pl.szczodrzynski.edziennik.ext.HOUR import pl.szczodrzynski.edziennik.ext.get +import pl.szczodrzynski.edziennik.ext.getSemesterStart import pl.szczodrzynski.edziennik.ext.singleOrNull import pl.szczodrzynski.edziennik.utils.models.Date diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt index 493ef179..64507362 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt @@ -11,7 +11,9 @@ import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.LoginMethod import pl.szczodrzynski.edziennik.ext.currentTimeUnix +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.ext.set import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Time @@ -85,7 +87,7 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da private var mStudentId: Int? = null var studentId: Int get() { mStudentId = mStudentId ?: profile?.getStudentData("studentId", 0); return mStudentId ?: 0 } - set(value) { profile?.putStudentData("studentId", value) ?: return; mStudentId = value } + set(value) { profile["studentId"] = value; mStudentId = value } /* __ __ _ \ \ / / | | @@ -125,7 +127,7 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da */ var globalId: String? get() { mGlobalId = mGlobalId ?: profile?.getStudentData("globalId", null); return mGlobalId } - set(value) { profile?.putStudentData("globalId", value) ?: return; mGlobalId = value } + set(value) { profile["globalId"] = value; mGlobalId = value } private var mGlobalId: String? = null /** @@ -135,7 +137,7 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da */ var loginEmail: String? get() { mLoginEmail = mLoginEmail ?: profile?.getStudentData("email", null); return mLoginEmail } - set(value) { profile?.putStudentData("email", value); mLoginEmail = value } + set(value) { profile["email"] = value; mLoginEmail = value } private var mLoginEmail: String? = null /** @@ -144,7 +146,7 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da */ var loginId: String? get() { mLoginId = mLoginId ?: profile?.getStudentData("loginId", null); return mLoginId } - set(value) { profile?.putStudentData("loginId", value) ?: return; mLoginId = value } + set(value) { profile["loginId"] = value; mLoginId = value } private var mLoginId: String? = null /** @@ -152,7 +154,7 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da */ var ciasteczkoAutoryzacji: String? get() { mCiasteczkoAutoryzacji = mCiasteczkoAutoryzacji ?: profile?.getStudentData("ciasteczkoAutoryzacji", null); return mCiasteczkoAutoryzacji } - set(value) { profile?.putStudentData("ciasteczkoAutoryzacji", value) ?: return; mCiasteczkoAutoryzacji = value } + set(value) { profile["ciasteczkoAutoryzacji"] = value; mCiasteczkoAutoryzacji = value } private var mCiasteczkoAutoryzacji: String? = null diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiAttendance.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiAttendance.kt index 054b2fc7..26079f44 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiAttendance.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiAttendance.kt @@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.Attendance.Companion.TYPE_PRESE import pl.szczodrzynski.edziennik.data.db.entity.Attendance.Companion.TYPE_RELEASED import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.enums.MetadataType +import pl.szczodrzynski.edziennik.ext.dateToSemester class MobidziennikApiAttendance(val data: DataMobidziennik, rows: List) { init { run { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebAttendance.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebAttendance.kt index 28371b18..08ce6457 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebAttendance.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebAttendance.kt @@ -21,6 +21,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.Attendance.Companion.TYPE_UNKNO import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.enums.MetadataType +import pl.szczodrzynski.edziennik.ext.dateToSemester import pl.szczodrzynski.edziennik.ext.fixName import pl.szczodrzynski.edziennik.ext.get import pl.szczodrzynski.edziennik.ext.singleOrNull diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt index ef6bb826..dcf77fee 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt @@ -15,6 +15,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_NORMAL import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.enums.MetadataType +import pl.szczodrzynski.edziennik.ext.dateToSemester import pl.szczodrzynski.edziennik.ext.fixWhiteSpaces import pl.szczodrzynski.edziennik.ext.get import pl.szczodrzynski.edziennik.ext.singleOrNull diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt index 52b05a20..afbe53eb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt @@ -10,7 +10,9 @@ import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.LoginMethod import pl.szczodrzynski.edziennik.ext.crc32 +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.ext.set class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { @@ -40,7 +42,7 @@ class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(a private var mApiUrl: String? = null var apiUrl: String? get() { mApiUrl = mApiUrl ?: profile?.getStudentData("apiUrl", null); return mApiUrl } - set(value) { profile?.putStudentData("apiUrl", value) ?: return; mApiUrl = value } + set(value) { profile["apiUrl"] = value; mApiUrl = value } /* ____ _ _ / __ \| | | | @@ -51,32 +53,32 @@ class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(a private var mStudentId: String? = null var studentId: String? get() { mStudentId = mStudentId ?: profile?.getStudentData("studentId", null); return mStudentId } - set(value) { profile?.putStudentData("studentId", value) ?: return; mStudentId = value } + set(value) { profile["studentId"] = value; mStudentId = value } private var mStudentLogin: String? = null var studentLogin: String? get() { mStudentLogin = mStudentLogin ?: profile?.getStudentData("studentLogin", null); return mStudentLogin } - set(value) { profile?.putStudentData("studentLogin", value) ?: return; mStudentLogin = value } + set(value) { profile["studentLogin"] = value; mStudentLogin = value } private var mSchoolName: String? = null var schoolName: String? get() { mSchoolName = mSchoolName ?: profile?.getStudentData("schoolName", null); return mSchoolName } - set(value) { profile?.putStudentData("schoolName", value) ?: return; mSchoolName = value } + set(value) { profile["schoolName"] = value; mSchoolName = value } private var mClassName: String? = null var className: String? get() { mClassName = mClassName ?: profile?.getStudentData("className", null); return mClassName } - set(value) { profile?.putStudentData("className", value) ?: return; mClassName = value } + set(value) { profile["className"] = value; mClassName = value } private var mSchoolYear: String? = null var schoolYear: String? get() { mSchoolYear = mSchoolYear ?: profile?.getStudentData("schoolYear", null); return mSchoolYear } - set(value) { profile?.putStudentData("schoolYear", value) ?: return; mSchoolYear = value } + set(value) { profile["schoolYear"] = value; mSchoolYear = value } private var mCurrentSemester: Int? = null var currentSemester: Int get() { mCurrentSemester = mCurrentSemester ?: profile?.getStudentData("currentSemester", 0); return mCurrentSemester ?: 0 } - set(value) { profile?.putStudentData("currentSemester", value) ?: return; mCurrentSemester = value } + set(value) { profile["currentSemester"] = value; mCurrentSemester = value } val schoolShortName: String? get() = studentLogin?.split('@')?.get(1)?.replace(".podlaskie.pl", "") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt index 15a3499f..04023f5f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiFinalGrades.kt @@ -17,6 +17,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.Grade.Companion.TYPE_YEAR_PROPO import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.enums.MetadataType import pl.szczodrzynski.edziennik.ext.getLong +import pl.szczodrzynski.edziennik.ext.getSemesterStart import pl.szczodrzynski.edziennik.ext.getString class PodlasieApiFinalGrades(val data: DataPodlasie, val rows: List) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/DataTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/DataTemplate.kt index 9d8c5f53..f0a1785e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/DataTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/DataTemplate.kt @@ -10,7 +10,9 @@ import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.LoginMethod import pl.szczodrzynski.edziennik.ext.currentTimeUnix +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.ext.set /** * Use http://patorjk.com/software/taag/#p=display&f=Big for the ascii art @@ -43,12 +45,12 @@ class DataTemplate(app: App, profile: Profile?, loginStore: LoginStore) : Data(a private var mWebCookie: String? = null var webCookie: String? get() { mWebCookie = mWebCookie ?: profile?.getStudentData("webCookie", null); return mWebCookie } - set(value) { profile?.putStudentData("webCookie", value) ?: return; mWebCookie = value } + set(value) { profile["webCookie"] = value; mWebCookie = value } private var mWebExpiryTime: Long? = null var webExpiryTime: Long get() { mWebExpiryTime = mWebExpiryTime ?: profile?.getStudentData("webExpiryTime", 0L); return mWebExpiryTime ?: 0L } - set(value) { profile?.putStudentData("webExpiryTime", value) ?: return; mWebExpiryTime = value } + set(value) { profile["webExpiryTime"] = value; mWebExpiryTime = value } /* _ /\ (_) @@ -61,10 +63,10 @@ class DataTemplate(app: App, profile: Profile?, loginStore: LoginStore) : Data(a private var mApiToken: String? = null var apiToken: String? get() { mApiToken = mApiToken ?: profile?.getStudentData("apiToken", null); return mApiToken } - set(value) { profile?.putStudentData("apiToken", value) ?: return; mApiToken = value } + set(value) { profile["apiToken"] = value; mApiToken = value } private var mApiExpiryTime: Long? = null var apiExpiryTime: Long get() { mApiExpiryTime = mApiExpiryTime ?: profile?.getStudentData("apiExpiryTime", 0L); return mApiExpiryTime ?: 0L } - set(value) { profile?.putStudentData("apiExpiryTime", value) ?: return; mApiExpiryTime = value } + set(value) { profile["apiExpiryTime"] = value; mApiExpiryTime = value } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/usos/DataUsos.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/usos/DataUsos.kt index f7149093..8ebafb51 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/usos/DataUsos.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/usos/DataUsos.kt @@ -9,6 +9,8 @@ import pl.szczodrzynski.edziennik.data.api.models.Data import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.LoginMethod +import pl.szczodrzynski.edziennik.ext.getStudentData +import pl.szczodrzynski.edziennik.ext.set class DataUsos( app: App, @@ -69,6 +71,6 @@ class DataUsos( var studentId: Int get() { mStudentId = mStudentId ?: profile?.getStudentData("studentId", 0); return mStudentId ?: 0 } - set(value) { profile?.putStudentData("studentId", value) ?: return; mStudentId = value } + set(value) { profile["studentId"] = value; mStudentId = value } private var mStudentId: Int? = null } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt index c1bf0074..b4e1610b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt @@ -12,7 +12,9 @@ import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.LoginMethod import pl.szczodrzynski.edziennik.ext.crc16 import pl.szczodrzynski.edziennik.ext.currentTimeUnix +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.ext.set import pl.szczodrzynski.fslogin.realm.RealmData class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { @@ -57,7 +59,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mSymbol: String? = null var symbol: String? get() { mSymbol = mSymbol ?: profile?.getStudentData("symbol", null); return mSymbol } - set(value) { profile?.putStudentData("symbol", value); mSymbol = value } + set(value) { profile["symbol"] = value; mSymbol = value } /** * Group symbol/number of the student's school. @@ -69,7 +71,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mSchoolSymbol: String? = null var schoolSymbol: String? get() { mSchoolSymbol = mSchoolSymbol ?: profile?.getStudentData("schoolSymbol", null); return mSchoolSymbol } - set(value) { profile?.putStudentData("schoolSymbol", value) ?: return; mSchoolSymbol = value } + set(value) { profile["schoolSymbol"] = value; mSchoolSymbol = value } /** * Short name of the school, used in some places. @@ -79,7 +81,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mSchoolShort: String? = null var schoolShort: String? get() { mSchoolShort = mSchoolShort ?: profile?.getStudentData("schoolShort", null); return mSchoolShort } - set(value) { profile?.putStudentData("schoolShort", value) ?: return; mSchoolShort = value } + set(value) { profile["schoolShort"] = value; mSchoolShort = value } /** * A school code consisting of the [symbol] and [schoolSymbol]. @@ -91,7 +93,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mSchoolCode: String? = null var schoolCode: String? get() { mSchoolCode = mSchoolCode ?: profile?.getStudentData("schoolName", null); return mSchoolCode } - set(value) { profile?.putStudentData("schoolName", value) ?: return; mSchoolCode = value } + set(value) { profile["schoolName"] = value; mSchoolCode = value } /** * ID of the student. @@ -101,7 +103,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mStudentId: Int? = null var studentId: Int get() { mStudentId = mStudentId ?: profile?.getStudentData("studentId", 0); return mStudentId ?: 0 } - set(value) { profile?.putStudentData("studentId", value) ?: return; mStudentId = value } + set(value) { profile["studentId"] = value; mStudentId = value } /** * ID of the student's account. @@ -111,7 +113,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mStudentLoginId: Int? = null var studentLoginId: Int get() { mStudentLoginId = mStudentLoginId ?: profile?.getStudentData("studentLoginId", 0); return mStudentLoginId ?: 0 } - set(value) { profile?.putStudentData("studentLoginId", value) ?: return; mStudentLoginId = value } + set(value) { profile["studentLoginId"] = value; mStudentLoginId = value } /** * ID of the student's class. @@ -121,7 +123,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mStudentClassId: Int? = null var studentClassId: Int get() { mStudentClassId = mStudentClassId ?: profile?.getStudentData("studentClassId", 0); return mStudentClassId ?: 0 } - set(value) { profile?.putStudentData("studentClassId", value) ?: return; mStudentClassId = value } + set(value) { profile["studentClassId"] = value; mStudentClassId = value } /** * ListaUczniow/IdOkresKlasyfikacyjny, e.g. 321 @@ -129,26 +131,26 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mStudentSemesterId: Int? = null var studentSemesterId: Int get() { mStudentSemesterId = mStudentSemesterId ?: profile?.getStudentData("studentSemesterId", 0); return mStudentSemesterId ?: 0 } - set(value) { profile?.putStudentData("studentSemesterId", value) ?: return; mStudentSemesterId = value } + set(value) { profile["studentSemesterId"] = value; mStudentSemesterId = value } private var mStudentUnitId: Int? = null var studentUnitId: Int get() { mStudentUnitId = mStudentUnitId ?: profile?.getStudentData("studentUnitId", 0); return mStudentUnitId ?: 0 } - set(value) { profile?.putStudentData("studentUnitId", value) ?: return; mStudentUnitId = value } + set(value) { profile["studentUnitId"] = value; mStudentUnitId = value } private var mStudentConstituentId: Int? = null var studentConstituentId: Int get() { mStudentConstituentId = mStudentConstituentId ?: profile?.getStudentData("studentConstituentId", 0); return mStudentConstituentId ?: 0 } - set(value) { profile?.putStudentData("studentConstituentId", value) ?: return; mStudentConstituentId = value } + set(value) { profile["studentConstituentId"] = value; mStudentConstituentId = value } private var mSemester1Id: Int? = null var semester1Id: Int get() { mSemester1Id = mSemester1Id ?: profile?.getStudentData("semester1Id", 0); return mSemester1Id ?: 0 } - set(value) { profile?.putStudentData("semester1Id", value) ?: return; mSemester1Id = value } + set(value) { profile["semester1Id"] = value; mSemester1Id = value } private var mSemester2Id: Int? = null var semester2Id: Int get() { mSemester2Id = mSemester2Id ?: profile?.getStudentData("semester2Id", 0); return mSemester2Id ?: 0 } - set(value) { profile?.putStudentData("semester2Id", value) ?: return; mSemester2Id = value } + set(value) { profile["semester2Id"] = value; mSemester2Id = value } /** * ListaUczniow/OkresNumer, e.g. 1 or 2 @@ -156,7 +158,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mStudentSemesterNumber: Int? = null var studentSemesterNumber: Int get() { mStudentSemesterNumber = mStudentSemesterNumber ?: profile?.getStudentData("studentSemesterNumber", 0); return mStudentSemesterNumber ?: 0 } - set(value) { profile?.putStudentData("studentSemesterNumber", value) ?: return; mStudentSemesterNumber = value } + set(value) { profile["studentSemesterNumber"] = value; mStudentSemesterNumber = value } /** * Date of the end of the current semester ([studentSemesterNumber]). @@ -166,7 +168,7 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mCurrentSemesterEndDate: Long? = null var currentSemesterEndDate: Long get() { mCurrentSemesterEndDate = mCurrentSemesterEndDate ?: profile?.getStudentData("currentSemesterEndDate", 0L); return mCurrentSemesterEndDate ?: 0L } - set(value) { profile?.putStudentData("currentSemesterEndDate", value) ?: return; mCurrentSemesterEndDate = value } + set(value) { profile["currentSemesterEndDate"] = value; mCurrentSemesterEndDate = value } /* _____ _____ ____ /\ | __ \_ _| |___ \ @@ -219,17 +221,17 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app private var mHebeContext: String? = null var hebeContext: String? get() { mHebeContext = mHebeContext ?: profile?.getStudentData("hebeContext", null); return mHebeContext } - set(value) { profile?.putStudentData("hebeContext", value) ?: return; mHebeContext = value } + set(value) { profile["hebeContext"] = value; mHebeContext = value } private var mMessageBoxKey: String? = null var messageBoxKey: String? get() { mMessageBoxKey = mMessageBoxKey ?: profile?.getStudentData("messageBoxKey", null); return mMessageBoxKey } - set(value) { profile?.putStudentData("messageBoxKey", value) ?: return; mMessageBoxKey = value } + set(value) { profile["messageBoxKey"] = value; mMessageBoxKey = value } private var mMessageBoxName: String? = null var messageBoxName: String? get() { mMessageBoxName = mMessageBoxName ?: profile?.getStudentData("messageBoxName", null); return mMessageBoxName } - set(value) { profile?.putStudentData("messageBoxName", value) ?: return; mMessageBoxName = value } + set(value) { profile["messageBoxName"] = value; mMessageBoxName = value } val apiUrl: String? get() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt index 012948c0..6b5b0f83 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Profile.kt @@ -5,25 +5,16 @@ package pl.szczodrzynski.edziennik.data.db.entity import android.content.Context -import android.graphics.PorterDuff -import android.graphics.PorterDuffColorFilter -import android.graphics.drawable.Drawable import android.widget.ImageView -import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory import androidx.room.ColumnInfo import androidx.room.Entity -import androidx.room.Ignore import com.google.gson.JsonObject -import pl.droidsonroids.gif.GifDrawable -import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.db.enums.LoginType -import pl.szczodrzynski.edziennik.ext.* -import pl.szczodrzynski.edziennik.utils.ProfileImageHolder +import pl.szczodrzynski.edziennik.ext.dateToSemester +import pl.szczodrzynski.edziennik.ext.getDrawable +import pl.szczodrzynski.edziennik.ext.getHolder import pl.szczodrzynski.edziennik.utils.models.Date -import pl.szczodrzynski.navlib.ImageHolder -import pl.szczodrzynski.navlib.R import pl.szczodrzynski.navlib.drawer.IDrawerProfile -import pl.szczodrzynski.navlib.getDrawableFromRes @Entity(tableName = "profiles", primaryKeys = ["profileId"]) open class Profile( @@ -60,9 +51,12 @@ open class Profile( } override var image: String? = null - var empty = true var archived = false + var syncEnabled = true + var enableSharedEvents = true + var registration = REGISTRATION_UNSPECIFIED + var userCode = "" /** * A unique ID matching [archived] profiles with current ones @@ -70,117 +64,31 @@ open class Profile( */ var archiveId: Int? = null - var syncEnabled = true - var enableSharedEvents = true - var registration = REGISTRATION_UNSPECIFIED - var userCode = "" - /** * The student's number in the class register. */ var studentNumber = -1 var studentClassName: String? = null var studentSchoolYearStart = Date.getToday().let { if (it.month < 9) it.year - 1 else it.year } - var dateSemester1Start = Date(studentSchoolYearStart, 9, 1) var dateSemester2Start = Date(studentSchoolYearStart + 1, 2, 1) var dateYearEnd = Date(studentSchoolYearStart + 1, 6, 30) - fun getSemesterStart(semester: Int) = if (semester == 1) dateSemester1Start else dateSemester2Start - fun getSemesterEnd(semester: Int) = if (semester == 1) dateSemester2Start.clone().stepForward(0, 0, -1) else dateYearEnd - fun dateToSemester(date: Date) = if (date >= dateSemester2Start) 2 else 1 - @delegate:Ignore - val currentSemester by lazy { dateToSemester(Date.getToday()) } - - fun shouldArchive(): Boolean { - // vulcan hotfix - if (dateYearEnd.month > 6) { - dateYearEnd.month = 6 - dateYearEnd.day = 30 - } - // fix for when versions <4.3 synced 2020/2021 year dates to older profiles during 2020 Jun-Aug - if (dateSemester1Start.year > studentSchoolYearStart) { - val diff = dateSemester1Start.year - studentSchoolYearStart - dateSemester1Start.year -= diff - dateSemester2Start.year -= diff - dateYearEnd.year -= diff - } - return App.config.archiverEnabled - && Date.getToday() >= dateYearEnd - && Date.getToday().year > studentSchoolYearStart - } - fun isBeforeYear() = false && Date.getToday() < dateSemester1Start - var disabledNotifications: List? = null - var lastReceiversSync: Long = 0 - fun hasStudentData(key: String) = studentData.has(key) - fun getStudentData(key: String, defaultValue: Boolean) = studentData.getBoolean(key) ?: defaultValue - fun getStudentData(key: String, defaultValue: String?) = studentData.getString(key) ?: defaultValue - fun getStudentData(key: String, defaultValue: Int) = studentData.getInt(key) ?: defaultValue - fun getStudentData(key: String, defaultValue: Long) = studentData.getLong(key) ?: defaultValue - fun getStudentData(key: String, defaultValue: Float) = studentData.getFloat(key) ?: defaultValue - fun getStudentData(key: String, defaultValue: Char) = studentData.getChar(key) ?: defaultValue - fun putStudentData(key: String, value: Boolean) { studentData[key] = value } - fun putStudentData(key: String, value: String?) { studentData[key] = value } - fun putStudentData(key: String, value: Number) { studentData[key] = value } - fun putStudentData(key: String, value: Char) { studentData[key] = value } - fun removeStudentData(key: String) { studentData.remove(key) } - + val currentSemester + get() = dateToSemester(Date.getToday()) val isParent get() = accountName != null - val accountOwnerName get() = accountName ?: studentNameLong - - @Ignore - val registerName = loginStoreType.name.lowercase() - + val registerName + get() = loginStoreType.name.lowercase() val canShare get() = registration == REGISTRATION_ENABLED && !archived - override fun getImageDrawable(context: Context): Drawable { - if (archived) { - return context.getDrawableFromRes(pl.szczodrzynski.edziennik.R.drawable.profile_archived).also { - it.colorFilter = PorterDuffColorFilter(colorFromName(name), PorterDuff.Mode.DST_OVER) - } - } - - if (!image.isNullOrEmpty()) { - try { - return if (image?.endsWith(".gif", true) == true) { - GifDrawable(image ?: "") - } else { - RoundedBitmapDrawableFactory.create(context.resources, image ?: "") - //return Drawable.createFromPath(image ?: "") ?: throw Exception() - } - } - catch (e: Exception) { - e.printStackTrace() - } - } - - return context.getDrawableFromRes(R.drawable.profile).also { - it.colorFilter = PorterDuffColorFilter(colorFromName(name), PorterDuff.Mode.DST_OVER) - } - } - - override fun getImageHolder(context: Context): ImageHolder { - if (archived) { - return ImageHolder(pl.szczodrzynski.edziennik.R.drawable.profile_archived, colorFromName(name)) - } - - return if (!image.isNullOrEmpty()) { - try { - ProfileImageHolder(image ?: "") - } catch (_: Exception) { - ImageHolder(R.drawable.profile, colorFromName(name)) - } - } - else { - ImageHolder(R.drawable.profile, colorFromName(name)) - } - } + override fun getImageDrawable(context: Context) = this.getDrawable(context) + override fun getImageHolder(context: Context) = this.getHolder() override fun applyImageTo(imageView: ImageView) { getImageHolder(imageView.context).applyTo(imageView) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt index 295b53ad..14c3f998 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt @@ -6,12 +6,37 @@ package pl.szczodrzynski.edziennik.data.firebase import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask -import pl.szczodrzynski.edziennik.data.api.edziennik.librus.* +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_ANNOUNCEMENTS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_ATTENDANCES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADE_CATEGORIES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADE_COMMENTS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_CLASS_FREE_DAYS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_TEXT_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_EVENTS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_HOMEWORK +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_LUCKY_NUMBER +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADE_CATEGORIES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADE_COMMENTS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_NOTICES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_POINT_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_POINT_GRADE_CATEGORIES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_PT_MEETINGS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_SCHOOL_FREE_DAYS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_TEXT_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_TIMETABLES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_MESSAGES_RECEIVED +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK import pl.szczodrzynski.edziennik.data.api.task.IApiTask import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.FeatureType import pl.szczodrzynski.edziennik.data.db.enums.LoginType import pl.szczodrzynski.edziennik.ext.getString +import pl.szczodrzynski.edziennik.ext.getStudentData class SzkolnyLibrusFirebase(val app: App, val profiles: List, val message: FirebaseService.Message) { /*{ diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyMobidziennikFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyMobidziennikFirebase.kt index ad2021e7..c1397ab2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyMobidziennikFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyMobidziennikFirebase.kt @@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.data.db.enums.FeatureType import pl.szczodrzynski.edziennik.data.db.enums.LoginType import pl.szczodrzynski.edziennik.ext.getLong import pl.szczodrzynski.edziennik.ext.getString +import pl.szczodrzynski.edziennik.ext.getStudentData class SzkolnyMobidziennikFirebase(val app: App, val profiles: List, val message: FirebaseService.Message) { /*{ diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyVulcanFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyVulcanFirebase.kt index 2c33964a..ec8f8011 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyVulcanFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyVulcanFirebase.kt @@ -4,15 +4,15 @@ package pl.szczodrzynski.edziennik.data.firebase -import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask import pl.szczodrzynski.edziennik.data.api.task.IApiTask -import pl.szczodrzynski.edziennik.data.db.entity.Message import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.enums.FeatureType import pl.szczodrzynski.edziennik.data.db.enums.LoginType import pl.szczodrzynski.edziennik.ext.getInt import pl.szczodrzynski.edziennik.ext.getString +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.toJsonObject class SzkolnyVulcanFirebase(val app: App, val profiles: List, val message: FirebaseService.Message) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ext/DataExtensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ext/DataExtensions.kt index 5958078d..977952e5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ext/DataExtensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ext/DataExtensions.kt @@ -6,14 +6,9 @@ package pl.szczodrzynski.edziennik.ext import android.util.LongSparseArray import androidx.core.util.forEach -import com.google.android.material.datepicker.CalendarConstraints -import com.google.gson.JsonElement -import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.config.AppData import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.entity.Teacher import pl.szczodrzynski.edziennik.data.db.entity.Team -import pl.szczodrzynski.edziennik.data.db.enums.FeatureType fun List.byId(id: Long) = firstOrNull { it.id == id } fun List.byNameFirstLast(nameFirstLast: String) = firstOrNull { it.name + " " + it.surname == nameFirstLast } @@ -34,22 +29,3 @@ fun LongSparseArray.getById(id: Long): Team? { } return null } - -operator fun Profile.set(key: String, value: JsonElement) = this.studentData.add(key, value) -operator fun Profile.set(key: String, value: Boolean) = this.studentData.addProperty(key, value) -operator fun Profile.set(key: String, value: String?) = this.studentData.addProperty(key, value) -operator fun Profile.set(key: String, value: Number) = this.studentData.addProperty(key, value) -operator fun Profile.set(key: String, value: Char) = this.studentData.addProperty(key, value) - -fun Profile.getSchoolYearConstrains(): CalendarConstraints { - return CalendarConstraints.Builder() - .setStart(dateSemester1Start.inMillisUtc) - .setEnd(dateYearEnd.inMillisUtc) - .build() -} - -fun Profile.hasFeature(featureType: FeatureType) = featureType in this.loginStoreType.features -fun Profile.hasUIFeature(featureType: FeatureType) = featureType.isUIAlwaysAvailable || hasFeature(featureType) - -fun Profile.getAppData() = - if (App.profileId == this.id) App.data else AppData.get(this.loginStoreType) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ext/ProfileExtensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ext/ProfileExtensions.kt new file mode 100644 index 00000000..a2ef0289 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ext/ProfileExtensions.kt @@ -0,0 +1,127 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2022-10-25. + */ + +package pl.szczodrzynski.edziennik.ext + +import android.content.Context +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.graphics.drawable.Drawable +import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory +import com.google.android.material.datepicker.CalendarConstraints +import com.google.gson.JsonElement +import pl.droidsonroids.gif.GifDrawable +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.config.AppData +import pl.szczodrzynski.edziennik.data.db.entity.Profile +import pl.szczodrzynski.edziennik.data.db.enums.FeatureType +import pl.szczodrzynski.edziennik.utils.ProfileImageHolder +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.navlib.ImageHolder +import pl.szczodrzynski.navlib.getDrawableFromRes + +// TODO refactor Data* fields and make the receiver non-nullable +operator fun Profile?.set(key: String, value: JsonElement) = this?.studentData?.add(key, value) +operator fun Profile?.set(key: String, value: Boolean) = this?.studentData?.addProperty(key, value) +operator fun Profile?.set(key: String, value: String?) = this?.studentData?.addProperty(key, value) +operator fun Profile?.set(key: String, value: Number) = this?.studentData?.addProperty(key, value) +operator fun Profile?.set(key: String, value: Char) = this?.studentData?.addProperty(key, value) + +fun Profile.getStudentData(key: String, defaultValue: Boolean) = + studentData.getBoolean(key) ?: defaultValue + +fun Profile.getStudentData(key: String, defaultValue: String?) = + studentData.getString(key) ?: defaultValue + +fun Profile.getStudentData(key: String, defaultValue: Int) = + studentData.getInt(key) ?: defaultValue + +fun Profile.getStudentData(key: String, defaultValue: Long) = + studentData.getLong(key) ?: defaultValue + +fun Profile.getStudentData(key: String, defaultValue: Float) = + studentData.getFloat(key) ?: defaultValue + +fun Profile.getStudentData(key: String, defaultValue: Char) = + studentData.getChar(key) ?: defaultValue + +fun Profile.getSemesterStart(semester: Int) = + if (semester == 1) dateSemester1Start else dateSemester2Start + +fun Profile.getSemesterEnd(semester: Int) = + if (semester == 1) dateSemester2Start.clone().stepForward(0, 0, -1) else dateYearEnd + +fun Profile.dateToSemester(date: Date) = if (date >= dateSemester2Start) 2 else 1 +fun Profile.isBeforeYear() = false && Date.getToday() < dateSemester1Start + +fun Profile.getSchoolYearConstrains(): CalendarConstraints { + return CalendarConstraints.Builder() + .setStart(dateSemester1Start.inMillisUtc) + .setEnd(dateYearEnd.inMillisUtc) + .build() +} + +fun Profile.hasFeature(featureType: FeatureType) = featureType in this.loginStoreType.features +fun Profile.hasUIFeature(featureType: FeatureType) = + featureType.isUIAlwaysAvailable || hasFeature(featureType) + +fun Profile.getAppData() = + if (App.profileId == this.id) App.data else AppData.get(this.loginStoreType) + +fun Profile.shouldArchive(): Boolean { + // vulcan hotfix + if (dateYearEnd.month > 6) { + dateYearEnd.month = 6 + dateYearEnd.day = 30 + } + // fix for when versions <4.3 synced 2020/2021 year dates to older profiles during 2020 Jun-Aug + if (dateSemester1Start.year > studentSchoolYearStart) { + val diff = dateSemester1Start.year - studentSchoolYearStart + dateSemester1Start.year -= diff + dateSemester2Start.year -= diff + dateYearEnd.year -= diff + } + return App.config.archiverEnabled && Date.getToday() >= dateYearEnd && Date.getToday().year > studentSchoolYearStart +} + +fun Profile.getDrawable(context: Context): Drawable { + if (archived) { + return context.getDrawableFromRes(R.drawable.profile_archived).also { + it.colorFilter = PorterDuffColorFilter(colorFromName(name), PorterDuff.Mode.DST_OVER) + } + } + + if (!image.isNullOrEmpty()) { + try { + return if (image?.endsWith(".gif", true) == true) { + GifDrawable(image ?: "") + } else { + RoundedBitmapDrawableFactory.create(context.resources, image ?: "") + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + return context.getDrawableFromRes(R.drawable.profile).also { + it.colorFilter = PorterDuffColorFilter(colorFromName(name), PorterDuff.Mode.DST_OVER) + } +} + +fun Profile.getHolder(): ImageHolder { + if (archived) { + return ImageHolder(R.drawable.profile_archived, colorFromName(name)) + } + + return if (!image.isNullOrEmpty()) { + try { + ProfileImageHolder(image ?: "") + } catch (_: Exception) { + ImageHolder(R.drawable.profile, colorFromName(name)) + } + } else { + ImageHolder(R.drawable.profile, colorFromName(name)) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/settings/cards/SettingsRegisterCard.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/settings/cards/SettingsRegisterCard.kt index 6e2c989a..d39e4905 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/settings/cards/SettingsRegisterCard.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/settings/cards/SettingsRegisterCard.kt @@ -12,8 +12,16 @@ import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.data.db.enums.FeatureType import pl.szczodrzynski.edziennik.data.db.enums.LoginType import pl.szczodrzynski.edziennik.ext.after +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ext.hasUIFeature -import pl.szczodrzynski.edziennik.ui.dialogs.settings.* +import pl.szczodrzynski.edziennik.ext.set +import pl.szczodrzynski.edziennik.ui.dialogs.settings.AgendaConfigDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.AttendanceConfigDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.BellSyncConfigDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.GradesConfigDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.MessagesConfigDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.RegistrationConfigDialog +import pl.szczodrzynski.edziennik.ui.dialogs.settings.TimetableConfigDialog import pl.szczodrzynski.edziennik.ui.settings.SettingsCard import pl.szczodrzynski.edziennik.ui.settings.SettingsUtil @@ -185,7 +193,7 @@ class SettingsRegisterCard(util: SettingsUtil) : SettingsCard(util) { icon = CommunityMaterial.Icon.cmd_account_arrow_right_outline, value = app.profile.getStudentData("showTeacherAbsences", true) ) { _, it -> - app.profile.putStudentData("showTeacherAbsences", it) + app.profile["showTeacherAbsences"] = it app.profileSave() } else diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt index 196aeee9..7ce59f51 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt @@ -19,14 +19,18 @@ import androidx.viewpager.widget.ViewPager import com.google.android.material.datepicker.MaterialDatePicker import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial import eu.szkolny.font.SzkolnyFont -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.async +import kotlinx.coroutines.launch import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.MainActivity import pl.szczodrzynski.edziennik.R -import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.enums.MetadataType import pl.szczodrzynski.edziennik.databinding.FragmentTimetableV2Binding import pl.szczodrzynski.edziennik.ext.getSchoolYearConstrains +import pl.szczodrzynski.edziennik.ext.getStudentData import pl.szczodrzynski.edziennik.ui.dialogs.settings.TimetableConfigDialog import pl.szczodrzynski.edziennik.ui.event.EventManualDialog import pl.szczodrzynski.edziennik.utils.models.Date diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java index 71296b85..30dc4cc2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java @@ -202,6 +202,7 @@ public class Date implements Comparable, Noteable { return Week.getWeekDayFromDate(this); } + @NonNull public Date stepForward(int years, int months, int days) { this.day += days; if (day <= 0) {