Skip to content

Commit

Permalink
Add Optional Validate Lambda to stringPref
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincianfarini committed May 10, 2019
1 parent f5ef7a4 commit f9c9462
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions krate/src/androidTest/java/hu/autsoft/krate/OptionalTests.kt
Expand Up @@ -2,6 +2,7 @@ package hu.autsoft.krate

import android.support.test.InstrumentationRegistry.getTargetContext
import android.support.test.runner.AndroidJUnit4
import hu.autsoft.krate.exception.KrateValidationException
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -103,4 +104,18 @@ class OptionalTests {
assertEquals(null, testKrate.optionalStringSet)
}

@Test(expected = KrateValidationException::class)
fun testOptionalStringValidatedPrefFailsValidation() {
assertEquals(null, testKrate.optionalValidatedString)

testKrate.optionalValidatedString = "Lorem ipsum dolor sit amet"
}

@Test
fun testOptionalStringValidatedPassesValidation() {
assertEquals(null, testKrate.optionalValidatedString)

testKrate.optionalValidatedString = "Lorem"
}

}
3 changes: 3 additions & 0 deletions krate/src/androidTest/java/hu/autsoft/krate/TestKrate.kt
Expand Up @@ -14,5 +14,8 @@ class TestKrate(context: Context) : SimpleKrate(context) {
var optionalLong by longPref("optionalLong")
var optionalString by stringPref("optionalString")
var optionalStringSet by stringSetPref("optionalStringSet")
var optionalValidatedString by stringPref("validatedString") {
it?.length ?: 5 == 5
}

}
9 changes: 7 additions & 2 deletions krate/src/main/kotlin/hu/autsoft/krate/Functions.kt
Expand Up @@ -46,9 +46,14 @@ public fun Krate.longPref(key: String): ReadWriteProperty<Krate, Long?> {

/**
* Creates an optional preference of type String with the given [key] in this [Krate] instance.
*
* @param [key] the key to identify this value in SharedPreferences
* @param [isValid] the validation function to be used when setting this property
*
* @return [StringDelegate]
*/
public fun Krate.stringPref(key: String): ReadWriteProperty<Krate, String?> {
return StringDelegate(key)
public fun Krate.stringPref(key: String, isValid: (String?) -> Boolean = { true }): ReadWriteProperty<Krate, String?> {
return StringDelegate(key, isValid)
}

/**
Expand Down
@@ -0,0 +1,3 @@
package hu.autsoft.krate.exception

class KrateValidationException(message: String) : Exception(message)
@@ -1,12 +1,14 @@
package hu.autsoft.krate.optional

import hu.autsoft.krate.Krate
import hu.autsoft.krate.exception.KrateValidationException
import hu.autsoft.krate.util.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

internal class StringDelegate(
private val key: String
private val key: String,
private val isValid: (String?) -> Boolean
) : ReadWriteProperty<Krate, String?> {

override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? {
Expand All @@ -18,6 +20,8 @@ internal class StringDelegate(
}

override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: String?) {
if (!isValid(value)) throw KrateValidationException("$value not valid.")

if (value == null) {
thisRef.sharedPreferences.edit { remove(key) }
} else {
Expand Down

0 comments on commit f9c9462

Please sign in to comment.