Skip to content

Commit

Permalink
Fix FavoriteChannels sometimes not working
Browse files Browse the repository at this point in the history
  • Loading branch information
zt64 committed Sep 18, 2024
1 parent 8bcbd3f commit 971d394
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 65 deletions.
6 changes: 4 additions & 2 deletions plugin/FavoriteChannels/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
version = "1.1.0"
version = "1.1.1"
description = "Add your favorite channels to the top of the channel list for easy access."

aliucord.changelog.set(
"""
# 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: This update will reset your favorite channels*
*Note: There is currently a bug where channels will not show if the category they're in is collapsed*
# 1.0.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,52 @@
package dev.zt64.aliucord.plugins.favoritechannels

import android.content.Context
import android.content.res.ColorStateList
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.FragmentManager
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.instead
import com.aliucord.utils.RxUtils
import com.aliucord.utils.RxUtils.switchMap
import com.aliucord.wrappers.ChannelWrapper.Companion.id
import com.aliucord.wrappers.ChannelWrapper.Companion.isDM
import com.aliucord.wrappers.ChannelWrapper.Companion.parentId
import com.discord.api.channel.Channel
import com.discord.databinding.WidgetChannelsListItemActionsBinding
import com.discord.restapi.RestAPIParams
import com.discord.stores.StoreChannelsSelected
import com.discord.stores.StoreChannelsSelected.UserChannelSelection
import com.discord.stores.StoreGuildSelected
import com.discord.stores.StoreGuildSubscriptions
import com.discord.stores.StoreNavigation
import com.discord.stores.StoreStream
import com.discord.stores.StoreUserGuildSettings
import com.discord.utilities.collections.LeastRecentlyAddedSet
import com.discord.utilities.color.ColorCompat
import com.discord.utilities.persister.Persister
import com.discord.widgets.channels.list.WidgetChannelListModel
import com.discord.widgets.channels.list.`WidgetChannelListModel$Companion$guildListBuilder$$inlined$forEach$lambda$1$2`
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.items.ChannelListItemTextChannel
import com.discord.widgets.channels.list.items.ChannelListItemThread
import com.discord.widgets.guilds.list.GuildListItem
import com.discord.widgets.guilds.list.GuildListViewHolder
import com.discord.widgets.guilds.list.WidgetGuildListAdapter
import com.discord.widgets.guilds.list.`WidgetGuildListAdapter$onCreateViewHolder$1`
import com.discord.widgets.guilds.list.WidgetGuildsListViewModel
import com.lytefast.flexinput.R
import de.robv.android.xposed.XposedBridge
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

private const val FLAG_FAVORITE = 1 shl 11

@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
@AliucordPlugin(requiresRestart = true)
class FavoriteChannels : Plugin() {
private val getBindingMethod = WidgetChannelsListItemChannelActions::class.java
.getDeclaredMethod("getBinding")
.apply { isAccessible = true }

private fun WidgetChannelsListItemChannelActions.getBinding() = getBindingMethod(this) as WidgetChannelsListItemActionsBinding

init {
settingsTab = SettingsTab(PluginSettings::class.java, SettingsTab.Type.BOTTOM_SHEET).withArgs(settings)
}

override fun start(context: Context) {
val userGuildSettings = StoreStream.getUserGuildSettings()

StoreStream.getStoreChannelCategories().collapsedCategories
val getBindingMethod = WidgetChannelsListItemChannelActions::class.java
.getDeclaredMethod("getBinding")
.apply { isAccessible = true }

patcher.after<WidgetChannelsListItemChannelActions>(
"configureUI",
Expand All @@ -82,13 +55,13 @@ class FavoriteChannels : Plugin() {
// Only run in servers
if (model.channel.isDM()) return@after

val root = getBinding().root as NestedScrollView
val ctx = root.context

val flags = userGuildSettings.guildSettings[model.guild.id]
?.channelOverrides
?.find { it.channelId == model.channel.id }
?.flags ?: return@after
?.flags ?: 0

val root = (getBindingMethod(this) as WidgetChannelsListItemActionsBinding).root as ViewGroup
val ctx = root.context

(root.getChildAt(0) as LinearLayout).addView(
TextView(
Expand All @@ -98,7 +71,7 @@ class FavoriteChannels : Plugin() {
R.i.UiKit_Settings_Item_Icon
).apply {
if (flags and (1 shl 11) != 0) {
text = "Unfavorite"
text = "Remove from favorites"
setOnClickListener {
dismiss()

Expand All @@ -108,7 +81,7 @@ class FavoriteChannels : Plugin() {
model.guild.id,
RestAPIParams.UserGuildSettings(
model.channel.id,
RestAPIParams.UserGuildSettings.ChannelOverride(null, flags and (1 shl 11).inv())
RestAPIParams.UserGuildSettings.ChannelOverride(null, flags and (FLAG_FAVORITE).inv())
),
StoreUserGuildSettings.SettingsUpdateType.CHANNEL
)
Expand All @@ -125,7 +98,7 @@ class FavoriteChannels : Plugin() {
null
)
} else {
text = "Favorite"
text = "Add to favorites"
setOnClickListener {
dismiss()

Expand All @@ -135,7 +108,7 @@ class FavoriteChannels : Plugin() {
model.guild.id,
RestAPIParams.UserGuildSettings(
model.channel.id,
RestAPIParams.UserGuildSettings.ChannelOverride(null, flags or (1 shl 11))
RestAPIParams.UserGuildSettings.ChannelOverride(null, flags or FLAG_FAVORITE)
),
StoreUserGuildSettings.SettingsUpdateType.CHANNEL
)
Expand Down Expand Up @@ -166,7 +139,7 @@ class FavoriteChannels : Plugin() {
val favoriteChannels = userGuildSettings
.guildSettings[model.selectedGuild.id]
?.channelOverrides
?.mapNotNull { c -> c.channelId.takeIf { c.flags and (1 shl 11) != 0 } }
?.mapNotNull { c -> c.channelId.takeIf { c.flags and FLAG_FAVORITE != 0 } }

if (favoriteChannels.isNullOrEmpty()) return@before

Expand All @@ -190,15 +163,15 @@ class FavoriteChannels : Plugin() {
)
}

patcher.after<WidgetChannelsListAdapter>(
patcher.before<WidgetChannelsListAdapter>(
"onCreateViewHolder",
ViewGroup::class.java,
Int::class.java
) { (param, _: Any, type: Int) ->
param.result = when (type) {
ChannelListItemFavoriteCategory.type -> ItemFavoriteCategory(this)
ChannelListItemDivider.type -> ItemDivider(this)
else -> param.result
else -> return@before
}
}
}
Expand Down

This file was deleted.

0 comments on commit 971d394

Please sign in to comment.