diff --git a/app/src/main/java/ro/code4/monitorizarevot/data/dao/FormsDao.kt b/app/src/main/java/ro/code4/monitorizarevot/data/dao/FormsDao.kt index b3b1689a..37ade4d0 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/data/dao/FormsDao.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/data/dao/FormsDao.kt @@ -126,4 +126,12 @@ interface FormsDao { @Query("SELECT COUNT(*) FROM answered_question WHERE synced=:synced") fun getCountOfNotSyncedQuestions(synced: Boolean = false): LiveData + @Query("DELETE FROM answered_question") + fun deleteAllAnswers() + @Query("DELETE FROM question") + fun deleteAllQuestions() + @Query("DELETE FROM section") + fun deleteAllSections() + @Query("DELETE FROM form_details") + fun deleteAllForms() } \ No newline at end of file diff --git a/app/src/main/java/ro/code4/monitorizarevot/data/dao/NoteDao.kt b/app/src/main/java/ro/code4/monitorizarevot/data/dao/NoteDao.kt index 81fc2bab..4379c3bb 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/data/dao/NoteDao.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/data/dao/NoteDao.kt @@ -28,4 +28,7 @@ interface NoteDao { @Query("SELECT COUNT(*) FROM note WHERE synced =:synced") fun getCountOfNotSyncedNotes(synced: Boolean = false): LiveData + + @Query("DELETE FROM note") + fun deleteAll() } \ No newline at end of file diff --git a/app/src/main/java/ro/code4/monitorizarevot/data/dao/PollingStationDao.kt b/app/src/main/java/ro/code4/monitorizarevot/data/dao/PollingStationDao.kt index 85ebd337..ec87478c 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/data/dao/PollingStationDao.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/data/dao/PollingStationDao.kt @@ -28,4 +28,7 @@ interface PollingStationDao { @Query("SELECT COUNT(*) FROM polling_station WHERE synced =:synced") fun getCountOfNotSyncedPollingStations(synced: Boolean = false): LiveData + + @Query("DELETE FROM polling_station") + fun deleteAll() } \ No newline at end of file diff --git a/app/src/main/java/ro/code4/monitorizarevot/helper/Constants.kt b/app/src/main/java/ro/code4/monitorizarevot/helper/Constants.kt index 9867619b..26587b87 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/helper/Constants.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/helper/Constants.kt @@ -23,6 +23,7 @@ object Constants { const val REMOTE_CONFIG_PRIVACY_POLICY_URL = "privacy_policy_url" const val REMOTE_CONFIG_OBSERVER_GUIDE_URL = "observer_guide_url" const val REMOTE_CONFIG_SAFETY_GUIDE_URL = "safety_guide_url" + const val REMOTE_CONFIG_ROUND_START_TIMESTAMP = "round_start_time" const val PUSH_NOTIFICATION_TITLE = "title" const val PUSH_NOTIFICATION_BODY = "body" diff --git a/app/src/main/java/ro/code4/monitorizarevot/helper/PreferenceManager.kt b/app/src/main/java/ro/code4/monitorizarevot/helper/PreferenceManager.kt index 1044c602..88c8318b 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/helper/PreferenceManager.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/helper/PreferenceManager.kt @@ -9,10 +9,12 @@ const val PREFS_POLLING_STATION_NUMBER = "PREFS_POLLING_STATION_NUMBER" const val ONBOARDING_COMPLETED = "ONBOARDING_COMPLETED" const val POLLING_STATION_CONFIG_COMPLETED = "POLLING_STATION_CONFIG_COMPLETED" const val PREFS_LANGUAGE_CODE = "PREFS_LANGUAGE_CODE" +const val PREFS_LAST_DB_RESET_TIMESTAMP = "PREFS_LAST_DB_RESET_TIMESTAMP" fun SharedPreferences.getString(key: String): String? = getString(key, null) fun SharedPreferences.getInt(key: String): Int = getInt(key, 0) +fun SharedPreferences.getLong(key: String): Long = getLong(key, 0) fun SharedPreferences.putString(key: String, value: String?) { val editor = edit() @@ -26,6 +28,12 @@ fun SharedPreferences.putInt(key: String, value: Int) { editor.apply() } +fun SharedPreferences.putLong(key: String, value: Long) { + val editor = edit() + editor.putLong(key, value) + editor.apply() +} + fun SharedPreferences.putBoolean(key: String, value: Boolean = true) { val editor = edit() editor.putBoolean(key, value) @@ -56,4 +64,20 @@ fun SharedPreferences.completedOnboarding() = putBoolean(ONBOARDING_COMPLETED) fun SharedPreferences.getLocaleCode(): String = getString(PREFS_LANGUAGE_CODE, BuildConfig.PREFERRED_LOCALE) ?: BuildConfig.PREFERRED_LOCALE -fun SharedPreferences.setLocaleCode(code: String) = putString(PREFS_LANGUAGE_CODE, code) \ No newline at end of file +fun SharedPreferences.setLocaleCode(code: String) = putString(PREFS_LANGUAGE_CODE, code) + +fun SharedPreferences.getLastDbResetTimestamp() = getLong(PREFS_LAST_DB_RESET_TIMESTAMP) +fun SharedPreferences.setLastDbResetTimestamp(value: Long) = putLong(PREFS_LAST_DB_RESET_TIMESTAMP, value) + +fun SharedPreferences.clearUserPrefs() = run { + completedPollingStationConfig(false) + removeCurrentLocationPrefs() + deleteToken() +} + +private fun SharedPreferences.removeCurrentLocationPrefs() { + val editor = edit() + editor.remove(PREFS_COUNTY_CODE) + editor.remove(PREFS_POLLING_STATION_NUMBER) + editor.apply() +} \ No newline at end of file diff --git a/app/src/main/java/ro/code4/monitorizarevot/repositories/Repository.kt b/app/src/main/java/ro/code4/monitorizarevot/repositories/Repository.kt index 083f2ef0..8fe282bd 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/repositories/Repository.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/repositories/Repository.kt @@ -1,6 +1,7 @@ package ro.code4.monitorizarevot.repositories import android.annotation.SuppressLint +import android.os.AsyncTask import android.util.Log import androidx.lifecycle.LiveData import io.reactivex.Observable @@ -394,5 +395,18 @@ class Repository : KoinComponent { .flatMap { postPollingStationDetails(it) } .subscribeOn(Schedulers.io()) } + + fun clearDBData() { + AsyncTask.execute { + db.noteDao().deleteAll() + + db.formDetailsDao().deleteAllAnswers() + db.formDetailsDao().deleteAllQuestions() + db.formDetailsDao().deleteAllSections() + db.formDetailsDao().deleteAllForms() + + db.pollingStationDao().deleteAll() + } + } } diff --git a/app/src/main/java/ro/code4/monitorizarevot/ui/splashscreen/SplashScreenViewModel.kt b/app/src/main/java/ro/code4/monitorizarevot/ui/splashscreen/SplashScreenViewModel.kt index e90e3a28..fa9b7ab9 100644 --- a/app/src/main/java/ro/code4/monitorizarevot/ui/splashscreen/SplashScreenViewModel.kt +++ b/app/src/main/java/ro/code4/monitorizarevot/ui/splashscreen/SplashScreenViewModel.kt @@ -7,14 +7,13 @@ import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings import org.koin.core.inject import ro.code4.monitorizarevot.BuildConfig import ro.code4.monitorizarevot.R -import ro.code4.monitorizarevot.helper.SingleLiveEvent -import ro.code4.monitorizarevot.helper.getToken -import ro.code4.monitorizarevot.helper.hasCompletedOnboarding -import ro.code4.monitorizarevot.helper.isPollingStationConfigCompleted +import ro.code4.monitorizarevot.helper.* +import ro.code4.monitorizarevot.repositories.Repository import ro.code4.monitorizarevot.ui.base.BaseViewModel class SplashScreenViewModel : BaseViewModel() { private val sharedPreferences: SharedPreferences by inject() + private val repository: Repository by inject() private val loginLiveData = SingleLiveEvent() fun loginLiveData(): LiveData = loginLiveData @@ -24,27 +23,18 @@ class SplashScreenViewModel : BaseViewModel() { } private fun remoteConfiguration() { - try { - - FirebaseRemoteConfig.getInstance().apply { - val configSettings = FirebaseRemoteConfigSettings.Builder() - .build() - setConfigSettingsAsync(configSettings) - setDefaultsAsync(R.xml.remote_config_defaults) - fetch(if (BuildConfig.DEBUG) 0 else 3600) - .addOnCompleteListener { - if (it.isSuccessful) { - FirebaseRemoteConfig.getInstance().activate() - } - checkLogin() - } - - } - } catch (e: Exception) { - checkLogin() + FirebaseRemoteConfig.getInstance().apply { + val configSettings = FirebaseRemoteConfigSettings.Builder() + .build() + setConfigSettingsAsync(configSettings) + setDefaultsAsync(R.xml.remote_config_defaults) + fetchAndActivate() + .addOnCompleteListener { + checkResetDB() + } } - + checkLogin() } private fun checkLogin() { @@ -64,4 +54,22 @@ class SplashScreenViewModel : BaseViewModel() { val isPollingStationConfigCompleted: Boolean, val onboardingCompleted: Boolean ) + + private fun checkResetDB() { + val roundStartTimestamp = try { + FirebaseRemoteConfig.getInstance().getLong(Constants.REMOTE_CONFIG_ROUND_START_TIMESTAMP) + } catch (e: Exception) { + 0L + } + val lastDbReset = sharedPreferences.getLastDbResetTimestamp() + + val currentTimestamp = System.currentTimeMillis()/1000 + + if (roundStartTimestamp in 1 until currentTimestamp && (lastDbReset == 0L || lastDbReset < roundStartTimestamp)) { + sharedPreferences.clearUserPrefs() + repository.clearDBData() + + sharedPreferences.setLastDbResetTimestamp(currentTimestamp) + } + } } \ No newline at end of file diff --git a/app/src/main/res/xml/remote_config_defaults.xml b/app/src/main/res/xml/remote_config_defaults.xml index 24e3382d..08983e38 100644 --- a/app/src/main/res/xml/remote_config_defaults.xml +++ b/app/src/main/res/xml/remote_config_defaults.xml @@ -4,4 +4,8 @@ filter_diaspora_forms false + + round_start_time + 1605534872 + \ No newline at end of file