View SaveAllValueToSP.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
View SomeFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
View FlowThrottleDebounce.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View FlowableLiveData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleViewModel : ViewModel() { | |
val sampleData = UiAsyncLoadLiveData<String> { | |
value = try { | |
async(coroutineContext + CommonPool) { | |
"very long time process!" | |
}.await() | |
} catch (e: CancellationException) { | |
null | |
} | |
} |
View PagedListGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
View NestedCoordinatorLayout.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View DatabindingUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.databinding.DataBindingUtil | |
import android.databinding.ViewDataBinding | |
fun <T : ViewDataBinding> Activity.contentViewBinding(layout: Int): Lazy<T> = lazy { | |
DataBindingUtil.setContentView<T>(this, layout) | |
} |
View SampleAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleAdapter() : RecyclerView.Adapter<SampleAdapter.ViewHolder>() { | |
var data: List<String> = emptyList() | |
var onClick: (String) -> Unit = {} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? { | |
val binding = ItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false) | |
binding.root.setOnClickListener { | |
onClick(data[(parent as RecyclerView).getChildAdapterPosition(it)]) | |
} | |
return ViewHolder(it) |
NewerOlder