@@ -43,6 +43,7 @@ import com.google.firebase.analytics.analytics
4343import com.google.firebase.crashlytics.FirebaseCrashlytics
4444import io.ktor.client.HttpClient
4545import io.ktor.client.engine.cio.CIO
46+ import io.ktor.client.plugins.HttpRequestRetry
4647import io.ktor.client.plugins.HttpTimeout
4748import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
4849import io.ktor.client.plugins.logging.Logger
@@ -65,29 +66,29 @@ import javax.net.ssl.X509TrustManager
6566class ScheduleApp : Application () {
6667 override fun onCreate () {
6768 super .onCreate()
68-
69+
6970 if (FirebaseApp .getApps(this ).isEmpty()) {
7071 FirebaseApp .initializeApp(this )
7172 }
72-
73+
7374 val crashlytics = FirebaseCrashlytics .getInstance()
7475 crashlytics.setCustomKey(" app_version" , BuildConfig .VERSION_NAME )
7576 crashlytics.setCustomKey(" app_version_code" , BuildConfig .VERSION_CODE )
76-
77+
7778 AppLogger .initialize(applicationContext)
7879 CrashHandler .initialize(applicationContext, CrashActivity ::class .java)
7980 .setSideEffect(crashlytics::recordException)
80-
81+
8182 val initTag = buildTag(LogScope .CORE , LogCat .INIT )
8283 Auditor .info(initTag, " Приложение запущено" )
83-
84+
8485 startKoin {
8586 androidContext(this @ScheduleApp)
8687 modules(generalModule, controllers)
8788 }
88-
89+
8990 val koin = getKoin()
90-
91+
9192 val appValues = koin.get<AppValues >()
9293 if (appValues.userId.get() == null ) {
9394 val userId = UUID .randomUUID().toString()
@@ -98,7 +99,7 @@ class ScheduleApp : Application() {
9899 } else {
99100 Auditor .debug(initTag, " Пользователь уже существует: ${appValues.userId.get()} " )
100101 }
101-
102+
102103 SingletonImageLoader .setSafe {
103104 ImageLoader .Builder (this )
104105 .crossfade(true )
@@ -107,9 +108,9 @@ class ScheduleApp : Application() {
107108 }
108109 .build()
109110 }
110-
111+
111112 val source = getInstallSource(this )
112-
113+
113114 when (source) {
114115 InstallSource .APK -> Auditor .debug(" d" , " install source Apk" )
115116 InstallSource .RuStore -> Auditor .debug(" d" , " install source RuStore" )
@@ -129,24 +130,24 @@ val controllers = module {
129130
130131val generalModule = module {
131132 single<CoroutineScope > { CoroutineScope (SupervisorJob () + Dispatchers .Main .immediate) }
132-
133+
133134 singleOf(::AppValues ) bind PreferenceStorage ::class
134135 singleOf(::AppUtils )
135136 singleOf(::GoogleDriveParser )
136137 singleOf(::FileManager )
137-
138+
138139 singleOf(::DGTUAccountService )
139-
140+
140141 singleOf(::InstitutionManager )
141142 singleOf(::ScheduleRepository )
142143 singleOf(::NewsRepository )
143-
144+
144145 single<AppUpdateManager > {
145146 val context = androidContext()
146147 val source = getInstallSource(context)
147-
148+
148149 when (source) {
149-
150+
150151 InstallSource .RuStore -> RuStoreUpdateManager (context, get())
151152 InstallSource .APK -> GitHubUpdateManager (
152153 GitHubUpdateService (get()),
@@ -160,7 +161,7 @@ val generalModule = module {
160161 )
161162 }
162163 }
163-
164+
164165 single {
165166 Room .databaseBuilder(
166167 androidContext(),
@@ -170,19 +171,36 @@ val generalModule = module {
170171 .fallbackToDestructiveMigration(true )
171172 .build()
172173 }
173-
174+
174175 single {
175176 HttpClient (CIO ) {
177+ install(HttpRequestRetry ) {
178+ maxRetries = 4
179+
180+ retryOnExceptionIf { _, cause ->
181+ cause is java.net.UnknownHostException ||
182+ cause is java.net.ConnectException ||
183+ cause is java.net.SocketTimeoutException
184+ }
185+
186+ retryOnServerErrors(maxRetries = 3 )
187+
188+ exponentialDelay(
189+ baseDelayMs = 300L ,
190+ maxDelayMs = 5000L
191+ )
192+ }
193+
176194 install(NetworkMonitorPlugin )
177-
195+
178196 install(Logging ) {
179197 logger = object : Logger {
180198 override fun log (message : String ) {
181199 Auditor .debug(buildTag(LogScope .NETWORK , LogCat .NET ), message)
182200 }
183201 }
184202 }
185-
203+
186204 install(ContentNegotiation ) {
187205 json(Json {
188206 classDiscriminator = " type"
@@ -192,12 +210,12 @@ val generalModule = module {
192210 explicitNulls = false
193211 })
194212 }
195-
213+
196214 install(HttpTimeout ) {
197215 this @HttpClient.expectSuccess = false
198216 requestTimeoutMillis = 60 * 1000
199217 }
200-
218+
201219 engine {
202220 https {
203221 trustManager = object : X509TrustManager {
@@ -206,13 +224,13 @@ val generalModule = module {
206224 authType : String
207225 ) {
208226 }
209-
227+
210228 override fun checkServerTrusted (
211229 chain : Array <X509Certificate >,
212230 authType : String
213231 ) {
214232 }
215-
233+
216234 override fun getAcceptedIssuers (): Array <X509Certificate > = arrayOf()
217235 }
218236 }
0 commit comments