Showing posts of multireddit is now available. Fix a typo.

This commit is contained in:
Alex Ning 2019-12-17 00:32:37 +08:00
parent b9893c6d14
commit 666ac642fc
14 changed files with 647 additions and 36 deletions

View File

@ -21,7 +21,11 @@
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:replace="android:label">
<activity android:name=".Activity.MultiRedditListingActivity"
<activity android:name=".Activity.ViewMultiRedditDetailActivity"
android:parentActivityName=".Activity.MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" />
<activity
android:name=".Activity.MultiRedditListingActivity"
android:label="@string/multi_reddit_listing_activity"
android:parentActivityName=".Activity.MainActivity"
android:theme="@style/AppTheme.NoActionBar" />

View File

@ -0,0 +1,219 @@
package ml.docilealligator.infinityforreddit.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.chip.Chip;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.AppBarStateChangeListener;
import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask;
import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
import ml.docilealligator.infinityforreddit.Fragment.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit;
import ml.docilealligator.infinityforreddit.PostDataSource;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit;
public class ViewMultiRedditDetailActivity extends BaseActivity implements SortTypeSelectionCallback,
PostLayoutBottomSheetFragment.PostLayoutSelectionCallback {
public static final String EXTRA_MULTIREDDIT_DATA = "EMD";
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
@BindView(R.id.coordinator_layout_view_multi_reddit_detail_activity)
CoordinatorLayout coordinatorLayout;
@BindView(R.id.appbar_layout_view_multi_reddit_detail)
AppBarLayout appBarLayout;
@BindView(R.id.collapsing_toolbar_layout_view_multi_reddit_detail_activity)
CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.icon_gif_image_view_view_multi_reddit_detail_activity)
GifImageView iconGifImageView;
@BindView(R.id.duplicate_multi_reddit_chip_view_multi_reddit_detail_activity)
Chip duplicateSubredditChip;
@BindView(R.id.multi_reddit_name_text_view_view_multi_reddit_detail_activity)
TextView multiRedditNameTextView;
@BindView(R.id.owner_text_view_view_multi_reddit_detail_activity)
TextView ownerTextView;
@BindView(R.id.subreddits_count_view_multi_reddit_detail_activity)
TextView subredditsCountTextView;
@BindView(R.id.description_text_view_view_multi_reddit_detail_activity)
TextView descriptionTextView;
@Inject
@Named("no_oauth")
Retrofit mRetrofit;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
SharedPreferences mSharedPreferences;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String mAccountName;
private String multiPath;
private boolean isInLazyMode = false;
private boolean showToast = false;
private Fragment mFragment;
private SortTypeBottomSheetFragment sortTypeBottomSheetFragment;
private SortTimeBottomSheetFragment sortTimeBottomSheetFragment;
private PostLayoutBottomSheetFragment postLayoutBottomSheetFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
((Infinity) getApplication()).getAppComponent().inject(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_multi_reddit_detail);
ButterKnife.bind(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources();
if ((resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet))
&& mSharedPreferences.getBoolean(SharedPreferencesUtils.IMMERSIVE_INTERFACE_KEY, true)) {
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
boolean lightNavBar = false;
if ((resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
lightNavBar = true;
}
boolean finalLightNavBar = lightNavBar;
View decorView = window.getDecorView();
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state) {
if (state == State.COLLAPSED) {
if (finalLightNavBar) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
} else if (state == State.EXPANDED) {
if (finalLightNavBar) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
}
}
});
int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (navBarResourceId > 0) {
showToast = true;
}
}
}
MultiReddit multiReddit = getIntent().getParcelableExtra(EXTRA_MULTIREDDIT_DATA);
if (multiReddit == null) {
Toast.makeText(this, R.string.multi_reddit_listing_activity, Toast.LENGTH_SHORT).show();
finish();
return;
}
multiPath = multiReddit.getPath();
multiRedditNameTextView.setText(multiReddit.getDisplayName());
ownerTextView.setText(multiReddit.getOwner());
if (savedInstanceState != null) {
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit();
if (!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndInitializeFragment();
}
} else {
getCurrentAccountAndInitializeFragment();
}
sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment();
Bundle bottomSheetBundle = new Bundle();
bottomSheetBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true);
sortTypeBottomSheetFragment.setArguments(bottomSheetBundle);
sortTimeBottomSheetFragment = new SortTimeBottomSheetFragment();
postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment();
}
private void getCurrentAccountAndInitializeFragment() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if (account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
initializeFragment();
}).execute();
}
private void initializeFragment() {
mFragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putString(PostFragment.EXTRA_NAME, multiPath);
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit();
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment);
}
@Override
public SharedPreferences getSharedPreferences() {
return mSharedPreferences;
}
@Override
public void postLayoutSelected(int postLayout) {
}
}

