Skip to content

Instantly share code, notes, and snippets.

View bherbst's full-sized avatar

Bryan Herbst bherbst

View GitHub Profile
@bherbst
bherbst / HackyGlanceNetworkImage.kt
Last active December 17, 2021 20:34
HackyGlanceNetworkImage
@Composable
fun NetworkImage(imageUrl: String, description: String) {
val context = LocalContext.current
LaunchedEffect(imageUrl) {
Coil.execute(
ImageRequest.Builder(context)
.data(imageUrl)
.target { result ->
loadedBitmaps[imageUrl] = (result as BitmapDrawable).bitmap
}
@bherbst
bherbst / Colors.kt
Created April 1, 2021 18:14
Style Dictionary - Compose Dark Mode
package com.example.tokens
import androidx.compose.ui.graphics.Color
class Colors(
val textPrimary: Color,
val textSecondary: Color,
val textInverse: Color,
val textSecondaryInverse: Color,
)
@bherbst
bherbst / AutofillEditText.kt
Last active May 21, 2018 17:48
Intercepting Autofill
class AutofillEditText : TextInputEditText {
@TargetApi(Build.VERSION_CODES.O)
override fun autofill(value: AutofillValue) {
val autofilledText = value.textValue
val processedText = sanitize(autofilledText)
super.autofill(AutofillValue.forText(processedText))
}
@bherbst
bherbst / MaybeUndeliverable.kt
Last active December 15, 2017 14:50
ConnectableObservable - potential UndeliverableException
someApi.retrofitCall() // This is a network request made using Retrofit
.publish()
.connect()
@bherbst
bherbst / RxJavaAutoConnectCrash.kt
Last active December 11, 2017 16:54
RxJava AutoConnect global error handler
val sharedSource = Observable.just(1)
.delay(1, TimeUnit.SECONDS)
.map { _ ->
// Some operation that throws an exception
throw Exception()
}
.publish()
.autoConnect(2)
val disposable = CompositeDisposable()
@bherbst
bherbst / RxJavaZipWithCrash.kt
Created December 11, 2017 15:52
RxJava - zipWith crash
val observable1 = Observable.error<Int>(Exception())
val observable2 = Observable.error<Int>(Exception())
val zipper = BiFunction<Int, Int, String> { one, two -> "$one - $two" }
observable1.zipWith(observable2, zipper)
.subscribe(
{ System.out.println(it) },
{ it.printStackTrace() }
)
@bherbst
bherbst / LifecycleProperty.kt
Created August 14, 2017 14:47
Android LifecycleProperty delegate
/**
* A property that automatically clears its reference according to the Android lifecycle.
*
* When the value for this property is set, it will be scheduled to be cleared in the opposing
* lifecycle event. For example, a property whose value is set during `onCreate()` will be cleared
* during `onDestroy()`.
*/
class LifecycleProperty<T> : LifecycleObserver {
companion object {
private val opposingEvents = mapOf(
@bherbst
bherbst / SampleFragment.kt
Last active July 21, 2017 14:21
Android View Nullability
class SampleFragment : Fragment() {
private var textView: TextView? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main2, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
@bherbst
bherbst / SampleFragment.kt
Last active July 21, 2017 14:35
Kotlin Android Extensions nullability
import kotlinx.android.synthetic.main.fragment_main.*
class SampleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onDestroy() {
super.onDestroy()
@bherbst
bherbst / gradle.properties
Created June 16, 2017 15:30
Typical gradle.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip