Skip to content

Commit

Permalink
Fix FavoriteChannels bug resulting in channels being hidden if their …
Browse files Browse the repository at this point in the history
…parent category is collapsed
  • Loading branch information
zt64 committed Oct 1, 2024
1 parent 971d394 commit a247f0b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
13 changes: 7 additions & 6 deletions plugin/FavoriteChannels/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
version = "1.1.1"
version = "1.1.2"
description = "Add your favorite channels to the top of the channel list for easy access."

aliucord.changelog.set(
"""
# 1.1.2
- Fixed favorited channels being hidden if their parent category is collapsed
# 1.1.1
- Fixed improper handling of channels causing the button to not get displayed
# 1.1.0
- Switched to using Discord's API for syncing favorite channels
*Note: There is currently a bug where channels will not show if the category they're in is collapsed*
# 1.0.1
- Fixed threads not moving along with their parent channels
# 1.0.0
- Initial release
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MISSING_DEPENDENCY_CLASS")

package dev.zt64.aliucord.plugins.favoritechannels

import android.content.Context
Expand All @@ -7,11 +9,8 @@ import android.widget.TextView
import androidx.core.content.ContextCompat
import com.aliucord.annotations.AliucordPlugin
import com.aliucord.entities.Plugin
import com.aliucord.patcher.after
import com.aliucord.patcher.before
import com.aliucord.patcher.component1
import com.aliucord.patcher.component2
import com.aliucord.patcher.component3
import com.aliucord.patcher.*
import com.aliucord.wrappers.ChannelWrapper.Companion.guildId
import com.aliucord.wrappers.ChannelWrapper.Companion.id
import com.aliucord.wrappers.ChannelWrapper.Companion.isDM
import com.aliucord.wrappers.ChannelWrapper.Companion.parentId
Expand All @@ -20,17 +19,11 @@ import com.discord.restapi.RestAPIParams
import com.discord.stores.StoreStream
import com.discord.stores.StoreUserGuildSettings
import com.discord.utilities.color.ColorCompat
import com.discord.widgets.channels.list.WidgetChannelListModel
import com.discord.widgets.channels.list.WidgetChannelsList
import com.discord.widgets.channels.list.WidgetChannelsListAdapter
import com.discord.widgets.channels.list.WidgetChannelsListItemChannelActions
import com.discord.widgets.channels.list.*
import com.discord.widgets.channels.list.items.ChannelListItemTextChannel
import com.discord.widgets.channels.list.items.ChannelListItemThread
import com.lytefast.flexinput.R
import dev.zt64.aliucord.plugins.favoritechannels.items.ChannelListItemDivider
import dev.zt64.aliucord.plugins.favoritechannels.items.ChannelListItemFavoriteCategory
import dev.zt64.aliucord.plugins.favoritechannels.items.ItemDivider
import dev.zt64.aliucord.plugins.favoritechannels.items.ItemFavoriteCategory
import dev.zt64.aliucord.plugins.favoritechannels.items.*

private const val FLAG_FAVORITE = 1 shl 11

Expand All @@ -48,6 +41,7 @@ class FavoriteChannels : Plugin() {
.getDeclaredMethod("getBinding")
.apply { isAccessible = true }

// Add favorite/unfavorite button to the channel item context menu
patcher.after<WidgetChannelsListItemChannelActions>(
"configureUI",
WidgetChannelsListItemChannelActions.Model::class.java
Expand All @@ -56,8 +50,7 @@ class FavoriteChannels : Plugin() {
if (model.channel.isDM()) return@after

val flags = userGuildSettings.guildSettings[model.guild.id]
?.channelOverrides
?.find { it.channelId == model.channel.id }
?.getChannelOverride(model.channel.id)
?.flags ?: 0

val root = (getBindingMethod(this) as WidgetChannelsListItemActionsBinding).root as ViewGroup
Expand All @@ -70,7 +63,7 @@ class FavoriteChannels : Plugin() {
0,
R.i.UiKit_Settings_Item_Icon
).apply {
if (flags and (1 shl 11) != 0) {
if (flags and FLAG_FAVORITE != 0) {
text = "Remove from favorites"
setOnClickListener {
dismiss()
Expand Down Expand Up @@ -174,6 +167,31 @@ class FavoriteChannels : Plugin() {
else -> return@before
}
}

// This unholy patch makes it so that collapsed categories do not remove channels if they are favorited
@Suppress("CAST_NEVER_SUCCEEDS")
patcher.instead<`WidgetChannelListModel$Companion$guildListBuilder$$inlined$forEach$lambda$1$2`>("invoke") {
with((`this$0` as `WidgetChannelListModel$Companion$guildListBuilder$$inlined$forEach$lambda$1`)) {
val noMentions = `$mentionCount` <= 0
val isCollapsed = `$textChannel`.parentId in `$collapsedCategories$inlined`
val isChannelOrChildFavorited = userGuildSettings.guildSettings[`$textChannel`.guildId]
?.getChannelOverride(`$textChannelId`)
?.let { it.flags and FLAG_FAVORITE != 0 } ?: false
val isChannelOrChildSelected = `$channelSelected` ||
(`$areAnyChildThreadsSelected$5$inlined` as `WidgetChannelListModel$Companion$guildListBuilder$5`)
.invoke(`$textChannel`.id)
val areAllChildThreadsRead = (`$areAllChildThreadsRead$4$inlined` as `WidgetChannelListModel$Companion$guildListBuilder$4`)
.invoke(`$textChannel`.id)
val shouldHideChannel = (isCollapsed && noMentions && (`$isCategoryMuted` || `$isMuted` || !`$unread`)) ||
(`$isMuted` && `$guild$inlined`.hideMutedChannels)
val shouldBeHidden =
!(isChannelOrChildFavorited || isChannelOrChildSelected || !areAllChildThreadsRead || !shouldHideChannel)

if (shouldBeHidden) `$hiddenChannelsIds$inlined`.add(`$textChannelId`)

shouldBeHidden
}
}
}

override fun stop(context: Context) = patcher.unpatchAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object ChannelListItemDivider : ChannelListItem {

class ItemDivider(adapter: WidgetChannelsListAdapter) :
WidgetChannelsListAdapter.Item(Utils.getResId("widget_channels_list_item_stage_events_separator", "layout"), adapter) {
init {
itemView.layoutParams.height = 2.dp
}
}
init {
itemView.layoutParams.height = 2.dp
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ object ChannelListItemFavoriteCategory : ChannelListItem {

class ItemFavoriteCategory(adapter: WidgetChannelsListAdapter) :
WidgetChannelsListAdapter.Item(Utils.getResId("widget_channels_list_item_category", "layout"), adapter) {
private val nameView = itemView.findViewById<TextView>(
Utils.getResId("channels_item_category_name", "id")
)
private val nameView = itemView.findViewById<TextView>(
Utils.getResId("channels_item_category_name", "id")
)

init {
itemView
.findViewById<ImageView>(Utils.getResId("channels_item_category_add", "id"))
.visibility = View.GONE
init {
itemView
.findViewById<ImageView>(Utils.getResId("channels_item_category_add", "id"))
.visibility = View.GONE

itemView
.findViewById<ImageView>(Utils.getResId("channels_item_category_arrow", "id"))
.visibility = View.INVISIBLE
itemView
.findViewById<ImageView>(Utils.getResId("channels_item_category_arrow", "id"))
.visibility = View.INVISIBLE

nameView.text = "Favorites"
}
nameView.text = "Favorites"
}

override fun onConfigure(i: Int, channelListItem: ChannelListItem) {
super.onConfigure(i, channelListItem)
override fun onConfigure(i: Int, channelListItem: ChannelListItem) {
super.onConfigure(i, channelListItem)

channelListItem as ChannelListItemFavoriteCategory
}
}
channelListItem as ChannelListItemFavoriteCategory
}
}

0 comments on commit a247f0b

Please sign in to comment.