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

336 lines
12 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;
import android.view.View;
import android.view.ViewGroup;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.R;
import org.telegram.messenger.UserConfig;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Cells.DrawerActionCell;
2014-11-10 12:05:22 +01:00
import org.telegram.ui.Cells.DividerCell;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.Cells.DrawerAddCell;
import org.telegram.ui.Cells.DrawerUserCell;
2014-11-10 12:05:22 +01:00
import org.telegram.ui.Cells.EmptyCell;
import org.telegram.ui.Cells.DrawerProfileCell;
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
2019-05-14 14:08:05 +02:00
import androidx.recyclerview.widget.RecyclerView;
2017-03-31 01:58:05 +02:00
public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter {
private Context mContext;
2017-03-31 01:58:05 +02:00
private 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-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;
2020-03-30 14:00:09 +02:00
accountsShown = UserConfig.getActivatedAccountsCount() > 1 && MessagesController.getGlobalMainSettings().getBoolean("accountsShown", true);
2017-03-31 01:58:05 +02:00
Theme.createDialogsResources(context);
resetItems();
}
2018-07-30 04:07:02 +02:00
private int getAccountRowsCount() {
int count = accountNumbers.size() + 1;
if (accountNumbers.size() < UserConfig.MAX_ACCOUNT_COUNT) {
count++;
}
return count;
}
@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;
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-03-30 14:00:09 +02:00
MessagesController.getGlobalMainSettings().edit().putBoolean("accountsShown", accountsShown).commit();
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
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;
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;
}
case 4: {
DrawerUserCell drawerUserCell = (DrawerUserCell) holder.itemView;
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 {
if (accountNumbers.size() < UserConfig.MAX_ACCOUNT_COUNT) {
if (i == accountNumbers.size()){
return 5;
} else if (i == accountNumbers.size() + 1) {
return 2;
}
} else {
if (i == accountNumbers.size()) {
return 2;
}
}
}
i -= getAccountRowsCount();
}
2019-12-31 14:08:08 +01:00
if (items.get(i) == null) {
return 2;
}
return 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();
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
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;
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;
}
2019-01-23 18:03:33 +01:00
int eventType = Theme.getEventType();
2020-03-30 14:00:09 +02:00
int newGroupIcon;
int newSecretIcon;
int newChannelIcon;
int contactsIcon;
int callsIcon;
int savedIcon;
int settingsIcon;
int inviteIcon;
int helpIcon;
2019-01-23 18:03:33 +01:00
if (eventType == 0) {
2020-03-30 14:00:09 +02:00
newGroupIcon = R.drawable.menu_groups_ny;
newSecretIcon = R.drawable.menu_secret_ny;
newChannelIcon = R.drawable.menu_channel_ny;
contactsIcon = R.drawable.menu_contacts_ny;
callsIcon = R.drawable.menu_calls_ny;
savedIcon = R.drawable.menu_bookmarks_ny;
settingsIcon = R.drawable.menu_settings_ny;
inviteIcon = R.drawable.menu_invite_ny;
helpIcon = R.drawable.menu_help_ny;
2020-02-13 19:26:53 +01:00
} else if (eventType == 1) {
2020-03-30 14:00:09 +02:00
newGroupIcon = R.drawable.menu_groups_14;
newSecretIcon = R.drawable.menu_secret_14;
newChannelIcon = R.drawable.menu_broadcast_14;
contactsIcon = R.drawable.menu_contacts_14;
callsIcon = R.drawable.menu_calls_14;
savedIcon = R.drawable.menu_bookmarks_14;
settingsIcon = R.drawable.menu_settings_14;
inviteIcon = R.drawable.menu_secret_ny;
helpIcon = R.drawable.menu_help;
2020-10-30 11:26:29 +01:00
} else if (eventType == 2) {
newGroupIcon = R.drawable.menu_groups_hw;
newSecretIcon = R.drawable.menu_secret_hw;
newChannelIcon = R.drawable.menu_broadcast_hw;
contactsIcon = R.drawable.menu_contacts_hw;
callsIcon = R.drawable.menu_calls_hw;
savedIcon = R.drawable.menu_bookmarks_hw;
settingsIcon = R.drawable.menu_settings_hw;
inviteIcon = R.drawable.menu_invite_hw;
helpIcon = R.drawable.menu_help_hw;
2019-01-23 18:03:33 +01:00
} else {
2020-03-30 14:00:09 +02:00
newGroupIcon = R.drawable.menu_groups;
newSecretIcon = R.drawable.menu_secret;
newChannelIcon = R.drawable.menu_broadcast;
contactsIcon = R.drawable.menu_contacts;
callsIcon = R.drawable.menu_calls;
savedIcon = R.drawable.menu_saved;
settingsIcon = R.drawable.menu_settings;
inviteIcon = R.drawable.menu_invite;
helpIcon = R.drawable.menu_help;
2019-01-23 18:03:33 +01:00
}
2020-03-30 14:00:09 +02:00
items.add(new Item(2, LocaleController.getString("NewGroup", R.string.NewGroup), newGroupIcon));
items.add(new Item(3, LocaleController.getString("NewSecretChat", R.string.NewSecretChat), newSecretIcon));
items.add(new Item(4, LocaleController.getString("NewChannel", R.string.NewChannel), newChannelIcon));
items.add(new Item(6, LocaleController.getString("Contacts", R.string.Contacts), contactsIcon));
items.add(new Item(10, LocaleController.getString("Calls", R.string.Calls), callsIcon));
items.add(new Item(11, LocaleController.getString("SavedMessages", R.string.SavedMessages), savedIcon));
items.add(new Item(8, LocaleController.getString("Settings", R.string.Settings), settingsIcon));
items.add(null); // divider
items.add(new Item(7, LocaleController.getString("InviteFriends", R.string.InviteFriends), inviteIcon));
items.add(new Item(9, LocaleController.getString("TelegramFAQ", R.string.TelegramFAQ), helpIcon));
}
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-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) {
actionCell.setTextAndIcon(text, icon);
}
}
}