View File

@ -21,7 +21,7 @@ import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewMultiRedditDetailActivity;
import ml.docilealligator.infinityforreddit.MultiReddit.FavoriteMultiReddit;
import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit;
import ml.docilealligator.infinityforreddit.R;
@ -101,20 +101,21 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
int offset = (mFavoriteMultiReddits != null && mFavoriteMultiReddits.size() > 0) ?
mFavoriteMultiReddits.size() + 2 : 0;
name = mMultiReddits.get(holder.getAdapterPosition() - offset).getName();
iconUrl = mMultiReddits.get(holder.getAdapterPosition() - offset).getIconUrl();
if(mMultiReddits.get(holder.getAdapterPosition() - offset).isFavorite()) {
MultiReddit multiReddit = mMultiReddits.get(holder.getAdapterPosition() - offset);
name = multiReddit.getName();
iconUrl = multiReddit.getIconUrl();
if(multiReddit.isFavorite()) {
((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
} else {
((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
((MultiRedditViewHolder) holder).favoriteImageView.setOnClickListener(view -> {
if(mMultiReddits.get(holder.getAdapterPosition() - offset).isFavorite()) {
if(multiReddit.isFavorite()) {
((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
mMultiReddits.get(holder.getAdapterPosition() - offset).setFavorite(false);
multiReddit.setFavorite(false);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
false, mMultiReddits.get(holder.getAdapterPosition() - offset),
false, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -138,9 +139,9 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
);
} else {
((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
mMultiReddits.get(holder.getAdapterPosition() - offset).setFavorite(true);
multiReddit.setFavorite(true);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
true, mMultiReddits.get(holder.getAdapterPosition() - offset),
true, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -165,8 +166,8 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
}
});
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name);
Intent intent = new Intent(mContext, ViewMultiRedditDetailActivity.class);
intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_DATA, multiReddit);
mContext.startActivity(intent);
});
@ -183,20 +184,21 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
}
((MultiRedditViewHolder) holder).multiRedditNameTextView.setText(name);
} else if (holder instanceof FavoriteMultiRedditViewHolder) {
String name = mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1).getName();
String iconUrl = mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1).getIconUrl();
if(mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1).isFavorite()) {
MultiReddit multiReddit = mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1);
String name = multiReddit.getName();
String iconUrl = multiReddit.getIconUrl();
if(multiReddit.isFavorite()) {
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
} else {
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setOnClickListener(view -> {
if(mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1).isFavorite()) {
if(multiReddit.isFavorite()) {
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1).setFavorite(false);
multiReddit.setFavorite(false);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
false, mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1),
false, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -220,9 +222,9 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
);
} else {
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1).setFavorite(true);
multiReddit.setFavorite(true);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
true, mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1),
true, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -247,8 +249,8 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
}
});
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name);
Intent intent = new Intent(mContext, ViewMultiRedditDetailActivity.class);
intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_DATA, multiReddit);
mContext.startActivity(intent);
});

View File

@ -27,6 +27,7 @@ import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActiv
import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewMultiRedditDetailActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity;
@ -119,4 +120,6 @@ public interface AppComponent {
void inject(ViewGIFActivity viewGIFActivity);
void inject(MultiRedditListingActivity multiRedditListingActivity);
void inject(ViewMultiRedditDetailActivity viewMultiRedditDetailActivity);
}

View File

