Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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>
@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 / EditTextUtils.kt
Created September 5, 2017 02:11
Kotlin: simple onTextChanged listener in Android
fun TextView.onTextChanged(block: (String) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
block(s.toString())
}
})
}
@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 / AndroidUtils.kt
Created September 5, 2017 02:04
Kotlin: string to editable
fun String.toEditable(): Editable = Editable.Factory.getInstance().newEditable(this)
@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))
}
}
}