Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
STAY HOME

Keita Kajiwara kakajika

🏠
STAY HOME
View GitHub Profile
@kakajika
kakajika / ReselectAwareNavigationView.kt
Created Mar 9, 2020
NavigationView with OnNavigationItemReselectedListener like BottomNavigationView.
View ReselectAwareNavigationView.kt
class ReselectAwareNavigationView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : NavigationView(context, attrs, defStyleAttr) {
interface OnNavigationItemReselectedListener {
fun onNavigationItemReselected(item: MenuItem)
}
private var selectedListener: OnNavigationItemSelectedListener? = null
@kakajika
kakajika / AnimatedVectorDrawableMarkerIcon.kt
Last active Mar 9, 2020
Workaround to use AnimatedVectorDrawable as marker icon on Google Maps SDK.
View AnimatedVectorDrawableMarkerIcon.kt
class AnimatedVectorDrawableMarkerIcon(
marker: Marker,
private val avd: AnimatedVectorDrawableCompat,
width: Int = avd.intrinsicWidth,
height: Int = avd.intrinsicHeight
) {
private val handler = Handler(Looper.getMainLooper())
private val invalidateTask = Runnable { avd.invalidateSelf() }
private var isRunning = false
private var isAutoLoop = false
@kakajika
kakajika / OneShotGlobalLayoutListener.kt
Last active Jan 30, 2020
Listener for View's global layout event only once like [OneShotPreDrawListener](https://developer.android.com/reference/androidx/core/view/OneShotPreDrawListener).
View OneShotGlobalLayoutListener.kt
class OneShotGlobalLayoutListener private constructor(
private val view: View,
private val runnable: Runnable
) : ViewTreeObserver.OnGlobalLayoutListener, View.OnAttachStateChangeListener {
private var viewTreeObserver: ViewTreeObserver = view.viewTreeObserver
init {
view.viewTreeObserver.addOnGlobalLayoutListener(this)
view.addOnAttachStateChangeListener(this)
}
@kakajika
kakajika / Pixel.kt
Last active Apr 25, 2020
Number extensions to convert px/dp/sp/pt/mm.
View Pixel.kt
/**
* px/dp/sp/pt/mm unit converter.
* This is not incompatible with Jetpack Compose.
*/
inline class Pixel(private val pxF: Float) {
val toPx: Int @Px get() = pxF.roundToInt()
fun toDp(res: Resources = Resources.getSystem()): Float = pxF / TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 1.0f, res.displayMetrics
)
@kakajika
kakajika / _LocaleTimePicker.java
Last active Aug 1, 2019
Android TimePicker with custom Locale.
View _LocaleTimePicker.java
public class LocaleTimePicker extends TimePicker {
public LocaleTimePicker(Context context) {
this(context, null);
}
public LocaleTimePicker(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, Resources.getSystem().getIdentifier("timePickerStyle", "attr", "android"));
}
@kakajika
kakajika / DimBehindPopupWindow.kt
Created Jul 10, 2019
PopupWindow with dim background
View DimBehindPopupWindow.kt
val popup = PopupWindow(...)
val container = popup.contentView.takeIf { it.layoutParams is WindowManager.LayoutParams }
?: (popup.contentView.parent as? View)?.takeIf { it.layoutParams is WindowManager.LayoutParams }
?: (popup.contentView.parent.parent as? View)?.takeIf { it.layoutParams is WindowManager.LayoutParams }
?: throw IllegalStateException("NO WindowManager.LayoutParams!")
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val lp = container.layoutParams as WindowManager.LayoutParams
lp.flags = lp.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
@kakajika
kakajika / Rx+mapNotNull.kt
Created Dec 13, 2018
mapNotNull operators for Rx Observable/Single/Maybe.
View Rx+mapNotNull.kt
fun <T, R> Observable<T>.mapNotNull(mapper: (T) -> R?): Observable<R> {
return lift { observer ->
object: Observer<T> {
override fun onNext(t: T) {
mapper(t)?.let { observer.onNext(it) }
}
override fun onComplete() {
observer.onComplete()
}
override fun onError(e: Throwable) {
@kakajika
kakajika / expand.kt
Last active Sep 24, 2018
A port of RxJS's expand to RxKotlin.
View expand.kt
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.rxkotlin.Flowables
import io.reactivex.rxkotlin.Maybes
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.Singles
fun <T> Observables.expand(source: Observable<T>, expander: (T) -> Observable<T>): Observable<T> {
@kakajika
kakajika / combinations.kt
Last active Sep 22, 2018
List all combinations of picking n elements.
View combinations.kt
fun <T> Collection<T>.combinations(n: Int): List<List<T>> {
return when {
n < 0 -> throw Error("combinations size cannot be negative")
n > size -> emptyList()
n == 1 -> map { mutableListOf(it) }
else -> foldIndexed<T, List<List<T>>>(mutableListOf()) { index, all, first ->
all + drop(index + 1)
.combinations(n - 1)
.map { it + first }
}
@kakajika
kakajika / lazyFast.kt
Created Sep 20, 2018
Non-atomic access extension for lazy.
View lazyFast.kt
fun <T> lazyFast(operation: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) {
operation()
}
You can’t perform that action at this time.