This file contains hidden or 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 hidden or 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 hidden or 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> |
This file contains hidden or 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 hidden or 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 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()) | |
} | |
}) | |
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 String.toEditable(): Editable = Editable.Factory.getInstance().newEditable(this) |
This file contains hidden or 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)) | |
} | |
} | |
} |
NewerOlder