LoginActivity: startActivityForResult deprecated.

Migrate startActivityForResult/onActivityResult to
StartActivityForResult/registerForActivityResult in
LoginActivity/OAuth.

This moves responsibility for scheduling the starting Intent from
OAuth to LoginActivity.

OAuth still generates the Intent.  But instead of starting the intent
directly in OAuth, the intent is returned to LoginActivity.  This
better associates processing the activity result with its invocation.

OAuthTest module updated to accommodate internal API change.
This commit is contained in:
Hugh Daschbach 2022-08-26 14:19:53 -07:00 committed by Ryan Harg
parent 38a3183b9d
commit bea1d1f397
No known key found for this signature in database
GPG Key ID: 89106F3A84E6958C
3 changed files with 20 additions and 26 deletions

View File

@ -5,6 +5,7 @@ import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.doOnLayout
import androidx.lifecycle.lifecycleScope
@ -40,26 +41,21 @@ class LoginActivity : AppCompatActivity() {
limitContainerWidth()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
private var resultLauncher =
registerForActivityResult(StartActivityForResult()) { result ->
result.data?.let {
oAuth.exchange(this, it) {
PowerPreference
.getFileByName(AppContext.PREFS_CREDENTIALS)
.setBoolean("anonymous", false)
data?.let {
when (requestCode) {
0 -> {
oAuth.exchange(this, data) {
PowerPreference
.getFileByName(AppContext.PREFS_CREDENTIALS)
.setBoolean("anonymous", false)
lifecycleScope.launch(Main) {
Userinfo.get(this@LoginActivity, oAuth)?.let {
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
lifecycleScope.launch(Main) {
Userinfo.get(this@LoginActivity, oAuth)?.let {
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
return@launch finish()
}
throw Exception(getString(R.string.login_error_userinfo))
}
return@launch finish()
}
throw Exception(getString(R.string.login_error_userinfo))
}
}
}
@ -134,7 +130,7 @@ class LoginActivity : AppCompatActivity() {
oAuth.init(hostname)
return oAuth.register {
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).setString("hostname", hostname)
oAuth.authorize(this)
resultLauncher.launch(oAuth.authorizeIntent(this))
}
}

View File

@ -184,11 +184,10 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory
)
}
fun authorize(activity: Activity) {
fun authorizeIntent(activity: Activity): Intent? {
val authService = service(activity)
authorizationRequest()?.let { it ->
val intent = authService.getAuthorizationRequestIntent(it)
activity.startActivityForResult(intent, 0)
return authorizationRequest()?.let { it ->
authService.getAuthorizationRequestIntent(it)
}
}

View File

@ -27,6 +27,7 @@ import org.junit.Before
import org.junit.Test
import strikt.api.expectThat
import strikt.api.expectThrows
import strikt.assertions.isA
import strikt.assertions.isEqualTo
import strikt.assertions.isFalse
import strikt.assertions.isNotNull
@ -282,7 +283,7 @@ class OAuthTest {
}
@Test
fun `authorize() should start activity for result`() {
fun `authorizeIntent() should return an Intent`() {
mockkStatic(PowerPreference::class)
every { PowerPreference.getFileByName(any()) } returns mockPreference
@ -302,9 +303,7 @@ class OAuthTest {
val activity = mockk<Activity>(relaxed = true)
oAuth.authorize(activity)
verify { activity.startActivityForResult(mockkIntent, 0) }
expectThat(oAuth.authorizeIntent(activity)).isNotNull().isA<Intent>()
}
private fun <T> deserializeJson(