mirror of
https://github.com/Yubico/yubioath-flutter.git
synced 2024-12-23 18:22:39 +03:00
refactor CompatUtil
This commit is contained in:
parent
4149f99f88
commit
567ae5ac60
@ -42,13 +42,18 @@ class CompatUtil(private val sdkVersion: Int) {
|
|||||||
/**
|
/**
|
||||||
* Wrapper class holding values computed by [CompatUtil]
|
* Wrapper class holding values computed by [CompatUtil]
|
||||||
*/
|
*/
|
||||||
class CompatValue<T>() {
|
class CompatValue<T> private constructor() {
|
||||||
var value: T? = null
|
companion object {
|
||||||
var isValid: Boolean = false
|
fun <T> of(value: T) = CompatValue(value)
|
||||||
|
fun <T> empty() = CompatValue<T>()
|
||||||
|
}
|
||||||
|
|
||||||
constructor(value: T) : this() {
|
private var value: T? = null
|
||||||
|
private var hasValue: Boolean = false
|
||||||
|
|
||||||
|
private constructor(value: T) : this() {
|
||||||
this.value = value
|
this.value = value
|
||||||
isValid = true
|
hasValue = true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,7 +61,7 @@ class CompatUtil(private val sdkVersion: Int) {
|
|||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun otherwise(block: () -> T): T =
|
fun otherwise(block: () -> T): T =
|
||||||
if (isValid) {
|
if (hasValue) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
null as T
|
null as T
|
||||||
} else {
|
} else {
|
||||||
@ -78,11 +83,7 @@ class CompatUtil(private val sdkVersion: Int) {
|
|||||||
* @return wrapped value
|
* @return wrapped value
|
||||||
*/
|
*/
|
||||||
fun <T> until(version: Int, block: () -> T): CompatValue<T> =
|
fun <T> until(version: Int, block: () -> T): CompatValue<T> =
|
||||||
if (sdkVersion < version) {
|
`when`({ sdkVersion < version }, block)
|
||||||
CompatValue(block())
|
|
||||||
} else {
|
|
||||||
CompatValue()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute [block] only on devices running higher or equal sdk version than [version]
|
* Execute [block] only on devices running higher or equal sdk version than [version]
|
||||||
@ -90,11 +91,17 @@ class CompatUtil(private val sdkVersion: Int) {
|
|||||||
* @return wrapped value
|
* @return wrapped value
|
||||||
*/
|
*/
|
||||||
fun <T> from(version: Int, block: () -> T): CompatValue<T> =
|
fun <T> from(version: Int, block: () -> T): CompatValue<T> =
|
||||||
if (sdkVersion >= version) {
|
`when`({ sdkVersion >= version }, block)
|
||||||
CompatValue(block())
|
|
||||||
} else {
|
/**
|
||||||
CompatValue()
|
* Execute [block] only when predicate [p] holds
|
||||||
}
|
*
|
||||||
|
* @return wrapped value
|
||||||
|
*/
|
||||||
|
private fun <T> `when`(p: () -> Boolean, block: () -> T): CompatValue<T> = when {
|
||||||
|
p() -> CompatValue.of(block())
|
||||||
|
else -> CompatValue.empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val compatUtil = CompatUtil(Build.VERSION.SDK_INT)
|
val compatUtil = CompatUtil(Build.VERSION.SDK_INT)
|
Loading…
Reference in New Issue
Block a user