kotlin - required context, found AnkoAsyncContext - kotlin

SOLVED
I'm trying to use this inside doAsync from org.jetbrains.anko:anko-commons:0.10.8 but it's not letting me.
Snippet of my code:
doAsync {
buttonNext.setOnClickListener {
if (editTextInput.text.isEmpty()) {
val alert = AlertDialog.Builder(this)
alert.setTitle("You forgot something")
alert.setMessage("Answer cannot be empty!")
alert.show()
}
}
...
}

Related

How to mock extensions function in kotlin

I am using Mockk in my project. I am trying to mock my extension function but it cannot find my function. I tried some piece of code but it cannot find the extension function inside my test. Can someone guide me, How can I solve this issue. Thanks
ExploreConsultationsActivity.kt
class ExploreConsultationsActivity : AppCompactActvity () {
... // more function
internal fun setupExploreConsultationVisibility(hasFocus: Boolean) {
if (hasFocus) {
.....
} else if (viewModel.queryText.isEmpty()) {
binding.consultationViewSwitcher.displayConsultationViewSwitcherChild(0)
}
}
internal fun ViewSwitcher.displayConsultationViewSwitcherChild(childNumber: Int) {
visible()
displayedChild = childNumber
}
}
ExploreConsultationsActivityTest.kt
class ExploreConsultationsActivityTest {
#get:Rule
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
private val subject by lazy { spyk(ExploreConsultationsActivity()) }
#MockK private lateinit var mockConsultationViewSwitcher: ViewSwitcher
#Before
fun setUp() {
MockKAnnotations.init(this, relaxed = true)
setupMockView()
}
private fun setupMockView() {
mockkStatic(ExploreConsultationsLayoutBinding::class)
every { mockRootView.findViewById<ChipGroup>(R.id.exploreConsultationChips) } returns mockChipGroup
}
#Test
fun `setupExploreConsultationVisibility - `() {
// STUBBING
mockViewModel.queryText = ""
every { mockViewModel.topicSelected } returns ConsultationTopicsArea.ALL
with(mockConsultationViewSwitcher){
any<ViewSwitcher>().displayConsultationViewSwitcherChild(0)
}
// EXECUTION
subject.setupExploreConsultationVisibility(false)
// VERIFICATION
verify {
mockViewModel.filterBy(ConsultationTopicsArea.ALL)
}
}
I am getting this error

How to use OnClickListener for items in AlertDialog Kotlin?

So i get this java code about AlertDialog:
AlertDialog.Builder builder= new AlertDialog.Builder(context:this);
builder.setTitle("Pick Image")
.setItems(options, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
if(which==0){
if(checkCameraPermission())pickFromcamera();
else requestCameraPermission();
} else{
if(checkStoragePermission()) pickFromGallery();
else requestStoragePermission();
}
}
}
And i try to use this in my Kotlin project, so i change it a bit. My code look like:
val options: Array<String> = arrayOf("Kamera","Gallery")
val builder= AlertDialog.Builder(this)
builder.setTitle("Pilih Gambar")
.setItems(options,DialogInterface.OnClickListener(){
#Override
fun onClick(dialog: DialogInterface, which: Int){
if(which==0){
if(checkCameraPermission()){
pickFromCamera()
}
else{
requestCameraPermission()
}
}
else{
if(checkStoragePermission()){
pickFromGallery()
}
else{
requestStoragePermission()
}
}
}
})
.show()
But i get this error : Expected 2 parameters of types DialogInterface!, Int at my setItems. Why is this happen? What did i do wrong?
You combined Java with Kotlin. Try writing it like this with your logic inside of the lambda assuming you're trying to do it in Kotlin.
val options: Array<String> = arrayOf("Kamera","Gallery")
val builder= AlertDialog.Builder(requireContext())
builder.setTitle("Pilih Gambar").setItems(options) { dialog, which ->
if(which==0){
if(checkCameraPermission()){
pickFromCamera()
}
else{
requestCameraPermission()
}
}
else{
if(checkStoragePermission()){
pickFromGallery()
}
else{
requestStoragePermission()
}
}
}
.show()

Inappropriate blocking method call warning within a separate Dispatchers.IO block

Why does the first code snippet produces Inappropriate blocking method call warning but not the second one?
private fun prepareList() = launch {
withContext(Dispatchers.IO) {
requireContext().openFileOutput(listFileName, Application.MODE_PRIVATE).use { out ->
requireContext().assets.open(listFileName).use {
it.copyTo(out)
}
}
}
}
private fun prepareList() = launch(Dispatchers.IO) {
requireContext().openFileOutput(listFileName, Application.MODE_PRIVATE).use { out ->
requireContext().assets.open(listFileName).use {
it.copyTo(out)
}
}
}

