repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.yongjhih.aws-ktx:awx-iot:-SNAPSHOT'
implementation 'com.github.yongjhih.aws-ktx:awx-cognito:-SNAPSHOT'
implementation 'com.github.yongjhih.aws-ktx:awx-lambda:-SNAPSHOT'
implementation 'com.github.yongjhih.aws-ktx:awx-lambda-moshi:-SNAPSHOT'
implementation 'com.github.yongjhih.aws-ktx:awx-lambda-kotlinx-serialization:-SNAPSHOT'
}
Allow disconnect and unsubscribe when the view lifecycle eneded.
Before:
mqttManager.connect(credentialsProvider) { status, _ ->
if (status == AWSIotMqttClientStatus.Connected) {
mqttManager.subscribeToTopic(
topic,
AWSIotMqttQos.QOS0,
object : AWSIotMqttSubscriptionStatusCallback {
override fun onSuccess() {
}
override fun onFailure(exception: Throwable) {
mqttManager.disconnect()
}
}) { _, data ->
println(String(data, StandardCharsets.UTF_8))
}
}
}
override fun onDestroyView() {
super.onDestroyView()
mqttManager.disconnect()
}
After:
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
// Auto discoonnect and auto unsubscribe when coroutine canceled
mqttManager.inConnection(credentialsProvider) {
mqttManager.subscribe(topic, AWSIotMqttQos.QOS0)
.map { String(it.second, StandardCharsets.UTF_8) }
.onEach { println(it) }
}
}
or
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
// Auto discoonnect and auto unsubscribe when coroutine canceled
mqttManager.connect(credentialsProvider)
.distinctUntilChanged()
.filter { it == AWSIotMqttClientStatus.Connected }
.take(1)
.flatMapConcat { mqttManager.subscribe(topic, AWSIotMqttQos.QOS0) }
.map { String(it.second, StandardCharsets.UTF_8) }
.onEach { println(it) }
}
If we just need a first message from the topic, then unsubscribe and disconnect.
Before:
mqttManager.connect(credentialsProvider) { status, _ ->
if (status == AWSIotMqttClientStatus.Connected) {
mqttManager.subscribeToTopic(
topic,
AWSIotMqttQos.QOS0,
object : AWSIotMqttSubscriptionStatusCallback {
override fun onSuccess() {
}
override fun onFailure(exception: Throwable) {
mqttManager.disconnect()
}
}) { topic, data ->
println(String(data, StandardCharsets.UTF_8))
+ mqttManager.unsubscribeTopic(topic)
+ mqttManager.disconnect()
}
}
}
override fun onDestroyView() {
super.onDestroyView()
mqttManager.disconnect()
}
After:
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
// Auto discoonnect and auto unsubscribe when coroutine canceled
mqttManager.inConnection(credentialsProvider) {
mqttManager.subscribe(topic, AWSIotMqttQos.QOS0)
.map { String(it.second, StandardCharsets.UTF_8) }
+ .take(1)
.onEach { println(it) }
}
}
or
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
// Auto discoonnect and auto unsubscribe when coroutine canceled
mqttManager.connect(credentialsProvider)
.distinctUntilChanged()
.filter { it == AWSIotMqttClientStatus.Connected }
.take(1)
.flatMapConcat { mqttManager.subscribe(topic, AWSIotMqttQos.QOS0) }
.map { String(it.second, StandardCharsets.UTF_8) }
+ .take(1)
.onEach { println(it) }
}
You can also get the first message concisely:
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
// Auto discoonnect and auto unsubscribe when coroutine canceled
val firstMessage = mqttManager.inConnection(credentialsProvider) {
mqttManager.subscribe(topic, AWSIotMqttQos.QOS0)
.map { String(it.second, StandardCharsets.UTF_8) }
.first()
}
println(firstMessage)
}
or
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
// Auto discoonnect and auto unsubscribe when coroutine canceled
val firstMessage = mqttManager.connect(credentialsProvider)
.distinctUntilChanged()
.filter { it == AWSIotMqttClientStatus.Connected }
.take(1)
.flatMapConcat { mqttManager.subscribe(topic, AWSIotMqttQos.QOS0) }
.map { String(it.second, StandardCharsets.UTF_8) }
.first()
println(firstMessage)
}
CognitoUser.getSession()
Before:
cognitoUserPool.getUser(username).getSessionInBackground(object : AuthenticationHandler {
override fun onSuccess(
userSession: CognitoUserSession,
newDevice: CognitoDevice,
) {
println(userSession.idToken?.jwtToken)
}
override fun getAuthenticationDetails(
authenticationContinuation: AuthenticationContinuation,
userId: String,
) {
authenticationContinuation.apply {
setAuthenticationDetails(AuthenticationDetails(userId, password, null))
continueTask()
}
}
override fun getMFACode(continuation: MultiFactorAuthenticationContinuation) {
}
override fun authenticationChallenge(continuation: ChallengeContinuation?) {
}
override fun onFailure(exception: Exception) {
}
})
After:
println(cognitoUserPool.getUser(username).getSessionAsync { _, userId ->
AuthenticationDetails(userId, password, null)
}.first.idToken?.jwtToken)
Before
val gitHubLambda: GitHubLambda = lambdaInvokerFactory.build(GitHubLambda::class.java)
After:
val gitHubLambda = lambdaInvokerFactory.build<GitHubLambda>()
// or
val gitHubLambda: GitHubLambda = lambdaInvokerFactory.build()
interface GitHubLambda {
@LambdaFunction(functionName = "user")
fun user(request: Map<String, Any>): User
}
@JsonClass(generateAdapter = false)
data class User(
val username: String,
)
val lambdaInvokerFactory = LambdaInvokerFactory.builder()
.context(context)
.region(REGION)
.credentialsProvider(credentialsProvider)
.build()
val gitHubLambda: GitHubLambda = lambdaInvokerFactory.build(LambdaMoshiBinder())
val user = gitHubLambda.user(mapOf { ... })
@Serializable
data class User(
val username: String,
)
val gitHubLambda: GitHubLambda = lambdaInvokerFactory.build(LambdaKotlinxSerializationBinder())
val user = gitHubLambda.user(mapOf { ... })