Skip to content

Commit

Permalink
refactor: reformat old code to follow cleaner style
Browse files Browse the repository at this point in the history
  • Loading branch information
zt64 committed Oct 27, 2024
1 parent 29aba4b commit a83e56d
Show file tree
Hide file tree
Showing 33 changed files with 194 additions and 522 deletions.
40 changes: 0 additions & 40 deletions plugin/AccountSwitcher/src/main/kotlin/AccountSwitcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import accountswitcher.settings.PluginSettings
import android.content.Context
import com.aliucord.Utils
import com.aliucord.annotations.AliucordPlugin
import com.aliucord.api.PatcherAPI
import com.aliucord.api.SettingsAPI
import com.aliucord.entities.Plugin
import com.aliucord.patcher.after
import com.aliucord.patcher.instead
import com.discord.utilities.rest.RestAPI
import com.discord.widgets.settings.WidgetSettings
import de.robv.android.xposed.XC_MethodHook
import kotlin.properties.ReadOnlyProperty

@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
@AliucordPlugin
Expand All @@ -25,7 +21,6 @@ class AccountSwitcher : Plugin() {
lateinit var mSettings: SettingsAPI
}

@Suppress("SetTextI18n")
override fun start(context: Context) {
mSettings = settings

Expand Down Expand Up @@ -64,39 +59,4 @@ class AccountSwitcher : Plugin() {
}

override fun stop(context: Context) = patcher.unpatchAll()
}

open class Arguments {
var rawArguments: List<Any> = emptyList()
var argumentClasses: Array<Class<*>> = arrayOf()

private var size: Int = 0

fun <T> argument(clazz: Class<T>): ReadOnlyProperty<Any?, T> {
argumentClasses += clazz

val greg = ReadOnlyProperty<Any?, T> { _, _ ->
@Suppress("UNCHECKED_CAST")
rawArguments.getOrNull(argumentClasses.lastIndex) as T
}

return greg
}
}

class TestArgs : Arguments() {
val test by argument(String::class.java)
}

