2016-10-11 13:57:01 +02:00
|
|
|
/*
|
2019-01-23 18:03:33 +01:00
|
|
|
* This is the source code of Telegram for Android v. 5.x.x.
|
2016-10-11 13:57:01 +02: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).
|
|
|
|
*
|
2019-01-23 18:03:33 +01:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2018.
|
2016-10-11 13:57:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-07-30 04:07:02 +02:00
|
|
|
import android.util.LongSparseArray;
|
2016-10-11 13:57:01 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
import org.telegram.messenger.DataQuery;
|
2016-10-11 13:57:01 +02:00
|
|
|
import org.telegram.messenger.LocaleController;
|
|
|
|
import org.telegram.messenger.NotificationCenter;
|
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.messenger.support.widget.LinearLayoutManager;
|
|
|
|
import org.telegram.messenger.support.widget.RecyclerView;
|
|
|
|
import org.telegram.tgnet.TLRPC;
|
|
|
|
import org.telegram.ui.ActionBar.ActionBar;
|
|
|
|
import org.telegram.ui.ActionBar.BaseFragment;
|
2017-03-31 01:58:05 +02:00
|
|
|
import org.telegram.ui.ActionBar.Theme;
|
|
|
|
import org.telegram.ui.ActionBar.ThemeDescription;
|
2016-10-11 13:57:01 +02:00
|
|
|
import org.telegram.ui.Cells.FeaturedStickerSetCell;
|
|
|
|
import org.telegram.ui.Cells.TextInfoPrivacyCell;
|
|
|
|
import org.telegram.ui.Components.LayoutHelper;
|
|
|
|
import org.telegram.ui.Components.RecyclerListView;
|
|
|
|
import org.telegram.ui.Components.StickersAlert;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class FeaturedStickersActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
|
|
|
|
|
|
|
|
private ListAdapter listAdapter;
|
2017-03-31 01:58:05 +02:00
|
|
|
private RecyclerListView listView;
|
2016-10-11 13:57:01 +02:00
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
|
|
|
|
private ArrayList<Long> unreadStickers = null;
|
2018-07-30 04:07:02 +02:00
|
|
|
private LongSparseArray<TLRPC.StickerSetCovered> installingStickerSets = new LongSparseArray<>();
|
2016-10-11 13:57:01 +02:00
|
|
|
|
|
|
|
private int stickersStartRow;
|
|
|
|
private int stickersEndRow;
|
|
|
|
private int stickersShadowRow;
|
|
|
|
private int rowCount;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onFragmentCreate() {
|
|
|
|
super.onFragmentCreate();
|
2018-07-30 04:07:02 +02:00
|
|
|
DataQuery.getInstance(currentAccount).checkFeaturedStickers();
|
2019-01-23 18:03:33 +01:00
|
|
|
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.featuredStickersDidLoad);
|
|
|
|
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.stickersDidLoad);
|
2018-07-30 04:07:02 +02:00
|
|
|
ArrayList<Long> arrayList = DataQuery.getInstance(currentAccount).getUnreadStickerSets();
|
2016-10-11 13:57:01 +02:00
|
|
|
if (arrayList != null) {
|
|
|
|
unreadStickers = new ArrayList<>(arrayList);
|
|
|
|
}
|
|
|
|
updateRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFragmentDestroy() {
|
|
|
|
super.onFragmentDestroy();
|
2019-01-23 18:03:33 +01:00
|
|
|
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.featuredStickersDidLoad);
|
|
|
|
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.stickersDidLoad);
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View createView(Context context) {
|
|
|
|
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
|
|
|
|
actionBar.setAllowOverlayTitle(true);
|
|
|
|
actionBar.setTitle(LocaleController.getString("FeaturedStickers", R.string.FeaturedStickers));
|
|
|
|
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(int id) {
|
|
|
|
if (id == -1) {
|
|
|
|
finishFragment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
listAdapter = new ListAdapter(context);
|
|
|
|
|
|
|
|
fragmentView = new FrameLayout(context);
|
|
|
|
FrameLayout frameLayout = (FrameLayout) fragmentView;
|
2017-03-31 01:58:05 +02:00
|
|
|
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
|
2016-10-11 13:57:01 +02:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
listView = new RecyclerListView(context);
|
2016-10-11 13:57:01 +02:00
|
|
|
listView.setItemAnimator(null);
|
|
|
|
listView.setLayoutAnimation(null);
|
|
|
|
listView.setFocusable(true);
|
|
|
|
listView.setTag(14);
|
|
|
|
layoutManager = new LinearLayoutManager(context) {
|
|
|
|
@Override
|
|
|
|
public boolean supportsPredictiveItemAnimations() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
|
|
|
listView.setLayoutManager(layoutManager);
|
|
|
|
|
|
|
|
frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
|
|
|
|
listView.setAdapter(listAdapter);
|
2019-01-23 18:03:33 +01:00
|
|
|
listView.setOnItemClickListener((view, position) -> {
|
|
|
|
if (position >= stickersStartRow && position < stickersEndRow && getParentActivity() != null) {
|
|
|
|
final TLRPC.StickerSetCovered stickerSet = DataQuery.getInstance(currentAccount).getFeaturedStickerSets().get(position);
|
|
|
|
TLRPC.InputStickerSet inputStickerSet;
|
|
|
|
if (stickerSet.set.id != 0) {
|
|
|
|
inputStickerSet = new TLRPC.TL_inputStickerSetID();
|
|
|
|
inputStickerSet.id = stickerSet.set.id;
|
|
|
|
} else {
|
|
|
|
inputStickerSet = new TLRPC.TL_inputStickerSetShortName();
|
|
|
|
inputStickerSet.short_name = stickerSet.set.short_name;
|
|
|
|
}
|
|
|
|
inputStickerSet.access_hash = stickerSet.set.access_hash;
|
|
|
|
StickersAlert stickersAlert = new StickersAlert(getParentActivity(), FeaturedStickersActivity.this, inputStickerSet, null, null);
|
|
|
|
stickersAlert.setInstallDelegate(new StickersAlert.StickersAlertInstallDelegate() {
|
|
|
|
@Override
|
|
|
|
public void onStickerSetInstalled() {
|
|
|
|
FeaturedStickerSetCell cell = (FeaturedStickerSetCell) view;
|
|
|
|
cell.setDrawProgress(true);
|
|
|
|
installingStickerSets.put(stickerSet.set.id, stickerSet);
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
@Override
|
|
|
|
public void onStickerSetUninstalled() {
|
2016-10-11 13:57:01 +02:00
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
showDialog(stickersAlert);
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-30 04:07:02 +02:00
|
|
|
public void didReceivedNotification(int id, int account, Object... args) {
|
2019-01-23 18:03:33 +01:00
|
|
|
if (id == NotificationCenter.featuredStickersDidLoad) {
|
2016-10-11 13:57:01 +02:00
|
|
|
if (unreadStickers == null) {
|
2018-07-30 04:07:02 +02:00
|
|
|
unreadStickers = DataQuery.getInstance(currentAccount).getUnreadStickerSets();
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
updateRows();
|
2019-01-23 18:03:33 +01:00
|
|
|
} else if (id == NotificationCenter.stickersDidLoad) {
|
2016-10-11 13:57:01 +02:00
|
|
|
updateVisibleTrendingSets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateVisibleTrendingSets() {
|
|
|
|
if (layoutManager == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int first = layoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (first == RecyclerView.NO_POSITION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int last = layoutManager.findLastVisibleItemPosition();
|
|
|
|
if (last == RecyclerView.NO_POSITION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listAdapter.notifyItemRangeChanged(first, last - first + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateRows() {
|
|
|
|
rowCount = 0;
|
2018-07-30 04:07:02 +02:00
|
|
|
ArrayList<TLRPC.StickerSetCovered> stickerSets = DataQuery.getInstance(currentAccount).getFeaturedStickerSets();
|
2016-10-11 13:57:01 +02:00
|
|
|
if (!stickerSets.isEmpty()) {
|
|
|
|
stickersStartRow = rowCount;
|
|
|
|
stickersEndRow = rowCount + stickerSets.size();
|
|
|
|
rowCount += stickerSets.size();
|
|
|
|
stickersShadowRow = rowCount++;
|
|
|
|
} else {
|
|
|
|
stickersStartRow = -1;
|
|
|
|
stickersEndRow = -1;
|
|
|
|
stickersShadowRow = -1;
|
|
|
|
}
|
|
|
|
if (listAdapter != null) {
|
|
|
|
listAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
DataQuery.getInstance(currentAccount).markFaturedStickersAsRead(true);
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (listAdapter != null) {
|
|
|
|
listAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
private class ListAdapter extends RecyclerListView.SelectionAdapter {
|
2016-10-11 13:57:01 +02:00
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
private Context mContext;
|
2016-10-11 13:57:01 +02:00
|
|
|
|
|
|
|
public ListAdapter(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return rowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
|
|
|
if (getItemViewType(position) == 0) {
|
2018-07-30 04:07:02 +02:00
|
|
|
ArrayList<TLRPC.StickerSetCovered> arrayList = DataQuery.getInstance(currentAccount).getFeaturedStickerSets();
|
2016-10-11 13:57:01 +02:00
|
|
|
FeaturedStickerSetCell cell = (FeaturedStickerSetCell) holder.itemView;
|
|
|
|
cell.setTag(position);
|
|
|
|
TLRPC.StickerSetCovered stickerSet = arrayList.get(position);
|
|
|
|
cell.setStickersSet(stickerSet, position != arrayList.size() - 1, unreadStickers != null && unreadStickers.contains(stickerSet.set.id));
|
2018-07-30 04:07:02 +02:00
|
|
|
boolean installing = installingStickerSets.indexOfKey(stickerSet.set.id) >= 0;
|
2016-10-11 13:57:01 +02:00
|
|
|
if (installing && cell.isInstalled()) {
|
|
|
|
installingStickerSets.remove(stickerSet.set.id);
|
|
|
|
installing = false;
|
|
|
|
cell.setDrawProgress(false);
|
|
|
|
}
|
|
|
|
cell.setDrawProgress(installing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean isEnabled(RecyclerView.ViewHolder holder) {
|
|
|
|
return holder.getItemViewType() == 0;
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View view = null;
|
|
|
|
switch (viewType) {
|
|
|
|
case 0:
|
|
|
|
view = new FeaturedStickerSetCell(mContext);
|
2017-03-31 01:58:05 +02:00
|
|
|
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
2019-01-23 18:03:33 +01:00
|
|
|
((FeaturedStickerSetCell) view).setAddOnClickListener(v -> {
|
|
|
|
FeaturedStickerSetCell parent1 = (FeaturedStickerSetCell) v.getParent();
|
|
|
|
TLRPC.StickerSetCovered pack = parent1.getStickerSet();
|
|
|
|
if (installingStickerSets.indexOfKey(pack.set.id) >= 0) {
|
|
|
|
return;
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
installingStickerSets.put(pack.set.id, pack);
|
|
|
|
DataQuery.getInstance(currentAccount).removeStickersSet(getParentActivity(), pack.set, 2, FeaturedStickersActivity.this, false);
|
|
|
|
parent1.setDrawProgress(true);
|
2016-10-11 13:57:01 +02:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
view = new TextInfoPrivacyCell(mContext);
|
2017-03-31 01:58:05 +02:00
|
|
|
view.setBackgroundDrawable(Theme.getThemedDrawable(mContext, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
|
2016-10-11 13:57:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
|
2017-03-31 01:58:05 +02:00
|
|
|
return new RecyclerListView.Holder(view);
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int i) {
|
|
|
|
if (i >= stickersStartRow && i < stickersEndRow) {
|
|
|
|
return 0;
|
|
|
|
} else if (i == stickersShadowRow) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 01:58:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ThemeDescription[] getThemeDescriptions() {
|
|
|
|
return new ThemeDescription[]{
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{FeaturedStickerSetCell.class}, null, null, null, Theme.key_windowBackgroundWhite),
|
|
|
|
new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray),
|
|
|
|
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault),
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle),
|
|
|
|
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector),
|
|
|
|
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector),
|
|
|
|
|
|
|
|
new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider),
|
|
|
|
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{TextInfoPrivacyCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow),
|
|
|
|
|
|
|
|
new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"progressPaint"}, null, null, null, Theme.key_featuredStickers_buttonProgress),
|
|
|
|
new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText),
|
|
|
|
new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2),
|
|
|
|
new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"addButton"}, null, null, null, Theme.key_featuredStickers_buttonText),
|
|
|
|
new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"checkImage"}, null, null, null, Theme.key_featuredStickers_addedIcon),
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_USEBACKGROUNDDRAWABLE, new Class[]{FeaturedStickerSetCell.class}, new String[]{"addButton"}, null, null, null, Theme.key_featuredStickers_addButton),
|
|
|
|
new ThemeDescription(listView, ThemeDescription.FLAG_USEBACKGROUNDDRAWABLE | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, new Class[]{FeaturedStickerSetCell.class}, new String[]{"addButton"}, null, null, null, Theme.key_featuredStickers_addButtonPressed),
|
|
|
|
};
|
|
|
|
}
|
2016-10-11 13:57:01 +02:00
|
|
|
}
|