Skip to content

Instantly share code, notes, and snippets.

View alexmprog's full-sized avatar

Alexandr Golovach alexmprog

View GitHub Profile
@adam-hurwitz
adam-hurwitz / Event.kt
Last active August 30, 2020 15:50
UDF - Kotlin Flow vs. Rx: OnEachEvent view effects with Flow
/**
* Used as a wrapper for data that is exposed via an observable that represents an event.
* Developed by Jose Alcérreca.
* See [https://gist.github.com/JoseAlcerreca/5b661f1800e1e654f07cc54fe87441af#file-event-kt]
*/
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
@discord-gists
discord-gists / PanelsPreLayoutViewUpdates.kt
Last active August 30, 2020 15:35
a simplified code example of how Discord's Android app maintains panel states through device rotations while applying view updates before onLayout
class OverlappingPanelsLayout: FrameLayout {
private lateinit var startPanel: View
private lateinit var centerPanel: View
private lateinit var endPanel: View
private var pendingUpdate: (() -> Unit)? = null
...
private fun openStartPanel() {
@discord-gists
discord-gists / PanelsHorizontalScrollingInChildViews.kt
Last active March 2, 2022 19:22
a simplified code example of how Discord's Android app selectively allows child views of OverlappingPanelsLayout to handle their own horizontal scrolls
class PanelsChildGestureRegionObserver : View.OnLayoutChangeListener {
...
override fun onLayoutChange(
view: View?,
left: Int,
top: Int,
right: Int,
bottom: Int,
oldLeft: Int,
oldTop: Int,
@discord-gists
discord-gists / CustomPanelGestureDetection.kt
Last active March 2, 2022 19:16
a simplified code example for how Discord's Android app supports custom panel gesture detection
class OverlappingPanelsLayout : FrameLayout {
private var scrollingSlopPx: Float = 0f
private var velocityTracker: VelocityTracker? = null
private var isScrollingHorizontally = false
private var xFromInterceptActionDown: Float = 0f
private var yFromInterceptActionDown: Float = 0f
... // initialize scrollingSlopPx and VelocityTracker
@davidliu
davidliu / FragmentViewModelFactory.kt
Created February 16, 2020 20:49
View model factory extensions
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.savedstate.SavedStateRegistryOwner
@marcellogalhardo
marcellogalhardo / ActivityScope.kt
Last active January 20, 2021 10:11
Scoping Dagger Components with ViewModels
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
@objcode
objcode / ConcurrencyHelpers.kt
Last active February 19, 2026 14:35
Helpers to control concurrency for one shot requests using Kotlin coroutines.
/* Copyright 2019 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
/*
ModelBinding<T>: a simple class for binding a Flutter app to a immutable
model of type T.
This is a complete application. The app shows the state of an instance of
MyModel in a button's label, and replaces its MyModel instance when the
button is pressed.
ModelBinding is an inherited widget that must be created in a context above
@SabagRonen
SabagRonen / Presentation.java
Created October 6, 2018 13:38
Why Should You Wrap LiveData With Your Own Abstraction
interface MyGeneralObserver<T> {
void notifyUpdate(T value);
}
interface ObservableViewState {
void observeTitle(LifecycleOwner lifecycleOwner, MyGeneralObserver<String> observer);
}
interface IScreenPresenter extends ObservableViewState {
void loadData();
/**
* Delegate that sets and disposes the fragment's listener by casting it to the fragment's activity.
*/
class ParentActivityDelegate<T>(fragment: Fragment) : BaseParentDelegate<T>(fragment) {
override fun extractValue(fragment: Fragment): T? = fragment.activity as? T
}
/**
* Delegate that sets and disposes the fragment's listener by casting it to the fragment's