Skip to content

Commit

Permalink
Update FavoriteChannels to use API
Browse files Browse the repository at this point in the history
  • Loading branch information
zt64 committed Sep 16, 2024
1 parent 3b631c4 commit 8bcbd3f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 60 deletions.
8 changes: 7 additions & 1 deletion plugin/FavoriteChannels/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
version = "1.0.0"
version = "1.1.0"
description = "Add your favorite channels to the top of the channel list for easy access."

aliucord.changelog.set(
"""
# 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
- Fixed threads not moving along with their parent channels
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
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.api.SettingsAPI
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.wrappers.ChannelWrapper.Companion.guildId
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.google.gson.reflect.TypeToken
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
Expand All @@ -47,41 +70,10 @@ class FavoriteChannels : Plugin() {
settingsTab = SettingsTab(PluginSettings::class.java, SettingsTab.Type.BOTTOM_SHEET).withArgs(settings)
}

companion object {
private lateinit var mSettings: SettingsAPI

private val favoritesType by lazy {
object : TypeToken<Map<Long, List<Long>>>() {}.type
}

// Map of guilds to favorite channels
private val favorites: MutableMap<Long, MutableList<Long>> by lazy {
mSettings.getObject("favorites", mutableMapOf(), favoritesType)
}

fun favoriteChannel(channel: Channel) {
favorites
.getOrPut(channel.guildId) { mutableListOf() }
.add(channel.id)

saveSettings()
Util.updateChannels()
}

fun unfavoriteChannel(channel: Channel) {
favorites[channel.guildId]?.remove(channel.id)

saveSettings()
Util.updateChannels()
}

private fun saveSettings() {
mSettings.setObject("favorites", favorites)
}
}

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

StoreStream.getStoreChannelCategories().collapsedCategories

patcher.after<WidgetChannelsListItemChannelActions>(
"configureUI",
Expand All @@ -93,19 +85,33 @@ class FavoriteChannels : Plugin() {
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

(root.getChildAt(0) as LinearLayout).addView(
TextView(
ctx,
null,
0,
R.i.UiKit_Settings_Item_Icon
).apply {
if (model.channel.id in favorites[model.guild.id].orEmpty()) {
if (flags and (1 shl 11) != 0) {
text = "Unfavorite"
setOnClickListener {
dismiss()

unfavoriteChannel(model.channel)
StoreUserGuildSettings.`access$updateUserGuildSettings`(
StoreStream.getUserGuildSettings(),
ctx,
model.guild.id,
RestAPIParams.UserGuildSettings(
model.channel.id,
RestAPIParams.UserGuildSettings.ChannelOverride(null, flags and (1 shl 11).inv())
),
StoreUserGuildSettings.SettingsUpdateType.CHANNEL
)
}
setCompoundDrawablesWithIntrinsicBounds(
ContextCompat
Expand All @@ -123,7 +129,16 @@ class FavoriteChannels : Plugin() {
setOnClickListener {
dismiss()

favoriteChannel(model.channel)
StoreUserGuildSettings.`access$updateUserGuildSettings`(
StoreStream.getUserGuildSettings(),
ctx,
model.guild.id,
RestAPIParams.UserGuildSettings(
model.channel.id,
RestAPIParams.UserGuildSettings.ChannelOverride(null, flags or (1 shl 11))
),
StoreUserGuildSettings.SettingsUpdateType.CHANNEL
)
}
setCompoundDrawablesWithIntrinsicBounds(
ContextCompat
Expand All @@ -141,38 +156,38 @@ class FavoriteChannels : Plugin() {
)
}

@OptIn(ExperimentalStdlibApi::class)
patcher.before<WidgetChannelsList>(
"configureUI",
WidgetChannelListModel::class.java
) { (_, model: WidgetChannelListModel) ->
// Only run in servers and when there are favorites
if (!model.isGuildSelected) return@before

val favoriteChannels = favorites[model.selectedGuild.id]
val favoriteChannels = userGuildSettings
.guildSettings[model.selectedGuild.id]
?.channelOverrides
?.mapNotNull { c -> c.channelId.takeIf { c.flags and (1 shl 11) != 0 } }

if (favoriteChannels.isNullOrEmpty()) return@before

val channelItems = model.items.filter { item ->
item is ChannelListItemTextChannel || item is ChannelListItemThread
val channels = model.items.filter { item ->
when (item) {
is ChannelListItemTextChannel -> item.channel.id
is ChannelListItemThread -> item.channel.parentId
else -> return@filter false
} in favoriteChannels
}
val items = buildList(channelItems.size) {
add(ChannelListItemFavoriteCategory)

val channels = channelItems.filter {
if (it is ChannelListItemTextChannel) {
it.channel.id
} else {
(it as ChannelListItemThread).channel.parentId
} in favoriteChannels
}

model.items.removeAll(channels)
model.items.removeAll(channels)

addAll(channels)
} + ChannelListItemDivider

model.items.addAll(0, items)
model.items.addAll(
0,
listOf(
ChannelListItemFavoriteCategory,
*channels.toTypedArray(),
ChannelListItemDivider
)
)
}

patcher.after<WidgetChannelsListAdapter>(
Expand Down

0 comments on commit 8bcbd3f

Please sign in to comment.