mirror of https://github.com/NekoX-Dev/NekoX.git
add chat filter
This commit is contained in:
parent
b2777eaca0
commit
a5d17f0bf9
|
@ -50,6 +50,8 @@ import java.util.concurrent.CountDownLatch;
|
|||
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
|
||||
import tw.nekomimi.nekogram.FilterPopup;
|
||||
|
||||
public class MessagesController implements NotificationCenter.NotificationCenterDelegate {
|
||||
|
||||
private ConcurrentHashMap<Integer, TLRPC.Chat> chats = new ConcurrentHashMap<>(100, 1.0f, 2);
|
||||
|
@ -845,6 +847,9 @@ public class MessagesController implements NotificationCenter.NotificationCenter
|
|||
dialogsUsersOnly.clear();
|
||||
dialogMessagesByIds.clear();
|
||||
dialogMessagesByRandomIds.clear();
|
||||
|
||||
FilterPopup.getInstance(currentAccount).cleanup();
|
||||
|
||||
channelAdmins.clear();
|
||||
loadingChannelAdmins.clear();
|
||||
users.clear();
|
||||
|
@ -2542,6 +2547,9 @@ public class MessagesController implements NotificationCenter.NotificationCenter
|
|||
dialogsGroupsOnly.remove(dialog);
|
||||
dialogsUsersOnly.remove(dialog);
|
||||
dialogsForward.remove(dialog);
|
||||
|
||||
FilterPopup.getInstance(currentAccount).remove(dialog);
|
||||
|
||||
dialogs_dict.remove(did);
|
||||
dialogs_read_inbox_max.remove(did);
|
||||
dialogs_read_outbox_max.remove(did);
|
||||
|
@ -10339,6 +10347,9 @@ public class MessagesController implements NotificationCenter.NotificationCenter
|
|||
dialogsGroupsOnly.remove(dialog);
|
||||
dialogsUsersOnly.remove(dialog);
|
||||
dialogsForward.remove(dialog);
|
||||
|
||||
FilterPopup.getInstance(currentAccount).remove(dialog);
|
||||
|
||||
dialogs_dict.remove(dialog.id);
|
||||
dialogs_read_inbox_max.remove(dialog.id);
|
||||
dialogs_read_outbox_max.remove(dialog.id);
|
||||
|
@ -10489,6 +10500,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
|
|||
}
|
||||
|
||||
public void sortDialogs(SparseArray<TLRPC.Chat> chatsDict) {
|
||||
FilterPopup.getInstance(currentAccount).cleanup();
|
||||
dialogsServerOnly.clear();
|
||||
dialogsCanAddUsers.clear();
|
||||
dialogsChannelsOnly.clear();
|
||||
|
@ -10517,6 +10529,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
|
|||
TLRPC.Dialog d = allDialogs.get(a);
|
||||
int high_id = (int) (d.id >> 32);
|
||||
int lower_id = (int) d.id;
|
||||
FilterPopup.getInstance(currentAccount).sortDialogs(d, high_id, lower_id);
|
||||
if (d instanceof TLRPC.TL_dialog) {
|
||||
boolean canAddToForward = true;
|
||||
if (lower_id != 0 && high_id != 1) {
|
||||
|
|
|
@ -85,6 +85,14 @@ public class DialogsAdapter extends RecyclerListView.SelectionAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
public int getDialogsType() {
|
||||
return dialogsType;
|
||||
}
|
||||
|
||||
public void setDialogsType(int type) {
|
||||
dialogsType = type;
|
||||
}
|
||||
|
||||
public void setOpenedDialogId(long id) {
|
||||
openedDialogId = id;
|
||||
}
|
||||
|
|
|
@ -133,6 +133,8 @@ import org.telegram.ui.Components.UndoView;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import tw.nekomimi.nekogram.FilterPopup;
|
||||
|
||||
public class DialogsActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
|
||||
|
||||
private RecyclerListView listView;
|
||||
|
@ -622,6 +624,14 @@ public class DialogsActivity extends BaseFragment implements NotificationCenter.
|
|||
super(args);
|
||||
}
|
||||
|
||||
public void updateDialogsType(int type) {
|
||||
dialogsType = type;
|
||||
if (dialogsAdapter != null) {
|
||||
dialogsAdapter.setDialogsType(type);
|
||||
dialogsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onFragmentCreate() {
|
||||
super.onFragmentCreate();
|
||||
|
@ -857,6 +867,14 @@ public class DialogsActivity extends BaseFragment implements NotificationCenter.
|
|||
}
|
||||
actionBar.setSupportsHolidayImage(true);
|
||||
}
|
||||
actionBar.setOnTouchListener((v, event) -> {
|
||||
int x = (int) event.getX();
|
||||
int y = (int) event.getY();
|
||||
if (event.getAction() == MotionEvent.ACTION_UP) {
|
||||
FilterPopup.getInstance(currentAccount).createMenu(this, actionBar, getParentActivity(), listView, fragmentView, x, y);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
actionBar.setTitleActionRunnable(() -> {
|
||||
hideFloatingButton(false);
|
||||
listView.smoothScrollToPosition(hasHiddenArchive() ? 1 : 0);
|
||||
|
@ -2983,8 +3001,9 @@ public class DialogsActivity extends BaseFragment implements NotificationCenter.
|
|||
return messagesController.dialogsChannelsOnly;
|
||||
} else if (dialogsType == 6) {
|
||||
return messagesController.dialogsGroupsOnly;
|
||||
} else {
|
||||
return FilterPopup.getInstance(currentAccount).getDialogs(dialogsType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setSideMenu(RecyclerView recyclerView) {
|
||||
|
|
|
@ -0,0 +1,260 @@
|
|||
package tw.nekomimi.nekogram;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffColorFilter;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.view.Gravity;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ScrollView;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.telegram.messenger.AndroidUtilities;
|
||||
import org.telegram.messenger.ChatObject;
|
||||
import org.telegram.messenger.DialogObject;
|
||||
import org.telegram.messenger.LocaleController;
|
||||
import org.telegram.messenger.MessagesController;
|
||||
import org.telegram.messenger.R;
|
||||
import org.telegram.messenger.UserConfig;
|
||||
import org.telegram.tgnet.TLRPC;
|
||||
import org.telegram.ui.ActionBar.ActionBar;
|
||||
import org.telegram.ui.ActionBar.ActionBarMenuSubItem;
|
||||
import org.telegram.ui.ActionBar.ActionBarPopupWindow;
|
||||
import org.telegram.ui.ActionBar.Theme;
|
||||
import org.telegram.ui.Components.LayoutHelper;
|
||||
import org.telegram.ui.DialogsActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FilterPopup {
|
||||
private static volatile FilterPopup[] Instance = new FilterPopup[UserConfig.MAX_ACCOUNT_COUNT];
|
||||
public ArrayList<TLRPC.Dialog> dialogsUsers = new ArrayList<>();
|
||||
public ArrayList<TLRPC.Dialog> dialogsGroups = new ArrayList<>();
|
||||
public ArrayList<TLRPC.Dialog> dialogsChannels = new ArrayList<>();
|
||||
public ArrayList<TLRPC.Dialog> dialogsBots = new ArrayList<>();
|
||||
public ArrayList<TLRPC.Dialog> dialogsAdmin = new ArrayList<>();
|
||||
private ActionBarPopupWindow scrimPopupWindow;
|
||||
private int currentAccount;
|
||||
|
||||
public FilterPopup(int num) {
|
||||
currentAccount = num;
|
||||
}
|
||||
|
||||
public static FilterPopup getInstance(int num) {
|
||||
FilterPopup localInstance = Instance[num];
|
||||
if (localInstance == null) {
|
||||
synchronized (MessagesController.class) {
|
||||
localInstance = Instance[num];
|
||||
if (localInstance == null) {
|
||||
Instance[num] = localInstance = new FilterPopup(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
return localInstance;
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
dialogsUsers.clear();
|
||||
dialogsGroups.clear();
|
||||
dialogsChannels.clear();
|
||||
dialogsBots.clear();
|
||||
dialogsAdmin.clear();
|
||||
}
|
||||
|
||||
public void remove(TLRPC.Dialog dialog) {
|
||||
dialogsUsers.remove(dialog);
|
||||
dialogsGroups.remove(dialog);
|
||||
dialogsChannels.remove(dialog);
|
||||
dialogsBots.remove(dialog);
|
||||
dialogsAdmin.remove(dialog);
|
||||
}
|
||||
|
||||
public void sortDialogs(TLRPC.Dialog dialog, int high_id, int lower_id) {
|
||||
if (lower_id != 0 && high_id != 1) {
|
||||
if (DialogObject.isChannel(dialog)) {
|
||||
TLRPC.Chat chat = MessagesController.getInstance(currentAccount).getChat(-lower_id);
|
||||
if (chat != null) {
|
||||
if (chat.megagroup) {
|
||||
dialogsGroups.add(dialog);
|
||||
} else {
|
||||
dialogsChannels.add(dialog);
|
||||
}
|
||||
}
|
||||
if (chat != null && (chat.creator || ChatObject.hasAdminRights(chat)))
|
||||
dialogsAdmin.add(dialog);
|
||||
} else if (lower_id < 0) {
|
||||
dialogsGroups.add(dialog);
|
||||
} else {
|
||||
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser((int) dialog.id);
|
||||
if (user != null) {
|
||||
if (user.bot)
|
||||
dialogsBots.add(dialog);
|
||||
else
|
||||
dialogsUsers.add(dialog);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance(currentAccount).getEncryptedChat(high_id);
|
||||
if (encryptedChat != null)
|
||||
dialogsUsers.add(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<TLRPC.Dialog> getDialogs(int type) {
|
||||
switch (type) {
|
||||
case DialogType.Users:
|
||||
return dialogsUsers;
|
||||
case DialogType.Groups:
|
||||
return dialogsGroups;
|
||||
case DialogType.Channels:
|
||||
return dialogsChannels;
|
||||
case DialogType.Bots:
|
||||
return dialogsBots;
|
||||
case DialogType.Admin:
|
||||
return dialogsAdmin;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void createMenu(DialogsActivity dialogsActivity, ActionBar actionBar, Activity parentActivity, RecyclerView listView, View fragmentView, int x, int y) {
|
||||
if (actionBar.isActionModeShowed()) {
|
||||
return;
|
||||
}
|
||||
ArrayList<CharSequence> items = new ArrayList<>();
|
||||
final ArrayList<Integer> options = new ArrayList<>();
|
||||
|
||||
items.add(LocaleController.getString("All", R.string.All));
|
||||
options.add(DialogType.All);
|
||||
items.add(LocaleController.getString("User", R.string.User));
|
||||
options.add(DialogType.Users);
|
||||
items.add(LocaleController.getString("Group", R.string.Group));
|
||||
options.add(DialogType.Groups);
|
||||
items.add(LocaleController.getString("Channel", R.string.Channel));
|
||||
options.add(DialogType.Channels);
|
||||
items.add(LocaleController.getString("Bot", R.string.Bot));
|
||||
options.add(DialogType.Bots);
|
||||
items.add(LocaleController.getString("Admin", R.string.Admin));
|
||||
options.add(DialogType.Admin);
|
||||
|
||||
if (scrimPopupWindow != null) {
|
||||
scrimPopupWindow.dismiss();
|
||||
scrimPopupWindow = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Rect rect = new Rect();
|
||||
|
||||
ActionBarPopupWindow.ActionBarPopupWindowLayout popupLayout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(parentActivity);
|
||||
popupLayout.setOnTouchListener((view, event) -> {
|
||||
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
||||
if (scrimPopupWindow != null && scrimPopupWindow.isShowing()) {
|
||||
actionBar.getHitRect(rect);
|
||||
if (!rect.contains((int) event.getX(), (int) event.getY())) {
|
||||
scrimPopupWindow.dismiss();
|
||||
}
|
||||
}
|
||||
} else if (event.getActionMasked() == MotionEvent.ACTION_OUTSIDE) {
|
||||
if (scrimPopupWindow != null && scrimPopupWindow.isShowing()) {
|
||||
scrimPopupWindow.dismiss();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
popupLayout.setDispatchKeyEventListener(keyEvent -> {
|
||||
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0 && scrimPopupWindow != null && scrimPopupWindow.isShowing()) {
|
||||
scrimPopupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
Rect backgroundPaddings = new Rect();
|
||||
Drawable shadowDrawable = parentActivity.getResources().getDrawable(R.drawable.popup_fixed_alert).mutate();
|
||||
shadowDrawable.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_dialogBackground), PorterDuff.Mode.MULTIPLY));
|
||||
shadowDrawable.getPadding(backgroundPaddings);
|
||||
popupLayout.setBackgroundDrawable(shadowDrawable);
|
||||
|
||||
LinearLayout linearLayout = new LinearLayout(parentActivity);
|
||||
ScrollView scrollView;
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
scrollView = new ScrollView(parentActivity, null, 0, R.style.scrollbarShapeStyle) {
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
setMeasuredDimension(linearLayout.getMeasuredWidth(), getMeasuredHeight());
|
||||
}
|
||||
};
|
||||
} else {
|
||||
scrollView = new ScrollView(parentActivity);
|
||||
}
|
||||
scrollView.setClipToPadding(false);
|
||||
popupLayout.addView(scrollView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT));
|
||||
|
||||
linearLayout.setMinimumWidth(AndroidUtilities.dp(200));
|
||||
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
||||
for (int a = 0, N = items.size(); a < N; a++) {
|
||||
ActionBarMenuSubItem cell = new ActionBarMenuSubItem(parentActivity);
|
||||
cell.setText(items.get(a).toString());
|
||||
linearLayout.addView(cell);
|
||||
final int i = a;
|
||||
cell.setOnClickListener(v1 -> {
|
||||
dialogsActivity.updateDialogsType(options.get(i));
|
||||
if (scrimPopupWindow != null) {
|
||||
scrimPopupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
}
|
||||
scrollView.addView(linearLayout, LayoutHelper.createScroll(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP));
|
||||
scrimPopupWindow = new ActionBarPopupWindow(popupLayout, LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT) {
|
||||
@Override
|
||||
public void dismiss() {
|
||||
super.dismiss();
|
||||
if (scrimPopupWindow != this) {
|
||||
return;
|
||||
}
|
||||
scrimPopupWindow = null;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
parentActivity.getWindow().getDecorView().setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
|
||||
}
|
||||
}
|
||||
};
|
||||
scrimPopupWindow.setDismissAnimationDuration(220);
|
||||
scrimPopupWindow.setOutsideTouchable(true);
|
||||
scrimPopupWindow.setClippingEnabled(true);
|
||||
scrimPopupWindow.setAnimationStyle(R.style.PopupContextAnimation);
|
||||
scrimPopupWindow.setFocusable(true);
|
||||
popupLayout.measure(View.MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), View.MeasureSpec.AT_MOST));
|
||||
scrimPopupWindow.setInputMethodMode(ActionBarPopupWindow.INPUT_METHOD_NOT_NEEDED);
|
||||
scrimPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
|
||||
scrimPopupWindow.getContentView().setFocusableInTouchMode(true);
|
||||
int popupX = actionBar.getLeft() + x - popupLayout.getMeasuredWidth() + backgroundPaddings.left - AndroidUtilities.dp(28);
|
||||
if (popupX < AndroidUtilities.dp(6)) {
|
||||
popupX = AndroidUtilities.dp(6);
|
||||
} else if (popupX > listView.getMeasuredWidth() - AndroidUtilities.dp(6) - popupLayout.getMeasuredWidth()) {
|
||||
popupX = listView.getMeasuredWidth() - AndroidUtilities.dp(6) - popupLayout.getMeasuredWidth();
|
||||
}
|
||||
if (AndroidUtilities.isTablet()) {
|
||||
int[] location = new int[2];
|
||||
fragmentView.getLocationInWindow(location);
|
||||
popupX += location[0];
|
||||
}
|
||||
int popupY;
|
||||
popupY = actionBar.getTop() + y;
|
||||
scrimPopupWindow.showAtLocation(actionBar, Gravity.LEFT | Gravity.TOP, popupX, popupY);
|
||||
|
||||
}
|
||||
|
||||
public static class DialogType {
|
||||
public static final int All = 0;
|
||||
public static final int Users = 7;
|
||||
public static final int Groups = 8;
|
||||
public static final int Channels = 9;
|
||||
public static final int Bots = 10;
|
||||
public static final int Admin = 11;
|
||||
}
|
||||
}
|
|
@ -3,7 +3,6 @@ package tw.nekomimi.nekogram;
|
|||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
|
|
@ -16,4 +16,9 @@
|
|||
<string name="Repeat">Repeat</string>
|
||||
<string name="CreateMention">Create Mention</string>
|
||||
<string name="ForceTabletMode">Force tablet mode</string>
|
||||
<string name="All">All</string>
|
||||
<string name="User">User</string>
|
||||
<string name="Group">Group</string>
|
||||
<string name="Channel">Channel</string>
|
||||
<string name="Admin">Admin</string>
|
||||
</resources>
|
Loading…
Reference in New Issue