Skip to content

Commit

Permalink
'Create Multireddit' action for subreddit combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrboisvert committed Nov 30, 2024
1 parent 7405e57 commit b239271
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*******************************************************************************
* 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.EditText;
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.RedditAccount;
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.views.liststatus.ErrorView;

import java.util.List;

public class CreateMultiredditDialog {

private final static String TAG = "CreateMultiredditDialog";

public static void show(
final AppCompatActivity activity,
final List<String> subredditNames,
final RedditAccount user) {

final MaterialAlertDialogBuilder alertBuilder
= new MaterialAlertDialogBuilder(activity);

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

final EditText editText
= root.findViewById(R.id.selected_multireddit);

alertBuilder.setView(root);

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

alertBuilder.setPositiveButton(
R.string.dialog_go,
(dialog, which) -> createMultireddit(activity, subredditNames, user, editText));

final AlertDialog alertDialog = alertBuilder.create();

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

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

private static void createMultireddit(
final AppCompatActivity activity,
final List<String> subredditNames,
final RedditAccount user,
final EditText editText) {

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

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

@Override
protected void onCallbackException(final Throwable t) {
Log.e(
TAG, "Error while creating 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("Created %s", multiredditName),
Toast.LENGTH_SHORT).show());

RedditMultiredditSubscriptionManager.getSingleton(
activity,
RedditAccountManager.getInstance(activity).getDefaultAccount())
.triggerUpdate(null, TimestampBound.NONE);
}
},
user,
multiredditName,
subredditNames,
activity
);

Toast.makeText(
activity,
String.format("Creating %s", multiredditName),
Toast.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@

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

import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccount;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.common.PrefsUtility;
import org.quantumbadger.redreader.fragments.CreateMultiredditDialog;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

public class RedditAPISubredditCombinationAction {

public enum SubredditCombinationAction {
CREATE_MULTIREDDIT;
}

private static class RCVMenuItem {
Expand All @@ -50,7 +56,7 @@ private RCVMenuItem(

public static void showActionMenu(
final AppCompatActivity activity,
List<String> subredditNames) {
final List<String> subredditNames) {

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

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

if(itemPref.contains(SubredditCombinationAction.CREATE_MULTIREDDIT)) {
menu.add(new RCVMenuItem(
activity,
R.string.create_multireddit,
SubredditCombinationAction.CREATE_MULTIREDDIT));
}

final String[] menuText = new String[menu.size()];

for(int i = 0; i < menuText.length; i++) {
Expand All @@ -92,5 +105,14 @@ private static void onActionMenuItemSelected(
final List<String> subredditNames,
final RedditAccount user,
final SubredditCombinationAction action) {

switch(action) {
case CREATE_MULTIREDDIT:
CreateMultiredditDialog.show(
activity,
subredditNames,
user
);
}
}
}
54 changes: 54 additions & 0 deletions src/main/res/layout/create_multireddit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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/create_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/create_multireddit" />

<EditText
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:autofillHints="Multireddit Name"
android:inputType="textNoSuggestions"
android:imeOptions="actionGo"
android:completionThreshold="1"
tools:ignore="LabelFor">

<requestFocus/>
</EditText>

</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 @@ -1330,9 +1330,11 @@

<!-- 2024-11-28 -->
<string-array name="pref_menus_subreddit_combinations_context_items">
<item>@string/create_multireddit</item>
</string-array>

<string-array name="pref_menus_subreddit_combinations_context_items_return">
<item>create_multireddit</item>
</string-array>

</resources>
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1912,4 +1912,6 @@
<string name="pref_menus_subreddit_combinations_header">Subreddit Combinations</string>
<string name="pref_menus_subreddit_combinations_context_items_key" translatable="false">pref_menus_subreddit_combination_context_items</string>
<string name="pref_menus_subreddit_combinations_context_items_title">Action menu items</string>

<string name="create_multireddit">Create Multireddit</string>
</resources>

0 comments on commit b239271

Please sign in to comment.