Skip to content

Commit

Permalink
Merge pull request #238 from aniri/feature/clear_db_remote_config
Browse files Browse the repository at this point in the history
Feature/clear db remote config
  • Loading branch information
AlexandraDaraban authored Nov 19, 2020
2 parents 299e99a + e4f691a commit 0071c9f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,12 @@ interface FormsDao {
@Query("SELECT COUNT(*) FROM answered_question WHERE synced=:synced")
fun getCountOfNotSyncedQuestions(synced: Boolean = false): LiveData<Int>

@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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ interface NoteDao {

@Query("SELECT COUNT(*) FROM note WHERE synced =:synced")
fun getCountOfNotSyncedNotes(synced: Boolean = false): LiveData<Int>

@Query("DELETE FROM note")
fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ interface PollingStationDao {

@Query("SELECT COUNT(*) FROM polling_station WHERE synced =:synced")
fun getCountOfNotSyncedPollingStations(synced: Boolean = false): LiveData<Int>

@Query("DELETE FROM polling_station")
fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
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()
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginStatus>()

fun loginLiveData(): LiveData<LoginStatus> = loginLiveData
Expand All @@ -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() {
Expand All @@ -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)
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/xml/remote_config_defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<key>filter_diaspora_forms</key>
<value>false</value>
</entry>
<entry>
<key>round_start_time</key>
<value>1605534872</value>
</entry>
</defaultsMap>

0 comments on commit 0071c9f

Please sign in to comment.