Skip to content

Commit

Permalink
fix: Remove deprecated on activity result
Browse files Browse the repository at this point in the history
  • Loading branch information
Leander Beernaert committed Aug 3, 2024
1 parent 8f5e5a2 commit e084b51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.lbeernaert.youhavemail

import android.Manifest
import android.app.Activity
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -37,7 +38,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity
import dev.lbeernaert.youhavemail.app.LOG_EXPORT_REQUEST
import dev.lbeernaert.youhavemail.app.NotificationActionClicked
import dev.lbeernaert.youhavemail.app.NotificationIntentAppNameKey
import dev.lbeernaert.youhavemail.app.NotificationIntentBackendKey
Expand Down Expand Up @@ -92,6 +92,15 @@ class MainActivity : ComponentActivity() {
mState!!.migrateAccounts(this)
}

var exportLogsLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.let {
exportLogs(this, it)
}
}
}

setContent {
YouHaveMailTheme {
// A surface container using the 'background' color from the theme
Expand Down Expand Up @@ -161,6 +170,14 @@ class MainActivity : ComponentActivity() {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
oneshotWorker(this)
},
onExportLogsClicked = {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/zip"
putExtra(Intent.EXTRA_TITLE, "you-have-mail-logs.zip")
}
exportLogsLauncher.launch(intent)
}
)
}
Expand Down Expand Up @@ -210,16 +227,6 @@ class MainActivity : ComponentActivity() {

super.onDestroy()
}

@Suppress("OverrideDeprecatedMigration")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
LOG_EXPORT_REQUEST -> data?.data?.let {
exportLogs(this, it)
}
}
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package dev.lbeernaert.youhavemail.screens

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.core.app.ActivityCompat.startActivityForResult
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import dev.lbeernaert.youhavemail.R
import dev.lbeernaert.youhavemail.app.LOG_EXPORT_REQUEST
import dev.lbeernaert.youhavemail.app.State
import dev.lbeernaert.youhavemail.testProxy
import kotlinx.coroutines.Dispatchers
Expand All @@ -27,6 +24,7 @@ fun MainNavController(
state: State,
requestPermissions: () -> Unit,
onPollClicked: () -> Unit,
onExportLogsClicked: () -> Unit
) {
val navController = rememberNavController()

Expand Down Expand Up @@ -130,21 +128,13 @@ fun MainNavController(
composable(Routes.Settings.route) {
Settings(
state = state,
context = context,
onBackClicked = {
navController.popBackStack()
},
onPollIntervalUpdate = { interval ->
state.setPollInterval(context, interval)
},
onExportLogsClicked = {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/zip"
putExtra(Intent.EXTRA_TITLE, "you-have-mail-logs.zip")
}
startActivityForResult(context, intent, LOG_EXPORT_REQUEST, null)
}
onExportLogsClicked = onExportLogsClicked
)
}
// ------------------ PROXY LOGIN --------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.lbeernaert.youhavemail.screens

import android.app.Activity
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -35,7 +34,6 @@ import dev.lbeernaert.youhavemail.components.AsyncScreen
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun Settings(
context: Activity,
state: State,
onBackClicked: () -> Unit,
onPollIntervalUpdate: (ULong) -> Unit,
Expand Down

0 comments on commit e084b51

Please sign in to comment.