Skip to content

Commit

Permalink
feat: 'add to folder' menu entry in chat profile
Browse files Browse the repository at this point in the history
Thanks to @nekox_broken

Co-authored-by: dic1911 <d69873@gmail.com>
  • Loading branch information
omg-xtao and dic1911 committed Oct 23, 2024
1 parent 7fab993 commit b8f93a2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public interface FiltersListBottomSheetDelegate {
}

private final ArrayList<Long> selectedDialogs;
private final DialogsActivity fragment;
private final BaseFragment fragment;

public FiltersListBottomSheet(DialogsActivity baseFragment, ArrayList<Long> selectedDialogs) {
public FiltersListBottomSheet(BaseFragment baseFragment, ArrayList<Long> selectedDialogs) {
super(baseFragment.getParentActivity(), false);
this.selectedDialogs = selectedDialogs;
this.fragment = baseFragment;
Expand Down Expand Up @@ -330,6 +330,12 @@ public void setDelegate(FiltersListBottomSheetDelegate filtersListBottomSheetDel
delegate = filtersListBottomSheetDelegate;
}

public static ArrayList<MessagesController.DialogFilter> getCanAddDialogFilters(BaseFragment fragment, Long dialogId) {
var arrays = new ArrayList<Long>(1);
arrays.add(dialogId);
return getCanAddDialogFilters(fragment, arrays);
}

public static ArrayList<MessagesController.DialogFilter> getCanAddDialogFilters(BaseFragment fragment, ArrayList<Long> selectedDialogs) {
ArrayList<MessagesController.DialogFilter> result = new ArrayList<>();
ArrayList<MessagesController.DialogFilter> filters = fragment.getMessagesController().dialogFilters;
Expand Down
69 changes: 69 additions & 0 deletions TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}
}
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -14502,6 +14509,68 @@ private boolean editRow(View view, int position) {
return true;
}

private void showAddCurrentChatToFolderSheet() {
ArrayList<Long> selectedDialogs = new ArrayList<>(1);
selectedDialogs.add(getDialogId());
FiltersListBottomSheet sheet = new FiltersListBottomSheet(ProfileActivity.this, selectedDialogs);
sheet.setDelegate((filter, checked) -> {
ArrayList<Long> 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);
Expand Down

0 comments on commit b8f93a2

Please sign in to comment.