Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit d892dbf

Browse files
committed
Prepares iosched for coroutines
Change-Id: Iba061c117f7050fadd5ab9e318852c4ecc8e3bbf
1 parent 90292d2 commit d892dbf

File tree

101 files changed

+1279
-323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1279
-323
lines changed

buildSrc/src/main/java/Libs.kt

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ object Libs {
2424
const val CONSTRAINT_LAYOUT = "androidx.constraintlayout:constraintlayout"
2525
const val CORE_KTX = "androidx.core:core-ktx"
2626
const val CRASHLYTICS = "com.crashlytics.sdk.android:crashlytics"
27+
const val COROUTINES = "org.jetbrains.kotlinx:kotlinx-coroutines-core"
28+
const val COROUTINES_TEST = "org.jetbrains.kotlinx:kotlinx-coroutines-test"
2729
const val DAGGER_ANDROID = "com.google.dagger:dagger-android:"
2830
const val DAGGER_ANDROID_PROCESSOR = "com.google.dagger:dagger-android-processor"
2931
const val DAGGER_ANDROID_SUPPORT = "com.google.dagger:dagger-android-support"

depconstraints/build.gradle.kts

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ val arcore = "1.7.0"
2727
val browser = "1.0.0"
2828
val constraintLayout = "1.1.3"
2929
val core = "1.2.0-alpha02"
30+
val coroutines = "1.3.3"
31+
val coroutinesTest = "1.3.0"
3032
val crashlytics = "2.9.8"
3133
val dagger = "2.24"
3234
val drawerLayout = "1.1.0-alpha02"
@@ -58,7 +60,7 @@ val okio = "1.14.0"
5860
val pageIndicator = "1.3.0"
5961
val room = "2.1.0"
6062
val rules = "1.1.1"
61-
val runner = "1.1.1"
63+
val runner = "1.2.0"
6264
val threetenabp = "1.0.5"
6365
val timber = "4.7.1"
6466
val viewpager2 = "1.0.0"
@@ -73,6 +75,8 @@ dependencies {
7375
api("${Libs.BROWSER}:$browser")
7476
api("${Libs.CONSTRAINT_LAYOUT}:$constraintLayout")
7577
api("${Libs.CORE_KTX}:$core")
78+
api("${Libs.COROUTINES}:$coroutines")
79+
api("${Libs.COROUTINES_TEST}:$coroutines")
7680
api("${Libs.CRASHLYTICS}:$crashlytics")
7781
api("${Libs.DAGGER_ANDROID}:$dagger")
7882
api("${Libs.DAGGER_ANDROID_SUPPORT}:$dagger")

gradle.properties

+3
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ map_default_camera_tilt=0
6262

6363
# Zoom level to use when camera is programmatically centered on a marker
6464
map_camera_focus_zoom=19f
65+
66+
# enable incremental annotation processing
67+
kapt.incremental.apt=true

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

mobile/build.gradle.kts

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ android {
3131
targetSdkVersion(Versions.TARGET_SDK)
3232
versionCode = Versions.versionCodeMobile
3333
versionName = Versions.versionName
34-
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
34+
testInstrumentationRunner = "com.google.samples.apps.iosched.tests.CustomTestRunner"
3535

3636
buildConfigField("com.google.android.gms.maps.model.LatLng",
3737
"MAP_VIEWPORT_BOUND_NE",
@@ -161,6 +161,7 @@ dependencies {
161161
implementation(Libs.INK_PAGE_INDICATOR)
162162

163163
// Architecture Components
164+
implementation(Libs.LIFECYCLE_LIVE_DATA_KTX)
164165
kapt(Libs.LIFECYCLE_COMPILER)
165166
testImplementation(Libs.ARCH_TESTING)
166167
implementation(Libs.NAVIGATION_FRAGMENT_KTX)
@@ -176,6 +177,8 @@ dependencies {
176177
implementation(Libs.DAGGER_ANDROID_SUPPORT)
177178
kapt(Libs.DAGGER_COMPILER)
178179
kapt(Libs.DAGGER_ANDROID_PROCESSOR)
180+
kaptAndroidTest(Libs.DAGGER_COMPILER)
181+
kaptAndroidTest(Libs.DAGGER_ANDROID_PROCESSOR)
179182

180183
// Glide
181184
implementation(Libs.GLIDE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.tests
18+
19+
import android.app.Application
20+
import android.content.Context
21+
import androidx.test.runner.AndroidJUnitRunner
22+
23+
/**
24+
* A custom [AndroidJUnitRunner] used to replace the application used in tests with a
25+
* [MainTestApplication].
26+
*/
27+
class CustomTestRunner : AndroidJUnitRunner() {
28+
29+
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
30+
return super.newApplication(cl, MainTestApplication::class.java.name, context)
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.tests
18+
19+
import com.google.samples.apps.iosched.MainApplication
20+
import com.google.samples.apps.iosched.tests.di.DaggerTestAppComponent
21+
import dagger.android.AndroidInjector
22+
import dagger.android.DaggerApplication
23+
24+
class MainTestApplication : MainApplication() {
25+
26+
/**
27+
* Tell Dagger which [AndroidInjector] to use - in our case
28+
* [com.google.samples.apps.iosched.tests.di.TestAppComponent]. `DaggerTestAppComponent`
29+
* is a class generated by Dagger based on the `TestAppComponent` class.
30+
*/
31+
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
32+
return DaggerTestAppComponent.builder().create(this)
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.tests.di
18+
19+
import com.google.samples.apps.iosched.MainApplication
20+
import com.google.samples.apps.iosched.di.ActivityBindingModule
21+
import com.google.samples.apps.iosched.di.AppModule
22+
import com.google.samples.apps.iosched.di.SignInModule
23+
import com.google.samples.apps.iosched.shared.di.BroadcastReceiverBindingModule
24+
import com.google.samples.apps.iosched.shared.di.FeatureFlagsModule
25+
import com.google.samples.apps.iosched.shared.di.ServiceBindingModule
26+
import com.google.samples.apps.iosched.shared.di.SharedModule
27+
import com.google.samples.apps.iosched.shared.di.ViewModelModule
28+
import com.google.samples.apps.iosched.ui.signin.SignInViewModelDelegateModule
29+
import com.google.samples.apps.iosched.ui.theme.ThemedActivityDelegateModule
30+
import dagger.Component
31+
import dagger.android.AndroidInjector
32+
import dagger.android.support.AndroidSupportInjectionModule
33+
import javax.inject.Singleton
34+
35+
/**
36+
* Main component of the app for instrumentation tests,
37+
* created and persisted in the MainTestApplication class.
38+
*
39+
* Whenever a new module is created, it should be added to the list of modules.
40+
* [AndroidSupportInjectionModule] is the module from Dagger.Android that helps with the
41+
* generation and location of subcomponents.
42+
*/
43+
@Singleton
44+
@Component(
45+
modules = [
46+
AndroidSupportInjectionModule::class,
47+
AppModule::class,
48+
// Test Module that overrides CoroutinesModule
49+
TestCoroutinesModule::class,
50+
ActivityBindingModule::class,
51+
BroadcastReceiverBindingModule::class,
52+
ViewModelModule::class,
53+
ServiceBindingModule::class,
54+
SharedModule::class,
55+
FeatureFlagsModule::class,
56+
SignInModule::class,
57+
SignInViewModelDelegateModule::class,
58+
ThemedActivityDelegateModule::class
59+
]
60+
)
61+
interface TestAppComponent : AndroidInjector<MainApplication> {
62+
@Component.Builder
63+
abstract class Builder : AndroidInjector.Builder<MainApplication>()
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.tests.di
18+
19+
import android.os.AsyncTask
20+
import com.google.samples.apps.iosched.shared.di.DefaultDispatcher
21+
import com.google.samples.apps.iosched.shared.di.IoDispatcher
22+
import com.google.samples.apps.iosched.shared.di.MainDispatcher
23+
import com.google.samples.apps.iosched.shared.di.MainImmediateDispatcher
24+
import dagger.Module
25+
import dagger.Provides
26+
import kotlinx.coroutines.CoroutineDispatcher
27+
import kotlinx.coroutines.Dispatchers
28+
import kotlinx.coroutines.asCoroutineDispatcher
29+
30+
@Module
31+
object TestCoroutinesModule {
32+
33+
@DefaultDispatcher
34+
@JvmStatic
35+
@Provides
36+
fun providesDefaultDispatcher(): CoroutineDispatcher =
37+
AsyncTask.THREAD_POOL_EXECUTOR.asCoroutineDispatcher()
38+
39+
@IoDispatcher
40+
@JvmStatic
41+
@Provides
42+
fun providesIoDispatcher(): CoroutineDispatcher =
43+
AsyncTask.THREAD_POOL_EXECUTOR.asCoroutineDispatcher()
44+
45+
@MainDispatcher
46+
@JvmStatic
47+
@Provides
48+
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
49+
50+
@MainImmediateDispatcher
51+
@JvmStatic
52+
@Provides
53+
fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
54+
}

mobile/src/main/java/com/google/samples/apps/iosched/MainApplication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import timber.log.Timber
3030
/**
3131
* Initialization of libraries.
3232
*/
33-
class MainApplication : DaggerApplication() {
33+
open class MainApplication : DaggerApplication() {
3434
// Even if the var isn't used, needs to be initialized at application startup.
3535
@Inject lateinit var analyticsHelper: AnalyticsHelper
3636

mobile/src/main/java/com/google/samples/apps/iosched/di/AppComponent.kt

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import javax.inject.Singleton
4242
modules = [
4343
AndroidSupportInjectionModule::class,
4444
AppModule::class,
45+
CoroutinesModule::class,
4546
ActivityBindingModule::class,
4647
BroadcastReceiverBindingModule::class,
4748
ViewModelModule::class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.di
18+
19+
import com.google.samples.apps.iosched.shared.di.DefaultDispatcher
20+
import com.google.samples.apps.iosched.shared.di.IoDispatcher
21+
import com.google.samples.apps.iosched.shared.di.MainDispatcher
22+
import com.google.samples.apps.iosched.shared.di.MainImmediateDispatcher
23+
import dagger.Module
24+
import dagger.Provides
25+
import kotlinx.coroutines.CoroutineDispatcher
26+
import kotlinx.coroutines.Dispatchers
27+
28+
@Module
29+
object CoroutinesModule {
30+
31+
@DefaultDispatcher
32+
@JvmStatic
33+
@Provides
34+
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
35+
36+
@IoDispatcher
37+
@JvmStatic
38+
@Provides
39+
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
40+
41+
@MainDispatcher
42+
@JvmStatic
43+
@Provides
44+
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
45+
46+
@MainImmediateDispatcher
47+
@JvmStatic
48+
@Provides
49+
fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
50+
}

mobile/src/main/java/com/google/samples/apps/iosched/ui/LaunchViewModel.kt

+8-18
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
package com.google.samples.apps.iosched.ui
1818

19-
import androidx.lifecycle.LiveData
20-
import androidx.lifecycle.MutableLiveData
2119
import androidx.lifecycle.ViewModel
20+
import androidx.lifecycle.liveData
2221
import com.google.samples.apps.iosched.shared.domain.prefs.OnboardingCompletedUseCase
2322
import com.google.samples.apps.iosched.shared.result.Event
24-
import com.google.samples.apps.iosched.shared.result.Result
25-
import com.google.samples.apps.iosched.shared.util.map
23+
import com.google.samples.apps.iosched.shared.result.data
2624
import javax.inject.Inject
2725

2826
/**
@@ -31,20 +29,12 @@ import javax.inject.Inject
3129
class LaunchViewModel @Inject constructor(
3230
onboardingCompletedUseCase: OnboardingCompletedUseCase
3331
) : ViewModel() {
34-
35-
private val onboardingCompletedResult = MutableLiveData<Result<Boolean>>()
36-
val launchDestination: LiveData<Event<LaunchDestination>>
37-
38-
init {
39-
// Check if onboarding has already been completed and then navigate the user accordingly
40-
onboardingCompletedUseCase(Unit, onboardingCompletedResult)
41-
launchDestination = onboardingCompletedResult.map {
42-
// If this check fails, prefer to launch main activity than show onboarding too often
43-
if ((it as? Result.Success)?.data == false) {
44-
Event(LaunchDestination.ONBOARDING)
45-
} else {
46-
Event(LaunchDestination.MAIN_ACTIVITY)
47-
}
32+
val launchDestination = liveData {
33+
val result = onboardingCompletedUseCase(Unit)
34+
if (result.data == false) {
35+
emit(Event(LaunchDestination.ONBOARDING))
36+
} else {
37+
emit(Event(LaunchDestination.MAIN_ACTIVITY))
4838
}
4939
}
5040
}

mobile/src/main/java/com/google/samples/apps/iosched/ui/SnackbarMessageFragmentExtensions.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ fun Fragment.setUpSnackbar(
4848
})
4949

5050
// Important reservations messages are handled with a message manager
51-
snackbarMessageManager.observeNextMessage().observe(viewLifecycleOwner,
51+
snackbarMessageManager.observeNextMessage().observe(
52+
viewLifecycleOwner,
5253
EventObserver { message ->
5354
val messageText = HtmlCompat.fromHtml(
5455
requireContext().getString(message.messageId, message.session?.title),
@@ -66,5 +67,6 @@ fun Fragment.setUpSnackbar(
6667
// are pending messages.
6768
dismissListener = { snackbarMessageManager.loadNextMessage() }
6869
)
69-
})
70+
}
71+
)
7072
}

0 commit comments

Comments
 (0)