From ee5fbce2ce991c722ce8805cfe82faac4daf9163 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 28 Aug 2023 09:56:12 -0300 Subject: [PATCH] refactor mainactivity to use IntentUtils.start --- .../amethyst/service/IntentUtils.kt | 90 +++++++++++++++++++ .../vitorpamplona/amethyst/ui/MainActivity.kt | 80 +---------------- 2 files changed, 91 insertions(+), 79 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt index 4039acd05..4a282fe9e 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/IntentUtils.kt @@ -1,10 +1,100 @@ package com.vitorpamplona.amethyst.service +import android.app.Activity import android.content.Intent +import android.widget.Toast import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts +import com.vitorpamplona.amethyst.Amethyst +import com.vitorpamplona.amethyst.LocalPreferences +import com.vitorpamplona.amethyst.ServiceManager +import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils +import com.vitorpamplona.amethyst.service.notifications.RegisterAccounts +import com.vitorpamplona.amethyst.service.relays.Client +import com.vitorpamplona.amethyst.ui.MainActivity +import com.vitorpamplona.quartz.events.Event +import com.vitorpamplona.quartz.events.RelayAuthEvent +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch object IntentUtils { lateinit var activityResultLauncher: ActivityResultLauncher lateinit var authActivityResultLauncher: ActivityResultLauncher lateinit var decryptActivityResultLauncher: ActivityResultLauncher + + @OptIn(DelicateCoroutinesApi::class) + fun start(activity: MainActivity) { + activityResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + return@registerForActivityResult + } + + val event = it.data?.getStringExtra("event") ?: "" + + val signedEvent = Event.fromJson(event) + val authEvent = RelayAuthEvent(signedEvent.id, signedEvent.pubKey, signedEvent.createdAt, signedEvent.tags, signedEvent.content, signedEvent.sig) + + RegisterAccounts(LocalPreferences.allSavedAccounts()).postRegistrationEvent( + listOf(authEvent) + ) + PushNotificationUtils.hasInit = true + ServiceManager.shouldPauseService = true + } + + authActivityResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + return@registerForActivityResult + } + + val event = it.data?.getStringExtra("event") ?: "" + + val signedEvent = Event.fromJson(event) + val authEvent = RelayAuthEvent(signedEvent.id, signedEvent.pubKey, signedEvent.createdAt, signedEvent.tags, signedEvent.content, signedEvent.sig) + + GlobalScope.launch(Dispatchers.IO) { + Client.send(authEvent, authEvent.relay()) + } + ServiceManager.shouldPauseService = true + } + + decryptActivityResultLauncher = activity.registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode != Activity.RESULT_OK) { + GlobalScope.launch(Dispatchers.Main) { + Toast.makeText( + Amethyst.instance, + "Sign request rejected", + Toast.LENGTH_SHORT + ).show() + } + AmberUtils.isActivityRunning = false + return@registerForActivityResult + } + + val event = it.data?.getStringExtra("signature") ?: "" + AmberUtils.content = event + AmberUtils.isActivityRunning = false + } + } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt index fd2e82042..396b54f92 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt @@ -1,6 +1,5 @@ package com.vitorpamplona.amethyst.ui -import android.app.Activity import android.content.Context import android.content.Intent import android.net.ConnectivityManager @@ -10,7 +9,6 @@ import android.net.NetworkRequest import android.os.Build import android.os.Bundle import android.util.Log -import android.widget.Toast import androidx.activity.compose.setContent import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi @@ -22,15 +20,11 @@ import androidx.compose.material.Surface import androidx.compose.ui.Modifier import androidx.core.os.LocaleListCompat import androidx.lifecycle.viewmodel.compose.viewModel -import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.ServiceManager -import com.vitorpamplona.amethyst.service.AmberUtils import com.vitorpamplona.amethyst.service.IntentUtils import com.vitorpamplona.amethyst.service.connectivitystatus.ConnectivityStatus import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils -import com.vitorpamplona.amethyst.service.notifications.RegisterAccounts -import com.vitorpamplona.amethyst.service.relays.Client import com.vitorpamplona.amethyst.ui.components.DefaultMutedSetting import com.vitorpamplona.amethyst.ui.components.keepPlayingMutex import com.vitorpamplona.amethyst.ui.navigation.Route @@ -45,10 +39,8 @@ import com.vitorpamplona.quartz.events.ChannelCreateEvent import com.vitorpamplona.quartz.events.ChannelMessageEvent import com.vitorpamplona.quartz.events.ChannelMetadataEvent import com.vitorpamplona.quartz.events.CommunityDefinitionEvent -import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.LiveActivitiesEvent import com.vitorpamplona.quartz.events.PrivateDmEvent -import com.vitorpamplona.quartz.events.RelayAuthEvent import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -57,79 +49,9 @@ import java.net.URLEncoder import java.nio.charset.StandardCharsets class MainActivity : AppCompatActivity() { - @OptIn(DelicateCoroutinesApi::class) @RequiresApi(Build.VERSION_CODES.R) override fun onCreate(savedInstanceState: Bundle?) { - IntentUtils.activityResultLauncher = registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - return@registerForActivityResult - } - - val event = it.data?.getStringExtra("event") ?: "" - - val signedEvent = Event.fromJson(event) - val authEvent = RelayAuthEvent(signedEvent.id, signedEvent.pubKey, signedEvent.createdAt, signedEvent.tags, signedEvent.content, signedEvent.sig) - - RegisterAccounts(LocalPreferences.allSavedAccounts()).postRegistrationEvent( - listOf(authEvent) - ) - PushNotificationUtils.hasInit = true - ServiceManager.shouldPauseService = true - } - - IntentUtils.authActivityResultLauncher = registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - return@registerForActivityResult - } - - val event = it.data?.getStringExtra("event") ?: "" - - val signedEvent = Event.fromJson(event) - val authEvent = RelayAuthEvent(signedEvent.id, signedEvent.pubKey, signedEvent.createdAt, signedEvent.tags, signedEvent.content, signedEvent.sig) - - GlobalScope.launch(Dispatchers.IO) { - Client.send(authEvent, authEvent.relay()) - } - ServiceManager.shouldPauseService = true - } - - IntentUtils.decryptActivityResultLauncher = registerForActivityResult( - ActivityResultContracts.StartActivityForResult() - ) { - if (it.resultCode != Activity.RESULT_OK) { - GlobalScope.launch(Dispatchers.Main) { - Toast.makeText( - Amethyst.instance, - "Sign request rejected", - Toast.LENGTH_SHORT - ).show() - } - AmberUtils.isActivityRunning = false - return@registerForActivityResult - } - - val event = it.data?.getStringExtra("signature") ?: "" - AmberUtils.content = event - AmberUtils.isActivityRunning = false - } + IntentUtils.start(this) super.onCreate(savedInstanceState)