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 / LottoTheme.kt
Last active November 2, 2021 11:35
Devfest 2021 Jetpack Compose Lottery App
@Composable
fun LottoTheme(
content : @Composable () -> Unit
) {
MaterialTheme(
content = content
)
}
@HarryTylenol
HarryTylenol / ConstraintLayout.kt
Created October 16, 2021 02:10
Compose ConstraintLayout Extensions
@Composable
fun ConstraintLayoutScope.t2t(
ref: ConstrainedLayoutReference? = null,
margin: Dp = 0.dp
): Constraint = {
val reference = ref ?: parent
top.linkTo(reference.top, margin)
}
fun <T> intentExtra(defaultValue: T? = null) = object : ReadOnlyProperty<Activity, T?> {
override fun getValue(thisRef: Activity, property: KProperty<*>): T? {
return when (defaultValue) {
is String -> thisRef.intent.getStringExtra(property.name) as T
is Int -> thisRef.intent.getIntExtra(property.name, defaultValue) as T
is Long -> thisRef.intent.getLongExtra(property.name, defaultValue) as T
// And More...
else -> null
}
}
binding.btnRequest.setOnClickListener {
viewModel.requestFestival( ... )
}
binding.btnRetry.setOnClickListener {
viewModel.refreshFestival()
}
viewModel.festival.observer {
// 페스티벌 데이터가 나옵니다.
}
val observer = { state: NetworkState? ->
private val festivalRequest = MutableLiveData<An>()
private val festivalResponse = festivalRequest.map { getFestivalUseCase.execute(it) }
val festival = festivalResponse.switchMap { it.data }
val festivalState = festivalResponse.switchMap { it.state }
val festivalRefreshState = festivalResponse.switchMap { it.refreshState }
fun getFestival( ... ): ResponseWrapper<FestivalData> {
val request: suspend () -> Unit = {
val festival = festivalAPI.get( ... )
("Festival API" to "Fetched from API").debug()
festivalDAO.insert(festival)
("Festival DAO" to "Inserted to DB").debug()
}
val refreshTrigger = MutableLiveData<Unit>()
val refreshState = refresh.switchMap {
("Festival Refresh" to "Refresh Request").debug()
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))
// ResponseWrapper
data class ResponseWrapper<T>(
val data: LiveData<T>,
val state: LiveData<NetworkState>,
val refreshState: LiveData<NetworkState>,
val refresh: () -> Unit
)
// NetworkState
// FestivalAPI
@GET(" ... ")
suspend fun get( ... ): FestivalData
// ...
// FestivalDAO
@Query("select * from festivals ...")
fun get( ... ): LiveData<FestivalData>
@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 {