From 216228cf0275d84d1895114fd92cb83a70318c81 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 18 Jul 2019 23:59:14 +0800 Subject: Add a search button in ViewSubredditDetailActivity and ViewUserDetailActivity. Search posts in a specific subreddit is available. Rename SearchActivity to SearchResultActivity. Add SearchActivity for searching instead of searching in MainActivity. --- app/src/main/AndroidManifest.xml | 7 +- .../infinityforreddit/MainActivity.java | 61 +----- .../infinityforreddit/ParseSubredditData.java | 2 +- .../infinityforreddit/PostDataSource.java | 44 +++- .../infinityforreddit/PostDataSourceFactory.java | 19 +- .../infinityforreddit/PostFragment.java | 36 +++- .../infinityforreddit/PostImageActivity.java | 6 +- .../infinityforreddit/PostLinkActivity.java | 22 +- .../infinityforreddit/PostTextActivity.java | 10 +- .../infinityforreddit/PostVideoActivity.java | 6 +- .../infinityforreddit/PostViewModel.java | 38 +++- .../infinityforreddit/RedditAPI.java | 10 +- .../infinityforreddit/SearchActivity.java | 221 +++++++-------------- .../infinityforreddit/SearchResultActivity.java | 182 +++++++++++++++++ .../SubredditSelectionActivity.java | 17 +- .../SubscribedSubredditsListingFragment.java | 7 +- .../SubscribedSubredditsRecyclerViewAdapter.java | 42 ++-- .../ViewSubredditDetailActivity.java | 19 +- .../infinityforreddit/ViewUserDetailActivity.java | 17 +- app/src/main/res/layout/activity_post_text.xml | 6 +- app/src/main/res/layout/activity_search.xml | 111 ++++++----- app/src/main/res/layout/activity_search_result.xml | 72 +++++++ app/src/main/res/menu/menu_search.xml | 5 +- app/src/main/res/menu/search_activity.xml | 12 ++ app/src/main/res/menu/search_result_activity.xml | 16 ++ .../res/menu/view_subreddit_detail_activity.xml | 10 +- .../main/res/menu/view_user_detail_activity.xml | 10 +- app/src/main/res/values/strings.xml | 3 + 28 files changed, 665 insertions(+), 346 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java create mode 100644 app/src/main/res/layout/activity_search_result.xml create mode 100644 app/src/main/res/menu/search_activity.xml create mode 100644 app/src/main/res/menu/search_result_activity.xml (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 24a79054..d77b0afe 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,6 +19,11 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> + diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index d555a456..6e4c6c99 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -23,7 +23,6 @@ import androidx.fragment.app.Fragment; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; -import com.ferfalk.simplesearchview.SimpleSearchView; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.bottomsheet.BottomSheetDialog; @@ -48,8 +47,6 @@ public class MainActivity extends AppCompatActivity { @BindView(R.id.drawer_layout) DrawerLayout drawer; @BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout; - @BindView(R.id.search_view_main_activity) SimpleSearchView simpleSearchView; - @BindView(R.id.transparent_overlay_main_activity) View transparentOverlay; @BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout; @BindView(R.id.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout; @BindView(R.id.settings_linear_layout_main_activity) LinearLayout settingsLinearLayout; @@ -117,50 +114,6 @@ public class MainActivity extends AppCompatActivity { params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); - transparentOverlay.setOnClickListener(view -> simpleSearchView.onBackPressed()); - - simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { - @Override - public boolean onQueryTextSubmit(String query) { - Intent intent = new Intent(MainActivity.this, SearchActivity.class); - intent.putExtra(SearchActivity.QUERY_KEY, query); - startActivity(intent); - return false; - } - - @Override - public boolean onQueryTextChange(String newText) { - return false; - } - - @Override - public boolean onQueryTextCleared() { - return false; - } - }); - - simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() { - @Override - public void onSearchViewShown() { - transparentOverlay.setVisibility(View.VISIBLE); - } - - @Override - public void onSearchViewClosed() { - transparentOverlay.setVisibility(View.GONE); - } - - @Override - public void onSearchViewShownAnimation() { - - } - - @Override - public void onSearchViewClosedAnimation() { - - } - }); - String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); if (accessToken.equals("")) { Intent loginIntent = new Intent(this, LoginActivity.class); @@ -169,7 +122,7 @@ public class MainActivity extends AppCompatActivity { if (savedInstanceState == null) { mFragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE); + bundle.putInt(PostFragment.EXTRA_POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE); mFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); } else { @@ -316,9 +269,6 @@ public class MainActivity extends AppCompatActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (simpleSearchView.onActivityResult(requestCode, resultCode, data)) { - return; - } if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) { Intent intent = getIntent(); finish(); @@ -333,7 +283,6 @@ public class MainActivity extends AppCompatActivity { public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_activity, menu); mMenu = menu; - simpleSearchView.setMenuItem(mMenu.findItem(R.id.action_search_main_activity)); MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity); if(isInLazyMode) { lazyModeItem.setTitle(R.string.action_stop_lazy_mode); @@ -352,6 +301,10 @@ public class MainActivity extends AppCompatActivity { public boolean onOptionsItemSelected(MenuItem item) { if (mFragment instanceof FragmentCommunicator) { switch (item.getItemId()) { + case R.id.action_search_main_activity: + Intent intent = new Intent(this, SearchActivity.class); + startActivity(intent); + return true; case R.id.action_refresh_main_activity: ((FragmentCommunicator) mFragment).refresh(); mFetchUserInfoSuccess = false; @@ -384,10 +337,6 @@ public class MainActivity extends AppCompatActivity { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { - if (simpleSearchView.onBackPressed()) { - return; - } - super.onBackPressed(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 6501b709..2e59e33a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -54,7 +54,7 @@ class ParseSubredditData { JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); mNCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY); subredditData = parseSubredditData(data); - /*String id = data.getString(JSONUtils.NAME_KEY); + /*String id = data.getString(JSONUtils.EXTRA_SUBREDDIT_NAME_KEY); String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME); String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index c70f6e5b..84bf4c88 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -27,7 +27,8 @@ class PostDataSource extends PageKeyedDataSource { private Retrofit retrofit; private String accessToken; private Locale locale; - private String name; + private String subredditName; + private String query; private int postType; private OnPostFetchedCallback onPostFetchedCallback; @@ -53,7 +54,20 @@ class PostDataSource extends PageKeyedDataSource { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; - this.name = subredditName; + this.subredditName = subredditName; + paginationNetworkStateLiveData = new MutableLiveData(); + initialLoadStateLiveData = new MutableLiveData(); + this.postType = postType; + this.onPostFetchedCallback = onPostFetchedCallback; + } + + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, + int postType, OnPostFetchedCallback onPostFetchedCallback) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.query = query; paginationNetworkStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData(); this.postType = postType; @@ -202,7 +216,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(name, null, RedditUtils.getOAuthHeader(accessToken)); + Call getPost = api.getSubredditBestPosts(subredditName, null, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -243,7 +257,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadSubredditPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); + Call getPost = api.getSubredditBestPosts(subredditName, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -277,7 +291,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadUserPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(name, lastItem, RedditUtils.getOAuthHeader(accessToken)); + Call getPost = api.getUserBestPosts(subredditName, lastItem, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -323,7 +337,7 @@ class PostDataSource extends PageKeyedDataSource { String after = lastItem == null ? params.key : lastItem; RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(name, after, RedditUtils.getOAuthHeader(accessToken)); + Call getPost = api.getUserBestPosts(subredditName, after, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -361,7 +375,14 @@ class PostDataSource extends PageKeyedDataSource { private void loadSearchPostsInitial(@NonNull final LoadInitialCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.searchPosts(name, null, RedditUtils.getOAuthHeader(accessToken)); + Call getPost; + + if(subredditName == null) { + getPost = api.searchPosts(subredditName, null, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.searchPostsInSpecificSubreddit(subredditName, query, null, RedditUtils.getOAuthHeader(accessToken)); + } + getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -402,7 +423,14 @@ class PostDataSource extends PageKeyedDataSource { private void loadSearchPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.searchPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); + Call getPost; + + if(subredditName == null) { + getPost = api.searchPosts(subredditName, params.key, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.searchPostsInSpecificSubreddit(subredditName, query, params.key, RedditUtils.getOAuthHeader(accessToken)); + } + getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 1f8d0ccc..117f3cad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -12,6 +12,7 @@ class PostDataSourceFactory extends DataSource.Factory { private String accessToken; private Locale locale; private String subredditName; + private String query; private int postType; private PostDataSource.OnPostFetchedCallback onPostFetchedCallback; @@ -28,12 +29,24 @@ class PostDataSourceFactory extends DataSource.Factory { this.onPostFetchedCallback = onPostFetchedCallback; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, + int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + postDataSourceLiveData = new MutableLiveData<>(); + this.postType = postType; + this.onPostFetchedCallback = onPostFetchedCallback; + } + + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, + String query, int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; this.subredditName = subredditName; + this.query = query; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; this.onPostFetchedCallback = onPostFetchedCallback; @@ -43,6 +56,8 @@ class PostDataSourceFactory extends DataSource.Factory { public DataSource create() { if(postType == PostDataSource.TYPE_FRONT_PAGE) { postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback); + } else if(postType == PostDataSource.TYPE_SEARCH) { + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, postType, onPostFetchedCallback); } else { postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 5f6bd33c..b00bed79 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -45,8 +45,10 @@ import retrofit2.Retrofit; */ public class PostFragment extends Fragment implements FragmentCommunicator { - static final String NAME_KEY = "NK"; - static final String POST_TYPE_KEY = "PTK"; + static final String EXTRA_SUBREDDIT_NAME_KEY = "ENK"; + static final String EXTRA_QUERY_KEY = "EQK"; + static final String EXTRA_POST_TYPE_KEY = "EPTK"; + private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; @BindView(R.id.coordinator_layout_post_fragment) CoordinatorLayout mCoordinatorLayout; @@ -163,21 +165,43 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return false; }); - int postType = getArguments().getInt(POST_TYPE_KEY); + int postType = getArguments().getInt(EXTRA_POST_TYPE_KEY); String accessToken = activity.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); PostViewModel.Factory factory; - if(postType != PostDataSource.TYPE_FRONT_PAGE) { - String name = getArguments().getString(NAME_KEY); + if(postType == PostDataSource.TYPE_SEARCH) { + String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME_KEY); + String query = getArguments().getString(EXTRA_QUERY_KEY); + + mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, + mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore()); + + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, subredditName, query, postType, new PostDataSource.OnPostFetchedCallback() { + @Override + public void hasPost() { + mFetchPostInfoLinearLayout.setVisibility(View.GONE); + } + + @Override + public void noPost() { + mFetchPostInfoLinearLayout.setOnClickListener(view -> { + //Do nothing + }); + showErrorView(R.string.no_posts); + } + }); + } else if(postType != PostDataSource.TYPE_FRONT_PAGE) { + String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME_KEY); mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, name, postType, new PostDataSource.OnPostFetchedCallback() { + getResources().getConfiguration().locale, subredditName, postType, new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 0d8a258c..6e71c523 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -411,10 +411,10 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS super.onActivityResult(requestCode, resultCode, data); if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { if(resultCode == RESULT_OK) { - subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY); - iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); subredditSelected = true; - subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER_KEY); + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); subreditNameTextView.setText(subredditName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index c0067e88..3e68a699 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -52,7 +52,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh @BindView(R.id.coordinator_layout_post_link_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.subreddit_icon_gif_image_view_post_link_activity) GifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_post_link_activity) TextView subreditNameTextView; + @BindView(R.id.subreddit_name_text_view_post_link_activity) TextView subredditNameTextView; @BindView(R.id.rules_button_post_link_activity) Button rulesButton; @BindView(R.id.flair_custom_text_view_post_link_activity) CustomTextView flairTextView; @BindView(R.id.spoiler_custom_text_view_post_link_activity) CustomTextView spoilerTextView; @@ -109,7 +109,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); if(subredditName != null) { - subreditNameTextView.setText(subredditName); + subredditNameTextView.setText(subredditName); flairTextView.setVisibility(View.VISIBLE); if(!loadSubredditIconSuccessful) { loadSubredditIcon(); @@ -130,7 +130,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } else { if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME); - subreditNameTextView.setText(subredditName); + subredditNameTextView.setText(subredditName); flairTextView.setVisibility(View.VISIBLE); loadSubredditIcon(); } else { @@ -145,7 +145,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); }); - subreditNameTextView.setOnClickListener(view -> { + subredditNameTextView.setOnClickListener(view -> { Intent intent = new Intent(this, SubredditSelectionActivity.class); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); }); @@ -247,9 +247,9 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh String subredditName; if(subredditIsUser) { - subredditName = "u_" + subreditNameTextView.getText().toString(); + subredditName = "u_" + subredditNameTextView.getText().toString(); } else { - subredditName = subreditNameTextView.getText().toString(); + subredditName = subredditNameTextView.getText().toString(); } SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName, @@ -299,13 +299,13 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh super.onActivityResult(requestCode, resultCode, data); if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { if(resultCode == RESULT_OK) { - subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY); - iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); subredditSelected = true; - subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER_KEY); + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); - subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); - subreditNameTextView.setText(subredditName); + subredditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); + subredditNameTextView.setText(subredditName); displaySubredditIcon(); flairTextView.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 828f556c..701c0e8d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -51,8 +51,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @BindView(R.id.coordinator_layout_post_text_activity) CoordinatorLayout coordinatorLayout; - @BindView(R.id.subreddit_icon_gif_image_view_post_text_activity) GifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_post_text_activity) TextView subreditNameTextView; + @BindView(R.id.subreddit_icon_gif_image_view_search_activity) GifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_search_activity) TextView subreditNameTextView; @BindView(R.id.rules_button_post_text_activity) Button rulesButton; @BindView(R.id.flair_custom_text_view_post_text_activity) CustomTextView flairTextView; @BindView(R.id.spoiler_custom_text_view_post_text_activity) CustomTextView spoilerTextView; @@ -304,10 +304,10 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh super.onActivityResult(requestCode, resultCode, data); if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { if(resultCode == RESULT_OK) { - subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY); - iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); subredditSelected = true; - subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER_KEY); + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); subreditNameTextView.setText(subredditName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 23214037..e4ccbfe1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -392,10 +392,10 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS super.onActivityResult(requestCode, resultCode, data); if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { if(resultCode == RESULT_OK) { - subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY); - iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); subredditSelected = true; - subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER_KEY); + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); subreditNameTextView.setText(subredditName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index ab622d09..21e7d03d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -1,7 +1,5 @@ package ml.docilealligator.infinityforreddit; -import java.util.Locale; - import androidx.annotation.NonNull; import androidx.arch.core.util.Function; import androidx.lifecycle.LiveData; @@ -10,6 +8,9 @@ import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; + +import java.util.Locale; + import retrofit2.Retrofit; public class PostViewModel extends ViewModel { @@ -53,6 +54,25 @@ public class PostViewModel extends ViewModel { posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); } + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, + int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, + query, postType, onPostFetchedCallback); + + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + dataSource -> dataSource.getInitialLoadStateLiveData()); + paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + dataSource -> dataSource.getPaginationNetworkStateLiveData()); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + } + LiveData> getPosts() { return posts; } @@ -82,6 +102,7 @@ public class PostViewModel extends ViewModel { private String accessToken; private Locale locale; private String subredditName; + private String query; private int postType; private PostDataSource.OnPostFetchedCallback onPostFetchedCallback; @@ -104,11 +125,24 @@ public class PostViewModel extends ViewModel { this.onPostFetchedCallback = onPostFetchedCallback; } + public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, + int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.query = query; + this.postType = postType; + this.onPostFetchedCallback = onPostFetchedCallback; + } + @NonNull @Override public T create(@NonNull Class modelClass) { if(postType == PostDataSource.TYPE_FRONT_PAGE) { return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback); + } else if(postType == PostDataSource.TYPE_SEARCH){ + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, postType, onPostFetchedCallback); } else { return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 75e99f7e..4efc649f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -65,16 +65,16 @@ public interface RedditAPI { @GET("subreddits/search.json?raw_json=1&include_over_18=on") Call searchSubreddits(@Query("q") String subredditName, @Query("after") String after); - @GET("search.json?raw_json=1&type=user") + @GET("search.json?raw_json=1&type=user&include_over_18=on") Call searchUsers(@Query("q") String profileName, @Query("after") String after); - @GET("search.json?raw_json=1&type=link") + @GET("search.json?raw_json=1&type=link&include_over_18=on") Call searchPosts(@Query("q") String query, @Query("after") String after, @HeaderMap Map headers); - @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true") - Call searchPostsInSpecificSubreddit(@Query("q") String query, @Query("after") String after, - @HeaderMap Map headers); + @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on") + Call searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, @Query("q") String query, + @Query("after") String after, @HeaderMap Map headers); @FormUrlEncoded @POST("api/comment") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java index 38147cb7..3600bd27 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java @@ -4,35 +4,36 @@ import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; +import android.widget.RelativeLayout; +import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentPagerAdapter; -import androidx.viewpager.widget.ViewPager; import com.ferfalk.simplesearchview.SimpleSearchView; -import com.google.android.material.tabs.TabLayout; import butterknife.BindView; import butterknife.ButterKnife; public class SearchActivity extends AppCompatActivity { - static final String QUERY_KEY = "QK"; - private String mQuery; + static final String EXTRA_SUBREDDIT_NAME = "ESN"; + static final String EXTRA_SUBREDDIT_IS_USER = "ESIU"; - @BindView(R.id.toolbar_search_activity) Toolbar toolbar; + private static final String SUBREDDIT_NAME_STATE = "SNS"; + private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; + + private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; + + @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.search_view_search_activity) SimpleSearchView simpleSearchView; - @BindView(R.id.tab_layout_search_activity) TabLayout tabLayout; - @BindView(R.id.transparent_overlay_search_activity) View transparentOverlay; - @BindView(R.id.view_pager_search_activity) ViewPager viewPager; + @BindView(R.id.subreddit_name_relative_layout_search_activity) RelativeLayout subredditNameRelativeLayout; + @BindView(R.id.subreddit_name_text_view_search_activity) TextView subredditNameTextView; - private SectionsPagerAdapter sectionsPagerAdapter; + private String subredditName; + private boolean subredditIsUser; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,23 +43,22 @@ public class SearchActivity extends AppCompatActivity { ButterKnife.bind(this); setSupportActionBar(toolbar); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - viewPager.setAdapter(sectionsPagerAdapter); - viewPager.setOffscreenPageLimit(2); - tabLayout.setupWithViewPager(viewPager); - - transparentOverlay.setOnClickListener(view -> simpleSearchView.onBackPressed()); simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { - Intent intent = getIntent(); - intent.putExtra(SearchActivity.QUERY_KEY, query); + Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class); + intent.putExtra(SearchResultActivity.EXTRA_QUERY, query); + if(subredditName != null) { + if(subredditIsUser) { + intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); + } else { + intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, subredditName); + } + } + finish(); startActivity(intent); - overridePendingTransition(0, 0); return false; } @@ -73,66 +73,68 @@ public class SearchActivity extends AppCompatActivity { } }); - simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() { - @Override - public void onSearchViewShown() { - transparentOverlay.setVisibility(View.VISIBLE); - } - - @Override - public void onSearchViewClosed() { - transparentOverlay.setVisibility(View.GONE); - } - - @Override - public void onSearchViewShownAnimation() { + if(savedInstanceState != null) { + subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); + subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); + if(subredditName == null) { + subredditNameTextView.setText(R.string.all_subreddits); + } else { + subredditNameTextView.setText(subredditName); } + } - @Override - public void onSearchViewClosedAnimation() { - - } + subredditNameRelativeLayout.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + intent.putExtra(SubredditSelectionActivity.EXTRA_EXTRA_CLEAR_SELECTION, true); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); }); - // Get the intent, verify the action and get the query Intent intent = getIntent(); - String query = intent.getExtras().getString(QUERY_KEY); - if(query != null) { - mQuery = query; - setTitle(query); + if(intent.hasExtra(EXTRA_SUBREDDIT_NAME)) { + subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); + subredditNameTextView.setText(subredditName); + subredditIsUser = intent.getExtras().getBoolean(EXTRA_SUBREDDIT_IS_USER); } } @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (simpleSearchView.onActivityResult(requestCode, resultCode, data)) { return; } + if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { + if(resultCode == RESULT_OK) { + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); + + if(subredditName == null) { + subredditNameTextView.setText(R.string.all_subreddits); + } else { + subredditNameTextView.setText(subredditName); + } + } + } + super.onActivityResult(requestCode, resultCode, data); } @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.main_activity, menu); - - simpleSearchView.setMenuItem(menu.findItem(R.id.action_search_main_activity)); - + getMenuInflater().inflate(R.menu.search_activity, menu); + simpleSearchView.setMenuItem(menu.findItem(R.id.action_search_search_activity)); return true; } @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - onBackPressed(); - return true; - case R.id.action_refresh_main_activity: - sectionsPagerAdapter.refresh(); - return true; + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if(item.getItemId() == android.R.id.home) { + finish(); + return true; } - return super.onOptionsItemSelected(item); + + return false; } @Override @@ -143,97 +145,10 @@ public class SearchActivity extends AppCompatActivity { super.onBackPressed(); } - private class SectionsPagerAdapter extends FragmentPagerAdapter { - private PostFragment postFragment; - private SubredditListingFragment subredditListingFragment; - private UserListingFragment userListingFragment; - - public SectionsPagerAdapter(FragmentManager fm) { - super(fm); - } - - @NonNull - @Override - public Fragment getItem(int position) { - switch (position) { - case 0: { - PostFragment mFragment = new PostFragment(); - Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_SEARCH); - bundle.putString(PostFragment.NAME_KEY, mQuery); - mFragment.setArguments(bundle); - return mFragment; - } - case 1: { - SubredditListingFragment mFragment = new SubredditListingFragment(); - Bundle bundle = new Bundle(); - bundle.putString(SubredditListingFragment.QUERY_KEY, mQuery); - mFragment.setArguments(bundle); - return mFragment; - } - default: - { - UserListingFragment mFragment = new UserListingFragment(); - Bundle bundle = new Bundle(); - bundle.putString(UserListingFragment.QUERY_KEY, mQuery); - mFragment.setArguments(bundle); - return mFragment; - } - } - } - - @Override - public int getCount() { - return 3; - } - - @Override - public CharSequence getPageTitle(int position) { - switch (position) { - case 0: - return "Posts"; - case 1: - return "Subreddits"; - case 2: - return "Users"; - } - return null; - } - - @NonNull - @Override - public Object instantiateItem(@NonNull ViewGroup container, int position) { - Fragment fragment = (Fragment) super.instantiateItem(container, position); - switch (position) { - case 0: - postFragment = (PostFragment) fragment; - break; - case 1: - subredditListingFragment = (SubredditListingFragment) fragment; - break; - case 2: - userListingFragment = (UserListingFragment) fragment; - break; - } - return fragment; - } - - public void refresh() { - if(postFragment != null) { - ((FragmentCommunicator) postFragment).refresh(); - } - if(subredditListingFragment != null) { - ((FragmentCommunicator) subredditListingFragment).refresh(); - } - if (userListingFragment != null) { - ((FragmentCommunicator) userListingFragment).refresh(); - } - } - - public void newSearch() { - getItem(0); - getItem(1); - getItem(2); - } + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(SUBREDDIT_NAME_STATE, subredditName); + outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java new file mode 100644 index 00000000..d9421af8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -0,0 +1,182 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Intent; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.ViewPager; + +import com.google.android.material.tabs.TabLayout; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class SearchResultActivity extends AppCompatActivity { + static final String EXTRA_QUERY = "QK"; + static final String EXTRA_SUBREDDIT_NAME = "ESN"; + + private String mQuery; + private String mSubredditName; + + @BindView(R.id.toolbar_search_result_activity) Toolbar toolbar; + @BindView(R.id.tab_layout_search_result_activity) TabLayout tabLayout; + @BindView(R.id.view_pager_search_result_activity) ViewPager viewPager; + + private SectionsPagerAdapter sectionsPagerAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_search_result); + + ButterKnife.bind(this); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + viewPager.setAdapter(sectionsPagerAdapter); + viewPager.setOffscreenPageLimit(2); + tabLayout.setupWithViewPager(viewPager); + + // Get the intent, verify the action and get the query + Intent intent = getIntent(); + String query = intent.getExtras().getString(EXTRA_QUERY); + + if(intent.hasExtra(EXTRA_SUBREDDIT_NAME)) { + mSubredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); + } + + if(query != null) { + mQuery = query; + setTitle(query); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.search_result_activity, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + return true; + case R.id.action_search_search_result_activity: + Intent intent = new Intent(this, SearchActivity.class); + finish(); + startActivity(intent); + return true; + case R.id.action_refresh_search_result_activity: + sectionsPagerAdapter.refresh(); + return true; + } + return super.onOptionsItemSelected(item); + } + + private class SectionsPagerAdapter extends FragmentPagerAdapter { + private PostFragment postFragment; + private SubredditListingFragment subredditListingFragment; + private UserListingFragment userListingFragment; + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @NonNull + @Override + public Fragment getItem(int position) { + switch (position) { + case 0: { + PostFragment mFragment = new PostFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.EXTRA_POST_TYPE_KEY, PostDataSource.TYPE_SEARCH); + bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME_KEY, mSubredditName); + bundle.putString(PostFragment.EXTRA_QUERY_KEY, mQuery); + mFragment.setArguments(bundle); + return mFragment; + } + case 1: { + SubredditListingFragment mFragment = new SubredditListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(SubredditListingFragment.QUERY_KEY, mQuery); + mFragment.setArguments(bundle); + return mFragment; + } + default: + { + UserListingFragment mFragment = new UserListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(UserListingFragment.QUERY_KEY, mQuery); + mFragment.setArguments(bundle); + return mFragment; + } + } + } + + @Override + public int getCount() { + return 3; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: + return "Posts"; + case 1: + return "Subreddits"; + case 2: + return "Users"; + } + return null; + } + + @NonNull + @Override + public Object instantiateItem(@NonNull ViewGroup container, int position) { + Fragment fragment = (Fragment) super.instantiateItem(container, position); + switch (position) { + case 0: + postFragment = (PostFragment) fragment; + break; + case 1: + subredditListingFragment = (SubredditListingFragment) fragment; + break; + case 2: + userListingFragment = (UserListingFragment) fragment; + break; + } + return fragment; + } + + public void refresh() { + if(postFragment != null) { + ((FragmentCommunicator) postFragment).refresh(); + } + if(subredditListingFragment != null) { + ((FragmentCommunicator) subredditListingFragment).refresh(); + } + if (userListingFragment != null) { + ((FragmentCommunicator) userListingFragment).refresh(); + } + } + + public void newSearch() { + getItem(0); + getItem(1); + getItem(2); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 7e8e4d7d..8dce836d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -14,9 +14,10 @@ import butterknife.ButterKnife; public class SubredditSelectionActivity extends AppCompatActivity { - static final String EXTRA_RETURN_SUBREDDIT_NAME_KEY = "ERSNK"; - static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIURLK"; - static final String EXTRA_RETURN_SUBREDDIT_IS_USER_KEY = "ERSIUK"; + static final String EXTRA_EXTRA_CLEAR_SELECTION = "EECS"; + static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; + static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIURL"; + static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -32,6 +33,10 @@ public class SubredditSelectionActivity extends AppCompatActivity { SubscribedSubredditsListingFragment fragment = 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)); + } fragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, fragment).commit(); } @@ -49,9 +54,9 @@ public class SubredditSelectionActivity extends AppCompatActivity { void getSelectedSubreddit(String name, String iconUrl, boolean subredditIsUser) { Intent returnIntent = new Intent(); - returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME_KEY, name); - returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY, iconUrl); - returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_IS_USER_KEY, subredditIsUser); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL, iconUrl); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_IS_USER, subredditIsUser); setResult(Activity.RESULT_OK, returnIntent); finish(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index c6e840a9..1e7304c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -31,7 +31,8 @@ import butterknife.ButterKnife; */ public class SubscribedSubredditsListingFragment extends Fragment { - static final String EXTRA_IS_SUBREDDIT_SELECTION = "EISSK"; + static final String EXTRA_IS_SUBREDDIT_SELECTION = "EISS"; + static final String EXTRA_EXTRA_CLEAR_SELECTION = "EECS"; @BindView(R.id.recycler_view_subscribed_subreddits_listing_fragment) RecyclerView mRecyclerView; @BindView(R.id.no_subscriptions_linear_layout_subreddits_listing_fragment) LinearLayout mLinearLayout; @@ -43,8 +44,6 @@ public class SubscribedSubredditsListingFragment extends Fragment { private SubscribedSubredditViewModel mSubscribedSubredditViewModel; - private boolean mInsertSuccess = false; - @Inject @Named("user_info") SharedPreferences sharedPreferences; @@ -73,7 +72,7 @@ public class SubscribedSubredditsListingFragment extends Fragment { SubscribedSubredditsRecyclerViewAdapter adapter; if(getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) { - adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, + adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, getArguments().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION), (name, iconUrl, subredditIsUser) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl, subredditIsUser)); } else { adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java index 7126213d..6f9288cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java @@ -28,6 +28,7 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter itemClickListener.onClick(null, null, false)); + return; + } else if(viewHolder.getAdapterPosition() == 1) { + name = username; + iconUrl = userIconUrl; + viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true)); + } else { + name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getName(); + iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getIconUrl(); + viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false)); + } } else { - name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName(); - iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl(); - } - - viewHolder.itemView.setOnClickListener(view -> { if(viewHolder.getAdapterPosition() == 0) { - itemClickListener.onClick(name, iconUrl, true); + name = username; + iconUrl = userIconUrl; + viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true)); } else { - itemClickListener.onClick(name, iconUrl, false); + name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName(); + iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl(); + viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false)); } - }); + } } else { name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName(); iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getIconUrl(); @@ -100,7 +112,11 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter + android:layout_height="match_parent" + tools:context=".SearchActivity"> - + android:layout_height="match_parent" + app:titleEnabled="false" + app:toolbarId="@+id/toolbar"> - + android:layout_height="wrap_content"> - + - + - + + + - + app:layout_behavior="@string/appbar_scrolling_view_behavior"> - + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_search_result.xml b/app/src/main/res/layout/activity_search_result.xml new file mode 100644 index 00000000..062ca4a0 --- /dev/null +++ b/app/src/main/res/layout/activity_search_result.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_search.xml b/app/src/main/res/menu/menu_search.xml index afd34627..f0cff4fd 100644 --- a/app/src/main/res/menu/menu_search.xml +++ b/app/src/main/res/menu/menu_search.xml @@ -1,10 +1,11 @@ + tools:context="ml.docilealligator.infinityforreddit.SearchResultActivity"> + android:icon="@drawable/ic_search_white_24dp" + app:showAsAction="ifRoom" /> diff --git a/app/src/main/res/menu/search_activity.xml b/app/src/main/res/menu/search_activity.xml new file mode 100644 index 00000000..c4820b38 --- /dev/null +++ b/app/src/main/res/menu/search_activity.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/menu/search_result_activity.xml b/app/src/main/res/menu/search_result_activity.xml new file mode 100644 index 00000000..1fe4fd88 --- /dev/null +++ b/app/src/main/res/menu/search_result_activity.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/view_subreddit_detail_activity.xml b/app/src/main/res/menu/view_subreddit_detail_activity.xml index ede3e6b9..41e83e7d 100644 --- a/app/src/main/res/menu/view_subreddit_detail_activity.xml +++ b/app/src/main/res/menu/view_subreddit_detail_activity.xml @@ -4,15 +4,21 @@ xmlns:tools="http://schemas.android.com/tools" tools:context="ml.docilealligator.infinityforreddit.ViewSubredditDetailActivity"> + \ No newline at end of file diff --git a/app/src/main/res/menu/view_user_detail_activity.xml b/app/src/main/res/menu/view_user_detail_activity.xml index 6f550333..58e469e9 100644 --- a/app/src/main/res/menu/view_user_detail_activity.xml +++ b/app/src/main/res/menu/view_user_detail_activity.xml @@ -3,15 +3,21 @@ xmlns:tools="http://schemas.android.com/tools" tools:context="ml.docilealligator.infinityforreddit.ViewUserDetailActivity"> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 005aa048..ba9c2924 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -128,4 +128,7 @@ No rule Error loading rules.\nTap to retry. + + Search in + All subreddits -- cgit v1.2.3