This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
SharedModule.kt
194 lines (175 loc) · 6.97 KB
/
SharedModule.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.iosched.shared.di
import android.content.Context
import androidx.work.Configuration
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
import com.google.samples.apps.iosched.shared.BuildConfig
import com.google.samples.apps.iosched.shared.R
import com.google.samples.apps.iosched.shared.data.BootstrapConferenceDataSource
import com.google.samples.apps.iosched.shared.data.ConferenceDataRepository
import com.google.samples.apps.iosched.shared.data.ConferenceDataSource
import com.google.samples.apps.iosched.shared.data.NetworkConferenceDataSource
import com.google.samples.apps.iosched.shared.data.config.AppConfigDataSource
import com.google.samples.apps.iosched.shared.data.config.RemoteAppConfigDataSource
import com.google.samples.apps.iosched.shared.data.db.AppDatabase
import com.google.samples.apps.iosched.shared.data.logistics.LogisticsDataSource
import com.google.samples.apps.iosched.shared.data.logistics.LogisticsRepository
import com.google.samples.apps.iosched.shared.data.logistics.RemoteConfigLogisticsDataSource
import com.google.samples.apps.iosched.shared.data.session.DefaultSessionRepository
import com.google.samples.apps.iosched.shared.data.session.SessionRepository
import com.google.samples.apps.iosched.shared.data.userevent.DefaultSessionAndUserEventRepository
import com.google.samples.apps.iosched.shared.data.userevent.FirestoreUserEventDataSource
import com.google.samples.apps.iosched.shared.data.userevent.SessionAndUserEventRepository
import com.google.samples.apps.iosched.shared.data.userevent.UserEventDataSource
import com.google.samples.apps.iosched.shared.data.work.IoschedWorkerFactory
import com.google.samples.apps.iosched.shared.fcm.FcmTopicSubscriber
import com.google.samples.apps.iosched.shared.fcm.TopicSubscriber
import com.google.samples.apps.iosched.shared.time.DefaultTimeProvider
import com.google.samples.apps.iosched.shared.time.TimeProvider
import com.google.samples.apps.iosched.shared.util.NetworkUtils
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Named
import javax.inject.Singleton
/**
* Module where classes created in the shared module are created.
*/
@Module
class SharedModule {
// Define the data source implementations that should be used. All data sources are singletons.
@Singleton
@Provides
@Named("remoteConfDatasource")
fun provideConferenceDataSource(
context: Context,
networkUtils: NetworkUtils
): ConferenceDataSource {
return NetworkConferenceDataSource(context, networkUtils)
}
@Singleton
@Provides
@Named("bootstrapConfDataSource")
fun provideBootstrapRemoteSessionDataSource(): ConferenceDataSource {
return BootstrapConferenceDataSource
}
@Singleton
@Provides
fun provideConferenceDataRepository(
@Named("remoteConfDatasource") remoteDataSource: ConferenceDataSource,
@Named("bootstrapConfDataSource") boostrapDataSource: ConferenceDataSource,
appDatabase: AppDatabase
): ConferenceDataRepository {
return ConferenceDataRepository(remoteDataSource, boostrapDataSource, appDatabase)
}
@Singleton
@Provides
fun provideSessionRepository(
conferenceDataRepository: ConferenceDataRepository
): SessionRepository {
return DefaultSessionRepository(conferenceDataRepository)
}
@Singleton
@Provides
fun provideUserEventDataSource(firestore: FirebaseFirestore): UserEventDataSource {
return FirestoreUserEventDataSource(firestore)
}
@Singleton
@Provides
fun provideSessionAndUserEventRepository(
userEventDataSource: UserEventDataSource,
sessionRepository: SessionRepository
): SessionAndUserEventRepository {
return DefaultSessionAndUserEventRepository(
userEventDataSource,
sessionRepository
)
}
@Singleton
@Provides
fun provideFirebaseFireStore(): FirebaseFirestore {
val firestore = FirebaseFirestore.getInstance()
firestore.firestoreSettings = FirebaseFirestoreSettings.Builder()
// This is to enable the offline data
// https://firebase.google.com/docs/firestore/manage-data/enable-offline
.setPersistenceEnabled(true)
.setTimestampsInSnapshotsEnabled(true)
.build()
return firestore
}
@Singleton
@Provides
fun provideTopicSubscriber(): TopicSubscriber {
return FcmTopicSubscriber()
}
@Singleton
@Provides
fun provideFirebaseRemoteConfigSettings(): FirebaseRemoteConfigSettings {
return FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(BuildConfig.DEBUG)
.build()
}
@Singleton
@Provides
fun provideFirebaseRemoteConfig(
configSettings: FirebaseRemoteConfigSettings
): FirebaseRemoteConfig {
val remoteConfig = FirebaseRemoteConfig.getInstance()
remoteConfig.setConfigSettings(configSettings)
remoteConfig.setDefaults(R.xml.remote_config_defaults)
return remoteConfig
}
@Singleton
@Provides
fun provideLogisticsDataSource(remoteConfig: FirebaseRemoteConfig): LogisticsDataSource {
return RemoteConfigLogisticsDataSource(remoteConfig)
}
@Singleton
@Provides
fun provideLogisticsRepository(
logisticsDataSource: LogisticsDataSource
): LogisticsRepository {
return LogisticsRepository(logisticsDataSource)
}
@Singleton
@Provides
fun provideAppConfigDataSource(
remoteConfig: FirebaseRemoteConfig,
configSettings: FirebaseRemoteConfigSettings,
@IoDispatcher ioDispatcher: CoroutineDispatcher
): AppConfigDataSource {
return RemoteAppConfigDataSource(remoteConfig, configSettings, ioDispatcher)
}
@Singleton
@Provides
fun provideTimeProvider(): TimeProvider {
return DefaultTimeProvider
}
@Singleton
@Provides
fun provideWorkManagerConfiguration(
ioschedWorkerFactory: IoschedWorkerFactory
): Configuration {
return Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.INFO)
.setWorkerFactory(ioschedWorkerFactory)
.build()
}
}