| 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 final int FORUM_LAYOUT_DEFAULT = 0; |
| 41 | public static final int FORUM_LAYOUT_TABS = 1; |
| 42 | public static final int FORUM_LAYOUT_LIST = 2; |
| 43 | public static final int FORUM_LAYOUT_SIDEBAR = 3; |
| 44 | |
| 45 | public static String getCustomForumTabsKey(long dialogId) { |
| 46 | return "customForumTabs_" + dialogId; |
| 47 | } |
| 48 | |
| 49 | private static String getCustomForumTabsModeKey(long dialogId) { |
| 50 | return "customForumTabsMode_" + dialogId; |
| 51 | } |
| 52 | |
| 53 | public static int getCustomForumTabsMode(long dialogId) { |
| 54 | final String key = getCustomForumTabsModeKey(dialogId); |
| 55 | if (preferences.contains(key)) { |
| 56 | return preferences.getInt(key, FORUM_LAYOUT_DEFAULT); |
| 57 | } |
| 58 | // migrate the old boolean config: true meant tabs, false meant list |
| 59 | final String oldKey = getCustomForumTabsKey(dialogId); |
| 60 | if (preferences.contains(oldKey)) { |
| 61 | final int mode = preferences.getBoolean(oldKey, false) ? FORUM_LAYOUT_TABS : FORUM_LAYOUT_LIST; |
| 62 | preferences.edit().putInt(key, mode).remove(oldKey).apply(); |
| 63 | return mode; |
| 64 | } |
| 65 | return FORUM_LAYOUT_DEFAULT; |
| 66 | } |
| 67 | |
| 68 | public static void setCustomForumTabsMode(long dialogId, int mode) { |
| 69 | final SharedPreferences.Editor editor = preferences.edit().remove(getCustomForumTabsKey(dialogId)); |
| 70 | if (mode == FORUM_LAYOUT_DEFAULT) { |
| 71 | editor.remove(getCustomForumTabsModeKey(dialogId)); |
| 72 | } else { |
| 73 | editor.putInt(getCustomForumTabsModeKey(dialogId), mode); |
| 74 | } |
| 75 | editor.apply(); |
| 76 | } |
| 77 | |
| 78 | public static void removeCustomForumTabsConfig(long dialogId) { |
| 79 | preferences.edit().remove(getCustomForumTabsModeKey(dialogId)).remove(getCustomForumTabsKey(dialogId)).apply(); |
| 80 | } |
| 81 | |
| 82 | public static String getShareTargetKey(long dialogId) { |
| 83 | return "sharetarget_" + dialogId; |
| 84 | } |
| 85 | |
| 86 | public static boolean isShareTargetEnable(long dialogId) { |
| 87 | return preferences.getBoolean(getShareTargetKey(dialogId), false); |
| 88 | } |
| 89 | |
| 90 | public static boolean hasShareTargetConfig(long dialogId) { |
| 91 | return preferences.contains(getShareTargetKey(dialogId)); |
| 92 | } |
| 93 | |
| 94 | public static void setShareTargetEnable(long dialogId, boolean enable) { |
| 95 | preferences.edit().putBoolean(getShareTargetKey(dialogId), enable).apply(); |
| 96 | } |
| 97 | |
| 98 | public static void removeShareTargetConfig(long dialogId) { |
| 99 | preferences.edit().remove(getShareTargetKey(dialogId)).apply(); |
| 100 | } |
| 101 | |
| 102 | public static ArrayList<Long> getShareTargetAll(boolean enable) { |
| 103 | ArrayList<Long> resultList = new ArrayList<>(); |
| 104 | |
| 105 | Map<String, ?> allEntries = preferences.getAll(); |
| 106 | for (Map.Entry<String, ?> entry : allEntries.entrySet()) { |
| 107 | String key = entry.getKey(); |
| 108 | Object value = entry.getValue(); |
| 109 | |
| 110 | if (key.startsWith("sharetarget_") && value instanceof Boolean && ((Boolean) value == enable)) { |
| 111 | try { |
| 112 | String numberPart = key.substring("sharetarget_".length()); |
| 113 | long longValue = Long.parseLong(numberPart); |
| 114 | resultList.add(longValue); |
| 115 | } catch (NumberFormatException e) { |
| 116 | e.printStackTrace(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return resultList; |
| 121 | } |
| 122 | |
| 123 | public static ArrayList<TLRPC.TL_topPeer> getShareTargetUpdate() { |
| 124 | ArrayList<Long> ids = getShareTargetAll(true); |
| 125 | ArrayList<TLRPC.TL_topPeer> resultList = new ArrayList<>(); |
| 126 | |
| 127 | for (Long peerId : ids) { |
| 128 | if (!DialogObject.isUserDialog(peerId)) { |
| 129 | continue; |
| 130 | } |
| 131 | TLRPC.TL_peerUser peer = new TLRPC.TL_peerUser(); |
| 132 | peer.user_id = peerId; |
| 133 | TLRPC.TL_topPeer topPeer = new TLRPC.TL_topPeer(); |
| 134 | topPeer.peer = peer; |
| 135 | topPeer.rating = 0; |
| 136 | resultList.add(topPeer); |
| 137 | } |
| 138 | return resultList; |
| 139 | } |
| 140 | |
| 141 | public static void modifyShareTarget(ArrayList<TLRPC.TL_topPeer> hintsFinal) { |
| 142 | ArrayList<Long> toDelete = getShareTargetAll(false); |
| 143 | ArrayList<TLRPC.TL_topPeer> toAdd = getShareTargetUpdate(); |
| 144 | |
| 145 | if (!toDelete.isEmpty()) { |
| 146 | Iterator<TLRPC.TL_topPeer> iterator = hintsFinal.iterator(); |
| 147 | while (iterator.hasNext()) { |
| 148 | TLRPC.TL_topPeer peer = iterator.next(); |
| 149 | if (peer.peer != null && toDelete.contains(MessageObject.getPeerId(peer.peer))) { |
| 150 | iterator.remove(); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (!toAdd.isEmpty()) { |
| 156 | hintsFinal.addAll(0, toAdd); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |