NekoX/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java

376 lines
14 KiB
Java
Raw Normal View History

/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
package org.telegram.ui.Adapters;
import android.content.Context;
2020-12-23 08:48:30 +01:00
import android.content.pm.PackageManager;
import android.view.View;
import android.view.ViewGroup;
2020-06-25 17:28:24 +02:00
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2020-12-23 08:48:30 +01:00
import org.telegram.messenger.ApplicationLoader;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
2020-06-25 17:28:24 +02:00
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
2020-06-25 17:28:24 +02:00
import org.telegram.messenger.SharedConfig;
import org.telegram.messenger.UserConfig;
import org.telegram.tgnet.TLRPC;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2014-11-10 12:05:22 +01:00
import org.telegram.ui.Cells.DividerCell;
2020-06-25 17:28:24 +02:00
import org.telegram.ui.Cells.DrawerActionCell;
import org.telegram.ui.Cells.DrawerActionCheckCell;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.Cells.DrawerAddCell;
2020-06-25 17:28:24 +02:00
import org.telegram.ui.Cells.DrawerProfileCell;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.Cells.DrawerUserCell;
2014-11-10 12:05:22 +01:00
import org.telegram.ui.Cells.EmptyCell;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.Components.RecyclerListView;
2020-03-30 14:00:09 +02:00
import org.telegram.ui.Components.SideMenultItemAnimator;
2017-03-31 01:58:05 +02:00
import java.util.ArrayList;
2018-07-30 04:07:02 +02:00
import java.util.Collections;
2017-03-31 01:58:05 +02:00
import cn.hutool.core.util.StrUtil;
2020-06-25 17:28:24 +02:00
import kotlin.jvm.functions.Function0;
import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.NekoXConfig;
2019-05-14 14:08:05 +02:00
2020-06-25 17:28:24 +02:00
public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter implements NotificationCenter.NotificationCenterDelegate {
private Context mContext;
2020-06-25 17:28:24 +02:00
public ArrayList<Item> items = new ArrayList<>(11);
2018-07-30 04:07:02 +02:00
private ArrayList<Integer> accountNumbers = new ArrayList<>();
2020-03-30 14:00:09 +02:00
private boolean accountsShown;
2018-07-30 04:07:02 +02:00
private DrawerProfileCell profileCell;
2020-03-30 14:00:09 +02:00
private SideMenultItemAnimator itemAnimator;
2020-12-23 08:48:30 +01:00
private boolean hasGps;
2020-03-30 14:00:09 +02:00
public DrawerLayoutAdapter(Context context, SideMenultItemAnimator animator) {
mContext = context;
2019-12-31 14:08:08 +01:00
itemAnimator = animator;
accountsShown = MessagesController.getGlobalMainSettings().getBoolean("accountsShown", true);
2021-07-15 16:24:57 +02:00
Theme.createCommonDialogResources(context);
2017-03-31 01:58:05 +02:00
resetItems();
2020-12-23 08:48:30 +01:00
try {
hasGps = ApplicationLoader.applicationContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
} catch (Throwable e) {
hasGps = false;
}
}
2018-07-30 04:07:02 +02:00
private int getAccountRowsCount() {
2021-03-12 13:37:39 +01:00
return accountNumbers.size() + 2;
2018-07-30 04:07:02 +02:00
}
@Override
2017-03-31 01:58:05 +02:00
public int getItemCount() {
2018-07-30 04:07:02 +02:00
int count = items.size() + 2;
2020-03-30 14:00:09 +02:00
if (accountsShown) {
2018-07-30 04:07:02 +02:00
count += getAccountRowsCount();
}
return count;
}
2020-03-30 14:00:09 +02:00
public void setAccountsShown(boolean value, boolean animated) {
if (accountsShown == value || itemAnimator.isRunning()) {
2018-07-30 04:07:02 +02:00
return;
}
2020-03-30 14:00:09 +02:00
accountsShown = value;
MessagesController.getGlobalMainSettings().edit().putBoolean("accountsShown", accountsShown).apply();
2018-07-30 04:07:02 +02:00
if (profileCell != null) {
2020-03-30 14:00:09 +02:00
profileCell.setAccountsShown(accountsShown, animated);
2018-07-30 04:07:02 +02:00
}
2020-06-25 17:28:24 +02:00
MessagesController.getGlobalMainSettings().edit().putBoolean("accountsShown", accountsShown).apply();
2018-07-30 04:07:02 +02:00
if (animated) {
2020-03-30 14:00:09 +02:00
itemAnimator.setShouldClipChildren(false);
if (accountsShown) {
2018-07-30 04:07:02 +02:00
notifyItemRangeInserted(2, getAccountRowsCount());
} else {
notifyItemRangeRemoved(2, getAccountRowsCount());
}
} else {
notifyDataSetChanged();
}
}
2020-03-30 14:00:09 +02:00
public boolean isAccountsShown() {
return accountsShown;
}
@Override
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.updateUserStatus) {
if (args[0] != null) {
TLRPC.TL_updateUserStatus update = (TLRPC.TL_updateUserStatus) args[0];
2021-09-20 11:14:20 +02:00
long selectedUserId = UserConfig.getInstance(UserConfig.selectedAccount).getClientUserId();
if (update.user_id != selectedUserId) {
return;
}
}
}
2020-06-25 17:28:24 +02:00
notifyDataSetChanged();
}
@Override
2017-03-31 01:58:05 +02:00
public void notifyDataSetChanged() {
resetItems();
super.notifyDataSetChanged();
}
@Override
2017-03-31 01:58:05 +02:00
public boolean isEnabled(RecyclerView.ViewHolder holder) {
2018-07-30 04:07:02 +02:00
int itemType = holder.getItemViewType();
2020-03-30 14:00:09 +02:00
return itemType == 3 || itemType == 4 || itemType == 5 || itemType == 6;
}
@Override
2017-03-31 01:58:05 +02:00
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 0:
2019-12-31 14:08:08 +01:00
view = profileCell = new DrawerProfileCell(mContext);
2017-03-31 01:58:05 +02:00
break;
case 2:
2014-11-10 12:05:22 +01:00
view = new DividerCell(mContext);
2017-03-31 01:58:05 +02:00
break;
case 3:
view = new DrawerActionCell(mContext);
2017-03-31 01:58:05 +02:00
break;
2020-06-25 17:28:24 +02:00
case 6:
view = new DrawerActionCheckCell(mContext);
break;
2018-07-30 04:07:02 +02:00
case 4:
view = new DrawerUserCell(mContext);
break;
case 5:
view = new DrawerAddCell(mContext);
break;
2019-12-31 14:08:08 +01:00
case 1:
default:
view = new EmptyCell(mContext, AndroidUtilities.dp(8));
break;
}
2017-03-31 01:58:05 +02:00
view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return new RecyclerListView.Holder(view);
}
2017-03-31 01:58:05 +02:00
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
2018-07-30 04:07:02 +02:00
case 0: {
2019-06-04 12:14:50 +02:00
DrawerProfileCell profileCell = (DrawerProfileCell) holder.itemView;
2020-03-30 14:00:09 +02:00
profileCell.setUser(MessagesController.getInstance(UserConfig.selectedAccount).getUser(UserConfig.getInstance(UserConfig.selectedAccount).getClientUserId()), accountsShown);
2017-03-31 01:58:05 +02:00
break;
2018-07-30 04:07:02 +02:00
}
case 3: {
2020-03-30 14:00:09 +02:00
DrawerActionCell drawerActionCell = (DrawerActionCell) holder.itemView;
2018-07-30 04:07:02 +02:00
position -= 2;
2020-03-30 14:00:09 +02:00
if (accountsShown) {
2018-07-30 04:07:02 +02:00
position -= getAccountRowsCount();
}
items.get(position).bind(drawerActionCell);
drawerActionCell.setPadding(0, 0, 0, 0);
break;
}
2020-06-25 17:28:24 +02:00
case 6: {
DrawerActionCheckCell drawerActionCell = (DrawerActionCheckCell) holder.itemView;
position -= 2;
if (accountsShown) {
position -= getAccountRowsCount();
}
((CheckItem) items.get(position)).bindCheck(drawerActionCell);
drawerActionCell.setPadding(0, 0, 0, 0);
break;
}
2018-07-30 04:07:02 +02:00
case 4: {
DrawerUserCell drawerUserCell = (DrawerUserCell) holder.itemView;
2020-06-25 17:28:24 +02:00
drawerUserCell.invalidate();
2018-07-30 04:07:02 +02:00
drawerUserCell.setAccount(accountNumbers.get(position - 2));
2017-03-31 01:58:05 +02:00
break;
2018-07-30 04:07:02 +02:00
}
2017-03-31 01:58:05 +02:00
}
}
@Override
public int getItemViewType(int i) {
if (i == 0) {
return 0;
2014-11-10 12:05:22 +01:00
} else if (i == 1) {
return 1;
2018-07-30 04:07:02 +02:00
}
i -= 2;
2020-03-30 14:00:09 +02:00
if (accountsShown) {
2018-07-30 04:07:02 +02:00
if (i < accountNumbers.size()) {
return 4;
} else {
2021-03-12 13:37:39 +01:00
if (i == accountNumbers.size()) {
return 5;
} else if (i == accountNumbers.size() + 1) {
return 2;
2018-07-30 04:07:02 +02:00
}
}
i -= getAccountRowsCount();
}
2019-12-31 14:08:08 +01:00
if (items.get(i) == null) {
return 2;
}
2020-06-25 17:28:24 +02:00
return items.get(i) instanceof CheckItem ? 6 : 3;
}
2020-09-30 15:48:47 +02:00
public void swapElements(int fromIndex, int toIndex) {
int idx1 = fromIndex - 2;
int idx2 = toIndex - 2;
if (idx1 < 0 || idx2 < 0 || idx1 >= accountNumbers.size() || idx2 >= accountNumbers.size()) {
return;
}
final UserConfig userConfig1 = UserConfig.getInstance(accountNumbers.get(idx1));
final UserConfig userConfig2 = UserConfig.getInstance(accountNumbers.get(idx2));
final int tempLoginTime = userConfig1.loginTime;
userConfig1.loginTime = userConfig2.loginTime;
userConfig2.loginTime = tempLoginTime;
userConfig1.saveConfig(false);
userConfig2.saveConfig(false);
Collections.swap(accountNumbers, idx1, idx2);
notifyItemMoved(fromIndex, toIndex);
}
2017-03-31 01:58:05 +02:00
private void resetItems() {
2018-07-30 04:07:02 +02:00
accountNumbers.clear();
2021-03-12 13:37:39 +01:00
for (int a : SharedConfig.activeAccounts) {
2018-07-30 04:07:02 +02:00
if (UserConfig.getInstance(a).isClientActivated()) {
accountNumbers.add(a);
}
}
2018-08-27 10:33:11 +02:00
Collections.sort(accountNumbers, (o1, o2) -> {
long l1 = UserConfig.getInstance(o1).loginTime;
long l2 = UserConfig.getInstance(o2).loginTime;
2018-08-27 10:33:11 +02:00
if (l1 > l2) {
return 1;
} else if (l1 < l2) {
return -1;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
return 0;
2018-07-30 04:07:02 +02:00
});
2017-03-31 01:58:05 +02:00
items.clear();
2018-07-30 04:07:02 +02:00
if (!UserConfig.getInstance(UserConfig.selectedAccount).isClientActivated()) {
2017-03-31 01:58:05 +02:00
return;
}
2020-06-25 17:28:24 +02:00
int contactsIcon = R.drawable.baseline_perm_contact_calendar_24;
int savedIcon = R.drawable.baseline_bookmark_24;
int settingsIcon = R.drawable.baseline_settings_24;
2021-01-31 18:28:28 +01:00
int callsIcon = R.drawable.baseline_call_24;
2020-03-30 14:00:09 +02:00
items.add(new Item(6, LocaleController.getString("Contacts", R.string.Contacts), contactsIcon));
items.add(new Item(11, LocaleController.getString("SavedMessages", R.string.SavedMessages), savedIcon));
items.add(new Item(8, LocaleController.getString("Settings", R.string.Settings), settingsIcon));
2021-01-31 18:28:28 +01:00
items.add(new Item(10, LocaleController.getString("Calls", R.string.Calls), callsIcon));
2020-06-25 17:28:24 +02:00
if (NekoConfig.useProxyItem && (!NekoConfig.hideProxyByDefault || SharedConfig.proxyEnabled)) {
items.add(new CheckItem(13, LocaleController.getString("Proxy", R.string.Proxy), R.drawable.baseline_security_24, () -> SharedConfig.proxyEnabled, () -> {
SharedConfig.setProxyEnable(!SharedConfig.proxyEnabled);
return true;
}));
}
2021-04-11 02:53:15 +02:00
if (NekoXConfig.disableStatusUpdate && !UserConfig.getInstance(UserConfig.selectedAccount).getCurrentUser().bot) {
boolean online = MessagesController.getInstance(UserConfig.selectedAccount).isOnline();
String message = online ? StrUtil.upperFirst(LocaleController.getString("Online", R.string.Online)) : LocaleController.getString("VoipOfflineTitle", R.string.VoipOfflineTitle);
if (NekoXConfig.keepOnlineStatus) {
message += " (" + LocaleController.getString("Locked", R.string.Locked) + ")";
}
items.add(new CheckItem(14, message, R.drawable.baseline_visibility_24, () -> online, () -> {
MessagesController controller = MessagesController.getInstance(UserConfig.selectedAccount);
controller.updateStatus(!online);
return true;
}));
}
2020-06-25 17:28:24 +02:00
items.add(null); // divider
items.add(new CheckItem(12, LocaleController.getString("DarkMode", R.string.NightMode), R.drawable.baseline_brightness_2_24, () -> Theme.getActiveTheme().isDark(), null));
}
2017-03-31 01:58:05 +02:00
public int getId(int position) {
2018-07-30 04:07:02 +02:00
position -= 2;
2020-03-30 14:00:09 +02:00
if (accountsShown) {
2018-07-30 04:07:02 +02:00
position -= getAccountRowsCount();
}
2017-03-31 01:58:05 +02:00
if (position < 0 || position >= items.size()) {
return -1;
}
Item item = items.get(position);
return item != null ? item.id : -1;
}
2020-09-30 15:48:47 +02:00
public int getFirstAccountPosition() {
if (!accountsShown) {
return RecyclerView.NO_POSITION;
}
return 2;
}
public int getLastAccountPosition() {
if (!accountsShown) {
return RecyclerView.NO_POSITION;
}
return 1 + accountNumbers.size();
}
2020-06-25 17:28:24 +02:00
public CheckItem getItem(int position) {
position -= 2;
if (accountsShown) {
position -= getAccountRowsCount();
}
if (position < 0 || position >= items.size()) {
return null;
}
Item item = items.get(position);
return item instanceof CheckItem ? (CheckItem) item : null;
}
2020-07-26 10:03:38 +02:00
private static class Item {
2017-03-31 01:58:05 +02:00
public int icon;
public String text;
public int id;
public Item(int id, String text, int icon) {
this.icon = icon;
this.id = id;
this.text = text;
}
public void bind(DrawerActionCell actionCell) {
2021-07-15 16:24:57 +02:00
actionCell.setTextAndIcon(id, text, icon);
2017-03-31 01:58:05 +02:00
}
}
2019-06-19 13:48:50 +02:00
2020-06-25 17:28:24 +02:00
public class CheckItem extends Item {
public Function0<Boolean> isChecked;
public Function0<Boolean> doSwitch;
public CheckItem(int id, String text, int icon, Function0<Boolean> isChecked, @Nullable Function0<Boolean> doSwitch) {
2020-06-25 17:28:24 +02:00
super(id, text, icon);
this.isChecked = isChecked;
this.doSwitch = doSwitch;
}
public void bindCheck(DrawerActionCheckCell actionCell) {
actionCell.setTextAndValueAndCheck(text, icon, null, isChecked.invoke(), false, false);
if (doSwitch != null) {
actionCell.setOnCheckClickListener((v) -> {
if (doSwitch.invoke()) {
actionCell.setChecked(isChecked.invoke());
}
});
}
}
}
}