Dependency injection improvement (#596)

* inject MastodonApi into LoginActivity

* inject AccountManager into MainActivity

* inject AccountManager into SplashActivity, convert to Kotlin

* inject AccountManager into AccountActivity

* inject AccountManager into LoginActivity

* inject AccountManager into NotificationsFragment and NotificationClearBroadcastReceiver, fix MainActivity

* ooops

* use same OkHttpClient for Retrofit & Picasso

* fix ordering of okhttp interceptors

* remove dependencies on TuskyApplication

* bugfix
This commit is contained in:
Konrad Pozniak 2018-04-22 17:20:01 +02:00 committed by GitHub
commit b4ba457d89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 196 additions and 176 deletions

View file

@ -61,6 +61,13 @@ abstract class ActivitiesModule {
@ContributesAndroidInjector()
abstract fun contributesAboutActivity(): AboutActivity
@ContributesAndroidInjector()
abstract fun contributesLoginActivity(): LoginActivity
@ContributesAndroidInjector()
abstract fun contributesSplashActivity(): SplashActivity
@ContributesAndroidInjector
abstract fun contributesReportActivity(): ReportActivity
}

View file

@ -32,7 +32,8 @@ import javax.inject.Singleton
NetworkModule::class,
AndroidInjectionModule::class,
ActivitiesModule::class,
ServicesModule::class
ServicesModule::class,
BroadcastReceiverModule::class
])
interface AppComponent {
@Component.Builder

View file

@ -0,0 +1,26 @@
/* Copyright 2018 Conny Duck
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.di
import com.keylesspalace.tusky.receiver.NotificationClearBroadcastReceiver
import dagger.Module
import dagger.android.ContributesAndroidInjector
@Module
abstract class BroadcastReceiverModule {
@ContributesAndroidInjector
abstract fun contributeNotificationClearBroadcastReceiver() : NotificationClearBroadcastReceiver
}

View file

@ -16,11 +16,12 @@
package com.keylesspalace.tusky.di
import android.content.SharedPreferences
import android.content.Context
import android.text.Spanned
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializer
import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.json.SpannedTypeAdapter
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
@ -31,8 +32,8 @@ import dagger.Provides
import dagger.multibindings.ClassKey
import dagger.multibindings.IntoMap
import dagger.multibindings.IntoSet
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
@ -67,32 +68,24 @@ class NetworkModule {
fun providesConverterFactory(gson: Gson): Converter.Factory = GsonConverterFactory.create(gson)
@Provides
@IntoSet
@Singleton
fun providesAuthInterceptor(accountManager: AccountManager): Interceptor {
// should accept AccountManager here probably but I don't want to break things yet
return InstanceSwitchAuthInterceptor(accountManager)
}
@Provides
@Singleton
fun providesHttpClient(interceptors: @JvmSuppressWildcards Set<Interceptor>,
preferences: SharedPreferences): OkHttpClient {
return OkHttpUtils.getCompatibleClientBuilder(preferences)
fun providesHttpClient(accountManager: AccountManager,
context: Context): OkHttpClient {
return OkHttpUtils.getCompatibleClientBuilder(context)
.apply {
interceptors.fold(this) { b, i ->
b.addInterceptor(i)
addInterceptor(InstanceSwitchAuthInterceptor(accountManager))
if (BuildConfig.DEBUG) {
addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
}
}
.build()
}
@Provides
@Singleton
fun providesRetrofit(httpClient: OkHttpClient,
converters: @JvmSuppressWildcards Set<Converter.Factory>): Retrofit {
return Retrofit.Builder().baseUrl("https://dummy.placeholder/")
return Retrofit.Builder().baseUrl("https://"+MastodonApi.PLACEHOLDER_DOMAIN)
.client(httpClient)
.let { builder ->
// Doing it this way in case builder will be immutable so we return the final