NekoX/TMessagesProj/src/main/java/org/telegram/ui/FeaturedStickersActivity.java

298 lines
14 KiB
Java

/*
* 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).
*
* Copyright Nikolai Kudashov, 2013-2018.
*/
package org.telegram.ui;
import android.content.Context;
import android.util.LongSparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import org.telegram.messenger.MediaDataController;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
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;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class FeaturedStickersActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
private ListAdapter listAdapter;
private RecyclerListView listView;
private LinearLayoutManager layoutManager;
private ArrayList<Long> unreadStickers = null;
private LongSparseArray<TLRPC.StickerSetCovered> installingStickerSets = new LongSparseArray<>();
private int stickersStartRow;
private int stickersEndRow;
private int stickersShadowRow;
private int rowCount;
@Override
public boolean onFragmentCreate() {
super.onFragmentCreate();
MediaDataController.getInstance(currentAccount).checkFeaturedStickers();
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.featuredStickersDidLoad);
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.stickersDidLoad);
ArrayList<Long> arrayList = MediaDataController.getInstance(currentAccount).getUnreadStickerSets();
if (arrayList != null) {
unreadStickers = new ArrayList<>(arrayList);
}
updateRows();
return true;
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.featuredStickersDidLoad);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.stickersDidLoad);
}
@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;
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
listView = new RecyclerListView(context);
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);
listView.setOnItemClickListener((view, position) -> {
if (position >= stickersStartRow && position < stickersEndRow && getParentActivity() != null) {
final TLRPC.StickerSetCovered stickerSet = MediaDataController.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, true);
installingStickerSets.put(stickerSet.set.id, stickerSet);
}
@Override
public void onStickerSetUninstalled() {
}
});
showDialog(stickersAlert);
}
});
return fragmentView;
}
@Override
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.featuredStickersDidLoad) {
if (unreadStickers == null) {
unreadStickers = MediaDataController.getInstance(currentAccount).getUnreadStickerSets();
}
updateRows();
} else if (id == NotificationCenter.stickersDidLoad) {
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;
ArrayList<TLRPC.StickerSetCovered> stickerSets = MediaDataController.getInstance(currentAccount).getFeaturedStickerSets();
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();
}
MediaDataController.getInstance(currentAccount).markFaturedStickersAsRead(true);
}
@Override
public void onResume() {
super.onResume();
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
}
private class ListAdapter extends RecyclerListView.SelectionAdapter {
private Context mContext;
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) {
ArrayList<TLRPC.StickerSetCovered> arrayList = MediaDataController.getInstance(currentAccount).getFeaturedStickerSets();
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));
boolean installing = installingStickerSets.indexOfKey(stickerSet.set.id) >= 0;
if (installing && cell.isInstalled()) {
installingStickerSets.remove(stickerSet.set.id);
installing = false;
}
cell.setDrawProgress(installing, false);
}
}
@Override
public boolean isEnabled(RecyclerView.ViewHolder holder) {
return holder.getItemViewType() == 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 0:
view = new FeaturedStickerSetCell(mContext);
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
((FeaturedStickerSetCell) view).setAddOnClickListener(v -> {
FeaturedStickerSetCell parent1 = (FeaturedStickerSetCell) v.getParent();
TLRPC.StickerSetCovered pack = parent1.getStickerSet();
if (installingStickerSets.indexOfKey(pack.set.id) >= 0) {
return;
}
installingStickerSets.put(pack.set.id, pack);
MediaDataController.getInstance(currentAccount).toggleStickerSet(getParentActivity(), pack, 2, FeaturedStickersActivity.this, false, false);
parent1.setDrawProgress(true, true);
});
break;
case 1:
default:
view = new TextInfoPrivacyCell(mContext);
view.setBackgroundDrawable(Theme.getThemedDrawable(mContext, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
break;
}
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
return new RecyclerListView.Holder(view);
}
@Override
public int getItemViewType(int i) {
if (i >= stickersStartRow && i < stickersEndRow) {
return 0;
} else if (i == stickersShadowRow) {
return 1;
}
return 0;
}
}
@Override
public ArrayList<ThemeDescription> getThemeDescriptions() {
ArrayList<ThemeDescription> themeDescriptions = new ArrayList<>();
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{FeaturedStickerSetCell.class}, null, null, null, Theme.key_windowBackgroundWhite));
themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{TextInfoPrivacyCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"progressPaint"}, null, null, null, Theme.key_featuredStickers_buttonProgress));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"addButton"}, null, null, null, Theme.key_featuredStickers_buttonText));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{FeaturedStickerSetCell.class}, new String[]{"checkImage"}, null, null, null, Theme.key_featuredStickers_addedIcon));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_USEBACKGROUNDDRAWABLE, new Class[]{FeaturedStickerSetCell.class}, new String[]{"addButton"}, null, null, null, Theme.key_featuredStickers_addButton));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_USEBACKGROUNDDRAWABLE | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, new Class[]{FeaturedStickerSetCell.class}, new String[]{"addButton"}, null, null, null, Theme.key_featuredStickers_addButtonPressed));
return themeDescriptions;
}
}