Skip to content

Instantly share code, notes, and snippets.

View es0329's full-sized avatar

Eric es0329

View GitHub Profile
@qamarelsafadi
qamarelsafadi / OnBackPressedAlternativeWay.kt
Last active April 4, 2023 10:09
OnBackPressed Alternative way for fragments and activities
/* ----------------------------------------- Activity ------------------------------------------ */
/* make sure you have at least 'androidx.activity:activity-ktx:1.6.0-rc01' at your dependencies
(just to let you know this dependency is not stable yet )
*/
implementation 'androidx.activity:activity-ktx:1.6.0-rc01'
fun AppCompatActivity.onBackPressed(isEnabled: Boolean, callback: () -> Unit) {
onBackPressedDispatcher.addCallback(this,
@GeePawHill
GeePawHill / gist:9d4bb8a21531f369e90be5e86207a2bb
Created August 10, 2022 01:49
A Real World Kotlin Builder
// Snippet one: This code creates an ImageView control inside of a Pane.
val root = pane {
imageview {
image = Image("/bucket.png")
isPreserveRatio = true
}
}
// Snippet two: This is the signature of the function imageview that's being called in the above snippet
@VitalyPeryatin
VitalyPeryatin / AppNavGraph.kt
Created July 6, 2022 19:26
Pass parcelable arguments (Compose)
...
override fun registerGraph(
navGraphBuilder: NavGraphBuilder,
navController: NavHostController,
modifier: Modifier,
parameters: NavGraphParameters
) {
...
navGraphBuilder.composable(VideoAnswerDestination) { navBackStackEntry ->
val arguments: VideoAnswerDestination.Arguments =
@VitalyPeryatin
VitalyPeryatin / AppActivity.kt
Created July 6, 2022 19:17
Open screen chain (Compose)
class AppActivity : BaseActivity() {
private val viewModel: AppViewModel by viewModel()
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
tryOpenScreenChain(intent)
}
private fun tryOpenScreenChain(intent: Intent?) {
@vinaygaba
vinaygaba / codeInsightSettings.xml
Last active October 12, 2022 01:14
Configuration setting that makes Jetpack Compose development more efficient by removing the noise from auto complete. More information in this article - https://www.jetpackcompose.app/articles/productivity-hack-to-save-tens-of-engineering-hours-when-working-with-Jetpack-Compose
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaProjectCodeInsightSettings">
<excluded-names>
<name>android.app.LauncherActivity.ListItem</name>
<name>android.graphics.drawable.Icon</name>
<name>android.graphics.fonts.FontFamily</name>
<name>android.inputmethodservice.Keyboard.Row</name>
<name>android.text.layout.Alignment</name>
<name>android.widget.GridLayout.Alignment</name>
@surajsau
surajsau / OAuthInterceptor.kt
Created April 8, 2022 10:47
HERE Platform Request OAuth 2.0 Token API's HeaderInterceptor
/*
Reference link: https://developer.here.com/documentation/identity-access-management/dev_guide/topics/sdk.html
*/
class OAuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val timeStampInMillis = System.currentTimeMillis()
val request = chain.request()
@colinrtwhite
colinrtwhite / complex_forwarding_painter.kt
Last active October 9, 2025 07:39
A painter that wraps another painter to overwrite its color filter, alpha, and/or onDraw.
/**
* Create and return a new [Painter] that wraps [painter] with its [alpha], [colorFilter], or [onDraw] overwritten.
*/
fun forwardingPainter(
painter: Painter,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
onDraw: DrawScope.(ForwardingDrawInfo) -> Unit = DefaultOnDraw,
): Painter = ForwardingPainter(painter, alpha, colorFilter, onDraw)
@kasem-sm
kasem-sm / CoilWithPlaceholder.kt
Created February 6, 2022 15:49
Small workaround to show thumbnail before loading actual image with Coil.
var isOriginalImageLoaded = false
// Thumbnail Request
val thumbRequest = ImageRequest.Builder(ctx)
.data(thumbUrl)
.target(
onSuccess = { result ->
// If highRes image is not loaded yet,
// show the thumbnail
if (!isOriginalImageLoaded) {
@raamcosta
raamcosta / UiText.kt
Created October 6, 2021 23:00
UiText.kt
sealed class UiText {
class DynamicText(val text: String) : UiText()
class ResourceText(@StringRes val stringRes: Int) : UiText()
}
fun UiText.getString(context: Context) = when (this) {
is UiText.DynamicText -> text
is UiText.ResourceText -> context.getString(stringRes)
@darvld
darvld / CircularReveal.kt
Created October 3, 2021 23:03
A circular reveal effect modifier for Jetpack Compose.
package cu.spin.catalog.ui.components
import android.annotation.SuppressLint
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.updateTransition
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset