Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Double preference #21

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ If this validation fails, an `IllegalArgumentException` will be thrown.

# Addons

Krate, by default, supports the types that `SharedPreferences` supports. These are `Boolean`, `Float`, `Int`, `Long`, `String` and `Set<String>`. You may of course want to store additional types in Krate.
Krate, by default, supports the types that `SharedPreferences` supports. These are `Boolean`, `Float`, `Int`, `Long`, `String` and `Set<String>`. Additionally, Krate supports `Double`. You may of course want to store additional types in Krate.

If you don't find support for the library or type you're looking for, implementing your own delegate in your own project based on the code of existing delegates should be quite simple, this is very much a supported use case. If you think your type might be commonly used, you can also open an issue to ask for an addon library for that type.

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ExampleActivity : AppCompatActivity() {
super.onResume()

booleanPreference.isChecked = exampleSettings.exampleBoolean
doublePreferenceInput.setText(exampleSettings.exampleDouble.toString())
floatPreferenceInput.setText(exampleSettings.exampleFloat.toString())
intPreferenceInput.setText(exampleSettings.exampleInt.toString())
longPreferenceInput.setText(exampleSettings.exampleLong.toString())
Expand All @@ -49,6 +50,7 @@ class ExampleActivity : AppCompatActivity() {
super.onPause()

exampleSettings.exampleBoolean = booleanPreference.isChecked
exampleSettings.exampleDouble = doublePreferenceInput.text.toString().toDouble()
exampleSettings.exampleFloat = floatPreferenceInput.text.toString().toFloat()
exampleSettings.exampleInt = intPreferenceInput.text.toString().toInt()
exampleSettings.exampleLong = longPreferenceInput.text.toString().toLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.SharedPreferences
import hu.autsoft.krate.Krate
import hu.autsoft.krate.booleanPref
import hu.autsoft.krate.doublePref
import hu.autsoft.krate.floatPref
import hu.autsoft.krate.intPref
import hu.autsoft.krate.longPref
Expand All @@ -21,6 +22,7 @@ class ExampleCustomKrate(context: Context) : Krate, ExampleSettings {
}

override var exampleBoolean by booleanPref("exampleBoolean", false)
override var exampleDouble by doublePref("exampleDouble", 0.0)
override var exampleFloat by floatPref("exampleFloat", 0f)
override var exampleInt by intPref("exampleInt", 0)
override var exampleLong by longPref("exampleLong", 0L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hu.autsoft.krateexample.krates

interface ExampleSettings {
var exampleBoolean: Boolean
var exampleDouble: Double
var exampleFloat: Float
var exampleInt: Int
var exampleLong: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hu.autsoft.krateexample.krates
import android.content.Context
import hu.autsoft.krate.SimpleKrate
import hu.autsoft.krate.booleanPref
import hu.autsoft.krate.doublePref
import hu.autsoft.krate.floatPref
import hu.autsoft.krate.intPref
import hu.autsoft.krate.longPref
Expand All @@ -16,6 +17,7 @@ class ExampleSimpleKrate(context: Context) : SimpleKrate(context, NAME), Example
}

override var exampleBoolean by booleanPref("exampleBoolean", false)
override var exampleDouble by doublePref("exampleDouble", 0.0)
override var exampleFloat by floatPref("exampleFloat", 0f)
override var exampleInt by intPref("exampleInt", 0)
override var exampleLong by longPref("exampleLong", 0L)
Expand Down
21 changes: 20 additions & 1 deletion app/src/main/res/layout/activity_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/floatPreference"
android:id="@+id/doublePreference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
Expand All @@ -31,6 +31,25 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/booleanPreference">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/doublePreferenceInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Double"
android:inputType="numberDecimal" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/floatPreference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/doublePreference">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/floatPreferenceInput"
android:layout_width="match_parent"
Expand Down
16 changes: 16 additions & 0 deletions krate/src/main/kotlin/hu/autsoft/krate/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package hu.autsoft.krate

import hu.autsoft.krate.default.BooleanDelegateWithDefault
import hu.autsoft.krate.default.DoubleDelegateWithDefault
import hu.autsoft.krate.default.FloatDelegateWithDefault
import hu.autsoft.krate.default.IntDelegateWithDefault
import hu.autsoft.krate.default.LongDelegateWithDefault
import hu.autsoft.krate.default.StringDelegateWithDefault
import hu.autsoft.krate.default.StringSetDelegateWithDefault
import hu.autsoft.krate.optional.BooleanDelegate
import hu.autsoft.krate.optional.DoubleDelegate
import hu.autsoft.krate.optional.FloatDelegate
import hu.autsoft.krate.optional.IntDelegate
import hu.autsoft.krate.optional.LongDelegate
Expand All @@ -23,6 +25,13 @@ public fun Krate.booleanPref(key: String): ReadWriteProperty<Krate, Boolean?> {
return BooleanDelegate(key)
}

/**
* Creates an option preference of type Double with the given [key] in this [Krate] instance.
*/
public fun Krate.doublePref(key: String): ReadWriteProperty<Krate, Double?> {
return DoubleDelegate(key)
}

/**
* Creates an optional preference of type [Float] with the given [key] in this [Krate] instance.
*/
Expand Down Expand Up @@ -66,6 +75,13 @@ public fun Krate.booleanPref(key: String, defaultValue: Boolean): ReadWritePrope
return BooleanDelegateWithDefault(key, defaultValue)
}

/**
* Creates a non-option preference of type Double with the given [key] and [defaultValue] in this [Krate] instance.
*/
public fun Krate.doublePref(key: String, defaultValue: Double): ReadWriteProperty<Krate, Double> {
return DoubleDelegateWithDefault(key, defaultValue)
}

/**
* Creates a non-optional preference of type [Float] with the given [key] and [defaultValue] in this [Krate] instance.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hu.autsoft.krate.default

import hu.autsoft.krate.Krate
import hu.autsoft.krate.util.edit
import hu.autsoft.krate.util.getDouble
import hu.autsoft.krate.util.putDouble
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

internal class DoubleDelegateWithDefault(
private val key: String,
private val default: Double
) : ReadWriteProperty<Krate, Double> {

override operator fun getValue(thisRef: Krate, property: KProperty<*>): Double {
return if (thisRef.sharedPreferences.contains(key)) {
thisRef.sharedPreferences.getDouble(key, default)
} else {
default
}
}

override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Double) {
thisRef.sharedPreferences.edit { putDouble(key, value) }
}
}
31 changes: 31 additions & 0 deletions krate/src/main/kotlin/hu/autsoft/krate/optional/DoubleDelegate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hu.autsoft.krate.optional

import hu.autsoft.krate.Krate
import hu.autsoft.krate.util.edit
import hu.autsoft.krate.util.getDouble
import hu.autsoft.krate.util.putDouble
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

internal class DoubleDelegate(
private val key: String
) : ReadWriteProperty<Krate, Double?> {

override operator fun getValue(thisRef: Krate, property: KProperty<*>): Double? {
return if (!thisRef.sharedPreferences.contains(key)) {
null
} else {
thisRef.sharedPreferences.getDouble(key, 0.0)
//thisRef.sharedPreferences.getLong(key, 0.0.toRawBits()).toDouble()
}
}

override fun setValue(thisRef: Krate, property: KProperty<*>, value: Double?) {
if (value == null) {
thisRef.sharedPreferences.edit { remove(key) }
} else {
thisRef.sharedPreferences.edit { putDouble(key, value) }
//thisRef.sharedPreferences.edit { putLong(key, value.toRawBits()) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ internal inline fun SharedPreferences.edit(edits: SharedPreferences.Editor.() ->
editor.edits()
editor.apply()
}

internal fun SharedPreferences.Editor.putDouble(key: String, double: Double) =
putLong(key, java.lang.Double.doubleToRawLongBits(double))

internal fun SharedPreferences.getDouble(key: String, default: Double) =
java.lang.Double.longBitsToDouble(getLong(key, java.lang.Double.doubleToRawLongBits(default)))
1 change: 1 addition & 0 deletions krate/src/test/java/hu/autsoft/krate/TestKrate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class TestKrate(context: Context) : SimpleKrate(context) {
}

var optionalBoolean by booleanPref("optionalBoolean")
var optionDouble by doublePref("optionalDouble")
var optionalFloat by floatPref("optionalFloat")
var optionalInt by intPref("optionalInt")
var optionalLong by longPref("optionalLong")
Expand Down
14 changes: 14 additions & 0 deletions krate/src/test/java/hu/autsoft/krate/test/OptionalTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ internal class OptionalTests {
assertEquals(null, testKrate.optionalBoolean)
}

@Test
fun testOptionalDoublePref() {
assertEquals(null, testKrate.optionDouble)

testKrate.optionDouble = -487741.0
assertEquals(-487741.0, testKrate.optionDouble)

testKrate.optionDouble = 215125.0
assertEquals(215125.0, testKrate.optionDouble)

testKrate.optionDouble = null
assertEquals(null, testKrate.optionDouble)
}

@Test
fun testOptionalFloatPref() {
assertEquals(null, testKrate.optionalFloat)
Expand Down