Skip to content

Instantly share code, notes, and snippets.

View cmorigaki's full-sized avatar

Cesar Morigaki cmorigaki

  • Campinas - Brasil
View GitHub Profile
@cmorigaki
cmorigaki / NestedRecyclerViewStateRecoverAdapter.kt
Last active February 10, 2021 14:10
[Nested RecyclerView State] - View recreation or refresh list
abstract class NestedRecyclerViewStateRecoverAdapter<T, VH : RecyclerView.ViewHolder>(
diffUtil: DiffUtil.ItemCallback<T>
) : ListAdapter<T, VH>(diffUtil) {
private val layoutManagerStates = hashMapOf<String, Parcelable?>()
private val visibleScrollableViews = hashMapOf<Int, ViewHolderRef>()
@CallSuper
override fun onViewRecycled(holder: VH) {
if (holder is NestedRecyclerViewViewHolder) {
// Save the scroll position state (LayoutManager state)
@cmorigaki
cmorigaki / NestedRecyclerViewStateRecoverAdapter.kt
Last active February 10, 2021 14:09
[Nested RecyclerView State] - Scrolling vertically
abstract class NestedRecyclerViewStateRecoverAdapter<T, VH : RecyclerView.ViewHolder>(
diffUtil: DiffUtil.ItemCallback<T>
) : ListAdapter<T, VH>(diffUtil) {
private val layoutManagerStates = hashMapOf<String, Parcelable?>()
@CallSuper
override fun onViewRecycled(holder: VH) {
if (holder is NestedRecyclerViewViewHolder) {
// Save the scroll position state (LayoutManager state)
val state = holder.getLayoutManager()?.onSaveInstanceState()
@cmorigaki
cmorigaki / MyViewModel.kt
Last active September 3, 2020 11:40
Replace SingleLiveEvent with Channel - ViewModel
class MyViewModel : ViewModel() {
protected val actionSender = BroadcastChannel<Action>(Channel.BUFFERED)
val actionReceiver = actionSender.asFlow()
}
@cmorigaki
cmorigaki / Activity.kt
Last active September 20, 2020 13:09
Replace SingleLiveEvent with Channel - Activity
private fun observeActionCommand() {
lifecycleScope.launchWhenStarted {
viewModel.actionReceiver.collect {
// TODO
}
}
}
@cmorigaki
cmorigaki / MarginItemDecoration.kt
Created May 24, 2020 21:08
Margin Item Decoration (Simple)
class MarginItemDecoration(private val spaceSize: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect, view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
with(outRect) {
if (parent.getChildAdapterPosition(view) == 0) {
top = spaceSize
}
@cmorigaki
cmorigaki / MarginItemDecoration.kt
Last active May 25, 2020 12:42
Margin Item Decoration (GridLayoutManager also)
class MarginItemDecoration(
private val spaceSize: Int,
private val spanCount: Int = 1,
private val orientation: Int = GridLayoutManager.VERTICAL
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State
) {
with(outRect) {
import Foundation
private class Localizer {
static let sharedInstance = Localizer()
lazy var localizableDictionary: NSDictionary! = {
if let path = Bundle.main.path(forResource: "Localizable", ofType: "plist") {
return NSDictionary(contentsOfFile: path)
}