diff options
Diffstat (limited to 'app/src/main/java/ml')
9 files changed, 515 insertions, 33 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java new file mode 100644 index 00000000..62d7e2ec --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java @@ -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) { + + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MultiRedditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MultiRedditListingRecyclerViewAdapter.java index 7503cfa7..e8c656e4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MultiRedditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MultiRedditListingRecyclerViewAdapter.java @@ -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); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 33dd286e..92b7ae3f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -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); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 17661c24..8e579689 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiReddit.java index 4692c8d4..71b14ad7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiReddit.java @@ -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); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 17970951..058dbc98 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -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); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 52015450..568efbf0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -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 { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 487ef5b1..ffba16c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -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 { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 9097b026..73b1df87 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -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); } |