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 registerForActivityResult.kt
fun <I> ActivityResultCaller.registerForActivityResult(
onCreateIntent: Context.(input: I) -> Intent,
callback: ActivityResultCallback<Pair<Int, Intent?>>,
) = registerForActivityResult(
activityResultContract(onCreateIntent),
callback,
)
/**
* ```
@yongjhih
yongjhih / DashedBorder.kt
Created September 27, 2023 12:17 — forked from DavidIbrahim/DashedBorder.kt
dashedBorder modifier for android compose
View DashedBorder.kt
import androidx.compose.foundation.BorderStroke
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
View side_sheet.dart
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
const Duration _sideSheetEnterDuration = Duration(milliseconds: 250);
const Duration _sideSheetExitDuration = Duration(milliseconds: 200);
const Curve _modalSideSheetCurve = decelerateEasing;
const double _minFlingVelocity = 700.0;
const double _closeProgressThreshold = 0.5;
View View.scrollChangesCompact.kt
@RequiresApi(Build.VERSION_CODES.M)
fun View.scrollChanges() = callbackFlow {
val listener = View.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
trySendBlocking(Quintuple(v, scrollX, scrollY, oldScrollX, oldScrollY))
}
setOnScrollChangeListener(listener)
awaitClose { setOnScrollChangeListener(null) }
}
fun ViewTreeObserver.scrollChanges() = callbackFlow {
View reflections.kt
fun <T : Any> Class<T>.getDeclaredFieldOrNull(name: String): Field? =
tryOrNull { getDeclaredField(name) }
@Suppress("SpreadOperator")
fun <T : Any> Class<T>.getDeclaredMethodOrNull(name: String, vararg parameterTypes: Class<*>): Method? =
tryOrNull { getDeclaredMethod(name, *parameterTypes) }
@Suppress("SpreadOperator")
fun <T : Any> Class<T>.getDeclaredConstructorOrNull(vararg parameterTypes: Class<*>): Constructor<T>? =
tryOrNull { getDeclaredConstructor(*parameterTypes) }
@yongjhih
yongjhih / MemoizedInterceptor.kt
Last active August 17, 2023 15:16
Memoized Cache Interceptor
View MemoizedInterceptor.kt
/**
* Intercepts and caches memoized responses based on the provided LruCache.
*
* @property cache an instance of LruCache to store the responses.
*/
class MemoizedInterceptor(private val cache: LruCache<String, Response>) : Interceptor {
/**
* Callback that will be invoked when a cached response is found.
* @param request the request being executed.
* @param isFresh `true` if the cached response is still fresh, `false` otherwise.
View context.activity.kt
@JvmName("getActivityInternal")
internal tailrec fun Context.getActivity(): Activity? =
this as? Activity
?: (this as? ContextWrapper)?.baseContext?.getActivity()
val Context.activity: Activity? get() = getActivity()
val View.activity: Activity? get() = context.getActivity()
View SimpleLifecycleObserver.kt
class SimpleLifecycleObserver(
var onCreate: SimpleLifecycleObserver.(LifecycleOwner) -> Unit = {},
var onStart: SimpleLifecycleObserver.(LifecycleOwner) -> Unit = {},
var onResume: SimpleLifecycleObserver.(LifecycleOwner) -> Unit = {},
var onPause: SimpleLifecycleObserver.(LifecycleOwner) -> Unit = {},
var onStop: SimpleLifecycleObserver.(LifecycleOwner) -> Unit = {},
var onDestroy: SimpleLifecycleObserver.(LifecycleOwner) -> Unit = {},
) : DefaultLifecycleObserver {
fun onCreate(onCreate: SimpleLifecycleObserver.(LifecycleOwner) -> Unit) = apply {
this.onCreate = onCreate
View WipEllipsisText.kt
@Preview
@Composable
fun EllipsisTextPreview(
) {
EllipsisText(
text = "This is a very long text that should This is a very long text that should This is a very long text that should bbbThis is a very long text that should be truncated",
maxLines = 2,
)
}
View EllipseText.kt
@Composable
fun EllipseText(
text: String,
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
fontStyle: FontStyle? = null,
maxLines: Int = Int.MAX_VALUE,
onMoreText: Context.() -> String = { "... Show More" },
moreTextStyle: SpanStyle = SpanStyle(fontWeight = FontWeight.W500),
textAlign: TextAlign? = null