|
| 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.RedditAccount; |
| 37 | +import org.quantumbadger.redreader.cache.CacheManager; |
| 38 | +import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfNotCached; |
| 39 | +import org.quantumbadger.redreader.common.RRError; |
| 40 | +import org.quantumbadger.redreader.reddit.APIResponseHandler; |
| 41 | +import org.quantumbadger.redreader.reddit.RedditAPI; |
| 42 | +import org.quantumbadger.redreader.views.liststatus.ErrorView; |
| 43 | + |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +public class RemoveSubredditFromMultiDialog { |
| 48 | + |
| 49 | + private final static String TAG = "RemoveSubFromMulti"; |
| 50 | + |
| 51 | + public static void show( |
| 52 | + final AppCompatActivity activity, |
| 53 | + final String multiredditName, |
| 54 | + final RedditAccount user) { |
| 55 | + |
| 56 | + final MaterialAlertDialogBuilder alertBuilder |
| 57 | + = new MaterialAlertDialogBuilder(activity); |
| 58 | + |
| 59 | + final View root = activity.getLayoutInflater().inflate( |
| 60 | + R.layout.remove_subreddit_from_multireddit, |
| 61 | + null); |
| 62 | + |
| 63 | + RedditAPI.requestMultiredditSubredditList( |
| 64 | + CacheManager.getInstance(activity), |
| 65 | + multiredditName, |
| 66 | + user, |
| 67 | + activity, |
| 68 | + new APIResponseHandler |
| 69 | + .ValueResponseHandler<List<String>>(activity) { |
| 70 | + @Override |
| 71 | + protected void onSuccess(@NonNull final List<String> subreddits) { |
| 72 | + Collections.sort(subreddits); |
| 73 | + activity.runOnUiThread(() -> { |
| 74 | + showDialog( |
| 75 | + activity, |
| 76 | + multiredditName, |
| 77 | + subreddits, |
| 78 | + root, |
| 79 | + alertBuilder, |
| 80 | + user); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void onCallbackException(final Throwable t) { |
| 86 | + Log.e( |
| 87 | + TAG, |
| 88 | + "Error while deleting multireddit", |
| 89 | + t); |
| 90 | + throw new RuntimeException(t); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void onFailure(@NonNull final RRError error) { |
| 95 | + activity.runOnUiThread(() -> { |
| 96 | + final MaterialAlertDialogBuilder builder |
| 97 | + = new MaterialAlertDialogBuilder(activity); |
| 98 | + builder.setView(new ErrorView(activity, error)); |
| 99 | + builder.create().show(); |
| 100 | + }); |
| 101 | + } |
| 102 | + }, |
| 103 | + DownloadStrategyIfNotCached.INSTANCE |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + private static void showDialog( |
| 108 | + final AppCompatActivity activity, |
| 109 | + final String multiredditName, |
| 110 | + final List<String> subreddits, |
| 111 | + final View root, |
| 112 | + final MaterialAlertDialogBuilder alertBuilder, |
| 113 | + final RedditAccount user |
| 114 | + ) { |
| 115 | + final ArrayAdapter<String> autocompleteAdapter = new ArrayAdapter<>( |
| 116 | + activity, |
| 117 | + android.R.layout.simple_dropdown_item_1line, |
| 118 | + subreddits); |
| 119 | + |
| 120 | + final AutoCompleteTextView editText |
| 121 | + = root.findViewById(R.id.selected_subreddit); |
| 122 | + editText.setAdapter(autocompleteAdapter); |
| 123 | + |
| 124 | + alertBuilder.setView(root); |
| 125 | + |
| 126 | + alertBuilder.setNegativeButton(R.string.dialog_cancel, null); |
| 127 | + |
| 128 | + alertBuilder.setPositiveButton( |
| 129 | + R.string.dialog_go, |
| 130 | + (dialog, which) -> removeSubredditFromMultireddit( |
| 131 | + activity, editText, multiredditName, user)); |
| 132 | + |
| 133 | + final AlertDialog alertDialog = alertBuilder.create(); |
| 134 | + |
| 135 | + editText.setOnEditorActionListener((v, actionId, event) -> { |
| 136 | + if(actionId == EditorInfo.IME_ACTION_GO |
| 137 | + || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { |
| 138 | + removeSubredditFromMultireddit(activity, editText, multiredditName, user); |
| 139 | + alertDialog.dismiss(); |
| 140 | + } |
| 141 | + return false; |
| 142 | + }); |
| 143 | + |
| 144 | + alertDialog.getWindow() |
| 145 | + .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE |
| 146 | + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); |
| 147 | + alertDialog.show(); |
| 148 | + } |
| 149 | + |
| 150 | + private static void removeSubredditFromMultireddit( |
| 151 | + final AppCompatActivity activity, |
| 152 | + final AutoCompleteTextView editText, |
| 153 | + final String multiredditName, |
| 154 | + final RedditAccount user) { |
| 155 | + |
| 156 | + final String subredditName = editText.getText() |
| 157 | + .toString() |
| 158 | + .trim() |
| 159 | + .replace(" ", ""); |
| 160 | + |
| 161 | + RedditAPI.removeSubredditFromMultireddit( |
| 162 | + CacheManager.getInstance(activity), |
| 163 | + new APIResponseHandler.ActionResponseHandler(activity) { |
| 164 | + |
| 165 | + @Override |
| 166 | + protected void onCallbackException(final Throwable t) { |
| 167 | + Log.e( |
| 168 | + TAG, "Error while removing subreddit from multireddit", t); |
| 169 | + throw new RuntimeException(t); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + protected void onFailure(@NonNull final RRError error) { |
| 174 | + activity.runOnUiThread(() -> { |
| 175 | + final MaterialAlertDialogBuilder builder |
| 176 | + = new MaterialAlertDialogBuilder(activity); |
| 177 | + builder.setView(new ErrorView(activity, error)); |
| 178 | + builder.create().show(); |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + protected void onSuccess() { |
| 184 | + activity.runOnUiThread(() -> Toast.makeText( |
| 185 | + activity, |
| 186 | + String.format("Removed %s from %s", subredditName, multiredditName), |
| 187 | + Toast.LENGTH_SHORT).show()); |
| 188 | + } |
| 189 | + }, |
| 190 | + user, |
| 191 | + multiredditName, |
| 192 | + subredditName, |
| 193 | + activity |
| 194 | + ); |
| 195 | + |
| 196 | + Toast.makeText( |
| 197 | + activity, |
| 198 | + String.format("Removing %s from %s", subredditName, multiredditName), |
| 199 | + Toast.LENGTH_SHORT).show(); |
| 200 | + } |
| 201 | +} |
0 commit comments