@ -12,6 +12,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@ -419,6 +420,51 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
getResources().getConfiguration().locale, subredditName, postType, sortType,
filter, nsfw);
}
} else if(postType == PostDataSource.TYPE_MULTI_REDDIT) {
String multiRedditName = getArguments().getString(EXTRA_NAME);
String sort;
String sortTime = null;
SortType sortType;
sort = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_ALL_POST, SortType.Type.HOT.name());
if(sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) {
sortTime = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_ALL_POST, SortType.Time.ALL.name());
}
postLayout = mSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_ALL_POST, SharedPreferencesUtils.POST_LAYOUT_CARD);
if(sortTime != null) {
sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime));
} else {
sortType = new SortType(SortType.Type.valueOf(sort));
}
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, postLayout, true, needBlurNsfw, needBlurSpoiler,
voteButtonsOnTheRight, showElapsedTime, new PostRecyclerViewAdapter.Callback() {
@Override
public void retryLoadingMore() {
mPostViewModel.retryLoadingMore();
}
@Override
public void typeChipClicked(int filter) {
Intent intent = new Intent(activity, FilteredThingActivity.class);
intent.putExtra(FilteredThingActivity.EXTRA_NAME, multiRedditName);
intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType);
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter);
startActivity(intent);
}
});
if (accessToken == null) {
factory = new PostViewModel.Factory(mRetrofit, accessToken,
getResources().getConfiguration().locale, multiRedditName, postType, sortType,
filter, nsfw);
} else {
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, multiRedditName, postType, sortType,
filter, nsfw);
}
} else if (postType == PostDataSource.TYPE_USER) {
String username = getArguments().getString(EXTRA_USER_NAME);
String where = getArguments().getString(EXTRA_USER_WHERE);

View File

@ -1,5 +1,8 @@
package ml.docilealligator.infinityforreddit.MultiReddit;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
@ -13,7 +16,7 @@ import ml.docilealligator.infinityforreddit.Account.Account;
@Entity(tableName = "multi_reddits", primaryKeys = {"path", "username"},
foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username",
childColumns = "username", onDelete = ForeignKey.CASCADE))
public class MultiReddit {
public class MultiReddit implements Parcelable {
@NonNull
@ColumnInfo(name = "path")
private String path;
@ -66,10 +69,10 @@ public class MultiReddit {
this.isFavorite = isFavorite;
}
public MultiReddit(@NonNull String path, String displayName, String name, String description, String copiedFrom,
String iconUrl, String visibility, @NonNull String owner,
int nSubscribers, long createdUTC, boolean over18, boolean isSubscriber,
boolean isFavorite, ArrayList<String> subreddits) {
public MultiReddit(@NonNull String path, @NonNull String displayName, @NonNull String name,
String description, String copiedFrom, String iconUrl, String visibility,
@NonNull String owner, int nSubscribers, long createdUTC, boolean over18,
boolean isSubscriber, boolean isFavorite, ArrayList<String> subreddits) {
this.displayName = displayName;
this.name = name;
this.description = description;
@ -86,6 +89,35 @@ public class MultiReddit {
this.subreddits = subreddits;
}
protected MultiReddit(Parcel in) {
path = in.readString();
displayName = in.readString();
name = in.readString();
description = in.readString();
copiedFrom = in.readString();
iconUrl = in.readString();
visibility = in.readString();
owner = in.readString();
nSubscribers = in.readInt();
createdUTC = in.readLong();
over18 = in.readByte() != 0;
isSubscriber = in.readByte() != 0;
isFavorite = in.readByte() != 0;
subreddits = in.readArrayList(MultiReddit.class.getClassLoader());
}
public static final Creator<MultiReddit> CREATOR = new Creator<MultiReddit>() {
@Override
public MultiReddit createFromParcel(Parcel in) {
return new MultiReddit(in);
}
@Override
public MultiReddit[] newArray(int size) {
return new MultiReddit[size];
}
};
@NonNull
public String getPath() {
return path;
@ -95,19 +127,21 @@ public class MultiReddit {
this.path = path;
}
@NonNull
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
public void setDisplayName(@NonNull String displayName) {
this.displayName = displayName;
}
@NonNull
public String getName() {
return name;
}
public void setName(String name) {
public void setName(@NonNull String name) {
this.name = name;
}
@ -198,4 +232,27 @@ public class MultiReddit {
public void setSubreddits(ArrayList<String> subreddits) {
this.subreddits = subreddits;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(path);
parcel.writeString(displayName);
parcel.writeString(name);
parcel.writeString(description);
parcel.writeString(copiedFrom);
parcel.writeString(iconUrl);
parcel.writeString(visibility);
parcel.writeString(owner);
parcel.writeInt(nSubscribers);
parcel.writeLong(createdUTC);
parcel.writeByte((byte) (over18 ? 1 : 0));
parcel.writeByte((byte) (isSubscriber ? 1 : 0));
parcel.writeByte((byte) (isFavorite ? 1 : 0));
parcel.writeStringList(subreddits);
}
}

View File

@ -18,6 +18,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
public static final int TYPE_SUBREDDIT = 1;
public static final int TYPE_USER = 2;
public static final int TYPE_SEARCH = 3;
public static final int TYPE_MULTI_REDDIT = 4;
public static final String USER_WHERE_SUBMITTED = "submitted";
public static final String USER_WHERE_UPVOTED = "upvoted";
@ -36,6 +37,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
private boolean nsfw;
private int filter;
private String userWhere;
private String multiRedditPath;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData;
@ -58,18 +60,22 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
this.nsfw = nsfw;
}
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType,
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String path, int postType,
SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.subredditOrUserName = subredditOrUserName;
if (postType == TYPE_SUBREDDIT) {
this.subredditOrUserName = path;
} else {
multiRedditPath = path;
}
paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>();
this.postType = postType;
if (sortType == null) {
if (subredditOrUserName.equals("popular") || subredditOrUserName.equals("all")) {
if (path.equals("popular") || path.equals("all")) {
this.sortType = new SortType(SortType.Type.HOT);
} else {
this.sortType = new SortType(SortType.Type.BEST);
@ -142,6 +148,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
case TYPE_SEARCH:
loadSearchPostsInitial(callback, null);
break;
case TYPE_MULTI_REDDIT:
loadMultiRedditPostsInitial(callback, null);
break;
}
}
@ -174,6 +183,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
case TYPE_SEARCH:
loadSearchPostsAfter(params, callback, null);
break;
case TYPE_MULTI_REDDIT:
loadMultiRedditPostsAfter(params, callback, null);
break;
}
}
@ -730,6 +742,129 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
});
}
private void loadMultiRedditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost;
if (accessToken == null) {
if (sortType.getTime() != null) {
getPost = api.getMultiRedditPosts(multiRedditPath, lastItem, sortType.getType().value,
sortType.getTime().value);
} else {
getPost = api.getMultiRedditPosts(multiRedditPath, lastItem, sortType.getType().value);
}
} else {
if (sortType.getTime() != null) {
getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, sortType.getType().value,
sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken));
} else {
getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, sortType.getType().value,
RedditUtils.getOAuthHeader(accessToken));
}
}
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
String nextPageKey;
if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) {
nextPageKey = null;
} else {
nextPageKey = lastItem;
}
if (newPosts.size() != 0) {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(true);
} else if (nextPageKey != null) {
loadMultiRedditPostsInitial(callback, nextPageKey);
return;
} else {
callback.onResult(newPosts, null, nextPageKey);
hasPostLiveData.postValue(false);
}
initialLoadStateLiveData.postValue(NetworkState.LOADED);
}
@Override
public void onParsePostsListingFail() {
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
}
});
} else {
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
String errorMessage = t.getMessage();
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
}
});
}
private void loadMultiRedditPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback, String lastItem) {
String after = lastItem == null ? params.key : lastItem;
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost;
if (accessToken == null) {
if (sortType.getTime() != null) {
getPost = api.getMultiRedditPosts(multiRedditPath, after, sortType.getType().value,
sortType.getTime().value);
} else {
getPost = api.getMultiRedditPosts(multiRedditPath, after, sortType.getType().value);
}
} else {
if (sortType.getTime() != null) {
getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, sortType.getType().value,
sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken));
} else {
getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, sortType.getType().value,
RedditUtils.getOAuthHeader(accessToken));
}
}
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) {
loadMultiRedditPostsAfter(params, callback, lastItem);
} else {
callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
}
}
@Override
public void onParsePostsListingFail() {
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
}
});
} else {
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
String errorMessage = t.getMessage();
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
}
});
}
void retryLoadingMore() {
loadAfter(params, callback);
}

