diff options
author | Kurian Vithayathil <kvithayathil@users.noreply.github.com> | 2022-11-28 09:08:32 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 09:08:32 +0000 |
commit | b1280bfb36ad5ceba7102f10a80e4460b8aa87a5 (patch) | |
tree | 0f0630f087b0df15f776fd30239486ef48c44c04 | |
parent | 234bd7a2dde0358e2451afd02416447c2572de66 (diff) | |
download | infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.tar infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.tar.gz infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.tar.bz2 infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.tar.lz infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.tar.xz infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.tar.zst infinity-for-reddit-b1280bfb36ad5ceba7102f10a80e4460b8aa87a5.zip |
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>
4 files changed, 308 insertions, 253 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index e2dfa1d4..6afa21b9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -1,7 +1,10 @@ package ml.docilealligator.infinityforreddit; +import android.app.Application; + import javax.inject.Singleton; +import dagger.BindsInstance; import dagger.Component; import ml.docilealligator.infinityforreddit.activities.AccountPostsActivity; import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity; @@ -104,7 +107,7 @@ import ml.docilealligator.infinityforreddit.settings.TranslationFragment; import ml.docilealligator.infinityforreddit.settings.VideoPreferenceFragment; @Singleton -@Component(modules = AppModule.class) +@Component(modules = {AppModule.class, NetworkModule.class}) public interface AppComponent { void inject(MainActivity mainActivity); @@ -309,4 +312,10 @@ public interface AppComponent { void inject(HistoryActivity historyActivity); void inject(MorePostsInfoFragment morePostsInfoFragment); + + @Component.Factory + interface Factory { + + AppComponent create(@BindsInstance Application application); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index d4399607..88f83d6c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -4,20 +4,19 @@ import android.app.Application; import android.content.Context; import android.content.SharedPreferences; -import androidx.preference.PreferenceManager; - -import com.google.android.exoplayer2.database.ExoDatabaseProvider; +import com.google.android.exoplayer2.database.StandaloneDatabaseProvider; import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; import com.google.android.exoplayer2.upstream.cache.SimpleCache; import java.io.File; import java.util.concurrent.Executor; import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import javax.inject.Named; import javax.inject.Singleton; +import androidx.preference.PreferenceManager; +import dagger.Binds; import dagger.Module; import dagger.Provides; 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.MediaSourceBuilder; 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 -class AppModule { - Application mApplication; - - public AppModule(Application application) { - mApplication = application; - } - - @Provides - @Named("oauth") - @Singleton - Retrofit provideOauthRetrofit(@Named("default") OkHttpClient okHttpClient) { - return new Retrofit.Builder() - .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(); - } +abstract class AppModule { - @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(); - } + @Binds + abstract Context providesContext(Application application); @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 - @Named("default") - @Singleton - OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, - @Named("current_account") SharedPreferences currentAccountSharedPreferences) { - 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); + static RedditDataRoomDatabase provideRedditDataRoomDatabase(Application application) { + return RedditDataRoomDatabase.getDatabase(application); } @Provides @Named("default") @Singleton - SharedPreferences provideSharedPreferences() { - return PreferenceManager.getDefaultSharedPreferences(mApplication); + static SharedPreferences provideSharedPreferences(Application application) { + return PreferenceManager.getDefaultSharedPreferences(application); } @Provides @Named("light_theme") @Singleton - SharedPreferences provideLightThemeSharedPreferences() { - return mApplication.getSharedPreferences(CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideLightThemeSharedPreferences(Application application) { + return application.getSharedPreferences(CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("dark_theme") @Singleton - SharedPreferences provideDarkThemeSharedPreferences() { - return mApplication.getSharedPreferences(CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideDarkThemeSharedPreferences(Application application) { + return application.getSharedPreferences(CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("amoled_theme") @Singleton - SharedPreferences provideAmoledThemeSharedPreferences() { - return mApplication.getSharedPreferences(CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideAmoledThemeSharedPreferences(Application application) { + return application.getSharedPreferences(CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("sort_type") - SharedPreferences provideSortTypeSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideSortTypeSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("post_layout") - SharedPreferences providePostLayoutSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences providePostLayoutSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("post_feed_scrolled_position_cache") - SharedPreferences providePostFeedScrolledPositionSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences providePostFeedScrolledPositionSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("main_activity_tabs") - SharedPreferences provideMainActivityTabsSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideMainActivityTabsSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("nsfw_and_spoiler") - SharedPreferences provideNsfwAndSpoilerSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideNsfwAndSpoilerSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("bottom_app_bar") - SharedPreferences provideBottomAppBarSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideBottoappBarSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("post_history") - SharedPreferences providePostHistorySharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences providePostHistorySharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("current_account") - SharedPreferences provideCurrentAccountSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideCurrentAccountSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("navigation_drawer") - SharedPreferences provideNavigationDrawerSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.NAVIGATION_DRAWER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideNavigationDrawerSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.NAVIGATION_DRAWER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("post_details") - SharedPreferences providePostDetailsSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_DETAILS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences providePostDetailsSharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.POST_DETAILS_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Named("security") @Singleton - SharedPreferences provideSecuritySharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + static SharedPreferences provideSecuritySharedPreferences(Application application) { + return application.getSharedPreferences(SharedPreferencesUtils.SECURITY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides @Singleton - CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, + static CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, @Named("dark_theme") SharedPreferences darkThemeSharedPreferences, @Named("amoled_theme") SharedPreferences amoledThemeSharedPreferences) { return new CustomThemeWrapper(lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences); } @Provides - @Singleton - SimpleCache provideSimpleCache() { - return new SimpleCache(new File(mApplication.getCacheDir(), "/exoplayer"), - new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024), new ExoDatabaseProvider(mApplication)); + @Named("app_cache_dir") + static File providesAppCache(Application application) { + return application.getCacheDir(); + } + @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 @Singleton - ExoCreator provideExoCreator(SimpleCache simpleCache, @Named("default") SharedPreferences sharedPreferences) { - Config config = new Config.Builder(mApplication).setMediaSourceBuilder(MediaSourceBuilder.DEFAULT).setCache(simpleCache) + static SimpleCache provideSimpleCache(StandaloneDatabaseProvider standaloneDatabaseProvider, + @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(); - return new LoopAvailableExoCreator(ToroExo.with(mApplication), config, sharedPreferences); + } + + @Provides + static ToroExo providesToroExo(Application application) { + return ToroExo.with(application); + } + + @Provides + @Singleton + static ExoCreator provideExoCreator(Config config, + ToroExo toroExo, + @Named("default") SharedPreferences sharedPreferences) { + return new LoopAvailableExoCreator(toroExo, config, sharedPreferences); } @Provides @Singleton - Executor provideExecutor() { + static Executor provideExecutor() { return Executors.newFixedThreadPool(4); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java index 237f3fc7..92cca084 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java @@ -11,13 +11,6 @@ import android.os.Bundle; import android.view.WindowManager; 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.livefront.bridge.Bridge; import com.livefront.bridge.SavedStateHandler; @@ -28,6 +21,12 @@ import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; 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.broadcastreceivers.NetworkWifiStatusReceiver; import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver; @@ -61,9 +60,8 @@ public class Infinity extends Application implements LifecycleObserver { public void onCreate() { super.onCreate(); - mAppComponent = DaggerAppComponent.builder() - .appModule(new AppModule(this)) - .build(); + mAppComponent = DaggerAppComponent.factory() + .create(this); mAppComponent.inject(this); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java new file mode 100644 index 00000000..62bf7471 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java @@ -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(); + } +} |