Skip to content

Commit

Permalink
perf: Some improvements to External Sticker Cache (NextAlone#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruattd authored and LiuYi0526 committed Feb 1, 2024
1 parent b2c84e4 commit 772dc55
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1953,9 +1953,6 @@ public void addNewStickerSet(TLRPC.TL_messages_stickerSet set) {
loadHash[type] = calcStickersHash(stickerSets[type]);
getNotificationCenter().postNotificationName(NotificationCenter.stickersDidLoad, type, true);
loadStickers(type, false, true);

// Na: [ExternalStickerCache] cache sticker sets
ExternalStickerCacheHelper.cacheStickers();
}

public void loadFeaturedStickers(boolean emoji, boolean cache, boolean force) {
Expand Down Expand Up @@ -2726,9 +2723,6 @@ public void loadStickers(int type, boolean cache, boolean force, boolean schedul
}));
}
}

// Na: [ExternalStickerCache] cache sticker sets
ExternalStickerCacheHelper.cacheStickers();
}

private void putStickersToCache(int type, ArrayList<TLRPC.TL_messages_stickerSet> stickers, int date, long hash) {
Expand Down Expand Up @@ -2764,9 +2758,6 @@ private void putStickersToCache(int type, ArrayList<TLRPC.TL_messages_stickerSet
} catch (Exception e) {
FileLog.e(e);
}

// Na: [ExternalStickerCache] cache sticker sets
ExternalStickerCacheHelper.cacheStickers();
});
}

Expand Down Expand Up @@ -3125,9 +3116,6 @@ public void toggleStickerSet(Context context, TLObject stickerSetObject, int tog
}

getNotificationCenter().postNotificationName(NotificationCenter.stickersDidLoad, type, true);

// Na: [ExternalStickerCache] cache sticker sets
ExternalStickerCacheHelper.cacheStickers();
}

public void removeMultipleStickerSets(Context context, BaseFragment fragment, ArrayList<TLRPC.TL_messages_stickerSet> sets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,9 +1166,6 @@ private void updateStickerTabs() {
stickersTab.onPageScrolled(lastPosition, lastPosition);
}
checkPanels();

// Na: [ExternalStickerCache] cache sticker sets
ExternalStickerCacheHelper.cacheStickers();
}

private void checkPanels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
import tw.nekomimi.nekogram.utils.ProxyUtil;
import tw.nekomimi.nekogram.utils.UIUtil;
import xyz.nextalone.nagram.NaConfig;
import xyz.nextalone.nagram.helper.ExternalStickerCacheHelper;

public class LaunchActivity extends BasePermissionsActivity implements INavigationLayout.INavigationLayoutDelegate, NotificationCenter.NotificationCenterDelegate, DialogsActivity.DialogsActivityDelegate {
public final static String EXTRA_FORCE_NOT_INTERNAL_APPS = "force_not_internal_apps";
Expand Down Expand Up @@ -1496,6 +1497,8 @@ private void checkCurrentAccount() {
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.currentUserPremiumStatusChanged);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.chatSwithcedToForum);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.storiesEnabledUpdate);
// Na: [ExternalStickerCache] remove observers
ExternalStickerCacheHelper.removeNotificationObservers(currentAccount);
}
currentAccount = UserConfig.selectedAccount;
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.appDidLogout);
Expand All @@ -1518,6 +1521,8 @@ private void checkCurrentAccount() {
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.currentUserPremiumStatusChanged);
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.chatSwithcedToForum);
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.storiesEnabledUpdate);
// Na: [ExternalStickerCache] add observers
ExternalStickerCacheHelper.addNotificationObservers(currentAccount);
}

private void checkLayout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
import tw.nekomimi.nekogram.utils.ShareUtil;
import tw.nekomimi.nekogram.utils.StickersUtil;
import tw.nekomimi.nekogram.utils.UIUtil;
import xyz.nextalone.nagram.NaConfig;
import xyz.nextalone.nagram.helper.ExternalStickerCacheHelper;

