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