Skip to content

Instantly share code, notes, and snippets.

@maxost
maxost / WordUtils.kt
Last active November 20, 2023 14:24
Kotlin: Russian word generator for counts
/**
* genitivePlural - родительный падеж, множественное число (примеры: "часов", "минут", "секунд")
* nominativeSingular - именительный падеж, единственное число (примеры: "час", "минута", "секунда")
* genitiveSingular - родительный падеж, единственное число (примеры: "часа", "минуты", "секунды")
*/
fun getCountNoun(count: Int, genitivePlural: String, nominativeSingular: String, genitiveSingular: String): String {
val lastDigit = count.toString().last().toString().toInt()
val word = if (count in 5..20) genitivePlural
else if (lastDigit == 0 || lastDigit >= 5) genitivePlural
else if (lastDigit == 1) nominativeSingular
@maxost
maxost / EditTextUtils.kt
Created September 5, 2017 02:12
Kotlin: editText Observable extension in Android
fun EditText.toObservable(): Observable<String> {
return Observable.create({
val watcher = object : TextWatcher {
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
scream("aaa" + p0.toString())
it.onNext(p0.toString())
}
override fun afterTextChanged(p0: Editable?) { }
@maxost
maxost / FileUtils.kt
Created September 5, 2017 02:01
Kotlin: get file name in Android
private fun getFileName(context: Context, uri: Uri): String {
if (uri.scheme == "content") {
val cursor = context.contentResolver.query(uri, null, null, null, null)
cursor.use {
if(cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
}
}
}
@maxost
maxost / EnumExt.kt
Last active November 2, 2022 10:37
Kotlin: Enum 'contains' and 'valueOf' with default value implementations
inline fun <reified T : Enum<T>> enumContains(name: String): Boolean {
return enumValues<T>().any { it.name == name}
}
inline fun <reified T : Enum<T>> enumValueOf(name: String, defaultValue: T): T {
return try {
enumValues<T>().first { it.name == name }
} catch (e: NoSuchElementException) {
defaultValue
}
@maxost
maxost / AndroidUtils.kt
Created September 5, 2017 02:09
Kotlin: make phone call in Android
fun Context.makePhoneCall(number: String) : Boolean {
try {
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$number"))
startActivity(intent)
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
@maxost
maxost / AndroidUtils.kt
Created September 5, 2017 02:08
Kotlin: RecyclerView assign on scroll to end listener
fun RecyclerView.onScrollToEnd(linearLayoutManager: LinearLayoutManager, onScrollNearEnd: (Unit) -> Unit)
= addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
if (linearLayoutManager.childCount + linearLayoutManager.findFirstVisibleItemPosition()
>= linearLayoutManager.itemCount - 5) { //if near fifth item from end
onScrollNearEnd(Unit)
}
}
})
@maxost
maxost / AndroidUtils.kt
Created September 5, 2017 02:06
Kotlin: dp-px converters
fun Int.dp2px(context: Context): Int = (this * context.resources.displayMetrics.density).toInt()
fun Int.px2dp(context: Context): Int = (this / context.resources.displayMetrics.density).toInt()
fun Float.dp2px(context: Context): Float = this * context.resources.displayMetrics.density
fun Float.px2dp(context: Context): Float = this / context.resources.displayMetrics.density
@maxost
maxost / SmsReceiver.kt
Created September 5, 2017 02:40
Kotlin: sms receiver and rx bus in Android
data class Sms(val phone: String, val text: String)
object SmsBus {
private val bus by lazy { PublishSubject.create<Sms>() }
fun incomingSms(): Observable<Sms> = bus
fun postSms(sms: Sms) = bus.onNext(sms)
}
@maxost
maxost / Option.kt
Created September 5, 2017 02:41
Kotlin: simple Option implementation
sealed class Option<out A> {
abstract fun get(): A
object None : Option<Nothing>() {
override fun get(): Nothing = throw NoSuchElementException("None.get")
}
data class Some<out A>(val value: A) : Option<A>() {
override fun get(): A = value
@maxost
maxost / MySimpleAdapter.kt
Created September 5, 2017 02:14
Kotlin: simple RecyclerView adapter in Android
interface MySimpleAdapter<in T> {
fun setNewData(data: List<T>)
}
inline fun <T> simpleAdapter(
itemLayout: Int,
crossinline getId: (T) -> Long,
crossinline bind: (holder: RecyclerView.ViewHolder, item: T) -> Unit,
crossinline onItemClick: (T) -> Unit)
: RecyclerView.Adapter<RecyclerView.ViewHolder>