diff options
Diffstat (limited to 'app/src/main/java/ml/docilealligator')
12 files changed, 732 insertions, 30 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java index e855177d..373a8655 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java @@ -255,9 +255,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - if (mFragment != null) { - getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); - } + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java index 31d85a0a..6db86514 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; @@ -8,8 +9,10 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; @@ -28,13 +31,17 @@ import ml.docilealligator.infinityforreddit.MultiReddit.CreateMultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditJSONModel; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SubredditWithSelection; import retrofit2.Retrofit; public class CreateMultiRedditActivity extends BaseActivity { + private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 1; 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"; + private static final String SELECTED_SUBSCRIBED_SUBREDDITS_STATE = "SSSS"; + private static final String SELECTED_OTHER_SUBREDDITS_STATE = "SOSS"; @BindView(R.id.coordinator_layout_create_multi_reddit_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.toolbar_create_multi_reddit_activity) @@ -59,7 +66,8 @@ public class CreateMultiRedditActivity extends BaseActivity { private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; - private ArrayList<String> subredditsName; + private ArrayList<SubredditWithSelection> mSelectedSubscribedSubreddits; + private ArrayList<SubredditWithSelection> mSelectedOtherSubreddits; @Override protected void onCreate(Bundle savedInstanceState) { @@ -72,23 +80,46 @@ public class CreateMultiRedditActivity extends BaseActivity { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getCurrentAccountAndBindView(); + if (savedInstanceState != null) { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + mSelectedSubscribedSubreddits = savedInstanceState.getParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE); + mSelectedOtherSubreddits = savedInstanceState.getParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE); + + if (!mNullAccessToken && mAccountName == null) { + getCurrentAccountAndBindView(); + } else { + bindView(); + } + } else { + getCurrentAccountAndBindView(); + } } private void getCurrentAccountAndBindView() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if (account == null) { mNullAccessToken = true; + Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); + finish(); } else { mAccessToken = account.getAccessToken(); mAccountName = account.getUsername(); + bindView(); } - bindView(); }).execute(); } private void bindView() { - + selectSubredditTextView.setOnClickListener(view -> { + Intent intent = new Intent(CreateMultiRedditActivity.this, SubredditMultiselectionActivity.class); + intent.putParcelableArrayListExtra(SubredditMultiselectionActivity.EXTRA_SELECTED_SUBSCRIBED_SUBREDDITS, + mSelectedSubscribedSubreddits); + intent.putParcelableArrayListExtra(SubredditMultiselectionActivity.EXTRA_SELECTED_OTHER_SUBREDDITS, + mSelectedOtherSubreddits); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); } @Override @@ -112,11 +143,12 @@ public class CreateMultiRedditActivity extends BaseActivity { Snackbar.make(coordinatorLayout, R.string.no_multi_reddit_name, Snackbar.LENGTH_SHORT).show(); return true; } - subredditsName = new ArrayList<>(); - /*subredditsName.add("funny"); - subredditsName.add("Infinity_For_Reddit");*/ + + ArrayList<SubredditWithSelection> allSelectedSubreddits = new ArrayList<>(); + allSelectedSubreddits.addAll(mSelectedSubscribedSubreddits); + allSelectedSubreddits.addAll(mSelectedOtherSubreddits); String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(), - visibilitySwitch.isChecked(), subredditsName).createJSONModel(); + visibilitySwitch.isChecked(), allSelectedSubreddits).createJSONModel(); CreateMultiReddit.createMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, "/user/" + mAccountName + "/m/" + nameEditText.getText().toString(), jsonModel, new CreateMultiReddit.CreateMultiRedditListener() { @@ -139,11 +171,42 @@ public class CreateMultiRedditActivity extends BaseActivity { } @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == SUBREDDIT_SELECTION_REQUEST_CODE && resultCode == RESULT_OK) { + if (data != null) { + if (mSelectedSubscribedSubreddits == null) { + mSelectedSubscribedSubreddits = new ArrayList<>(); + } + if (mSelectedOtherSubreddits == null) { + mSelectedOtherSubreddits = new ArrayList<>(); + } + ArrayList<SubredditWithSelection> selectedSubscribedSubreddits = data.getParcelableArrayListExtra( + SubredditMultiselectionActivity.EXTRA_RETURN_SELECTED_SUBSCRIBED_SUBREDDITS); + ArrayList<SubredditWithSelection> selectedOtherSubreddits = data.getParcelableArrayListExtra( + SubredditMultiselectionActivity.EXTRA_RETURN_SUBSCRIBED_OTHER_SUBREDDITS); + if (selectedSubscribedSubreddits != null) { + mSelectedSubscribedSubreddits.clear(); + mSelectedSubscribedSubreddits.addAll(selectedSubscribedSubreddits); + } + if (selectedOtherSubreddits != null) { + mSelectedOtherSubreddits.clear(); + mSelectedOtherSubreddits.addAll(selectedOtherSubreddits); + } + + descriptionEditText.setText(mSelectedSubscribedSubreddits.toString() + mSelectedOtherSubreddits.toString()); + } + } + } + + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); + outState.putParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE, mSelectedSubscribedSubreddits); + outState.putParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE, mSelectedOtherSubreddits); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchSubredditsResultActivity.java index 5d3c0bf1..c68040ff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchSubredditsResultActivity.java @@ -175,9 +175,7 @@ public class SearchSubredditsResultActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - if (mFragment != null) { - getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); - } + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java new file mode 100644 index 00000000..87b45f3c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java @@ -0,0 +1,268 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.Intent; +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.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.Toolbar; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.google.android.material.appbar.AppBarLayout; + +import java.util.ArrayList; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Adapter.SubredditMultiselectionRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; +import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditWithSelection; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditViewModel; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; +import retrofit2.Retrofit; + +public class SubredditMultiselectionActivity extends BaseActivity { + + static final String EXTRA_SELECTED_SUBSCRIBED_SUBREDDITS = "ESS"; + static final String EXTRA_SELECTED_OTHER_SUBREDDITS = "EOSS"; + static final String EXTRA_RETURN_SELECTED_SUBSCRIBED_SUBREDDITS = "ERSSS"; + static final String EXTRA_RETURN_SUBSCRIBED_OTHER_SUBREDDITS = "EROSS"; + + private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 1; + 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"; + private static final String SELECTED_SUBSCRIBED_SUBREDDITS_STATE = "SSSS"; + private static final String SELECTED_OTHER_SUBREDDITS_STATE = "SOSS"; + + @BindView(R.id.appbar_layout_subreddits_multiselection_activity) + AppBarLayout mAppBarLayout; + @BindView(R.id.toolbar_subscribed_subreddits_multiselection_activity) + Toolbar mToolbar; + @BindView(R.id.swipe_refresh_layout_subscribed_subscribed_subreddits_multiselection_activity) + SwipeRefreshLayout mSwipeRefreshLayout; + @BindView(R.id.recycler_view_subscribed_subscribed_subreddits_multiselection_activity) + RecyclerView mRecyclerView; + @BindView(R.id.no_subscriptions_linear_layout_subscribed_subreddits_multiselection_activity) + LinearLayout mLinearLayout; + @BindView(R.id.no_subscriptions_image_view_subscribed_subreddits_multiselection_activity) + ImageView mImageView; + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + SharedPreferences mSharedPreferences; + private boolean mNullAccessToken = false; + private String mAccessToken; + private String mAccountName; + private SubredditMultiselectionRecyclerViewAdapter mAdapter; + private RequestManager mGlide; + private SubscribedSubredditViewModel mSubscribedSubredditViewModel; + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_subscribed_subreddits_multiselection); + + 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(); + if (finalLightNavBar) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + mAppBarLayout.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 statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mToolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + mToolbar.setLayoutParams(params); + } + + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + + setSupportActionBar(mToolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + mGlide = Glide.with(this); + + mSwipeRefreshLayout.setEnabled(false); + + if (savedInstanceState != null) { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + + if (!mNullAccessToken && mAccountName == null) { + getCurrentAccountAndBindView(savedInstanceState.getParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE), + savedInstanceState.getParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE)); + } else { + bindView(savedInstanceState.getParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE), + savedInstanceState.getParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE)); + } + } else { + getCurrentAccountAndBindView(getIntent().getParcelableArrayListExtra(EXTRA_SELECTED_SUBSCRIBED_SUBREDDITS), + getIntent().getParcelableArrayListExtra(EXTRA_SELECTED_OTHER_SUBREDDITS)); + } + } + + private void getCurrentAccountAndBindView(ArrayList<SubredditWithSelection> selectedSubscribedSubreddits, + ArrayList<SubredditWithSelection> otherSubreddits) { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if (account == null) { + mNullAccessToken = true; + Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); + finish(); + } else { + mAccessToken = account.getAccessToken(); + mAccountName = account.getUsername(); + bindView(selectedSubscribedSubreddits, otherSubreddits); + } + }).execute(); + } + + private void bindView(ArrayList<SubredditWithSelection> selectedSubscribedSubreddits, + ArrayList<SubredditWithSelection> otherSubreddits) { + mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mAdapter = new SubredditMultiselectionRecyclerViewAdapter(this, selectedSubscribedSubreddits, + otherSubreddits); + mRecyclerView.setAdapter(mAdapter); + + mSubscribedSubredditViewModel = new ViewModelProvider(this, + new SubscribedSubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)) + .get(SubscribedSubredditViewModel.class); + mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, subscribedSubredditData -> { + mSwipeRefreshLayout.setRefreshing(false); + if (subscribedSubredditData == null || subscribedSubredditData.size() == 0) { + mRecyclerView.setVisibility(View.GONE); + mLinearLayout.setVisibility(View.VISIBLE); + mGlide.load(R.drawable.error_image).into(mImageView); + } else { + mLinearLayout.setVisibility(View.GONE); + mRecyclerView.setVisibility(View.VISIBLE); + mGlide.clear(mImageView); + } + + mAdapter.setSubscribedSubreddits(subscribedSubredditData); + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.subreddit_multiselection_activity, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_save_subreddit_multiselection_activity: + if (mAdapter != null) { + Intent returnIntent = new Intent(); + returnIntent.putParcelableArrayListExtra(EXTRA_RETURN_SELECTED_SUBSCRIBED_SUBREDDITS, + mAdapter.getAllSelectedSubscribedSubreddits()); + returnIntent.putParcelableArrayListExtra(EXTRA_RETURN_SUBSCRIBED_OTHER_SUBREDDITS, + mAdapter.getAllSelectedOtherSubreddits()); + setResult(RESULT_OK, returnIntent); + } + finish(); + return true; + case R.id.action_search_subreddit_multiselection_activity: + Intent intent = new Intent(this, SearchActivity.class); + intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, true); + startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); + } + + return false; + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == SUBREDDIT_SEARCH_REQUEST_CODE && resultCode == RESULT_OK && data != null + && mAdapter != null) { + String subredditName = data.getStringExtra(SearchActivity.EXTRA_RETURN_SUBREDDIT_NAME); + String subredditIconUrl = data.getStringExtra(SearchActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); + mAdapter.addOtherSubreddit(new SubredditData("-1", subredditName, subredditIconUrl, + null, null, null, 0)); + } + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); + outState.putParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE, mAdapter.getAllSelectedSubscribedSubreddits()); + outState.putParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE, mAdapter.getAllOtherSubreddits()); + } + + @Override + public SharedPreferences getSharedPreferences() { + return mSharedPreferences; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java index e6bbd51c..1bd90df3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java @@ -270,9 +270,7 @@ public class SubredditSelectionActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - if (mFragment != null) { - getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); - } + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE, mInsertSuccess); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java new file mode 100644 index 00000000..350ef8fe --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java @@ -0,0 +1,277 @@ +package ml.docilealligator.infinityforreddit.Adapter; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.request.RequestOptions; + +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditWithSelection; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import pl.droidsonroids.gif.GifImageView; + +public class SubredditMultiselectionRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + + private static final int SUBSCRIBED_SUBREDDIT_VIEW_TYPE = 0; + private static final int OTHER_SUBREDDIT_VIEW_TYPE = 1; + + private ArrayList<SubredditWithSelection> subscribedSubreddits; + private ArrayList<SubredditWithSelection> otherSubreddits; + private ArrayList<SubredditWithSelection> selectedSubscribedSubreddits; + private ArrayList<SubredditWithSelection> selectedOtherSubreddits; + private Context context; + private RequestManager glide; + + public SubredditMultiselectionRecyclerViewAdapter(Context context, + ArrayList<SubredditWithSelection> selectedSubscribedSubreddits, + ArrayList<SubredditWithSelection> otherSubreddits) { + this.context = context; + glide = Glide.with(context); + subscribedSubreddits = new ArrayList<>(); + this.otherSubreddits = new ArrayList<>(); + this.selectedSubscribedSubreddits = new ArrayList<>(); + selectedOtherSubreddits = new ArrayList<>(); + + if (selectedSubscribedSubreddits != null) { + this.selectedSubscribedSubreddits.addAll(selectedSubscribedSubreddits); + } + + if (otherSubreddits != null) { + this.selectedOtherSubreddits.addAll(otherSubreddits); + this.otherSubreddits.addAll(otherSubreddits); + } + } + + @Override + public int getItemViewType(int position) { + if (otherSubreddits.size() > 0) { + if (position >= otherSubreddits.size()) { + return SUBSCRIBED_SUBREDDIT_VIEW_TYPE; + } + + return OTHER_SUBREDDIT_VIEW_TYPE; + } + + return SUBSCRIBED_SUBREDDIT_VIEW_TYPE; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + if (viewType == SUBSCRIBED_SUBREDDIT_VIEW_TYPE) { + return new SubscribedSubredditViewHolder(LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_subscribed_subreddit_multi_selection, parent, false)); + } else { + return new OtherSubredditViewHolder(LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_subscribed_subreddit_multi_selection, parent, false)); + } + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (holder instanceof SubscribedSubredditViewHolder) { + int offset = otherSubreddits.size(); + ((SubscribedSubredditViewHolder) holder).nameTextView.setText(subscribedSubreddits.get(position - offset).getName()); + glide.load(subscribedSubreddits.get(position - offset).getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((SubscribedSubredditViewHolder) holder).iconImageView); + if (subscribedSubreddits.get(position - offset).isSelected()) { + ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(true); + } else { + ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(false); + } + ((SubscribedSubredditViewHolder) holder).checkBox.setOnClickListener(view -> { + if (subscribedSubreddits.get(position - offset).isSelected()) { + ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(false); + subscribedSubreddits.get(position - offset).setSelected(false); + selectedSubscribedSubreddits.remove(subscribedSubreddits.get(position - offset)); + } else { + ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(true); + subscribedSubreddits.get(position - offset).setSelected(true); + insertIntoSelectedSubredditsArrayListAscend(selectedSubscribedSubreddits, + subscribedSubreddits.get(position - offset)); + } + }); + ((SubscribedSubredditViewHolder) holder).itemView.setOnClickListener(view -> + ((SubscribedSubredditViewHolder) holder).checkBox.performClick()); + } else if (holder instanceof OtherSubredditViewHolder) { + SubredditWithSelection subreddit = otherSubreddits.get(position); + ((OtherSubredditViewHolder) holder).nameTextView.setText(subreddit.getName()); + glide.load(subreddit.getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((OtherSubredditViewHolder) holder).iconImageView); + if (subreddit.isSelected()) { + ((OtherSubredditViewHolder) holder).checkBox.setChecked(true); + } else { + ((OtherSubredditViewHolder) holder).checkBox.setChecked(false); + } + ((OtherSubredditViewHolder) holder).checkBox.setOnClickListener(view -> { + if (subreddit.isSelected()) { + ((OtherSubredditViewHolder) holder).checkBox.setChecked(false); + subreddit.setSelected(false); + selectedOtherSubreddits.remove(subreddit); + } else { + ((OtherSubredditViewHolder) holder).checkBox.setChecked(true); + subreddit.setSelected(true); + insertIntoSelectedSubredditsArrayListAscend(selectedOtherSubreddits, subreddit); + } + }); + ((OtherSubredditViewHolder) holder).itemView.setOnClickListener(view -> + ((OtherSubredditViewHolder) holder).checkBox.performClick()); + } + } + + @Override + public int getItemCount() { + return subscribedSubreddits.size() + otherSubreddits.size(); + } + + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + super.onViewRecycled(holder); + if (holder instanceof SubscribedSubredditViewHolder) { + glide.clear(((SubscribedSubredditViewHolder) holder).iconImageView); + } else if (holder instanceof OtherSubredditViewHolder) { + glide.clear(((OtherSubredditViewHolder) holder).iconImageView); + } + } + + public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) { + if (this.subscribedSubreddits.isEmpty()) { + ArrayList<SubredditWithSelection> temp = + SubredditWithSelection.convertSubscribedSubreddits(subscribedSubreddits); + this.subscribedSubreddits.addAll(temp); + checkAllSelectedSubreddits(0, 0); + notifyDataSetChanged(); + } + } + + public void addOtherSubreddit(SubredditData subreddit) { + SubredditWithSelection subredditWithSelection = + SubredditWithSelection.convertSubreddit(subreddit); + subredditWithSelection.setSelected(true); + int i = subscribedSubreddits.indexOf(subredditWithSelection); + if (i > 0) { + subscribedSubreddits.get(i).setSelected(true); + insertIntoSelectedSubredditsArrayListAscend(selectedSubscribedSubreddits, subredditWithSelection); + Toast.makeText(context, context.getString(R.string.subreddit_selected, subreddit.getName()), Toast.LENGTH_SHORT).show(); + return; + } + insertIntoOtherSubredditsArrayListAscend(subredditWithSelection); + insertIntoSelectedSubredditsArrayListAscend(selectedOtherSubreddits, subredditWithSelection); + } + + public ArrayList<SubredditWithSelection> getAllSelectedSubscribedSubreddits() { + return new ArrayList<>(selectedSubscribedSubreddits); + } + + public ArrayList<SubredditWithSelection> getAllSelectedOtherSubreddits() { + return new ArrayList<>(selectedOtherSubreddits); + } + + public ArrayList<SubredditWithSelection> getAllOtherSubreddits() { + return new ArrayList<>(otherSubreddits); + } + + private void insertIntoSelectedSubredditsArrayListAscend(ArrayList<SubredditWithSelection> subreddits, + SubredditWithSelection subreddit) { + for (int i = 0; i < subreddits.size(); i++) { + if (subreddits.get(i).compareName(subreddit) < 0) { + continue; + } + if (subreddits.get(i).compareName(subreddit) == 0) { + return; + } + subreddits.add(i, subreddit); + return; + } + subreddits.add(subreddit); + } + + private void insertIntoOtherSubredditsArrayListAscend(SubredditWithSelection subreddit) { + for (int i = 0; i < otherSubreddits.size(); i++) { + if (otherSubreddits.get(i).compareName(subreddit) < 0) { + continue; + } + if (otherSubreddits.get(i).compareName(subreddit) == 0) { + return; + } + otherSubreddits.add(i, subreddit); + notifyItemInserted(i); + return; + } + otherSubreddits.add(subreddit); + notifyItemInserted(otherSubreddits.size() - 1); + } + + private void checkAllSelectedSubreddits(int i1, int i2) { + if (selectedSubscribedSubreddits.size() <= i1 || subscribedSubreddits.size() <= i2) { + return; + } + + if (selectedSubscribedSubreddits.get(i1).compareName(subscribedSubreddits.get(i2)) == 0) { + subscribedSubreddits.get(i2).setSelected(true); + checkAllSelectedSubreddits(i1 + 1, i2 + 1); + } else if (selectedSubscribedSubreddits.get(i1).compareName(subscribedSubreddits.get(i2)) < 0) { + //Insert to other subreddits + insertIntoOtherSubredditsArrayListAscend(selectedSubscribedSubreddits.get(i1)); + insertIntoSelectedSubredditsArrayListAscend(selectedOtherSubreddits, selectedSubscribedSubreddits.get(i1)); + checkAllSelectedSubreddits(i1 + 1, i2); + } else { + checkAllSelectedSubreddits(i1, i2 + 1); + } + } + + class SubscribedSubredditViewHolder extends RecyclerView.ViewHolder { + View itemView; + @BindView(R.id.icon_gif_image_view_item_subscribed_subreddit_multiselection) + GifImageView iconImageView; + @BindView(R.id.name_text_view_item_subscribed_subreddit_multiselection) + TextView nameTextView; + @BindView(R.id.checkbox_item_subscribed_subreddit_multiselection) + CheckBox checkBox; + + SubscribedSubredditViewHolder(@NonNull View itemView) { + super(itemView); + this.itemView = itemView; + ButterKnife.bind(this, itemView); + } + } + + class OtherSubredditViewHolder extends RecyclerView.ViewHolder { + View itemView; + @BindView(R.id.icon_gif_image_view_item_subscribed_subreddit_multiselection) + GifImageView iconImageView; + @BindView(R.id.name_text_view_item_subscribed_subreddit_multiselection) + TextView nameTextView; + @BindView(R.id.checkbox_item_subscribed_subreddit_multiselection) + CheckBox checkBox; + + OtherSubredditViewHolder(@NonNull View itemView) { + super(itemView); + this.itemView = itemView; + ButterKnife.bind(this, itemView); + } + } +}
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java index c379cdc9..3b2d43f9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java @@ -67,13 +67,6 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte this.itemClickListener = itemClickListener; } - /*public SubscribedSubredditsRecyclerViewAdapter(Context context, boolean hasClearSelectionRow, ItemClickListener itemClickListener) { - mContext = context; - this.hasClearSelectionRow = hasClearSelectionRow; - glide = Glide.with(context.getApplicationContext()); - this.itemClickListener = itemClickListener; - }*/ - @Override public int getItemViewType(int position) { if (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 124f1d64..efe1a48a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -24,6 +24,7 @@ import ml.docilealligator.infinityforreddit.Activity.SearchResultActivity; import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActivity; import ml.docilealligator.infinityforreddit.Activity.SettingsActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity; +import ml.docilealligator.infinityforreddit.Activity.SubredditMultiselectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActivity; import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; @@ -134,4 +135,6 @@ public interface AppComponent { void inject(GesturesAndButtonsPreferenceFragment gesturesAndButtonsPreferenceFragment); void inject(CreateMultiRedditActivity createMultiRedditActivity); + + void inject(SubredditMultiselectionActivity subredditMultiselectionActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java index 91d72ae1..c76b726e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java @@ -49,8 +49,8 @@ public class InsertMultiRedditAsyncTask extends AsyncTask<Void, Void, Void> { } private void compareTwoMultiRedditList(List<MultiReddit> newMultiReddits, - List<MultiReddit> oldMultiReddits, - List<String> deletedMultiReddits, int i1, int i2) { + List<MultiReddit> oldMultiReddits, + List<String> deletedMultiReddits, int i1, int i2) { if (newMultiReddits.size() <= i1 && oldMultiReddits.size() <= i2) { return; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java index 9f8c98fe..7d16277b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java @@ -82,9 +82,8 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); - Resources resources = getResources(); - 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)) { int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (navBarResourceId > 0) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java index 812319e2..74e8ddd1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java @@ -4,6 +4,8 @@ import com.google.gson.Gson; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.SubredditWithSelection; + public class MultiRedditJSONModel { private String display_name; private String description_md; @@ -12,7 +14,8 @@ public class MultiRedditJSONModel { public MultiRedditJSONModel() {} - public MultiRedditJSONModel(String display_name, String description_md, boolean isPrivate, ArrayList<String> subreddits) { + public MultiRedditJSONModel(String display_name, String description_md, boolean isPrivate, + ArrayList<SubredditWithSelection> subreddits) { this.display_name = display_name; this.description_md = description_md; if (isPrivate) { @@ -24,7 +27,7 @@ public class MultiRedditJSONModel { if (subreddits != null) { this.subreddits = new SubredditInMultiReddit[subreddits.size()]; for (int i = 0; i < subreddits.size(); i++) { - SubredditInMultiReddit subredditInMultiReddit = new SubredditInMultiReddit(subreddits.get(i)); + SubredditInMultiReddit subredditInMultiReddit = new SubredditInMultiReddit(subreddits.get(i).getName()); this.subreddits[i] = subredditInMultiReddit; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java new file mode 100644 index 00000000..bb95c446 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java @@ -0,0 +1,102 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.Parcel; +import android.os.Parcelable; + +import androidx.annotation.Nullable; + +import java.util.ArrayList; +import java.util.List; + +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; + +public class SubredditWithSelection implements Parcelable { + private String name; + private String iconUrl; + private boolean selected; + + public SubredditWithSelection(String name, String iconUrl) { + this.name = name; + this.iconUrl = iconUrl; + selected = false; + } + + protected SubredditWithSelection(Parcel in) { + super(); + name = in.readString(); + iconUrl = in.readString(); + selected = in.readByte() != 0; + } + + public static final Creator<SubredditWithSelection> CREATOR = new Creator<SubredditWithSelection>() { + @Override + public SubredditWithSelection createFromParcel(Parcel in) { + return new SubredditWithSelection(in); + } + + @Override + public SubredditWithSelection[] newArray(int size) { + return new SubredditWithSelection[size]; + } + }; + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public boolean isSelected() { + return selected; + } + + public void setSelected(boolean selected) { + this.selected = selected; + } + + public static ArrayList<SubredditWithSelection> convertSubscribedSubreddits( + List<SubscribedSubredditData> subscribedSubredditData) { + ArrayList<SubredditWithSelection> subredditWithSelections = new ArrayList<>(); + for (SubscribedSubredditData s : subscribedSubredditData) { + subredditWithSelections.add(new SubredditWithSelection(s.getName(), s.getIconUrl())); + } + + return subredditWithSelections; + } + + public static SubredditWithSelection convertSubreddit(SubredditData subreddit) { + return new SubredditWithSelection(subreddit.getName(), subreddit.getIconUrl()); + } + + public int compareName(SubredditWithSelection subredditWithSelection) { + if (subredditWithSelection != null) { + return name.compareToIgnoreCase(subredditWithSelection.getName()); + } else { + return -1; + } + } + + @Override + public boolean equals(@Nullable Object obj) { + if (!(obj instanceof SubredditWithSelection)) { + return false; + } else { + return this.getName().compareToIgnoreCase(((SubredditWithSelection) obj).getName()) == 0; + } + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(name); + parcel.writeString(iconUrl); + parcel.writeByte((byte) (selected ? 1 : 0)); + } +} |