Created
October 12, 2017 04:28
-
-
Save rubenweerts/162c15debbbf1c982bbb56ed500eff05 to your computer and use it in GitHub Desktop.
Collection of Kotlin extention functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, message, duration).show() | |
fun List<String>.containsAny(other: List<String>): Boolean { | |
forEach { left -> | |
other.forEach { right -> | |
if (left.equals(right)) { | |
return true | |
} | |
} | |
} | |
return false | |
} | |
fun View.hideKeyboard() { | |
val keyboard = getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | |
keyboard.hideSoftInputFromWindow(getApplicationWindowToken(), 0) | |
} | |
fun Context.openBrowser(url: String) = startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extension for RxJava to apply schedulers | |
*/ | |
//region schedulers | |
fun <T> Observable<T>.applySchedulers(): Observable<T> = compose { | |
subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) | |
} | |
fun <T> Flowable<T>.applySchedulers(): Flowable<T> = compose { | |
subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) | |
} | |
fun <T> Single<T>.applySchedulers(): Single<T> = compose { | |
subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) | |
} | |
fun Completable.applySchedulers(): Completable = compose { | |
subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) | |
} | |
//endregion | |
//region retryDelayed | |
fun <T> Observable<T>.retryDelayed(delayMillis: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS) = compose { | |
retryWhen { it.delay(delayMillis, timeUnit) } | |
} | |
fun <T> Flowable<T>.retryDelayed(delayMillis: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS) = compose { | |
retryWhen { it.delay(delayMillis, timeUnit) } | |
} | |
fun <T> Single<T>.retryDelayed(delayMillis: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS) = compose { | |
retryWhen { it.delay(delayMillis, timeUnit) } | |
} | |
fun Completable.retryDelayed(delayMillis: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS) = compose { | |
retryWhen { it.delay(delayMillis, timeUnit) } | |
} | |
//endregion | |
//region Fake loader delay | |
fun <T> Observable<T>.fakeLoadingDelay() = compose { | |
delay(Constants.FAKE_LOADING_TIME_MS, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) | |
} | |
fun <T> Flowable<T>.fakeLoadingDelay() = compose { | |
delay(Constants.FAKE_LOADING_TIME_MS, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) | |
} | |
fun <T> Single<T>.fakeLoadingDelay() = compose { | |
delay(Constants.FAKE_LOADING_TIME_MS, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) | |
} | |
fun Completable.fakeLoadingDelay() = compose { | |
delay(Constants.FAKE_LOADING_TIME_MS, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) | |
} | |
//endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//region LocalDateTime | |
fun LocalDateTime.isMorning(): Boolean = hour in 7..11 //some devs want to work early and test timeslots | |
fun LocalDateTime.isAfternoon(): Boolean = hour in 12..17 | |
fun LocalDateTime.isEvening(): Boolean = hour in 18..23 | |
fun LocalDateTime.isToday(): Boolean = toLocalDate().isEqual(LocalDate.now()) | |
fun LocalDateTime.isInPeriod(start: LocalDateTime, end: LocalDateTime) = isEqual(start) || isEqual(end) || (isAfter(start) && isBefore(end)) | |
//endregion | |
//region LocalTime | |
/** | |
* Formatter for local time | |
*/ | |
@JvmField | |
val LOCALTIME_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("H.mm") | |
fun LocalTime.format(): String = LOCALTIME_FORMATTER.format(this) | |
fun LocalTime.isMorning(): Boolean = hour in 0..11 | |
fun LocalTime.isAfternoon(): Boolean = hour in 12..17 | |
fun LocalTime.isEvening(): Boolean = hour in 18..23 | |
/** | |
* Get the duration until the next quarter hour. | |
* Defaults to seconds. | |
*/ | |
fun LocalTime.untilNextQuarter(unit: ChronoUnit = ChronoUnit.SECONDS): Long { | |
return this.until(this.nextQuarterHour(), unit) | |
} | |
/** | |
* Get the next quarter hour. | |
* i.e. 10:12 will result in 10:15 | |
*/ | |
fun LocalTime.nextQuarterHour(): LocalTime { | |
return this.truncatedTo(ChronoUnit.HOURS) | |
.plusMinutes(15L * (this.minute / 15)) | |
.plusMinutes(15L) | |
} | |
//endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment