View crosspost parent in ViewPostDetailActivity when clicking the crosspost icon or select Crosspost parent menu item in toolbar. Filter NSFW posts in FilteredThingActivity is now available.

This commit is contained in:
Alex Ning 2019-08-30 11:35:44 +08:00
parent 92762974ff
commit a5a77fca04
14 changed files with 99 additions and 29 deletions

Binary file not shown.

View File

@ -55,7 +55,7 @@ dependencies {
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.archLifecycleVersion"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$rootProject.archLifecycleVersion"
annotationProcessor "androidx.lifecycle:lifecycle-common-java8:$rootProject.archLifecycleVersion"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.6.1'

View File

@ -315,6 +315,11 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
}
if(mPost.isCrosspost()) {
((PostDetailViewHolder) holder).mCrosspostImageView.setOnClickListener(view -> {
Intent crosspostIntent = new Intent(mActivity, ViewPostDetailActivity.class);
crosspostIntent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, mPost.getCrosspostParentId());
mActivity.startActivity(crosspostIntent);
});
((PostDetailViewHolder) holder).mCrosspostImageView.setVisibility(View.VISIBLE);
}
@ -356,6 +361,14 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
}
if(mPost.isNSFW()) {
((PostDetailViewHolder) holder).mNSFWChip.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, FilteredThingActivity.class);
intent.putExtra(FilteredThingActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2));
intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT);
intent.putExtra(FilteredThingActivity.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_HOT);
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, Post.NSFW_TYPE);
mActivity.startActivity(intent);
});
((PostDetailViewHolder) holder).mNSFWChip.setVisibility(View.VISIBLE);
} else {
((PostDetailViewHolder) holder).mNSFWChip.setVisibility(View.GONE);

View File

@ -217,6 +217,9 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
}
switch (filter) {
case Post.NSFW_TYPE:
toolbar.setSubtitle(R.string.nsfw);
break;
case Post.TEXT_TYPE:
toolbar.setSubtitle(R.string.text);
break;
@ -243,10 +246,10 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
bundle.putInt(PostFragment.EXTRA_FILTER, filter);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
if(postType == PostDataSource.TYPE_USER) {
bundle.putString(PostFragment.EXTRA_USER_WHERE, getIntent().getExtras().getString(EXTRA_USER_WHERE));
bundle.putString(PostFragment.EXTRA_USER_WHERE, getIntent().getStringExtra(EXTRA_USER_WHERE));
}
if(postType == PostDataSource.TYPE_SEARCH) {
bundle.putString(PostFragment.EXTRA_QUERY, getIntent().getExtras().getString(EXTRA_QUERY));
bundle.putString(PostFragment.EXTRA_QUERY, getIntent().getStringExtra(EXTRA_QUERY));
}
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit();

View File

@ -98,7 +98,7 @@ class ParsePost {
try {
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
post = parseBasicData(data, locale, -1);
post = parseBasicData(data, locale);
} catch (JSONException e) {
Log.e("parsing post error", "message: " + e.getMessage());
parseFailed = true;
@ -115,7 +115,7 @@ class ParsePost {
for (int i = 0; i < size; i++) {
try {
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
Post post = parseBasicData(data, locale, i);
Post post = parseBasicData(data, locale);
if(!(!nsfw && post.isNSFW())) {
if (filter == PostFragment.EXTRA_NO_FILTER) {
newPosts.add(post);
@ -123,6 +123,8 @@ class ParsePost {
newPosts.add(post);
} else if (filter == Post.LINK_TYPE && post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) {
newPosts.add(post);
} else if(filter == Post.NSFW_TYPE && post.isNSFW()) {
newPosts.add(post);
}
}
} catch (JSONException e) {
@ -151,7 +153,7 @@ class ParsePost {
}
}
private static Post parseBasicData(JSONObject data, Locale locale, int i) throws JSONException {
private static Post parseBasicData(JSONObject data, Locale locale) throws JSONException {
String id = data.getString(JSONUtils.ID_KEY);
String fullName = data.getString(JSONUtils.NAME_KEY);
String subredditName = data.getString(JSONUtils.SUBREDDIT_KEY);
@ -200,15 +202,18 @@ class ParsePost {
if(data.has(JSONUtils.CROSSPOST_PARENT_LIST)) {
//Cross post
data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0);
return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
Post crosspostParent = parseBasicData(data, locale);
Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved,
true, i);
true);
post.setCrosspostParentId(crosspostParent.getId());
return post;
} else {
return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved,
false, i);
false);
}
}
@ -217,7 +222,7 @@ class ParsePost {
String formattedPostTime, String title, String previewUrl, int previewWidth,
int previewHeight, int score, int voteType, int gilded, String flair,
boolean spoiler, boolean nsfw, boolean stickied, boolean archived,
boolean locked, boolean saved, boolean isCrosspost, int i) throws JSONException {
boolean locked, boolean saved, boolean isCrosspost) throws JSONException {
Post post;
boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY);

View File

@ -8,6 +8,7 @@ import android.os.Parcelable;
*/
class Post implements Parcelable {
static final int NSFW_TYPE = -1;
static final int TEXT_TYPE = 0;
static final int IMAGE_TYPE = 1;
static final int LINK_TYPE = 2;
@ -47,6 +48,7 @@ class Post implements Parcelable {
private boolean isCrosspost;
private boolean isDashVideo;
private boolean isDownloadableGifOrVideo;
private String crosspostParentId;
Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author,
String postTime, String title, String previewUrl, String permalink, int score, int postType,
@ -166,6 +168,7 @@ class Post implements Parcelable {
isCrosspost = in.readByte() != 0;
isDashVideo = in.readByte() != 0;
isDownloadableGifOrVideo = in.readByte() != 0;
crosspostParentId = in.readString();
}
public static final Creator<Post> CREATOR = new Creator<Post>() {
@ -373,6 +376,14 @@ class Post implements Parcelable {
return isCrosspost;
}
public String getCrosspostParentId() {
return crosspostParentId;
}
public void setCrosspostParentId(String crosspostParentId) {
this.crosspostParentId = crosspostParentId;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(id);
@ -407,5 +418,6 @@ class Post implements Parcelable {
parcel.writeByte((byte) (isCrosspost ? 1 : 0));
parcel.writeByte((byte) (isDashVideo ? 1 : 0));
parcel.writeByte((byte) (isDownloadableGifOrVideo ? 1 : 0));
parcel.writeString(crosspostParentId);
}
}

View File

@ -60,7 +60,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
static final String EXTRA_POST_TYPE = "EPT";
static final String EXTRA_SORT_TYPE = "EST";
static final String EXTRA_FILTER = "EF";
static final int EXTRA_NO_FILTER = -1;
static final int EXTRA_NO_FILTER = -2;
static final String EXTRA_ACCESS_TOKEN = "EAT";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";

View File

@ -309,6 +309,16 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
}
if(nsfw) {
if(!(mContext instanceof FilteredThingActivity)) {
((DataViewHolder) holder).nsfwChip.setOnClickListener(view -> {
Intent intent = new Intent(mContext, FilteredThingActivity.class);
intent.putExtra(FilteredThingActivity.EXTRA_NAME, post.getSubredditNamePrefixed().substring(2));
intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT);
intent.putExtra(FilteredThingActivity.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_HOT);
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, Post.NSFW_TYPE);
mContext.startActivity(intent);
});
}
((DataViewHolder) holder).nsfwChip.setVisibility(View.VISIBLE);
}

View File

@ -48,6 +48,7 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.setInitialLoadSizeHint(75)
.build();
posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> {
@ -79,6 +80,7 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.setInitialLoadSizeHint(75)
.build();
posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> {
@ -110,6 +112,7 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.setInitialLoadSizeHint(75)
.build();
posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> {
@ -141,6 +144,7 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.setInitialLoadSizeHint(75)
.build();
posts = Transformations.switchMap(sortTypeLiveData, sort -> {

View File

@ -340,6 +340,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true);
}
mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null);
}
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
@ -429,13 +431,15 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
} else {
saveItem.setVisible(false);
}
}
if(mMenu != null && mPost.getAuthor().equals(mAccountName)) {
if(mPost.getPostType() == Post.TEXT_TYPE) {
mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true);
if(mPost.getAuthor().equals(mAccountName)) {
if(mPost.getPostType() == Post.TEXT_TYPE) {
mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true);
}
mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true);
}
mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true);
mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null);
}
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this,
@ -637,6 +641,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
} else {
saveItem.setVisible(false);
}
mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null);
}
}
@ -654,7 +660,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchPostInfoLinearLayout.setOnClickListener(view -> fetchPostAndCommentsById(subredditId));
mFetchPostInfoTextView.setText(R.string.load_posts_error);
mFetchPostInfoTextView.setText(R.string.load_post_error);
mGlide.load(R.drawable.error_image).into(mFetchPostInfoImageView);
}
@ -838,6 +844,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
menu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true);
}
menu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null);
}
return true;
}
@ -915,7 +923,12 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
});
}
}
break;
return true;
case R.id.action_view_crosspost_parent_view_post_detail_activity:
Intent crosspostIntent = new Intent(this, ViewPostDetailActivity.class);
crosspostIntent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, mPost.getCrosspostParentId());
startActivity(crosspostIntent);
return true;
case R.id.action_edit_view_post_detail_activity:
Intent editPostItent = new Intent(this, EditPostActivity.class);
editPostItent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken);

View File

@ -11,7 +11,7 @@
<item
android:id="@+id/action_save_view_post_detail_activity"
android:orderInCategory="2"
android:title="@string/action_add_comment"
android:title="@string/action_save_post"
app:showAsAction="ifRoom"
android:visible="false" />
@ -22,34 +22,41 @@
app:showAsAction="never" />
<item
android:id="@+id/action_edit_view_post_detail_activity"
android:id="@+id/action_view_crosspost_parent_view_post_detail_activity"
android:orderInCategory="4"
android:title="@string/action_view_crosspost_parent"
app:showAsAction="never"
android:visible="false" />
<item
android:id="@+id/action_edit_view_post_detail_activity"
android:orderInCategory="5"
android:title="@string/action_edit_post"
app:showAsAction="never"
android:visible="false" />
<item
android:id="@+id/action_delete_view_post_detail_activity"
android:orderInCategory="5"
android:orderInCategory="6"
android:title="@string/action_delete_post"
app:showAsAction="never"
android:visible="false" />
<item
android:id="@+id/action_nsfw_view_post_detail_activity"
android:orderInCategory="6"
app:showAsAction="never"
android:visible="false" />
<item
android:id="@+id/action_spoiler_view_post_detail_activity"
android:orderInCategory="7"
app:showAsAction="never"
android:visible="false" />
<item
android:id="@+id/action_edit_flair_view_post_detail_activity"
android:id="@+id/action_spoiler_view_post_detail_activity"
android:orderInCategory="8"
app:showAsAction="never"
android:visible="false" />
<item
android:id="@+id/action_edit_flair_view_post_detail_activity"
android:orderInCategory="9"
android:title="@string/action_edit_flair"
app:showAsAction="never"
android:visible="false" />

View File

@ -23,6 +23,8 @@
<string name="action_download">Download</string>
<string name="action_refresh">Refresh</string>
<string name="action_add_comment">Add a comment</string>
<string name="action_save_post">Save post</string>
<string name="action_view_crosspost_parent">Crosspost parent</string>
<string name="action_search">Search</string>
<string name="action_start_lazy_mode">Start Lazy Mode</string>
<string name="action_stop_lazy_mode">Stop Lazy Mode</string>
@ -44,6 +46,7 @@
<string name="tap_to_retry">Error loading image. Tap to retry.</string>
<string name="load_posts_error">Error loading posts.\nTap to retry.</string>
<string name="load_post_error">Error loading this post.\nTap to retry.</string>
<string name="search_subreddits_error">Error searching subreddits.\nTap to retry.</string>
<string name="search_users_error">Error searching users.\nTap to retry.</string>
<string name="no_posts">No posts found</string>

View File

@ -31,6 +31,6 @@ task clean(type: Delete) {
}
ext {
roomVersion = '2.2.0-alpha02'
roomVersion = '2.2.0-beta01'
archLifecycleVersion = '2.2.0-alpha03'
}