|
| 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.ArrayAdapter; |
| 26 | +import android.widget.AutoCompleteTextView; |
| 27 | +import android.widget.Toast; |
| 28 | + |
| 29 | +import androidx.annotation.NonNull; |
| 30 | +import androidx.appcompat.app.AlertDialog; |
| 31 | +import androidx.appcompat.app.AppCompatActivity; |
| 32 | + |
| 33 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
| 34 | + |
| 35 | +import org.quantumbadger.redreader.R; |
| 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.reddit.things.SubredditCanonicalId; |
| 44 | +import org.quantumbadger.redreader.views.liststatus.ErrorView; |
| 45 | + |
| 46 | +import java.util.ArrayList; |
| 47 | +import java.util.Collections; |
| 48 | + |
| 49 | +public class AddToMultiredditDialog { |
| 50 | + |
| 51 | + private final static String TAG = "AddToMultiredditDialog"; |
| 52 | + |
| 53 | + public static void show( |
| 54 | + final AppCompatActivity activity, |
| 55 | + final SubredditCanonicalId subredditCanonicalId) { |
| 56 | + |
| 57 | + final MaterialAlertDialogBuilder alertBuilder |
| 58 | + = new MaterialAlertDialogBuilder(activity); |
| 59 | + |
| 60 | + final View root = activity.getLayoutInflater().inflate( |
| 61 | + R.layout.add_to_multireddit, |
| 62 | + null); |
| 63 | + |
| 64 | + final RedditMultiredditSubscriptionManager multiredditManager |
| 65 | + = RedditMultiredditSubscriptionManager.getSingleton( |
| 66 | + activity, |
| 67 | + RedditAccountManager.getInstance(activity).getDefaultAccount()); |
| 68 | + |
| 69 | + final ArrayList<String> multireddits = multiredditManager.getSubscriptionList(); |
| 70 | + Collections.sort(multireddits); |
| 71 | + |
| 72 | + final ArrayAdapter<String> autocompleteAdapter = new ArrayAdapter<>( |
| 73 | + activity, |
| 74 | + android.R.layout.simple_dropdown_item_1line, |
| 75 | + multireddits); |
| 76 | + |
| 77 | + final AutoCompleteTextView editText |
| 78 | + = root.findViewById(R.id.selected_multireddit); |
| 79 | + editText.setAdapter(autocompleteAdapter); |
| 80 | + |
| 81 | + alertBuilder.setView(root); |
| 82 | + |
| 83 | + alertBuilder.setNegativeButton(R.string.dialog_cancel, null); |
| 84 | + |
| 85 | + alertBuilder.setPositiveButton( |
| 86 | + R.string.dialog_go, |
| 87 | + (dialog, which) -> addToMultireddit(activity, editText, subredditCanonicalId)); |
| 88 | + |
| 89 | + final AlertDialog alertDialog = alertBuilder.create(); |
| 90 | + |
| 91 | + editText.setOnEditorActionListener((v, actionId, event) -> { |
| 92 | + if(actionId == EditorInfo.IME_ACTION_GO |
| 93 | + || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { |
| 94 | + addToMultireddit(activity, editText, subredditCanonicalId); |
| 95 | + alertDialog.dismiss(); |
| 96 | + } |
| 97 | + return false; |
| 98 | + }); |
| 99 | + |
| 100 | + alertDialog.getWindow() |
| 101 | + .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE |
| 102 | + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); |
| 103 | + alertDialog.show(); |
| 104 | + } |
| 105 | + |
| 106 | + private static void addToMultireddit( |
| 107 | + final AppCompatActivity activity, |
| 108 | + final AutoCompleteTextView editText, |
| 109 | + final SubredditCanonicalId subredditCanonicalId) { |
| 110 | + |
| 111 | + final String multiredditName = editText.getText() |
| 112 | + .toString() |
| 113 | + .trim() |
| 114 | + .replace(" ", ""); |
| 115 | + final String subredditName = subredditCanonicalId.getDisplayNameLowercase(); |
| 116 | + |
| 117 | + RedditAPI.addSubredditToMultireddit( |
| 118 | + CacheManager.getInstance(activity), |
| 119 | + new APIResponseHandler.ActionResponseHandler(activity) { |
| 120 | + |
| 121 | + @Override |
| 122 | + protected void onCallbackException(final Throwable t) { |
| 123 | + Log.e( |
| 124 | + TAG, "Error while adding subreddit to multireddit", t); |
| 125 | + throw new RuntimeException(t); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void onFailure(@NonNull final RRError error) { |
| 130 | + activity.runOnUiThread(() -> { |
| 131 | + final MaterialAlertDialogBuilder builder |
| 132 | + = new MaterialAlertDialogBuilder(activity); |
| 133 | + builder.setView(new ErrorView(activity, error)); |
| 134 | + builder.create().show(); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected void onSuccess() { |
| 140 | + activity.runOnUiThread(() -> Toast.makeText( |
| 141 | + activity, |
| 142 | + String.format("Added %s to %s", subredditName, multiredditName), |
| 143 | + Toast.LENGTH_SHORT).show()); |
| 144 | + RedditMultiredditSubscriptionManager.getSingleton( |
| 145 | + activity, |
| 146 | + RedditAccountManager |
| 147 | + .getInstance(activity).getDefaultAccount()) |
| 148 | + .triggerUpdate(null, TimestampBound.NONE); |
| 149 | + } |
| 150 | + }, |
| 151 | + RedditAccountManager.getInstance(activity).getDefaultAccount(), |
| 152 | + multiredditName, |
| 153 | + subredditName, |
| 154 | + activity |
| 155 | + ); |
| 156 | + |
| 157 | + Toast.makeText( |
| 158 | + activity, |
| 159 | + String.format("Adding %s to %s", subredditName, multiredditName), |
| 160 | + Toast.LENGTH_SHORT).show(); |
| 161 | + } |
| 162 | +} |
0 commit comments