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