| 1 | package tw.nekomimi.nkmr; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.widget.LinearLayout; |
| 5 | |
| 6 | import androidx.recyclerview.widget.RecyclerView; |
| 7 | |
| 8 | import org.telegram.messenger.AndroidUtilities; |
| 9 | import org.telegram.messenger.LocaleController; |
| 10 | import org.telegram.messenger.R; |
| 11 | import org.telegram.ui.ActionBar.AlertDialog; |
| 12 | import org.telegram.ui.ActionBar.BaseFragment; |
| 13 | import org.telegram.ui.ActionBar.Theme; |
| 14 | import org.telegram.ui.Cells.HeaderCell; |
| 15 | import org.telegram.ui.Cells.RadioColorCell; |
| 16 | import org.telegram.ui.Cells.TextCheckCell; |
| 17 | import org.telegram.ui.Cells.TextDetailSettingsCell; |
| 18 | import org.telegram.ui.Cells.TextSettingsCell; |
| 19 | import org.telegram.ui.Components.EditTextBoldCursor; |
| 20 | import org.telegram.ui.Components.LayoutHelper; |
| 21 | import org.telegram.ui.Components.RecyclerListView; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | |
| 25 | import cn.hutool.core.util.StrUtil; |
| 26 | |
| 27 | public class Cells { |
| 28 | public static final int ITEM_TYPE_DIVIDER = 1; |
| 29 | public static final int ITEM_TYPE_TEXT_SETTINGS_CELL = 2; |
| 30 | public static final int ITEM_TYPE_TEXT_CHECK = 3; |
| 31 | public static final int ITEM_TYPE_HEADER = 4; |
| 32 | public static final int ITEM_TYPE_TEXT_DETAIL = 5; |
| 33 | public static final int ITEM_TYPE_TEXT = 6; |
| 34 | |
| 35 | |
| 36 | private BaseFragment thisFragment; |
| 37 | private RecyclerListView listView; |
| 38 | private RecyclerListView.SelectionAdapter listAdapter; |
| 39 | private ArrayList<NekomuraTGCell> rows; |
| 40 | |
| 41 | public CallBackSettingsChanged callBackSettingsChanged; |
| 42 | |
| 43 | public Cells(BaseFragment thisFragment, ArrayList<NekomuraTGCell> rows) { |
| 44 | this.thisFragment = thisFragment; |
| 45 | this.rows = rows; |
| 46 | } |
| 47 | |
| 48 | public void setListAdapter(RecyclerListView lv, RecyclerListView.SelectionAdapter la) { |
| 49 | this.listView = lv; |
| 50 | this.listAdapter = la; |
| 51 | } |
| 52 | |
| 53 | public interface CallBackSettingsChanged { |
| 54 | void run(String key, Object newValue); |
| 55 | } |
| 56 | |
| 57 | private void runCallback(String key, Object newValue) { |
| 58 | if (callBackSettingsChanged == null) return; |
| 59 | try { |
| 60 | callBackSettingsChanged.run(key, newValue); |
| 61 | } catch (Exception e) { |
| 62 | |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public interface OnBindViewHolder { |
| 67 | void onBindViewHolder(RecyclerView.ViewHolder holder); |
| 68 | } |
| 69 | |
| 70 | public interface NekomuraTGCell { |
| 71 | int getType(); |
| 72 | |
| 73 | boolean isEnabled(); |
| 74 | |
| 75 | void onBindViewHolder(RecyclerView.ViewHolder holder); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | //TG Cells |
| 80 | |
| 81 | public class NekomuraTGDivider implements NekomuraTGCell { |
| 82 | public int getType() { |
| 83 | return ITEM_TYPE_DIVIDER; |
| 84 | } |
| 85 | |
| 86 | public boolean isEnabled() { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public class NekomuraTGCustom implements NekomuraTGCell { |
| 95 | public final int type; |
| 96 | public final boolean enabled; |
| 97 | |
| 98 | public NekomuraTGCustom(int type, boolean enabled) { |
| 99 | this.type = type; |
| 100 | this.enabled = enabled; |
| 101 | } |
| 102 | |
| 103 | public int getType() { |
| 104 | return type; |
| 105 | } |
| 106 | |
| 107 | public boolean isEnabled() { |
| 108 | return enabled; |
| 109 | } |
| 110 | |
| 111 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 112 | // Not Used |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public class NekomuraTGHeader implements NekomuraTGCell { |
| 117 | private final String title; |
| 118 | |
| 119 | public NekomuraTGHeader(String title) { |
| 120 | this.title = title; |
| 121 | } |
| 122 | |
| 123 | public int getType() { |
| 124 | return ITEM_TYPE_HEADER; |
| 125 | } |
| 126 | |
| 127 | public boolean isEnabled() { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 132 | HeaderCell headerCell = (HeaderCell) holder.itemView; |
| 133 | headerCell.setText(title); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | //TextDetailSettingsCell |
| 138 | public class NekomuraTGTextDetail implements NekomuraTGCell { |
| 139 | private final NekomuraConfig.ConfigItem bindConfig; |
| 140 | private final String title; |
| 141 | private final String hint; |
| 142 | public final RecyclerListView.OnItemClickListener onItemClickListener; |
| 143 | |
| 144 | public NekomuraTGTextDetail(NekomuraConfig.ConfigItem bind, RecyclerListView.OnItemClickListener onItemClickListener, String hint) { |
| 145 | this.bindConfig = bind; |
| 146 | this.title = LocaleController.getString(bindConfig.getKey()); |
| 147 | this.hint = hint == null ? "" : hint; |
| 148 | this.onItemClickListener = onItemClickListener; |
| 149 | } |
| 150 | |
| 151 | public int getType() { |
| 152 | return ITEM_TYPE_TEXT_DETAIL; |
| 153 | } |
| 154 | |
| 155 | public boolean isEnabled() { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 160 | TextDetailSettingsCell cell = (TextDetailSettingsCell) holder.itemView; |
| 161 | cell.setTextAndValue(title, StrUtil.isNotBlank(bindConfig.String()) ? bindConfig.String() : hint , !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 162 | } |
| 163 | } |
| 164 | public class NekomuraTGTextCheck implements NekomuraTGCell { |
| 165 | private final NekomuraConfig.ConfigItem bindConfig; |
| 166 | private final String title; |
| 167 | private final String subtitle; |
| 168 | |
| 169 | public NekomuraTGTextCheck(NekomuraConfig.ConfigItem bind) { |
| 170 | this.bindConfig = bind; |
| 171 | this.title = LocaleController.getString(bindConfig.getKey()); |
| 172 | this.subtitle = null; |
| 173 | } |
| 174 | |
| 175 | public NekomuraTGTextCheck(NekomuraConfig.ConfigItem bind, String subtitle) { |
| 176 | this.bindConfig = bind; |
| 177 | this.title = LocaleController.getString(bindConfig.getKey()); |
| 178 | this.subtitle = subtitle; |
| 179 | } |
| 180 | |
| 181 | public int getType() { |
| 182 | return ITEM_TYPE_TEXT_CHECK; |
| 183 | } |
| 184 | |
| 185 | public boolean isEnabled() { |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 190 | TextCheckCell cell = (TextCheckCell) holder.itemView; |
| 191 | if (subtitle == null) { |
| 192 | cell.setTextAndCheck(title, bindConfig.Bool(), !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 193 | } else { |
| 194 | cell.setTextAndValueAndCheck(title, subtitle, bindConfig.Bool(), true, !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public void onClick(TextCheckCell cell) { |
| 199 | boolean newV = bindConfig.toggleConfigBool(); |
| 200 | cell.setChecked(newV); |
| 201 | |
| 202 | runCallback(bindConfig.getKey(), newV); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // TextSettingsCell, select from a list |
| 207 | // Can be used without select list(custom) |
| 208 | public class NekomuraTGSelectBox implements NekomuraTGCell { |
| 209 | private final NekomuraConfig.ConfigItem bindConfig; |
| 210 | private final String[] selectList; // split by \n |
| 211 | private final String title; |
| 212 | private Runnable onClickCustom; |
| 213 | |
| 214 | |
| 215 | // default: customTitle=null customOnClick=null |
| 216 | public NekomuraTGSelectBox(String customTitle, NekomuraConfig.ConfigItem bind, Object selectList_s, Runnable customOnClick) { |
| 217 | this.bindConfig = bind; |
| 218 | if (selectList_s == null) { |
| 219 | this.selectList = null; |
| 220 | } else if (selectList_s instanceof String) { |
| 221 | this.selectList = ((String) selectList_s).split("\n"); |
| 222 | } else if (selectList_s instanceof String[]) { |
| 223 | this.selectList = (String[]) selectList_s; |
| 224 | } else { |
| 225 | this.selectList = null; |
| 226 | } |
| 227 | if (customTitle == null) { |
| 228 | title = LocaleController.getString(bindConfig.getKey()); |
| 229 | } else { |
| 230 | title = customTitle; |
| 231 | } |
| 232 | this.onClickCustom = customOnClick; |
| 233 | } |
| 234 | |
| 235 | public int getType() { |
| 236 | return ITEM_TYPE_TEXT_SETTINGS_CELL; |
| 237 | } |
| 238 | |
| 239 | public boolean isEnabled() { |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 244 | TextSettingsCell cell = (TextSettingsCell) holder.itemView; |
| 245 | cell.setTextAndValue(title, selectList == null ? "" : selectList[bindConfig.Int()], !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 246 | } |
| 247 | |
| 248 | public void onClick() { |
| 249 | if (onClickCustom != null) { |
| 250 | try { |
| 251 | onClickCustom.run(); |
| 252 | } catch (Exception e) { |
| 253 | } |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | Context context = thisFragment.getParentActivity(); |
| 258 | if (context == null) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | AlertDialog.Builder builder = new AlertDialog.Builder(context); //TODO Replace with pop-up menu |
| 263 | builder.setTitle(LocaleController.getString(bindConfig.getKey())); |
| 264 | final LinearLayout linearLayout = new LinearLayout(context); |
| 265 | linearLayout.setOrientation(LinearLayout.VERTICAL); |
| 266 | builder.setView(linearLayout); |
| 267 | |
| 268 | for (int i = 0; i < selectList.length; i++) { |
| 269 | RadioColorCell cell = new RadioColorCell(context); |
| 270 | cell.setPadding(AndroidUtilities.dp(4), 0, AndroidUtilities.dp(4), 0); |
| 271 | cell.setTag(i); |
| 272 | cell.setCheckColor(Theme.getColor(Theme.key_radioBackground), Theme.getColor(Theme.key_dialogRadioBackgroundChecked)); |
| 273 | cell.setTextAndValue(selectList[i], bindConfig.Int() == i); |
| 274 | linearLayout.addView(cell); |
| 275 | cell.setOnClickListener(v -> { |
| 276 | Integer which = (Integer) v.getTag(); |
| 277 | bindConfig.setConfigInt(which); |
| 278 | |
| 279 | listAdapter.notifyItemChanged(rows.indexOf(this)); |
| 280 | builder.getDismissRunnable().run(); |
| 281 | thisFragment.parentLayout.rebuildAllFragmentViews(false, false); |
| 282 | |
| 283 | runCallback(bindConfig.getKey(), which); |
| 284 | }); |
| 285 | } |
| 286 | builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); |
| 287 | thisFragment.showDialog(builder.create()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | public class NekomuraTGTextInput implements NekomuraTGCell { |
| 292 | private final NekomuraConfig.ConfigItem bindConfig; |
| 293 | private final String hint; |
| 294 | private final String title; |
| 295 | private Runnable onClickCustom; |
| 296 | |
| 297 | |
| 298 | // default: customTitle=null customOnClick=null |
| 299 | public NekomuraTGTextInput(String customTitle, NekomuraConfig.ConfigItem bind, String hint, Runnable customOnClick) { |
| 300 | this.bindConfig = bind; |
| 301 | if (hint == null) { |
| 302 | this.hint = ""; |
| 303 | } else { |
| 304 | this.hint = hint; |
| 305 | } |
| 306 | if (customTitle == null) { |
| 307 | title = LocaleController.getString(bindConfig.getKey()); |
| 308 | } else { |
| 309 | title = customTitle; |
| 310 | } |
| 311 | this.onClickCustom = customOnClick; |
| 312 | } |
| 313 | |
| 314 | public int getType() { |
| 315 | return ITEM_TYPE_TEXT_SETTINGS_CELL; |
| 316 | } |
| 317 | |
| 318 | public boolean isEnabled() { |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 323 | TextSettingsCell cell = (TextSettingsCell) holder.itemView; |
| 324 | cell.setTextAndValue(title, bindConfig.String(), !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 325 | } |
| 326 | |
| 327 | public void onClick() { |
| 328 | if (onClickCustom != null) { |
| 329 | try { |
| 330 | onClickCustom.run(); |
| 331 | } catch (Exception e) { |
| 332 | } |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | Context context = thisFragment.getParentActivity(); |
| 337 | if (context == null) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 342 | builder.setTitle(LocaleController.getString(bindConfig.getKey())); |
| 343 | LinearLayout linearLayout = new LinearLayout(context); |
| 344 | linearLayout.setOrientation(LinearLayout.VERTICAL); |
| 345 | |
| 346 | EditTextBoldCursor editText = new EditTextBoldCursor(context); |
| 347 | editText.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText)); |
| 348 | editText.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText)); |
| 349 | editText.setHint(hint); |
| 350 | editText.setText(bindConfig.String()); |
| 351 | linearLayout.addView(editText, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, AndroidUtilities.dp(8), 0, AndroidUtilities.dp(10), 0)); |
| 352 | |
| 353 | builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), (d, v) -> { |
| 354 | String newV = editText.getText().toString(); |
| 355 | bindConfig.setConfigString(newV); |
| 356 | |
| 357 | //refresh |
| 358 | listAdapter.notifyItemChanged(rows.indexOf(this)); |
| 359 | builder.getDismissRunnable().run(); |
| 360 | thisFragment.parentLayout.rebuildAllFragmentViews(false, false); |
| 361 | |
| 362 | runCallback(bindConfig.getKey(), newV); |
| 363 | }); |
| 364 | builder.setView(linearLayout); |
| 365 | thisFragment.showDialog(builder.create()); |
| 366 | } |
| 367 | } |
| 368 | } |