kotlin

Moshi in Android Kotlin - ENUM as MutableMap key being converted to String when deseralized

柔情痞子 提交于 2021-02-08 04:10:12
问题 I have a MutableMap<CryptoTypes, CurrentTradingInfo> that I'm wanting to save in onSaveInstanceState and was going to use Moshi to convert back and forth. CryptoTypes is an ENUM private var tickerData: MutableMap<CryptoTypes, CurrentTradingInfo> = mutableMapOf() fun convertTickerDataJson(): String { val moshi = Moshi.Builder().build() val jsonAdapter = moshi.adapter<MutableMap<CryptoTypes, CurrentTradingInfo>>(MutableMap::class.java) return jsonAdapter.toJson(tickerData) } fun

Should a library function be suspend or return deferred

家住魔仙堡 提交于 2021-02-07 20:29:00
问题 Let's assume I'm writing a library that returns a string which is a complex and long running task. I can chose between offering this interface StringGenerator { suspend fun generateString(): String } or interface StringGenerator { fun generateString(): Deferred<String> } Are there any (dis-)advantages of either of the options and which are they? Which should I choose? 回答1: Kotlin coroutines are designed along the "sequential by default" guideline. That means that your API should always expose

How to have generic ViewModel in BaseActivty class

怎甘沉沦 提交于 2021-02-07 20:16:45
问题 I want to have a base activity class that takes care of some initialization I started defining it like this. abstract class BaseActivity<VIEW_MODEL : ViewModel, BINDING : ViewDataBinding> : AppCompatActivity() { lateinit var viewmodel: VIEW_MODEL lateinit var binding: BINDING lateinit var glide: RequestManager @get:LayoutRes abstract val layoutResource: Int override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this,

How to have generic ViewModel in BaseActivty class

守給你的承諾、 提交于 2021-02-07 20:14:27
问题 I want to have a base activity class that takes care of some initialization I started defining it like this. abstract class BaseActivity<VIEW_MODEL : ViewModel, BINDING : ViewDataBinding> : AppCompatActivity() { lateinit var viewmodel: VIEW_MODEL lateinit var binding: BINDING lateinit var glide: RequestManager @get:LayoutRes abstract val layoutResource: Int override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this,

Why it is not a tail recursion?

China☆狼群 提交于 2021-02-07 20:11:29
问题 I have the following code, that I do not understand, why it is not a tail recursion: override fun drop(n: Int): List<A> = if (n == 0) this else tail.drop(n - 1) whereas this is a tail recursion: fun drop(n: Int): List<A> { tailrec fun drop(n: Int, list: List<A>): List<A> = if (n <= 0) list else when (list) { is Cons -> drop(n - 1, list.tail) is Nil -> list } return drop(n, this) } Why is the first example not a tail recursion? 回答1: It isn't tail recursion because Kotlin checks the recursive

How to set width and track text in a Switch/SwitchCompat button and achieve this result? (Image and GIF attached)

这一生的挚爱 提交于 2021-02-07 19:22:36
问题 I need to implement a button in my app like this I used a SwitchCompat button but the closest I arrived was to this point, having two main problems: 1 - The width of the button does not adjust correctly when screen sizes change (drawable gets cut off, become too small etc), it is important that the width occupies the parent view correctly ( a small linear layout enclosing it) 2 - I was not able to understand how I could get the letters in the Switch Track Is it possible to achieve this result

Android only setStackFromEnd only if RecycleView/List is larger than screen

送分小仙女□ 提交于 2021-02-07 19:16:56
问题 I have a RecyclerView that I want to start showing the bottom items, so I'm using: myLayoutManager.setStackFromEnd(true) . It works just as I want when there are enough items to fill the screen, but if there are only a few items in the RecyclerView, it pushes them down and leaves empty space at the top of the screen Is there any way prevent this, or to only setStackFromEnd if the RecyclerView is larger than the screen height? Here's an example of what I get and what I'm trying to get: 回答1:

Android only setStackFromEnd only if RecycleView/List is larger than screen

不羁的心 提交于 2021-02-07 19:16:05
问题 I have a RecyclerView that I want to start showing the bottom items, so I'm using: myLayoutManager.setStackFromEnd(true) . It works just as I want when there are enough items to fill the screen, but if there are only a few items in the RecyclerView, it pushes them down and leaves empty space at the top of the screen Is there any way prevent this, or to only setStackFromEnd if the RecyclerView is larger than the screen height? Here's an example of what I get and what I'm trying to get: 回答1:

org.mockito.exceptions.misusing.MissingMethodInvocationException in MOCKITO

删除回忆录丶 提交于 2021-02-07 18:22:18
问题 I am having a simple controller class @RestController open class MyController() { @Autowired lateinit var myInterface: MyInterface @GetMapping(value = ["/v1/call-Api"], produces = ["application/json"]) fun getData():Response{ callFx() /// Here I have logic } fun callFx():String{ return myInterface.getmyStringData() } } Now Come to implementation part of MyInterface @Service class MyImpl: MyInterface { override fun getmyStringData(){ return "Some string" } } Please note that for MyInterface ,

kotlin, how to simplify passing parameters to base class constructor?

前提是你 提交于 2021-02-07 18:17:27
问题 We have a package that we are looking to convert to kotlin from python in order to then be able to migrate systems using that package. Within the package there are a set of classes that are all variants, or 'flavours' of a common base class. Most of the code is in the base class which has a significant number of optional parameters. So consider: open class BaseTree(val height:Int=10,val roots:Boolean=true, //...... lots more!! class FruitTree(val fruitSize, height:Int=10, roots:Boolean=true,