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
| @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() |
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
| /** | |
| * 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. |
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
| 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 |
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 <I> ActivityResultCaller.registerForActivityResult( | |
| onCreateIntent: Context.(input: I) -> Intent, | |
| callback: ActivityResultCallback<Pair<Int, Intent?>>, | |
| ) = registerForActivityResult( | |
| activityResultContract(onCreateIntent), | |
| callback, | |
| ) | |
| /** | |
| * ``` |
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
| @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, | |
| ) | |
| } |
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
| @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 |
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 <T: View> T.focuses(): Flow<Pair<T, Boolean>> = callbackFlow { | |
| val oldListener = onFocusChangeListener | |
| val listener = View.OnFocusChangeListener { v, hasFocus -> | |
| oldListener?.onFocusChange(v, hasFocus) | |
| @Suppress("UNCHECKED_CAST") | |
| trySend(v as T to hasFocus) | |
| } | |
| onFocusChangeListener = listener | |
| awaitClose { onFocusChangeListener = oldListener } | |
| } |
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 <T> Sequence<T>.until(predicate: (T) -> Boolean) = sequence { | |
| for (it in this@until) { | |
| if (predicate(it)) break | |
| yield(it) | |
| } | |
| } | |
| fun <T> Sequence<T>.untilInclusive(predicate: (T) -> Boolean) = sequence { | |
| for (it in this@untilInclusive) { | |
| yield(it) |
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 CharSequence.indicesOf(pattern: String): Sequence<Int> = sequence { | |
| var startIndex = 0 | |
| while (true) { | |
| startIndex = indexOf(pattern, startIndex) | |
| if (startIndex < 0) break | |
| yield(startIndex) | |
| startIndex += pattern.length | |
| } | |
| } |
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
| @OptIn(ExperimentalContracts::class) | |
| fun <T: () -> R, R> T.measureTimeMillis(test: Boolean = true, onMeasure: (Long) -> Unit): R { | |
| contract { | |
| callsInPlace(this@measureTimeMillis, InvocationKind.EXACTLY_ONCE) | |
| } | |
| if (!test) return this() | |
| val start = System.currentTimeMillis() | |
| val res = this() | |
| onMeasure(System.currentTimeMillis() - start) |