Add last sync date in sync settings (#1436)

This commit is contained in:
Mateusz Idziejczak 2021-08-15 15:59:32 +02:00 committed by GitHub
parent 626169de11
commit 9c819835ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 1 deletions

View File

@ -13,9 +13,12 @@ import io.github.wulkanowy.R
import io.github.wulkanowy.ui.modules.dashboard.DashboardItem
import io.github.wulkanowy.ui.modules.grade.GradeAverageMode
import io.github.wulkanowy.ui.modules.grade.GradeSortingMode
import io.github.wulkanowy.utils.toLocalDateTime
import io.github.wulkanowy.utils.toTimestamp
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import java.time.LocalDateTime
import javax.inject.Inject
import javax.inject.Singleton
@ -168,6 +171,13 @@ class PreferencesRepository @Inject constructor(
R.bool.pref_default_optional_arithmetic_average
)
var lasSyncDate: LocalDateTime
get() = getLong(
R.string.pref_key_last_sync_date,
R.string.pref_default_last_sync_date
).toLocalDateTime()
set(value) = sharedPref.edit().putLong("last_sync_date", value.toTimestamp()).apply()
var dashboardItemsPosition: Map<DashboardItem.Type, Int>?
get() {
val json = sharedPref.getString(PREF_KEY_DASHBOARD_ITEMS_POSITION, null) ?: return null
@ -211,6 +221,11 @@ class PreferencesRepository @Inject constructor(
return flowSharedPref.getStringSet(prefKey, defaultSet)
}
private fun getLong(id: Int, default: Int) = getLong(context.getString(id), default)
private fun getLong(id: String, default: Int) =
sharedPref.getLong(id, context.resources.getString(default).toLong())
private fun getString(id: Int, default: Int) = getString(context.getString(id), default)
private fun getString(id: String, default: Int) =

View File

@ -22,6 +22,8 @@ import io.github.wulkanowy.services.sync.works.Work
import io.github.wulkanowy.utils.getCompatColor
import kotlinx.coroutines.coroutineScope
import timber.log.Timber
import java.time.LocalDateTime
import java.time.ZoneId
import kotlin.random.Random
@HiltWorker
@ -48,6 +50,7 @@ class SyncWorker @AssistedInject constructor(
Timber.i("${work::class.java.simpleName} is starting")
work.doWork(student, semester)
Timber.i("${work::class.java.simpleName} result: Success")
preferencesRepository.lasSyncDate = LocalDateTime.now(ZoneId.systemDefault())
null
} catch (e: Throwable) {
Timber.w("${work::class.java.simpleName} result: An exception ${e.message} occurred")

View File

@ -39,6 +39,12 @@ class SyncFragment : PreferenceFragmentCompat(),
}
}
override fun setLastSyncDate(lastSyncDate: String) {
findPreference<Preference>(getString(R.string.pref_key_services_force_sync))?.run {
summary = getString(R.string.pref_services_last_full_sync_date, lastSyncDate)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.onAttachView(this)

View File

@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.utils.AnalyticsHelper
import io.github.wulkanowy.utils.isHolidays
import io.github.wulkanowy.utils.toFormattedString
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
@ -27,6 +28,7 @@ class SyncPresenter @Inject constructor(
Timber.i("Settings sync view was initialized")
view.setServicesSuspended(preferencesRepository.serviceEnableKey, now().isHolidays)
view.initView()
setSyncDateInView()
}
fun onSharedPreferenceChanged(key: String) {
@ -63,10 +65,21 @@ class SyncPresenter @Inject constructor(
}
else -> Timber.d("Sync now state: ${workInfo.state}")
}
if (workInfo.state.isFinished) setSyncInProgress(false)
if (workInfo.state.isFinished) {
setSyncInProgress(false)
setSyncDateInView()
}
}.catch {
Timber.e(it, "Sync now failed")
}.launch("sync")
}
}
private fun setSyncDateInView() {
val lastSyncDate = preferencesRepository.lasSyncDate
if (lastSyncDate.year == 1970) return
view?.setLastSyncDate(lastSyncDate.toFormattedString("dd.MM.yyyy HH:mm:ss"))
}
}

View File

@ -10,6 +10,8 @@ interface SyncView : BaseView {
fun initView()
fun setLastSyncDate(lastSyncDate: String)
fun setServicesSuspended(serviceEnablesKey: String, isHolidays: Boolean)
fun setSyncInProgress(inProgress: Boolean)

View File

@ -25,6 +25,7 @@
<bool name="pref_default_homework_fullscreen">false</bool>
<bool name="pref_default_subjects_without_grades">false</bool>
<bool name="pref_default_optional_arithmetic_average">false</bool>
<string name="pref_default_last_sync_date">0</string>
<string-array name="pref_default_dashboard_tiles">
<item>LUCKY_NUMBER</item>
<item>MESSAGES</item>

View File

@ -31,4 +31,5 @@
<string name="pref_key_optional_arithmetic_average">optional_arithmetic_average</string>
<string name="pref_key_message_send_is_draft">message_send_is_draft</string>
<string name="pref_key_message_send_draft">message_send_recipients</string>
<string name="pref_key_last_sync_date">last_sync_date</string>
</resources>

View File

@ -615,6 +615,7 @@
<string name="pref_services_message_sync_success">Synced!</string>
<string name="pref_services_message_sync_failed">Sync failed</string>
<string name="pref_services_sync_in_progress">Sync in progress</string>
<string name="pref_services_last_full_sync_date">Last full sync: %s</string>
<string name="pref_other_grade_modifier_plus">Value of the plus</string>
<string name="pref_other_grade_modifier_minus">Value of the minus</string>