This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
AppModule.kt
116 lines (103 loc) · 4.44 KB
/
AppModule.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
/*
* 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.di
import android.content.ClipboardManager
import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.samples.apps.iosched.shared.analytics.AnalyticsHelper
import com.google.samples.apps.iosched.shared.data.agenda.AgendaRepository
import com.google.samples.apps.iosched.shared.data.agenda.DefaultAgendaRepository
import com.google.samples.apps.iosched.shared.data.config.AppConfigDataSource
import com.google.samples.apps.iosched.shared.data.db.AppDatabase
import com.google.samples.apps.iosched.shared.data.prefs.PreferenceStorage
import com.google.samples.apps.iosched.shared.data.prefs.SharedPreferenceStorage
import com.google.samples.apps.iosched.shared.di.ApplicationScope
import com.google.samples.apps.iosched.shared.di.DefaultDispatcher
import com.google.samples.apps.iosched.shared.di.MainThreadHandler
import com.google.samples.apps.iosched.shared.domain.internal.IOSchedHandler
import com.google.samples.apps.iosched.shared.domain.internal.IOSchedMainHandler
import com.google.samples.apps.iosched.ui.signin.SignInViewModelDelegate
import com.google.samples.apps.iosched.util.FirebaseAnalyticsHelper
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton
/**
* Defines all the classes that need to be provided in the scope of the app.
*
* Define here all objects that are shared throughout the app, like SharedPreferences, navigators or
* others. If some of those objects are singletons, they should be annotated with `@Singleton`.
*/
@InstallIn(ApplicationComponent::class)
@Module
class AppModule {
@Singleton
@Provides
fun providePreferenceStorage(@ApplicationContext context: Context): PreferenceStorage =
SharedPreferenceStorage(context)
@Provides
fun provideWifiManager(@ApplicationContext context: Context): WifiManager =
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
@Provides
fun provideConnectivityManager(@ApplicationContext context: Context): ConnectivityManager =
context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE)
as ConnectivityManager
@Provides
fun provideClipboardManager(@ApplicationContext context: Context): ClipboardManager =
context.applicationContext.getSystemService(Context.CLIPBOARD_SERVICE)
as ClipboardManager
@ApplicationScope
@Singleton
@Provides
fun providesApplicationScope(
@DefaultDispatcher defaultDispatcher: CoroutineDispatcher
): CoroutineScope = CoroutineScope(SupervisorJob() + defaultDispatcher)
@Singleton
@Provides
@MainThreadHandler
fun provideMainThreadHandler(): IOSchedHandler = IOSchedMainHandler()
@Singleton
@Provides
fun provideAnalyticsHelper(
@ApplicationScope applicationScope: CoroutineScope,
@ApplicationContext context: Context,
signInDelegate: SignInViewModelDelegate,
preferenceStorage: PreferenceStorage
): AnalyticsHelper =
FirebaseAnalyticsHelper(applicationScope, context, signInDelegate, preferenceStorage)
@Singleton
@Provides
fun provideAgendaRepository(appConfigDataSource: AppConfigDataSource): AgendaRepository =
DefaultAgendaRepository(appConfigDataSource)
@Singleton
@Provides
fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase {
return AppDatabase.buildDatabase(context)
}
@Singleton
@Provides
fun provideGson(): Gson {
return GsonBuilder().create()
}
}