diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-07-15 23:52:28 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-07-16 00:25:07 +0200 |
| commit | 42564c71cfb70c28831c662a3b6bf4084e079353 (patch) | |
| tree | 111456fd3e8dce884d0380a81d70950062c7d212 /libs/utils/src/main | |
| parent | 4a8f6807c9d3ee6bcfac25aee832163036b4e6fe (diff) | |
Break out io code in libs
Preparing for adding more io implementations.
Really tried writing the convention plugins in kotlin dsl but could
not find the exact right hacks to get it to work.
Diffstat (limited to 'libs/utils/src/main')
| -rw-r--r-- | libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt | 14 | ||||
| -rw-r--r-- | libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt | 32 |
2 files changed, 46 insertions, 0 deletions
diff --git a/libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt b/libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt new file mode 100644 index 0000000..7f6ab1f --- /dev/null +++ b/libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt @@ -0,0 +1,14 @@ +package org.the_jk.cleversync + +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer + +fun <T> LiveData<T>.safeValue(): T? { + if (this.hasActiveObservers()) + return value + var ret: T? = null + val observer = Observer<T> { value -> ret = value } + this.observeForever(observer) + this.removeObserver(observer) + return ret +} diff --git a/libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt b/libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt new file mode 100644 index 0000000..6adea24 --- /dev/null +++ b/libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt @@ -0,0 +1,32 @@ +package org.the_jk.cleversync + +object StringUtils { + fun split(input: String, delimiter: Char, keepEmpty: Boolean = true, limit: Int = 0): List<String> { + return buildList { + var offset = 0 + var count = 0 + while (true) { + val next = input.indexOf(delimiter, offset) + if (next == -1) { + if (keepEmpty || offset < input.length) { + if (limit > 0 && count == limit) { + add("${removeLast()}${delimiter}${input.substring(offset)}") + break + } + add(input.substring(offset)) + } + break + } + if (keepEmpty || offset < next) { + if (limit > 0 && count == limit) { + add("${removeLast()}${delimiter}${input.substring(offset)}") + break + } + add(input.substring(offset, next)) + count++ + } + offset = next + 1 + } + } + } +} |
