kotlin

How can I use vararg with different generics in Kotlin?

不羁岁月 提交于 2020-07-22 10:08:04
问题 I want to use vararg with generics with different types of each arguments what I've already tried: class GeneralSpecification<T> { fun <P> ifNotNullCreateSpec(vararg propToCreateFun: Pair<P?, (P) -> Specification<T>>): List<Specification<T>> = propToCreateFun.mapNotNull { (prop, funCreateSpec) -> prop?.let(funCreateSpec) } ... } but I can't use this like: ifNotNullCreateSpec("asdf" to ::createStringSpec, 5 to ::createIntSpec) (different types in vararg pairs) How can I use vararg with

Kotlin - use action parameter in repeat function

*爱你&永不变心* 提交于 2020-07-22 06:14:11
问题 I know how to pass times as the first parameter of repeat function: repeat(3) { println("This will print 3 times") } But I checked Kotlin documentation, it shows there's another parameter action to use(see kotlin doc): inline fun repeat(times: Int, action: (Int) -> Unit) I tried this code but failed with the error Expecting ')' : repeat(3, 2 -> anotherFun()) { println("This will show 2 times?") } fun anotherFun() { println("head into the 2nd time and print this out.") } I know I have got the

ViewPager2 with Tablayout inside a fragment shows no swipe effect in kotlin

99封情书 提交于 2020-07-22 05:13:47
问题 In my MainActivity I have a viewpager2 with Tablayout with 5 tabs. The swipe there is working fine. But in one of the 5 tabs I again have a viewpager2 with a tablayout of 3 tabs. In the inner viewpager2 (which is inside a fragment) the swipe isnt working at all and I don't understand why. The outer viewpager2 is inside a activity while the inner viewpager2 is in the fragment Viewpager2 inside fragment , swipe not working : class FreelancerMyProjectsFragment : Fragment() { lateinit var binding

Dagger2 - How to use @Named with @BindsInstance

六眼飞鱼酱① 提交于 2020-07-21 07:30:28
问题 How is @Named used with @BindsInstance? I have the this component interface AppComponent : AndroidInjector<MyApplication>{ @Component.Builder abstract class Builder : AndroidInjector.Builder<MyApplication>() { @BindsInstance abstract fun preferenceName( @Named("PreferenceName") name : String ) : Builder } } and trying to inject in MyApplication @Inject @Named("PreferenceName") lateinit var prefName : String But it fails with MissingBinding for String. I could resolve this with a module

Error [Dagger/MissingBinding] androidx.lifecycle.ViewModelProvider.Factory cannot be provided without an @Provides-annotated method

淺唱寂寞╮ 提交于 2020-07-21 07:11:10
问题 I am facing an issue while implementing MultiBinding using dagger 2.2. I am using dagger with MVVM architecture. I have injected the ViewModelProvideFactory constructor and binds the dependency from module. I have followed the tutorial of Mitch from youtube https://www.youtube.com/watch?v=DToD1W9WdsE&list=PLgCYzUzKIBE8AOAspC3DHoBNZIBHbIOsC&index=13 I have searched on these links for the solutions but still facing the same issue. Dagger2: ViewModel cannot be provided without an @Provides

what is the difference between sortBy - sortedBy and sortWith - sortedWith in kotlin

喜你入骨 提交于 2020-07-21 06:31:30
问题 I'm just exploring kotlin collection and I observed one important behavior. val sports = listOf<Sports>( Sports("cricket", "7"), Sports("gilli", "10"), Sports("lagori", "8"), Sports("goli", "6"), Sports("dabba", "4") ) sports.sortedBy { it.rating } // sortedByDescending is to sort in descending .forEach({ println("${it.name} ${it.rating}") }) } class Sports(name: String, rating: String) { var name: String = name var rating: String = rating } above I can only get sortedBy method i.e which

what is the difference between sortBy - sortedBy and sortWith - sortedWith in kotlin

谁都会走 提交于 2020-07-21 06:31:07
问题 I'm just exploring kotlin collection and I observed one important behavior. val sports = listOf<Sports>( Sports("cricket", "7"), Sports("gilli", "10"), Sports("lagori", "8"), Sports("goli", "6"), Sports("dabba", "4") ) sports.sortedBy { it.rating } // sortedByDescending is to sort in descending .forEach({ println("${it.name} ${it.rating}") }) } class Sports(name: String, rating: String) { var name: String = name var rating: String = rating } above I can only get sortedBy method i.e which

Kotlin MutableList initial capacity

前提是你 提交于 2020-07-21 04:11:27
问题 I'm creating a list of values, in a context where it so happens that, though the values are being added one at a time, the eventual number is known in advance. This is in a function that will be called many times, so the faster it runs, the better. In Java, I would use the ArrayList constructor that specifies an initial capacity, because in theory this makes it slightly faster because it avoids resizing. In Kotlin, one normally uses mutableListOf(), but this does not allow an initial capacity

Encrypt/Decrypt String Kotlin

久未见 提交于 2020-07-21 04:03:27
问题 I've created this two extensions in Kotlin to Encrypt/Decrypt strings: fun String.encrypt(seed : String): String { val keyGenerator = KeyGenerator.getInstance("AES") val secureRandom = SecureRandom.getInstance("SHA1PRNG") secureRandom.setSeed(seed.toByteArray()) keyGenerator.init(128, secureRandom) val skey = keyGenerator.generateKey() val rawKey : ByteArray = skey.encoded val skeySpec = SecretKeySpec(rawKey, "AES") val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") cipher.init(Cipher

AndroidThreeTen not working in unit test without robolectric?

谁都会走 提交于 2020-07-20 21:03:01
问题 I'm having trouble creating a unit test without needing robolectric. I am using AndroidThreeTen.init(this) in my code and when I run my test if I disable robolectric I get an error: org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered and if I leave it enabled I get this: [Robolectric] com.mycomp.,yapp.utilities.log.LogTest.on Calling function w it returns an Int: sdk=28; resources=BINARY I have tried using testImplementation ‘com.jakewharton.threetenabp:threetenabp:1.1