Skip to content

Commit aa5878d

Browse files
committed
'Create Multireddit' action for subreddit combinations
1 parent bd1122c commit aa5878d

File tree

6 files changed

+244
-7
lines changed

6 files changed

+244
-7
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*******************************************************************************
2+
* This file is part of RedReader.
3+
*
4+
* RedReader is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* RedReader is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
package org.quantumbadger.redreader.fragments;
19+
20+
import android.util.Log;
21+
import android.view.KeyEvent;
22+
import android.view.View;
23+
import android.view.WindowManager;
24+
import android.view.inputmethod.EditorInfo;
25+
import android.widget.EditText;
26+
import android.widget.Toast;
27+
28+
import androidx.annotation.NonNull;
29+
import androidx.appcompat.app.AlertDialog;
30+
import androidx.appcompat.app.AppCompatActivity;
31+
32+
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
33+
34+
import org.quantumbadger.redreader.R;
35+
import org.quantumbadger.redreader.account.RedditAccount;
36+
import org.quantumbadger.redreader.account.RedditAccountManager;
37+
import org.quantumbadger.redreader.cache.CacheManager;
38+
import org.quantumbadger.redreader.common.RRError;
39+
import org.quantumbadger.redreader.common.TimestampBound;
40+
import org.quantumbadger.redreader.reddit.APIResponseHandler;
41+
import org.quantumbadger.redreader.reddit.RedditAPI;
42+
import org.quantumbadger.redreader.reddit.api.RedditMultiredditSubscriptionManager;
43+
import org.quantumbadger.redreader.views.liststatus.ErrorView;
44+
45+
import java.util.List;
46+
47+
public class CreateMultiredditDialog {
48+
49+
private final static String TAG = "CreateMultiredditDialog";
50+
51+
public static void show(
52+
final AppCompatActivity activity,
53+
final List<String> subredditNames,
54+
final RedditAccount user) {
55+
56+
final MaterialAlertDialogBuilder alertBuilder
57+
= new MaterialAlertDialogBuilder(activity);
58+
59+
final View root = activity.getLayoutInflater().inflate(
60+
R.layout.create_multireddit,
61+
null);
62+
63+
final EditText editText
64+
= root.findViewById(R.id.selected_multireddit);
65+
66+
alertBuilder.setView(root);
67+
68+
alertBuilder.setNegativeButton(R.string.dialog_cancel, null);
69+
70+
alertBuilder.setPositiveButton(
71+
R.string.dialog_go,
72+
(dialog, which) -> createMultireddit(activity, subredditNames, user, editText));
73+
74+
final AlertDialog alertDialog = alertBuilder.create();
75+
76+
editText.setOnEditorActionListener((v, actionId, event) -> {
77+
if(actionId == EditorInfo.IME_ACTION_GO
78+
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
79+
createMultireddit(activity, subredditNames, user, editText);
80+
alertDialog.dismiss();
81+
}
82+
return false;
83+
});
84+
85+
alertDialog.getWindow()
86+
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
87+
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
88+
alertDialog.show();
89+
}
90+
91+
private static void createMultireddit(
92+
final AppCompatActivity activity,
93+
final List<String> subredditNames,
94+
final RedditAccount user,
95+
final EditText editText) {
96+
97+
final String multiredditName = editText.getText()
98+
.toString()
99+
.trim()
100+
.replace(" ", "");
101+
102+
RedditAPI.createMultireddit(
103+
CacheManager.getInstance(activity),
104+
new APIResponseHandler.ActionResponseHandler(activity) {
105+
106+
@Override
107+
protected void onCallbackException(final Throwable t) {
108+
Log.e(
109+
TAG, "Error while creating multireddit", t);
110+
throw new RuntimeException(t);
111+
}
112+
113+
@Override
114+
protected void onFailure(@NonNull final RRError error) {
115+
activity.runOnUiThread(() -> {
116+
final MaterialAlertDialogBuilder builder
117+
= new MaterialAlertDialogBuilder(activity);
118+
builder.setView(new ErrorView(activity, error));
119+
builder.create().show();
120+
});
121+
}
122+
123+
@Override
124+
protected void onSuccess() {
125+
activity.runOnUiThread(() -> Toast.makeText(
126+
activity,
127+
String.format("Created %s", multiredditName),
128+
Toast.LENGTH_SHORT).show());
129+
130+
RedditMultiredditSubscriptionManager.getSingleton(
131+
activity,
132+
RedditAccountManager.getInstance(activity).getDefaultAccount())
133+
.triggerUpdate(null, TimestampBound.NONE);
134+
}
135+
},
136+
user,
137+
multiredditName,
138+
subredditNames,
139+
activity
140+
);
141+
142+
Toast.makeText(
143+
activity,
144+
String.format("Creating %s", multiredditName),
145+
Toast.LENGTH_SHORT).show();
146+
}
147+
}

