Skip to content

Instantly share code, notes, and snippets.

View UploadFilesUseCase.kt
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()
View UseCase.kt
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 Dec 21, 2019
Base class for Java Observable which needs to be aware of whether it's "busy" and expose this information to its clients
View BaseBusyObservable.java
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 May 8, 2020
Base class for Java Observable
View BaseObservable.java
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 Jul 18, 2021 — forked from SangsooNam/gh-pages-deploy.sh
Script to deploy a target directory to `gh-pages` branch and force server-side cache to update
View gh-pages-deploy.sh
#!/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 Jul 21, 2021
Helpers to control concurrency for one shot requests using Kotlin coroutines.
View ConcurrencyHelpers.kt
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
View CoroutinesTestRule.kt
@ExperimentalCoroutinesApi
class CoroutinesTestRule(
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}
View FragmentBindingModule.kt
@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 May 13, 2021
Example MVI Implementation with Coroutines
View ExampleActivity.kt
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 Jan 6, 2019
Script to deploy a target directory to `gh-pages` branch.
View gh-pages-deploy.sh
#!/bin/bash
directory=_site
branch=gh-pages
build_command() {
jekyll build
}
echo -e "\033[0;32mDeleting old content...\033[0m"
rm -rf $directory