Skip to content

Commit

Permalink
#8/ Changed name for session restore method from sharedPref. Updated …
Browse files Browse the repository at this point in the history
…dependency version for RxJava. Checked to prevent reinitialization of the class
  • Loading branch information
temnik15 committed Nov 8, 2020
1 parent 3bf3057 commit 1790da8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// https://mvnrepository.com/artifact/io.reactivex.rxjava3/rxjava
implementation group: 'io.reactivex.rxjava3', name: 'rxjava', version: '3.0.0'
implementation group: 'io.reactivex.rxjava3', name: 'rxjava', version: '3.0.7'
// https://mvnrepository.com/artifact/io.reactivex.rxjava3/rxandroid
implementation group: 'io.reactivex.rxjava3', name: 'rxandroid', version: '3.0.0'
}
16 changes: 9 additions & 7 deletions app/src/main/java/so/codex/hawk/SessionKeeper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ object SessionKeeper {
* @param context Must be an applicationContext to avoid memory leaks.
*/
fun init(context: Context) {
this.context = context
val preferences = context.getSharedPreferences(KEY_SESSION_PREF, Context.MODE_PRIVATE)
getSessionFromPref(preferences)
preferences.registerOnSharedPreferenceChangeListener { pref, key ->
val value = pref.getString(key, "")!!
prefSubject.onNext(key to value)
if(!this::context.isInitialized){
this.context = context
val preferences = context.getSharedPreferences(KEY_SESSION_PREF, Context.MODE_PRIVATE)
restoreSessionFromPref(preferences)
preferences.registerOnSharedPreferenceChangeListener { pref, key ->
val value = pref.getString(key, "")!!
prefSubject.onNext(key to value)
}
}
}

Expand Down Expand Up @@ -99,7 +101,7 @@ object SessionKeeper {
* @param preferences sharedPreferences saved on the device.
* @see init
*/
private fun getSessionFromPref(preferences: SharedPreferences) {
private fun restoreSessionFromPref(preferences: SharedPreferences) {
val accessToken = preferences.getString(KEY_ACCESS_TOKEN, "")
val refreshToken = preferences.getString(KEY_REFRESH_TOKEN, "")
val timeStr = preferences.getString(KEY_TIME_SESSION, "0")
Expand Down

0 comments on commit 1790da8

Please sign in to comment.