From 1abb9ac3784239317ff2cfd533d5cdc2c2aeb065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 7 Jan 2020 10:45:21 +0100 Subject: [PATCH] [API] Fix config not reading from DB. Do not sync device if not changed. --- .../szczodrzynski/edziennik/config/Config.kt | 13 ++++-- .../edziennik/config/ProfileConfig.kt | 5 +++ .../edziennik/data/api/szkolny/SzkolnyApi.kt | 45 ++++++++++++++----- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/config/Config.kt b/app/src/main/java/pl/szczodrzynski/edziennik/config/Config.kt index d8d523d5..ac8831e3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/config/Config.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/config/Config.kt @@ -40,6 +40,11 @@ class Config(val db: AppDb) : CoroutineScope, AbstractConfig { get() { mDataVersion = mDataVersion ?: values.get("dataVersion", 0); return mDataVersion ?: 0 } set(value) { set("dataVersion", value); mDataVersion = value } + private var mHash: String? = null + var hash: String + get() { mHash = mHash ?: values.get("hash", ""); return mHash ?: "" } + set(value) { set("hash", value); mHash = value } + private var mAppVersion: Int? = null var appVersion: Int get() { mAppVersion = mAppVersion ?: values.get("appVersion", BuildConfig.VERSION_CODE); return mAppVersion ?: BuildConfig.VERSION_CODE } @@ -90,11 +95,11 @@ class Config(val db: AppDb) : CoroutineScope, AbstractConfig { ConfigMigration(app, this) } fun getFor(profileId: Int): ProfileConfig { - return profileConfigs[profileId] ?: ProfileConfig(db, profileId, rawEntries) - } - fun forProfile(): ProfileConfig { - return profileConfigs[App.profileId] ?: ProfileConfig(db, App.profileId, rawEntries) + return profileConfigs[profileId] ?: ProfileConfig(db, profileId, db.configDao().getAllNow(profileId)).also { + profileConfigs[profileId] = it + } } + fun forProfile() = getFor(App.profileId) fun setProfile(profileId: Int) { } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt b/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt index ec5ae3f0..3dd29361 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt @@ -37,6 +37,11 @@ class ProfileConfig(val db: AppDb, val profileId: Int, rawEntries: List - ServerSyncRequest.User( + users = profiles.mapNotNull { profile -> + val config = app.config.getFor(profile.id) + val user = ServerSyncRequest.User( profile.userCode, profile.studentNameLong ?: "", profile.studentNameShort ?: "", profile.loginStoreType, teams.filter { it.profileId == profile.id }.map { it.code } ) + user.toString().md5().let { + if (it == config.hash) + null + else { + config.hash = it + user + } + } }, notifications = notifications.map { ServerSyncRequest.Notification(it.profileName ?: "", it.type, it.text) } )).execute().body()