Skip to content

Instantly share code, notes, and snippets.

@roschlau
roschlau / UploadFilesUseCase.kt
Last active May 9, 2020 07:33
Kotlin Coroutines implementation of "More Granular Retry" from https://www.techyourchance.com/concurrency-frameworks-overrated-android/
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import java.io.File
class UploadFilesUseCase : BaseBusyObservable<UploadFilesUseCase.Listener>() {
interface Listener {
fun onFilesUploaded()
class UploadFilesUseCase(
private val schedulerProvider: SchedulerProvider,
private val httpManager: HttpManager
) {
private var operation: Completable? = null
fun uploadFiles(): Completable = synchronized(this) {
operation
?: (doUploadFiles()
@techyourchance
techyourchance / BaseBusyObservable.java
Created December 21, 2019 10:41
Base class for Java Observable which needs to be aware of whether it's "busy" and expose this information to its clients
public abstract class BaseBusyObservable<LISTENER_CLASS> extends BaseObservable<LISTENER_CLASS> {
private final AtomicBoolean mIsBusy = new AtomicBoolean(false);
public final boolean isBusy() {
return mIsBusy.get();
}
/**
* Atomically assert not busy and become busy
@techyourchance
techyourchance / BaseObservable.java
Last active December 28, 2021 13:38
Base class for Java Observable
public abstract class BaseObservable<LISTENER_CLASS> {
private final Object MONITOR = new Object();
private final Set<LISTENER_CLASS> mListeners = new HashSet<>();
public void registerListener(LISTENER_CLASS listener) {
synchronized (MONITOR) {
boolean hadNoListeners = mListeners.size() == 0;
mListeners.add(listener);
@georgegach
georgegach / gh-pages-deploy.sh
Last active August 21, 2022 07:42 — forked from SangsooNam/gh-pages-deploy.sh
Script to deploy a target directory to `gh-pages` branch and force server-side cache to update
#!/bin/bash
directory=_site
branch=gh-pages
build_command() {
jekyll build
}
echo -e "\033[0;32mDeleting existing $branch...\033[0m"
git push origin --delete $branch
git branch -D $branch
@objcode
objcode / ConcurrencyHelpers.kt
Last active January 15, 2024 05:17
Helpers to control concurrency for one shot requests using Kotlin coroutines.
/* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ExperimentalCoroutinesApi
class CoroutinesTestRule(
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}
@Module
abstract class FragmentBindingModule {
@Binds
abstract fun bindFragmentFactory(factory: FragmentInjectionFactory): FragmentFactory
@Binds
@IntoMap
@FragmentKey(MainFragment::class)
abstract fun bindMainFragment(fragment: MainFragment): Fragment
@ZakTaccardi
ZakTaccardi / ExampleActivity.kt
Last active October 20, 2023 02:27
Example MVI Implementation with Coroutines
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.CoroutineScope
class ExampleActivity : Activity(), CoroutineScope by CoroutineScope(Dispatchers.Main) {
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
val ui = Ui(this) // bind views, etc
@SangsooNam
SangsooNam / gh-pages-deploy.sh
Created January 6, 2019 23:11
Script to deploy a target directory to `gh-pages` branch.
#!/bin/bash
directory=_site
branch=gh-pages
build_command() {
jekyll build
}
echo -e "\033[0;32mDeleting old content...\033[0m"
rm -rf $directory