Skip to content

Instantly share code, notes, and snippets.

View chibatching's full-sized avatar
🍃

Takao Chiba chibatching

🍃
View GitHub Profile
@chibatching
chibatching / FlowThrottleDebounce.kt
Last active May 9, 2024 08:28
Throttle and Debounce on Flow Kotlin Coroutines
fun <T> Flow<T>.throttle(waitMillis: Int) = flow {
coroutineScope {
val context = coroutineContext
var nextMillis = 0L
var delayPost: Deferred<Unit>? = null
collect {
val current = SystemClock.uptimeMillis()
if (nextMillis < current) {
nextMillis = current + waitMillis
@chibatching
chibatching / PagedListGroup.java
Last active February 27, 2020 01:27
Use PagedList with Groupie
import android.arch.paging.AsyncPagedListDiffer;
import android.arch.paging.PagedList;
import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.AsyncDifferConfig;
import android.support.v7.util.DiffUtil;
import android.support.v7.util.ListUpdateCallback;
import com.xwray.groupie.Group;
import com.xwray.groupie.GroupDataObserver;
import com.xwray.groupie.Item;
@chibatching
chibatching / SaveAllValueToSP.kt
Last active January 21, 2020 03:31
Save all values include default values declared in KotprefModel
SomePref::class.declaredMemberProperties
.forEach { property ->
property.isAccessible = true
(property.getDelegate(SomePref) as? AbstractPref<Any>)?.let {
it.setToPreference(property, property.get(SomePref)!!, SomePref.preferences)
println("save preference ${property.name} -> ${property.get(SomePref)}")
}
(property.getDelegate(SomePref) as? AbstractStringSetPref)?.let {
getSharedPreferences(SomePref.kotprefName, Context.MODE_PRIVATE)
.edit()
@chibatching
chibatching / SomeFragment.kt
Last active December 5, 2019 15:05
Simple ViewModel Dagger Injection
import androidx.fragment.app.viewModels
import dagger.android.support.DaggerFragment
class SomeFragment : DaggerFragment() {
@Inject
lateinit var viewModelFactory: ViewModelFactory<SomeViewModel>
private val viewModel: SomeViewModel by viewModels { viewModelFactory }
@chibatching
chibatching / FlowableLiveData.kt
Last active April 19, 2019 05:50
LiveData as Flow of Kotlin Coroutines
fun <T> LiveData<T>.asFlow() = flowViaChannel<T?> {
it.send(value)
val observer = Observer<T> { t -> it.offer(t) }
observeForever(observer)
it.invokeOnClose {
removeObserver(observer)
}
}
val text = MutableLiveData<String>()
class SampleViewModel : ViewModel() {
val sampleData = UiAsyncLoadLiveData<String> {
value = try {
async(coroutineContext + CommonPool) {
"very long time process!"
}.await()
} catch (e: CancellationException) {
null
}
}
@chibatching
chibatching / OAuthPostHurlStack.java
Last active February 3, 2018 18:14
OAuth signed POST request with Volley + oauth-signpost
public class OAuthPostHurlStack extends HurlStack {
private final OAuthConsumer mConsumer;
private ArrayList<String> mOauthSignedPosts = new ArrayList<>();
public OAuthPostHurlStack(OAuthConsumer consumer) {
mConsumer = consumer;
}
@Override
import android.content.Context
import android.support.design.widget.CoordinatorLayout
import android.support.v4.view.NestedScrollingChild
import android.support.v4.view.NestedScrollingChildHelper
import android.util.AttributeSet
import android.view.View
/**
* Propagate nested scroll event to in/out nested coordinator layout
import com.google.android.gms.maps.model.LatLng
// Compute latitude and longitude from current lat lng,
// using Vincenty's direct formulae https://en.wikipedia.org/wiki/Vincenty's_formulae
fun LatLng.computeLatLngByDistanceAndBearing(distance: Double, bearing: Double): LatLng {
val PI = Math.PI / 180.0
val MAXITERS = 20
@chibatching
chibatching / play_service_proj_builder.py
Last active November 26, 2016 09:02
Create eclipse library project from google play service aar in android sdk
from distutils.spawn import find_executable
import os
import re
import sys
import subprocess
import zipfile
import tempfile
import shutil