public class StickersActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {

Expand Down Expand Up @@ -1721,6 +1723,10 @@ protected void onRemoveButtonClick() {
options.add(R.drawable.msg_reorder, LocaleController.getString("StickersReorder", R.string.StickersReorder), () -> processSelectionOption(4, stickerSet));
options.add(R.drawable.msg_share, LocaleController.getString("StickersShare", R.string.StickersShare), () -> processSelectionOption(2, stickerSet));
options.add(R.drawable.msg_delete, LocaleController.getString("StickersRemove", R.string.StickersRemove), true, () -> processSelectionOption(MENU_DELETE, stickerSet));
if (!NaConfig.INSTANCE.getExternalStickerCache().String().isBlank()) {
options.add(R.drawable.menu_views_reposts, LocaleController.getString(R.string.ExternalStickerCacheRefresh), () -> ExternalStickerCacheHelper.refreshCacheFiles(stickerSet));
options.add(R.drawable.msg_delete, LocaleController.getString(R.string.ExternalStickerCacheDelete), () -> ExternalStickerCacheHelper.deleteCacheFiles(stickerSet));
}
}
options.setMinWidth(190);
options.show();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tw.nekomimi.nekogram.config.cell;

import androidx.recyclerview.widget.RecyclerView;
import org.telegram.messenger.LocaleController;
import org.telegram.ui.Cells.TextSettingsCell;
import tw.nekomimi.nekogram.config.CellGroup;

public class ConfigCellText extends AbstractConfigCell implements WithKey, WithOnClick {
private final String key;
private final String value;
private final Runnable onClick;
private boolean enabled = true;
private TextSettingsCell cell;

public ConfigCellText(String key, String customValue, Runnable onClick) {
this.key = key;
this.value = (customValue == null) ? "" : customValue;
this.onClick = onClick;
}

public ConfigCellText(String key, Runnable onClick) {
this(key, null, onClick);
}

public int getType() {
return CellGroup.ITEM_TYPE_TEXT_SETTINGS_CELL;
}

public String getKey() {
return key;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
if (this.cell != null) this.cell.setEnabled(this.enabled);
}

public void onBindViewHolder(RecyclerView.ViewHolder holder) {
TextSettingsCell cell = (TextSettingsCell) holder.itemView;
this.cell = cell;
String title = LocaleController.getString(key);
cell.setTextAndValue(title, value, cellGroup.needSetDivider(this));
cell.setEnabled(enabled);
}

public void onClick() {
if (!enabled) return;
if (onClick != null) {
try {
onClick.run();
} catch (Exception ignored) {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tw.nekomimi.nekogram.config.cell;

public interface WithKey {
String getKey();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tw.nekomimi.nekogram.config.cell;

public interface WithOnClick {
void onClick();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@

import tw.nekomimi.nekogram.config.CellGroup;
import tw.nekomimi.nekogram.config.ConfigItem;
import tw.nekomimi.nekogram.config.cell.AbstractConfigCell;
import tw.nekomimi.nekogram.config.cell.ConfigCellAutoTextCheck;
import tw.nekomimi.nekogram.config.cell.ConfigCellCustom;
import tw.nekomimi.nekogram.config.cell.ConfigCellSelectBox;
import tw.nekomimi.nekogram.config.cell.ConfigCellTextCheck;
import tw.nekomimi.nekogram.config.cell.ConfigCellTextDetail;
import tw.nekomimi.nekogram.config.cell.ConfigCellTextInput;
import tw.nekomimi.nekogram.config.cell.*;

public class BaseNekoXSettingsActivity extends BaseFragment {
protected BlurredRecyclerView listView;
Expand Down Expand Up @@ -79,7 +73,9 @@ protected ConfigItem getBindConfig(AbstractConfigCell row) {
}

protected String getRowKey(AbstractConfigCell row) {
if (row instanceof ConfigCellTextCheck) {
if (row instanceof WithKey) {
return ((WithKey) row).getKey();
} else if (row instanceof ConfigCellTextCheck) {
return ((ConfigCellTextCheck) row).getKey();
} else if (row instanceof ConfigCellSelectBox) {
return ((ConfigCellSelectBox) row).getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

Expand All @@ -24,11 +23,9 @@
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MediaController;
import org.telegram.messenger.R;
import org.telegram.messenger.SharedConfig;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.AlertDialog;
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
import org.telegram.ui.Cells.EmptyCell;
Expand All @@ -41,15 +38,13 @@
import org.telegram.ui.Cells.TextSettingsCell;
import org.telegram.ui.Components.AlertsCreator;
import org.telegram.ui.Components.BlurredRecyclerView;
import org.telegram.ui.Components.BulletinFactory;
import org.telegram.ui.Components.LayoutHelper;
import org.telegram.ui.Components.RecyclerListView;
import org.telegram.ui.Components.UndoView;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Locale;

import kotlin.Unit;

Expand Down Expand Up @@ -104,24 +99,38 @@ public class NekoExperimentalSettingsActivity extends BaseNekoXSettingsActivity
private final AbstractConfigCell customArtworkApiRow = cellGroup.appendCell(new ConfigCellTextInput(null, NaConfig.INSTANCE.getCustomArtworkApi(), "", null));
private final AbstractConfigCell fakeHighPerformanceDeviceRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getFakeHighPerformanceDevice()));
private final AbstractConfigCell disableEmojiDrawLimitRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableEmojiDrawLimit()));
private final AbstractConfigCell divider1 = cellGroup.appendCell(new ConfigCellDivider());

private final AbstractConfigCell header3 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString(R.string.ExternalStickerCache)));
private final AbstractConfigCell externalStickerCacheRow = cellGroup.appendCell(new ConfigCellAutoTextCheck(
NaConfig.INSTANCE.getExternalStickerCache(), LocaleController.getString(R.string.ExternalStickerCacheHint), this::onExternalStickerCacheButtonClick));
private final AbstractConfigCell divider1 = cellGroup.appendCell(new ConfigCellDivider());
private final AbstractConfigCell externalStickerCacheAutoSyncRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getExternalStickerCacheAutoRefresh(), LocaleController.getString(R.string.ExternalStickerCacheAutoRefreshHint)));
private final AbstractConfigCell externalStickerCacheDirNameTypeRow = cellGroup.appendCell(new ConfigCellSelectBox(null, NaConfig.INSTANCE.getExternalStickerCacheDirNameType(), new String[]{ "Short name", "ID" }, null));
private final AbstractConfigCell externalStickerCacheSyncAllRow = cellGroup.appendCell(new ConfigCellText("ExternalStickerCacheRefreshAll", ExternalStickerCacheHelper::syncAllCaches));
private final AbstractConfigCell externalStickerCacheDeleteAllRow = cellGroup.appendCell(new ConfigCellText("ExternalStickerCacheDeleteAll", ExternalStickerCacheHelper::deleteAllCaches));
private final AbstractConfigCell divider2 = cellGroup.appendCell(new ConfigCellDivider());

private UndoView tooltip;

private static final int INTENT_PICK_CUSTOM_EMOJI_PACK = 114;
private static final int INTENT_PICK_EXTERNAL_STICKER_DIRECTORY = 514;

private void setExternalStickerCacheCellsEnabled(boolean enabled) {
((ConfigCellText) externalStickerCacheSyncAllRow).setEnabled(enabled);
((ConfigCellText) externalStickerCacheDeleteAllRow).setEnabled(enabled);
}

private void refreshExternalStickerStorageState() {
ConfigCellAutoTextCheck cell = (ConfigCellAutoTextCheck) externalStickerCacheRow;
setExternalStickerCacheCellsEnabled(!cell.getBindConfig().String().isEmpty());
Context context = ApplicationLoader.applicationContext;
ExternalStickerCacheHelper.checkUri(cell, context);
}

private void onExternalStickerCacheButtonClick(boolean isChecked) {
if (isChecked) {
// clear config
setExternalStickerCacheCellsEnabled(false);
ConfigCellAutoTextCheck cell = (ConfigCellAutoTextCheck) externalStickerCacheRow;
cell.setSubtitle(null);
NaConfig.INSTANCE.getExternalStickerCache().setConfigString("");
Expand Down Expand Up @@ -179,6 +188,8 @@ public void onItemClick(int id) {
((ConfigCellTextCheck) a).onClick((TextCheckCell) view);
} else if (a instanceof ConfigCellSelectBox) {
((ConfigCellSelectBox) a).onClick(view);
} else if (a instanceof WithOnClick) {
((WithOnClick) a).onClick();
} else if (a instanceof ConfigCellTextInput) {
((ConfigCellTextInput) a).onClick();
} else if (a instanceof ConfigCellAutoTextCheck) {
Expand Down
12 changes: 12 additions & 0 deletions TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,18 @@ object NaConfig {
var externalStickerCacheUri: Uri?
get() = externalStickerCache.String().let { if (it.isBlank()) return null else return Uri.parse(it) }
set(value) = externalStickerCache.setConfigString(value.toString())
val externalStickerCacheAutoRefresh =
addConfig(
"ExternalStickerCacheAutoRefresh",
ConfigItem.configTypeBool,
false
)
val externalStickerCacheDirNameType =
addConfig(
"ExternalStickerCacheDirNameType",
ConfigItem.configTypeInt,
0
)

private fun addConfig(
k: String,
Expand Down

0 comments on commit 772dc55

Please sign in to comment.