From 679820b96ebae991b667c0bfd845eae6d6e27e48 Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Mon, 21 Feb 2022 22:52:22 +0800 Subject: [PATCH] Submitting poll posts is available. --- app/src/main/AndroidManifest.xml | 9 +- .../infinityforreddit/AppComponent.java | 3 + .../infinityforreddit/PollPayload.java | 48 ++ .../RedditGalleryPayload.java | 1 + .../activities/MainActivity.java | 4 + .../activities/PostPollActivity.java | 689 ++++++++++++++++++ .../activities/SearchResultActivity.java | 4 + .../ViewSubredditDetailActivity.java | 7 +- .../activities/ViewUserDetailActivity.java | 4 + .../infinityforreddit/apis/RedditAPI.java | 3 + .../PostTypeBottomSheetFragment.java | 8 + .../events/SubmitPollPostEvent.java | 13 + .../services/SubmitPostService.java | 45 +- .../main/res/drawable-night/ic_poll_24dp.xml | 9 + app/src/main/res/drawable/ic_poll_24dp.xml | 9 + .../main/res/layout/activity_post_poll.xml | 326 +++++++++ .../fragment_post_type_bottom_sheet.xml | 19 + app/src/main/res/menu/post_poll_activity.xml | 10 + app/src/main/res/values/strings.xml | 11 + 19 files changed, 1218 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PollPayload.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitPollPostEvent.java create mode 100644 app/src/main/res/drawable-night/ic_poll_24dp.xml create mode 100644 app/src/main/res/drawable/ic_poll_24dp.xml create mode 100644 app/src/main/res/layout/activity_post_poll.xml create mode 100644 app/src/main/res/menu/post_poll_activity.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3632c4a2..71359562 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,7 +25,7 @@ + = Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + mGlide = Glide.with(this); + + mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); + + resources = getResources(); + + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + + Resources resources = getResources(); + + if (savedInstanceState != null) { + subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); + iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); + subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); + subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); + loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); + isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); + flair = savedInstanceState.getParcelable(FLAIR_STATE); + isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); + isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); + + if (subredditName != null) { + subredditNameTextView.setTextColor(primaryTextColor); + subredditNameTextView.setText(subredditName); + flairTextView.setVisibility(View.VISIBLE); + if (!loadSubredditIconSuccessful) { + loadSubredditIcon(); + } + } + displaySubredditIcon(); + + if (isPosting) { + mPostingSnackbar.show(); + } + + if (flair != null) { + flairTextView.setText(flair.getText()); + flairTextView.setBackgroundColor(flairBackgroundColor); + flairTextView.setBorderColor(flairBackgroundColor); + flairTextView.setTextColor(flairTextColor); + } + if (isSpoiler) { + spoilerTextView.setBackgroundColor(spoilerBackgroundColor); + spoilerTextView.setBorderColor(spoilerBackgroundColor); + spoilerTextView.setTextColor(spoilerTextColor); + } + if (isNSFW) { + nsfwTextView.setBackgroundColor(nsfwBackgroundColor); + nsfwTextView.setBorderColor(nsfwBackgroundColor); + nsfwTextView.setTextColor(nsfwTextColor); + } + } else { + isPosting = false; + + if (getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { + loadSubredditIconSuccessful = false; + subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); + subredditSelected = true; + subredditNameTextView.setTextColor(primaryTextColor); + subredditNameTextView.setText(subredditName); + flairTextView.setVisibility(View.VISIBLE); + loadSubredditIcon(); + } else { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + } + } + + iconGifImageView.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + + subredditNameTextView.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + + rulesButton.setOnClickListener(view -> { + if (subredditName == null) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + } else { + Intent intent = new Intent(this, RulesActivity.class); + if (subredditIsUser) { + intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); + } else { + intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, subredditName); + } + startActivity(intent); + } + }); + + flairTextView.setOnClickListener(view -> { + if (flair == null) { + flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); + flairSelectionBottomSheetFragment.setArguments(bundle); + flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag()); + } else { + flairTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + flairTextView.setTextColor(primaryTextColor); + flairTextView.setText(getString(R.string.flair)); + flair = null; + } + }); + + spoilerTextView.setOnClickListener(view -> { + if (!isSpoiler) { + spoilerTextView.setBackgroundColor(spoilerBackgroundColor); + spoilerTextView.setBorderColor(spoilerBackgroundColor); + spoilerTextView.setTextColor(spoilerTextColor); + isSpoiler = true; + } else { + spoilerTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + spoilerTextView.setTextColor(primaryTextColor); + isSpoiler = false; + } + }); + + nsfwTextView.setOnClickListener(view -> { + if (!isNSFW) { + nsfwTextView.setBackgroundColor(nsfwBackgroundColor); + nsfwTextView.setBorderColor(nsfwBackgroundColor); + nsfwTextView.setTextColor(nsfwTextColor); + isNSFW = true; + } else { + nsfwTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + nsfwTextView.setTextColor(primaryTextColor); + isNSFW = false; + } + }); + + receivePostReplyNotificationsLinearLayout.setOnClickListener(view -> { + receivePostReplyNotificationsSwitchMaterial.performClick(); + }); + + votingLengthTextView.setText(getString(R.string.voting_length, (int) votingLengthSlider.getValue())); + votingLengthSlider.addOnChangeListener((slider, value, fromUser) -> votingLengthTextView.setText(getString(R.string.voting_length, (int) value))); + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, null, toolbar); + int secondaryTextColor = mCustomThemeWrapper.getSecondaryTextColor(); + subredditNameTextView.setTextColor(secondaryTextColor); + rulesButton.setTextColor(mCustomThemeWrapper.getButtonTextColor()); + rulesButton.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme()); + primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor(); + receivePostReplyNotificationsTextView.setTextColor(primaryTextColor); + int dividerColor = mCustomThemeWrapper.getDividerColor(); + divider1.setDividerColor(dividerColor); + divider2.setDividerColor(dividerColor); + divider3.setDividerColor(dividerColor); + flairBackgroundColor = mCustomThemeWrapper.getFlairBackgroundColor(); + flairTextColor = mCustomThemeWrapper.getFlairTextColor(); + spoilerBackgroundColor = mCustomThemeWrapper.getSpoilerBackgroundColor(); + spoilerTextColor = mCustomThemeWrapper.getSpoilerTextColor(); + nsfwBackgroundColor = mCustomThemeWrapper.getNsfwBackgroundColor(); + nsfwTextColor = mCustomThemeWrapper.getNsfwTextColor(); + flairTextView.setTextColor(primaryTextColor); + spoilerTextView.setTextColor(primaryTextColor); + nsfwTextView.setTextColor(primaryTextColor); + titleEditText.setTextColor(primaryTextColor); + titleEditText.setHintTextColor(secondaryTextColor); + option1TextInputLayout.setBoxStrokeColor(primaryTextColor); + option1TextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + option1TextInputEditText.setTextColor(primaryTextColor); + + option2TextInputLayout.setBoxStrokeColor(primaryTextColor); + option2TextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + option2TextInputEditText.setTextColor(primaryTextColor); + + option3TextInputLayout.setBoxStrokeColor(primaryTextColor); + option3TextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + option3TextInputEditText.setTextColor(primaryTextColor); + + option4TextInputLayout.setBoxStrokeColor(primaryTextColor); + option4TextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + option4TextInputEditText.setTextColor(primaryTextColor); + + option5TextInputLayout.setBoxStrokeColor(primaryTextColor); + option5TextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + option5TextInputEditText.setTextColor(primaryTextColor); + + option6TextInputLayout.setBoxStrokeColor(primaryTextColor); + option6TextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + option6TextInputEditText.setTextColor(primaryTextColor); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + Drawable cursorDrawable = Utils.getTintedDrawable(this, R.drawable.edit_text_cursor, primaryTextColor); + option1TextInputEditText.setTextCursorDrawable(cursorDrawable); + option2TextInputEditText.setTextCursorDrawable(cursorDrawable); + option3TextInputEditText.setTextCursorDrawable(cursorDrawable); + option4TextInputEditText.setTextCursorDrawable(cursorDrawable); + option5TextInputEditText.setTextCursorDrawable(cursorDrawable); + option6TextInputEditText.setTextCursorDrawable(cursorDrawable); + } else { + setCursorDrawableColor(option1TextInputEditText, primaryTextColor); + setCursorDrawableColor(option2TextInputEditText, primaryTextColor); + setCursorDrawableColor(option3TextInputEditText, primaryTextColor); + setCursorDrawableColor(option4TextInputEditText, primaryTextColor); + setCursorDrawableColor(option5TextInputEditText, primaryTextColor); + setCursorDrawableColor(option6TextInputEditText, primaryTextColor); + } + + if (typeface != null) { + subredditNameTextView.setTypeface(typeface); + rulesButton.setTypeface(typeface); + receivePostReplyNotificationsTextView.setTypeface(typeface); + flairTextView.setTypeface(typeface); + spoilerTextView.setTypeface(typeface); + nsfwTextView.setTypeface(typeface); + titleEditText.setTypeface(typeface); + option1TextInputEditText.setTypeface(typeface); + option2TextInputEditText.setTypeface(typeface); + option3TextInputEditText.setTypeface(typeface); + option4TextInputEditText.setTypeface(typeface); + option5TextInputEditText.setTypeface(typeface); + option6TextInputEditText.setTypeface(typeface); + } + } + + public void setCursorDrawableColor(EditText editText, int color) { + try { + Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); + fCursorDrawableRes.setAccessible(true); + int mCursorDrawableRes = fCursorDrawableRes.getInt(editText); + Field fEditor = TextView.class.getDeclaredField("mEditor"); + fEditor.setAccessible(true); + Object editor = fEditor.get(editText); + Class clazz = editor.getClass(); + Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); + fCursorDrawable.setAccessible(true); + Drawable[] drawables = new Drawable[2]; + drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes); + drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes); + drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); + drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); + fCursorDrawable.set(editor, drawables); + } catch (Throwable ignored) { } + } + + private void displaySubredditIcon() { + if (iconUrl != null && !iconUrl.equals("")) { + mGlide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(iconGifImageView); + } else { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + } + } + + private void loadSubredditIcon() { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, mAccessToken, mOauthRetrofit, mRetrofit, iconImageUrl -> { + iconUrl = iconImageUrl; + displaySubredditIcon(); + loadSubredditIconSuccessful = true; + }); + } + + private void promptAlertDialog(int titleResId, int messageResId) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(titleResId) + .setMessage(messageResId) + .setPositiveButton(R.string.discard_dialog_button, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.post_poll_activity, menu); + applyMenuItemTheme(menu); + mMemu = menu; + if (isPosting) { + mMemu.findItem(R.id.action_send_post_poll_activity).setEnabled(false); + mMemu.findItem(R.id.action_send_post_poll_activity).getIcon().setAlpha(130); + } + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + if (isPosting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); + return true; + } else { + if (!titleEditText.getText().toString().equals("") + || !option1TextInputEditText.getText().toString().equals("") + || !option2TextInputEditText.getText().toString().equals("") + || !option3TextInputEditText.getText().toString().equals("") + || !option4TextInputEditText.getText().toString().equals("") + || !option5TextInputEditText.getText().toString().equals("") + || !option6TextInputEditText.getText().toString().equals("")) { + promptAlertDialog(R.string.discard, R.string.discard_detail); + return true; + } + } + finish(); + return true; + } else if (itemId == R.id.action_send_post_poll_activity) { + if (!subredditSelected) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + return true; + } + + if (titleEditText.getText() == null) { + Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); + return true; + } + + String subredditName; + if (subredditIsUser) { + subredditName = "u_" + subredditNameTextView.getText().toString(); + } else { + subredditName = subredditNameTextView.getText().toString(); + } + + ArrayList optionList = new ArrayList<>(); + if (!option1TextInputEditText.getText().toString().equals("")) { + optionList.add(option1TextInputEditText.getText().toString()); + } + if (!option2TextInputEditText.getText().toString().equals("")) { + optionList.add(option2TextInputEditText.getText().toString()); + } + if (!option3TextInputEditText.getText().toString().equals("")) { + optionList.add(option3TextInputEditText.getText().toString()); + } + if (!option4TextInputEditText.getText().toString().equals("")) { + optionList.add(option4TextInputEditText.getText().toString()); + } + if (!option5TextInputEditText.getText().toString().equals("")) { + optionList.add(option5TextInputEditText.getText().toString()); + } + if (!option6TextInputEditText.getText().toString().equals("")) { + optionList.add(option6TextInputEditText.getText().toString()); + } + + if (optionList.size() < 2) { + Snackbar.make(coordinatorLayout, R.string.two_options_required, Snackbar.LENGTH_SHORT).show(); + return true; + } + + isPosting = true; + + item.setEnabled(false); + item.getIcon().setAlpha(130); + + mPostingSnackbar.show(); + + Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_POLL); + PollPayload payload = new PollPayload(subredditName, titleEditText.getText().toString(), + optionList.toArray(new String[0]), (int) votingLengthSlider.getValue(), isNSFW, isSpoiler, flair, receivePostReplyNotificationsSwitchMaterial.isChecked(), + subredditIsUser ? "profile" : "subreddit"); + intent.putExtra(SubmitPostService.EXTRA_POLL_PAYLOAD, new Gson().toJson(payload)); + + ContextCompat.startForegroundService(this, intent); + + return true; + } + + return false; + } + + @Override + public void onBackPressed() { + if (isPosting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); + } else { + if (!titleEditText.getText().toString().equals("") + || !option1TextInputEditText.getText().toString().equals("") + || !option2TextInputEditText.getText().toString().equals("") + || !option3TextInputEditText.getText().toString().equals("") + || !option4TextInputEditText.getText().toString().equals("") + || !option5TextInputEditText.getText().toString().equals("") + || !option6TextInputEditText.getText().toString().equals("")) { + promptAlertDialog(R.string.discard, R.string.discard_detail); + } else { + finish(); + } + } + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(SUBREDDIT_NAME_STATE, subredditName); + outState.putString(SUBREDDIT_ICON_STATE, iconUrl); + outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected); + outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); + outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); + outState.putBoolean(IS_POSTING_STATE, isPosting); + outState.putParcelable(FLAIR_STATE, flair); + outState.putBoolean(IS_SPOILER_STATE, isSpoiler); + outState.putBoolean(IS_NSFW_STATE, isNSFW); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { + if (resultCode == RESULT_OK) { + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); + subredditSelected = true; + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER); + + subredditNameTextView.setTextColor(primaryTextColor); + subredditNameTextView.setText(subredditName); + displaySubredditIcon(); + + flairTextView.setVisibility(View.VISIBLE); + flairTextView.setBackgroundColor(resources.getColor(android.R.color.transparent)); + flairTextView.setTextColor(primaryTextColor); + flairTextView.setText(getString(R.string.flair)); + flair = null; + } + } + } + + @Override + public void flairSelected(Flair flair) { + this.flair = flair; + flairTextView.setText(flair.getText()); + flairTextView.setBackgroundColor(flairBackgroundColor); + flairTextView.setBorderColor(flairBackgroundColor); + flairTextView.setTextColor(flairTextColor); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Subscribe + public void onSubmitPollPostEvent(SubmitPollPostEvent submitPollPostEvent) { + isPosting = false; + mPostingSnackbar.dismiss(); + if (submitPollPostEvent.postSuccess) { + Intent intent = new Intent(this, LinkResolverActivity.class); + intent.setData(Uri.parse(submitPollPostEvent.postUrl)); + startActivity(intent); + finish(); + } else { + mMemu.findItem(R.id.action_send_post_poll_activity).setEnabled(true); + mMemu.findItem(R.id.action_send_post_poll_activity).getIcon().setAlpha(255); + if (submitPollPostEvent.errorMessage == null || submitPollPostEvent.errorMessage.equals("")) { + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, submitPollPostEvent.errorMessage.substring(0, 1).toUpperCase() + + submitPollPostEvent.errorMessage.substring(1), Snackbar.LENGTH_SHORT).show(); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index 2d928ac5..ab60515e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -741,6 +741,10 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect case PostTypeBottomSheetFragment.TYPE_GALLERY: intent = new Intent(this, PostGalleryActivity.class); startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_POLL: + intent = new Intent(this, PostPollActivity.class); + startActivity(intent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index b2b82fa6..f63c1cd3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -1277,7 +1277,12 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp break; case PostTypeBottomSheetFragment.TYPE_GALLERY: intent = new Intent(this, PostGalleryActivity.class); - intent.putExtra(PostVideoActivity.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(PostGalleryActivity.EXTRA_SUBREDDIT_NAME, subredditName); + startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_POLL: + intent = new Intent(this, PostPollActivity.class); + intent.putExtra(PostPollActivity.EXTRA_SUBREDDIT_NAME, subredditName); startActivity(intent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 731c6aa9..78625af4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -1570,6 +1570,10 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele case PostTypeBottomSheetFragment.TYPE_GALLERY: intent = new Intent(this, PostGalleryActivity.class); startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_POLL: + intent = new Intent(this, PostPollActivity.class); + startActivity(intent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index fa672901..f4120d14 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -294,6 +294,9 @@ public interface RedditAPI { @POST("/api/submit_gallery_post.json?resubmit=true&raw_json=1") Call submitGalleryPost(@HeaderMap Map headers, @Body String body); + @POST("/api/submit_poll_post.json?resubmit=true&raw_json=1&gilding_detail=1") + Call submitPollPost(@HeaderMap Map headers, @Body String body); + @GET("/api/trending_searches_v1.json?withAds=0&raw_json=1&gilding_detail=1") Call getTrendingSearches(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java index fd6fc650..bc410123 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java @@ -31,6 +31,7 @@ public class PostTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomS public static final int TYPE_IMAGE = 2; public static final int TYPE_VIDEO = 3; public static final int TYPE_GALLERY = 4; + public static final int TYPE_POLL = 5; @BindView(R.id.text_type_linear_layout_post_type_bottom_sheet_fragment) TextView textTypeTextView; @BindView(R.id.link_type_linear_layout_post_type_bottom_sheet_fragment) @@ -41,6 +42,8 @@ public class PostTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomS TextView videoTypeTextView; @BindView(R.id.gallery_type_linear_layout_post_type_bottom_sheet_fragment) TextView galleryTypeTextView; + @BindView(R.id.poll_type_linear_layout_post_type_bottom_sheet_fragment) + TextView pollTypeTextView; private BaseActivity activity; public PostTypeBottomSheetFragment() { @@ -83,6 +86,11 @@ public class PostTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomS dismiss(); }); + pollTypeTextView.setOnClickListener(view -> { + ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_POLL); + dismiss(); + }); + if (activity.typeface != null) { Utils.setFontToAllTextViews(rootView, activity.typeface); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitPollPostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitPollPostEvent.java new file mode 100644 index 00000000..63b4c9cf --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitPollPostEvent.java @@ -0,0 +1,13 @@ +package ml.docilealligator.infinityforreddit.events; + +public class SubmitPollPostEvent { + public boolean postSuccess; + public String postUrl; + public String errorMessage; + + public SubmitPollPostEvent(boolean postSuccess, String postUrl, String errorMessage) { + this.postSuccess = postSuccess; + this.postUrl = postUrl; + this.errorMessage = errorMessage; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java index f1ba75f3..0882e491 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java @@ -47,6 +47,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SubmitCrosspostEvent; import ml.docilealligator.infinityforreddit.events.SubmitGalleryPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitImagePostEvent; +import ml.docilealligator.infinityforreddit.events.SubmitPollPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitVideoOrGifPostEvent; import ml.docilealligator.infinityforreddit.post.Post; @@ -63,6 +64,7 @@ public class SubmitPostService extends Service { public static final String EXTRA_TITLE = "ET"; public static final String EXTRA_CONTENT = "EC"; public static final String EXTRA_REDDIT_GALLERY_PAYLOAD = "ERGP"; + public static final String EXTRA_POLL_PAYLOAD = "EPP"; public static final String EXTRA_KIND = "EK"; public static final String EXTRA_FLAIR = "EF"; public static final String EXTRA_IS_SPOILER = "EIS"; @@ -73,7 +75,8 @@ public class SubmitPostService extends Service { public static final int EXTRA_POST_TYPE_IMAGE = 1; public static final int EXTRA_POST_TYPE_VIDEO = 2; public static final int EXTRA_POST_TYPE_GALLERY = 3; - public static final int EXTRA_POST_TYPE_CROSSPOST = 4; + public static final int EXTRA_POST_TYPE_POLL = 4; + public static final int EXTRA_POST_TYPE_CROSSPOST = 5; private static final String EXTRA_MEDIA_URI = "EU"; @Inject @@ -136,8 +139,10 @@ public class SubmitPostService extends Service { Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI)); submitVideoPost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW, receivePostReplyNotifications); - } else { + } else if (postType == EXTRA_POST_TYPE_GALLERY) { submitGalleryPost(accessToken, bundle.getString(EXTRA_REDDIT_GALLERY_PAYLOAD)); + } else { + submitPollPost(accessToken, bundle.getString(EXTRA_POLL_PAYLOAD)); } } } @@ -373,6 +378,42 @@ public class SubmitPostService extends Service { } } + private void submitPollPost(String accessToken, String payload) { + try { + Response response = mOauthRetrofit.create(RedditAPI.class).submitPollPost(APIUtils.getOAuthHeader(accessToken), payload).execute(); + if (response.isSuccessful()) { + JSONObject responseObject = new JSONObject(response.body()).getJSONObject(JSONUtils.JSON_KEY); + if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { + JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY) + .getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1); + if (error.length() != 0) { + String errorMessage; + if (error.length() >= 2) { + errorMessage = error.getString(1); + } else { + errorMessage = error.getString(0); + } + handler.post(() -> EventBus.getDefault().post(new SubmitPollPostEvent(false, null, errorMessage))); + } else { + handler.post(() -> EventBus.getDefault().post(new SubmitPollPostEvent(false, null, null))); + } + } else { + String postUrl = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.URL_KEY); + handler.post(() -> { + EventBus.getDefault().post(new SubmitPollPostEvent(true, postUrl, null)); + }); + } + } else { + handler.post(() -> EventBus.getDefault().post(new SubmitPollPostEvent(false, null, response.message()))); + } + } catch (IOException | JSONException e) { + e.printStackTrace(); + handler.post(() -> EventBus.getDefault().post(new SubmitPollPostEvent(false, null, e.getMessage()))); + } finally { + stopService(); + } + } + private static void copyFileToCache(InputStream fileInputStream, String destinationFilePath) throws IOException { OutputStream out = new FileOutputStream(destinationFilePath); byte[] buf = new byte[2048]; diff --git a/app/src/main/res/drawable-night/ic_poll_24dp.xml b/app/src/main/res/drawable-night/ic_poll_24dp.xml new file mode 100644 index 00000000..fa199daf --- /dev/null +++ b/app/src/main/res/drawable-night/ic_poll_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_poll_24dp.xml b/app/src/main/res/drawable/ic_poll_24dp.xml new file mode 100644 index 00000000..c4033782 --- /dev/null +++ b/app/src/main/res/drawable/ic_poll_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_post_poll.xml b/app/src/main/res/layout/activity_post_poll.xml new file mode 100644 index 00000000..3d12d616 --- /dev/null +++ b/app/src/main/res/layout/activity_post_poll.xml @@ -0,0 +1,326 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml b/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml index 7138d814..a597db67 100644 --- a/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml +++ b/app/src/main/res/layout/fragment_post_type_bottom_sheet.xml @@ -105,6 +105,25 @@ android:focusable="true" android:background="?attr/selectableItemBackground" /> + + \ No newline at end of file diff --git a/app/src/main/res/menu/post_poll_activity.xml b/app/src/main/res/menu/post_poll_activity.xml new file mode 100644 index 00000000..e497c4e5 --- /dev/null +++ b/app/src/main/res/menu/post_poll_activity.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5c384e11..e0a00dca 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -44,6 +44,7 @@ Trending Wiki Edit Profile + Poll Post Open navigation drawer Close navigation drawer @@ -197,10 +198,12 @@ The post needs a good title Hey where is the link? Please select an image first + Voting length: %1$d days Posting Could not post it Error processing image Error processing video + A good poll needs two or more options! Download Started. Check the notification for progress. @@ -229,6 +232,7 @@ Image Video Gallery + Poll Gif Gallery @@ -1271,5 +1275,12 @@ You need to agree to Reddit User Agreement (%1$s) and Infinity for Reddit\'s Privacy Policy (%2$s) before logging in. Agree Don\'t Agree + + Option 1 (Required) + Option 2 (Required) + Option 3 + Option 4 + Option 5 + Option 6