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
/** | |
* 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 |
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 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?) { } |
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
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)) | |
} | |
} | |
} |
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
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 | |
} |
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.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 | |
} | |
} |
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 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) | |
} | |
} | |
}) |
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 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 |
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
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) | |
} | |
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
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 |
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
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> |
NewerOlder