From b8f93a2fa0f2d00abd647c4e0f0716db072bb3d6 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Wed, 23 Oct 2024 23:19:54 +0800 Subject: [PATCH] feat: 'add to folder' menu entry in chat profile Thanks to @nekox_broken Co-authored-by: dic1911 --- .../ui/Components/FiltersListBottomSheet.java | 10 ++- .../java/org/telegram/ui/ProfileActivity.java | 69 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/FiltersListBottomSheet.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/FiltersListBottomSheet.java index 0ff2685f4a..160aa07e25 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Components/FiltersListBottomSheet.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/FiltersListBottomSheet.java @@ -61,9 +61,9 @@ public interface FiltersListBottomSheetDelegate { } private final ArrayList selectedDialogs; - private final DialogsActivity fragment; + private final BaseFragment fragment; - public FiltersListBottomSheet(DialogsActivity baseFragment, ArrayList selectedDialogs) { + public FiltersListBottomSheet(BaseFragment baseFragment, ArrayList selectedDialogs) { super(baseFragment.getParentActivity(), false); this.selectedDialogs = selectedDialogs; this.fragment = baseFragment; @@ -330,6 +330,12 @@ public void setDelegate(FiltersListBottomSheetDelegate filtersListBottomSheetDel delegate = filtersListBottomSheetDelegate; } + public static ArrayList getCanAddDialogFilters(BaseFragment fragment, Long dialogId) { + var arrays = new ArrayList(1); + arrays.add(dialogId); + return getCanAddDialogFilters(fragment, arrays); + } + public static ArrayList getCanAddDialogFilters(BaseFragment fragment, ArrayList selectedDialogs) { ArrayList result = new ArrayList<>(); ArrayList filters = fragment.getMessagesController().dialogFilters; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java index b2da2e990d..cc4dae9c36 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java @@ -230,6 +230,7 @@ import org.telegram.ui.Components.EditTextBoldCursor; import org.telegram.ui.Components.EmojiPacksAlert; import org.telegram.ui.Components.EmptyStubSpan; +import org.telegram.ui.Components.FiltersListBottomSheet; import org.telegram.ui.Components.FloatingDebug.FloatingDebugController; import org.telegram.ui.Components.Forum.ForumUtilities; import org.telegram.ui.Components.FragmentContextView; @@ -580,6 +581,7 @@ public void setAlpha(int a) { private final static int event_log = 102; private final static int message_filter = 103; private final static int clear_cache = 104; + private final static int add_to_folder = 105; private Rect rect = new Rect(); @@ -2803,6 +2805,8 @@ public void didChangeOwner(TLRPC.User user) { args.putLong("dialog_id", userId != 0 ? dialogId : -chatId); CacheControlActivity fragment = new CacheControlActivity(args); presentFragment(fragment); + } else if (id == add_to_folder) { + showAddCurrentChatToFolderSheet(); } } }); @@ -10701,6 +10705,9 @@ private void createActionBarMenu(boolean animated) { if (selfUser && !myProfile) { otherItem.addSubItem(logout, R.drawable.msg_leave, LocaleController.getString(R.string.LogOut)); } else { + if (getDialogId() != 0 && !FiltersListBottomSheet.getCanAddDialogFilters(this, getDialogId()).isEmpty()) { + otherItem.addSubItem(add_to_folder, R.drawable.msg_folders, LocaleController.getString(R.string.FilterAddTo)); + } otherItem.addSubItem(clear_cache, R.drawable.msg_delete, LocaleController.getString(R.string.ClearCache)); } if (!isPulledDown) { @@ -14502,6 +14509,68 @@ private boolean editRow(View view, int position) { return true; } + private void showAddCurrentChatToFolderSheet() { + ArrayList selectedDialogs = new ArrayList<>(1); + selectedDialogs.add(getDialogId()); + FiltersListBottomSheet sheet = new FiltersListBottomSheet(ProfileActivity.this, selectedDialogs); + sheet.setDelegate((filter, checked) -> { + ArrayList alwaysShow = FiltersListBottomSheet.getDialogsCount(ProfileActivity.this, filter, selectedDialogs, true, false); + if (!checked) { + int currentCount; + if (filter != null) { + currentCount = filter.alwaysShow.size(); + } else { + currentCount = 0; + } + int totalCount = currentCount + alwaysShow.size(); + if ((totalCount > getMessagesController().dialogFiltersChatsLimitDefault && !getUserConfig().isPremium()) || totalCount > getMessagesController().dialogFiltersChatsLimitPremium) { + showDialog(new LimitReachedBottomSheet(ProfileActivity.this, fragmentView.getContext(), LimitReachedBottomSheet.TYPE_CHATS_IN_FOLDER, currentAccount, null)); + return; + } + } + if (filter != null) { + if (checked) { + for (int a = 0; a < selectedDialogs.size(); a++) { + filter.neverShow.add(selectedDialogs.get(a)); + filter.alwaysShow.remove(selectedDialogs.get(a)); + } + FilterCreateActivity.saveFilterToServer(filter, filter.flags, filter.emoticon, filter.name, filter.color, filter.alwaysShow, filter.neverShow, filter.pinnedDialogs, false, false, true, true, false, ProfileActivity.this, null); + long did; + if (selectedDialogs.size() == 1) { + did = selectedDialogs.get(0); + } else { + did = 0; + } + final UndoView undoView = getUndoView(); + if (undoView != null) { + undoView.showWithAction(did, UndoView.ACTION_REMOVED_FROM_FOLDER, selectedDialogs.size(), filter, null, null); + } + } else { + if (!alwaysShow.isEmpty()) { + for (int a = 0; a < alwaysShow.size(); a++) { + filter.neverShow.remove(alwaysShow.get(a)); + } + filter.alwaysShow.addAll(alwaysShow); + FilterCreateActivity.saveFilterToServer(filter, filter.flags, filter.emoticon, filter.name, filter.color, filter.alwaysShow, filter.neverShow, filter.pinnedDialogs, false, false, true, true, false, ProfileActivity.this, null); + } + long did; + if (alwaysShow.size() == 1) { + did = alwaysShow.get(0); + } else { + did = 0; + } + final UndoView undoView = getUndoView(); + if (undoView != null) { + undoView.showWithAction(did, UndoView.ACTION_ADDED_TO_FOLDER, alwaysShow.size(), filter, null, null); + } + } + } else { + presentFragment(new FilterCreateActivity(null, alwaysShow)); + } + }); + showDialog(sheet); + } + private void updateItemsUsername() { if (!myProfile) return; TLRPC.User user = getMessagesController().getUser(userId);