From a21eccca0d788958824c747f102ad8508aa089f8 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Sat, 24 Dec 2022 06:17:24 +0800 Subject: Inject Streamable api provider instead of Retrofit instance (#1217) Low effort/high impact case. Simple change that removes Retrofit instance creation from startup path because it is used only after receiving response from Reddit. Eliminates Retrofit instance creation completely if it is not used. Also improves type safety by replacing common Retrofit type with specific api type. --- .../infinityforreddit/FetchStreamableVideo.java | 6 ++++-- .../infinityforreddit/NetworkModule.java | 7 +++++++ .../activities/ViewVideoActivity.java | 7 ++++--- .../adapters/HistoryPostRecyclerViewAdapter.java | 12 +++++++----- .../adapters/PostDetailRecyclerViewAdapter.java | 10 ++++++---- .../adapters/PostRecyclerViewAdapter.java | 12 +++++++----- .../fragments/HistoryPostFragment.java | 7 ++++--- .../infinityforreddit/fragments/PostFragment.java | 19 ++++++++++--------- .../fragments/ViewPostDetailFragment.java | 9 +++++---- 9 files changed, 54 insertions(+), 35 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java index 91fc164c..ef5629d4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java @@ -10,6 +10,8 @@ import org.json.JSONObject; import java.io.IOException; import java.util.concurrent.Executor; +import javax.inject.Provider; + import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import retrofit2.Call; @@ -22,11 +24,11 @@ public class FetchStreamableVideo { void failed(); } - public static void fetchStreamableVideo(Executor executor, Handler handler, Retrofit streamableRetrofit, + public static void fetchStreamableVideo(Executor executor, Handler handler, Provider streamableApiProvider, String videoUrl, FetchStreamableVideoListener fetchStreamableVideoListener) { executor.execute(() -> { try { - Response response = streamableRetrofit.create(StreamableAPI.class).getStreamableData(videoUrl).execute(); + Response response = streamableApiProvider.get().getStreamableData(videoUrl).execute(); if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(response.body()); String title = jsonObject.getString(JSONUtils.TITLE_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java index 8638ea8b..647d87c8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java @@ -9,6 +9,7 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; +import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.network.SortTypeConverterFactory; import ml.docilealligator.infinityforreddit.utils.APIUtils; import okhttp3.ConnectionPool; @@ -198,4 +199,10 @@ abstract class NetworkModule { .baseUrl(APIUtils.STREAMABLE_API_BASE_URI) .build(); } + + @Provides + @Singleton + static StreamableAPI provideStreamableApi(@Named("streamable") Retrofit streamableRetrofit) { + return streamableRetrofit.create(StreamableAPI.class); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index b9fa2655..ae8de478 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -76,6 +76,7 @@ import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import app.futured.hauler.DragDirection; import app.futured.hauler.HaulerView; @@ -88,6 +89,7 @@ import ml.docilealligator.infinityforreddit.FetchStreamableVideo; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.StreamableVideo; +import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.apis.VReddIt; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PlaybackSpeedBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -211,8 +213,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe Retrofit vReddItRetrofit; @Inject - @Named("streamable") - Retrofit streamableRetrofit; + Provider streamableApiProvider; @Inject @Named("default") @@ -835,7 +836,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe private void loadStreamableVideo(String shortCode, Bundle savedInstanceState) { progressBar.setVisibility(View.VISIBLE); - FetchStreamableVideo.fetchStreamableVideo(mExecutor, new Handler(), streamableRetrofit, shortCode, + FetchStreamableVideo.fetchStreamableVideo(mExecutor, new Handler(), streamableApiProvider, shortCode, new FetchStreamableVideo.FetchStreamableVideoListener() { @Override public void success(StreamableVideo streamableVideo) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java index 39639da2..db7abf03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java @@ -57,6 +57,8 @@ import java.util.ArrayList; import java.util.Locale; import java.util.concurrent.Executor; +import javax.inject.Provider; + import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.BlurTransformation; @@ -141,7 +143,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter mStreamableApiProvider; private String mAccessToken; private RequestManager mGlide; private int mMaxResolution; @@ -220,7 +222,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter streambleApiProvider, CustomThemeWrapper customThemeWrapper, Locale locale, String accessToken, String accountName, int postType, int postLayout, boolean displaySubredditName, SharedPreferences sharedPreferences, SharedPreferences currentAccountSharedPreferences, @@ -236,7 +238,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter mStreamableApiProvider; private RedditDataRoomDatabase mRedditDataRoomDatabase; private SharedPreferences mCurrentAccountSharedPreferences; private RequestManager mGlide; @@ -208,7 +210,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter streamableApiProvider, RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide, boolean separatePostAndComments, String accessToken, String accountName, Post post, Locale locale, @@ -225,7 +227,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter mStreamableApiProvider; private String mAccessToken; private RequestManager mGlide; private int mMaxResolution; @@ -231,7 +233,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter streamableApiProvider, CustomThemeWrapper customThemeWrapper, Locale locale, String accessToken, String accountName, int postType, int postLayout, boolean displaySubredditName, SharedPreferences sharedPreferences, SharedPreferences currentAccountSharedPreferences, @@ -248,7 +250,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter mStreamableApiProvider; @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @@ -379,7 +380,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.HISTORY_POST_LAYOUT_READ_POST, defaultPostLayout); mAdapter = new HistoryPostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mExoCreator, new HistoryPostRecyclerViewAdapter.Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 35b470b8..af2d68e0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -63,6 +63,7 @@ import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import butterknife.BindView; import butterknife.ButterKnife; @@ -82,6 +83,7 @@ import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.adapters.Paging3LoadingStateAdapter; import ml.docilealligator.infinityforreddit.adapters.PostRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; @@ -190,8 +192,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Named("redgifs") Retrofit mRedgifsRetrofit; @Inject - @Named("streamable") - Retrofit mStreamableRetrofit; + Provider mStreamableApiProvider; @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @@ -462,7 +463,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -539,7 +540,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, displaySubredditName, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -610,7 +611,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -675,7 +676,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -736,7 +737,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -796,7 +797,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE + multiRedditPath, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -853,7 +854,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mCustomThemeWrapper, locale, + mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 40121261..c72aacf3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -62,6 +62,7 @@ import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import butterknife.BindView; import butterknife.ButterKnife; @@ -83,6 +84,7 @@ import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.adapters.CommentsRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.PostDetailRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostCommentSortTypeBottomSheetFragment; @@ -161,8 +163,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic @Named("redgifs") Retrofit mRedgifsRetrofit; @Inject - @Named("streamable") - Retrofit mStreamableRetrofit; + Provider mStreamableApiProvider; @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @@ -597,7 +598,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mPostAdapter = new PostDetailRecyclerViewAdapter(activity, this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mStreamableRetrofit, mRedditDataRoomDatabase, mGlide, + mRedgifsRetrofit, mStreamableApiProvider, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostDetailsSharedPreferences, mExoCreator, post -> EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); @@ -1305,7 +1306,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mPostAdapter = new PostDetailRecyclerViewAdapter(activity, ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, - mStreamableRetrofit, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, + mStreamableApiProvider, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostDetailsSharedPreferences, mExoCreator, -- cgit v1.2.3