Convert RxJava code to Kotlin properly
Tag : java , By : CodeOfficer
Date : March 29 2020, 07:55 AM
Hope this helps In kotlin you can replace interfaces implementations, having just one method, for a lambda expression ( doc). .subscribe(
{ item: Long? ->
},
{ t: Throwable ->
},
{//onComplete
})
.subscribe(
{ item: Long ->
},
{ t: Throwable ->
},
{//onComplete
})
|
Trouble converting Java code to Kotlin to implement an OnChangeListener
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have this code to implement a listener for a Range Bar library: , Your rangebar is null, To avoid crash, use like this : rangebar?.setOnRangeBarChangeListener(object: RangeBar.OnRangeBarChangeListener {
override fun onRangeChangeListener(rangeBar:RangeBar, leftPinIndex:Int, rightPinIndex:Int, leftPinValue:String, rightPinValue:String) {
textView.text = "$leftPinIndex"
}
})
rangebar?.setOnRangeBarChangeListener { rangeBar, leftPinIndex, rightPinIndex, leftPinValue, rightPinValue ->
textView.text = "$leftPinIndex"
}
|
Annotations not converted properly after changing code from Java to Kotlin
Tag : java , By : kangfoo2
Date : March 29 2020, 07:55 AM
it helps some times Kotlin confuses its own IntRange class with the IntRange annotation of the Android SDK. Make an import like that giving it another name: import android.support.annotation.IntRange as AndroidIntRange
fun foo(@AndroidIntRange(from = 0, to = 255) bar: Int) {
}
|
Implement code after convert Java to Kotlin error
Date : March 29 2020, 07:55 AM
hop of those help? You can use the operators (infix functions) shr and and for the type Int (and Long) in Kotlin. Just change b with b.toInt(): private fun appendHex(sb: StringBuffer, b: Byte) {
sb.append(HEX[b.toInt() shr 4 and 0x0f]).append(HEX[b.toInt() and 0x0f])
}
|
How to implement NIO Socket (client) using Kotlin coroutines in Java Code?
Date : March 29 2020, 07:55 AM
I hope this helps . As Marko pointed out your code will still end up blocking a thread even when that blocking operation is in the async coroutine. To truly get the asynchronous behavior you desire with Java and Kotlin you need to use the Async version of Socket ChannelWith this, you get true asynchronous socket handling. With that class and Kotlin's suspendCoroutine builder method, you can turn the async handlers into suspendable calls. class TcpSocket(private val socket: AsynchronousSocketChannel) {
suspend fun read(buffer: ByteBuffer): Int {
return socket.asyncRead(buffer)
}
fun close() {
socket.close()
}
private suspend fun AsynchronousSocketChannel.asyncRead(buffer: ByteBuffer): Int {
return suspendCoroutine { continuation ->
this.read(buffer, continuation, ReadCompletionHandler)
}
}
object ReadCompletionHandler : CompletionHandler<Int, Continuation<Int>> {
override fun completed(result: Int, attachment: Continuation<Int>) {
attachment.resume(result)
}
override fun failed(exc: Throwable, attachment: Continuation<Int>) {
attachment.resumeWithException(exc)
}
}
}
suspend fun AsynchronousSocketChannel.asyncRead(buffer: ByteBuffer): Int {
return suspendCoroutine { continuation ->
this.read(buffer, continuation, ReadCompletionHandler)
}
}
object ReadCompletionHandler : CompletionHandler<Int, Continuation<Int>> {
override fun completed(result: Int, attachment: Continuation<Int>) {
attachment.resume(result)
}
override fun failed(exc: Throwable, attachment: Continuation<Int>) {
attachment.resumeWithException(exc)
}
}
|