Skip to content

Instantly share code, notes, and snippets.

Avatar
🍃

Takao Chiba chibatching

🍃
View GitHub Profile
@chibatching
chibatching / FlowThrottleDebounce.kt
Last active Mar 15, 2022
Throttle and Debounce on Flow Kotlin Coroutines
View FlowThrottleDebounce.kt
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 Feb 27, 2020
Use PagedList with Groupie
View PagedListGroup.java
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 Jan 21, 2020
Save all values include default values declared in KotprefModel
View SaveAllValueToSP.kt
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 Dec 5, 2019
Simple ViewModel Dagger Injection
View SomeFragment.kt
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 Apr 19, 2019
LiveData as Flow of Kotlin Coroutines
View FlowableLiveData.kt
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>()
View SampleViewModel.kt
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 Feb 3, 2018
OAuth signed POST request with Volley + oauth-signpost
View OAuthPostHurlStack.java
public class OAuthPostHurlStack extends HurlStack {
private final OAuthConsumer mConsumer;
private ArrayList<String> mOauthSignedPosts = new ArrayList<>();
public OAuthPostHurlStack(OAuthConsumer consumer) {
mConsumer = consumer;
}
@Override
View NestedCoordinatorLayout.kt
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
View LatLngUtils.kt
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 Nov 26, 2016
Create eclipse library project from google play service aar in android sdk
View play_service_proj_builder.py
from distutils.spawn import find_executable
import os
import re
import sys
import subprocess
import zipfile
import tempfile
import shutil