Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
STAY HOME

Keita Kajiwara kakajika

🏠
STAY HOME
View GitHub Profile
@kakajika
kakajika / ConcatObjectAdapter.kt
Created Sep 13, 2021
Concat multiple androidx.leanback.widget.ObjectAdapter like androidx.recyclerview.widget.ConcatAdapter
View ConcatObjectAdapter.kt
class ConcatObjectAdapter(
private vararg val adapters: ObjectAdapter
) : ObjectAdapter() {
init {
adapters.forEach { adapter ->
adapter.registerObserver(object : DataObserver() {
override fun onChanged() {
super.onChanged()
this@ConcatObjectAdapter.notifyChanged()
}
@kakajika
kakajika / MaxLinearLayout.java
Last active Jun 7, 2022
Custom LinearLayout takes max measured width of all children for its width.
View MaxLinearLayout.java
/**
* @author kakajika
* @since 2016/04/27
*/
public class MaxLinearLayout extends LinearLayout {
public MaxLinearLayout(Context context) {
super(context);
}
@kakajika
kakajika / ScopeFuncs.swift
Last active Nov 22, 2021
A port of Kotlin's scope functions to Swift.
View ScopeFuncs.swift
protocol ScopeFunc {}
extension ScopeFunc {
@inline(__always) func apply(block: (Self) -> ()) -> Self {
block(self)
return self
}
@inline(__always) func letIt<R>(block: (Self) -> R) -> R {
return block(self)
}
}
@kakajika
kakajika / FullscreenOverlayService.java
Last active Oct 12, 2021
Android Service implementation with fullscreen overlay window (over status bar & navigation bar)
View FullscreenOverlayService.java
class FullscreenOverlayService extends Service {
private View overlay;
private WindowManager windowManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@kakajika
kakajika / AvoidSpinnerDropdownFocus.java
Last active Aug 30, 2021
Avoid taking window focus by Android Spinner's Dropdown to keep setSystemUiVisibility flags (such as Immersive Mode).
View AvoidSpinnerDropdownFocus.java
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.Spinner;
public static void avoidSpinnerDropdownFocus(Spinner spinner) {
try {
Field listPopupField = Spinner.class.getDeclaredField("mPopup");
listPopupField.setAccessible(true);
Object listPopup = listPopupField.get(spinner);
if (listPopup instanceof ListPopupWindow) {
@kakajika
kakajika / LineHeightSpanCompat.kt
Created Apr 30, 2021
LineHeightSpan.Standard for lower API versions
View LineHeightSpanCompat.kt
import android.os.Build
import android.text.style.LineHeightSpan
import androidx.annotation.Px
import kotlin.math.roundToInt
object LineHeightSpanCompat {
fun standard(@Px lineHeight: Int): LineHeightSpan =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
LineHeightSpan.Standard(lineHeight)
} else {
@kakajika
kakajika / PathSimplifier.swift
Last active Jul 14, 2020
SwiftによるCGPathの曲線近似の実装(Inspired by paper.js)
View PathSimplifier.swift
// PathSimplifier.swift
import UIKit
private let TOLERANCE: CGFloat = 1e-6
private let EPSILON: CGFloat = 1e-12
extension CGPoint {
func add(p: CGPoint) -> CGPoint {
return CGPointMake(x+p.x, y+p.y)
@kakajika
kakajika / RxFirebaseDatabase.kt
Last active Apr 27, 2020
FirebaseDatabase events listener with RxKotlin
View RxFirebaseDatabase.kt
import com.google.firebase.database.*
import rx.Observable
import rx.lang.kotlin.add
import rx.lang.kotlin.observable
/**
* @author kakajika
* @since 2016/11/08
*/
class RxFirebaseDatabase {
@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 / 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