Skip to content

Instantly share code, notes, and snippets.

View HarryTylenol's full-sized avatar
🍊

Harry Park HarryTylenol

🍊
  • Goyang, Gyeong-Gi
View GitHub Profile
@HarryTylenol
HarryTylenol / AnkoRecyclerViewAdapter.kt
Last active April 11, 2022 21:29
Make RecyclerView with Anko Like Pro
abstract class AnkoRecyclerViewAdapter<Model : Any, AnkoView : AnkoComponent<ViewGroup>, ViewHolder : RecyclerView.ViewHolder> : RecyclerView.Adapter<ViewHolder>() {
abstract val data: List<Model> // Data list
abstract val ankoView: AnkoView // Layout as AnkoComponent<ViewGroup>
abstract fun ViewHolder.setup(model: Model) // setup model from ViewHolder
abstract val onItemClickListenerUnit: (Model) -> Unit // Item Click Listener
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.setup(data[position])
holder.itemView.setOnClickListener {
@HarryTylenol
HarryTylenol / FirestoreExtensions.kt
Created January 2, 2018 18:00
Nice Kotlin Extensions for Google Firestore
fun test() {
class City(var name: String, var location: GeoPoint)
var query = FirebaseFirestore.getInstance().collection("city").limit(10)
var error : (Exception) -> Unit = { it.printStackTrace() }
query.query<City>(callbackWithKey = { keyList, cityList -> }, error = error)
query.query(callback = { documentSnapshotList -> }, error = error)
query.addChangeListener(isAdded = {}, isRemoved = {}, isModified = {}, error = error)
@HarryTylenol
HarryTylenol / Auto_HashMappable_Object_Example.kt
Last active December 8, 2019 04:08
Auto HashMappable Object with Kotlin (Thanks to Property Delegation!)
class User : HashMappable() {
var name: String by HashMappableProperty("")
var age: Int? by HashMappableProperty(null)
}
fun main() {
val harry = User()
harry.name = "Harry"
@HarryTylenol
HarryTylenol / AnkoExtensions.kt
Created June 2, 2018 03:37
Anko Stream Extensions
inline fun <V : View> V.alpha(alpha: Double): V {
this.alpha = alpha.toFloat()
return this
}
inline fun <V : View> V.backgroundColor(color: Int): V {
backgroundColor = color
return this
}
@HarryTylenol
HarryTylenol / Anko+.kt
Last active June 19, 2018 00:29
Anko LiveData Binding
class LifecycleGetter<T : View>(var lifecycleOwner: LifecycleOwner, var view: T)
fun <T : View> T.observe(lifecycleOwner: LifecycleOwner): LifecycleGetter<T> {
return LifecycleGetter(lifecycleOwner, this)
}
fun <T : TextView> LifecycleGetter<T>.liveText(liveData: LiveData<String>): LifecycleGetter<T> {
liveData.observe(lifecycleOwner, Observer {
this.view.text = it
})
class DelegatedPreference<T>(val context: Context, val default: T) : ReadWriteProperty<Any, T> {
override fun getValue(thisRef: Any, property: KProperty<*>): T {
return context.defaultSharedPreferences.run {
when (default) {
is String -> getString(property.name, default)
is Long -> getLong(property.name, default)
is Int -> getInt(property.name, default)
is Boolean -> getBoolean(property.name, default)
@HarryTylenol
HarryTylenol / DateExtension.kt
Last active January 16, 2019 06:36
Android/Kotlin Date DSL
inline fun date(timeInMillis: Long = System.currentTimeMillis(), block: SimpleDate.() -> Unit = {}): SimpleDate {
val dateDsl = SimpleDate()
dateDsl.timeInMillis = timeInMillis
block(dateDsl)
return dateDsl
}
class SimpleDate {
// FestivalAPI
@GET(" ... ")
suspend fun get( ... ): FestivalData
// ...
// FestivalDAO
@Query("select * from festivals ...")
fun get( ... ): LiveData<FestivalData>
// ResponseWrapper
data class ResponseWrapper<T>(
val data: LiveData<T>,
val state: LiveData<NetworkState>,
val refreshState: LiveData<NetworkState>,
val refresh: () -> Unit
)
// NetworkState
private fun createStateForRequest(request: suspend () -> Unit) =
// LiveData (Lifecycle) 2.2.0-alph01 버전 부터 지원
liveData(Dispatchers.IO) {
emit(NetworkState.loading)
try {
request()
emit(NetworkState.success)
} catch (e: IOException) {
e.printStackTrace()
emit(NetworkState.failed(e))