Skip to content

Instantly share code, notes, and snippets.

@RicardAparicio
Last active October 29, 2022 17:07
Show Gist options
  • Save RicardAparicio/99a1a04074c4ee9830303fa769addad2 to your computer and use it in GitHub Desktop.
Save RicardAparicio/99a1a04074c4ee9830303fa769addad2 to your computer and use it in GitHub Desktop.
Set of methods to get result of an Activity with Kotlin Coroutines. Deprecated in favor of new Result API's, updated version here https://gist.github.com/RicardAparicio/2cd3f633e5f706268a22b8c42312f6eb")
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.commitNow
import es.edenred.BuildConfig
import io.reactivex.Single
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.rx2.rxSingle
import kotlinx.coroutines.withContext
@Deprecated("in favor of new Result API's, updated version here https://gist.github.com/RicardAparicio/2cd3f633e5f706268a22b8c42312f6eb")
sealed class IntentResult(val intent: Intent?) {
data class Ok(val data: Intent?) : IntentResult(data)
data class Canceled(val data: Intent?) : IntentResult(data)
}
class IntentResultResolverFragment : Fragment() {
private var completableDeferred: CompletableDeferred<IntentResult>? = null
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_LOGIN) {
completableDeferred?.complete(if (resultCode == Activity.RESULT_OK) IntentResult.Ok(data) else IntentResult.Canceled(data))
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onDestroy() {
super.onDestroy()
completableDeferred?.cancel()
completableDeferred = null
}
suspend fun resolveIntentResult(intent: Intent): IntentResult {
return CompletableDeferred<Result>().run {
completableDeferred = this
startActivityForResult(intent, REQUEST_LOGIN)
await()
}
}
companion object {
private const val REQUEST_ID = 1
const val TAG = "${BuildConfig.APPLICATION_ID}.IntentResultResolverFragment"
}
}
fun Fragment.getResultRxFor(intent: Intent): Single<IntentResult> =
GlobalScope.rxSingle { execute(intent) }
suspend fun Activity.getResultFor(intent: Intent): IntentResult {
return execute(intent)
}
suspend fun Fragment.getResultFor(intent: Intent): IntentResult {
return execute(intent)
}
private suspend fun Any.execute(intent: Intent): IntentResult {
/* Getting fm depending on the context
----------------- */
val fm = when (this) {
is Fragment -> childFragmentManager
is AppCompatActivity -> supportFragmentManager
else -> throw IllegalArgumentException("Please, you must request result from an Activity or Fragment...: $this")
}
return withContext(Dispatchers.Main) {
val recycledFragment = fm.findFragmentByTag(IntentResultResolverFragment.TAG)
if ((recycledFragment as? IntentResultResolverFragment) != null) {
return@withContext recycledFragment.resolveIntentResult(intent)
} else {
val fragment = IntentResultResolverFragment()
fm.commitNow { add(fragment, IntentResultResolverFragment.TAG) }
return@withContext fragment.resolveIntentResult(intent)
}
}
}
@RicardAparicio
Copy link
Author

RicardAparicio commented Nov 4, 2019

Example in the context of an Activity or Fragment:

suspend fun getResultForIntent(intent: Intent) {
    when(result = getResultFor(intent)) {
        is Result.Ok -> // whatever
        is Result.Cancelled -> // whatever
    }
}

@jishindev
Copy link

onActivityResult is deprecated now. Do you think we can build something similar for the new Activity Results APIs?

@RicardAparicio
Copy link
Author

For sure! I'll check that, I think this will be even simpler ;)

@AhmedBermo
Copy link

For sure! I'll check that, I think this will be even simpler ;)

hi , did you build any? thanks

@RicardAparicio
Copy link
Author

Hey @AhmedBermo and @jishindev , I've created a new versions for the new Result API's! Please let me know what do you think !!
https://gist.github.com/RicardAparicio/2cd3f633e5f706268a22b8c42312f6eb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment