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
/** | |
* 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. |
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
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 -> |
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
//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 | |
} | |
} | |
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
@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. |
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
view.rootWindowInsets?.isVisible(WindowInsets.Type.ime()) |
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
//hide the status bars | |
windowInsetsController?.hide(WindowInsets.Type.statusBars()) | |
//show the IME | |
windowInsetsController?.show(WindowInsets.Type.ime()) |
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
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) | |
} |
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
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 { |
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
@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. |
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
@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) |