How to subscribe to StateFlow in kotlin-react useEffect

I'm trying to create a small counter example for kotlin-react with functionalComponent with kotlin 1.4-M2.
The example should use kotlinx.coroutines.flow. I'm struggling at collecting the values from the store in reacts useEffect hook.
Store:
object CounterModel { // Modified sample from kotlin StateFlow doc
private val _counter = MutableStateFlow(0) // private mutable state flow
val counter: StateFlow<Int> get() = _counter // publicly exposed as read-only state flow
fun inc() { _counter.value++ }
}
Component:
val counter = functionalComponent<RProps> {
val (counterState, setCounter) = useState(CounterModel.counter.value)
useEffect(listOf()) {
// This does not work
GlobalScope.launch { CounterModel.counter.collect { setCounter(it) } }
}
div {
h1 {
+"Counter: $counterState"
}
button {
attrs.onClickFunction = { CounterModel.inc() }
}
}
}
When I directly call CounterModel.counter.collect { setCounter(it) } it complains about Suspend function 'collect' should be called only from a coroutine or another suspend function.
How would you implement this useEffect hook?
And once the subscription works, how would you unsubscribe from it (use useEffectWithCleanup instead of useEffect)?
Finally found a solution. We can use onEach to do an action for every new value and then 'subscribe' with launchIn. This returns a job that can be canceled for cleanup:
object CounterStore {
private val _counter = MutableStateFlow(0)
val counter: StateFlow<Int> get() = _counter
fun inc() { _counter.value++ }
}
val welcome = functionalComponent<RProps> {
val (counter, setCounter) = useState(CounterStore.counter.value)
useEffectWithCleanup(listOf()) {
val job = CounterStore.counter.onEach { setCounter(it) }.launchIn(GlobalScope)
return#useEffectWithCleanup { job.cancel() }
}
div {
+"Counter: $counter"
}
button {
attrs.onClickFunction = { CounterStore.inc() }
+"Increment"
}
}
We can extract this StateFlow logic to a custom react hook:
fun <T> useStateFlow(flow: StateFlow<T>): T {
val (state, setState) = useState(flow.value)
useEffectWithCleanup(listOf()) {
val job = flow.onEach { setState(it) }.launchIn(GlobalScope)
return#useEffectWithCleanup { job.cancel() }
}
return state
}
And use it like this in our component:
val counter = useStateFlow(CounterStore.counter)
The complete project can be found here.
The Flow-Api is very experimental so this might not be the final solution :)
if's very important to check that the value hasn't changed,
before calling setState, otherwise the rendering happens twice
external interface ViewModelProps : RProps {
var viewModel : MyViewModel
}
val App = functionalComponent<ViewModelProps> { props ->
val model = props.viewModel
val (state, setState) = useState(model.stateFlow.value)
useEffectWithCleanup {
val job = model.stateFlow.onEach {
if (it != state) {
setState(it)
}
}.launchIn(GlobalScope)
return#useEffectWithCleanup { job.cancel() }
}
}

How to create a flow with a few subscribtions in Kotlin?

I need to run a task, which emits some data. I want to subscribe to this data like PublishSubject. But I can't solve a problem of one-instance flow. If I try to call it again, it will create another instance and the job will be done twice.
I tried to run the flow internally and post values to the BroadcastChannel, but this solution doesn't seem correct.
What is the best practice for such a task?
This will do the magic:
fun <T> Flow<T>.refCount(capacity: Int = Channel.CONFLATED, dispatcher: CoroutineDispatcher = Dispatchers.Default): Flow<T> {
class Context(var counter: Int) {
lateinit var job: Job
lateinit var channel: BroadcastChannel<T>
}
val context = Context(0)
fun lock() = synchronized(context) {
if (++context.counter > 1) {
return#synchronized
}
context.channel = BroadcastChannel(capacity)
context.job = GlobalScope.async(dispatcher) {
try {
collect { context.channel.offer(it) }
} catch (e: Exception) {
context.channel.close(e)
}
}
}
fun unlock() = synchronized(context) {
if (--context.counter == 0) {
context.job.cancel()
}
}
return flow {
lock()
try {
emitAll(context.channel.openSubscription())
} finally {
unlock()
}
}
}