src/main/java/org/quantumbadger/redreader/reddit/RedditAPI.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import androidx.appcompat.app.AppCompatActivity;
2727

2828
import org.json.JSONArray;
29+
import org.json.JSONException;
2930
import org.json.JSONObject;
3031
import org.quantumbadger.redreader.account.RedditAccount;
3132
import org.quantumbadger.redreader.activities.BugReportActivity;
@@ -67,7 +68,6 @@
6768
import java.util.ArrayList;
6869
import java.util.Collection;
6970
import java.util.Collections;
70-
import java.util.HashMap;
7171
import java.util.LinkedList;
7272
import java.util.List;
7373
import java.util.Map;
@@ -1006,20 +1006,30 @@ public static void createMultireddit(
10061006
.appendPath("m")
10071007
.appendPath(multiredditName);
10081008

1009-
final Map<String, String> jsonData = new HashMap<>();
1010-
jsonData.put("display_name", multiredditName);
1011-
jsonData.put("visibility", "private");
1012-
jsonData.put("subreddits", new JSONArray(subredditNames).toString());
1009+
final JSONObject jsonObject = new JSONObject();
1010+
try {
1011+
jsonObject.put("display_name", multiredditName);
1012+
jsonObject.put("subreddits", subredditNamesJson(subredditNames));
1013+
} catch (final JSONException e) {
1014+
throw new RuntimeException(e);
1015+
}
10131016

10141017
cm.makeRequest(createPostRequest(
10151018
UriString.from(builder.build()),
10161019
user,
10171020
new ArrayList<>(Collections.singleton(
1018-
new PostField("model", new JSONObject(jsonData).toString()))),
1021+
new PostField("model", jsonObject.toString()))),
10191022
context,
10201023
new GenericResponseHandler(handler)));
10211024
}
10221025

