From 1c8ba320bc8b1c64dfaff32e4debe740b6820213 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 8 Aug 2019 11:14:18 +0800 Subject: Refactored some of the untouched classes last time to support multi user. Bugs fixed related to subreddit selection in PostXXXActivity. --- app/src/main/java/Account/AccountDao.java | 12 ++-- .../infinityforreddit/AppComponent.java | 1 + .../infinityforreddit/AppModule.java | 13 ---- .../infinityforreddit/CommentActivity.java | 40 ++++++++++-- .../infinityforreddit/CommentsListingFragment.java | 2 +- .../infinityforreddit/FetchFlairsInSubreddit.java | 4 +- .../infinityforreddit/FilteredPostsActivity.java | 2 +- .../FlairBottomSheetFragment.java | 25 ++++---- .../FollowedUsersListingFragment.java | 2 +- .../infinityforreddit/Infinity.java | 2 +- .../infinityforreddit/LoginActivity.java | 4 +- .../infinityforreddit/MainActivity.java | 36 ++++------- .../infinityforreddit/ParseAndSaveAccountInfo.java | 74 ++++++++++++++++++++++ .../infinityforreddit/ParseMyInfo.java | 68 -------------------- .../infinityforreddit/PostFragment.java | 2 +- .../infinityforreddit/PostImageActivity.java | 50 ++++++++++++--- .../infinityforreddit/PostLinkActivity.java | 31 ++++++--- .../infinityforreddit/PostTextActivity.java | 25 ++++---- .../infinityforreddit/PostVideoActivity.java | 27 +++++--- .../infinityforreddit/RulesActivity.java | 2 +- .../infinityforreddit/SearchResultActivity.java | 2 +- .../SearchSubredditsResultActivity.java | 2 +- .../infinityforreddit/SubmitPost.java | 31 ++++----- .../infinityforreddit/SubmitPostService.java | 72 ++++++++++----------- .../SubredditListingFragment.java | 6 +- .../SubredditSelectionActivity.java | 65 +++++++++++++++---- .../SubscribedSubredditsListingFragment.java | 2 +- .../SubscribedSubredditsRecyclerViewAdapter.java | 2 +- .../SubscribedThingListingActivity.java | 2 +- .../infinityforreddit/UserListingFragment.java | 7 +- .../infinityforreddit/ViewPostDetailActivity.java | 2 +- .../ViewSubredditDetailActivity.java | 2 +- .../infinityforreddit/ViewUserDetailActivity.java | 2 +- 33 files changed, 353 insertions(+), 266 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseMyInfo.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/Account/AccountDao.java b/app/src/main/java/Account/AccountDao.java index 8db57683..3052e2b5 100644 --- a/app/src/main/java/Account/AccountDao.java +++ b/app/src/main/java/Account/AccountDao.java @@ -22,12 +22,16 @@ public interface AccountDao { @Query("DELETE FROM accounts") void deleteAllAccounts(); - @Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") - LiveData getAccountLiveData(String userName); + @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1") + LiveData getAccountLiveData(String username); - @Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") - Account getAccountData(String userName); + @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1") + Account getAccountData(String username); @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") Account getCurrentAccount(); + + @Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " + + "karma = :karma WHERE username = :username") + void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 12997920..39616ae6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -30,4 +30,5 @@ interface AppComponent { void inject(SearchResultActivity searchResultActivity); void inject(SearchSubredditsResultActivity searchSubredditsResultActivity); void inject(FollowedUsersListingFragment followedUsersListingFragment); + void inject(SubredditSelectionActivity subredditSelectionActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 76afe22c..418b349a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -1,8 +1,6 @@ package ml.docilealligator.infinityforreddit; import android.app.Application; -import android.content.Context; -import android.content.SharedPreferences; import javax.inject.Named; import javax.inject.Singleton; @@ -66,17 +64,6 @@ class AppModule { return okHttpClientBuilder.build(); } - @Provides @Named("auth_info") - @Singleton - SharedPreferences provideAuthInfoSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE); - } - - @Provides @Named("user_info") - SharedPreferences provideUserInfoSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE); - } - @Provides @Singleton RedditDataRoomDatabase provideRedditDataRoomDatabase() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index b4560802..85a01ff2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -32,11 +31,16 @@ public class CommentActivity extends AppCompatActivity { static final String EXTRA_IS_REPLYING_KEY = "EIRK"; static final int WRITE_COMMENT_REQUEST_CODE = 1; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; + @BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.toolbar_comment_activity) Toolbar toolbar; @BindView(R.id.comment_parent_markwon_view_comment_activity) MarkwonView commentParentMarkwonView; @BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText; + private boolean mNullAccessToken = false; + private String mAccessToken; private String parentFullname; private int parentDepth; private int parentPosition; @@ -47,8 +51,7 @@ public class CommentActivity extends AppCompatActivity { Retrofit mOauthRetrofit; @Inject - @Named("auth_info") - SharedPreferences sharedPreferences; + RedditDataRoomDatabase mRedditDataRoomDatabase; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,7 +60,17 @@ public class CommentActivity extends AppCompatActivity { ButterKnife.bind(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); + + if(savedInstanceState == null) { + getCurrentAccount(); + } else { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + if(!mNullAccessToken && mAccessToken == null) { + getCurrentAccount(); + } + } Intent intent = getIntent(); commentParentMarkwonView.setMarkdown(intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY)); @@ -72,6 +85,16 @@ public class CommentActivity extends AppCompatActivity { setSupportActionBar(toolbar); } + private void getCurrentAccount() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if(account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + } + }).execute(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.comment_activity, menu); @@ -92,7 +115,7 @@ public class CommentActivity extends AppCompatActivity { SendComment.sendComment(commentEditText.getText().toString(), parentFullname, parentDepth, getResources().getConfiguration().locale, mOauthRetrofit, - sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""), + mAccessToken, new SendComment.SendCommentListener() { @Override public void sendCommentSuccess(CommentData commentData) { @@ -126,4 +149,11 @@ public class CommentActivity extends AppCompatActivity { return false; } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java index 7a4e1fef..d093158e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java @@ -70,7 +70,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_comments_listing, container, false); - ((Infinity) activity.getApplication()).getmAppComponent().inject(this); + ((Infinity) activity.getApplication()).getAppComponent().inject(this); ButterKnife.bind(this, rootView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java index 602fd35d..4a04ee2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.content.SharedPreferences; import android.os.AsyncTask; import androidx.annotation.NonNull; @@ -21,10 +20,9 @@ class FetchFlairsInSubreddit { void fetchFailed(); } - static void fetchFlairs(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { + static void fetchFlairs(Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); Call flairsCall = api.getFlairs(RedditUtils.getOAuthHeader(accessToken), subredditName); flairsCall.enqueue(new Callback() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java index 6d5d8a2d..18d57300 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredPostsActivity.java @@ -51,7 +51,7 @@ public class FilteredPostsActivity extends AppCompatActivity implements SortType ButterKnife.bind(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java index 4e21d86d..5ccec346 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; -import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -35,19 +34,19 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { void flairSelected(String flair); } + static final String EXTRA_ACCESS_TOKEN = "EAT"; static final String EXTRA_SUBREDDIT_NAME = "ESN"; @BindView(R.id.progress_bar_flair_bottom_sheet_fragment) ProgressBar progressBar; @BindView(R.id.error_text_view_flair_bottom_sheet_fragment) TextView errorTextView; @BindView(R.id.recycler_view_bottom_sheet_fragment) RecyclerView recyclerView; + private String mAccessToken; + private String mSubredditName; + private Activity mAcitivity; private FlairBottomSheetRecyclerViewAdapter mAdapter; - @Inject - @Named("auth_info") - SharedPreferences mAuthInfoSharedPreferences; - @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -66,21 +65,23 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { mAcitivity = getActivity(); - ((Infinity) mAcitivity.getApplication()).getmAppComponent().inject(this); + ((Infinity) mAcitivity.getApplication()).getAppComponent().inject(this); mAdapter = new FlairBottomSheetRecyclerViewAdapter(flair -> ((FlairSelectionCallback) mAcitivity).flairSelected(flair)); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(mAdapter); - String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); - fetchFlairs(subredditName); + mAccessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); + mSubredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); + + fetchFlairs(); return rootView; } - private void fetchFlairs(String subredditName) { - FetchFlairsInSubreddit.fetchFlairs(mOauthRetrofit, mAuthInfoSharedPreferences, - subredditName, new FetchFlairsInSubreddit.FetchFlairsInSubredditListener() { + private void fetchFlairs() { + FetchFlairsInSubreddit.fetchFlairs(mOauthRetrofit, mAccessToken, + mSubredditName, new FetchFlairsInSubreddit.FetchFlairsInSubredditListener() { @Override public void fetchSuccessful(ArrayList flairs) { progressBar.setVisibility(View.GONE); @@ -98,7 +99,7 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { progressBar.setVisibility(View.GONE); errorTextView.setVisibility(View.VISIBLE); errorTextView.setText(R.string.error_loading_flairs); - errorTextView.setOnClickListener(view -> fetchFlairs(subredditName)); + errorTextView.setOnClickListener(view -> fetchFlairs()); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java index 1d67a82d..53fb9506 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java @@ -57,7 +57,7 @@ public class FollowedUsersListingFragment extends Fragment { mActivity = getActivity(); - ((Infinity) mActivity.getApplication()).getmAppComponent().inject(this); + ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); mGlide = Glide.with(this); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java index 24345789..df5a1100 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java @@ -34,7 +34,7 @@ public class Infinity extends Application { }); } - public AppComponent getmAppComponent() { + public AppComponent getAppComponent() { return mAppComponent; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java index e5e125f3..00e24bcb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java @@ -47,7 +47,7 @@ public class LoginActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -100,7 +100,7 @@ public class LoginActivity extends AppCompatActivity { FetchMyInfo.fetchMyInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() { @Override public void onFetchMyInfoSuccess(String response) { - ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() { + ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() { @Override public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { new ParseAndInsertNewAccountAsyncTask(name, accessToken, refreshToken, profileImageUrl, bannerImageUrl, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index a5e3018d..6c3831f2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -86,10 +85,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe private boolean isInLazyMode = false; - @Inject - @Named("user_info") - SharedPreferences mUserInfoSharedPreferences; - @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -104,7 +99,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe ButterKnife.bind(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); @@ -149,13 +144,15 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe private void getCurrentAccountAndBindView() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { + mNullAccessToken = true; Intent loginIntent = new Intent(this, LoginActivity.class); startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE); } else { mAccessToken = account.getAccessToken(); - if(mAccessToken == null) { - mNullAccessToken = true; - } + mName = account.getUsername(); + mProfileImageUrl = account.getProfileImageUrl(); + mBannerImageUrl = account.getBannerImageUrl(); + mKarma = Integer.toString(account.getKarma()); bindView(); } }).execute(); @@ -183,12 +180,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main); mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main); - loadUserData(mAccessToken); - - mName = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""); - mProfileImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, ""); - mBannerImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, ""); - mKarma = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.KARMA_KEY, ""); + loadUserData(); mNameTextView.setText(mName); mKarmaTextView.setText(mKarma); @@ -225,12 +217,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe }); } - private void loadUserData(String accessToken) { + private void loadUserData() { if (!mFetchUserInfoSuccess) { - FetchMyInfo.fetchMyInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() { + FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() { @Override public void onFetchMyInfoSuccess(String response) { - ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() { + ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() { @Override public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { mNameTextView.setText(name); @@ -256,12 +248,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mKarmaTextView.setText(mKarma); - SharedPreferences.Editor editor = mUserInfoSharedPreferences.edit(); - editor.putString(SharedPreferencesUtils.USER_KEY, name); - editor.putString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, profileImageUrl); - editor.putString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, bannerImageUrl); - editor.putString(SharedPreferencesUtils.KARMA_KEY, mKarma); - editor.apply(); mFetchUserInfoSuccess = true; } @@ -328,7 +314,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe case R.id.action_refresh_main_activity: sectionsPagerAdapter.refresh(viewPager.getCurrentItem()); mFetchUserInfoSuccess = false; - loadUserData(mAccessToken); + loadUserData(); return true; case R.id.action_lazy_mode_main_activity: /*MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java new file mode 100644 index 00000000..ae133e2e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java @@ -0,0 +1,74 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; +import android.text.Html; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +class ParseAndSaveAccountInfo { + interface ParseAndSaveAccountInfoListener { + void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma); + void onParseMyInfoFail(); + } + + static void parseAndSaveAccountInfo(String response, RedditDataRoomDatabase redditDataRoomDatabase, + ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener) { + new ParseAndSaveAccountInfoAsyncTask(response, redditDataRoomDatabase, parseAndSaveAccountInfoListener).execute(); + } + + private static class ParseAndSaveAccountInfoAsyncTask extends AsyncTask { + private JSONObject jsonResponse; + private RedditDataRoomDatabase redditDataRoomDatabase; + private ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener; + private boolean parseFailed; + + private String name; + private String profileImageUrl; + private String bannerImageUrl; + private int karma; + + ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase, + ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener){ + try { + jsonResponse = new JSONObject(response); + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.parseAndSaveAccountInfoListener = parseAndSaveAccountInfoListener; + parseFailed = false; + } catch (JSONException e) { + Log.i("user info json error", "message: " + e.getMessage()); + parseAndSaveAccountInfoListener.onParseMyInfoFail(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + name = jsonResponse.getString(JSONUtils.NAME_KEY); + profileImageUrl = Html.fromHtml(jsonResponse.getString(JSONUtils.ICON_IMG_KEY)).toString(); + if(!jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) { + bannerImageUrl = Html.fromHtml(jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY)).toString(); + } + int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY); + int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY); + karma = linkKarma + commentKarma; + + redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma); + } catch (JSONException e) { + parseFailed = true; + Log.i("parse comment error", "message: " + e.getMessage()); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if(!parseFailed) { + parseAndSaveAccountInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); + } else { + parseAndSaveAccountInfoListener.onParseMyInfoFail(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseMyInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseMyInfo.java deleted file mode 100644 index 87d0a200..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseMyInfo.java +++ /dev/null @@ -1,68 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; -import android.text.Html; -import android.util.Log; - -import org.json.JSONException; -import org.json.JSONObject; - -class ParseMyInfo { - interface ParseMyInfoListener { - void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma); - void onParseMyInfoFail(); - } - - static void parseMyInfo(String response, ParseMyInfoListener parseMyInfoListener) { - new ParseMyInfoAsyncTask(response, parseMyInfoListener).execute(); - } - - private static class ParseMyInfoAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private ParseMyInfoListener parseMyInfoListener; - private boolean parseFailed; - - private String name; - private String profileImageUrl; - private String bannerImageUrl; - private int karma; - - ParseMyInfoAsyncTask(String response, ParseMyInfoListener parseMyInfoListener){ - try { - jsonResponse = new JSONObject(response); - this.parseMyInfoListener = parseMyInfoListener; - parseFailed = false; - } catch (JSONException e) { - Log.i("user info json error", e.getMessage()); - parseMyInfoListener.onParseMyInfoFail(); - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - name = jsonResponse.getString(JSONUtils.NAME_KEY); - profileImageUrl = Html.fromHtml(jsonResponse.getString(JSONUtils.ICON_IMG_KEY)).toString(); - if(!jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) { - bannerImageUrl = Html.fromHtml(jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY)).toString(); - } - int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY); - int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY); - karma = linkKarma + commentKarma; - } catch (JSONException e) { - parseFailed = true; - Log.i("parse comment error", e.getMessage()); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if(!parseFailed) { - parseMyInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); - } else { - parseMyInfoListener.onParseMyInfoFail(); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 6c75d4dc..231a846a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -108,7 +108,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_post, container, false); - ((Infinity) activity.getApplication()).getmAppComponent().inject(this); + ((Infinity) activity.getApplication()).getAppComponent().inject(this); ButterKnife.bind(this, rootView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 927e33d7..2c22b131 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -58,6 +58,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; + private static final String ACCOUNT_NAME_STATE = "ANS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int PICK_IMAGE_REQUEST_CODE = 1; @@ -78,6 +81,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS @BindView(R.id.select_again_text_view_post_image_activity) TextView selectAgainTextView; @BindView(R.id.image_view_post_image_activity) ImageView imageView; + private boolean mNullAccessToken = false; + private String mAccessToken; + private String mAccountName; private String iconUrl; private String subredditName; private boolean subredditSelected = false; @@ -108,7 +114,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS Retrofit mUploadMediaRetrofit; @Inject - RedditDataRoomDatabase redditDataRoomDatabase; + RedditDataRoomDatabase mRedditDataRoomDatabase; @Override protected void onCreate(Bundle savedInstanceState) { @@ -119,7 +125,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS EventBus.getDefault().register(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -127,6 +133,14 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS mGlide = Glide.with(this); if(savedInstanceState != null) { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + + if(!mNullAccessToken && mAccessToken == null) { + getCurrentAccount(); + } + subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); @@ -167,6 +181,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } } else { + getCurrentAccount(); + isPosting = false; if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -208,6 +224,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); flairSelectionBottomSheetFragment.setArguments(bundle); flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag()); @@ -270,6 +287,17 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS }); } + private void getCurrentAccount() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if(account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + mAccountName = account.getUsername(); + } + }).execute(); + } + private void loadImage() { constraintLayout.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); @@ -292,7 +320,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS } private void loadSubredditIcon() { - new LoadSubredditIconAsyncTask(redditDataRoomDatabase.subredditDao(), + new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(), subredditName, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); @@ -344,6 +372,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS Intent intent = new Intent(this, SubmitPostService.class); intent.setData(imageUri); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); @@ -378,6 +407,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS outState.putString(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override @@ -434,13 +466,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) { isPosting = false; if(submitImagePostEvent.postSuccess) { - new GetCurrentAccountAsyncTask(redditDataRoomDatabase.accountDao(), account -> { - Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, - account.getUsername()); - startActivity(intent); - finish(); - }).execute(); + Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, + mAccountName); + startActivity(intent); + finish(); } else { mPostingSnackbar.dismiss(); mMemu.getItem(R.id.action_send_post_image_activity).setEnabled(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index f0f866ff..e8d028ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -46,8 +46,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCOUNT_NAME_STATE = "NATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -62,8 +62,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh @BindView(R.id.post_title_edit_text_post_link_activity) EditText titleEditText; @BindView(R.id.post_link_edit_text_post_link_activity) EditText contentEditText; - private boolean mNullAccountName = false; - private String mAccountName; + private boolean mNullAccessToken = false; + private String mAccessToken; private String iconUrl; private String subredditName; private boolean subredditSelected = false; @@ -100,7 +100,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh EventBus.getDefault().register(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -108,6 +108,13 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh mGlide = Glide.with(this); if(savedInstanceState != null) { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + + if(!mNullAccessToken && mAccessToken == null) { + getCurrentAccount(); + } + subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); @@ -143,6 +150,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } } else { + getCurrentAccount(); + isPosting = false; if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -185,6 +194,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); flairSelectionBottomSheetFragment.setArguments(bundle); flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag()); @@ -216,12 +226,12 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh }); } - private void getCurrentAccountName() { + private void getCurrentAccount() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { - mNullAccountName = true; + mNullAccessToken = true; } else { - mAccountName = account.getUsername(); + mAccessToken = account.getAccessToken(); } }).execute(); } @@ -287,6 +297,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); @@ -315,8 +326,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh outState.putString(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 957360b1..6b8784de 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -47,8 +46,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCOUNT_NAME_STATE = "NATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -63,8 +62,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh @BindView(R.id.post_title_edit_text_post_text_activity) EditText titleEditText; @BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText; - private boolean mNullAccountName = false; - private String mAccountName; + private boolean mNullAccessToken = false; + private String mAccessToken; private String iconUrl; private String subredditName; private boolean subredditSelected = false; @@ -89,10 +88,6 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh @Named("oauth") Retrofit mOauthRetrofit; - @Inject - @Named("auth_info") - SharedPreferences sharedPreferences; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @@ -105,7 +100,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh EventBus.getDefault().register(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -191,6 +186,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); if(subredditIsUser) { bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); } else { @@ -229,9 +225,9 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private void getCurrentAccountName() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { - mNullAccountName = true; + mNullAccessToken = true; } else { - mAccountName = account.getUsername(); + mAccessToken = account.getAccessToken(); } }).execute(); } @@ -297,6 +293,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); @@ -325,8 +322,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh outState.putString(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 0864c8ff..6adf0c75 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -53,7 +53,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCOUNT_NAME_STATE = "NATS"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCOUNT_NAME_STATE = "ANS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -75,7 +76,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS @BindView(R.id.select_again_text_view_post_video_activity) TextView selectAgainTextView; @BindView(R.id.video_view_post_video_activity) VideoView videoView; - private boolean mNullAccountName = false; + private boolean mNullAccessToken = false; + private String mAccessToken; private String mAccountName; private String iconUrl; private String subredditName; @@ -122,7 +124,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS EventBus.getDefault().register(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -139,11 +141,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS flair = savedInstanceState.getString(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); - mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE); + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - if(!mNullAccountName && mAccountName == null) { - getCurrentAccountName(); + if(!mNullAccessToken && mAccessToken == null) { + getCurrentAccount(); } if(savedInstanceState.getString(VIDEO_URI_STATE) != null) { @@ -176,7 +179,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } } else { - getCurrentAccountName(); + getCurrentAccount(); isPosting = false; @@ -219,6 +222,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS if(flair == null) { mFlairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); mFlairSelectionBottomSheetFragment.setArguments(bundle); mFlairSelectionBottomSheetFragment.show(getSupportFragmentManager(), mFlairSelectionBottomSheetFragment.getTag()); @@ -277,11 +281,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS }); } - private void getCurrentAccountName() { + private void getCurrentAccount() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { - mNullAccountName = true; + mNullAccessToken = true; } else { + mAccessToken = account.getAccessToken(); mAccountName = account.getUsername(); } }).execute(); @@ -362,6 +367,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS Intent intent = new Intent(this, SubmitPostService.class); intent.setData(videoUri); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); @@ -402,7 +408,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS outState.putString(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java index 18584546..4471cec6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java @@ -53,7 +53,7 @@ public class RulesActivity extends AppCompatActivity { ButterKnife.bind(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); ActionBar actionBar = getSupportActionBar(); Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index 42cf85b1..eb07265a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -55,7 +55,7 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos ButterKnife.bind(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java index aac45900..2776ed38 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java @@ -44,7 +44,7 @@ public class SearchSubredditsResultActivity extends AppCompatActivity { ButterKnife.bind(this); - ((Infinity) getApplication()).getmAppComponent().inject(this); + ((Infinity) getApplication()).getAppComponent().inject(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index e2ce499c..60b90560 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.content.SharedPreferences; import android.graphics.Bitmap; import android.os.AsyncTask; import android.util.Log; @@ -41,23 +40,23 @@ class SubmitPost { void uploadFailed(@Nullable String errorMessage); } - static void submitTextOrLinkPost(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, + static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken, Locale locale, String subredditName, String title, String content, String flair, boolean isSpoiler, boolean isNSFW, String kind, SubmitPostListener submitPostListener) { - submitPost(oauthRetrofit, authInfoSharedPreferences, locale, subredditName, title, content, + submitPost(oauthRetrofit, accessToken, locale, subredditName, title, content, flair, isSpoiler, isNSFW, kind, null, submitPostListener); } static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, - SharedPreferences authInfoSharedPreferences, Locale locale, + String accessToken, Locale locale, String subredditName, String title, Bitmap image, String flair, boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) { - uploadImage(oauthRetrofit, uploadMediaRetrofit, authInfoSharedPreferences, image, + uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image, new UploadImageListener() { @Override public void uploaded(String imageUrl) { - submitPost(oauthRetrofit, authInfoSharedPreferences, locale, + submitPost(oauthRetrofit, accessToken, locale, subredditName, title, imageUrl, flair, isSpoiler, isNSFW, RedditUtils.KIND_IMAGE, null, submitPostListener); } @@ -70,12 +69,11 @@ class SubmitPost { } static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, - Retrofit uploadVideoRetrofit, SharedPreferences authInfoSharedPreferences, + Retrofit uploadVideoRetrofit, String accessToken, Locale locale, String subredditName, String title, byte[] buffer, String mimeType, Bitmap posterBitmap, String flair, boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); String fileType = mimeType.substring(mimeType.indexOf("/") + 1); @@ -110,16 +108,16 @@ class SubmitPost { new ParseXMLReponseFromAWSAsyncTask(response.body(), new ParseXMLReponseFromAWSAsyncTask.ParseXMLResponseFromAWSListener() { @Override public void parseSuccessful(String url) { - uploadImage(oauthRetrofit, uploadMediaRetrofit, authInfoSharedPreferences, + uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, posterBitmap, new UploadImageListener() { @Override public void uploaded(String imageUrl) { if(fileType.equals("gif")) { - submitPost(oauthRetrofit, authInfoSharedPreferences, locale, + submitPost(oauthRetrofit, accessToken, locale, subredditName, title, url, flair, isSpoiler, isNSFW, RedditUtils.KIND_VIDEOGIF, imageUrl, submitPostListener); } else { - submitPost(oauthRetrofit, authInfoSharedPreferences, locale, + submitPost(oauthRetrofit, accessToken, locale, subredditName, title, url, flair, isSpoiler, isNSFW, RedditUtils.KIND_VIDEO, imageUrl, submitPostListener); } @@ -167,12 +165,11 @@ class SubmitPost { }); } - private static void submitPost(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, + private static void submitPost(Retrofit oauthRetrofit, String accessToken, Locale locale, String subredditName, String title, String content, String flair, boolean isSpoiler, boolean isNSFW, String kind, @Nullable String posterUrl, SubmitPostListener submitPostListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); Map params = new HashMap<>(); params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON); @@ -211,7 +208,7 @@ class SubmitPost { Log.i("code", "asfd" + response.body()); if(response.isSuccessful()) { try { - getSubmittedPost(response.body(), kind, oauthRetrofit, authInfoSharedPreferences, + getSubmittedPost(response.body(), kind, oauthRetrofit, accessToken, locale, submitPostListener); } catch (JSONException e) { e.printStackTrace(); @@ -232,10 +229,9 @@ class SubmitPost { } private static void uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, - SharedPreferences authInfoSharedPreferences, Bitmap image, + String accessToken, Bitmap image, UploadImageListener uploadImageListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); Map uploadImageParams = new HashMap<>(); uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_image.jpg"); @@ -414,7 +410,7 @@ class SubmitPost { } private static void getSubmittedPost(String response, String kind, Retrofit oauthRetrofit, - SharedPreferences authInfoSharedPreferences, Locale locale, + String accessToken, Locale locale, SubmitPostListener submitPostListener) throws JSONException { JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY); if(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { @@ -442,7 +438,6 @@ class SubmitPost { String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY); RedditAPI api = oauthRetrofit.create(RedditAPI.class); - String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); Call getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken)); getPostCall.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java index b78f4ab2..67b90f20 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java @@ -5,7 +5,6 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; import android.content.Intent; -import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -33,6 +32,7 @@ import javax.inject.Named; import retrofit2.Retrofit; public class SubmitPostService extends Service { + static final String EXTRA_ACCESS_TOKEN = "EAT"; static final String EXTRA_SUBREDDIT_NAME = "ESN"; static final String EXTRA_TITLE = "ET"; static final String EXTRA_CONTENT = "EC"; @@ -45,6 +45,16 @@ public class SubmitPostService extends Service { static final int EXTRA_POST_TYPE_IMAGE = 1; static final int EXTRA_POST_TYPE_VIDEO = 2; + private String mAccessToken; + private String subredditName; + private String title; + private String flair; + private boolean isSpoiler; + private boolean isNSFW; + private String content; + private String kind; + private Uri mediaUri; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -57,14 +67,6 @@ public class SubmitPostService extends Service { @Named("upload_video") Retrofit mUploadVideoRetrofit; - @Inject - @Named("user_info") - SharedPreferences mUserInfoSharedPreferences; - - @Inject - @Named("auth_info") - SharedPreferences sharedPreferences; - public SubmitPostService() { } @@ -75,13 +77,14 @@ public class SubmitPostService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - ((Infinity) getApplication()).getmAppComponent().inject(this); - - String subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); - String title = intent.getExtras().getString(EXTRA_TITLE); - String flair = intent.getExtras().getString(EXTRA_FLAIR); - boolean isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER); - boolean isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW); + ((Infinity) getApplication()).getAppComponent().inject(this); + + mAccessToken = intent.getExtras().getString(EXTRA_ACCESS_TOKEN); + subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); + title = intent.getExtras().getString(EXTRA_TITLE); + flair = intent.getExtras().getString(EXTRA_FLAIR); + isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER); + isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW); int postType = intent.getExtras().getInt(EXTRA_POST_TYPE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -96,18 +99,18 @@ public class SubmitPostService extends Service { } if(postType == EXTRA_POST_TEXT_OR_LINK) { - String content = intent.getExtras().getString(EXTRA_CONTENT); - String kind = intent.getExtras().getString(EXTRA_KIND); + content = intent.getExtras().getString(EXTRA_CONTENT); + kind = intent.getExtras().getString(EXTRA_KIND); startForeground(1, createNotification(R.string.posting)); - submitTextOrLinkPost(subredditName, title, content, flair, isSpoiler, isNSFW, kind); + submitTextOrLinkPost(); } else if(postType == EXTRA_POST_TYPE_IMAGE) { - Uri imageUri = intent.getData(); + mediaUri = intent.getData(); startForeground(1, createNotification(R.string.posting_image)); - submitImagePost(imageUri, subredditName, title, flair, isSpoiler, isNSFW); + submitImagePost(); } else { - Uri videoUri = intent.getData(); + mediaUri = intent.getData(); startForeground(1, createNotification(R.string.posting_video)); - submitVideoPost(videoUri, subredditName, title, flair, isSpoiler, isNSFW); + submitVideoPost(); } return START_NOT_STICKY; @@ -121,9 +124,8 @@ public class SubmitPostService extends Service { .build(); } - private void submitTextOrLinkPost(String subredditName, String title, String content, String flair, - boolean isSpoiler, boolean isNSFW, String kind) { - SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, getResources().getConfiguration().locale, + private void submitTextOrLinkPost() { + SubmitPost.submitTextOrLinkPost(mOauthRetrofit, mAccessToken, getResources().getConfiguration().locale, subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() { @Override public void submitSuccessful(Post post) { @@ -143,16 +145,15 @@ public class SubmitPostService extends Service { }); } - private void submitImagePost(Uri imageUri, String subredditName, String title, String flair, - boolean isSpoiler, boolean isNSFW) { + private void submitImagePost() { Glide.with(this) .asBitmap() - .load(imageUri) + .load(mediaUri) .into(new CustomTarget() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences, + SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, getResources().getConfiguration().locale, subredditName, title, resource, flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() { @Override @@ -181,9 +182,8 @@ public class SubmitPostService extends Service { }); } - private void submitVideoPost(Uri videoUri, String subredditName, String title, String flair, - boolean isSpoiler, boolean isNSFW) { - try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(videoUri, "r")) { + private void submitVideoPost() { + try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mediaUri, "r")) { FileInputStream in = new FileInputStream(pfd.getFileDescriptor()); byte[] buffer; buffer = new byte[in.available()]; @@ -191,13 +191,13 @@ public class SubmitPostService extends Service { Glide.with(this) .asBitmap() - .load(videoUri) + .load(mediaUri) .into(new CustomTarget() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit, - sharedPreferences, getResources().getConfiguration().locale, subredditName, title, - buffer, getContentResolver().getType(videoUri), resource, flair, isSpoiler, isNSFW, + mAccessToken, getResources().getConfiguration().locale, subredditName, title, + buffer, getContentResolver().getType(mediaUri), resource, flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() { @Override public void submitSuccessful(Post post) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java index e4930217..61399f2b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -53,9 +52,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun SubredditListingViewModel mSubredditListingViewModel; - @Inject @Named("auth_info") - SharedPreferences mAuthInfoSharedPreferences; - @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -78,7 +74,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun Activity activity = getActivity(); - ((Infinity) activity.getApplication()).getmAppComponent().inject(this); + ((Infinity) activity.getApplication()).getAppComponent().inject(this); ButterKnife.bind(this, rootView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 4367d19e..3c054b1f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -12,6 +12,8 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; +import javax.inject.Inject; + import butterknife.BindView; import butterknife.ButterKnife; @@ -23,12 +25,22 @@ public class SubredditSelectionActivity extends AppCompatActivity { static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU"; private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 0; + private static final String NULL_ACCOUNT_NAME_STATE = "NATS"; + private static final String ACCOUNT_NAME_STATE = "ATS"; + private static final String ACCOUNT_PROFILE_IMAGE_URL = "APIU"; private static final String FRAGMENT_OUT_STATE = "FOS"; @BindView(R.id.toolbar_subreddit_selection_activity) Toolbar toolbar; + private boolean mNullAccountName = false; + private String mAccountName; + private String mAccountProfileImageUrl; + private Fragment mFragment; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -36,23 +48,51 @@ public class SubredditSelectionActivity extends AppCompatActivity { ButterKnife.bind(this); + ((Infinity) getApplication()).getAppComponent().inject(this); + setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); if(savedInstanceState == null) { - mFragment = new SubscribedSubredditsListingFragment(); - Bundle bundle = new Bundle(); - bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_IS_SUBREDDIT_SELECTION, true); - if(getIntent().hasExtra(EXTRA_EXTRA_CLEAR_SELECTION)) { - bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_EXTRA_CLEAR_SELECTION, - getIntent().getExtras().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION)); - } - mFragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); + getCurrentAccountAndInitializeFragment(); } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); + mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + mAccountProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL); + + if(!mNullAccountName && mAccountName == null) { + getCurrentAccountAndInitializeFragment(); + } else { + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); + } + } + } + + private void getCurrentAccountAndInitializeFragment() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if(account == null) { + mNullAccountName = true; + } else { + mAccountName = account.getUsername(); + mAccountProfileImageUrl = account.getProfileImageUrl(); + } + initializeFragment(); + }).execute(); + } + + private void initializeFragment() { + mFragment = new SubscribedSubredditsListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(SubscribedSubredditsListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + bundle.putString(SubscribedSubredditsListingFragment.EXTRA_ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl); + bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_IS_SUBREDDIT_SELECTION, true); + if(getIntent().hasExtra(EXTRA_EXTRA_CLEAR_SELECTION)) { + bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_EXTRA_CLEAR_SELECTION, + getIntent().getExtras().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION)); } + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); } @Override @@ -109,5 +149,8 @@ public class SubredditSelectionActivity extends AppCompatActivity { if (mFragment != null) { getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); } + outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); + outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index 21a7c894..d4c877d8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -61,7 +61,7 @@ public class SubscribedSubredditsListingFragment extends Fragment { mActivity = getActivity(); - ((Infinity) mActivity.getApplication()).getmAppComponent().inject(this); + ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java index 6f9288cd..e3cc97b6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java @@ -94,7 +94,7 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter