Dagger networking refactor (#1125)

* Separate network dependency injection module

- Moved network-related dependencies into a separate module
- Consolidated common dependencies to save resources constructing a http client/retrofit

* Separate construction of access token interceptor

* Create providers for Context and Application to be injectable

* Refactor AppModule and AppComponent

- Use component builder to store application context and provide to modules
- Optimise AppModule providers

* Use component factory to add component dependencies

* Updated network dependencies to singleton.

Add missing OAuth base url for oauth request

Co-authored-by: Kurian Vithayathil <no.reply@github.com>
Co-authored-by: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>
This commit is contained in:
Kurian Vithayathil 2022-11-28 04:08:32 -05:00 committed by GitHub
parent 234bd7a2dd
commit b1280bfb36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 308 additions and 253 deletions

View File

@ -1,7 +1,10 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Application;
import javax.inject.Singleton; import javax.inject.Singleton;
import dagger.BindsInstance;
import dagger.Component; import dagger.Component;
import ml.docilealligator.infinityforreddit.activities.AccountPostsActivity; import ml.docilealligator.infinityforreddit.activities.AccountPostsActivity;
import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity; import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity;
@ -104,7 +107,7 @@ import ml.docilealligator.infinityforreddit.settings.TranslationFragment;
import ml.docilealligator.infinityforreddit.settings.VideoPreferenceFragment; import ml.docilealligator.infinityforreddit.settings.VideoPreferenceFragment;
@Singleton @Singleton
@Component(modules = AppModule.class) @Component(modules = {AppModule.class, NetworkModule.class})
public interface AppComponent { public interface AppComponent {
void inject(MainActivity mainActivity); void inject(MainActivity mainActivity);
@ -309,4 +312,10 @@ public interface AppComponent {
void inject(HistoryActivity historyActivity); void inject(HistoryActivity historyActivity);
void inject(MorePostsInfoFragment morePostsInfoFragment); void inject(MorePostsInfoFragment morePostsInfoFragment);
@Component.Factory
interface Factory {
AppComponent create(@BindsInstance Application application);
}
} }

View File

@ -4,20 +4,19 @@ import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import androidx.preference.PreferenceManager; import com.google.android.exoplayer2.database.StandaloneDatabaseProvider;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import java.io.File; import java.io.File;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import androidx.preference.PreferenceManager;
import dagger.Binds;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
@ -30,334 +29,171 @@ import ml.docilealligator.infinityforreddit.videoautoplay.Config;
import ml.docilealligator.infinityforreddit.videoautoplay.ExoCreator; import ml.docilealligator.infinityforreddit.videoautoplay.ExoCreator;
import ml.docilealligator.infinityforreddit.videoautoplay.MediaSourceBuilder; import ml.docilealligator.infinityforreddit.videoautoplay.MediaSourceBuilder;
import ml.docilealligator.infinityforreddit.videoautoplay.ToroExo; import ml.docilealligator.infinityforreddit.videoautoplay.ToroExo;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.guava.GuavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
@Module @Module
class AppModule { abstract class AppModule {
Application mApplication;
public AppModule(Application application) { @Binds
mApplication = application; abstract Context providesContext(Application application);
}
@Provides @Provides
@Named("oauth")
@Singleton @Singleton
Retrofit provideOauthRetrofit(@Named("default") OkHttpClient okHttpClient) { static RedditDataRoomDatabase provideRedditDataRoomDatabase(Application application) {
return new Retrofit.Builder() return RedditDataRoomDatabase.getDatabase(application);
.baseUrl(APIUtils.OAUTH_API_BASE_URI)
.client(okHttpClient)
.addConverterFactory(SortTypeConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(GuavaCallAdapterFactory.create())
.build();
}
@Provides
@Named("oauth_without_authenticator")
@Singleton
Retrofit provideOauthWithoutAuthenticatorRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI)
.addConverterFactory(SortTypeConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("no_oauth")
@Singleton
Retrofit provideRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.API_BASE_URI)
.addConverterFactory(SortTypeConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(GuavaCallAdapterFactory.create())
.build();
}
@Provides
@Named("upload_media")
@Singleton
Retrofit provideUploadMediaRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.API_UPLOAD_MEDIA_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("upload_video")
@Singleton
Retrofit provideUploadVideoRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.API_UPLOAD_VIDEO_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("download_media")
@Singleton
Retrofit provideDownloadRedditVideoRetrofit() {
return new Retrofit.Builder()
.baseUrl("http://localhost/")
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("gfycat")
@Singleton
Retrofit provideGfycatRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.GFYCAT_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("redgifs")
@Singleton
Retrofit provideRedgifsRetrofit(@Named("current_account") SharedPreferences currentAccountSharedPreferences) {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder
.addInterceptor(chain -> chain.proceed(
chain.request()
.newBuilder()
.header("User-Agent", APIUtils.USER_AGENT)
.build()
))
.addInterceptor(new RedgifsAccessTokenAuthenticator(currentAccountSharedPreferences))
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS));
return new Retrofit.Builder()
.baseUrl(APIUtils.REDGIFS_API_BASE_URI)
.client(okHttpClientBuilder.build())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("imgur")
@Singleton
Retrofit provideImgurRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.IMGUR_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("pushshift")
@Singleton
Retrofit providePushshiftRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.PUSHSHIFT_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("reveddit")
@Singleton
Retrofit provideRevedditRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.REVEDDIT_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("vReddIt")
@Singleton
Retrofit provideVReddItRetrofit() {
return new Retrofit.Builder()
.baseUrl("http://localhost/")
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("strapi")
@Singleton
Retrofit providestrapiRetrofit(@Named("default") OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(APIUtils.STRAPI_BASE_URI)
.client(okHttpClient)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides
@Named("streamable")
@Singleton
Retrofit provideStreamableRetrofit(@Named("default") OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(APIUtils.STREAMABLE_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
} }
@Provides @Provides
@Named("default") @Named("default")
@Singleton @Singleton
OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, static SharedPreferences provideSharedPreferences(Application application) {
@Named("current_account") SharedPreferences currentAccountSharedPreferences) { return PreferenceManager.getDefaultSharedPreferences(application);
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences))
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS));
return okHttpClientBuilder.build();
}
@Provides
@Named("rpan")
@Singleton
OkHttpClient provideRPANOkHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
}
@Provides
@Singleton
RedditDataRoomDatabase provideRedditDataRoomDatabase() {
return RedditDataRoomDatabase.create(mApplication);
}
@Provides
@Named("default")
@Singleton
SharedPreferences provideSharedPreferences() {
return PreferenceManager.getDefaultSharedPreferences(mApplication);
} }
@Provides @Provides
@Named("light_theme") @Named("light_theme")
@Singleton @Singleton
SharedPreferences provideLightThemeSharedPreferences() { static SharedPreferences provideLightThemeSharedPreferences(Application application) {
return mApplication.getSharedPreferences(CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("dark_theme") @Named("dark_theme")
@Singleton @Singleton
SharedPreferences provideDarkThemeSharedPreferences() { static SharedPreferences provideDarkThemeSharedPreferences(Application application) {
return mApplication.getSharedPreferences(CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("amoled_theme") @Named("amoled_theme")
@Singleton @Singleton
SharedPreferences provideAmoledThemeSharedPreferences() { static SharedPreferences provideAmoledThemeSharedPreferences(Application application) {
return mApplication.getSharedPreferences(CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("sort_type") @Named("sort_type")
SharedPreferences provideSortTypeSharedPreferences() { static SharedPreferences provideSortTypeSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("post_layout") @Named("post_layout")
SharedPreferences providePostLayoutSharedPreferences() { static SharedPreferences providePostLayoutSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("post_feed_scrolled_position_cache") @Named("post_feed_scrolled_position_cache")
SharedPreferences providePostFeedScrolledPositionSharedPreferences() { static SharedPreferences providePostFeedScrolledPositionSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("main_activity_tabs") @Named("main_activity_tabs")
SharedPreferences provideMainActivityTabsSharedPreferences() { static SharedPreferences provideMainActivityTabsSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("nsfw_and_spoiler") @Named("nsfw_and_spoiler")
SharedPreferences provideNsfwAndSpoilerSharedPreferences() { static SharedPreferences provideNsfwAndSpoilerSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("bottom_app_bar") @Named("bottom_app_bar")
SharedPreferences provideBottomAppBarSharedPreferences() { static SharedPreferences provideBottoappBarSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("post_history") @Named("post_history")
SharedPreferences providePostHistorySharedPreferences() { static SharedPreferences providePostHistorySharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("current_account") @Named("current_account")
SharedPreferences provideCurrentAccountSharedPreferences() { static SharedPreferences provideCurrentAccountSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("navigation_drawer") @Named("navigation_drawer")
SharedPreferences provideNavigationDrawerSharedPreferences() { static SharedPreferences provideNavigationDrawerSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.NAVIGATION_DRAWER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.NAVIGATION_DRAWER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("post_details") @Named("post_details")
SharedPreferences providePostDetailsSharedPreferences() { static SharedPreferences providePostDetailsSharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_DETAILS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.POST_DETAILS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Named("security") @Named("security")
@Singleton @Singleton
SharedPreferences provideSecuritySharedPreferences() { static SharedPreferences provideSecuritySharedPreferences(Application application) {
return mApplication.getSharedPreferences(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return application.getSharedPreferences(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides @Provides
@Singleton @Singleton
CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, static CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences,
@Named("dark_theme") SharedPreferences darkThemeSharedPreferences, @Named("dark_theme") SharedPreferences darkThemeSharedPreferences,
@Named("amoled_theme") SharedPreferences amoledThemeSharedPreferences) { @Named("amoled_theme") SharedPreferences amoledThemeSharedPreferences) {
return new CustomThemeWrapper(lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences); return new CustomThemeWrapper(lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences);
} }
@Provides @Provides
@Singleton @Named("app_cache_dir")
SimpleCache provideSimpleCache() { static File providesAppCache(Application application) {
return new SimpleCache(new File(mApplication.getCacheDir(), "/exoplayer"), return application.getCacheDir();
new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024), new ExoDatabaseProvider(mApplication)); }
@Provides
@Named("exo_player_cache")
static File providesExoPlayerCache(@Named("app_cache_dir") File appCache) {
return new File(appCache, "/exoplayer");
}
@Provides
static StandaloneDatabaseProvider provideExoDatabaseProvider(Application application) {
return new StandaloneDatabaseProvider(application);
} }
@Provides @Provides
@Singleton @Singleton
ExoCreator provideExoCreator(SimpleCache simpleCache, @Named("default") SharedPreferences sharedPreferences) { static SimpleCache provideSimpleCache(StandaloneDatabaseProvider standaloneDatabaseProvider,
Config config = new Config.Builder(mApplication).setMediaSourceBuilder(MediaSourceBuilder.DEFAULT).setCache(simpleCache) @Named("exo_player_cache") File exoPlayerCache) {
return new SimpleCache(exoPlayerCache,
new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024),
standaloneDatabaseProvider);
}
@Provides
static Config providesMediaConfig(Application application, SimpleCache simpleCache) {
return new Config.Builder(application)
.setMediaSourceBuilder(MediaSourceBuilder.DEFAULT)
.setCache(simpleCache)
.build(); .build();
return new LoopAvailableExoCreator(ToroExo.with(mApplication), config, sharedPreferences); }
@Provides
static ToroExo providesToroExo(Application application) {
return ToroExo.with(application);
} }
@Provides @Provides
@Singleton @Singleton
Executor provideExecutor() { static ExoCreator provideExoCreator(Config config,
ToroExo toroExo,
@Named("default") SharedPreferences sharedPreferences) {
return new LoopAvailableExoCreator(toroExo, config, sharedPreferences);
}
@Provides
@Singleton
static Executor provideExecutor() {
return Executors.newFixedThreadPool(4); return Executors.newFixedThreadPool(4);
} }
} }

View File

@ -11,13 +11,6 @@ import android.os.Bundle;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;
import com.evernote.android.state.StateSaver; import com.evernote.android.state.StateSaver;
import com.livefront.bridge.Bridge; import com.livefront.bridge.Bridge;
import com.livefront.bridge.SavedStateHandler; import com.livefront.bridge.SavedStateHandler;
@ -28,6 +21,12 @@ import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;
import ml.docilealligator.infinityforreddit.activities.LockScreenActivity; import ml.docilealligator.infinityforreddit.activities.LockScreenActivity;
import ml.docilealligator.infinityforreddit.broadcastreceivers.NetworkWifiStatusReceiver; import ml.docilealligator.infinityforreddit.broadcastreceivers.NetworkWifiStatusReceiver;
import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver; import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver;
@ -61,9 +60,8 @@ public class Infinity extends Application implements LifecycleObserver {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
mAppComponent = DaggerAppComponent.builder() mAppComponent = DaggerAppComponent.factory()
.appModule(new AppModule(this)) .create(this);
.build();
mAppComponent.inject(this); mAppComponent.inject(this);

View File

@ -0,0 +1,212 @@
package ml.docilealligator.infinityforreddit;
import android.content.SharedPreferences;
import java.util.concurrent.TimeUnit;
import javax.inject.Named;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.guava.GuavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
@Module(includes = AppModule.class)
abstract class NetworkModule {
@Provides
@Singleton
static OkHttpClient providesBaseOkhttp() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
}
@Provides
@Singleton
static Retrofit providesBaseRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(APIUtils.API_BASE_URI)
.client(okHttpClient)
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(GuavaCallAdapterFactory.create())
.build();
}
@Provides
static ConnectionPool providesConnectionPool() {
return new ConnectionPool(0, 1, TimeUnit.NANOSECONDS);
}
@Provides
@Named("no_oauth")
static Retrofit provideRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI)
.build();
}
@Provides
@Named("oauth")
static Retrofit providesOAuthetrofit(Retrofit retrofit) {
return retrofit;
}
@Provides
@Named("default")
@Singleton
static OkHttpClient provideOkHttpClient(OkHttpClient httpClient,
Retrofit retrofit,
RedditDataRoomDatabase accountRoomDatabase,
@Named("current_account") SharedPreferences currentAccountSharedPreferences,
ConnectionPool connectionPool) {
return httpClient.newBuilder()
.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences))
.connectionPool(connectionPool)
.build();
}
@Provides
@Named("rpan")
static OkHttpClient provideRPANOkHttpClient(OkHttpClient httpClient) {
return httpClient;
}
@Provides
@Named("oauth_without_authenticator")
@Singleton
static Retrofit provideOauthWithoutAuthenticatorRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI)
.build();
}
@Provides
@Named("upload_media")
@Singleton
static Retrofit provideUploadMediaRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.API_UPLOAD_MEDIA_URI)
.build();
}
@Provides
@Named("upload_video")
@Singleton
static Retrofit provideUploadVideoRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.API_UPLOAD_VIDEO_URI)
.build();
}
@Provides
@Named("download_media")
@Singleton
static Retrofit provideDownloadRedditVideoRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl("http://localhost/")
.build();
}
@Provides
@Named("gfycat")
@Singleton
static Retrofit provideGfycatRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.GFYCAT_API_BASE_URI)
.build();
}
@Provides
@Named("RedgifsAccessTokenAuthenticator")
static Interceptor redgifsAccessTokenAuthenticator(@Named("current_account") SharedPreferences currentAccountSharedPreferences) {
return new RedgifsAccessTokenAuthenticator(currentAccountSharedPreferences);
}
@Provides
@Named("redgifs")
@Singleton
static Retrofit provideRedgifsRetrofit(@Named("RedgifsAccessTokenAuthenticator") Interceptor accessTokenAuthenticator,
OkHttpClient httpClient,
Retrofit retrofit,
ConnectionPool connectionPool) {
OkHttpClient.Builder okHttpClientBuilder = httpClient.newBuilder()
.addInterceptor(chain -> chain.proceed(
chain.request()
.newBuilder()
.header("User-Agent", APIUtils.USER_AGENT)
.build()
))
.addInterceptor(accessTokenAuthenticator)
.connectionPool(connectionPool);
return retrofit.newBuilder()
.baseUrl(APIUtils.REDGIFS_API_BASE_URI)
.client(okHttpClientBuilder.build())
.build();
}
@Provides
@Named("imgur")
@Singleton
static Retrofit provideImgurRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.IMGUR_API_BASE_URI)
.build();
}
@Provides
@Named("pushshift")
@Singleton
static Retrofit providePushshiftRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.PUSHSHIFT_API_BASE_URI)
.build();
}
@Provides
@Named("reveddit")
@Singleton
static Retrofit provideRevedditRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.REVEDDIT_API_BASE_URI)
.build();
}
@Provides
@Named("vReddIt")
@Singleton
static Retrofit provideVReddItRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl("http://localhost/")
.build();
}
@Provides
@Named("strapi")
@Singleton
static Retrofit providestrapiRetrofit(@Named("default") OkHttpClient okHttpClient,
Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.STRAPI_BASE_URI)
.client(okHttpClient)
.build();
}
@Provides
@Named("streamable")
@Singleton
static Retrofit provideStreamableRetrofit(Retrofit retrofit) {
return retrofit.newBuilder()
.baseUrl(APIUtils.STREAMABLE_API_BASE_URI)
.build();
}
}