Skip to content

Instantly share code, notes, and snippets.

View abhimuktheeswarar's full-sized avatar

Abhi Muktheeswarar abhimuktheeswarar

View GitHub Profile
@abhimuktheeswarar
abhimuktheeswarar / CounterActivity.kt
Last active August 1, 2021 10:19
CounterActivity
class CounterActivity : AppCompatActivity() {
private val viewModel by viewModels<CounterViewModel>()
private lateinit var binding: ActivityCounterBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCounterBinding.inflate(layoutInflater)
setContentView(binding.root)
@abhimuktheeswarar
abhimuktheeswarar / CounterViewModel.kt
Last active July 17, 2021 16:49
Simple CounterViewModel
class CounterViewModel(private val store: Store<CounterState>) : ViewModel() {
val actions: Flow<Action> = store.actions
val states: Flow<CounterState> = store.states
override fun onCleared() {
super.onCleared()
store.terminate()
}
@abhimuktheeswarar
abhimuktheeswarar / SimpleStore.kt
Last active July 17, 2021 15:08
Simple store
class Store<S : State>(
initialState: S,
reduce: (Action, S) -> S,
val scope: CoroutineScope,
) {
private val inputActionsChannel: Channel<Action> =
Channel(capacity = Channel.UNLIMITED)
private val outputChannel: Channel<S> = Channel()
@abhimuktheeswarar
abhimuktheeswarar / stateMachine.kt
Last active July 17, 2021 13:32
Actor based simple StateMachine
fun <S : State> CoroutineScope.stateMachine(
initialState: S,
inputActionsChannel: ReceiveChannel<Action>,
outputChannel: SendChannel<S>,
publishActions: MutableSharedFlow<Action>,
outputStateFlow: MutableStateFlow<S>,
reduce: (Action, S) -> S,
) = launch {
var state = initialState
@abhimuktheeswarar
abhimuktheeswarar / ConcurrencyHelpers.kt
Created June 8, 2021 18:52 — forked from objcode/ConcurrencyHelpers.kt
Helpers to control concurrency for one shot requests using Kotlin coroutines.
import kotlinx.coroutines.CoroutineStart.LAZY
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.yield
import java.util.concurrent.atomic.AtomicReference
import kotlin.DeprecationLevel.ERROR
@abhimuktheeswarar
abhimuktheeswarar / EqualSpaceItemDecoration.kt
Created June 27, 2020 10:46
This decorator adds equal spacing for items in GridLayoutManager
package com.yourcompany.name
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
class EqualSpaceItemDecoration(private val padding: Int) :
RecyclerView.ItemDecoration() {
@abhimuktheeswarar
abhimuktheeswarar / CheckableConstraintLayout.kt
Created June 26, 2020 05:47
A ConstraintLayout that implements Checkable interface
package com.yourcompany.name
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Checkable
import androidx.constraintlayout.widget.ConstraintLayout
class CheckableConstraintLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
@abhimuktheeswarar
abhimuktheeswarar / EqualSpacingGridItemDecorator.java
Created June 13, 2018 12:56
RecyclerView's ItemDecorator to achieve equal spacing around the items when using a GridLayoutManager
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class EqualSpacingGridItemDecorator extends RecyclerView.ItemDecoration {
private final int padding;
public EqualSpacingGridItemDecorator(int padding) {
@abhimuktheeswarar
abhimuktheeswarar / SquareCardView
Created August 7, 2017 14:46
Create a perfect square cardview
public class SquareCardView extends CardView {
public SquareCardView(Context context) {
super(context);
}
public SquareCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override