aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-17 11:28:09 -0700
committerAllan Wang <me@allanwang.ca>2017-06-17 11:28:09 -0700
commit575b3827baa7d74ac723a6131433993ea2935da4 (patch)
treecf5922570c4115c3b93bbc6cb38c30a41e62320b /library/src/main/kotlin/ca
parent9ae298d6691d56467627198cc557a8765cc0e9a9 (diff)
downloadkau-575b3827baa7d74ac723a6131433993ea2935da4.tar.gz
kau-575b3827baa7d74ac723a6131433993ea2935da4.tar.bz2
kau-575b3827baa7d74ac723a6131433993ea2935da4.zip
Add either
Diffstat (limited to 'library/src/main/kotlin/ca')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt2
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt1
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt1
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt32
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt6
5 files changed, 36 insertions, 6 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
index 5495c9f..4af837b 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
@@ -1,10 +1,8 @@
package ca.allanwang.kau.kpref.items
-import android.support.annotation.StringRes
import android.view.View
import android.widget.CheckBox
import ca.allanwang.kau.R
-import ca.allanwang.kau.kpref.KPrefAdapterBuilder
import ca.allanwang.kau.utils.tint
/**
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt
index 4f4192c..294aec0 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt
@@ -2,7 +2,6 @@ package ca.allanwang.kau.kpref.items
import android.view.View
import ca.allanwang.kau.R
-import ca.allanwang.kau.kpref.KPrefAdapterBuilder
/**
* Created by Allan Wang on 2017-06-07.
diff --git a/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
index 3b4d651..02f5219 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
@@ -11,6 +11,7 @@ import timber.log.Timber
open class TimberLogger(tag: String) {
internal val TAG = "$tag: %s"
fun e(s: String) = Timber.e(TAG, s)
+ fun e(t: Throwable) = Timber.e(t, TAG, "error")
fun d(s: String) = Timber.d(TAG, s)
fun i(s: String) = Timber.i(TAG, s)
fun v(s: String) = Timber.v(TAG, s)
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt
new file mode 100644
index 0000000..dab5810
--- /dev/null
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt
@@ -0,0 +1,32 @@
+package ca.allanwang.kau.utils
+
+/**
+ * Created by Allan Wang on 2017-06-17.
+ *
+ * Courtesy of adelnizamutdinov
+ *
+ * https://github.com/adelnizamutdinov/kotlin-either
+ */
+@Suppress("unused")
+sealed class Either<out L, out R>
+
+data class Left<out T>(val value: T) : Either<T, Nothing>()
+data class Right<out T>(val value: T) : Either<Nothing, T>()
+
+inline fun <L, R, T> Either<L, R>.fold(left: (L) -> T, right: (R) -> T): T =
+ when (this) {
+ is Left -> left(value)
+ is Right -> right(value)
+ }
+
+inline fun <L, R, T> Either<L, R>.flatMap(f: (R) -> Either<L, T>): Either<L, T> =
+ fold({ this as Left }, f)
+
+inline fun <L, R, T> Either<L, R>.map(f: (R) -> T): Either<L, T> =
+ flatMap { Right(f(it)) }
+
+val <T> Either<T, *>.isLeft: Boolean
+ get() = this is Left<T>
+
+val <T> Either<*, T>.isRight: Boolean
+ get() = this is Right<T> \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt
index a8ddd2a..fca5677 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt
@@ -108,11 +108,11 @@ private fun viewNotFound(id:Int, desc: KProperty<*>): Nothing =
@Suppress("UNCHECKED_CAST")
private fun <T, V : View> required(id: Int, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> t.finder(id) as V? ?: viewNotFound(id, desc) }
+ = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) }
@Suppress("UNCHECKED_CAST")
private fun <T, V : View> optional(id: Int, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> t.finder(id) as V? }
+ = Lazy { t: T, _ -> t.finder(id) as V? }
@Suppress("UNCHECKED_CAST")
private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?)
@@ -120,7 +120,7 @@ private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?)
@Suppress("UNCHECKED_CAST")
private fun <T, V : View> optional(ids: IntArray, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> ids.map { t.finder(it) as V? }.filterNotNull() }
+ = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() }
// Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it
private class Lazy<T, V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> {