Skip to content

Commit

Permalink
Remove bus and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
premnirmal committed Dec 23, 2021
1 parent 9f311d0 commit 40c363c
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.github.premnirmal.ticker.analytics.Analytics
import com.github.premnirmal.ticker.components.AsyncBus
import com.github.premnirmal.ticker.components.InAppMessage
import com.github.premnirmal.ticker.events.ErrorEvent
import com.github.premnirmal.ticker.model.IStocksProvider
import com.github.premnirmal.ticker.model.IStocksProvider.FetchState
import com.github.premnirmal.ticker.showDialog
import com.github.premnirmal.tickerwidget.R
import io.github.inflationx.viewpump.ViewPumpContextWrapper
Expand All @@ -25,7 +25,7 @@ import javax.inject.Inject
abstract class BaseActivity : AppCompatActivity() {

abstract val simpleName: String
@Inject internal lateinit var bus: AsyncBus
@Inject internal lateinit var stocksProvider: IStocksProvider
@Inject internal lateinit var analytics: Analytics
open val subscribeToErrorEvents = true
private var isErrorDialogShowing = false
Expand All @@ -47,12 +47,13 @@ abstract class BaseActivity : AppCompatActivity() {
super.onResume()
if (subscribeToErrorEvents) {
lifecycleScope.launch {
val errorFlow = bus.receive<ErrorEvent>()
errorFlow.collect { event ->
if (this.isActive && !isErrorDialogShowing && !isFinishing) {
isErrorDialogShowing = true
showDialog(event.message).setOnDismissListener { isErrorDialogShowing = false }
delay(500L)
stocksProvider.fetchState.collect { state ->
if (state is FetchState.Failure) {
if (this.isActive && !isErrorDialogShowing && !isFinishing) {
isErrorDialogShowing = true
showDialog(state.displayString).setOnDismissListener { isErrorDialogShowing = false }
delay(500L)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class AppModule(private val app: StocksApp) {

@Provides @Singleton fun provideClock(): AppClock = AppClockImpl

@Provides @Singleton fun provideEventBus(): AsyncBus = AsyncBus()

@Provides @Singleton fun provideMainThreadHandler(): Handler =
Handler(Looper.getMainLooper())

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import android.content.Context
import android.content.SharedPreferences
import com.github.premnirmal.ticker.AppPreferences
import com.github.premnirmal.ticker.components.AppClock
import com.github.premnirmal.ticker.components.AsyncBus
import com.github.premnirmal.ticker.components.Injector
import com.github.premnirmal.ticker.events.ErrorEvent
import com.github.premnirmal.ticker.model.IStocksProvider.FetchState
import com.github.premnirmal.ticker.network.StocksApi
import com.github.premnirmal.ticker.network.data.Holding
import com.github.premnirmal.ticker.network.data.Position
import com.github.premnirmal.ticker.network.data.Quote
import com.github.premnirmal.ticker.repo.StocksStorage
import com.github.premnirmal.ticker.widget.WidgetDataProvider
import com.github.premnirmal.tickerwidget.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -48,7 +45,6 @@ class StocksProvider : IStocksProvider, CoroutineScope {
@Inject lateinit var context: Context
@Inject lateinit var preferences: SharedPreferences
@Inject lateinit var appPreferences: AppPreferences
@Inject lateinit var bus: AsyncBus
@Inject lateinit var widgetDataProvider: WidgetDataProvider
@Inject lateinit var alarmScheduler: AlarmScheduler
@Inject lateinit var clock: AppClock
Expand Down Expand Up @@ -151,7 +147,7 @@ class StocksProvider : IStocksProvider, CoroutineScope {

override fun fetch(): Flow<FetchResult<List<Quote>>> = flow {
if (tickerSet.isEmpty()) {
bus.send(ErrorEvent(context.getString(R.string.no_symbols_in_portfolio)))
_fetchState.emit(FetchState.Failure(FetchException("No symbols in portfolio")))
emit(FetchResult.failure<List<Quote>>(FetchException("No symbols in portfolio")))
} else {
try {
Expand All @@ -163,7 +159,6 @@ class StocksProvider : IStocksProvider, CoroutineScope {
}
val fetchedStocks = fr.data
if (fetchedStocks.isEmpty()) {
bus.send(ErrorEvent(context.getString(R.string.refresh_failed)))
emit(FetchResult.failure<List<Quote>>(FetchException("Refresh failed")))
} else {
synchronized(tickers) {
Expand All @@ -181,7 +176,6 @@ class StocksProvider : IStocksProvider, CoroutineScope {
} catch (ex: Throwable) {
Timber.w(ex)
_fetchState.emit(FetchState.Failure(ex))
bus.send(ErrorEvent(context.getString(R.string.refresh_failed)))
val backOffTimeMs = exponentialBackoff.getBackoffDurationMs()
scheduleUpdateWithMs(backOffTimeMs)
emit(FetchResult.failure<List<Quote>>(FetchException("Failed to fetch", ex)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.github.premnirmal.ticker.base.BaseActivity
import com.github.premnirmal.ticker.components.InAppMessage
import com.github.premnirmal.ticker.components.Injector
import com.github.premnirmal.ticker.dismissKeyboard
import com.github.premnirmal.ticker.model.IStocksProvider
import com.github.premnirmal.ticker.network.data.Holding
import com.github.premnirmal.tickerwidget.R
import kotlinx.android.synthetic.main.activity_positions.addButton
Expand All @@ -26,7 +25,6 @@ import kotlinx.android.synthetic.main.activity_positions.totalShares
import kotlinx.android.synthetic.main.activity_positions.totalValue
import kotlinx.android.synthetic.main.layout_position_holding.view.remove_position
import java.text.NumberFormat
import javax.inject.Inject

/**
* Created by premnirmal on 2/25/16.
Expand All @@ -38,7 +36,6 @@ class AddPositionActivity : BaseActivity() {
const val TICKER = "TICKER"
}

@Inject internal lateinit var stocksProvider: IStocksProvider
internal lateinit var ticker: String
override val simpleName: String = "AddPositionActivity"

Expand Down
10 changes: 6 additions & 4 deletions app/src/test/kotlin/com/github/premnirmal/ticker/BaseUnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doNothing
import com.nhaarman.mockitokotlin2.whenever
import junit.framework.TestCase
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Before
import org.junit.runner.RunWith
Expand All @@ -28,11 +30,11 @@ abstract class BaseUnitTest : TestCase() {
super.setUp()
val iStocksProvider = Mocker.provide(IStocksProvider::class)
doNothing().whenever(iStocksProvider).schedule()
whenever(iStocksProvider.fetch()).thenReturn(FetchResult.success(ArrayList()))
whenever(iStocksProvider.getTickers()).thenReturn(emptyList())
whenever(iStocksProvider.fetch()).thenReturn(flowOf(FetchResult.success(ArrayList())))
whenever(iStocksProvider.tickers).thenReturn(MutableStateFlow((emptyList())))
whenever(iStocksProvider.addStock(any())).thenReturn(emptyList())
whenever(iStocksProvider.fetchState).thenReturn(FetchState.NotFetched)
whenever(iStocksProvider.nextFetch()).thenReturn("--")
whenever(iStocksProvider.fetchState).thenReturn(MutableStateFlow(FetchState.NotFetched))
whenever(iStocksProvider.nextFetchMs).thenReturn(MutableStateFlow(0L))
}

fun parseJsonFile(fileName: String): JsonElement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import com.github.premnirmal.ticker.AppPreferences
import com.github.premnirmal.ticker.StocksApp
import com.github.premnirmal.ticker.analytics.Analytics
import com.github.premnirmal.ticker.components.AppClock
import com.github.premnirmal.ticker.components.AsyncBus
import com.github.premnirmal.ticker.repo.StocksStorage
import com.github.premnirmal.ticker.repo.QuoteDao
import com.github.premnirmal.ticker.repo.QuotesDB
import com.github.premnirmal.ticker.repo.StocksStorage
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
Expand All @@ -27,8 +26,6 @@ class MockAppModule(private val app: StocksApp) {

@Provides @Singleton internal fun provideClock(): AppClock = Mocker.provide(AppClock::class)

@Provides @Singleton internal fun provideEventBus(): AsyncBus = AsyncBus()

@Provides @Singleton internal fun provideMainThreadHandler(): Handler =
Handler(Looper.getMainLooper())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.premnirmal.ticker.mock

import android.content.Context
import com.github.premnirmal.ticker.components.AsyncBus
import com.github.premnirmal.ticker.model.AlarmScheduler
import com.github.premnirmal.ticker.model.IHistoryProvider
import com.github.premnirmal.ticker.model.IStocksProvider
Expand All @@ -28,8 +27,7 @@ import javax.inject.Singleton
class MockNetworkModule {

@Provides @Singleton internal fun provideHttpClient(
context: Context,
bus: AsyncBus
context: Context
): OkHttpClient =
Mocker.provide(OkHttpClient::class)

Expand Down

0 comments on commit 40c363c

Please sign in to comment.