Skip to content

Commit

Permalink
Create 'Add to Multireddit' action for subreddits
Browse files Browse the repository at this point in the history
  • Loading branch information
mrboisvert committed Nov 28, 2024
1 parent 0e8f4a8 commit 9a4d736
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.quantumbadger.redreader.common.ScreenreaderPronunciation;
import org.quantumbadger.redreader.common.SharedPrefsWrapper;
import org.quantumbadger.redreader.common.UriString;
import org.quantumbadger.redreader.fragments.AddToMultiredditDialog;
import org.quantumbadger.redreader.fragments.MainMenuFragment;
import org.quantumbadger.redreader.receivers.announcements.Announcement;
import org.quantumbadger.redreader.receivers.announcements.AnnouncementDownloader;
Expand Down Expand Up @@ -117,7 +118,8 @@ public enum SubredditAction {
UNPIN(R.string.unpin_subreddit),
SUBSCRIBE(R.string.options_subscribe),
UNSUBSCRIBE(R.string.options_unsubscribe),
EXTERNAL(R.string.action_external);
EXTERNAL(R.string.action_external),
ADD_TO_MULTIREDDIT(R.string.add_subreddit_to_multireddit);

public final int descriptionResId;

Expand Down Expand Up @@ -820,6 +822,13 @@ public static void showActionMenu(
}
}
}

if(itemPref.contains(SubredditAction.ADD_TO_MULTIREDDIT)) {
menu.add(new SubredditMenuItem(
activity,
R.string.add_subreddit_to_multireddit,
SubredditAction.ADD_TO_MULTIREDDIT));
}
}

final String[] menuText = new String[menu.size()];
Expand Down Expand Up @@ -941,6 +950,11 @@ private static void onSubredditActionMenuItemSelected(
Toast.LENGTH_SHORT).show();
}
break;

case ADD_TO_MULTIREDDIT:

AddToMultiredditDialog.show(activity, subredditCanonicalId);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.quantumbadger.redreader.fragments;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.common.RRError;
import org.quantumbadger.redreader.common.TimestampBound;
import org.quantumbadger.redreader.reddit.APIResponseHandler;
import org.quantumbadger.redreader.reddit.RedditAPI;
import org.quantumbadger.redreader.reddit.api.RedditMultiredditSubscriptionManager;
import org.quantumbadger.redreader.reddit.things.SubredditCanonicalId;
import org.quantumbadger.redreader.views.liststatus.ErrorView;

import java.util.ArrayList;
import java.util.Collections;

public class AddToMultiredditDialog {

private final static String TAG = "AddToMultiredditDialog";

public static void show(
final AppCompatActivity activity,
final SubredditCanonicalId subredditCanonicalId) {

final MaterialAlertDialogBuilder alertBuilder
= new MaterialAlertDialogBuilder(activity);

final View root = activity.getLayoutInflater().inflate(
R.layout.add_to_multireddit,
null);

final RedditMultiredditSubscriptionManager multiredditManager
= RedditMultiredditSubscriptionManager.getSingleton(
activity,
RedditAccountManager.getInstance(activity).getDefaultAccount());

final ArrayList<String> multireddits = multiredditManager.getSubscriptionList();
Collections.sort(multireddits);

final ArrayAdapter<String> autocompleteAdapter = new ArrayAdapter<>(
activity,
android.R.layout.simple_dropdown_item_1line,
multireddits);

final AutoCompleteTextView editText
= root.findViewById(R.id.selected_multireddit);
editText.setAdapter(autocompleteAdapter);

alertBuilder.setView(root);

alertBuilder.setNegativeButton(R.string.dialog_cancel, null);

alertBuilder.setPositiveButton(
R.string.dialog_go,
(dialog, which) -> addToMultireddit(activity, editText, subredditCanonicalId));

final AlertDialog alertDialog = alertBuilder.create();

editText.setOnEditorActionListener((v, actionId, event) -> {
if(actionId == EditorInfo.IME_ACTION_GO
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
addToMultireddit(activity, editText, subredditCanonicalId);
alertDialog.dismiss();
}
return false;
});

alertDialog.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
alertDialog.show();
}

private static void addToMultireddit(
final AppCompatActivity activity,
final AutoCompleteTextView editText,
final SubredditCanonicalId subredditCanonicalId) {

final String multiredditName = editText.getText()
.toString()
.trim()
.replace(" ", "");
final String subredditName = subredditCanonicalId.getDisplayNameLowercase();

RedditAPI.addSubredditToMultireddit(
CacheManager.getInstance(activity),
new APIResponseHandler.ActionResponseHandler(activity) {

@Override
protected void onCallbackException(final Throwable t) {
Log.e(
TAG, "Error while adding subreddit to multireddit", t);
throw new RuntimeException(t);
}

@Override
protected void onFailure(@NonNull final RRError error) {
activity.runOnUiThread(() -> {
final MaterialAlertDialogBuilder builder
= new MaterialAlertDialogBuilder(activity);
builder.setView(new ErrorView(activity, error));
builder.create().show();
});
}

@Override
protected void onSuccess() {
activity.runOnUiThread(() -> Toast.makeText(
activity,
String.format("Added %s to %s", subredditName, multiredditName),
Toast.LENGTH_SHORT).show());
RedditMultiredditSubscriptionManager.getSingleton(
activity,
RedditAccountManager
.getInstance(activity).getDefaultAccount())
.triggerUpdate(null, TimestampBound.NONE);
}
},
RedditAccountManager.getInstance(activity).getDefaultAccount(),
multiredditName,
subredditName,
activity
);

Toast.makeText(
activity,
String.format("Adding %s to %s", subredditName, multiredditName),
Toast.LENGTH_SHORT).show();
}
}
53 changes: 53 additions & 0 deletions src/main/res/layout/add_to_multireddit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ This file is part of RedReader.
~
~ RedReader is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ RedReader is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with RedReader. If not, see <http://www.gnu.org/licenses/>.
-->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/add_to_multireddit_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/add_to_multireddit_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:text="@string/add_subreddit_to_multireddit" />

<AutoCompleteTextView
android:id="@+id/selected_multireddit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:inputType="textNoSuggestions"
android:imeOptions="actionGo"
android:completionThreshold="1"
tools:ignore="LabelFor">

<requestFocus/>
</AutoCompleteTextView>

</LinearLayout>
2 changes: 2 additions & 0 deletions src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@
<item>@string/pin_subreddit</item>
<item>@string/options_subscribe</item>
<item>@string/block_subreddit</item>
<item>@string/add_subreddit_to_multireddit</item>
</string-array>

<!-- Constants. Do not change. -->
Expand All @@ -505,6 +506,7 @@
<item>pin</item>
<item>subscribe</item>
<item>block</item>
<item>add_to_multireddit</item>
</string-array>

<string-array name="pref_menus_subreddit_context_items_default">
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1908,4 +1908,6 @@
<string name="error_401_message">Reddit says that you are not logged in or have provided invalid credentials.</string>
<string name="error_400_title">Bad Request</string>
<string name="error_400_message">Reddit says that you have submitted something invalid.</string>

<string name="add_subreddit_to_multireddit">Add to New or Existing Multireddit</string>
</resources>

0 comments on commit 9a4d736

Please sign in to comment.