Skip to content

Commit 4dc145b

Browse files
committed
Rework chat folder icon selection logic
1 parent 68ebc61 commit 4dc145b

File tree

6 files changed

+90
-49
lines changed

6 files changed

+90
-49
lines changed

app/src/main/java/org/thunderdog/challegram/config/Config.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Config {
3838
public static final boolean COLLAPSE_CHAT_FOLDER_HEADER_ON_SCROLL = CHAT_FOLDERS_REDESIGN;
3939
public static final boolean RESTRICT_HIDING_MAIN_LIST = true;
4040
public static final boolean SEARCH_MESSAGES_ONLY_IN_SELECTED_FOLDER = BuildConfig.EXPERIMENTAL;
41+
public static final boolean CHAT_FOLDERS_UNSET_DEFAULT_ICONS = false; // Until there's a fix on server
4142

4243
public static final boolean TEST_NEW_FEATURES_PROMPTS = false;
4344

app/src/main/java/org/thunderdog/challegram/telegram/Tdlib.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11952,18 +11952,58 @@ public String getMessageSenderTitle (TdApi.MessageSender sender) {
1195211952
}
1195311953
}
1195411954

11955+
public static <T extends TdApi.Object> T executeOrNull(TdApi.Function<T> query) {
11956+
try {
11957+
return Client.execute(query);
11958+
} catch (Client.ExecutionException e) {
11959+
return null;
11960+
}
11961+
}
11962+
1195511963
@Nullable
11956-
public TdApi.ChatFolderIcon chatFolderIcon (TdApi.ChatFolder chatFolder) {
11957-
if (chatFolder.icon != null && !StringUtils.isEmpty(chatFolder.icon.name)) {
11958-
return chatFolder.icon;
11964+
public TdApi.ChatFolderIcon defaultChatFolderIcon (TdApi.ChatFolder chatFolder) {
11965+
TdApi.ChatFolder checkChatFolder = chatFolder;
11966+
if (checkChatFolder.icon != null) {
11967+
checkChatFolder = Td.copyOf(checkChatFolder);
11968+
checkChatFolder.icon = null;
1195911969
}
11960-
TdApi.ChatFolderIcon result = clientExecuteT(new TdApi.GetChatFolderDefaultIconName(chatFolder), false);
11961-
if (result != null && !StringUtils.isEmpty(result.name)) {
11962-
return result;
11970+
TdApi.ChatFolderIcon defaultIcon = executeOrNull(new TdApi.GetChatFolderDefaultIconName(checkChatFolder));
11971+
if (!Td.isEmpty(defaultIcon)) {
11972+
return defaultIcon;
1196311973
}
1196411974
return null;
1196511975
}
1196611976

11977+
@Nullable
11978+
public TdApi.ChatFolderIcon chatFolderIcon (TdApi.ChatFolder chatFolder) {
11979+
if (!Td.isEmpty(chatFolder.icon)) {
11980+
return chatFolder.icon;
11981+
}
11982+
return defaultChatFolderIcon(chatFolder);
11983+
}
11984+
11985+
public void setChatFolderIcon (int chatFolderId, @Nullable TdApi.ChatFolderIcon icon, boolean unsetOnDefault) {
11986+
send(new TdApi.GetChatFolder(chatFolderId), (chatFolder, error) -> {
11987+
if (chatFolder != null) {
11988+
TdApi.ChatFolderIcon newIcon = icon;
11989+
if (!Td.isEmpty(newIcon) && unsetOnDefault) {
11990+
TdApi.ChatFolderIcon defaultIcon = defaultChatFolderIcon(chatFolder);
11991+
if (Td.equalsTo(newIcon, defaultIcon)) {
11992+
newIcon = null;
11993+
}
11994+
}
11995+
if (!Td.equalsTo(chatFolder.icon, newIcon)) {
11996+
chatFolder.icon = newIcon;
11997+
send(new TdApi.EditChatFolder(chatFolderId, chatFolder), (chatFolderInfo, setIconError) -> {
11998+
if (setIconError != null) {
11999+
UI.showError(setIconError);
12000+
}
12001+
});
12002+
}
12003+
}
12004+
});
12005+
}
12006+
1196712007
public String chatFolderIconName (TdApi.ChatFolder chatFolder) {
1196812008
TdApi.ChatFolderIcon icon = chatFolderIcon(chatFolder);
1196912009
return icon != null ? icon.name : "";

app/src/main/java/org/thunderdog/challegram/ui/ChatFolderIconSelector.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.widget.ImageView;
2323

2424
import androidx.annotation.Nullable;
25-
import androidx.core.util.ObjectsCompat;
2625
import androidx.recyclerview.widget.GridLayoutManager;
2726
import androidx.recyclerview.widget.RecyclerView;
2827

@@ -44,8 +43,8 @@
4443
import java.util.List;
4544

4645
import me.vkryl.android.widget.FrameLayoutFix;
47-
import me.vkryl.core.ObjectUtils;
4846
import me.vkryl.core.StringUtils;
47+
import me.vkryl.td.Td;
4948

5049
public class ChatFolderIconSelector {
5150

@@ -57,7 +56,7 @@ public class ChatFolderIconSelector {
5756

5857
private PopupLayout popupLayout;
5958

60-
public ChatFolderIconSelector (ViewController<?> owner, @Nullable String selectedIconName, Delegate delegate) {
59+
public ChatFolderIconSelector (ViewController<?> owner, @Nullable String selectedIconName, boolean isDefault, Delegate delegate) {
6160
this.context = owner.context();
6261
this.delegate = delegate;
6362
this.selectedIconName = selectedIconName;
@@ -106,7 +105,7 @@ protected void setCustom (ListItem item, SettingHolder holder, int position) {
106105
if (iconResource != 0) {
107106
imageView.setImageDrawable(Drawables.get(imageView.getResources(), iconResource));
108107
String iconName = item.getStringValue();
109-
boolean isSelected = ObjectsCompat.equals(iconName, selectedIconName);
108+
boolean isSelected = !isDefault && isSelectedIcon(iconName);
110109
int iconColorId = isSelected ? ColorId.iconActive : ColorId.icon;
111110
imageView.setColorFilter(Theme.getColor(iconColorId));
112111
owner.removeThemeListenerByTarget(imageView);
@@ -174,7 +173,7 @@ private boolean isSelectedIcon (@Nullable String iconName) {
174173
}
175174

176175
private static boolean isSameIcon (@Nullable String a, @Nullable String b) {
177-
return ObjectUtils.equals(a, b) || (isFolderIcon(a) && isFolderIcon(b));
176+
return StringUtils.equalsOrBothEmpty(a, b) || (isFolderIcon(a) && isFolderIcon(b));
178177
}
179178

180179
private static boolean isFolderIcon (@Nullable String iconName) {
@@ -189,8 +188,19 @@ default void onShow () {}
189188
default void onDismiss () {}
190189
}
191190

192-
public static ChatFolderIconSelector show (ViewController<?> owner, @Nullable String selectedIconName, Delegate delegate) {
193-
ChatFolderIconSelector selector = new ChatFolderIconSelector(owner, selectedIconName, delegate);
191+
public static ChatFolderIconSelector show (ViewController<?> owner, TdApi.ChatFolder chatFolder, Delegate delegate) {
192+
TdApi.ChatFolderIcon icon = chatFolder.icon;
193+
boolean isDefault = false;
194+
if (Td.isEmpty(icon)) {
195+
icon = owner.tdlib().chatFolderIcon(chatFolder);
196+
isDefault = true;
197+
}
198+
return show(owner, icon, isDefault, delegate);
199+
}
200+
201+
public static ChatFolderIconSelector show (ViewController<?> owner, @Nullable TdApi.ChatFolderIcon selectedIcon, boolean isDefault, Delegate delegate) {
202+
String iconName = selectedIcon != null && !StringUtils.isEmpty(selectedIcon.name) ? selectedIcon.name : null;
203+
ChatFolderIconSelector selector = new ChatFolderIconSelector(owner, iconName, isDefault || StringUtils.isEmpty(iconName), delegate);
194204
selector.show();
195205
return selector;
196206
}

app/src/main/java/org/thunderdog/challegram/ui/EditChatFolderController.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ public void onTextChanged (int id, ListItem item, MaterialEditTextGroup v) {
573573
private void onTitleChanged (String text) {
574574
editedChatFolder.title = text;
575575
updateDoneButton();
576+
updateFolderIcon();
576577
}
577578

578579
private void fillIncludedChats (TdApi.ChatFolder chatFolder, List<ListItem> outList) {
@@ -710,6 +711,7 @@ private void updateChatFolder (TdApi.ChatFolder chatFolder) {
710711
updateDoneButton();
711712
updateIncludedChats();
712713
updateExcludedChats();
714+
updateFolderIcon();
713715
}
714716

715717
private void updateIncludedChats () {
@@ -766,7 +768,6 @@ private void updateFolderName () {
766768
int includedChatType = includedChatTypes[0];
767769
String chatTypeName = Lang.getString(TD.chatTypeName(includedChatType));
768770
boolean isNameChanged = false;
769-
boolean isIconChanged = false;
770771
if (!StringUtils.equalsOrBothEmpty(editedChatFolder.title, chatTypeName)) {
771772
editedChatFolder.title = chatTypeName;
772773
isNameChanged = true;
@@ -777,19 +778,9 @@ private void updateFolderName () {
777778
headerCell.setInput(chatTypeName);
778779
}
779780
}
780-
if (editedChatFolder.icon == null) {
781-
TdApi.ChatFolderIcon chatTypeIcon = TD.chatTypeIcon(includedChatType);
782-
if (chatTypeIcon != null) {
783-
editedChatFolder.icon = chatTypeIcon;
784-
isIconChanged = true;
785-
}
786-
}
787-
if (input != null && (isNameChanged || isIconChanged)) {
781+
if (input != null && isNameChanged) {
788782
adapter.updateSimpleItemById(input.getId());
789783
}
790-
if (headerCell != null && isIconChanged) {
791-
updateFolderIcon();
792-
}
793784
}
794785

795786
@Override
@@ -861,6 +852,7 @@ private void showRemoveConditionConfirm (int position, ListItem item) {
861852
editedChatFolder.excludeArchived = false;
862853
}
863854
updateFolderName();
855+
updateFolderIcon();
864856
updateDoneButton();
865857
});
866858
}
@@ -934,7 +926,7 @@ private void updateFolderIcon () {
934926
adapter.updateSimpleItemById(input.getId());
935927
}
936928
if (headerCell != null) {
937-
int iconResource = TD.findFolderIcon(editedChatFolder.icon, R.drawable.baseline_folder_24);
929+
int iconResource = tdlib.chatFolderIconDrawable(editedChatFolder, R.drawable.baseline_folder_24);
938930
headerCell.setIcon(iconResource, ColorId.white);
939931
}
940932
}
@@ -1000,7 +992,7 @@ protected void setCustom (ListItem item, SettingHolder holder, int position) {
1000992
editText.setText(item.getCharSequenceValue());
1001993

1002994
ImageView imageView = holder.itemView.findViewById(android.R.id.icon);
1003-
int iconResource = TD.findFolderIcon(editedChatFolder.icon, R.drawable.baseline_folder_24);
995+
int iconResource = tdlib.chatFolderIconDrawable(editedChatFolder, R.drawable.baseline_folder_24);
1004996
imageView.setImageDrawable(Drawables.get(imageView.getResources(), iconResource));
1005997
}
1006998

@@ -1105,12 +1097,17 @@ public void onRemove (RecyclerView.ViewHolder viewHolder) {
11051097
}
11061098

11071099
private void showIconSelector () {
1108-
ChatFolderIconSelector.show(this, TD.getIconName(editedChatFolder), selectedIcon -> {
1109-
if (!Td.equalsTo(editedChatFolder.icon, selectedIcon)) {
1110-
editedChatFolder.icon = selectedIcon;
1111-
updateFolderIcon();
1112-
updateDoneButton();
1100+
ChatFolderIconSelector.show(this, editedChatFolder, selectedIcon -> {
1101+
TdApi.ChatFolderIcon newIcon = selectedIcon;
1102+
if (Config.CHAT_FOLDERS_UNSET_DEFAULT_ICONS) {
1103+
TdApi.ChatFolderIcon defaultIcon = tdlib.defaultChatFolderIcon(editedChatFolder);
1104+
if (Td.equalsTo(selectedIcon, defaultIcon)) {
1105+
newIcon = null;
1106+
}
11131107
}
1108+
editedChatFolder.icon = newIcon;
1109+
updateFolderIcon();
1110+
updateDoneButton();
11141111
});
11151112
}
11161113

app/src/main/java/org/thunderdog/challegram/ui/MainController.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,22 +2398,15 @@ private void showChatListOptions (TdApi.ChatList chatList) {
23982398
}
23992399
}));
24002400
} else if (id == R.id.btn_changeFolderIcon) {
2401-
ChatFolderIconSelector.show(this, TD.getIconName(chatFolderInfo), selectedIcon -> {
2402-
tdlib.send(new TdApi.GetChatFolder(chatFolderId), (chatFolder, getError) -> {
2403-
if (getError != null) {
2404-
UI.showError(getError);
2405-
} else {
2406-
if (!Td.equalsTo(chatFolder.icon, selectedIcon)) {
2407-
chatFolder.icon = selectedIcon;
2408-
tdlib.send(new TdApi.EditChatFolder(chatFolderId, chatFolder), (info, editError) -> {
2409-
if (editError != null) {
2410-
UI.showError(editError);
2411-
}
2412-
});
2413-
}
2414-
}
2415-
});
2416-
});
2401+
tdlib.send(new TdApi.GetChatFolder(chatFolderId), (chatFolder, error) -> runOnUiThreadOptional(() -> {
2402+
if (error != null) {
2403+
UI.showError(error);
2404+
} else {
2405+
ChatFolderIconSelector.show(this, chatFolder, selectedIcon ->
2406+
tdlib.setChatFolderIcon(chatFolderId, selectedIcon, Config.CHAT_FOLDERS_UNSET_DEFAULT_ICONS)
2407+
);
2408+
}
2409+
}));
24172410
} else if (id == R.id.btn_shareFolder) {
24182411
TdApi.ChatFolderInfo info = ObjectsCompat.requireNonNull(chatFolderInfo);
24192412
if (info.hasMyInviteLinks) {

vkryl/td

0 commit comments

Comments
 (0)