Skip to content

Commit

Permalink
v5.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jguerinet committed Feb 28, 2019
1 parent 3c58234 commit a9c50cc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Version 5.1.1 (2019-02-28)

- `prefs`:
- Pulled out the `SharedPrefLiveData` into a separate class to be used by others
- Made the `liveData()` function on the `BasePref` open

## Version 5.1.0 (2019-02-28)

- `prefs`:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android.useAndroidX=true
android.enableJetifier=true

# Project properties
suitcase_version=5.0.1
suitcase_version=5.1.1
group=com.guerinet

# Android specific properties
Expand Down
29 changes: 2 additions & 27 deletions prefs/src/main/java/com/guerinet/suitcase/prefs/BasePref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.lifecycle.LiveData
* @since 2.0.0
*/
abstract class BasePref<T>(
protected val prefs: SharedPreferences,
val prefs: SharedPreferences,
protected val key: String,
protected val defaultValue: T
) {
Expand All @@ -42,30 +42,5 @@ abstract class BasePref<T>(
*/
open fun clear() = prefs.edit().remove(key).apply()

fun liveData() = SharedPrefLiveData()

/**
* Abstract class that converts a Shared Pref to a LiveData. To be implemented for the different types of valies
*/
inner class SharedPrefLiveData : LiveData<T>() {

private val prefChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == this@BasePref.key) {
// If the changed key was this one, update the LiveData value
value = this@BasePref.value
}
}

override fun onActive() {
super.onActive()
// First value should be the current one
value = this@BasePref.value
prefs.registerOnSharedPreferenceChangeListener(prefChangeListener)
}

override fun onInactive() {
prefs.unregisterOnSharedPreferenceChangeListener(prefChangeListener)
super.onInactive()
}
}
open fun liveData(): LiveData<T> = SharedPrefLiveData(prefs, key, this::value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016-2019 Julien Guerinet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.guerinet.suitcase.prefs

import android.content.SharedPreferences
import androidx.lifecycle.LiveData

/**
* Abstract class that converts a Shared Pref to a LiveData. To be implemented for the different types of values
* Built off of: https://gist.github.com/rharter/1df1cd72ce4e9d1801bd2d49f2a96810
* @author Julien Guerinet
* @since 5.1.1
*/
class SharedPrefLiveData<T>(
private val prefs: SharedPreferences,
private val key: String,
private val valueFromPrefs: () -> T
) : LiveData<T>() {

private val prefChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == this.key) {
// If the changed key was this one, update the LiveData value
value = valueFromPrefs()
}
}

override fun onActive() {
super.onActive()
// First value should be the current one
value = valueFromPrefs()
prefs.registerOnSharedPreferenceChangeListener(prefChangeListener)
}

override fun onInactive() {
prefs.unregisterOnSharedPreferenceChangeListener(prefChangeListener)
super.onInactive()
}
}

0 comments on commit a9c50cc

Please sign in to comment.