View File

@ -85,7 +85,7 @@ class PostDataSourceFactory extends DataSource.Factory {
} else if (postType == PostDataSource.TYPE_SEARCH) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query,
postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SUBREDDIT) {
} else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType,
sortType, filter, nsfw);
} else {

View File

@ -252,7 +252,7 @@ public class PostViewModel extends ViewModel {
} else if (postType == PostDataSource.TYPE_SEARCH) {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query,
postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SUBREDDIT) {
} else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType,
sortType, filter, nsfw);
} else {

View File

@ -268,4 +268,24 @@ public interface RedditAPI {
@FormUrlEncoded
@POST("/api/multi/favorite?raw_json=1&gilding_detail=1")
Call<String> favoriteMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@GET("/api/multi/user/{username}")
Call<String> getUserMultiReddits(@Path("username") String username);
@GET("{multipath}?raw_json=1")
Call<String> getMultiRedditPosts(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after,
@Query("sort") String sortType);
@GET("{multipath}?raw_json=1")
Call<String> getMultiRedditPosts(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after,
@Query("sort") String sortType, @Query("t") String sortTime);
@GET("{multipath}.json?raw_json=1")
Call<String> getMultiRedditPostsOauth(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after,
@Query("sort") String sortType, @Query("t") String sortTime,
@HeaderMap Map<String, String> headers);
@GET("{multipath}.json?raw_json=1")
Call<String> getMultiRedditPostsOauth(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after,
@Query("sort") String sortType, @HeaderMap Map<String, String> headers);
}

View File

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator_layout_view_multi_reddit_detail_activity"
android:background="?attr/backgroundColor"
tools:context=".Activity.ViewMultiRedditDetailActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout_view_multi_reddit_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/toolbarAndTabBackgroundColor"
android:theme="@style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout_view_multi_reddit_detail_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:toolbarId="@+id/toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/icon_gif_image_view_view_multi_reddit_detail_activity"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginTop="-36dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:elevation="4dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="36dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:layout_below="@id/icon_gif_image_view_view_multi_reddit_detail_activity"
android:background="?attr/backgroundColor">
<TextView
android:id="@+id/multi_reddit_name_text_view_view_multi_reddit_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textSize="?attr/font_18"
android:textColor="@color/colorAccent"
android:layout_gravity="center_horizontal"/>
<com.google.android.material.chip.Chip
android:id="@+id/duplicate_multi_reddit_chip_view_multi_reddit_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/owner_text_view_view_multi_reddit_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/usernameColor"
android:textSize="?attr/font_default"/>
<TextView
android:id="@+id/subreddits_count_view_multi_reddit_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"/>
</LinearLayout>
<TextView
android:id="@+id/description_text_view_view_multi_reddit_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:navigationIcon="?attr/homeAsUpIndicator" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/frame_layout_view_multi_reddit_detail_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -6,7 +6,7 @@
android:layout_height="match_parent"
android:id="@+id/coordinator_layout_view_subreddit_detail_activity"
android:background="?attr/backgroundColor"
tools:application=".ViewSubredditDetailActivity">
tools:application=".Activity.ViewSubredditDetailActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout_view_subreddit_detail"

View File

@ -79,4 +79,8 @@
<color name="flairBackgroundColor">#00AA8C</color>
<color name="authorFlairTextColor">#EE02C4</color>
<color name="usernameColor">#0D47A1</color>
<color name="subredditNameColor">#FF4081</color>
</resources>

View File

@ -293,7 +293,7 @@
<string name="settings_vote_buttons_on_the_right_title">Vote Buttons on the Right</string>
<string name="settings_volume_keys_navigate_comments_title">Use Volume Keys to Navigate Comments in Posts</string>
<string name="settings_volume_keys_navigate_posts_title">Use Volume Keys to Navigate Posts</string>
<string name="settings_show_elapsed_time">Show Elpased Time in Posts and Comments</string>
<string name="settings_show_elapsed_time">Show Elapsed Time in Posts and Comments</string>
<string name="swipe_to_go_back_from_post_detail">Swipe Right to Go Back From Comments</string>
<string name="settings_lazy_mode_interval_title">Lazy Mode Interval</string>
<string name="settings_font_size_title">Font Size</string>
@ -372,4 +372,6 @@
<string name="elapsed_time_months_ago">%1$d Months</string>
<string name="elapsed_time_a_year_ago">1 Year</string>
<string name="elapsed_time_years_ago">%1$d Years</string>
<string name="error_getting_multi_reddit_data">Error getting multireddit data</string>
</resources>