Skip to content

Instantly share code, notes, and snippets.

View ericntd's full-sized avatar

Eric Nguyen ericntd

View GitHub Profile
@ericntd
ericntd / MainActivityComponentImpl.java
Last active June 27, 2021 22:42
Scoped dependencies
private final class MainActivityComponentImpl implements MainActivityComponent {
private Provider<MainPresenter> presenterProvider; // <-- Reference here
private MainActivityComponentImpl(
MainActivityComponent.MainActivityModule mainActivityModuleParam) {
initialize(mainActivityModuleParam);
}
@SuppressWarnings("unchecked")
@ericntd
ericntd / room-rxjava-read-render.kt
Last active May 31, 2021 20:10
Typical Room RxJava read stream and presentation of data
schoolDAO.getData() // Flowable
.subscribeOn(schedulerProvider.io())
.doOnSubscribe { showProgressSpinner() }
.flatMapSingle { school -> getExtraData(school) }
.observeOn(schedulerProvider.ui())
.subscribe({ data ->
hideProgressSpinner()
updateUi(data)
}, { throwable ->
hideProgressSpinner()
@ericntd
ericntd / timeout-rxjava.txt
Created May 31, 2021 19:57
Timeout exception RxJava
java.util.concurrent.TimeoutException: The source did not signal an event for 2 seconds and has been terminated.
at io.reactivex.internal.operators.observable.ObservableTimeoutTimed$TimeoutObserver.onTimeout(ObservableTimeoutTimed.java:134)
at io.reactivex.internal.operators.observable.ObservableTimeoutTimed$TimeoutTask.run(ObservableTimeoutTimed.java:165)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
@ericntd
ericntd / room-rxjava-timeout-happy-path.kt
Last active May 31, 2021 20:20
Simulate Room RxJava read timeout in happy path
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit
fun main() {
getFakeRoomReadStream()
.filter { it < 3 }
.timeout(2L, TimeUnit.SECONDS, Schedulers.io())
.observeOn(Schedulers.trampoline())
@ericntd
ericntd / gradle-tests-hang.md
Last active March 12, 2021 20:18
Gradle tests hang log
2021-03-12T15:15:55.747-0500 [DEBUG] [TestEventLogger] com.myapp.DummyTest > testErrorCase STARTED
2021-03-12T15:15:55.756-0500 [DEBUG] [TestEventLogger] 
2021-03-12T15:15:55.756-0500 [DEBUG] [TestEventLogger] com.myapp.DummyTest > testErrorCase STANDARD_ERROR
2021-03-12T15:15:55.757-0500 [DEBUG] [TestEventLogger]     io.reactivex.exceptions.CompositeException: 2 exceptions occurred. 
2021-03-12T15:15:55.757-0500 [DEBUG] [TestEventLogger]          at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:49)
2021-03-12T15:15:55.757-0500 [DEBUG] [TestEventLogger]          at io.reactivex.internal.disposables.EmptyDisposable.error(EmptyDisposable.java:78)
2021-03-12T15:15:55.757-0500 [DEBUG] [TestEventLogger]          at io.reactivex.internal.operators.single.SingleError.subscribeActual(SingleError.java:42)
2021-03-12T15:15:55.757-0500 [DEBUG] [TestEventLogger]          at io.reactivex.Single.subscribe(Single.java:3603)
2021-03-12T15:15:55.757-0500 [DEBUG] [TestEventLogge
@ericntd
ericntd / MyRoom.kt
Created December 29, 2020 16:41
Room configuration
@Database(
entities = [
//...
QuestionEntity::class
],
//...
)
abstract class MyDb : RoomDatabase() {
//...
abstract fun questionDao(): QuestionRoomDao
@ericntd
ericntd / matching-records-bst.kt
Last active December 15, 2020 18:31
Find matching records in 2 text files with Binary Search Tree
package matching_logs
import java.io.File
data class BinarySearchTree(
private val data: String,
internal var left: BinarySearchTree? = null,
internal var right: BinarySearchTree? = null
) {
private fun compare(s1: String, s2: String): Int {
@ericntd
ericntd / ActionCardV2.md
Last active December 1, 2020 19:35
Action Card 2.0

ActionCardInteractorV2 (More about Dagger multi-binding here)

@Inject
lateinit var cardHandlers : Map<String, com.coffeemeetsbagel.components.Builder>
//...

override fun didBecomeActive() {
    super.didBecomeActive()
 val key = theActionCard.action
@ericntd
ericntd / rxjava-polling.md
Created November 18, 2020 14:24
Repetitive actions with RxJava and unit tests

The Use Case

class PayInvoiceUseCase constructor(
    private val invoiceRepository: InvoiceRepository,
    private val schedulerProvider: SchedulerProvider
) {
    fun getInvoiceWPolling(invoiceId: Long): Observable<Result<Invoice, ErrorReason>> {
        val stopped = AtomicBoolean()
        println("start polling")
        return Observable.interval(2L, TimeUnit.SECONDS, schedulerProvider.io())
// prepare variable employees which is a list of Employee object
// Same as Task 0
employees.groupBy { it.department }
.map { group ->
val departmentName = group.key
val departmentEmployees = group.value
val departmentAverageSalary = departmentEmployees.sumBy { it.salary } / departmentEmployees.size
println("Average salary for department $departmentName is $departmentAverageSalary")
}