Skip to content

Instantly share code, notes, and snippets.

View yongjhih's full-sized avatar
🏠
Working from home

Andrew Chen yongjhih

🏠
Working from home
View GitHub Profile
View BackCompatHandler.kt
/**
* See Also: BackHandler { onBack() }
*/
@Composable
fun BackCompatHandler(
enabled: Boolean = true,
onBack: () -> Unit = {}
) {
val currentOnBack by rememberUpdatedState(onBack)
val backCallback = remember {
View byWeakReference.kt
fun <T> byWeakReference(initialValue: T? = null): MutableProperty<T?> = mutablePropertyOf(WeakReference(initialValue)).map(
set = { if (this.field.get() == it) this.field else WeakReference(it) },
get = { it.get() },
)
View IBreakIterator.kt
import android.os.Build
import androidx.annotation.RequiresApi
import java.text.BreakIterator
import java.text.CharacterIterator
interface IBreakIterator {
fun first(): Int
fun last(): Int
fun next(): Int
fun next(value: Int): Int
View ModifierBorders.kt
fun Modifier.border(
top: Dp = 0.dp,
bottom: Dp = 0.dp,
start: Dp = 0.dp,
end: Dp = 0.dp,
color: Color,
) = border(
top = BorderStroke(top, color),
bottom = BorderStroke(bottom, color),
start = BorderStroke(start, color),
View GiphyRepository.kt
/**
* ```
* val giphyRepository = Retrofit.Builder()
* .baseUrl("https://api.giphy.com/")
* .addConverterFactory(Json {
* ignoreUnknownKeys = true
* }.asConverterFactory(contentType))
* .build()
* .create<GiphyRepository>()
*
View SharedPreferencesX.kt
class MyViewModel(private val sharedPreferences: SharedPreferences) : ViewModel {
var isAdult by sharedPreferences.isAdult
init {
println(isAdult) // null
isAdult = true
println(isAdult) // true
isAdult.value = false
println(isAdult) // false
View MutableSharedPreferencesState.kt
/**
* Interface representing a read-only property with a value
*
* Usage:
*
* ```
* val doubleValue by propertyOf(1).map { it * 2 }.map { it * 2 }
*
* doubleValue // 4
* ```
View ViewGroup.childrenByType.kt
inline fun <reified T : View> ViewGroup.childrenByType(): Sequence<T> = descendantsBreadth.filterIsInstance<T>()
val ViewGroup.descendantsBreadth: Sequence<View>
get() = sequence {
val queue: Queue<View> = LinkedList()
this@descendantsBreadth.forEach { child -> queue.offer(child) }
var child = queue.poll()
while (child != null) {
yield(child)
View findDeclaredField.kt
inline fun <reified T> Any.getDeclaredFieldValue(fieldName: String): T =
javaClass.findDeclaredField(fieldName).run {
if (!isAccessible) isAccessible = true
get(this@getDeclaredFieldValue) as T
}
inline fun <reified T> Any.getDeclaredFieldValueOrNull(fieldName: String): T? =
try { getDeclaredFieldValue(fieldName) }
catch (e: NoSuchFieldException) { null }
catch (e: SecurityException) { null }
View NestedScrollingChildImpl-crash.kt
private val childHelper by lazy { NestedScrollingChildHelper(this).apply {
//isNestedScrollingEnabled = false
} }
init {
isNestedScrollingEnabled = true
}
override fun setNestedScrollingEnabled(enabled: Boolean) {
return childHelper.setNestedScrollingEnabled(enabled) // Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object kotlin.Lazy.getValue()' on a null object reference