Last active
October 29, 2022 17:07
-
-
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")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
For sure! I'll check that, I think this will be even simpler ;)
hi , did you build any? thanks
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
For sure! I'll check that, I think this will be even simpler ;)