From 584ee96ee0edd09557cc94b566c014f47b4cf8c3 Mon Sep 17 00:00:00 2001 From: Vologhat <47892605+Vologhat@users.noreply.github.com> Date: Fri, 14 Jul 2023 01:35:23 +0300 Subject: [PATCH] fix #1406 --- .../aefyr/tsg/g2/TelegramStickersService.java | 2 + .../vtosters/hooks/MainActivityInjector.java | 3 + .../lite/ui/adapters/StickerPackAdapter.java | 8 ++- .../lite/ui/fragments/VTSettings.java | 62 +++++++------------ 4 files changed, 33 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java b/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java index ded64f57c3..1e1b927744 100755 --- a/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java +++ b/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java @@ -8,6 +8,7 @@ import com.aefyr.tsg.g2.sql.TelegramStickersDbHelper; import com.aefyr.tsg.g2.stickersgrabber.TelegramStickersGrabber; import com.aefyr.tsg.g2.stickersgrabber.TelegramStickersPackInfo; +import ru.vtosters.lite.ui.fragments.VTSettings; import ru.vtosters.lite.ui.fragments.tgstickers.StickersFragment; import java.io.File; @@ -152,6 +153,7 @@ private void notifyPackDownloadError(TelegramStickersPack pack, Exception error) private void notifyActivePacksListChanged() { getGlobalContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD)); + getGlobalContext().sendBroadcast(new Intent(VTSettings.ACTION_INVALIDATE_TGS_COUNT)); if (listeners.isEmpty()) return; diff --git a/app/src/main/java/ru/vtosters/hooks/MainActivityInjector.java b/app/src/main/java/ru/vtosters/hooks/MainActivityInjector.java index 53d84b2d5b..54a9026c51 100644 --- a/app/src/main/java/ru/vtosters/hooks/MainActivityInjector.java +++ b/app/src/main/java/ru/vtosters/hooks/MainActivityInjector.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.os.Build; import b.h.g.k.VKProgressDialog; +import com.aefyr.tsg.g2.TelegramStickersService; import com.vk.core.dialogs.alert.VkAlertDialog; import com.vtosters.lite.R; import ru.vtosters.hooks.other.Preferences; @@ -65,6 +66,8 @@ public static void inject(Activity activity) { InstallGMS.alert(activity); DisableBattery.alert(activity); // VKIDProtection.alert(activity); + //needs to show selected tgs pack count in settings after cold launch + TelegramStickersService.getInstance(activity); } private static void updateBinsAndTmpArchive(Activity activity) { diff --git a/app/src/main/java/ru/vtosters/lite/ui/adapters/StickerPackAdapter.java b/app/src/main/java/ru/vtosters/lite/ui/adapters/StickerPackAdapter.java index d36b3e3560..7e296563ef 100644 --- a/app/src/main/java/ru/vtosters/lite/ui/adapters/StickerPackAdapter.java +++ b/app/src/main/java/ru/vtosters/lite/ui/adapters/StickerPackAdapter.java @@ -1,6 +1,7 @@ package ru.vtosters.lite.ui.adapters; import android.annotation.SuppressLint; +import android.content.Intent; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; @@ -28,6 +29,7 @@ import ru.vtosters.lite.tgs.TGPref; import ru.vtosters.lite.tgs.TGRoot; import ru.vtosters.lite.ui.components.IItemMovingListener; +import ru.vtosters.lite.ui.fragments.VTSettings; import java.io.File; import java.util.ArrayList; @@ -138,7 +140,11 @@ public void bind(int position) { }); mName.setText(pack.title); mSwitch.setChecked(pack.enabled); - mSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> sService.setPackEnabled(pack, isChecked, true)); + mSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { + sService.setPackEnabled(pack, isChecked, false); + buttonView.getContext().sendBroadcast(new Intent(VTSettings.ACTION_INVALIDATE_TGS_COUNT)); + }); + mStickersCount.setText(pack.stickersCount + " " + mStickersCount.getContext().getString(R.string.stickerscount)); SwitchHook.setSwitchCompatColors(mSwitch, mSwitch.getContext()); diff --git a/app/src/main/java/ru/vtosters/lite/ui/fragments/VTSettings.java b/app/src/main/java/ru/vtosters/lite/ui/fragments/VTSettings.java index b3e2cca769..76360f9a54 100644 --- a/app/src/main/java/ru/vtosters/lite/ui/fragments/VTSettings.java +++ b/app/src/main/java/ru/vtosters/lite/ui/fragments/VTSettings.java @@ -1,13 +1,15 @@ package ru.vtosters.lite.ui.fragments; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import androidx.annotation.StringRes; import androidx.preference.Preference; -import com.aefyr.tsg.g2.TelegramStickersPack; import com.aefyr.tsg.g2.TelegramStickersService; import com.vk.about.AboutAppFragment; import com.vk.balance.BalanceFragment; @@ -42,7 +44,19 @@ import java.util.Locale; -public class VTSettings extends TrackedMaterialPreferenceToolbarFragment implements TelegramStickersService.StickersEventsListener { +public class VTSettings extends TrackedMaterialPreferenceToolbarFragment { + + final static public String ACTION_INVALIDATE_TGS_COUNT="com.vtosters.lite.intent.action.INVALIDATE_TGS_COUNT"; + + final BroadcastReceiver mTgsReceiver=new BroadcastReceiver() + { + @Override + public void onReceive(Context context, Intent intent) + { + if(ACTION_INVALIDATE_TGS_COUNT.equals(intent.getAction())) + findPreference("tgs_stickers").setSummary(getTGSsumm()); + } + }; public static String getValAsString(@StringRes int strRes, Boolean value) { if (value) { @@ -76,42 +90,12 @@ private void switchTheme(boolean isDarkTheme) { ThemesUtils.setTheme(isDarkTheme ? ThemesUtils.getDarkTheme() : ThemesUtils.getLightTheme(), requireActivity(), true); } - @Override - public void onPackAdded(TelegramStickersPack pack, int atIndex) { - - } - - @Override - public void onPackRemoved(TelegramStickersPack pack, int atIndex) { - - } - - @Override - public void onPackChanged(TelegramStickersPack pack, int atIndex) { - - } - - @Override - public void onPackDownloadError(TelegramStickersPack pack, Exception error) { - - } - - @Override - public void onActivePacksListChanged() { - findPreference("tgs_stickers").setSummary(getTGSsumm()); - } - - @Override - public void onInactivePacksListChanged() { - - } - @SuppressWarnings("ConstantConditions") @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); - TelegramStickersService.getInstance(requireContext()).addStickersEventsListener(this); + requireContext().registerReceiver(mTgsReceiver,new IntentFilter(ACTION_INVALIDATE_TGS_COUNT)); this.addPreferencesFromResource(R.xml.empty); @@ -590,18 +574,14 @@ public void onCreate(Bundle bundle) { } @Override - public void onResume() { - super.onResume(); + public void onDestroy() + { + requireContext().unregisterReceiver(mTgsReceiver); + super.onDestroy(); } @Override public int T4() { return R.string.notification_settings; } - - @Override - public void onDestroyView() { - super.onDestroyView(); - TelegramStickersService.getInstance(requireContext()).removeStickersEventsListener(this); - } }