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

402 lines
17 KiB
Java

package org.telegram.ui;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MediaDataController;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
import org.telegram.messenger.UserConfig;
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.AvailableReactionCell;
import org.telegram.ui.Cells.TextInfoPrivacyCell;
import org.telegram.ui.Cells.ThemePreviewMessagesCell;
import org.telegram.ui.Components.AnimatedEmojiDrawable;
import org.telegram.ui.Components.LayoutHelper;
import org.telegram.ui.Components.Premium.PremiumFeatureBottomSheet;
import org.telegram.ui.Components.Reactions.ReactionsLayoutInBubble;
import org.telegram.ui.Components.RecyclerListView;
import org.telegram.ui.Components.SimpleThemeDescription;
import java.util.ArrayList;
import java.util.List;
public class ReactionsDoubleTapManageActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
private LinearLayout contentView;
private RecyclerListView listView;
private RecyclerView.Adapter listAdapter;
int previewRow;
int infoRow;
int reactionsStartRow = -1;
int premiumReactionRow;
int rowCount;
public ReactionsDoubleTapManageActivity() {
super();
}
@Override
public boolean onFragmentCreate() {
getNotificationCenter().addObserver(this, NotificationCenter.reactionsDidLoad);
getNotificationCenter().addObserver(this, NotificationCenter.currentUserPremiumStatusChanged);
return super.onFragmentCreate();
}
@Override
public View createView(Context context) {
actionBar.setTitle(LocaleController.getString("Reactions", R.string.Reactions));
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(true);
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
}
}
});
LinearLayout linaerLayout = new LinearLayout(context);
linaerLayout.setOrientation(LinearLayout.VERTICAL);
listView = new RecyclerListView(context);
((DefaultItemAnimator)listView.getItemAnimator()).setSupportsChangeAnimations(false);
listView.setLayoutManager(new LinearLayoutManager(context));
listView.setAdapter(listAdapter = new RecyclerListView.SelectionAdapter() {
@Override
public boolean isEnabled(RecyclerView.ViewHolder holder) {
return holder.getItemViewType() == 3 || holder.getItemViewType() == 2;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 0:
ThemePreviewMessagesCell messagesCell = new ThemePreviewMessagesCell(context, parentLayout, ThemePreviewMessagesCell.TYPE_REACTIONS_DOUBLE_TAP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
messagesCell.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
}
messagesCell.fragment = ReactionsDoubleTapManageActivity.this;
view = messagesCell;
break;
case 2:
TextInfoPrivacyCell cell = new TextInfoPrivacyCell(context);
cell.setText(LocaleController.getString("DoubleTapPreviewRational", R.string.DoubleTapPreviewRational));
cell.setBackground(Theme.getThemedDrawable(context, R.drawable.greydivider, Theme.key_windowBackgroundGrayShadow));
view = cell;
break;
case 3:
SetDefaultReactionCell setcell = new SetDefaultReactionCell(context);
setcell.update(false);
view = setcell;
break;
case 4:
view = new View(context) {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(
MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(16), MeasureSpec.EXACTLY)
);
}
};
view.setBackground(Theme.getThemedDrawable(context, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
break;
default:
case 1: {
view = new AvailableReactionCell(context, true, true);
}
break;
}
return new RecyclerListView.Holder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case 1:
AvailableReactionCell reactionCell = (AvailableReactionCell) holder.itemView;
TLRPC.TL_availableReaction react = getAvailableReactions().get(position - reactionsStartRow);
reactionCell.bind(react, react.reaction.contains(MediaDataController.getInstance(currentAccount).getDoubleTapReaction()), currentAccount);
break;
}
}
@Override
public int getItemCount() {
return rowCount + (premiumReactionRow < 0 ? getAvailableReactions().size() : 0) + 1;
}
@Override
public int getItemViewType(int position) {
if (position == previewRow) {
return 0;
}
if (position == infoRow) {
return 2;
}
if (position == premiumReactionRow) {
return 3;
}
if (position == getItemCount() - 1) {
return 4;
}
return 1;
}
});
listView.setOnItemClickListener((view, position) -> {
if (view instanceof AvailableReactionCell) {
AvailableReactionCell cell = (AvailableReactionCell) view;
if (cell.locked && !getUserConfig().isPremium()) {
showDialog(new PremiumFeatureBottomSheet(this, PremiumPreviewFragment.PREMIUM_FEATURE_REACTIONS, true));
return;
}
MediaDataController.getInstance(currentAccount).setDoubleTapReaction(cell.react.reaction);
listView.getAdapter().notifyItemRangeChanged(0, listView.getAdapter().getItemCount());
} else if (view instanceof SetDefaultReactionCell) {
showSelectStatusDialog((SetDefaultReactionCell) view);
}
});
linaerLayout.addView(listView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
fragmentView = contentView = linaerLayout;
updateColors();
updateRows();
return contentView;
}
private class SetDefaultReactionCell extends FrameLayout {
private TextView textView;
private AnimatedEmojiDrawable.SwapAnimatedEmojiDrawable imageDrawable;
public SetDefaultReactionCell(Context context) {
super(context);
setBackgroundColor(getThemedColor(Theme.key_windowBackgroundWhite));
textView = new TextView(context);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setTextColor(getThemedColor(Theme.key_windowBackgroundWhiteBlackText));
textView.setText(LocaleController.getString("DoubleTapSetting", R.string.DoubleTapSetting));
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.FILL_HORIZONTAL, 20, 0, 48, 0));
imageDrawable = new AnimatedEmojiDrawable.SwapAnimatedEmojiDrawable(this, AndroidUtilities.dp(24));
}
public void update(boolean animated) {
String reactionString = MediaDataController.getInstance(currentAccount).getDoubleTapReaction();
if (reactionString != null && reactionString.startsWith("animated_")) {
try {
long documentId = Long.parseLong(reactionString.substring(9));
imageDrawable.set(documentId, animated);
return;
} catch (Exception ignore) {}
}
TLRPC.TL_availableReaction reaction = MediaDataController.getInstance(currentAccount).getReactionsMap().get(reactionString);
if (reaction != null) {
imageDrawable.set(reaction.static_icon, animated);
}
}
public void updateImageBounds() {
imageDrawable.setBounds(
getWidth() - imageDrawable.getIntrinsicWidth() - AndroidUtilities.dp(21),
(getHeight() - imageDrawable.getIntrinsicHeight()) / 2,
getWidth() - AndroidUtilities.dp(21),
(getHeight() + imageDrawable.getIntrinsicHeight()) / 2
);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
updateImageBounds();
imageDrawable.draw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(
MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(50), MeasureSpec.EXACTLY)
);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
imageDrawable.detach();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
imageDrawable.attach();
}
}
private SelectAnimatedEmojiDialog.SelectAnimatedEmojiDialogWindow selectAnimatedEmojiDialog;
public void showSelectStatusDialog(SetDefaultReactionCell cell) {
if (selectAnimatedEmojiDialog != null) {
return;
}
final SelectAnimatedEmojiDialog.SelectAnimatedEmojiDialogWindow[] popup = new SelectAnimatedEmojiDialog.SelectAnimatedEmojiDialogWindow[1];
int xoff = 0, yoff = 0;
AnimatedEmojiDrawable.SwapAnimatedEmojiDrawable scrimDrawable = null;
View scrimDrawableParent = null;
if (cell != null) {
scrimDrawable = cell.imageDrawable;
scrimDrawableParent = cell;
if (cell.imageDrawable != null) {
cell.imageDrawable.play();
cell.updateImageBounds();
AndroidUtilities.rectTmp2.set(cell.imageDrawable.getBounds());
yoff = -(cell.getHeight() - AndroidUtilities.rectTmp2.centerY()) - AndroidUtilities.dp(16);
int popupWidth = (int) Math.min(AndroidUtilities.dp(340 - 16), AndroidUtilities.displaySize.x * .95f);
xoff = AndroidUtilities.rectTmp2.centerX() - (AndroidUtilities.displaySize.x - popupWidth);
}
}
SelectAnimatedEmojiDialog popupLayout = new SelectAnimatedEmojiDialog(this, getContext(), false, xoff, SelectAnimatedEmojiDialog.TYPE_SET_DEFAULT_REACTION, null) {
@Override
protected void onEmojiSelected(View emojiView, Long documentId, TLRPC.Document document, Integer until) {
if (documentId == null) {
return;
}
MediaDataController.getInstance(currentAccount).setDoubleTapReaction("animated_" + documentId);
if (cell != null) {
cell.update(true);
}
if (popup[0] != null) {
selectAnimatedEmojiDialog = null;
popup[0].dismiss();
}
}
@Override
protected void onReactionClick(ImageViewEmoji emoji, ReactionsLayoutInBubble.VisibleReaction reaction) {
MediaDataController.getInstance(currentAccount).setDoubleTapReaction(reaction.emojicon);
if (cell != null) {
cell.update(true);
}
if (popup[0] != null) {
selectAnimatedEmojiDialog = null;
popup[0].dismiss();
}
}
};
String selectedReaction = getMediaDataController().getDoubleTapReaction();
if (selectedReaction != null && selectedReaction.startsWith("animated_")) {
try {
popupLayout.setSelected(Long.parseLong(selectedReaction.substring(9)));
} catch (Exception e) {}
}
List<TLRPC.TL_availableReaction> availableReactions = getAvailableReactions();
ArrayList<ReactionsLayoutInBubble.VisibleReaction> reactions = new ArrayList<>(20);
for (int i = 0; i < availableReactions.size(); ++i) {
ReactionsLayoutInBubble.VisibleReaction reaction = new ReactionsLayoutInBubble.VisibleReaction();
TLRPC.TL_availableReaction tlreaction = availableReactions.get(i);
reaction.emojicon = tlreaction.reaction;
reactions.add(reaction);
}
popupLayout.setRecentReactions(reactions);
popupLayout.setSaveState(3);
popupLayout.setScrimDrawable(scrimDrawable, scrimDrawableParent);
popup[0] = selectAnimatedEmojiDialog = new SelectAnimatedEmojiDialog.SelectAnimatedEmojiDialogWindow(popupLayout, LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT) {
@Override
public void dismiss() {
super.dismiss();
selectAnimatedEmojiDialog = null;
}
};
popup[0].showAsDropDown(cell, 0, yoff, Gravity.TOP | Gravity.RIGHT);
popup[0].dimBehind();
}
private void updateRows() {
rowCount = 0;
previewRow = rowCount++;
infoRow = rowCount++;
if (UserConfig.getInstance(currentAccount).isPremium()) {
reactionsStartRow = -1;
premiumReactionRow = rowCount++;
} else {
premiumReactionRow = -1;
reactionsStartRow = rowCount;
}
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
getNotificationCenter().removeObserver(this, NotificationCenter.reactionsDidLoad);
getNotificationCenter().removeObserver(this, NotificationCenter.currentUserPremiumStatusChanged);
}
private List<TLRPC.TL_availableReaction> getAvailableReactions() {
return getMediaDataController().getReactionsList();
}
@Override
public ArrayList<ThemeDescription> getThemeDescriptions() {
return SimpleThemeDescription.createThemeDescriptions(this::updateColors,
Theme.key_windowBackgroundWhite,
Theme.key_windowBackgroundWhiteBlackText,
Theme.key_windowBackgroundWhiteGrayText2,
Theme.key_listSelector,
Theme.key_windowBackgroundGray,
Theme.key_windowBackgroundWhiteGrayText4,
Theme.key_windowBackgroundWhiteRedText4,
Theme.key_windowBackgroundChecked,
Theme.key_windowBackgroundCheckText,
Theme.key_switchTrackBlue,
Theme.key_switchTrackBlueChecked,
Theme.key_switchTrackBlueThumb,
Theme.key_switchTrackBlueThumbChecked
);
}
@SuppressLint("NotifyDataSetChanged")
private void updateColors() {
contentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
listAdapter.notifyDataSetChanged();
}
@SuppressLint("NotifyDataSetChanged")
@Override
public void didReceivedNotification(int id, int account, Object... args) {
if (account != currentAccount) return;
if (id == NotificationCenter.reactionsDidLoad) {
listAdapter.notifyDataSetChanged();
} else if (id == NotificationCenter.currentUserPremiumStatusChanged) {
updateRows();
listAdapter.notifyDataSetChanged();
}
}
}