Created
November 9, 2018 23:12
-
-
Save davidair/1b13a21c1cf8c7e226411ffc663b8590 to your computer and use it in GitHub Desktop.
A base class activity that leverages Kotlin coroutines and allows streamlining activity invocation
This file contains hidden or 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
/** | |
* Copyright 2017 Google LLC. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
*/ | |
import android.content.Intent | |
import android.support.v7.app.AppCompatActivity | |
import kotlinx.coroutines.CompletableDeferred | |
import kotlinx.coroutines.Deferred; | |
/** | |
* Wraps the parameters of onActivityResult | |
* | |
* @property resultCode the result code returned from the activity. | |
* @property data the optional intent returned from the activity. | |
*/ | |
class ActivityResult( | |
val resultCode: Int, | |
val data: Intent?) { | |
} | |
/** | |
* Provides a mechanism to launch intents using co-routines. | |
* Example usage: | |
* | |
* GlobalScope.launch(Dispatchers.Main) { | |
* val result = launchIntent(intent).await() | |
* result?.data?.let { | |
* // Do something with "it" | |
* } | |
* } | |
*/ | |
abstract class AsyncActivity : AppCompatActivity() { | |
var currentCode : Int = 0 | |
var resultByCode = mutableMapOf<Int, CompletableDeferred<ActivityResult?>>() | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
resultByCode[requestCode]?.let { | |
it.complete(ActivityResult(resultCode, data)) | |
resultByCode.remove(requestCode) | |
} ?: run { | |
super.onActivityResult(requestCode, resultCode, data) | |
} | |
} | |
/** | |
* Launches the intent allowing to process the result using await() | |
* | |
* @param intent the intent to be launched. | |
* | |
* @return Deferred<ActivityResult> | |
*/ | |
fun launchIntent(intent: Intent) : Deferred<ActivityResult?> | |
{ | |
val activityResult = CompletableDeferred<ActivityResult?>() | |
if (intent.resolveActivity(packageManager) != null) { | |
val resultCode = currentCode++ | |
resultByCode[resultCode] = activityResult | |
startActivityForResult(intent, resultCode) | |
} else { | |
activityResult.complete(null) | |
} | |
return activityResult | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately it never survives process killing. e.g. when start camera app and your app become background.