| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.content.SharedPreferences; |
| 5 | |
| 6 | import org.telegram.messenger.ApplicationLoader; |
| 7 | import org.telegram.messenger.DialogObject; |
| 8 | import org.telegram.messenger.MessageObject; |
| 9 | import org.telegram.tgnet.TLRPC; |
| 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.Iterator; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | import xyz.nextalone.nagram.NaConfig; |
| 16 | |
| 17 | public class DialogConfig { |
| 18 | private static final SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nekodialogconfig", Context.MODE_PRIVATE); |
| 19 | |
| 20 | public static String getAutoTranslateKey(long dialogId, long topicId) { |
| 21 | return "autoTranslate_" + dialogId + (topicId != 0 ? "_" + topicId : ""); |
| 22 | } |
| 23 | |
| 24 | public static boolean isAutoTranslateEnable(long dialogId, long topicId) { |
| 25 | return preferences.getBoolean(getAutoTranslateKey(dialogId, topicId), NaConfig.INSTANCE.getAutoTranslate().Bool()); |
| 26 | } |
| 27 | |
| 28 | public static boolean hasAutoTranslateConfig(long dialogId, long topicId) { |
| 29 | return preferences.contains(getAutoTranslateKey(dialogId, topicId)); |
| 30 | } |
| 31 | |
| 32 | public static void setAutoTranslateEnable(long dialogId, long topicId, boolean enable) { |
| 33 | preferences.edit().putBoolean(getAutoTranslateKey(dialogId, topicId), enable).apply(); |
| 34 | } |
| 35 | |
| 36 | public static void removeAutoTranslateConfig(long dialogId, long topicId) { |
| 37 | preferences.edit().remove(getAutoTranslateKey(dialogId, topicId)).apply(); |
| 38 | } |
| 39 | |
| 40 | public static String getCustomForumTabsKey(long dialogId) { |
| 41 | return "customForumTabs_" + dialogId; |
| 42 | } |
| 43 | |
| 44 | public static boolean isCustomForumTabsEnable(long dialogId) { |
| 45 | return preferences.getBoolean(getCustomForumTabsKey(dialogId), false); |
| 46 | } |
| 47 | |
| 48 | public static boolean hasCustomForumTabsConfig(long dialogId) { |
| 49 | return preferences.contains(getCustomForumTabsKey(dialogId)); |
| 50 | } |
| 51 | |
| 52 | public static void setCustomForumTabsEnable(long dialogId, boolean enable) { |
| 53 | preferences.edit().putBoolean(getCustomForumTabsKey(dialogId), enable).apply(); |
| 54 | } |
| 55 | |
| 56 | public static void removeCustomForumTabsConfig(long dialogId) { |
| 57 | preferences.edit().remove(getCustomForumTabsKey(dialogId)).apply(); |
| 58 | } |
| 59 | |
| 60 | public static String getShareTargetKey(long dialogId) { |
| 61 | return "sharetarget_" + dialogId; |
| 62 | } |
| 63 | |
| 64 | public static boolean isShareTargetEnable(long dialogId) { |
| 65 | return preferences.getBoolean(getShareTargetKey(dialogId), false); |
| 66 | } |
| 67 | |
| 68 | public static boolean hasShareTargetConfig(long dialogId) { |
| 69 | return preferences.contains(getShareTargetKey(dialogId)); |
| 70 | } |
| 71 | |
| 72 | public static void setShareTargetEnable(long dialogId, boolean enable) { |
| 73 | preferences.edit().putBoolean(getShareTargetKey(dialogId), enable).apply(); |
| 74 | } |
| 75 | |
| 76 | public static void removeShareTargetConfig(long dialogId) { |
| 77 | preferences.edit().remove(getShareTargetKey(dialogId)).apply(); |
| 78 | } |
| 79 | |
| 80 | public static ArrayList<Long> getShareTargetAll(boolean enable) { |
| 81 | ArrayList<Long> resultList = new ArrayList<>(); |
| 82 | |
| 83 | Map<String, ?> allEntries = preferences.getAll(); |
| 84 | for (Map.Entry<String, ?> entry : allEntries.entrySet()) { |
| 85 | String key = entry.getKey(); |
| 86 | Object value = entry.getValue(); |
| 87 | |
| 88 | if (key.startsWith("sharetarget_") && value instanceof Boolean && ((Boolean) value == enable)) { |
| 89 | try { |
| 90 | String numberPart = key.substring("sharetarget_".length()); |
| 91 | long longValue = Long.parseLong(numberPart); |
| 92 | resultList.add(longValue); |
| 93 | } catch (NumberFormatException e) { |
| 94 | e.printStackTrace(); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return resultList; |
| 99 | } |
| 100 | |
| 101 | public static ArrayList<TLRPC.TL_topPeer> getShareTargetUpdate() { |
| 102 | ArrayList<Long> ids = getShareTargetAll(true); |
| 103 | ArrayList<TLRPC.TL_topPeer> resultList = new ArrayList<>(); |
| 104 | |
| 105 | for (Long peerId : ids) { |
| 106 | if (!DialogObject.isUserDialog(peerId)) { |
| 107 | continue; |
| 108 | } |
| 109 | TLRPC.TL_peerUser peer = new TLRPC.TL_peerUser(); |
| 110 | peer.user_id = peerId; |
| 111 | TLRPC.TL_topPeer topPeer = new TLRPC.TL_topPeer(); |
| 112 | topPeer.peer = peer; |
| 113 | topPeer.rating = 0; |
| 114 | resultList.add(topPeer); |
| 115 | } |
| 116 | return resultList; |
| 117 | } |
| 118 | |
| 119 | public static void modifyShareTarget(ArrayList<TLRPC.TL_topPeer> hintsFinal) { |
| 120 | ArrayList<Long> toDelete = getShareTargetAll(false); |
| 121 | ArrayList<TLRPC.TL_topPeer> toAdd = getShareTargetUpdate(); |
| 122 | |
| 123 | if (!toDelete.isEmpty()) { |
| 124 | Iterator<TLRPC.TL_topPeer> iterator = hintsFinal.iterator(); |
| 125 | while (iterator.hasNext()) { |
| 126 | TLRPC.TL_topPeer peer = iterator.next(); |
| 127 | if (peer.peer != null && toDelete.contains(MessageObject.getPeerId(peer.peer))) { |
| 128 | iterator.remove(); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (!toAdd.isEmpty()) { |
| 134 | hintsFinal.addAll(0, toAdd); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |