fix(core): old bitmoji selfie

This commit is contained in:
rhunk
2023-10-29 13:17:09 +01:00
parent a879419fc5
commit 45f4c65ab3
3 changed files with 26 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package me.rhunk.snapenhance.core.features.impl.ui
import android.net.Uri
import me.rhunk.snapenhance.common.util.snap.BitmojiSelfie
import me.rhunk.snapenhance.core.event.events.impl.NetworkApiRequestEvent
import me.rhunk.snapenhance.core.features.Feature
@ -8,15 +9,25 @@ import me.rhunk.snapenhance.core.features.FeatureLoadParams
class OldBitmojiSelfie : Feature("OldBitmojiSelfie", loadParams = FeatureLoadParams.INIT_SYNC) {
override fun init() {
val urlPrefixes = arrayOf("https://images.bitmoji.com/3d/render/", "https://cf-st.sc-cdn.net/3d/render/")
val state by context.config.userInterface.ddBitmojiSelfie
val oldBitmojiSelfie = context.config.userInterface.oldBitmojiSelfie.getNullable() ?: return
context.event.subscribe(NetworkApiRequestEvent::class, { state }) { event ->
context.event.subscribe(NetworkApiRequestEvent::class) { event ->
if (urlPrefixes.firstOrNull { event.url.startsWith(it) } == null) return@subscribe
val bitmojiURI = event.url.substringAfterLast("/")
event.url =
BitmojiSelfie.BitmojiSelfieType.STANDARD.prefixUrl +
bitmojiURI +
(bitmojiURI.takeIf { !it.contains("?") }?.let { "?" } ?: "&") + "transparent=1"
event.url = event.url.replace("ua=1", "") // replace ua=1 with nothing for old 3d selfies/background
// replace with old 2d selfies
if (oldBitmojiSelfie == "2d" && event.url.contains("trim=circle")) {
val bitmojiPath = event.url.substringAfterLast("/").substringBeforeLast("?")
event.url = Uri.parse(BitmojiSelfie.BitmojiSelfieType.STANDARD.prefixUrl)
.buildUpon()
.appendPath(bitmojiPath)
.appendQueryParameter("transparent", "1")
.appendQueryParameter("trim", "circle")
.build()
.toString()
}
if (arrayOf("?", "&").any { event.url.endsWith(it) }) event.url = event.url.dropLast(1)
}
}
}