1026+
private static JSONArray subredditNamesJson(final List<String> subredditNames) {
1027+
final JSONArray jsonArray = new JSONArray();
1028+
subredditNames.stream().forEach(
1029+
sn -> jsonArray.put(new JSONObject(Collections.singletonMap("name", sn))));
1030+
return jsonArray;
1031+
}
1032+
10231033
@Nullable
10241034
private static APIResponseHandler.APIFailureType findFailureType(final JsonValue response) {
10251035

src/main/java/org/quantumbadger/redreader/reddit/api/RedditAPISubredditCombinationAction.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@
2424

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

27+
import org.quantumbadger.redreader.R;
2728
import org.quantumbadger.redreader.account.RedditAccount;
2829
import org.quantumbadger.redreader.account.RedditAccountManager;
30+
import org.quantumbadger.redreader.common.PrefsUtility;
31+
import org.quantumbadger.redreader.fragments.CreateMultiredditDialog;
32+
2933
import java.util.ArrayList;
34+
import java.util.EnumSet;
3035
import java.util.List;
3136

3237
public class RedditAPISubredditCombinationAction {
3338

3439
public enum SubredditCombinationAction {
40+
CREATE_MULTIREDDIT;
3541
}
3642

3743
private static class RCVMenuItem {
@@ -50,7 +56,7 @@ private RCVMenuItem(
5056

5157
public static void showActionMenu(
5258
final AppCompatActivity activity,
53-
List<String> subredditNames) {
59+
final List<String> subredditNames) {
5460

5561
final EnumSet<SubredditCombinationAction> itemPref
5662
= PrefsUtility.pref_menus_subreddit_combination_context_items();
@@ -68,6 +74,13 @@ public static void showActionMenu(
6874

6975
final ArrayList<RCVMenuItem> menu = new ArrayList<>();
7076

77+
if(itemPref.contains(SubredditCombinationAction.CREATE_MULTIREDDIT)) {
78+
menu.add(new RCVMenuItem(
79+
activity,
80+
R.string.create_multireddit,
81+
SubredditCombinationAction.CREATE_MULTIREDDIT));
82+
}
83+
7184
final String[] menuText = new String[menu.size()];
7285

7386
for(int i = 0; i < menuText.length; i++) {
@@ -92,5 +105,14 @@ private static void onActionMenuItemSelected(
92105
final List<String> subredditNames,
93106
final RedditAccount user,
94107
final SubredditCombinationAction action) {
108+
109+
switch(action) {
110+
case CREATE_MULTIREDDIT:
111+
CreateMultiredditDialog.show(
112+
activity,
113+
subredditNames,
114+
user
115+
);
116+
}
95117
}
96118
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
~ This file is part of RedReader.
5+
~
6+
~ RedReader is free software: you can redistribute it and/or modify
7+
~ it under the terms of the GNU General Public License as published by
8+
~ the Free Software Foundation, either version 3 of the License, or
9+
~ (at your option) any later version.
10+
~
11+
~ RedReader is distributed in the hope that it will be useful,
12+
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
~ GNU General Public License for more details.
15+
~
16+
~ You should have received a copy of the GNU General Public License
17+
~ along with RedReader. If not, see <http://www.gnu.org/licenses/>.
18+
-->
19+
20+
<LinearLayout
21+
xmlns:android="http://schemas.android.com/apk/res/android"
22+
xmlns:tools="http://schemas.android.com/tools"
23+
android:id="@+id/create_multireddit_root"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:orientation="vertical">
27+
28+
<TextView
29+
android:id="@+id/add_to_multireddit_header"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:layout_marginLeft="8dp"
33+
android:layout_marginRight="8dp"
34+
android:layout_marginTop="16dp"
35+
android:text="@string/create_multireddit" />
36+
37+
<EditText
38+
android:id="@+id/selected_multireddit"
39+
android:layout_width="match_parent"
40+
android:layout_height="wrap_content"
41+
android:layout_marginBottom="16dp"
42+
android:layout_marginLeft="16dp"
43+
android:layout_marginRight="16dp"
44+
android:layout_marginTop="16dp"
45+
android:autofillHints="Multireddit Name"
46+
android:inputType="textNoSuggestions"
47+
android:imeOptions="actionGo"
48+
android:completionThreshold="1"
49+
tools:ignore="LabelFor">
50+
51+
<requestFocus/>
52+
</EditText>
53+
54+
</LinearLayout>

src/main/res/values/arrays.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,9 +1332,11 @@
13321332

13331333
<!-- 2024-11-28 -->
13341334
<string-array name="pref_menus_subreddit_combinations_context_items">
1335+
<item>@string/create_multireddit</item>
13351336
</string-array>
13361337

13371338
<string-array name="pref_menus_subreddit_combinations_context_items_return">
1339+
<item>create_multireddit</item>
13381340
</string-array>
13391341

13401342
</resources>

src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,4 +1914,6 @@
19141914
<string name="pref_menus_subreddit_combinations_header">Subreddit Combinations</string>
19151915
<string name="pref_menus_subreddit_combinations_context_items_key" translatable="false">pref_menus_subreddit_combination_context_items</string>
19161916
<string name="pref_menus_subreddit_combinations_context_items_title">Action menu items</string>
1917+
1918+
<string name="create_multireddit">Create Multireddit</string>
19171919
</resources>

0 commit comments

Comments
 (0)