Skip to content

Instantly share code, notes, and snippets.

View teegarcs's full-sized avatar

Clinton teegarcs

  • CapTech Ventures
  • Washington, D.C.
View GitHub Profile
/**
* Contract that extends ActivityResultContract to implement a
* custom intent creation and handling of the results from that
* intent.
*/
class MessageContract : ActivityResultContract<Unit, String>() {
/**
* Create an intent to start MessageActivity. If you were
* providing a starting point you would pass that text here.
private val multiPermissionCallback =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { map ->
//handle individual results if desired
map.entries.forEach { entry ->
when (entry.key) {
Manifest.permission.ACCESS_FINE_LOCATION ->
mBinding.iconLocationPermission.isEnabled = entry.value
Manifest.permission.CAMERA ->
mBinding.iconCameraPermission.isEnabled = entry.value
Manifest.permission.RECORD_AUDIO ->
//register result contract - takes in a URI for where to store the image and returns a success boolean
private val takePictureCallback =
registerForActivityResult(ActivityResultContracts.TakePicture()) { successful ->
if (successful) {
//load image from provided URI
} else {
//show user an error
}
}
@RequiresApi(Build.VERSION_CODES.R)
fun View.addKeyboardInsetListener(keyboardCallback: (visible: Boolean) -> Unit) {
doOnLayout {
//get init state of keyboard
var keyboardVisible = rootWindowInsets?.isVisible(WindowInsets.Type.ime()) == true
//callback as soon as the layout is set with whether the keyboard is open or not
keyboardCallback(keyboardVisible)
//whenever there is an inset change on the App, check if the keyboard is visible.
view.rootWindowInsets?.isVisible(WindowInsets.Type.ime())
//hide the status bars
windowInsetsController?.hide(WindowInsets.Type.statusBars())
//show the IME
windowInsetsController?.show(WindowInsets.Type.ime())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//only required if using the KeyboardInsetListener
window.setDecorFitsSystemWindows(false)
//only receives callbacks when the inset affects my window.
binding.container.addKeyboardInsetListener(mainViewModel.keyboardCallback)
}
class MainViewModel : ViewModel() {
private val _keyboardActive = MutableLiveData<Int>()
val keyboardActive: LiveData<Int> = _keyboardActive
//callback to be provided to extension function for keyboard changes
val keyboardCallback: (visible: Boolean) -> Unit = {
_keyboardActive.value = if (it) {
View.GONE
} else {
@RequiresApi(Build.VERSION_CODES.R)
fun View.addKeyboardListener(keyboardCallback: (visible: Boolean) -> Unit) {
doOnLayout {
//get init state of keyboard
var keyboardVisible = rootWindowInsets?.isVisible(WindowInsets.Type.ime()) == true
//callback as soon as the layout is set with whether the keyboard is open or not
keyboardCallback(keyboardVisible)
//whenever the layout resizes/changes, callback with the state of the keyboard.
@Test
fun testLaunchActivityEventSample() {
var eventReceived: ActivityEvent? = null
val observer = Observer<NavEvent> {
eventReceived = it as ActivityEvent
}
//Observe forever since we don't have a lifecycle
viewModel.navEventSample.observeForever(observer)
//Mock the Bundle Creation
mockkConstructor(Bundle::class)