diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt index 1e6cd511..e725c42a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt @@ -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? 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) = diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt index 49d61a41..ea1f79cb 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt @@ -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") diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncFragment.kt index 207fe2ff..34298809 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncFragment.kt @@ -39,6 +39,12 @@ class SyncFragment : PreferenceFragmentCompat(), } } + override fun setLastSyncDate(lastSyncDate: String) { + findPreference(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) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncPresenter.kt index 36b8d792..63e86a47 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncPresenter.kt @@ -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")) + } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncView.kt index 9da473ba..6ffe156f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/sync/SyncView.kt @@ -10,6 +10,8 @@ interface SyncView : BaseView { fun initView() + fun setLastSyncDate(lastSyncDate: String) + fun setServicesSuspended(serviceEnablesKey: String, isHolidays: Boolean) fun setSyncInProgress(inProgress: Boolean) diff --git a/app/src/main/res/values/preferences_defaults.xml b/app/src/main/res/values/preferences_defaults.xml index 5721763f..1ba9359c 100644 --- a/app/src/main/res/values/preferences_defaults.xml +++ b/app/src/main/res/values/preferences_defaults.xml @@ -25,6 +25,7 @@ false false false + 0 LUCKY_NUMBER MESSAGES diff --git a/app/src/main/res/values/preferences_keys.xml b/app/src/main/res/values/preferences_keys.xml index 876e4333..aa60bb7e 100644 --- a/app/src/main/res/values/preferences_keys.xml +++ b/app/src/main/res/values/preferences_keys.xml @@ -31,4 +31,5 @@ optional_arithmetic_average message_send_is_draft message_send_recipients + last_sync_date diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c1b3a3ee..0b25f4a5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -615,6 +615,7 @@ Synced! Sync failed Sync in progress + Last full sync: %s Value of the plus Value of the minus