inline fun <reified T : Any, reified R> PatcherAPI.a(
method: String,
arguments: () -> R,
crossinline body: T.(param: XC_MethodHook.MethodHookParam, args: R) -> Unit
): Runnable where R : Arguments {
val args = arguments()

return after<T>(method, paramTypes = args.argumentClasses) {
args.rawArguments = it.args.asList()
body(it, args)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.lytefast.flexinput.R

@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
class SwitcherPage(private val accounts: ArrayList<Account>) : SettingsPage() {
@Suppress("SetTextI18n")
override fun onViewBound(view: View) {
super.onViewBound(view)

Expand Down Expand Up @@ -54,10 +53,7 @@ class SwitcherPage(private val accounts: ArrayList<Account>) : SettingsPage() {
Button(ctx).apply {
text = "Log Out"
setBackgroundColor(
view.resources.getColor(
R.c.uikit_btn_bg_color_selector_red,
view.context.theme
)
view.resources.getColor(R.c.uikit_btn_bg_color_selector_red, view.context.theme)
)
setOnClickListener { StoreStream.getAuthentication().setAuthed(null) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ class AccountAdapter(
if (it) saveAccounts()
}

fun removeAccount(token: String) = accounts
.removeIf {
it.token == token
}.also { if (it) saveAccounts() }
fun removeAccount(token: String) = accounts.removeIf {
it.token == token
}.also { if (it) saveAccounts() }

override fun getItemCount() = accounts.size

Expand All @@ -50,30 +49,24 @@ class AccountAdapter(
isSettings = isSettings
)

@Suppress("SetTextI18n")
override fun onBindViewHolder(holder: AccountViewHolder, position: Int): Unit =
accounts[position].let { account ->
Utils.threadPool.execute {
val user = StoreStream.getUsers().users[account.id] ?: RestAPI.api
.userGet(account.id)
.await()
.first
?.let { user -> CoreUser(user) }

holder.name.text = if (user == null) {
"Failed to load user"
} else {
UserUtils.INSTANCE.getUserNameWithDiscriminator(
user,
null,
null
)
}
holder.userId.text = "ID: ${user?.id ?: "Unknown"}"

IconUtils.setIcon(holder.avatar, user)
override fun onBindViewHolder(holder: AccountViewHolder, position: Int): Unit = accounts[position].let { account ->
Utils.threadPool.execute {
val user = StoreStream.getUsers().users[account.id] ?: RestAPI.api
.userGet(account.id)
.await()
.first
?.let(::CoreUser)

holder.name.text = if (user == null) {
"Failed to load user"
} else {
UserUtils.INSTANCE.getUserNameWithDiscriminator(user, null, null)
}
holder.userId.text = "ID: ${user?.id ?: "Unknown"}"

IconUtils.setIcon(holder.avatar, user)
}
}

fun onEdit(position: Int) {
AccountDialog(this, getAccounts()[position])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ private const val TOKEN_REGEX =
"""(mfa\.[a-z0-9_-]{20,})|([a-z0-9_-]{23,28}\.[a-z0-9_-]{6,7}\.([a-z0-9_-]{27,38}))"""

@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
class AccountDialog(private val adapter: AccountAdapter, private val account: Account? = null) :
InputDialog() {
class AccountDialog(private val adapter: AccountAdapter, private val account: Account? = null) : InputDialog() {
private val token = account?.token

private val buttonStates = arrayOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.lytefast.flexinput.R

@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
class PluginSettings(private val settings: SettingsAPI) : SettingsPage() {
@Suppress("SetTextI18n")
override fun onViewBound(view: View) {
super.onViewBound(view)

Expand Down Expand Up @@ -93,15 +92,11 @@ class PluginSettings(private val settings: SettingsAPI) : SettingsPage() {
text = "Add Current Account"
setOnClickListener {
when {
getAccounts().any { it.token == token } -> Utils.showToast(
"Account already added"
)

getAccounts().any { it.token == token } -> Utils.showToast("Account already added")
token != null -> {
accountAdapter.addAccount(token, StoreStream.getUsers().me.id)
Utils.showToast("Added current account")
}

else -> Utils.showToast("Failed to fetch token")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import java.util.concurrent.TimeUnit
object Patches {
private var settings: SettingsAPI = PluginManager.plugins["BetterMediaViewer"]?.settings!!

// Add a mute button to the media player
fun PatcherAPI.patchMuteButton() {
}

fun PatcherAPI.patchWidget() {
val downloadManager = Utils.appContext
.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
Expand Down Expand Up @@ -106,12 +110,9 @@ object Patches {
WindowInsetsControllerCompat(Utils.appActivity.window, binding.root).hide(
when (settings.getInt("immersiveModeType", 0)) {
0 -> WindowInsetsCompat.Type.statusBars()

1 -> WindowInsetsCompat.Type.navigationBars()

2 -> WindowInsetsCompat.Type.statusBars() or
WindowInsetsCompat.Type.statusBars()

else -> return@after
}
)
Expand All @@ -126,7 +127,7 @@ object Patches {
binding.root
.findViewById<AppBarLayout>(R.f.action_bar_toolbar_layout)
.layoutParams as FrameLayout.LayoutParams
).gravity = Gravity.BOTTOM
).gravity = Gravity.BOTTOM
}
}
}
Expand All @@ -152,12 +153,9 @@ object Patches {
ObservableExtensionsKt.ui(timer, this, null),
WidgetMedia::class.java,
null,
`WidgetMedia$showControls$1`(
this
),
`WidgetMedia$showControls$1`(this),
null,
{
},
{},
{ },
`WidgetMedia$showControls$2`(this)
)
Expand Down Expand Up @@ -203,7 +201,7 @@ object Patches {
(
widgetMedia.toolbarHeight.toFloat() /
widgetMedia.playerControlsHeight.toFloat()
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ import java.io.File
class PluginSettings(private val settings: SettingsAPI) : SettingsPage() {
private var launcher: ActivityResultLauncher<Intent>? = null

@Suppress("SetTextI18n")
override fun onViewBound(view: View) {
super.onViewBound(view)

val ctx = requireContext()

fun createCheckedSetting(
title: String,
subtitle: String,
setting: String,
defValue: Boolean
): CheckedSetting {
fun createCheckedSetting(title: String, subtitle: String, setting: String, defValue: Boolean): CheckedSetting {
return Utils
.createCheckedSetting(
ctx,
Expand Down Expand Up @@ -73,16 +67,14 @@ class PluginSettings(private val settings: SettingsAPI) : SettingsPage() {
progress = controlsTimeout - offset
12.dp.let { setPadding(it, 0, it, 0) }
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) =
with((progress / 100) * 100) {
seekBar.progress = this
currentTimeout.text = "${this + offset} ms"
}
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) = with((progress / 100) * 100) {
seekBar.progress = this
currentTimeout.text = "${this + offset} ms"
}

override fun onStartTrackingTouch(seekBar: SeekBar) {}

override fun onStopTrackingTouch(seekBar: SeekBar) =
settings.setInt("controlsTimeout", seekBar.progress + offset)
override fun onStopTrackingTouch(seekBar: SeekBar) = settings.setInt("controlsTimeout", seekBar.progress + offset)
})
}
val timeoutSection = LinearLayout(ctx, null, 0, R.i.UiKit_Settings_Item).apply {
Expand Down
14 changes: 2 additions & 12 deletions plugin/ChannelInvites/src/main/kotlin/ChannelInvites.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import android.content.Context
import android.view.View
import android.widget.LinearLayout
Expand All @@ -17,7 +16,6 @@ import com.lytefast.flexinput.R
@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
@AliucordPlugin
class ChannelInvites : Plugin() {
@Suppress("SetTextI18n")
override fun start(c: Context) {
val invitesLayoutId = View.generateViewId()
val scrollViewId = Utils.getResId("scroll_view", "id")
Expand All @@ -27,10 +25,7 @@ class ChannelInvites : Plugin() {
WidgetTextChannelSettings.Model::class.java
) {
val root = WidgetTextChannelSettings.`access$getBinding$p`(this).root
val content = root
.findViewById<NestedScrollView>(
scrollViewId
).getChildAt(0) as LinearLayout
val content = root.findViewById<NestedScrollView>(scrollViewId).getChildAt(0) as LinearLayout

if (content.findViewById<LinearLayout>(invitesLayoutId) != null) return@after

Expand All @@ -55,12 +50,7 @@ class ChannelInvites : Plugin() {
)!!
.mutate()
.apply {
setTint(
ColorCompat.getThemedColor(
context,
R.b.colorInteractiveNormal
)
)
setTint(ColorCompat.getThemedColor(context, R.b.colorInteractiveNormal))
}

text = "Invites"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import com.discord.widgets.servers.settings.invites.WidgetServerSettingsInstantI
import com.google.gson.stream.JsonReader

@Suppress("MISSING_DEPENDENCY_SUPERCLASS")
class InvitesPage(private val channel: Channel) :
AppFragment(Utils.getResId("widget_server_settings_instant_invites", "layout")) {
class InvitesPage(private val channel: Channel) : AppFragment(Utils.getResId("widget_server_settings_instant_invites", "layout")) {
private val viewFlipperId = Utils.getResId(
"server_settings_instant_invites_view_flipper",
"id"
Expand All @@ -49,16 +48,9 @@ class InvitesPage(private val channel: Channel) :
.execute()
.text()

val invites = InboundGatewayGsonParser.fromJson(
JsonReader(json.reader()),
Array<ModelInvite>::class.java
)
val invites = InboundGatewayGsonParser.fromJson(JsonReader(json.reader()), Array<ModelInvite>::class.java)
val inviteItems = invites.map { modelInvite ->
WidgetServerSettingsInstantInvites.Model.InviteItem(
modelInvite,
channel.guildId,
null
)
WidgetServerSettingsInstantInvites.Model.InviteItem(modelInvite, channel.guildId, null)
}

Utils.mainThread.post {
Expand All @@ -69,21 +61,14 @@ class InvitesPage(private val channel: Channel) :
invitesRecycler.run {
adapter = WidgetServerSettingsInstantInvites.Adapter(this).also { adapter ->
val onInviteSelectedListener = { modelInvite: ModelInvite ->
WidgetServerSettingsInstantInvitesActions.create(
parentFragmentManager,
modelInvite.code
)
WidgetServerSettingsInstantInvitesActions.create(parentFragmentManager, modelInvite.code)
}

val onInviteExpiredListener = { modelInvite: ModelInvite ->
StoreStream.getInstantInvites().onInviteRemoved(modelInvite)
}

adapter.configure(
inviteItems,
onInviteSelectedListener,
onInviteExpiredListener
)
adapter.configure(inviteItems, onInviteSelectedListener, onInviteExpiredListener)
}
layoutManager = LinearLayoutManager(context)
}
Expand Down
Loading

0 comments on commit a83e56d

Please sign in to comment.