2014-10-31 20:02:29 +01:00
|
|
|
/*
|
2015-10-29 18:10:07 +01:00
|
|
|
* This is the source code of Telegram for Android v. 3.x.x.
|
2014-10-31 20:02:29 +01:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
2017-03-31 01:58:05 +02:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2017.
|
2014-10-31 20:02:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui.Adapters;
|
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
import android.app.Activity;
|
2014-10-31 20:02:29 +01:00
|
|
|
import android.content.Context;
|
2017-12-08 18:35:59 +01:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Gravity;
|
2014-10-31 20:02:29 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-12-08 18:35:59 +01:00
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.TextView;
|
2014-10-31 20:02:29 +01:00
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2017-12-08 18:35:59 +01:00
|
|
|
import org.telegram.messenger.ApplicationLoader;
|
|
|
|
import org.telegram.messenger.LocaleController;
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.MessagesController;
|
2017-12-08 18:35:59 +01:00
|
|
|
import org.telegram.messenger.R;
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.support.widget.RecyclerView;
|
2017-12-08 18:35:59 +01:00
|
|
|
import org.telegram.tgnet.TLObject;
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.tgnet.TLRPC;
|
2017-12-08 18:35:59 +01:00
|
|
|
import org.telegram.ui.ActionBar.Theme;
|
2014-10-31 20:02:29 +01:00
|
|
|
import org.telegram.ui.Cells.DialogCell;
|
2017-12-08 18:35:59 +01:00
|
|
|
import org.telegram.ui.Cells.DialogMeUrlCell;
|
|
|
|
import org.telegram.ui.Cells.DialogsEmptyCell;
|
|
|
|
import org.telegram.ui.Cells.HeaderCell;
|
2014-11-19 02:23:46 +01:00
|
|
|
import org.telegram.ui.Cells.LoadingCell;
|
2017-12-08 18:35:59 +01:00
|
|
|
import org.telegram.ui.Components.LayoutHelper;
|
2017-03-31 01:58:05 +02:00
|
|
|
import org.telegram.ui.Components.RecyclerListView;
|
2014-10-31 20:02:29 +01:00
|
|
|
|
2015-06-29 19:12:11 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
public class DialogsAdapter extends RecyclerListView.SelectionAdapter {
|
2014-10-31 20:02:29 +01:00
|
|
|
|
|
|
|
private Context mContext;
|
2015-06-29 19:12:11 +02:00
|
|
|
private int dialogsType;
|
2014-10-31 20:02:29 +01:00
|
|
|
private long openedDialogId;
|
2015-02-26 02:32:51 +01:00
|
|
|
private int currentCount;
|
2017-12-08 18:35:59 +01:00
|
|
|
private boolean isOnlySelect;
|
|
|
|
private ArrayList<Long> selectedDialogs;
|
|
|
|
private boolean hasHints;
|
2014-10-31 20:02:29 +01:00
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
public DialogsAdapter(Context context, int type, boolean onlySelect) {
|
2014-10-31 20:02:29 +01:00
|
|
|
mContext = context;
|
2015-06-29 19:12:11 +02:00
|
|
|
dialogsType = type;
|
2017-12-08 18:35:59 +01:00
|
|
|
isOnlySelect = onlySelect;
|
|
|
|
hasHints = type == 0 && !onlySelect;
|
|
|
|
if (onlySelect) {
|
|
|
|
selectedDialogs = new ArrayList<>();
|
|
|
|
}
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setOpenedDialogId(long id) {
|
|
|
|
openedDialogId = id;
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
public boolean hasSelectedDialogs() {
|
|
|
|
return selectedDialogs != null && !selectedDialogs.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addOrRemoveSelectedDialog(long did, View cell) {
|
|
|
|
if (selectedDialogs.contains(did)) {
|
|
|
|
selectedDialogs.remove(did);
|
|
|
|
if (cell instanceof DialogCell) {
|
|
|
|
((DialogCell) cell).setChecked(false, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectedDialogs.add(did);
|
|
|
|
if (cell instanceof DialogCell) {
|
|
|
|
((DialogCell) cell).setChecked(true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Long> getSelectedDialogs() {
|
|
|
|
return selectedDialogs;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
public boolean isDataSetChanged() {
|
|
|
|
int current = currentCount;
|
2015-11-26 22:04:02 +01:00
|
|
|
return current != getItemCount() || current == 1;
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
2016-06-24 12:27:15 +02:00
|
|
|
private ArrayList<TLRPC.TL_dialog> getDialogsArray() {
|
2015-06-29 19:12:11 +02:00
|
|
|
if (dialogsType == 0) {
|
|
|
|
return MessagesController.getInstance().dialogs;
|
|
|
|
} else if (dialogsType == 1) {
|
|
|
|
return MessagesController.getInstance().dialogsServerOnly;
|
|
|
|
} else if (dialogsType == 2) {
|
|
|
|
return MessagesController.getInstance().dialogsGroupsOnly;
|
2017-12-08 18:35:59 +01:00
|
|
|
} else if (dialogsType == 3) {
|
|
|
|
return MessagesController.getInstance().dialogsForward;
|
2015-06-29 19:12:11 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
@Override
|
2015-05-21 23:27:27 +02:00
|
|
|
public int getItemCount() {
|
2015-06-29 19:12:11 +02:00
|
|
|
int count = getDialogsArray().size();
|
2014-10-31 20:02:29 +01:00
|
|
|
if (count == 0 && MessagesController.getInstance().loadingDialogs) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-08 18:35:59 +01:00
|
|
|
if (!MessagesController.getInstance().dialogsEndReached || count == 0) {
|
2014-10-31 20:02:29 +01:00
|
|
|
count++;
|
|
|
|
}
|
2017-12-08 18:35:59 +01:00
|
|
|
if (hasHints) {
|
|
|
|
count += 2 + MessagesController.getInstance().hintDialogs.size();
|
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
currentCount = count;
|
2014-10-31 20:02:29 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
public TLObject getItem(int i) {
|
2016-06-24 12:27:15 +02:00
|
|
|
ArrayList<TLRPC.TL_dialog> arrayList = getDialogsArray();
|
2017-12-08 18:35:59 +01:00
|
|
|
if (hasHints) {
|
|
|
|
int count = MessagesController.getInstance().hintDialogs.size();
|
|
|
|
if (i < 2 + count) {
|
|
|
|
return MessagesController.getInstance().hintDialogs.get(i - 1);
|
|
|
|
} else {
|
|
|
|
i -= count + 2;
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 19:12:11 +02:00
|
|
|
if (i < 0 || i >= arrayList.size()) {
|
|
|
|
return null;
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
2015-06-29 19:12:11 +02:00
|
|
|
return arrayList.get(i);
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:35:59 +01:00
|
|
|
@Override
|
|
|
|
public void notifyDataSetChanged() {
|
|
|
|
hasHints = dialogsType == 0 && !isOnlySelect && !MessagesController.getInstance().hintDialogs.isEmpty();
|
|
|
|
super.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
@Override
|
|
|
|
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
|
|
|
|
if (holder.itemView instanceof DialogCell) {
|
|
|
|
((DialogCell) holder.itemView).checkCurrentDialogIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
@Override
|
2017-03-31 01:58:05 +02:00
|
|
|
public boolean isEnabled(RecyclerView.ViewHolder holder) {
|
2017-12-08 18:35:59 +01:00
|
|
|
int viewType = holder.getItemViewType();
|
|
|
|
return viewType != 1 && viewType != 5 && viewType != 3;
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-21 23:27:27 +02:00
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
|
2017-12-08 18:35:59 +01:00
|
|
|
View view;
|
|
|
|
switch (viewType) {
|
|
|
|
case 0:
|
|
|
|
view = new DialogCell(mContext, isOnlySelect);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
view = new LoadingCell(mContext);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
HeaderCell headerCell = new HeaderCell(mContext);
|
|
|
|
headerCell.setText(LocaleController.getString("RecentlyViewed", R.string.RecentlyViewed));
|
|
|
|
|
|
|
|
TextView textView = new TextView(mContext);
|
|
|
|
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
|
|
|
|
textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
|
|
|
|
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueHeader));
|
|
|
|
textView.setText(LocaleController.getString("RecentlyViewedHide", R.string.RecentlyViewedHide));
|
|
|
|
textView.setGravity((LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL);
|
|
|
|
headerCell.addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.TOP, 17, 15, 17, 0));
|
|
|
|
textView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
MessagesController.getInstance().hintDialogs.clear();
|
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
|
|
|
|
preferences.edit().remove("installReferer").commit();
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
view = headerCell;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
FrameLayout frameLayout = new FrameLayout(mContext) {
|
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(12), MeasureSpec.EXACTLY));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
|
|
|
|
View v = new View(mContext);
|
|
|
|
v.setBackgroundDrawable(Theme.getThemedDrawable(mContext, R.drawable.greydivider, Theme.key_windowBackgroundGrayShadow));
|
|
|
|
frameLayout.addView(v, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
|
|
|
|
view = frameLayout;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
view = new DialogMeUrlCell(mContext);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
default:
|
|
|
|
view = new DialogsEmptyCell(mContext);
|
|
|
|
break;
|
2015-05-21 23:27:27 +02:00
|
|
|
}
|
2017-12-08 18:35:59 +01:00
|
|
|
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, viewType == 5 ? RecyclerView.LayoutParams.MATCH_PARENT : RecyclerView.LayoutParams.WRAP_CONTENT));
|
2017-03-31 01:58:05 +02:00
|
|
|
return new RecyclerListView.Holder(view);
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-08 18:35:59 +01:00
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
|
|
|
|
switch (holder.getItemViewType()) {
|
|
|
|
case 0: {
|
|
|
|
DialogCell cell = (DialogCell) holder.itemView;
|
|
|
|
TLRPC.TL_dialog dialog = (TLRPC.TL_dialog) getItem(i);
|
|
|
|
if (hasHints) {
|
|
|
|
i -= 2 + MessagesController.getInstance().hintDialogs.size();
|
|
|
|
}
|
|
|
|
cell.useSeparator = (i != getItemCount() - 1);
|
|
|
|
if (dialogsType == 0) {
|
|
|
|
if (AndroidUtilities.isTablet()) {
|
|
|
|
cell.setDialogSelected(dialog.id == openedDialogId);
|
|
|
|
}
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
2017-12-08 18:35:59 +01:00
|
|
|
if (selectedDialogs != null) {
|
|
|
|
cell.setChecked(selectedDialogs.contains(dialog.id), false);
|
|
|
|
}
|
|
|
|
cell.setDialog(dialog, i, dialogsType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
DialogMeUrlCell cell = (DialogMeUrlCell) holder.itemView;
|
|
|
|
cell.setRecentMeUrl((TLRPC.RecentMeUrl) getItem(i));
|
|
|
|
break;
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int i) {
|
2017-12-08 18:35:59 +01:00
|
|
|
if (hasHints) {
|
|
|
|
int count = MessagesController.getInstance().hintDialogs.size();
|
|
|
|
if (i < 2 + count) {
|
|
|
|
if (i == 0) {
|
|
|
|
return 2;
|
|
|
|
} else if (i == 1 + count) {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
} else {
|
|
|
|
i -= 2 + count;
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 19:12:11 +02:00
|
|
|
if (i == getDialogsArray().size()) {
|
2017-12-08 18:35:59 +01:00
|
|
|
if (!MessagesController.getInstance().dialogsEndReached) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 5;
|
|
|
|
}
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|