Skip to content

Commit 42c2fed

Browse files
committed
Implement activity contracts
1 parent 397d77b commit 42c2fed

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Demo projects are split into separate folders within this repository, check them
88

99
If you want to open the code in Android Studio, you'll need to open the root folder.
1010

11+
- [Activity contracts](activity-contracts/README.md): simplified handling of `Activity` results
1112
- [Coroutine basics](coroutines/README.md): Kotlin coroutine basics on Android
1213
- [Dagger 2](dagger-2/README.md): dependency injection
1314
- [Data binding](data-binding/README.md): crash course on the data binding library

activity-contracts/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Activity Contracts
2+
3+
🎞️ [YouTube video](https://youtu.be/)
4+
📓 [Official documentation](https://developer.android.com/training/basics/intents/result)
5+
6+
## Brief Explanation
7+
The important stuff happens in the `ContractsDemoActivity`. Have a look at how we can easily
8+
get results from activities. This includes simplified requesting of runtime permissions.
Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.github.mrbean355.android.activitycontracts
22

33
import android.Manifest
4-
import android.content.Intent
5-
import android.content.pm.PackageManager
6-
import android.os.Build
74
import android.os.Bundle
5+
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
86
import androidx.activity.viewModels
97
import androidx.appcompat.app.AppCompatActivity
10-
import androidx.core.content.ContextCompat
118
import com.github.mrbean355.android.activitycontracts.databinding.ActivityContractsDemoBinding
129

13-
private const val REQUEST_CODE_PERMISSION = 123
14-
private const val REQUEST_CODE_CUSTOM = 456
15-
1610
class ContractsDemoActivity : AppCompatActivity() {
11+
private val requestPermission = registerForActivityResult(RequestPermission()) { granted ->
12+
viewModel.onPermissionsResult(granted)
13+
}
14+
private val customContract = registerForActivityResult(PaymentActivity.Contract()) { result ->
15+
viewModel.onCustomContractResult(result)
16+
}
1717
private val viewModel: ContractsDemoViewModel by viewModels()
1818

1919
override fun onCreate(savedInstanceState: Bundle?) {
@@ -26,32 +26,11 @@ class ContractsDemoActivity : AppCompatActivity() {
2626
}
2727

2828
binding.requestPermission.setOnClickListener {
29-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
30-
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
31-
viewModel.onPermissionsResult(true)
32-
} else {
33-
requestPermissions(arrayOf(Manifest.permission.CAMERA), REQUEST_CODE_PERMISSION)
34-
}
35-
}
29+
requestPermission.launch(Manifest.permission.CAMERA)
3630
}
3731

3832
binding.customContract.setOnClickListener {
39-
val intent = PaymentActivity.createIntent(this, Payment("Test User", 4200))
40-
startActivityForResult(intent, REQUEST_CODE_CUSTOM)
41-
}
42-
}
43-
44-
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
45-
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
46-
if (requestCode == REQUEST_CODE_PERMISSION) {
47-
viewModel.onPermissionsResult(grantResults.first() == PackageManager.PERMISSION_GRANTED)
48-
}
49-
}
50-
51-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
52-
super.onActivityResult(requestCode, resultCode, data)
53-
if (requestCode == REQUEST_CODE_CUSTOM) {
54-
viewModel.onCustomContractResult(PaymentActivity.parseResult(resultCode, data))
33+
customContract.launch(Payment("Test User", 4200))
5534
}
5635
}
5736
}

activity-contracts/src/main/java/com/github/mrbean355/android/activitycontracts/PaymentActivity.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.mrbean355.android.activitycontracts
33
import android.content.Context
44
import android.content.Intent
55
import android.os.Bundle
6+
import androidx.activity.result.contract.ActivityResultContract
67
import androidx.appcompat.app.AppCompatActivity
78
import com.github.mrbean355.android.activitycontracts.databinding.ActivityPaymentBinding
89

@@ -30,16 +31,16 @@ class PaymentActivity : AppCompatActivity() {
3031
}
3132
}
3233

33-
companion object {
34+
class Contract : ActivityResultContract<Payment, TransactionResult>() {
3435

35-
fun createIntent(context: Context, payment: Payment): Intent =
36+
override fun createIntent(context: Context, input: Payment?): Intent =
3637
Intent(context, PaymentActivity::class.java)
37-
.putExtra(KEY_NAME, payment.recipientName)
38-
.putExtra(KEY_AMOUNT, payment.amount)
38+
.putExtra(KEY_NAME, input?.recipientName)
39+
.putExtra(KEY_AMOUNT, input?.amount)
3940

40-
fun parseResult(resultCode: Int, data: Intent?): TransactionResult = TransactionResult(
41+
override fun parseResult(resultCode: Int, intent: Intent?) = TransactionResult(
4142
success = resultCode == RESULT_OK,
42-
message = data?.getStringExtra(KEY_RESULT).orEmpty()
43+
message = intent?.getStringExtra(KEY_RESULT).orEmpty()
4344
)
4445
}
4546
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ buildscript {
22
/** Various versions to be used throughout all modules. */
33
ext {
44
// Android
5-
activity_version = '1.1.0'
5+
activity_version = '1.2.0'
66
appcompat_version = '1.2.0'
77
core_version = '1.3.2'
88
fragment_version = '1.3.0'

0 commit comments

Comments
 (0)