fix: activity launcher helper crashes
This commit is contained in:
@@ -22,23 +22,17 @@ import me.rhunk.snapenhance.ui.manager.data.ModMappingsInfo
|
||||
import me.rhunk.snapenhance.ui.manager.data.SnapchatAppInfo
|
||||
import me.rhunk.snapenhance.ui.setup.Requirements
|
||||
import me.rhunk.snapenhance.ui.setup.SetupActivity
|
||||
import me.rhunk.snapenhance.ui.util.ActivityLauncherHelper
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class RemoteSideContext(
|
||||
val androidContext: Context
|
||||
) {
|
||||
private var _activity: WeakReference<Activity>? = null
|
||||
private var _activity: WeakReference<ComponentActivity>? = null
|
||||
lateinit var bridgeService: BridgeService
|
||||
|
||||
lateinit var activityLauncherHelper: ActivityLauncherHelper
|
||||
var activity: Activity?
|
||||
var activity: ComponentActivity?
|
||||
get() = _activity?.get()
|
||||
set(value) {
|
||||
_activity?.clear();
|
||||
_activity = WeakReference(value)
|
||||
activityLauncherHelper = ActivityLauncherHelper(value as ComponentActivity)
|
||||
}
|
||||
set(value) { _activity?.clear(); _activity = WeakReference(value) }
|
||||
|
||||
val config = ModConfig()
|
||||
val translation = LocaleWrapper()
|
||||
@@ -93,15 +87,15 @@ class RemoteSideContext(
|
||||
)
|
||||
|
||||
fun longToast(message: Any) {
|
||||
activity?.runOnUiThread {
|
||||
Toast.makeText(activity, message.toString(), Toast.LENGTH_LONG).show()
|
||||
androidContext.mainExecutor.execute {
|
||||
Toast.makeText(androidContext, message.toString(), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
Logger.debug(message.toString())
|
||||
}
|
||||
|
||||
fun shortToast(message: Any) {
|
||||
activity?.runOnUiThread {
|
||||
Toast.makeText(activity, message.toString(), Toast.LENGTH_SHORT).show()
|
||||
androidContext.mainExecutor.execute {
|
||||
Toast.makeText(androidContext, message.toString(), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
Logger.debug(message.toString())
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ import me.rhunk.snapenhance.core.config.PropertyKey
|
||||
import me.rhunk.snapenhance.core.config.PropertyPair
|
||||
import me.rhunk.snapenhance.core.config.PropertyValue
|
||||
import me.rhunk.snapenhance.ui.manager.Section
|
||||
import me.rhunk.snapenhance.ui.util.ActivityLauncherHelper
|
||||
import me.rhunk.snapenhance.ui.util.chooseFolder
|
||||
import me.rhunk.snapenhance.ui.util.openFile
|
||||
import me.rhunk.snapenhance.ui.util.saveFile
|
||||
@@ -95,6 +96,7 @@ class FeaturesSection : Section() {
|
||||
}
|
||||
|
||||
|
||||
private lateinit var activityLauncherHelper: ActivityLauncherHelper
|
||||
private val featuresRouteName by lazy { context.translation["manager.routes.features"] }
|
||||
|
||||
private lateinit var rememberScaffoldState: BottomSheetScaffoldState
|
||||
@@ -135,6 +137,10 @@ class FeaturesSection : Section() {
|
||||
return featuresRouteName
|
||||
}
|
||||
|
||||
override fun init() {
|
||||
activityLauncherHelper = ActivityLauncherHelper(context.activity!!)
|
||||
}
|
||||
|
||||
override fun build(navGraphBuilder: NavGraphBuilder) {
|
||||
navGraphBuilder.navigation(route = enumSection.route, startDestination = MAIN_ROUTE) {
|
||||
composable(MAIN_ROUTE) {
|
||||
@@ -186,7 +192,7 @@ class FeaturesSection : Section() {
|
||||
|
||||
if (property.key.params.flags.contains(ConfigFlag.FOLDER)) {
|
||||
IconButton(onClick = registerClickCallback {
|
||||
context.activityLauncherHelper.chooseFolder { uri ->
|
||||
activityLauncherHelper.chooseFolder { uri ->
|
||||
propertyValue.setAny(uri)
|
||||
}
|
||||
}.let { { it.invoke(true) } }) {
|
||||
@@ -449,7 +455,7 @@ class FeaturesSection : Section() {
|
||||
val actions = remember {
|
||||
mapOf(
|
||||
"Export" to {
|
||||
context.activityLauncherHelper.saveFile("config.json", "application/json") { uri ->
|
||||
activityLauncherHelper.saveFile("config.json", "application/json") { uri ->
|
||||
context.androidContext.contentResolver.openOutputStream(Uri.parse(uri))?.use {
|
||||
context.config.writeConfig()
|
||||
context.config.exportToString().byteInputStream().copyTo(it)
|
||||
@@ -458,7 +464,7 @@ class FeaturesSection : Section() {
|
||||
}
|
||||
},
|
||||
"Import" to {
|
||||
context.activityLauncherHelper.openFile("application/json") { uri ->
|
||||
activityLauncherHelper.openFile("application/json") { uri ->
|
||||
context.androidContext.contentResolver.openInputStream(Uri.parse(uri))?.use {
|
||||
runCatching {
|
||||
context.config.loadFromString(it.readBytes().toString(Charsets.UTF_8))
|
||||
|
||||
@@ -9,13 +9,17 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import me.rhunk.snapenhance.Logger
|
||||
import me.rhunk.snapenhance.ui.setup.screens.SetupScreen
|
||||
import me.rhunk.snapenhance.ui.util.ActivityLauncherHelper
|
||||
import me.rhunk.snapenhance.ui.util.ObservableMutableState
|
||||
import me.rhunk.snapenhance.ui.util.chooseFolder
|
||||
|
||||
class SaveFolderScreen : SetupScreen() {
|
||||
private lateinit var saveFolder: ObservableMutableState<String>
|
||||
|
||||
private lateinit var activityLauncherHelper: ActivityLauncherHelper
|
||||
|
||||
override fun init() {
|
||||
activityLauncherHelper = ActivityLauncherHelper(context.activity!!)
|
||||
saveFolder = ObservableMutableState(
|
||||
defaultValue = "",
|
||||
onChange = { _, newValue ->
|
||||
@@ -34,7 +38,7 @@ class SaveFolderScreen : SetupScreen() {
|
||||
DialogText(text = context.translation["setup.dialogs.save_folder"])
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Button(onClick = {
|
||||
context.activityLauncherHelper.chooseFolder {
|
||||
activityLauncherHelper.chooseFolder {
|
||||
saveFolder.value = it
|
||||
}
|
||||
}) {
|
||||
|
||||
Reference in New Issue
Block a user