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. --- .../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 ++- 8 files changed, 245 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java (limited to 'app/src/main/java/ml/docilealligator') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 45b0eba0..d2797aad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -33,6 +33,7 @@ import ml.docilealligator.infinityforreddit.activities.RulesActivity; import ml.docilealligator.infinityforreddit.activities.SearchActivity; import ml.docilealligator.infinityforreddit.activities.SearchResultActivity; import ml.docilealligator.infinityforreddit.activities.SearchSubredditsResultActivity; +import ml.docilealligator.infinityforreddit.activities.SearchUsersResultActivity; import ml.docilealligator.infinityforreddit.activities.SelectUserFlairActivity; import ml.docilealligator.infinityforreddit.activities.SelectedSubredditsActivity; import ml.docilealligator.infinityforreddit.activities.SendPrivateMessageActivity; @@ -236,4 +237,6 @@ public interface AppComponent { void inject(PostFilterPreferenceActivity postFilterPreferenceActivity); void inject(PostFilterUsageListingActivity postFilterUsageListingActivity); + + void inject(SearchUsersResultActivity searchUsersResultActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index f58d010f..2b5b2141 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -692,7 +692,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect SubredditListingFragment mFragment = new SubredditListingFragment(); Bundle bundle = new Bundle(); bundle.putString(SubredditListingFragment.EXTRA_QUERY, mQuery); - bundle.putBoolean(SubredditListingFragment.EXTRA_IS_POSTING, false); + bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, false); bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); @@ -702,6 +702,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect UserListingFragment mFragment = new UserListingFragment(); Bundle bundle = new Bundle(); bundle.putString(UserListingFragment.EXTRA_QUERY, mQuery); + bundle.putBoolean(UserListingFragment.EXTRA_IS_GETTING_USER_INFO, false); bundle.putString(UserListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(UserListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java index 270e9728..9caccdae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java @@ -43,7 +43,7 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti static final String EXTRA_QUERY = "EQ"; static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; - static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIURL"; + static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIU"; private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; @@ -157,7 +157,7 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti mFragment = new SubredditListingFragment(); Bundle bundle = new Bundle(); bundle.putString(SubredditListingFragment.EXTRA_QUERY, query); - bundle.putBoolean(SubredditListingFragment.EXTRA_IS_POSTING, true); + bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, true); bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java new file mode 100644 index 00000000..23dc6fd4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java @@ -0,0 +1,200 @@ +package ml.docilealligator.infinityforreddit.activities; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.fragment.app.Fragment; + +import com.google.android.material.appbar.AppBarLayout; +import com.r0adkll.slidr.Slidr; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + +import java.util.concurrent.Executor; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.fragments.UserListingFragment; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; + +public class SearchUsersResultActivity extends BaseActivity implements ActivityToolbarInterface { + + static final String EXTRA_QUERY = "EQ"; + static final String EXTRA_RETURN_USER_NAME = "ERUN"; + static final String EXTRA_RETURN_USER_ICON_URL = "ERUIU"; + + 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 String FRAGMENT_OUT_STATE = "FOS"; + + @BindView(R.id.coordinator_layout_search_subreddits_result_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_search_subreddits_result_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_search_subreddits_result_activity) + Toolbar toolbar; + Fragment mFragment; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + @Inject + Executor mExecutor; + private boolean mNullAccessToken = false; + private String mAccessToken; + private String mAccountName; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_search_users_result); + + ButterKnife.bind(this); + + EventBus.getDefault().register(this); + + applyCustomTheme(); + + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) { + Slidr.attach(this); + } + + if (Build.VERSION.SDK_INT >= 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); -- cgit v1.2.3