Skip to content

Commit

Permalink
Merge pull request #288 from wealthfront/coroutines-sample
Browse files Browse the repository at this point in the history
Migrate sample app to use coroutines
  • Loading branch information
cmathew committed Oct 12, 2023
2 parents 9d57a03 + a832fbd commit d68e331
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 62 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {
}

dependencies {
implementation 'com.android.tools.build:gradle:8.1.1'
implementation 'com.android.tools.build:gradle:8.1.2'
}

compileKotlin {
Expand Down
6 changes: 0 additions & 6 deletions magellan-sample-migration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ android {
dependencies {
implementation project(':magellan-library')
implementation project(':magellan-legacy')
implementation project(':magellan-rx2')

implementation Libs.appCompat
implementation Libs.dagger
kapt Libs.daggerCompiler

implementation Libs.glide
implementation Libs.retrofit
implementation Libs.rxjava2
implementation Libs.rxAndroid2
implementation Libs.rxJava2Adapter
implementation Libs.jackson
implementation Libs.okhttp
implementation Libs.coroutines
Expand All @@ -78,8 +74,6 @@ dependencies {
testImplementation Libs.truth

androidTestImplementation Libs.extJunit
androidTestImplementation Libs.rxjava2
androidTestImplementation Libs.rx2idler
androidTestImplementation Libs.espressoCore
androidTestImplementation Libs.espressoContrib
androidTestImplementation Libs.testCore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wealthfront.magellan

import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.idling.CountingIdlingResource
import com.wealthfront.magellan.sample.migration.api.CallEventListener
import com.wealthfront.magellan.sample.migration.api.RequestCountingInterceptor
import org.junit.rules.ExternalResource

class CoroutineIdlingRule : ExternalResource() {
private val coroutineCountingIdlingResource = CountingIdlingResource("coroutine")

override fun after() {
IdlingRegistry.getInstance().unregister(coroutineCountingIdlingResource)
RequestCountingInterceptor.listener = null
}

override fun before() {
RequestCountingInterceptor.listener = object : CallEventListener {
override fun onRequestEnd() {
coroutineCountingIdlingResource.decrement()
}

override fun onRequestStart() {
coroutineCountingIdlingResource.increment()
}
}
IdlingRegistry.getInstance().register(coroutineCountingIdlingResource)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.wealthfront.magellan.CoroutineIdlingRule
import com.wealthfront.magellan.sample.migration.MainActivity
import com.wealthfront.magellan.sample.migration.R
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class NavigationTest {

lateinit var activityScenario: ActivityScenario<MainActivity>
@Rule @JvmField
val coroutineIdlingRule = CoroutineIdlingRule()

private lateinit var activityScenario: ActivityScenario<MainActivity>

@Before
fun setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import com.wealthfront.magellan.navigation.NavigationTraverser;
import com.wealthfront.magellan.sample.migration.api.DogApi;
import com.wealthfront.magellan.sample.migration.api.RequestCountingInterceptor;
import com.wealthfront.magellan.sample.migration.toolbar.ToolbarHelper;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

@Module
Expand Down Expand Up @@ -49,7 +48,6 @@ DogApi provideDogApi(Retrofit retrofit) {
Retrofit provideRetrofit(OkHttpClient httpClient) {
return new Retrofit.Builder()
.baseUrl(DOG_BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.addConverterFactory(JacksonConverterFactory.create())
.client(httpClient)
.build();
Expand All @@ -60,7 +58,10 @@ Retrofit provideRetrofit(OkHttpClient httpClient) {
OkHttpClient provideHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder().addInterceptor(interceptor).build();
return new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(new RequestCountingInterceptor())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wealthfront.magellan.sample.migration.api

import com.fasterxml.jackson.annotation.JsonProperty
import io.reactivex.Observable
import retrofit2.http.GET
import retrofit2.http.Path

Expand All @@ -11,7 +10,7 @@ interface DogApi {
suspend fun getListOfAllBreedsOfRetriever(): DogBreeds

@GET("breed/{id}/images/random")
fun getRandomImageForBreed(@Path("id") breed: String): Observable<DogMessage>
suspend fun getRandomImageForBreed(@Path("id") breed: String): DogMessage
}

data class DogMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.wealthfront.magellan.sample.migration.api

import okhttp3.Interceptor
import okhttp3.Response

class RequestCountingInterceptor : Interceptor {

companion object {

var listener: CallEventListener? = null
}

override fun intercept(chain: Interceptor.Chain): Response {
listener?.onRequestStart()
val response = chain.proceed(chain.request())
listener?.onRequestEnd()
return response
}
}

interface CallEventListener {

fun onRequestStart()
fun onRequestEnd()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ import android.view.View
import android.widget.Toast
import com.wealthfront.magellan.OpenForMocking
import com.wealthfront.magellan.Screen
import com.wealthfront.magellan.lifecycle.attachFieldToLifecycle
import com.wealthfront.magellan.rx2.RxDisposer
import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.R
import com.wealthfront.magellan.sample.migration.api.DogApi
import com.wealthfront.magellan.sample.migration.toolbar.ToolbarHelper
import com.wealthfront.magellan.transitions.CircularRevealTransition
import io.reactivex.android.schedulers.AndroidSchedulers.mainThread
import kotlinx.coroutines.launch
import javax.inject.Inject

@OpenForMocking
class DogDetailsScreen(private val breed: String) : Screen<DogDetailsView>() {

@Inject lateinit var api: DogApi
@Inject lateinit var toolbarHelper: ToolbarHelper
private val rxUnsubscriber by attachFieldToLifecycle(RxDisposer())

override fun createView(context: Context): DogDetailsView {
(context.applicationContext as AppComponentContainer).injector().inject(this)
Expand All @@ -34,13 +31,10 @@ class DogDetailsScreen(private val breed: String) : Screen<DogDetailsView>() {
}
toolbarHelper.setMenuColor(R.color.water)

rxUnsubscriber.autoDispose(
api.getRandomImageForBreed(breed)
.observeOn(mainThread())
.subscribe {
view!!.setDogPic(it.message)
}
)
shownScope.launch {
val imageResponse = api.getRandomImageForBreed(breed)
view!!.setDogPic(imageResponse.message)
}
}

fun goToHelpScreen(originView: View) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import android.content.Context
import android.content.DialogInterface
import com.wealthfront.magellan.LegacyStep
import com.wealthfront.magellan.OpenForMocking
import com.wealthfront.magellan.lifecycle.attachFieldToLifecycle
import com.wealthfront.magellan.rx2.RxDisposer
import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.api.DogApi
import io.reactivex.android.schedulers.AndroidSchedulers.mainThread
import kotlinx.coroutines.launch
import javax.inject.Inject

@OpenForMocking
class HelpScreen(private val goToBreedsStep: () -> Unit) : LegacyStep<HelpView>() {

@Inject lateinit var api: DogApi
private val rxUnsubscriber by attachFieldToLifecycle(RxDisposer())

override fun onCreate(context: Context) {
(context.applicationContext as AppComponentContainer).injector().inject(this)
Expand All @@ -27,13 +24,10 @@ class HelpScreen(private val goToBreedsStep: () -> Unit) : LegacyStep<HelpView>(
}

override fun onShow(context: Context) {
rxUnsubscriber.autoDispose(
api.getRandomImageForBreed("husky")
.observeOn(mainThread())
.subscribe {
view!!.setDogPic(it.message)
}
)
shownScope.launch {
val imageResponse = api.getRandomImageForBreed("husky")
view!!.setDogPic(imageResponse.message)
}
}

fun showHelpDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.TestAppComponent
import com.wealthfront.magellan.sample.migration.api.DogApi
import com.wealthfront.magellan.sample.migration.api.DogMessage
import io.reactivex.Observable
import com.wealthfront.magellan.sample.migration.coWhen
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.quality.Strictness
Expand Down Expand Up @@ -54,7 +53,7 @@ class DogDetailsScreenTest {
}
}

`when`(api.getRandomImageForBreed("robotic")).thenReturn(Observable.just(breedData))
coWhen { api.getRandomImageForBreed("robotic") }.thenReturn(breedData)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.TestAppComponent
import com.wealthfront.magellan.sample.migration.api.DogApi
import com.wealthfront.magellan.sample.migration.api.DogMessage
import io.reactivex.Observable
import com.wealthfront.magellan.sample.migration.coWhen
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.quality.Strictness
Expand Down Expand Up @@ -50,12 +49,10 @@ class HelpScreenTest {
}
}

`when`(api.getRandomImageForBreed("husky")).thenReturn(
Observable.just(
DogMessage(
message = "https://dailybeagle.com/latest-picture",
status = "something"
)
coWhen { api.getRandomImageForBreed("husky") }.thenReturn(
DogMessage(
message = "https://dailybeagle.com/latest-picture",
status = "something"
)
)
}
Expand Down

0 comments on commit d68e331

Please sign in to comment.