diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-05 02:30:22 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-05 02:30:22 +0000 |
commit | 0d1fcecde60bbb1eb3b43736807883b12e54b2e8 (patch) | |
tree | d796ee393dfd4ea3c0f09232db38478125459cc6 /app/src/main/java | |
parent | 44e2987a848ec07fd866f8ed76bb29c66b03e60a (diff) | |
download | infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.tar infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.tar.gz infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.tar.bz2 infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.tar.lz infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.tar.xz infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.tar.zst infinity-for-reddit-0d1fcecde60bbb1eb3b43736807883b12e54b2e8.zip |
Searching subreddits to post is available. Use Toolbar instead of ActionBar in various activities. Minor bugs fixed.
Diffstat (limited to '')
14 files changed, 239 insertions, 74 deletions
diff --git a/app/src/main/java/CustomView/CustomMarkwonView.java b/app/src/main/java/CustomView/CustomMarkwonView.java index c0d59f05..50ac4bbf 100644 --- a/app/src/main/java/CustomView/CustomMarkwonView.java +++ b/app/src/main/java/CustomView/CustomMarkwonView.java @@ -24,23 +24,24 @@ public class CustomMarkwonView extends MarkwonView { super(context, attrs); } - public void setMarkdown(@Nullable String markdown, Context conte) { - SpannableConfiguration configuration = SpannableConfiguration.builder(conte).linkResolver((view, link) -> { + public void setMarkdown(@Nullable String markdown, Context context) { + SpannableConfiguration configuration = SpannableConfiguration.builder(context).linkResolver((view, link) -> { if(link.startsWith("/u/") || link.startsWith("u/")) { - Intent intent = new Intent(conte, ViewUserDetailActivity.class); + Intent intent = new Intent(context, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3)); - conte.startActivity(intent); + context.startActivity(intent); } else if(link.startsWith("/r/") || link.startsWith("r/")) { - Intent intent = new Intent(conte, ViewSubredditDetailActivity.class); + Intent intent = new Intent(context, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3)); - conte.startActivity(intent); + context.startActivity(intent); } else { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); // add share action to menu list builder.addDefaultShareMenuItem(); - builder.setToolbarColor(conte.getResources().getColor(R.color.colorPrimary)); + builder.setToolbarColor(context.getResources().getColor(R.color.colorPrimary)); CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(conte, Uri.parse(link)); + customTabsIntent.intent.setPackage(context.getPackageName()); + customTabsIntent.launchUrl(context, Uri.parse(link)); } }).build(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 3810ed96..64662e0a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -300,6 +300,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe return true; case R.id.action_search_main_activity: Intent intent = new Intent(this, SearchActivity.class); + intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, false); startActivity(intent); return true; case R.id.action_refresh_main_activity: diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index cd4e84dc..212f3fa5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -19,8 +19,8 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.content.FileProvider; @@ -69,6 +69,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS private static final int CAPTURE_IMAGE_REQUEST_CODE = 2; @BindView(R.id.coordinator_layout_post_image_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.toolbar_post_image_activity) Toolbar toolbar; @BindView(R.id.subreddit_icon_gif_image_view_post_image_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_name_text_view_post_image_activity) TextView subreditNameTextView; @BindView(R.id.rules_button_post_image_activity) Button rulesButton; @@ -126,9 +127,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS ((Infinity) getApplication()).getmAppComponent().inject(this); - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index af507488..6c240293 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -13,8 +12,8 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import com.bumptech.glide.Glide; @@ -51,6 +50,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @BindView(R.id.coordinator_layout_post_link_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.toolbar_post_link_activity) Toolbar toolbar; @BindView(R.id.subreddit_icon_gif_image_view_post_link_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_name_text_view_post_link_activity) TextView subredditNameTextView; @BindView(R.id.rules_button_post_link_activity) Button rulesButton; @@ -95,9 +95,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh ((Infinity) getApplication()).getmAppComponent().inject(this); - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 1f4abf27..e5601697 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -13,8 +12,8 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import com.bumptech.glide.Glide; @@ -51,6 +50,7 @@ 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.toolbar_post_text_activity) Toolbar toolbar; @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; @@ -95,9 +95,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh ((Infinity) getApplication()).getmAppComponent().inject(this); - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index e57a65c4..349d1c46 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -19,8 +19,8 @@ import android.widget.VideoView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout; @@ -66,6 +66,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS private static final int CAPTURE_VIDEO_REQUEST_CODE = 2; @BindView(R.id.coordinator_layout_post_video_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.toolbar_post_video_activity) Toolbar toolbar; @BindView(R.id.subreddit_icon_gif_image_view_post_video_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_name_text_view_post_video_activity) TextView subreditNameTextView; @BindView(R.id.rules_button_post_video_activity) Button rulesButton; @@ -127,9 +128,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS ((Infinity) getApplication()).getmAppComponent().inject(this); - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java index 113b9835..e0bedcdf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java @@ -1,9 +1,11 @@ package ml.docilealligator.infinityforreddit; +import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; +import android.view.View; import android.view.WindowManager; import android.widget.RelativeLayout; import android.widget.TextView; @@ -22,11 +24,15 @@ public class SearchActivity extends AppCompatActivity { static final String EXTRA_SUBREDDIT_NAME = "ESN"; static final String EXTRA_SUBREDDIT_IS_USER = "ESIU"; + static final String EXTRA_SEARCH_ONLY_SUBREDDITS = "ESOS"; + static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; + static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIURL"; 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; + private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 1; @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.search_view_search_activity) SimpleSearchView simpleSearchView; @@ -45,6 +51,8 @@ public class SearchActivity extends AppCompatActivity { setSupportActionBar(toolbar); + boolean searchOnlySubreddits = getIntent().getExtras().getBoolean(EXTRA_SEARCH_ONLY_SUBREDDITS); + simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() { @Override public void onSearchViewShown() { @@ -70,19 +78,24 @@ public class SearchActivity extends AppCompatActivity { simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String 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); + if(searchOnlySubreddits) { + Intent intent = new Intent(SearchActivity.this, SearchSubredditsResultActivity.class); + intent.putExtra(SearchSubredditsResultActivity.EXTRA_QUERY, query); + startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); + } else { + 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); + } } + startActivity(intent); + finish(); } - - finish(); - startActivity(intent); - return false; + return true; } @Override @@ -107,11 +120,15 @@ public class SearchActivity extends AppCompatActivity { } } - subredditNameRelativeLayout.setOnClickListener(view -> { - Intent intent = new Intent(this, SubredditSelectionActivity.class); - intent.putExtra(SubredditSelectionActivity.EXTRA_EXTRA_CLEAR_SELECTION, true); - startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); - }); + if(searchOnlySubreddits) { + subredditNameRelativeLayout.setVisibility(View.GONE); + } else { + subredditNameRelativeLayout.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + intent.putExtra(SubredditSelectionActivity.EXTRA_EXTRA_CLEAR_SELECTION, true); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + } Intent intent = getIntent(); if(intent.hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -146,6 +163,16 @@ public class SearchActivity extends AppCompatActivity { subredditNameTextView.setText(subredditName); } } + } else if(requestCode == SUBREDDIT_SEARCH_REQUEST_CODE) { + if(resultCode == RESULT_OK) { + String name = data.getExtras().getString(SearchSubredditsResultActivity.EXTRA_RETURN_SUBREDDIT_NAME); + String iconUrl = data.getExtras().getString(SearchSubredditsResultActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL, iconUrl); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } } super.onActivityResult(requestCode, resultCode, data); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index a63f1de2..c8010021 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -149,7 +149,8 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos case 1: { SubredditListingFragment mFragment = new SubredditListingFragment(); Bundle bundle = new Bundle(); - bundle.putString(SubredditListingFragment.QUERY_KEY, mQuery); + bundle.putString(SubredditListingFragment.EXTRA_QUERY_KEY, mQuery); + bundle.putBoolean(SubredditListingFragment.EXTRA_IS_POSTING, false); mFragment.setArguments(bundle); return mFragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java new file mode 100644 index 00000000..e8029bf9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java @@ -0,0 +1,68 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.Fragment; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class SearchSubredditsResultActivity extends AppCompatActivity { + + static final String EXTRA_QUERY = "EQ"; + static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; + static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIURL"; + + private static final String FRAGMENT_OUT_STATE = "FOS"; + + @BindView(R.id.toolbar_search_subreddits_result_activity) Toolbar toolbar; + + Fragment mFragment; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_search_subreddits_result); + + ButterKnife.bind(this); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + String query = getIntent().getExtras().getString(EXTRA_QUERY); + + if(savedInstanceState == null) { + mFragment = new SubredditListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(SubredditListingFragment.EXTRA_QUERY_KEY, query); + bundle.putBoolean(SubredditListingFragment.EXTRA_IS_POSTING, true); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); + } else { + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); + + } + } + + void getSelectedSubreddit(String name, String iconUrl) { + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL, iconUrl); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + if (mFragment != null) { + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java index 2168bddd..7c250cdf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java @@ -1,6 +1,8 @@ package ml.docilealligator.infinityforreddit; +import android.app.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; @@ -33,7 +35,8 @@ import retrofit2.Retrofit; */ public class SubredditListingFragment extends Fragment implements FragmentCommunicator { - static final String QUERY_KEY = "QK"; + static final String EXTRA_QUERY_KEY = "EQK"; + static final String EXTRA_IS_POSTING = "EIP"; @BindView(R.id.coordinator_layout_subreddit_listing_fragment) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.recycler_view_subreddit_listing_fragment) RecyclerView mSubredditListingRecyclerView; @@ -44,8 +47,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun private LinearLayoutManager mLinearLayoutManager; - private String mQuery; - private SubredditListingRecyclerViewAdapter mAdapter; SubredditListingViewModel mSubredditListingViewModel; @@ -70,16 +71,19 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_subreddit_listing, container, false); - ((Infinity) getActivity().getApplication()).getmAppComponent().inject(this); + Activity activity = getActivity(); + + ((Infinity) activity.getApplication()).getmAppComponent().inject(this); ButterKnife.bind(this, rootView); mLinearLayoutManager = new LinearLayoutManager(getActivity()); mSubredditListingRecyclerView.setLayoutManager(mLinearLayoutManager); - mQuery = getArguments().getString(QUERY_KEY); + String query = getArguments().getString(EXTRA_QUERY_KEY); + boolean isPosting = getArguments().getBoolean(EXTRA_IS_POSTING); - SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, mQuery, + SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query, PostDataSource.SORT_TYPE_RELEVANCE, new SubredditListingDataSource.OnSubredditListingDataFetchedCallback() { @Override public void hasSubreddit() { @@ -98,7 +102,23 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mAdapter = new SubredditListingRecyclerViewAdapter(getActivity(), mOauthRetrofit, mRetrofit, mAuthInfoSharedPreferences, SubscribedSubredditRoomDatabase.getDatabase(getContext()).subscribedSubredditDao(), - () -> mSubredditListingViewModel.retryLoadingMore()); + new SubredditListingRecyclerViewAdapter.Callback() { + @Override + public void retryLoadingMore() { + mSubredditListingViewModel.retryLoadingMore(); + } + + @Override + public void subredditSelected(String subredditName, String iconUrl) { + if(isPosting) { + ((SearchSubredditsResultActivity) activity).getSelectedSubreddit(subredditName, iconUrl); + } else { + Intent intent = new Intent(activity, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName); + activity.startActivity(intent); + } + } + }); mSubredditListingRecyclerView.setAdapter(mAdapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java index 4de52062..d18e48b6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.view.LayoutInflater; import android.view.View; @@ -31,8 +30,9 @@ import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<SubredditData, RecyclerView.ViewHolder> { - interface RetryLoadingMoreCallback { + interface Callback { void retryLoadingMore(); + void subredditSelected(String subredditName, String iconUrl); } private RequestManager glide; @@ -48,19 +48,19 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred private SubscribedSubredditDao subscribedSubredditDao; private NetworkState networkState; - private RetryLoadingMoreCallback retryLoadingMoreCallback; + private Callback callback; SubredditListingRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit, SharedPreferences authInfoSharedPreferences, SubscribedSubredditDao subscribedSubredditDao, - RetryLoadingMoreCallback retryLoadingMoreCallback) { + Callback callback) { super(DIFF_CALLBACK); this.context = context; this.oauthRetrofit = oauthRetrofit; this.retrofit = retrofit; this.authInfoSharedPreferences = authInfoSharedPreferences; this.subscribedSubredditDao = subscribedSubredditDao; - this.retryLoadingMoreCallback = retryLoadingMoreCallback; + this.callback = callback; glide = Glide.with(context.getApplicationContext()); } @@ -95,11 +95,8 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { if(holder instanceof DataViewHolder) { SubredditData subredditData = getItem(position); - ((DataViewHolder) holder).constraintLayout.setOnClickListener(view -> { - Intent intent = new Intent(context, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); - context.startActivity(intent); - }); + ((DataViewHolder) holder).constraintLayout.setOnClickListener(view -> + callback.subredditSelected(subredditData.getName(), subredditData.getIconUrl())); if(!subredditData.getIconUrl().equals("")) { glide.load(subredditData.getIconUrl()) @@ -207,7 +204,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred ErrorViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); - retryButton.setOnClickListener(view -> retryLoadingMoreCallback.retryLoadingMore()); + retryButton.setOnClickListener(view -> callback.retryLoadingMore()); errorTextView.setText(R.string.load_comments_failed); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 8dce836d..4367d19e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -2,14 +2,17 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Intent; -import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.view.Menu; import android.view.MenuItem; import androidx.annotation.NonNull; -import androidx.appcompat.app.ActionBar; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.Fragment; +import butterknife.BindView; import butterknife.ButterKnife; public class SubredditSelectionActivity extends AppCompatActivity { @@ -19,6 +22,13 @@ public class SubredditSelectionActivity extends AppCompatActivity { static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIURL"; static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU"; + private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 0; + private static final String FRAGMENT_OUT_STATE = "FOS"; + + @BindView(R.id.toolbar_subreddit_selection_activity) Toolbar toolbar; + + private Fragment mFragment; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -26,19 +36,29 @@ public class SubredditSelectionActivity extends AppCompatActivity { ButterKnife.bind(this); - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); - 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)); + 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(); + } else { + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); } - fragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, fragment).commit(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.subreddit_selection_activity, menu); + return true; } @Override @@ -47,6 +67,11 @@ public class SubredditSelectionActivity extends AppCompatActivity { case android.R.id.home: finish(); return true; + case R.id.action_search_subreddit_selection_activity: + Intent intent = new Intent(this, SearchActivity.class); + intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, true); + startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); + return true; } return false; @@ -60,4 +85,29 @@ public class SubredditSelectionActivity extends AppCompatActivity { setResult(Activity.RESULT_OK, returnIntent); finish(); } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + if(requestCode == SUBREDDIT_SEARCH_REQUEST_CODE) { + if(resultCode == RESULT_OK) { + String name = data.getExtras().getString(SearchActivity.EXTRA_RETURN_SUBREDDIT_NAME); + String iconUrl = data.getExtras().getString(SearchActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL, iconUrl); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_IS_USER, false); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } + } + super.onActivityResult(requestCode, resultCode, data); + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + if (mFragment != null) { + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index fb5664ad..4b9a0984 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -312,6 +312,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So Intent intent = new Intent(this, SearchActivity.class); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_IS_USER, false); + intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, false); startActivity(intent); break; case R.id.action_refresh_view_subreddit_detail_activity: diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index b5c11f1c..47c4faee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -328,6 +328,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { Intent intent = new Intent(this, SearchActivity.class); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, userName); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_IS_USER, true); + intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, false); startActivity(intent); break; case R.id.action_refresh_view_user_detail_activity: |