diff options
Diffstat (limited to 'app/src/main/java')
19 files changed, 719 insertions, 279 deletions
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 3ce4ce2f..be346485 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java @@ -35,7 +35,6 @@ 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 { @@ -44,8 +43,7 @@ public class CreateMultiRedditActivity extends BaseActivity { 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"; + private static final String SELECTED_SUBREDDITS_STATE = "SSS"; @BindView(R.id.coordinator_layout_create_multi_reddit_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_create_multi_reddit_activity) @@ -81,8 +79,7 @@ public class CreateMultiRedditActivity extends BaseActivity { private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; - private ArrayList<SubredditWithSelection> mSelectedSubscribedSubreddits; - private ArrayList<SubredditWithSelection> mSelectedOtherSubreddits; + private ArrayList<String> mSubreddits; @Override protected void onCreate(Bundle savedInstanceState) { @@ -105,8 +102,7 @@ public class CreateMultiRedditActivity extends BaseActivity { 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); + mSubreddits = savedInstanceState.getStringArrayList(SELECTED_SUBREDDITS_STATE); if (!mNullAccessToken && mAccountName == null) { getCurrentAccountAndBindView(); @@ -114,8 +110,7 @@ public class CreateMultiRedditActivity extends BaseActivity { bindView(); } } else { - mSelectedSubscribedSubreddits = new ArrayList<>(); - mSelectedOtherSubreddits = new ArrayList<>(); + mSubreddits = new ArrayList<>(); getCurrentAccountAndBindView(); } } @@ -136,11 +131,8 @@ public class CreateMultiRedditActivity extends BaseActivity { 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); + Intent intent = new Intent(CreateMultiRedditActivity.this, SelectedSubredditsActivity.class); + intent.putStringArrayListExtra(SelectedSubredditsActivity.EXTRA_SELECTED_SUBREDDITS, mSubreddits); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); }); } @@ -158,7 +150,7 @@ public class CreateMultiRedditActivity extends BaseActivity { case android.R.id.home: finish(); return true; - case R.id.action_create_create_multi_reddit_activity: + case R.id.action_save_create_multi_reddit_activity: if (mAccountName == null || mAccessToken == null) { Snackbar.make(coordinatorLayout, R.string.something_went_wrong, Snackbar.LENGTH_SHORT).show(); return true; @@ -168,11 +160,8 @@ public class CreateMultiRedditActivity extends BaseActivity { return true; } - ArrayList<SubredditWithSelection> allSelectedSubreddits = new ArrayList<>(); - allSelectedSubreddits.addAll(mSelectedSubscribedSubreddits); - allSelectedSubreddits.addAll(mSelectedOtherSubreddits); String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(), - visibilitySwitch.isChecked(), allSelectedSubreddits).createJSONModel(); + visibilitySwitch.isChecked(), mSubreddits).createJSONModel(); CreateMultiReddit.createMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, "/user/" + mAccountName + "/m/" + nameEditText.getText().toString(), jsonModel, new CreateMultiReddit.CreateMultiRedditListener() { @@ -199,26 +188,8 @@ public class CreateMultiRedditActivity extends BaseActivity { 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()); + mSubreddits = data.getStringArrayListExtra( + SubredditMultiselectionActivity.EXTRA_RETURN_SELECTED_SUBREDDITS); } } } @@ -229,8 +200,7 @@ public class CreateMultiRedditActivity extends BaseActivity { 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); + outState.putStringArrayList(SELECTED_SUBREDDITS_STATE, mSubreddits); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditMultiRedditActivity.java new file mode 100644 index 00000000..d914b236 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditMultiRedditActivity.java @@ -0,0 +1,263 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.ColorStateList; +import android.os.Build; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +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; + +import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.snackbar.Snackbar; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MultiReddit.EditMultiReddit; +import ml.docilealligator.infinityforreddit.MultiReddit.FetchMultiRedditInfo; +import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; +import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditJSONModel; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import retrofit2.Retrofit; + +public class EditMultiRedditActivity extends BaseActivity { + public static final String EXTRA_MULTI_REDDIT = "EMR"; + public static final String EXTRA_MULTI_PATH = "EMP"; + 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 MULTI_REDDIT_STATE = "MRS"; + private static final String MULTI_PATH_STATE = "MPS"; + @BindView(R.id.coordinator_layout_edit_multi_reddit_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_edit_multi_reddit_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_edit_multi_reddit_activity) + Toolbar toolbar; + @BindView(R.id.progress_bar_edit_multi_reddit_activity) + ProgressBar progressBar; + @BindView(R.id.linear_layout_edit_multi_reddit_activity) + LinearLayout linearLayout; + @BindView(R.id.multi_reddit_name_edit_text_edit_multi_reddit_activity) + EditText nameEditText; + @BindView(R.id.divider_1_edit_multi_reddit_activity) + View divider1; + @BindView(R.id.description_edit_text_edit_multi_reddit_activity) + EditText descriptionEditText; + @BindView(R.id.divider_2_edit_multi_reddit_activity) + View divider2; + @BindView(R.id.visibility_wrapper_linear_layout_edit_multi_reddit_activity) + LinearLayout visibilityLinearLayout; + @BindView(R.id.visibility_text_view_edit_multi_reddit_activity) + TextView visibilityTextView; + @BindView(R.id.visibility_switch_edit_multi_reddit_activity) + Switch visibilitySwitch; + @BindView(R.id.select_subreddit_text_view_edit_multi_reddit_activity) + TextView selectSubredditTextView; + @Inject + @Named("oauth") + Retrofit mRetrofit; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + private boolean mNullAccessToken = false; + private String mAccessToken; + private String mAccountName; + private MultiReddit multiReddit; + private String multipath; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_edit_multi_reddit); + + ButterKnife.bind(this); + + applyCustomTheme(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + if (savedInstanceState != null) { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + multiReddit = savedInstanceState.getParcelable(MULTI_REDDIT_STATE); + multipath = savedInstanceState.getString(MULTI_PATH_STATE); + + if (!mNullAccessToken && mAccountName == null) { + getCurrentAccountAndBindView(); + } else { + bindView(); + } + } else { + multiReddit = getIntent().getParcelableExtra(EXTRA_MULTI_REDDIT); + multipath = getIntent().getStringExtra(EXTRA_MULTI_PATH); + 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(); + } + }).execute(); + } + + private void bindView() { + if (multiReddit == null) { + FetchMultiRedditInfo.fetchMultiRedditInfo(mRetrofit, mAccessToken, multipath, new FetchMultiRedditInfo.FetchMultiRedditInfoListener() { + @Override + public void success(MultiReddit multiReddit) { + EditMultiRedditActivity.this.multiReddit = multiReddit; + progressBar.setVisibility(View.GONE); + linearLayout.setVisibility(View.VISIBLE); + nameEditText.setText(multiReddit.getDisplayName()); + descriptionEditText.setText(multiReddit.getDescription()); + visibilitySwitch.setChecked(!multiReddit.getVisibility().equals("public")); + } + + @Override + public void failed() { + Snackbar.make(coordinatorLayout, R.string.cannot_fetch_multireddit, Snackbar.LENGTH_SHORT).show(); + } + }); + } else { + progressBar.setVisibility(View.GONE); + linearLayout.setVisibility(View.VISIBLE); + nameEditText.setText(multiReddit.getDisplayName()); + descriptionEditText.setText(multiReddit.getDescription()); + visibilitySwitch.setChecked(!multiReddit.getVisibility().equals("public")); + } + + selectSubredditTextView.setOnClickListener(view -> { + Intent intent = new Intent(EditMultiRedditActivity.this, SelectedSubredditsActivity.class); + if (multiReddit.getSubreddits() != null) { + intent.putStringArrayListExtra(SelectedSubredditsActivity.EXTRA_SELECTED_SUBREDDITS, multiReddit.getSubreddits()); + } + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.edit_multi_reddit_activity, menu); + applyMenuItemTheme(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_edit_multi_reddit_activity: + if (mAccountName == null || mAccessToken == null) { + Snackbar.make(coordinatorLayout, R.string.something_went_wrong, Snackbar.LENGTH_SHORT).show(); + return true; + } + if (nameEditText.getText() == null || nameEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.no_multi_reddit_name, Snackbar.LENGTH_SHORT).show(); + return true; + } + + String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(), + visibilitySwitch.isChecked(), multiReddit.getSubreddits()).createJSONModel(); + EditMultiReddit.editMultiReddit(mRetrofit, mAccessToken, multiReddit.getPath(), + jsonModel, new EditMultiReddit.EditMultiRedditListener() { + @Override + public void success() { + finish(); + } + + @Override + public void failed() { + Snackbar.make(coordinatorLayout, R.string.edit_multi_reddit_failed, Snackbar.LENGTH_SHORT).show(); + } + }); + } + return false; + } + + @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) { + multiReddit.setSubreddits(data.getStringArrayListExtra( + SubredditMultiselectionActivity.EXTRA_RETURN_SELECTED_SUBREDDITS)); + } + } + } + + @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.putParcelable(MULTI_REDDIT_STATE, multiReddit); + outState.putString(MULTI_PATH_STATE, multipath); + } + + @Override + public SharedPreferences getSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + progressBar.setIndeterminateTintList(ColorStateList.valueOf(mCustomThemeWrapper.getColorAccent())); + int primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor(); + nameEditText.setTextColor(primaryTextColor); + int dividerColor = mCustomThemeWrapper.getDividerColor(); + divider1.setBackgroundColor(dividerColor); + divider2.setBackgroundColor(dividerColor); + descriptionEditText.setTextColor(primaryTextColor); + visibilityTextView.setTextColor(primaryTextColor); + selectSubredditTextView.setTextColor(primaryTextColor); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java index a4ccc4b7..50357ff0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java @@ -38,7 +38,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask import ml.docilealligator.infinityforreddit.AsyncTask.InsertMultiRedditAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.MultiReddit.FetchMultiReddit; +import ml.docilealligator.infinityforreddit.MultiReddit.FetchMyMultiReddits; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditViewModel; import ml.docilealligator.infinityforreddit.R; @@ -204,7 +204,7 @@ public class MultiRedditListingActivity extends BaseActivity { private void loadMultiReddits() { mSwipeRefreshLayout.setRefreshing(true); - FetchMultiReddit.fetchMyMultiReddits(mOauthRetrofit, mAccessToken, new FetchMultiReddit.FetchMultiRedditListener() { + FetchMyMultiReddits.fetchMyMultiReddits(mOauthRetrofit, mAccessToken, new FetchMyMultiReddits.FetchMyMultiRedditsListener() { @Override public void success(ArrayList<MultiReddit> multiReddits) { new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, mAccountName, () -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SelectedSubredditsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SelectedSubredditsActivity.java new file mode 100644 index 00000000..7e136584 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SelectedSubredditsActivity.java @@ -0,0 +1,146 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.floatingactionbutton.FloatingActionButton; + +import java.util.ArrayList; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Adapter.SelectedSubredditsRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; + +public class SelectedSubredditsActivity extends BaseActivity { + + public static final String EXTRA_SELECTED_SUBREDDITS = "ESS"; + public static final String EXTRA_RETURN_SELECTED_SUBREDDITS = "ERSS"; + private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 1; + private static final String SELECTED_SUBREDDITS_STATE = "SSS"; + + @BindView(R.id.coordinator_layout_selected_subreddits_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_selected_subreddits_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_selected_subreddits_activity) + Toolbar toolbar; + @BindView(R.id.recycler_view_selected_subreddits_activity) + RecyclerView recyclerView; + @BindView(R.id.fab_selected_subreddits_activity) + FloatingActionButton fab; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + private SelectedSubredditsRecyclerViewAdapter adapter; + private ArrayList<String> subreddits; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_selected_subreddits); + + ButterKnife.bind(this); + applyCustomTheme(); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + if (savedInstanceState != null) { + subreddits = savedInstanceState.getStringArrayList(SELECTED_SUBREDDITS_STATE); + } else { + subreddits = getIntent().getStringArrayListExtra(EXTRA_SELECTED_SUBREDDITS); + } + + adapter = new SelectedSubredditsRecyclerViewAdapter(mCustomThemeWrapper, subreddits); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView.setAdapter(adapter); + + fab.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditMultiselectionActivity.class); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.selected_subreddits_activity, menu); + applyMenuItemTheme(menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } else if (item.getItemId() == R.id.action_save_selected_subreddits_activity) { + if (adapter != null) { + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_SELECTED_SUBREDDITS, adapter.getSubreddits()); + setResult(Activity.RESULT_OK, returnIntent); + } + finish(); + } + return false; + } + + @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 (subreddits == null) { + subreddits = new ArrayList<>(); + } + subreddits = data.getStringArrayListExtra(SubredditMultiselectionActivity.EXTRA_RETURN_SELECTED_SUBREDDITS); + adapter.addSubreddits(subreddits); + } + } + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + if (adapter != null) { + outState.putStringArrayList(SELECTED_SUBREDDITS_STATE, adapter.getSubreddits()); + } + } + + @Override + protected SharedPreferences getSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + applyFABTheme(fab, R.drawable.ic_add_24dp); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java index 569d02e5..d0ffcfa8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java @@ -40,24 +40,17 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; 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 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"; + static final String EXTRA_RETURN_SELECTED_SUBREDDITS = "ERSS"; 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.coordinator_layout_subreddits_multiselection_activity) CoordinatorLayout mCoordinatorLayout; @@ -132,20 +125,16 @@ public class SubredditMultiselectionActivity extends BaseActivity { mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); if (!mNullAccessToken && mAccountName == null) { - getCurrentAccountAndBindView(savedInstanceState.getParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE), - savedInstanceState.getParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE)); + getCurrentAccountAndBindView(); } else { - bindView(savedInstanceState.getParcelableArrayList(SELECTED_SUBSCRIBED_SUBREDDITS_STATE), - savedInstanceState.getParcelableArrayList(SELECTED_OTHER_SUBREDDITS_STATE)); + bindView(); } } else { - getCurrentAccountAndBindView(getIntent().getParcelableArrayListExtra(EXTRA_SELECTED_SUBSCRIBED_SUBREDDITS), - getIntent().getParcelableArrayListExtra(EXTRA_SELECTED_OTHER_SUBREDDITS)); + getCurrentAccountAndBindView(); } } - private void getCurrentAccountAndBindView(ArrayList<SubredditWithSelection> selectedSubscribedSubreddits, - ArrayList<SubredditWithSelection> otherSubreddits) { + private void getCurrentAccountAndBindView() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if (account == null) { mNullAccessToken = true; @@ -154,16 +143,14 @@ public class SubredditMultiselectionActivity extends BaseActivity { } else { mAccessToken = account.getAccessToken(); mAccountName = account.getUsername(); - bindView(selectedSubscribedSubreddits, otherSubreddits); + bindView(); } }).execute(); } - private void bindView(ArrayList<SubredditWithSelection> selectedSubscribedSubreddits, - ArrayList<SubredditWithSelection> otherSubreddits) { + private void bindView() { mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - mAdapter = new SubredditMultiselectionRecyclerViewAdapter(this, mCustomThemeWrapper, - selectedSubscribedSubreddits, otherSubreddits); + mAdapter = new SubredditMultiselectionRecyclerViewAdapter(this, mCustomThemeWrapper); mRecyclerView.setAdapter(mAdapter); mSubscribedSubredditViewModel = new ViewModelProvider(this, @@ -201,10 +188,8 @@ public class SubredditMultiselectionActivity extends BaseActivity { 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()); + returnIntent.putStringArrayListExtra(EXTRA_RETURN_SELECTED_SUBREDDITS, + mAdapter.getAllSelectedSubreddits()); setResult(RESULT_OK, returnIntent); } finish(); @@ -221,12 +206,13 @@ public class SubredditMultiselectionActivity extends BaseActivity { @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)); + if (requestCode == SUBREDDIT_SEARCH_REQUEST_CODE && resultCode == RESULT_OK && data != null) { + Intent returnIntent = new Intent(); + ArrayList<String> selectedSubreddits = mAdapter.getAllSelectedSubreddits(); + selectedSubreddits.add(data.getStringExtra(SearchActivity.EXTRA_RETURN_SUBREDDIT_NAME)); + returnIntent.putStringArrayListExtra(EXTRA_RETURN_SELECTED_SUBREDDITS, selectedSubreddits); + setResult(RESULT_OK, returnIntent); + finish(); } } @@ -236,8 +222,6 @@ public class SubredditMultiselectionActivity extends BaseActivity { 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 diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java index d6d32b67..168d7276 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java @@ -33,6 +33,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MultiReddit.DeleteMultiReddit; +import ml.docilealligator.infinityforreddit.MultiReddit.EditMultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; @@ -248,7 +249,12 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT case R.id.action_change_post_layout_view_multi_reddit_detail_activity: postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); return true; - case R.id.action_delte_view_multi_reddit_detail_activity: + case R.id.action_edit_view_multi_reddit_detail_activity: + Intent editIntent = new Intent(this, EditMultiRedditActivity.class); + editIntent.putExtra(EditMultiRedditActivity.EXTRA_MULTI_PATH, multiPath); + startActivity(editIntent); + return true; + case R.id.action_delete_view_multi_reddit_detail_activity: new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) .setTitle(R.string.delete) .setMessage(R.string.delete_multi_reddit_dialog_message) 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 0022c7c5..4463f071 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MultiRedditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MultiRedditListingRecyclerViewAdapter.java @@ -109,7 +109,7 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter< mFavoriteMultiReddits.size() + 2 : 0; MultiReddit multiReddit = mMultiReddits.get(holder.getAdapterPosition() - offset); - name = multiReddit.getName(); + name = multiReddit.getDisplayName(); iconUrl = multiReddit.getIconUrl(); if(multiReddit.isFavorite()) { ((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); @@ -192,7 +192,7 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter< ((MultiRedditViewHolder) holder).multiRedditNameTextView.setText(name); } else if (holder instanceof FavoriteMultiRedditViewHolder) { MultiReddit multiReddit = mFavoriteMultiReddits.get(holder.getAdapterPosition() - 1); - String name = multiReddit.getName(); + String name = multiReddit.getDisplayName(); String iconUrl = multiReddit.getIconUrl(); if(multiReddit.isFavorite()) { ((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SelectedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SelectedSubredditsRecyclerViewAdapter.java new file mode 100644 index 00000000..ce7c4812 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SelectedSubredditsRecyclerViewAdapter.java @@ -0,0 +1,79 @@ +package ml.docilealligator.infinityforreddit.Adapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.R; + +public class SelectedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + private CustomThemeWrapper customThemeWrapper; + private ArrayList<String> subreddits; + + public SelectedSubredditsRecyclerViewAdapter(CustomThemeWrapper customThemeWrapper, ArrayList<String> subreddits) { + this.customThemeWrapper = customThemeWrapper; + if (subreddits == null) { + this.subreddits = new ArrayList<>(); + } else { + this.subreddits = subreddits; + } + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new SubredditViewHolder(LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_selected_subreddit, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (holder instanceof SubredditViewHolder) { + ((SubredditViewHolder) holder).subredditNameTextView.setText(subreddits.get(holder.getAdapterPosition())); + ((SubredditViewHolder) holder).deleteButton.setOnClickListener(view -> { + subreddits.remove(holder.getAdapterPosition()); + notifyItemRemoved(holder.getAdapterPosition()); + }); + } + } + + @Override + public int getItemCount() { + return subreddits.size(); + } + + public void addSubreddits(ArrayList<String> newSubreddits) { + int oldSize = subreddits.size(); + subreddits.addAll(newSubreddits); + notifyItemRangeInserted(oldSize, newSubreddits.size()); + } + + public ArrayList<String> getSubreddits() { + return subreddits; + } + + class SubredditViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.subreddit_name_item_selected_subreddit) + TextView subredditNameTextView; + @BindView(R.id.delete_image_view_item_selected_subreddit) + ImageView deleteButton; + + public SubredditViewHolder(@NonNull View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + + subredditNameTextView.setTextColor(customThemeWrapper.getPrimaryIconColor()); + deleteButton.setColorFilter(customThemeWrapper.getPrimaryIconColor(), android.graphics.PorterDuff.Mode.SRC_IN); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java index 63116bdb..25b57611 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java @@ -7,7 +7,6 @@ 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; @@ -24,134 +23,61 @@ import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; 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; private int primaryTextColor; private int colorAccent; - public SubredditMultiselectionRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper, - ArrayList<SubredditWithSelection> selectedSubscribedSubreddits, - ArrayList<SubredditWithSelection> otherSubreddits) { - this.context = context; + public SubredditMultiselectionRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper) { 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); - } - primaryTextColor = customThemeWrapper.getPrimaryTextColor(); colorAccent = customThemeWrapper.getColorAccent(); } - @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)); - } + return new SubscribedSubredditViewHolder(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()) + ((SubscribedSubredditViewHolder) holder).nameTextView.setText(subscribedSubreddits.get(position).getName()); + glide.load(subscribedSubreddits.get(position).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()) { + if (subscribedSubreddits.get(position).isSelected()) { ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(true); } else { ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(false); } ((SubscribedSubredditViewHolder) holder).checkBox.setOnClickListener(view -> { - if (subscribedSubreddits.get(position - offset).isSelected()) { + if (subscribedSubreddits.get(position).isSelected()) { ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(false); - subscribedSubreddits.get(position - offset).setSelected(false); - selectedSubscribedSubreddits.remove(subscribedSubreddits.get(position - offset)); + subscribedSubreddits.get(position).setSelected(false); } else { ((SubscribedSubredditViewHolder) holder).checkBox.setChecked(true); - subscribedSubreddits.get(position - offset).setSelected(true); - insertIntoSelectedSubredditsArrayListAscend(selectedSubscribedSubreddits, - subscribedSubreddits.get(position - offset)); + subscribedSubreddits.get(position).setSelected(true); } }); ((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(); + return subscribedSubreddits == null ? 0 : subscribedSubreddits.size(); } @Override @@ -159,95 +85,22 @@ public class SubredditMultiselectionRecyclerViewAdapter extends RecyclerView.Ada 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); + this.subscribedSubreddits = SubredditWithSelection.convertSubscribedSubreddits(subscribedSubreddits); + notifyDataSetChanged(); } - 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; + public ArrayList<String> getAllSelectedSubreddits() { + ArrayList<String> selectedSubreddits = new ArrayList<>(); + for (SubredditWithSelection s : subscribedSubreddits) { + if (s.isSelected()) { + selectedSubreddits.add(s.getName()); } - 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); } + return selectedSubreddits; } class SubscribedSubredditViewHolder extends RecyclerView.ViewHolder { @@ -267,22 +120,4 @@ public class SubredditMultiselectionRecyclerViewAdapter extends RecyclerView.Ada checkBox.setButtonTintList(ColorStateList.valueOf(colorAccent)); } } - - 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); - nameTextView.setTextColor(primaryTextColor); - checkBox.setButtonTintList(ColorStateList.valueOf(colorAccent)); - } - } }
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 3a8fd8bb..d8dcc56e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -10,6 +10,7 @@ import ml.docilealligator.infinityforreddit.Activity.CreateMultiRedditActivity; import ml.docilealligator.infinityforreddit.Activity.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity; +import ml.docilealligator.infinityforreddit.Activity.EditMultiRedditActivity; import ml.docilealligator.infinityforreddit.Activity.EditPostActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; @@ -24,6 +25,7 @@ import ml.docilealligator.infinityforreddit.Activity.RulesActivity; import ml.docilealligator.infinityforreddit.Activity.SearchActivity; import ml.docilealligator.infinityforreddit.Activity.SearchResultActivity; import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActivity; +import ml.docilealligator.infinityforreddit.Activity.SelectedSubredditsActivity; import ml.docilealligator.infinityforreddit.Activity.SettingsActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditMultiselectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity; @@ -155,4 +157,8 @@ public interface AppComponent { void inject(AdvancedPreferenceFragment advancedPreferenceFragment); void inject(ThemePreviewActivity themePreviewActivity); + + void inject(EditMultiRedditActivity editMultiRedditActivity); + + void inject(SelectedSubredditsActivity selectedSubredditsActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java new file mode 100644 index 00000000..53ffd5f4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java @@ -0,0 +1,43 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class EditMultiReddit { + public interface EditMultiRedditListener { + void success(); + void failed(); + } + + public static void editMultiReddit(Retrofit oauthRetrofit, String accessToken, String multipath, String model, + EditMultiRedditListener editMultiRedditListener) { + Map<String, String> params = new HashMap<>(); + params.put(RedditUtils.MULTIPATH_KEY, multipath); + params.put(RedditUtils.MODEL_KEY, model); + oauthRetrofit.create(RedditAPI.class).updateMultiReddit(RedditUtils.getOAuthHeader(accessToken), + params).enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + editMultiRedditListener.success(); + } else { + editMultiRedditListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + editMultiRedditListener.failed(); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java new file mode 100644 index 00000000..4c91585d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java @@ -0,0 +1,101 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchMultiRedditInfo { + + public interface FetchMultiRedditInfoListener { + void success(MultiReddit multiReddit); + void failed(); + } + + public static void fetchMultiRedditInfo(Retrofit retrofit, String accessToken, String multipath, + FetchMultiRedditInfoListener fetchMultiRedditInfoListener) { + retrofit.create(RedditAPI.class).getMultiRedditInfo(RedditUtils.getOAuthHeader(accessToken), multipath).enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + new ParseMultiRedditInfoAsyncTask(response.body(), fetchMultiRedditInfoListener).execute(); + } else { + fetchMultiRedditInfoListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + fetchMultiRedditInfoListener.failed(); + } + }); + } + + private static class ParseMultiRedditInfoAsyncTask extends AsyncTask<Void, Void, Void> { + + private String response; + private FetchMultiRedditInfoListener fetchMultiRedditInfoListener; + private MultiReddit multiReddit; + private boolean parseFailed = false; + + public ParseMultiRedditInfoAsyncTask(String response, FetchMultiRedditInfoListener fetchMultiRedditInfoListener) { + this.response = response; + this.fetchMultiRedditInfoListener = fetchMultiRedditInfoListener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject object = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY); + String path = object.getString(JSONUtils.PATH_KEY); + String displayName = object.getString(JSONUtils.DISPLAY_NAME_KEY); + String name = object.getString(JSONUtils.NAME_KEY); + String description = object.getString(JSONUtils.DESCRIPTION_MD_KEY); + String copiedFrom = object.getString(JSONUtils.COPIED_FROM_KEY); + String iconUrl = object.getString(JSONUtils.ICON_URL_KEY); + String visibility = object.getString(JSONUtils.VISIBILITY_KEY); + String owner = object.getString(JSONUtils.OWNER_KEY); + int nSubscribers = object.getInt(JSONUtils.NUM_SUBSCRIBERS_KEY); + long createdUTC = object.getLong(JSONUtils.CREATED_UTC_KEY); + boolean over18 = object.getBoolean(JSONUtils.OVER_18_KEY); + boolean isSubscriber = object.getBoolean(JSONUtils.IS_SUBSCRIBER_KEY); + boolean isFavorite = object.getBoolean(JSONUtils.IS_FAVORITED_KEY); + ArrayList<String> subreddits = new ArrayList<>(); + JSONArray subredditsArray = object.getJSONArray(JSONUtils.SUBREDDITS_KEY); + for (int i = 0; i < subredditsArray.length(); i++) { + subreddits.add(subredditsArray.getJSONObject(i).getString(JSONUtils.NAME_KEY)); + } + + multiReddit = new MultiReddit(path, displayName, name, description, copiedFrom, iconUrl, + visibility, owner, nSubscribers, createdUTC, over18, isSubscriber, isFavorite, + subreddits); + } catch (JSONException e) { + parseFailed = true; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (parseFailed) { + fetchMultiRedditInfoListener.failed(); + } else { + fetchMultiRedditInfoListener.success(multiReddit); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMyMultiReddits.java index 16f36098..85d1aa6f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMyMultiReddits.java @@ -11,13 +11,13 @@ import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; -public class FetchMultiReddit { - public interface FetchMultiRedditListener { +public class FetchMyMultiReddits { + public interface FetchMyMultiRedditsListener { void success(ArrayList<MultiReddit> multiReddits); void failed(); } - public static void fetchMyMultiReddits(Retrofit oauthRetrofit, String accessToken, FetchMultiRedditListener fetchMultiRedditListener) { + public static void fetchMyMultiReddits(Retrofit oauthRetrofit, String accessToken, FetchMyMultiRedditsListener fetchMyMultiRedditsListener) { oauthRetrofit.create(RedditAPI.class) .getMyMultiReddits(RedditUtils.getOAuthHeader(accessToken)).enqueue(new Callback<String>() { @Override @@ -26,22 +26,22 @@ public class FetchMultiReddit { ParseMultiReddit.parseMultiRedditsList(response.body(), new ParseMultiReddit.ParseMultiRedditsListListener() { @Override public void success(ArrayList<MultiReddit> multiReddits) { - fetchMultiRedditListener.success(multiReddits); + fetchMyMultiRedditsListener.success(multiReddits); } @Override public void failed() { - fetchMultiRedditListener.failed(); + fetchMyMultiRedditsListener.failed(); } }); } else { - fetchMultiRedditListener.failed(); + fetchMyMultiRedditsListener.failed(); } } @Override public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { - fetchMultiRedditListener.failed(); + fetchMyMultiRedditsListener.failed(); } }); } 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 74e8ddd1..df921d35 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java @@ -4,8 +4,6 @@ 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; @@ -15,7 +13,7 @@ public class MultiRedditJSONModel { public MultiRedditJSONModel() {} public MultiRedditJSONModel(String display_name, String description_md, boolean isPrivate, - ArrayList<SubredditWithSelection> subreddits) { + ArrayList<String> subreddits) { this.display_name = display_name; this.description_md = description_md; if (isPrivate) { @@ -27,7 +25,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).getName()); + SubredditInMultiReddit subredditInMultiReddit = new SubredditInMultiReddit(subreddits.get(i)); this.subreddits[i] = subredditInMultiReddit; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java index 6d0d3740..635a9646 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java @@ -32,9 +32,9 @@ public class ParseMultiReddit { } private static MultiReddit parseMultiReddit(JSONObject singleMultiRedditJSON) throws JSONException { - String displayName = singleMultiRedditJSON.getString(JSONUtils.DISPLAY_NAME); + String displayName = singleMultiRedditJSON.getString(JSONUtils.DISPLAY_NAME_KEY); String name = singleMultiRedditJSON.getString(JSONUtils.NAME_KEY); - String description = singleMultiRedditJSON.getString(JSONUtils.DESCRIPTION_HTML_KEY); + String description = singleMultiRedditJSON.getString(JSONUtils.DESCRIPTION_MD_KEY); int nSubscribers = singleMultiRedditJSON.getInt(JSONUtils.NUM_SUBSCRIBERS_KEY); String copiedFrom = singleMultiRedditJSON.getString(JSONUtils.COPIED_FROM_KEY); String iconUrl = singleMultiRedditJSON.getString(JSONUtils.ICON_URL_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 78a5efa8..bb64d7df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -22,7 +22,7 @@ class ParseSubredditData { private static SubredditData parseSubredditData(JSONObject subredditDataJsonObject) throws JSONException { String id = subredditDataJsonObject.getString(JSONUtils.NAME_KEY); - String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME); + String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME_KEY); String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); String sidebarDescription = subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 4287b8b7..97827fc2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -72,7 +72,7 @@ class ParseSubscribedThing { JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); for (int i = 0; i < children.length(); i++) { JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - String name = data.getString(JSONUtils.DISPLAY_NAME); + String name = data.getString(JSONUtils.DISPLAY_NAME_KEY); String bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY); if (bannerImageUrl.equals("") || bannerImageUrl.equals("null")) { bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY); @@ -95,7 +95,7 @@ class ParseSubscribedThing { //It's a user newSubscribedUserData.add(new SubscribedUserData(name.substring(2), iconUrl, accountName, isFavorite)); } else { - String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME); + String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_KEY); String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); String sidebarDescription = data.getString(JSONUtils.DESCRIPTION_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index e25fba92..371e31e9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -12,6 +12,7 @@ import retrofit2.http.GET; import retrofit2.http.HeaderMap; import retrofit2.http.Multipart; import retrofit2.http.POST; +import retrofit2.http.PUT; import retrofit2.http.Part; import retrofit2.http.PartMap; import retrofit2.http.Path; @@ -294,6 +295,13 @@ public interface RedditAPI { @POST("/api/multi/multipath") Call<String> createMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); + @FormUrlEncoded + @PUT("/api/multi/multipath") + Call<String> updateMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); + @DELETE("/api/multi/multipath") Call<String> deleteMultiReddit(@HeaderMap Map<String, String> headers, @Query("multipath") String multipath); + + @GET("/api/multi/multipath/") + Call<String> getMultiRedditInfo(@HeaderMap Map<String, String> headers, @Query("multipath") String multipath); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java index 2d7d7919..b7aae929 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java @@ -58,7 +58,7 @@ public class JSONUtils { public static final String COMMUNITY_ICON_KEY = "community_icon"; public static final String LINK_KARMA_KEY = "link_karma"; public static final String COMMENT_KARMA_KEY = "comment_karma"; - public static final String DISPLAY_NAME = "display_name"; + public static final String DISPLAY_NAME_KEY = "display_name"; public static final String SUBREDDIT_TYPE_KEY = "subreddit_type"; public static final String SUBREDDIT_TYPE_VALUE_USER = "user"; public static final String SUBSCRIBERS_KEY = "subscribers"; @@ -80,6 +80,7 @@ public class JSONUtils { public static final String SHORT_NAME_KEY = "short_name"; public static final String DESCRIPTION_KEY = "description"; public static final String DESCRIPTION_HTML_KEY = "description_html"; + public static final String DESCRIPTION_MD_KEY = "description_md"; public static final String ARCHIVED_KEY = "archived"; public static final String LOCKEC_KEY = "locked"; public static final String SAVED_KEY = "saved"; |