From 1fc84239069fa19da7f7c7b0b0825717aad4999c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 26 Jul 2019 10:50:32 +0800 Subject: Sorting posts is now available. Minor bugs fixed. --- .../infinityforreddit/MainActivity.java | 59 +++++--- .../infinityforreddit/ParseComment.java | 3 +- .../infinityforreddit/ParsePost.java | 21 +-- .../infinityforreddit/PostDataSource.java | 162 ++++++++++++++------- .../infinityforreddit/PostDataSourceFactory.java | 25 +++- .../infinityforreddit/PostFragment.java | 39 +++-- .../infinityforreddit/PostImageActivity.java | 1 + .../infinityforreddit/PostVideoActivity.java | 23 ++- .../infinityforreddit/PostViewModel.java | 50 ++++++- .../infinityforreddit/RedditAPI.java | 10 +- .../infinityforreddit/SearchResultActivity.java | 1 + .../SortTypeBottomSheetFragment.java | 88 +++++++++++ .../ViewSubredditDetailActivity.java | 33 ++++- 13 files changed, 393 insertions(+), 122 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 7df67fd1..efb651a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -37,7 +37,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback { private static final String FRAGMENT_OUT_STATE = "FOS"; private static final String FETCH_USER_INFO_STATE = "FUIS"; @@ -52,8 +52,6 @@ public class MainActivity extends AppCompatActivity { @BindView(R.id.settings_linear_layout_main_activity) LinearLayout settingsLinearLayout; @BindView(R.id.fab_main_activity) FloatingActionButton fab; - private BottomSheetDialog dialog; - private TextView mNameTextView; private TextView mKarmaTextView; private GifImageView mProfileImageView; @@ -62,6 +60,8 @@ public class MainActivity extends AppCompatActivity { private Fragment mFragment; private RequestManager glide; private AppBarLayout.LayoutParams params; + private BottomSheetDialog postTypedialog; + private SortTypeBottomSheetFragment sortTypeBottomSheetFragment; private String mName; private String mProfileImageUrl; @@ -92,16 +92,18 @@ public class MainActivity extends AppCompatActivity { ButterKnife.bind(this); - View dialogView = View.inflate(this, R.layout.post_type_bottom_sheet, null); - LinearLayout textTypeLinearLayout = dialogView.findViewById(R.id.text_type_linear_layout_post_type_bottom_sheet); - LinearLayout linkTypeLinearLayout = dialogView.findViewById(R.id.link_type_linear_layout_post_type_bottom_sheet); - LinearLayout imageTypeLinearLayout = dialogView.findViewById(R.id.image_type_linear_layout_post_type_bottom_sheet); - LinearLayout videoTypeLinearLayout = dialogView.findViewById(R.id.video_type_linear_layout_post_type_bottom_sheet); + ((Infinity) getApplication()).getmAppComponent().inject(this); - dialog = new BottomSheetDialog(this); - dialog.setContentView(dialogView); + View postTypeDialogView = View.inflate(this, R.layout.post_type_bottom_sheet, null); + LinearLayout textTypeLinearLayout = postTypeDialogView.findViewById(R.id.text_type_linear_layout_post_type_bottom_sheet); + LinearLayout linkTypeLinearLayout = postTypeDialogView.findViewById(R.id.link_type_linear_layout_post_type_bottom_sheet); + LinearLayout imageTypeLinearLayout = postTypeDialogView.findViewById(R.id.image_type_linear_layout_post_type_bottom_sheet); + LinearLayout videoTypeLinearLayout = postTypeDialogView.findViewById(R.id.video_type_linear_layout_post_type_bottom_sheet); - ((Infinity) getApplication()).getmAppComponent().inject(this); + postTypedialog = new BottomSheetDialog(this); + postTypedialog.setContentView(postTypeDialogView); + + sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -120,11 +122,7 @@ public class MainActivity extends AppCompatActivity { startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE); } else { if (savedInstanceState == null) { - mFragment = new PostFragment(); - Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE); - mFragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); + replaceFragment(PostDataSource.SORT_TYPE_BEST); } else { mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); @@ -186,30 +184,37 @@ public class MainActivity extends AppCompatActivity { textTypeLinearLayout.setOnClickListener(view -> { Intent intent = new Intent(MainActivity.this, PostTextActivity.class); startActivity(intent); - dialog.dismiss(); + postTypedialog.dismiss(); }); linkTypeLinearLayout.setOnClickListener(view -> { Intent intent = new Intent(MainActivity.this, PostLinkActivity.class); startActivity(intent); - dialog.dismiss(); + postTypedialog.dismiss(); }); imageTypeLinearLayout.setOnClickListener(view -> { Intent intent = new Intent(MainActivity.this, PostImageActivity.class); startActivity(intent); - dialog.dismiss(); + postTypedialog.dismiss(); }); videoTypeLinearLayout.setOnClickListener(view -> { Intent intent = new Intent(MainActivity.this, PostVideoActivity.class); startActivity(intent); - dialog.dismiss(); + postTypedialog.dismiss(); }); - fab.setOnClickListener(view -> { - dialog.show(); - }); + fab.setOnClickListener(view -> postTypedialog.show()); + } + + private void replaceFragment(String sortType) { + mFragment = new PostFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE); + bundle.putString(PostFragment.EXTRA_SORT_TYPE, sortType); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); } private void loadUserData() { @@ -301,6 +306,9 @@ public class MainActivity extends AppCompatActivity { public boolean onOptionsItemSelected(MenuItem item) { if (mFragment instanceof FragmentCommunicator) { switch (item.getItemId()) { + case R.id.action_sort_main_activity: + sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + return true; case R.id.action_search_main_activity: Intent intent = new Intent(this, SearchActivity.class); startActivity(intent); @@ -351,4 +359,9 @@ public class MainActivity extends AppCompatActivity { outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); } + + @Override + public void sortTypeSelected(String sortType) { + replaceFragment(sortType); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 2edb526e..1a9bfe93 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; +import android.text.Html; import android.util.Log; import androidx.annotation.Nullable; @@ -223,7 +224,7 @@ class ParseComment { if(!singleCommentData.isNull(JSONUtils.BODY_HTML_KEY)) { commentContent = singleCommentData.getString(JSONUtils.BODY_HTML_KEY).trim(); } - String permalink = singleCommentData.getString(JSONUtils.PERMALINK_KEY); + String permalink = Html.fromHtml(singleCommentData.getString(JSONUtils.PERMALINK_KEY)).toString(); int score = singleCommentData.getInt(JSONUtils.SCORE_KEY); long submitTime = singleCommentData.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index a2146668..a1f614ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; +import android.text.Html; import android.util.Log; import org.json.JSONArray; @@ -122,7 +123,7 @@ class ParsePost { parsePostListener.onParsePostSuccess(post); } } else { - if(newPosts != null) { + if(parsePostsListingListener != null) { parsePostsListingListener.onParsePostsListingFail(); } else { parsePostListener.onParsePostFail(); @@ -160,7 +161,7 @@ class ParsePost { postTimeCalendar.setTimeInMillis(postTime); String formattedPostTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", locale).format(postTimeCalendar.getTime()); - String permalink = data.getString(JSONUtils.PERMALINK_KEY); + String permalink = Html.fromHtml(data.getString(JSONUtils.PERMALINK_KEY)).toString(); String previewUrl = ""; int previewWidth = -1; @@ -194,7 +195,7 @@ class ParsePost { Post post; boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); - String url = data.getString(JSONUtils.URL_KEY); + String url = Html.fromHtml(data.getString(JSONUtils.URL_KEY)).toString(); if(!data.has(JSONUtils.PREVIEW_KEY) && previewUrl.equals("")) { if(url.contains(permalink)) { @@ -224,8 +225,8 @@ class ParsePost { } } else { if(previewUrl.equals("")) { - previewUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) - .getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); + previewUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) + .getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); } if(isVideo) { @@ -233,7 +234,7 @@ class ParsePost { Log.i("video", Integer.toString(i)); JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); int postType = Post.VIDEO_TYPE; - String videoUrl = redditVideoObject.getString(JSONUtils.DASH_URL_KEY); + String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.DASH_URL_KEY)).toString(); post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, @@ -249,8 +250,8 @@ class ParsePost { //Gif video post (MP4) Log.i("gif video mp4", Integer.toString(i)); int postType = Post.GIF_VIDEO_TYPE; - String videoUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); - String gifDownloadUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); + String videoUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); + String gifDownloadUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, @@ -264,8 +265,8 @@ class ParsePost { //Gif video post (Dash) Log.i("gif video dash", Integer.toString(i)); int postType = Post.GIF_VIDEO_TYPE; - String videoUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY) - .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY); + String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY) + .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY)).toString(); post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 09d5451a..2a96524e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -24,12 +24,21 @@ class PostDataSource extends PageKeyedDataSource { static final int TYPE_USER = 2; static final int TYPE_SEARCH = 3; + static final String SORT_TYPE_BEST = "best"; + static final String SORT_TYPE_HOT = "hot"; + static final String SORT_TYPE_NEW = "new"; + static final String SORT_TYPE_RANDOM = "random"; + static final String SORT_TYPE_RISING = "rising"; + static final String SORT_TYPE_TOP = "top"; + static final String SORT_TYPE_CONTROVERSIAL = "controversial"; + private Retrofit retrofit; private String accessToken; private Locale locale; private String subredditName; private String query; private int postType; + private String sortType; private OnPostFetchedCallback onPostFetchedCallback; private MutableLiveData paginationNetworkStateLiveData; @@ -40,17 +49,33 @@ class PostDataSource extends PageKeyedDataSource { private LoadParams params; private LoadCallback callback; - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, OnPostFetchedCallback onPostFetchedCallback) { + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, + OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; paginationNetworkStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData(); this.postType = postType; + this.sortType = sortType; this.onPostFetchedCallback = onPostFetchedCallback; } - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, OnPostFetchedCallback onPostFetchedCallback) { + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + String sortType, OnPostFetchedCallback onPostFetchedCallback) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + paginationNetworkStateLiveData = new MutableLiveData(); + initialLoadStateLiveData = new MutableLiveData(); + this.postType = postType; + this.sortType = sortType; + this.onPostFetchedCallback = onPostFetchedCallback; + } + + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -62,7 +87,7 @@ class PostDataSource extends PageKeyedDataSource { } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, OnPostFetchedCallback onPostFetchedCallback) { + int postType, String sortType, OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -71,6 +96,7 @@ class PostDataSource extends PageKeyedDataSource { paginationNetworkStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData(); this.postType = postType; + this.sortType = sortType; this.onPostFetchedCallback = onPostFetchedCallback; } @@ -140,35 +166,54 @@ class PostDataSource extends PageKeyedDataSource { private void loadBestPostsInitial(@NonNull final LoadInitialCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost = api.getBestPosts(null, RedditUtils.getOAuthHeader(accessToken)); + Call bestPost = api.getBestPosts(sortType, null, RedditUtils.getOAuthHeader(accessToken)); bestPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if(newPosts.size() == 0) { - onPostFetchedCallback.noPost(); - } else { - onPostFetchedCallback.hasPost(); - } + if(sortType.equals(SORT_TYPE_RANDOM)) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(Post post) { + ArrayList singlePostList = new ArrayList<>(); + singlePostList.add(post); + onPostFetchedCallback.hasPost(); + callback.onResult(singlePostList, null, null); + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } - if(lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - callback.onResult(newPosts, null, null); - } else { - callback.onResult(newPosts, null, lastItem); + @Override + public void onParsePostFail() { + Log.i("Post fetch error", "Error parsing data"); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } else { + ParsePost.parsePosts(response.body(), locale, -1, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if(newPosts.size() == 0) { + onPostFetchedCallback.noPost(); + } else { + onPostFetchedCallback.hasPost(); + } + + if(lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + callback.onResult(newPosts, null, null); + } else { + callback.onResult(newPosts, null, lastItem); + } + initialLoadStateLiveData.postValue(NetworkState.LOADED); } - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - @Override - public void onParsePostsListingFail() { - Log.i("Post fetch error", "Error parsing data"); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); + @Override + public void onParsePostsListingFail() { + Log.i("Post fetch error", "Error parsing data"); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } } else { Log.i("Post fetch error", response.message()); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); @@ -185,7 +230,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadBestPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost = api.getBestPosts(params.key, RedditUtils.getOAuthHeader(accessToken)); + Call bestPost = api.getBestPosts(sortType, params.key, RedditUtils.getOAuthHeader(accessToken)); bestPost.enqueue(new Callback() { @Override @@ -224,35 +269,54 @@ class PostDataSource extends PageKeyedDataSource { private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(subredditName, null, RedditUtils.getOAuthHeader(accessToken)); + Call getPost = api.getSubredditBestPosts(subredditName, sortType, null, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if(newPosts.size() == 0) { - onPostFetchedCallback.noPost(); - } else { - onPostFetchedCallback.hasPost(); - } + if(sortType.equals(SORT_TYPE_RANDOM)) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(Post post) { + ArrayList singlePostList = new ArrayList<>(); + singlePostList.add(post); + onPostFetchedCallback.hasPost(); + callback.onResult(singlePostList, null, null); + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } - if(lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - callback.onResult(newPosts, null, null); - } else { - callback.onResult(newPosts, null, lastItem); + @Override + public void onParsePostFail() { + Log.i("Post fetch error", "Error parsing data"); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } else { + ParsePost.parsePosts(response.body(), locale, -1, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if(newPosts.size() == 0) { + onPostFetchedCallback.noPost(); + } else { + onPostFetchedCallback.hasPost(); + } + + if(lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + callback.onResult(newPosts, null, null); + } else { + callback.onResult(newPosts, null, lastItem); + } + initialLoadStateLiveData.postValue(NetworkState.LOADED); } - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - @Override - public void onParsePostsListingFail() { - Log.i("Post fetch error", "Error parsing data"); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); + @Override + public void onParsePostsListingFail() { + Log.i("Post fetch error", "Error parsing data"); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } } else { Log.i("Post fetch error", response.message()); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); @@ -269,7 +333,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(subredditName, params.key, RedditUtils.getOAuthHeader(accessToken)); + Call getPost = api.getSubredditBestPosts(subredditName, sortType, 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 117f3cad..31e91c54 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -14,18 +14,32 @@ class PostDataSourceFactory extends DataSource.Factory { private String subredditName; private String query; private int postType; + private String sortType; private PostDataSource.OnPostFetchedCallback onPostFetchedCallback; private PostDataSource postDataSource; private MutableLiveData postDataSourceLiveData; - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; + this.sortType = sortType; + this.onPostFetchedCallback = onPostFetchedCallback; + } + + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, + int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + postDataSourceLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType; this.onPostFetchedCallback = onPostFetchedCallback; } @@ -41,7 +55,7 @@ class PostDataSourceFactory extends DataSource.Factory { } PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - String query, int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + String query, int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -49,15 +63,18 @@ class PostDataSourceFactory extends DataSource.Factory { this.query = query; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; + this.sortType = sortType; this.onPostFetchedCallback = onPostFetchedCallback; } @Override public DataSource create() { if(postType == PostDataSource.TYPE_FRONT_PAGE) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback); + postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType, onPostFetchedCallback); } else if(postType == PostDataSource.TYPE_SEARCH) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, postType, onPostFetchedCallback); + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, postType, sortType, onPostFetchedCallback); + } else if(postType == PostDataSource.TYPE_SUBREDDIT) { + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, sortType, 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 3fde7f58..7196e640 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -48,6 +48,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { static final String EXTRA_SUBREDDIT_NAME = "EN"; static final String EXTRA_QUERY = "EQ"; static final String EXTRA_POST_TYPE = "EPT"; + static final String EXTRA_SORT_TYPE = "EST"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; @@ -166,6 +167,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); int postType = getArguments().getInt(EXTRA_POST_TYPE); + String sortType = getArguments().getString(EXTRA_SORT_TYPE); String accessToken = activity.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); @@ -176,11 +178,31 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); String query = getArguments().getString(EXTRA_QUERY); + mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, + mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore()); + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, subredditName, query, postType, sortType, 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_SUBREDDIT) { + String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); + mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, query, postType, new PostDataSource.OnPostFetchedCallback() { + getResources().getConfiguration().locale, subredditName, postType, sortType, new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); @@ -194,12 +216,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { showErrorView(R.string.no_posts); } }); - } else if(postType != PostDataSource.TYPE_FRONT_PAGE) { - if(postType == PostDataSource.TYPE_USER) { - CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); - params.height = ViewGroup.LayoutParams.WRAP_CONTENT; - mFetchPostInfoLinearLayout.setLayoutParams(params); - } + } else if(postType == PostDataSource.TYPE_USER) { + CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); + params.height = ViewGroup.LayoutParams.WRAP_CONTENT; + mFetchPostInfoLinearLayout.setLayoutParams(params); String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); @@ -207,7 +227,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, postType, new PostDataSource.OnPostFetchedCallback() { + getResources().getConfiguration().locale, subredditName, postType, sortType, + new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); @@ -226,7 +247,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, postType, new PostDataSource.OnPostFetchedCallback() { + getResources().getConfiguration().locale, postType, sortType, 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 4aec1ddc..bc750c2a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -260,6 +260,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS imageUri = null; selectAgainTextView.setVisibility(View.GONE); mGlide.clear(imageView); + imageView.setVisibility(View.GONE); constraintLayout.setVisibility(View.VISIBLE); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index b4c266eb..81553663 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -13,9 +13,9 @@ import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; -import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; +import android.widget.VideoView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -77,7 +77,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS @BindView(R.id.capture_fab_post_video_activity) FloatingActionButton captureFab; @BindView(R.id.select_from_library_fab_post_video_activity) FloatingActionButton selectFromLibraryFab; @BindView(R.id.select_again_text_view_post_video_activity) TextView selectAgainTextView; - @BindView(R.id.image_view_post_video_activity) ImageView imageView; + @BindView(R.id.video_view_post_video_activity) VideoView videoView; private String iconUrl; private String subredditName; @@ -248,19 +248,26 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.select_from_gallery)), PICK_VIDEO_REQUEST_CODE); }); + videoView.setOnPreparedListener(mediaPlayer -> { + mediaPlayer.setLooping(true); + mediaPlayer.setVolume(0, 0); + }); + selectAgainTextView.setOnClickListener(view -> { videoUri = null; selectAgainTextView.setVisibility(View.GONE); - mGlide.clear(imageView); + videoView.stopPlayback(); + videoView.setVisibility(View.GONE); constraintLayout.setVisibility(View.VISIBLE); }); } private void loadImage() { constraintLayout.setVisibility(View.GONE); - imageView.setVisibility(View.VISIBLE); + videoView.setVisibility(View.VISIBLE); selectAgainTextView.setVisibility(View.VISIBLE); - mGlide.asBitmap().load(videoUri).into(imageView); + videoView.setVideoURI(videoUri); + videoView.start(); } private void displaySubredditIcon() { @@ -376,6 +383,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS return false; } + @Override + protected void onStop() { + super.onStop(); + videoView.stopPlayback(); + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 21e7d03d..3c5548f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -19,9 +19,9 @@ public class PostViewModel extends ViewModel { private LiveData initialLoadingState; private LiveData> posts; - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, onPostFetchedCallback); + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, sortType, onPostFetchedCallback); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), (Function>) PostDataSource::getInitialLoadStateLiveData); @@ -36,6 +36,24 @@ public class PostViewModel extends ViewModel { posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); } + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, postType, sortType, 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(); + } + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback); @@ -55,9 +73,9 @@ public class PostViewModel extends ViewModel { } public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - query, postType, onPostFetchedCallback); + query, postType, sortType, onPostFetchedCallback); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), dataSource -> dataSource.getInitialLoadStateLiveData()); @@ -104,14 +122,27 @@ public class PostViewModel extends ViewModel { private String subredditName; private String query; private int postType; + private String sortType; private PostDataSource.OnPostFetchedCallback onPostFetchedCallback; - public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, + public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; this.postType = postType; + this.sortType = sortType; + this.onPostFetchedCallback = onPostFetchedCallback; + } + + public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.postType = postType; + this.sortType = sortType; this.onPostFetchedCallback = onPostFetchedCallback; } @@ -126,13 +157,14 @@ public class PostViewModel extends ViewModel { } public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { + int postType, String sortType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; this.subredditName = subredditName; this.query = query; this.postType = postType; + this.sortType = sortType; this.onPostFetchedCallback = onPostFetchedCallback; } @@ -140,9 +172,11 @@ public class PostViewModel extends ViewModel { @Override public T create(@NonNull Class modelClass) { if(postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback); + return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, onPostFetchedCallback); } else if(postType == PostDataSource.TYPE_SEARCH){ - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, postType, onPostFetchedCallback); + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, postType, sortType, onPostFetchedCallback); + } else if(postType == PostDataSource.TYPE_SUBREDDIT) { + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, 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 9ad7d23d..0aba1d50 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -41,12 +41,12 @@ public interface RedditAPI { @GET("comments/{id}.json?raw_json=1") Call getPost(@Path("id") String id, @HeaderMap Map headers); - @GET("best?raw_json=1") - Call getBestPosts(@Query("after") String lastItem, @HeaderMap Map headers); + @GET("{sortType}?raw_json=1") + Call getBestPosts(@Path("sortType") String sortType, @Query("after") String lastItem, @HeaderMap Map headers); - @GET("r/{subredditName}.json?raw_json=1&limit=25") - Call getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem, - @HeaderMap Map headers); + @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25") + Call getSubredditBestPosts(@Path("subredditName") String subredditName, @Path("sortType") String sortType, + @Query("after") String lastItem, @HeaderMap Map headers); @GET("user/{username}/submitted.json?raw_json=1&limit=25") Call getUserBestPosts(@Path("username") String username, @Query("after") String lastItem, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index a677b058..b8f304bd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -102,6 +102,7 @@ public class SearchResultActivity extends AppCompatActivity { PostFragment mFragment = new PostFragment(); Bundle bundle = new Bundle(); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SEARCH); + bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST); bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, mSubredditName); bundle.putString(PostFragment.EXTRA_QUERY, mQuery); mFragment.setArguments(bundle); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java new file mode 100644 index 00000000..1af128a0 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java @@ -0,0 +1,88 @@ +package ml.docilealligator.infinityforreddit; + + +import android.app.Activity; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; + + +/** + * A simple {@link Fragment} subclass. + */ +public class SortTypeBottomSheetFragment extends BottomSheetDialogFragment { + + interface SortTypeSelectionCallback { + void sortTypeSelected(String sortType); + } + + @BindView(R.id.best_type_text_view_sort_type_bottom_sheet_fragment) TextView bestTypeTextView; + @BindView(R.id.hot_type_text_view_sort_type_bottom_sheet_fragment) TextView hotTypeTextView; + @BindView(R.id.new_type_text_view_sort_type_bottom_sheet_fragment) TextView newTypeTextView; + @BindView(R.id.random_type_text_view_sort_type_bottom_sheet_fragment) TextView randomTypeTextView; + @BindView(R.id.rising_type_text_view_sort_type_bottom_sheet_fragment) TextView risingTypeTextView; + @BindView(R.id.top_type_text_view_sort_type_bottom_sheet_fragment) TextView topTypeTextView; + @BindView(R.id.controversial_type_text_view_sort_type_bottom_sheet_fragment) TextView controversialTypeTextView; + + public SortTypeBottomSheetFragment() { + // Required empty public constructor + } + + + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_sort_type_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + Activity activity = getActivity(); + + bestTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_BEST); + dismiss(); + }); + + hotTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_HOT); + dismiss(); + }); + + newTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_NEW); + dismiss(); + }); + + randomTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_RANDOM); + dismiss(); + }); + + risingTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_RISING); + dismiss(); + }); + + topTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_TOP); + dismiss(); + }); + + controversialTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_BEST); + dismiss(); + }); + + return rootView; + } + +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 4c6abc3e..e6ff9bfe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -43,7 +43,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; -public class ViewSubredditDetailActivity extends AppCompatActivity { +public class ViewSubredditDetailActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback { public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN"; @@ -72,6 +72,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { private Menu mMenu; private AppBarLayout.LayoutParams params; private BottomSheetDialog dialog; + private SortTypeBottomSheetFragment sortTypeBottomSheetFragment; private SubscribedSubredditDao subscribedSubredditDao; private SubredditViewModel mSubredditViewModel; @@ -95,6 +96,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { ButterKnife.bind(this); + ((Infinity) getApplication()).getmAppComponent().inject(this); + View dialogView = View.inflate(this, R.layout.post_type_bottom_sheet, null); LinearLayout textTypeLinearLayout = dialogView.findViewById(R.id.text_type_linear_layout_post_type_bottom_sheet); LinearLayout linkTypeLinearLayout = dialogView.findViewById(R.id.link_type_linear_layout_post_type_bottom_sheet); @@ -104,7 +107,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { dialog = new BottomSheetDialog(this); dialog.setContentView(dialogView); - ((Infinity) getApplication()).getmAppComponent().inject(this); + sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); @@ -259,12 +262,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { }); if(savedInstanceState == null) { - mFragment = new PostFragment(); - Bundle bundle = new Bundle(); - bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, subredditName); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); - mFragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit(); + replaceFragment(PostDataSource.SORT_TYPE_BEST); } else { mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); if(mFragment == null) { @@ -272,6 +270,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { Bundle bundle = new Bundle(); bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST); mFragment.setArguments(bundle); } isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); @@ -335,6 +334,9 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { case android.R.id.home: finish(); return true; + case R.id.action_sort_view_subreddit_detail_activity: + sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + return true; case R.id.action_search_view_subreddit_detail_activity: Intent intent = new Intent(this, SearchActivity.class); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); @@ -367,6 +369,16 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { return false; } + private void replaceFragment(String sortType) { + mFragment = new PostFragment(); + Bundle bundle = new Bundle(); + bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, subredditName); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putString(PostFragment.EXTRA_SORT_TYPE, sortType); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit(); + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -378,6 +390,11 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show(); } + @Override + public void sortTypeSelected(String sortType) { + replaceFragment(sortType); + } + private static class InsertSubredditDataAsyncTask extends AsyncTask { private SubredditDao mSubredditDao; -- cgit v1.2.3