Skip to content

Instantly share code, notes, and snippets.

View abhimuktheeswarar's full-sized avatar

Abhi Muktheeswarar abhimuktheeswarar

View GitHub Profile
@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 / 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 / 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 / counterStateMachine.kt
Last active August 6, 2021 06:15
counterStateMachine for blog
fun CoroutineScope.counterStateMachine(
initialState: CounterState,
mutableStateFlow: MutableStateFlow<CounterState>,
mutableMessages: MutableSharedFlow<CounterMessage>,
) =
actor<CounterMessage> {
var state: CounterState = initialState
channel.consumeEach { message ->
when (message) {
is IncrementCounter -> {
@abhimuktheeswarar
abhimuktheeswarar / CounterSideEffect.kt
Last active August 6, 2021 06:17
CounterSideEffect for blog
class Repository {
suspend fun update(count: Int): Boolean {
TODO("Save to DB")
}
}
class CounterSideEffect(
private val repository: Repository,
private val counterStateStore: CounterStateStore,
@abhimuktheeswarar
abhimuktheeswarar / CounterViewModel.kt
Created August 1, 2021 10:13
CounterViewModel for blog
class CounterViewModel(private val counterStateStore: CounterStateStore) : ViewModel() {
val stateFlow: Flow<CounterState> = counterStateStore.stateFlow
fun dispatch(message: CounterMessage) {
counterStateStore.dispatch(message)
}
override fun onCleared() {
super.onCleared()
@abhimuktheeswarar
abhimuktheeswarar / CounterMessage.kt
Created August 1, 2021 10:20
CounterMessage for blog
sealed interface CounterMessage
object IncrementCounter : CounterMessage
object DecrementCounter : CounterMessage
class GetCounterState(val deferred: CompletableDeferred<CounterState>) : CounterMessage
@abhimuktheeswarar
abhimuktheeswarar / customCounterStateMachine.kt
Last active August 1, 2021 11:22
customCounterStateMachine for blog
fun CoroutineScope.customCounterStateMachine(channel : ReceiveChannel<CounterMessage>)
= launch {
var counter = 0 // actor state
channel.consumeEach { message ->
ensureActive()
when (message) {
is IncrementCounter -> counter++
is GetCounter -> message.response.complete(counter)
}
}
@abhimuktheeswarar
abhimuktheeswarar / DownloadAndOpenPdf.kt
Last active September 15, 2021 09:57
Code snippet for downloading and opening a PDF file
class DownloadAndOpenPdf(
private val applicationContext: Context,
private val okHttpClient: OkHttpClient,
private val scope: CoroutineScope
) {
private val tag = "DownloadAndOpenPdf"
private var inProgress: Boolean = false
private lateinit var file: File
@abhimuktheeswarar
abhimuktheeswarar / TaskManager.java
Created November 22, 2021 09:04 — forked from chuangx/TaskManager.java
Bring your launcher task to front
public class TaskManager {
/**
* Bring up launcher task to front
*/
public void navToLauncherTask(@Nonnull Context appContext) {
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
// iterate app tasks available and navigate to launcher task (browse task)
final List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : appTasks) {