From a809cb59c27a01e2bbd155809780f187ae52d355 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 4 Jan 2021 17:13:31 +0800 Subject: SearchUsersResultActivity is added. --- app/src/main/AndroidManifest.xml | 4 +- .../infinityforreddit/AppComponent.java | 3 + .../activities/SearchResultActivity.java | 3 +- .../activities/SearchSubredditsResultActivity.java | 4 +- .../activities/SearchUsersResultActivity.java | 200 +++++++++++++++++++++ .../activities/SendPrivateMessageActivity.java | 3 + .../adapters/UserListingRecyclerViewAdapter.java | 22 ++- .../fragments/SubredditListingFragment.java | 6 +- .../fragments/UserListingFragment.java | 23 ++- .../res/layout/activity_search_users_result.xml | 32 ++++ app/src/main/res/values/strings.xml | 2 +- 11 files changed, 281 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java create mode 100644 app/src/main/res/layout/activity_search_users_result.xml (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 910d92b6..4cd7c035 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,7 +32,9 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> - + = Build.VERSION_CODES.M) { + Window window = getWindow(); + + if (isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + if (isImmersiveInterface()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); + } else { + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + } + adjustToolbar(toolbar); + } + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setToolbarGoToTop(toolbar); + + String query = getIntent().getExtras().getString(EXTRA_QUERY); + + if (savedInstanceState == null) { + getCurrentAccountAndInitializeFragment(query); + } else { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + + if (!mNullAccessToken && mAccessToken == null) { + getCurrentAccountAndInitializeFragment(query); + } else { + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); + } + } + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + } + + private void getCurrentAccountAndInitializeFragment(String query) { + GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { + if (account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + mAccountName = account.getUsername(); + } + + mFragment = new UserListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(UserListingFragment.EXTRA_QUERY, query); + bundle.putBoolean(UserListingFragment.EXTRA_IS_GETTING_USER_INFO, true); + bundle.putString(UserListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(UserListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); + }); + } + + public void getSelectedUser(String name, String iconUrl) { + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_USER_NAME, name); + returnIntent.putExtra(EXTRA_RETURN_USER_ICON_URL, iconUrl); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Override + public void onLongPress() { + if (mFragment != null) { + ((UserListingFragment) mFragment).goBackToTop(); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java index 9f707702..57744487 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java @@ -71,6 +71,9 @@ public class SendPrivateMessageActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { ((Infinity) getApplication()).getAppComponent().inject(this); + + setImmersiveModeNotApplicable(); + super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_private_message); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java index a7bd4bc3..2fb83bae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit.adapters; import android.content.Context; -import android.content.Intent; import android.content.res.ColorStateList; import android.view.LayoutInflater; import android.view.View; @@ -26,11 +25,10 @@ import com.bumptech.glide.request.RequestOptions; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; -import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; -import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUserAsyncTask; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUserAsyncTask; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserDao; import ml.docilealligator.infinityforreddit.user.UserData; import ml.docilealligator.infinityforreddit.user.UserFollowing; @@ -67,12 +65,12 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(context, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, userData.getName()); - context.startActivity(intent); + callback.userSelected(userData.getName(), userData.getIconUrl()); }); if (!userData.getIconUrl().equals("")) { @@ -211,8 +207,10 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter retryLoadingMoreCallback.retryLoadingMore()); + retryButton.setOnClickListener(view -> callback.retryLoadingMore()); errorTextView.setText(R.string.load_comments_failed); errorTextView.setTextColor(primaryTextColor); retryButton.setTextColor(buttonTextColor); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java index 2924b393..ef0d7a89 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java @@ -53,7 +53,7 @@ import retrofit2.Retrofit; public class SubredditListingFragment extends Fragment implements FragmentCommunicator { public static final String EXTRA_QUERY = "EQ"; - public static final String EXTRA_IS_POSTING = "EIP"; + public static final String EXTRA_IS_GETTING_SUBREDDIT_INFO = "EIGSI"; public static final String EXTRA_ACCESS_TOKEN = "EAT"; public static final String EXTRA_ACCOUNT_NAME = "EAN"; @@ -127,7 +127,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mSubredditListingRecyclerView.setLayoutManager(mLinearLayoutManager); String query = getArguments().getString(EXTRA_QUERY); - boolean isPosting = getArguments().getBoolean(EXTRA_IS_POSTING); + boolean isGettingSubredditInfo = getArguments().getBoolean(EXTRA_IS_GETTING_SUBREDDIT_INFO); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); @@ -145,7 +145,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun @Override public void subredditSelected(String subredditName, String iconUrl) { - if (isPosting) { + if (isGettingSubredditInfo) { ((SearchSubredditsResultActivity) mActivity).getSelectedSubreddit(subredditName, iconUrl); } else { Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java index c663bd63..ecd16441 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.fragments; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Build; @@ -37,6 +38,8 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.activities.BaseActivity; +import ml.docilealligator.infinityforreddit.activities.SearchUsersResultActivity; +import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.adapters.UserListingRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.user.UserListingViewModel; @@ -50,6 +53,7 @@ import retrofit2.Retrofit; public class UserListingFragment extends Fragment implements FragmentCommunicator { public static final String EXTRA_QUERY = "EQ"; + public static final String EXTRA_IS_GETTING_USER_INFO = "EIGUI"; public static final String EXTRA_ACCESS_TOKEN = "EAT"; public static final String EXTRA_ACCOUNT_NAME = "EAN"; @@ -124,6 +128,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mUserListingRecyclerView.setLayoutManager(mLinearLayoutManager); mQuery = getArguments().getString(EXTRA_QUERY); + boolean isGettingUserInfo = getArguments().getBoolean(EXTRA_IS_GETTING_USER_INFO); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER, SortType.Type.RELEVANCE.value); @@ -132,7 +137,23 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mAdapter = new UserListingRecyclerViewAdapter(getActivity(), mOauthRetrofit, mRetrofit, customThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase.subscribedUserDao(), - () -> mUserListingViewModel.retryLoadingMore()); + new UserListingRecyclerViewAdapter.Callback() { + @Override + public void retryLoadingMore() { + mUserListingViewModel.retryLoadingMore(); + } + + @Override + public void userSelected(String username, String iconUrl) { + if (isGettingUserInfo) { + ((SearchUsersResultActivity) mActivity).getSelectedUser(username, iconUrl); + } else { + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, username); + mActivity.startActivity(intent); + } + } + }); mUserListingRecyclerView.setAdapter(mAdapter); diff --git a/app/src/main/res/layout/activity_search_users_result.xml b/app/src/main/res/layout/activity_search_users_result.xml new file mode 100644 index 00000000..b594ccb5 --- /dev/null +++ b/app/src/main/res/layout/activity_search_users_result.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a5aa5353..9658a65a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -993,7 +993,7 @@ Subreddit User Leave it blank to apply this post filter to all the subreddits / users / multireddits - + You are doing this too frequently. Try again later. This is Reddit API\'s rate limit. Read all messages successfully Unable to read all messages -- cgit v1.2.3