| 1 | package tw.nekomimi.nkmr; |
| 2 | |
| 3 | import android.annotation.SuppressLint; |
| 4 | import android.content.Context; |
| 5 | import android.os.Bundle; |
| 6 | import android.view.Gravity; |
| 7 | import android.view.View; |
| 8 | import android.view.ViewGroup; |
| 9 | import android.widget.FrameLayout; |
| 10 | import android.widget.LinearLayout; |
| 11 | |
| 12 | import androidx.recyclerview.widget.LinearLayoutManager; |
| 13 | import androidx.recyclerview.widget.RecyclerView; |
| 14 | |
| 15 | import org.telegram.messenger.AndroidUtilities; |
| 16 | import org.telegram.messenger.LocaleController; |
| 17 | import org.telegram.messenger.MessagesController; |
| 18 | import org.telegram.messenger.NotificationCenter; |
| 19 | import org.telegram.messenger.R; |
| 20 | import org.telegram.messenger.UserConfig; |
| 21 | import org.telegram.messenger.UserObject; |
| 22 | import org.telegram.tgnet.TLRPC; |
| 23 | import org.telegram.ui.ActionBar.ActionBar; |
| 24 | import org.telegram.ui.ActionBar.AlertDialog; |
| 25 | import org.telegram.ui.ActionBar.BaseFragment; |
| 26 | import org.telegram.ui.ActionBar.Theme; |
| 27 | import org.telegram.ui.ActionBar.ThemeDescription; |
| 28 | import org.telegram.ui.Cells.EmptyCell; |
| 29 | import org.telegram.ui.Cells.HeaderCell; |
| 30 | import org.telegram.ui.Cells.NotificationsCheckCell; |
| 31 | import org.telegram.ui.Cells.RadioColorCell; |
| 32 | import org.telegram.ui.Cells.ShadowSectionCell; |
| 33 | import org.telegram.ui.Cells.TextCheckCell; |
| 34 | import org.telegram.ui.Cells.TextDetailSettingsCell; |
| 35 | import org.telegram.ui.Cells.TextInfoPrivacyCell; |
| 36 | import org.telegram.ui.Cells.TextSettingsCell; |
| 37 | import org.telegram.ui.Components.LayoutHelper; |
| 38 | import org.telegram.ui.Components.RecyclerListView; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | |
| 42 | @SuppressLint("RtlHardcoded") |
| 43 | public class NekomuraSettingsFragment extends BaseFragment { |
| 44 | |
| 45 | private RecyclerListView listView; |
| 46 | private ListAdapter listAdapter; |
| 47 | |
| 48 | private ArrayList<NekomuraTGCell> rows = new ArrayList<>(); |
| 49 | |
| 50 | //添加设置行 |
| 51 | |
| 52 | private final NekomuraTGCell header1 = addNekomuraTGCell(new NekomuraTGHeader(LocaleController.getString("General"))); |
| 53 | private final NekomuraTGCell largeAvatarInDrawer = addNekomuraTGCell(new NekomuraTGTextSettings(null, NekomuraConfig.largeAvatarInDrawer, LocaleController.getString("valuesLargeAvatarInDrawer"), null)); |
| 54 | private final NekomuraTGCell unreadBadgeOnBackButton = addNekomuraTGCell(new NekomuraTGTextCheck(NekomuraConfig.unreadBadgeOnBackButton)); |
| 55 | private final NekomuraTGCell divider1 = addNekomuraTGCell(new NekomuraTGDivider()); |
| 56 | |
| 57 | private static final int ITEM_TYPE_DIVIDER = 1; |
| 58 | private static final int ITEM_TYPE_TEXT_SETTINGS = 2; //可以在右边设置文字 |
| 59 | private static final int ITEM_TYPE_TEXT_CHECK = 3; |
| 60 | private static final int ITEM_TYPE_HEADER = 4; |
| 61 | private static final int ITEM_TYPE_TEXT_DETAIL = 6; |
| 62 | private static final int ITEM_TYPE_TEXT = 7; |
| 63 | |
| 64 | public NekomuraTGCell addNekomuraTGCell(NekomuraTGCell a) { |
| 65 | rows.add(a); |
| 66 | return a; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean onFragmentCreate() { |
| 71 | super.onFragmentCreate(); |
| 72 | |
| 73 | updateRows(); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | @SuppressLint("NewApi") |
| 79 | @Override |
| 80 | public View createView(Context context) { |
| 81 | actionBar.setBackButtonImage(R.drawable.ic_ab_back); |
| 82 | actionBar.setTitle(LocaleController.getString("NekomuraSettings", R.string.NekomuraSettings)); |
| 83 | |
| 84 | actionBar.setOccupyStatusBar(!AndroidUtilities.isTablet()); |
| 85 | actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() { |
| 86 | @Override |
| 87 | public void onItemClick(int id) { |
| 88 | if (id == -1) { |
| 89 | finishFragment(); |
| 90 | } |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | listAdapter = new ListAdapter(context); |
| 95 | |
| 96 | fragmentView = new FrameLayout(context); |
| 97 | fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray)); |
| 98 | FrameLayout frameLayout = (FrameLayout) fragmentView; |
| 99 | |
| 100 | listView = new RecyclerListView(context); |
| 101 | listView.setVerticalScrollBarEnabled(false); |
| 102 | listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)); |
| 103 | frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); |
| 104 | listView.setAdapter(listAdapter); |
| 105 | |
| 106 | // 上面是各种窗口初始化 |
| 107 | |
| 108 | // 设置回调事件。。。 |
| 109 | listView.setOnItemClickListener((view, position, x, y) -> { |
| 110 | NekomuraTGCell a = rows.get(position); |
| 111 | if (a instanceof NekomuraTGTextCheck) { |
| 112 | ((NekomuraTGTextCheck) a).onClick((TextCheckCell) view); |
| 113 | } else if (a instanceof NekomuraTGTextSettings) { |
| 114 | ((NekomuraTGTextSettings) a).onClick(); |
| 115 | } |
| 116 | }); |
| 117 | listView.setOnItemLongClickListener((view, position) -> { |
| 118 | return true; |
| 119 | }); |
| 120 | |
| 121 | return fragmentView; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onResume() { |
| 126 | super.onResume(); |
| 127 | if (listAdapter != null) { |
| 128 | listAdapter.notifyDataSetChanged(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private void updateRows() { |
| 133 | if (listAdapter != null) { |
| 134 | listAdapter.notifyDataSetChanged(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public ArrayList<ThemeDescription> getThemeDescriptions() { |
| 140 | ArrayList<ThemeDescription> themeDescriptions = new ArrayList<>(); |
| 141 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{EmptyCell.class, TextSettingsCell.class, TextCheckCell.class, HeaderCell.class, TextDetailSettingsCell.class, NotificationsCheckCell.class}, null, null, null, Theme.key_windowBackgroundWhite)); |
| 142 | themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray)); |
| 143 | |
| 144 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue)); |
| 145 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_avatar_backgroundActionBarBlue)); |
| 146 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_avatar_actionBarIconBlue)); |
| 147 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle)); |
| 148 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_avatar_actionBarSelectorBlue)); |
| 149 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUBACKGROUND, null, null, null, null, Theme.key_actionBarDefaultSubmenuBackground)); |
| 150 | themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SUBMENUITEM, null, null, null, null, Theme.key_actionBarDefaultSubmenuItem)); |
| 151 | |
| 152 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector)); |
| 153 | |
| 154 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider)); |
| 155 | |
| 156 | themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{ShadowSectionCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow)); |
| 157 | |
| 158 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 159 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteValueText)); |
| 160 | |
| 161 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 162 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); |
| 163 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack)); |
| 164 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{NotificationsCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked)); |
| 165 | |
| 166 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 167 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); |
| 168 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack)); |
| 169 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked)); |
| 170 | |
| 171 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{HeaderCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader)); |
| 172 | |
| 173 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText)); |
| 174 | themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextDetailSettingsCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2)); |
| 175 | |
| 176 | return themeDescriptions; |
| 177 | } |
| 178 | |
| 179 | private class ListAdapter extends RecyclerListView.SelectionAdapter { |
| 180 | |
| 181 | private Context mContext; |
| 182 | |
| 183 | public ListAdapter(Context context) { |
| 184 | mContext = context; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int getItemCount() { |
| 189 | return rows.size(); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 194 | // onBindViewHolder 一般设置文字,是否打勾什么的 |
| 195 | NekomuraTGCell a = rows.get(position); |
| 196 | if (a != null) { |
| 197 | a.onBindViewHolder(holder); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public boolean isEnabled(RecyclerView.ViewHolder holder) { |
| 203 | int position = holder.getAdapterPosition(); |
| 204 | NekomuraTGCell a = rows.get(position); |
| 205 | if (a != null) { |
| 206 | return a.isEnabled(); |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 213 | View view = null; |
| 214 | switch (viewType) { |
| 215 | case ITEM_TYPE_DIVIDER: |
| 216 | view = new ShadowSectionCell(mContext); |
| 217 | break; |
| 218 | case ITEM_TYPE_TEXT_SETTINGS: |
| 219 | view = new TextSettingsCell(mContext); |
| 220 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 221 | break; |
| 222 | case ITEM_TYPE_TEXT_CHECK: |
| 223 | view = new TextCheckCell(mContext); |
| 224 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 225 | break; |
| 226 | case ITEM_TYPE_HEADER: |
| 227 | view = new HeaderCell(mContext); |
| 228 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 229 | break; |
| 230 | case ITEM_TYPE_TEXT_DETAIL: |
| 231 | view = new TextDetailSettingsCell(mContext); |
| 232 | view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); |
| 233 | break; |
| 234 | case ITEM_TYPE_TEXT: |
| 235 | view = new TextInfoPrivacyCell(mContext); |
| 236 | // view.setBackground(Theme.getThemedDrawable(mContext, R.drawable.greydivider, Theme.key_windowBackgroundGrayShadow)); |
| 237 | break; |
| 238 | } |
| 239 | //noinspection ConstantConditions |
| 240 | view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); |
| 241 | return new RecyclerListView.Holder(view); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public int getItemViewType(int position) { |
| 246 | NekomuraTGCell a = rows.get(position); |
| 247 | if (a != null) { |
| 248 | return a.getType(); |
| 249 | } |
| 250 | return 6; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | //设置对象 |
| 255 | |
| 256 | public interface NekomuraTGCell { |
| 257 | int getType(); |
| 258 | |
| 259 | boolean isEnabled(); |
| 260 | |
| 261 | void onBindViewHolder(RecyclerView.ViewHolder holder); |
| 262 | } |
| 263 | |
| 264 | public class NekomuraTGDivider implements NekomuraTGCell { |
| 265 | public int getType() { |
| 266 | return ITEM_TYPE_DIVIDER; |
| 267 | } |
| 268 | |
| 269 | public boolean isEnabled() { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public class NekomuraTGHeader implements NekomuraTGCell { |
| 278 | private final String title; |
| 279 | |
| 280 | public NekomuraTGHeader(String title) { |
| 281 | this.title = title; |
| 282 | } |
| 283 | |
| 284 | public int getType() { |
| 285 | return ITEM_TYPE_HEADER; |
| 286 | } |
| 287 | |
| 288 | public boolean isEnabled() { |
| 289 | return false; |
| 290 | //还有特殊情况哦 |
| 291 | } |
| 292 | |
| 293 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 294 | HeaderCell headerCell = (HeaderCell) holder.itemView; |
| 295 | headerCell.setText(title); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | public class NekomuraTGTextCheck implements NekomuraTGCell { |
| 300 | private final NekomuraConfig.ConfigItem bindConfig; |
| 301 | private final String title; |
| 302 | |
| 303 | |
| 304 | public NekomuraTGTextCheck(NekomuraConfig.ConfigItem bind) { |
| 305 | this.bindConfig = bind; |
| 306 | this.title = LocaleController.getString(bindConfig.getKey()); |
| 307 | } |
| 308 | |
| 309 | public NekomuraTGTextCheck(String customTitle, NekomuraConfig.ConfigItem bind) { |
| 310 | this.bindConfig = bind; |
| 311 | if (customTitle == null) { |
| 312 | title = LocaleController.getString(bindConfig.getKey()); |
| 313 | } else { |
| 314 | title = customTitle; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | public int getType() { |
| 319 | return ITEM_TYPE_TEXT_CHECK; |
| 320 | } |
| 321 | |
| 322 | public boolean isEnabled() { |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 327 | TextCheckCell cell = (TextCheckCell) holder.itemView; |
| 328 | cell.setTextAndCheck(title, bindConfig.Bool(), !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 329 | } |
| 330 | |
| 331 | public void onClick(TextCheckCell cell) { |
| 332 | cell.setChecked(bindConfig.toggleConfigBool()); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | public class NekomuraTGTextSettings implements NekomuraTGCell { |
| 337 | private final NekomuraConfig.ConfigItem bindConfig; |
| 338 | private final String[] selectList;//实际上是int类型的设置 |
| 339 | private final String title; |
| 340 | private Runnable onClickCustom; |
| 341 | |
| 342 | |
| 343 | //不需要的custom就填null |
| 344 | public NekomuraTGTextSettings(String customTitle, NekomuraConfig.ConfigItem bind, String selectList_s, Runnable customOnClick) { |
| 345 | this.bindConfig = bind; |
| 346 | if (selectList_s == null) { |
| 347 | this.selectList = null; |
| 348 | } else { |
| 349 | this.selectList = selectList_s.split("\n"); |
| 350 | } |
| 351 | if (customTitle == null) { |
| 352 | title = LocaleController.getString(bindConfig.getKey()); |
| 353 | } else { |
| 354 | title = customTitle; |
| 355 | } |
| 356 | this.onClickCustom = customOnClick; |
| 357 | } |
| 358 | |
| 359 | public int getType() { |
| 360 | return ITEM_TYPE_TEXT_SETTINGS; |
| 361 | } |
| 362 | |
| 363 | public boolean isEnabled() { |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | public void onBindViewHolder(RecyclerView.ViewHolder holder) { |
| 368 | TextSettingsCell cell = (TextSettingsCell) holder.itemView; |
| 369 | cell.setTextAndValue(title, selectList == null ? "" : selectList[bindConfig.Int()], !(rows.get(rows.indexOf(this) + 1) instanceof NekomuraTGDivider)); |
| 370 | } |
| 371 | |
| 372 | public void onClick() { |
| 373 | if (onClickCustom != null) { |
| 374 | try { |
| 375 | onClickCustom.run(); |
| 376 | } catch (Exception e) { |
| 377 | } |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | Context context = getParentActivity(); |
| 382 | if (context == null) { |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 387 | builder.setTitle(LocaleController.getString(bindConfig.getKey())); |
| 388 | final LinearLayout linearLayout = new LinearLayout(context); |
| 389 | linearLayout.setOrientation(LinearLayout.VERTICAL); |
| 390 | builder.setView(linearLayout); |
| 391 | |
| 392 | for (int i = 0; i < selectList.length; i++) { |
| 393 | RadioColorCell cell = new RadioColorCell(context); |
| 394 | cell.setPadding(AndroidUtilities.dp(4), 0, AndroidUtilities.dp(4), 0); |
| 395 | cell.setTag(i); |
| 396 | cell.setCheckColor(Theme.getColor(Theme.key_radioBackground), Theme.getColor(Theme.key_dialogRadioBackgroundChecked)); |
| 397 | cell.setTextAndValue(selectList[i], bindConfig.Int() == i); |
| 398 | linearLayout.addView(cell); |
| 399 | cell.setOnClickListener(v -> { |
| 400 | Integer which = (Integer) v.getTag(); |
| 401 | bindConfig.setConfigInt(which); |
| 402 | listAdapter.notifyItemChanged(rows.indexOf(this)); |
| 403 | builder.getDismissRunnable().run(); |
| 404 | parentLayout.rebuildAllFragmentViews(false, false); |
| 405 | }); |
| 406 | } |
| 407 | builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); |
| 408 | showDialog(builder.create()); |
| 409 | } |
| 410 | } |
| 411 | } |