Skip to content

Commit

Permalink
Updated CustomStatusPresets to V1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zt64 committed Dec 13, 2021
1 parent cdfed35 commit 1bf6881
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
15 changes: 13 additions & 2 deletions CustomStatusPresets/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
version = "1.2.1"
description = "Adds presets to the custom status menu. Long press on the save button to save a preset"
version = "1.3.0"
description = "Adds presets to the custom status menu. Long press on the save button to save a preset"

aliucord {
changelog.set(
"""
# 1.3.0
* Fixed overlapping views on the status page
* Fixed position changing after editing emoji
* Code cleanup
""".trimIndent()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.aliucord.Utils
import com.aliucord.annotations.AliucordPlugin
import com.aliucord.api.SettingsAPI
import com.aliucord.entities.Plugin
import com.aliucord.patcher.Hook
import com.aliucord.patcher.after
import com.aliucord.utils.DimenUtils.dp
import com.aliucord.views.Divider
import com.discord.models.domain.emoji.ModelEmojiCustom
Expand All @@ -29,7 +29,8 @@ import tk.zt64.plugins.customstatuspresets.PresetAdapter

@AliucordPlugin
class CustomStatusPresets : Plugin() {
private val presetType = TypeToken.getParameterized(ArrayList::class.java, UserStatusPresenceCustomView.ViewState.WithStatus::class.javaObjectType).getType()
private val presetType = TypeToken.getParameterized(ArrayList::class.java, UserStatusPresenceCustomView.ViewState.WithStatus::class.javaObjectType)
.getType()

companion object {
lateinit var mSettings: SettingsAPI
Expand All @@ -42,10 +43,9 @@ class CustomStatusPresets : Plugin() {
val statusExpirationId = Utils.getResId("set_custom_status_expiration", "id")
val saveButtonId = Utils.getResId("set_custom_status_save", "id")

patcher.patch(WidgetUserSetCustomStatus::class.java.getDeclaredMethod("onViewBound", View::class.java), Hook {
patcher.after<WidgetUserSetCustomStatus>("onViewBound", View::class.java) {
val rootView = it.args[0] as CoordinatorLayout
val widgetUserSetCustomStatus = it.thisObject as WidgetUserSetCustomStatus
val presetAdapter = PresetAdapter(widgetUserSetCustomStatus, settings.getObject("presets", ArrayList(), presetType))
val presetAdapter = PresetAdapter(this, settings.getObject("presets", ArrayList(), presetType))

with(rootView.findViewById<RadioGroup>(statusExpirationId).parent as LinearLayout) {
val ctx = this.context
Expand All @@ -57,20 +57,22 @@ class CustomStatusPresets : Plugin() {
addView(RecyclerView(ctx).apply {
adapter = presetAdapter
layoutManager = LinearLayoutManager(ctx)
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200.dp)
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 250.dp)
})
}

rootView.findViewById<FloatingActionButton>(saveButtonId).setOnLongClickListener {
val formState = (WidgetUserSetCustomStatus.`access$getViewModel$p`(widgetUserSetCustomStatus).viewState as WidgetUserSetCustomStatusViewModel.ViewState.Loaded).formState
val formState = (WidgetUserSetCustomStatus.`access$getViewModel$p`(this).viewState as WidgetUserSetCustomStatusViewModel.ViewState.Loaded).formState

if (formState.emoji == null && formState.text.isEmpty()) return@setOnLongClickListener false

val emoji = when (formState.emoji) {
is ModelEmojiUnicode -> UserStatusPresenceCustomView.Emoji(null, (formState.emoji as ModelEmojiUnicode).surrogates, false)
is ModelEmojiCustom -> StoreStream.getEmojis().getCustomEmojiInternal(formState.emoji.uniqueId.toLong()).let { emoji ->
UserStatusPresenceCustomView.Emoji(emoji.id.toString(), emoji.name, emoji.isAnimated)
}
is ModelEmojiCustom -> StoreStream.getEmojis()
.getCustomEmojiInternal(formState.emoji.uniqueId.toLong())
.let { emoji ->
UserStatusPresenceCustomView.Emoji(emoji.id.toString(), emoji.name, emoji.isAnimated)
}
else -> null
}

Expand All @@ -80,7 +82,10 @@ class CustomStatusPresets : Plugin() {
presetAdapter.notifyItemInserted(presetAdapter.itemCount)
true
}
})

// Hide the Wumpus image
rootView.getChildAt(2).visibility = View.GONE
}
}

override fun stop(context: Context) = patcher.unpatchAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.discord.utilities.color.ColorCompat
import com.discord.utilities.icon.IconUtils
import com.discord.utilities.images.MGImages
import com.discord.widgets.chat.input.emoji.EmojiPickerContextType
import com.discord.widgets.chat.input.emoji.EmojiPickerListener
import com.discord.widgets.chat.input.emoji.EmojiPickerNavigator
import com.discord.widgets.user.WidgetUserSetCustomStatus
import com.discord.widgets.user.profile.UserStatusPresenceCustomView
Expand All @@ -22,29 +23,30 @@ import com.lytefast.flexinput.R
import tk.zt64.plugins.CustomStatusPresets

class PresetAdapter(private val widgetUserSetCustomStatus: WidgetUserSetCustomStatus, private val presets: ArrayList<UserStatusPresenceCustomView.ViewState.WithStatus>) : RecyclerView.Adapter<PresetViewHolder>() {
private fun saveAccounts() = CustomStatusPresets.mSettings.setObject("presets", presets)
fun addPreset(preset: UserStatusPresenceCustomView.ViewState.WithStatus): Boolean = presets.add(preset).also { if (it) saveAccounts() }
private fun removePreset(preset: UserStatusPresenceCustomView.ViewState.WithStatus): Boolean = presets.remove(preset).also { if (it) saveAccounts() }

private val statusViewId = Utils.getResId("view_user_status_presence_custom", "layout")
private val emojiPreviewSize = Utils.getResId("custom_status_emoji_preview_size", "dimen")

private fun savePresets() = CustomStatusPresets.mSettings.setObject("presets", presets)
fun addPreset(preset: UserStatusPresenceCustomView.ViewState.WithStatus): Boolean = presets.add(preset)
.also { if (it) savePresets() }

private fun removePreset(preset: UserStatusPresenceCustomView.ViewState.WithStatus): Boolean = presets.remove(preset)
.also { if (it) savePresets() }

private fun setEmoji(customEmoji: SimpleDraweeView, emoji: UserStatusPresenceCustomView.Emoji) {
val ctx = customEmoji.context
var str: String? = null

if (emoji.id != null)
str = ModelEmojiCustom.getImageUri(emoji.id.toLong(), emoji.isAnimated, IconUtils.getMediaProxySize(ctx.resources.getDimensionPixelSize(emojiPreviewSize)))
else {
val modelEmojiUnicode = StoreStream.getEmojis().unicodeEmojiSurrogateMap[emoji.name]
if (modelEmojiUnicode != null) str = ModelEmojiUnicode.getImageUri(modelEmojiUnicode.codePoints, ctx)
val str = if (emoji.id != null)
ModelEmojiCustom.getImageUri(emoji.id.toLong(), emoji.isAnimated, IconUtils.getMediaProxySize(ctx.resources.getDimensionPixelSize(emojiPreviewSize)))
else StoreStream.getEmojis().unicodeEmojiSurrogateMap[emoji.name]?.let { modelEmojiUnicode ->
ModelEmojiUnicode.getImageUri(modelEmojiUnicode.codePoints, ctx)
}

MGImages.setImage(customEmoji, str)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
PresetViewHolder(this, LayoutInflater.from(parent.context).inflate(statusViewId, parent, false) as LinearLayout)
PresetViewHolder(this, LayoutInflater.from(parent.context)
.inflate(statusViewId, parent, false) as LinearLayout)

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: PresetViewHolder, position: Int) = presets[position].let {
Expand All @@ -66,29 +68,30 @@ class PresetAdapter(private val widgetUserSetCustomStatus: WidgetUserSetCustomSt
}

fun onClick(position: Int) = presets[position].let {
with (WidgetUserSetCustomStatus.`access$getViewModel$p`(widgetUserSetCustomStatus)) {
with(WidgetUserSetCustomStatus.`access$getViewModel$p`(widgetUserSetCustomStatus)) {
clearStatusTextAndEmoji()
if (it.statusText != null) setStatusText(it.statusText)
if (it.emoji != null)
setStatusEmoji(
if (it.emoji.id == null)
StoreStream.getEmojis().unicodeEmojiSurrogateMap[it.emoji.name]
else
StoreStream.getEmojis().getCustomEmojiInternal(it.emoji.id.toLong())
)
if (it.emoji != null) setStatusEmoji(if (it.emoji.id == null)
StoreStream.getEmojis().unicodeEmojiSurrogateMap[it.emoji.name]
else
StoreStream.getEmojis().getCustomEmojiInternal(it.emoji.id.toLong())
)
}
}

fun editEmoji(customEmoji: SimpleDraweeView, position: Int) = presets[position].let {
val function = { modelEmoji: Emoji ->
val emoji = StoreStream.getEmojis().getCustomEmojiInternal(modelEmoji.uniqueId.toLong()).let { modelEmojiCustom ->
UserStatusPresenceCustomView.Emoji(modelEmojiCustom.id.toString(), modelEmojiCustom.name, modelEmojiCustom.isAnimated)
}
val emojiPickerListener = EmojiPickerListener { modelEmoji: Emoji ->
val emoji = StoreStream.getEmojis()
.getCustomEmojiInternal(modelEmoji.uniqueId.toLong())
.let { modelEmojiCustom ->
UserStatusPresenceCustomView.Emoji(modelEmojiCustom.id.toString(), modelEmojiCustom.name, modelEmojiCustom.isAnimated)
}

presets[position] = it.copy(emoji, it.statusText)
savePresets()
setEmoji(customEmoji, emoji)
removePreset(it)
addPreset(it.copy(emoji, it.statusText))
Unit.a
}
EmojiPickerNavigator.launchBottomSheet(Utils.appActivity.supportFragmentManager, function, EmojiPickerContextType.Global.INSTANCE, null)

EmojiPickerNavigator.launchBottomSheet(Utils.appActivity.supportFragmentManager, emojiPickerListener, EmojiPickerContextType.Global.INSTANCE, null)
}
}

0 comments on commit 1bf6881

Please sign in to comment.