| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import org.apache.commons.lang3.StringUtils; |
| 4 | import org.telegram.messenger.MessagesController; |
| 5 | import org.telegram.tgnet.ConnectionsManager; |
| 6 | import org.telegram.tgnet.TLRPC; |
| 7 | |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.Collections; |
| 10 | import java.util.List; |
| 11 | import java.util.concurrent.ConcurrentHashMap; |
| 12 | import java.util.stream.Collectors; |
| 13 | |
| 14 | /** |
| 15 | * @author luvletter2333 |
| 16 | */ |
| 17 | public class PinnedStickerHelper { |
| 18 | private static final ConcurrentHashMap<Integer, PinnedStickerHelper> _instances = new ConcurrentHashMap<>(); |
| 19 | |
| 20 | public static PinnedStickerHelper getInstance(int accountNum) { |
| 21 | PinnedStickerHelper obj = _instances.get(accountNum); |
| 22 | if (obj != null) |
| 23 | return obj; |
| 24 | synchronized (PinnedStickerHelper.class) { |
| 25 | if (obj == null) { |
| 26 | obj = new PinnedStickerHelper(accountNum); |
| 27 | _instances.put(accountNum, obj); |
| 28 | } |
| 29 | } |
| 30 | return obj; |
| 31 | } |
| 32 | |
| 33 | public List<Long> pinnedList; |
| 34 | public final int accountNum; |
| 35 | |
| 36 | private PinnedStickerHelper(int accountNum) { |
| 37 | this.accountNum = accountNum; |
| 38 | String[] configs = MessagesController.getMainSettings(accountNum).getString("pinnedStickers", "").split(","); |
| 39 | this.pinnedList = new ArrayList<>(configs.length); |
| 40 | boolean hasInvalid = false; |
| 41 | for (String str : configs) { |
| 42 | if (str.isEmpty()) |
| 43 | continue; |
| 44 | try { |
| 45 | this.pinnedList.add(Long.parseLong(str)); |
| 46 | } catch (NumberFormatException e) { |
| 47 | hasInvalid = true; |
| 48 | } |
| 49 | } |
| 50 | if (hasInvalid) |
| 51 | updateConfig(); |
| 52 | } |
| 53 | |
| 54 | private void updateConfig() { |
| 55 | MessagesController.getMainSettings(accountNum) |
| 56 | .edit() |
| 57 | .putString("pinnedStickers", StringUtils.join(this.pinnedList, ",")) |
| 58 | .apply(); |
| 59 | } |
| 60 | |
| 61 | public boolean isPinned(long sticker_id) { |
| 62 | return this.pinnedList.contains(sticker_id); |
| 63 | } |
| 64 | |
| 65 | public List<Long> pinNewSticker(long stickerID) { |
| 66 | if (!this.pinnedList.contains(stickerID)) { |
| 67 | this.pinnedList.add(0, stickerID); |
| 68 | updateConfig(); |
| 69 | } |
| 70 | return this.pinnedList; |
| 71 | } |
| 72 | |
| 73 | public void removePinnedStickerLocal(long stickerID) { |
| 74 | this.pinnedList.remove(stickerID); |
| 75 | updateConfig(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * This method will reorder pinned Stickers in given list, according to the order of the local pinnedList |
| 80 | * This method accepts a list of TLRPC.StickerSet |
| 81 | * <p> |
| 82 | * PLEASE KEEP THESE TWO METHODS WITH THE SAME BEHAVIOR |
| 83 | * |
| 84 | * @param stickerSets this list will be reordered |
| 85 | * @param removeLocal if true, stickerSets in local but not in given list will be removed from local pinned list |
| 86 | * @return return whether a syncing is needed |
| 87 | **/ |
| 88 | public synchronized boolean reorderPinnedStickersForSS(List<TLRPC.StickerSet> stickerSets, boolean removeLocal) { |
| 89 | if (removeLocal) { |
| 90 | boolean removed = this.pinnedList.removeIf(id -> stickerSets.stream().noneMatch(ss -> ss.id == id)); |
| 91 | if (removed && stickerSets.size() > 0) // prevent poor network causing losing pinned lsit |
| 92 | updateConfig(); |
| 93 | } |
| 94 | List<TLRPC.StickerSet> pinnedList = stickerSets.stream() |
| 95 | .filter(ss -> isPinned(ss.id)) |
| 96 | .sorted((o1, o2) -> this.pinnedList.indexOf(o1.id) - this.pinnedList.indexOf(o2.id)) |
| 97 | .collect(Collectors.toList()); |
| 98 | boolean needSync = !isOrdered(stickerSets, pinnedList); |
| 99 | if (needSync) { // order is not correct |
| 100 | stickerSets.removeAll(pinnedList); |
| 101 | stickerSets.addAll(0, pinnedList); |
| 102 | } |
| 103 | return needSync; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * This method will reorder pinned Stickers in given list, according to the order of the local pinnedList |
| 108 | * This method accepts a list of TLRPC.TL_messages_stickerSet |
| 109 | * <p> |
| 110 | * PLEASE KEEP THESE TWO METHODS WITH THE SAME BEHAVIOR |
| 111 | * |
| 112 | * @param stickerSets this list will be reordered |
| 113 | * @param removeLocal if true, stickerSets in local but not in given list will be removed from local pinned list |
| 114 | * @return return whether a syncing is needed |
| 115 | **/ |
| 116 | public synchronized boolean reorderPinnedStickers(List<TLRPC.TL_messages_stickerSet> stickerSets, boolean removeLocal) { |
| 117 | if (removeLocal) { |
| 118 | boolean removed = this.pinnedList.removeIf(id -> stickerSets.stream().noneMatch(TLM_ss -> TLM_ss.set.id == id)); |
| 119 | if (removed && stickerSets.size() > 0) // prevent poor network causing losing pinned lsit |
| 120 | updateConfig(); |
| 121 | } |
| 122 | List<TLRPC.TL_messages_stickerSet> pinnedList = stickerSets.stream() |
| 123 | .filter(TLM_stickerSet -> isPinned(TLM_stickerSet.set.id)) |
| 124 | .sorted((o1, o2) -> this.pinnedList.indexOf(o1.set.id) - this.pinnedList.indexOf(o2.set.id)) |
| 125 | .collect(Collectors.toList()); |
| 126 | boolean needSync = !isOrdered(stickerSets, pinnedList); |
| 127 | if (needSync) { // order is not correct |
| 128 | stickerSets.removeAll(pinnedList); |
| 129 | stickerSets.addAll(0, pinnedList); |
| 130 | } |
| 131 | return needSync; |
| 132 | } |
| 133 | |
| 134 | public boolean reorderPinnedStickers(List<TLRPC.TL_messages_stickerSet> stickerSets) { |
| 135 | return reorderPinnedStickers(stickerSets, true); |
| 136 | } |
| 137 | |
| 138 | public void swap(int index1, int index2) { |
| 139 | if (index1 >= 0 && index1 < pinnedList.size() && index2 >= 0 && index2 < pinnedList.size()) { |
| 140 | Collections.swap(pinnedList, index1, index2); |
| 141 | updateConfig(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * check whether list2 is included in list1, with the same order, starting from list1.get(0) |
| 147 | */ |
| 148 | private static boolean isOrdered(List list1, List list2) { |
| 149 | for (int i = 0; i < list2.size(); i++) |
| 150 | if (!list1.get(i).equals(list2.get(i))) |
| 151 | return false; |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | public void sendOrderSyncForSS(List<TLRPC.StickerSet> stickerSetIDs) { |
| 156 | TLRPC.TL_messages_reorderStickerSets req = new TLRPC.TL_messages_reorderStickerSets(); |
| 157 | req.masks = false; // only image sticker |
| 158 | for (int a = 0; a < stickerSetIDs.size(); a++) { |
| 159 | req.order.add(stickerSetIDs.get(a).id); |
| 160 | } |
| 161 | ConnectionsManager.getInstance(accountNum).sendRequest(req, (response, error) -> { |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | public void sendOrderSync(List<TLRPC.TL_messages_stickerSet> stickerSetIDs) { |
| 166 | TLRPC.TL_messages_reorderStickerSets req = new TLRPC.TL_messages_reorderStickerSets(); |
| 167 | req.masks = false; // only image sticker |
| 168 | for (int a = 0; a < stickerSetIDs.size(); a++) { |
| 169 | req.order.add(stickerSetIDs.get(a).set.id); |
| 170 | } |
| 171 | ConnectionsManager.getInstance(accountNum).sendRequest(req, (response, error) -> { |
| 172 | }); |
| 173 | } |
| 174 | } |
| 175 | |