Can't use custom getter with delegated property - properties

I am new to kotlin and can't figure out this issue.
I have a property that is not nullable and may or may not be used.
Hence I have delegated it in a lazy way so that it is initialized when required.
private val labelAnimator: ObjectAnimator by lazy {
ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
}
However, I also need to set some property of that object every time it is accessed.
fun getLabelAnimator(): ObjectAnimator {
labelAnimator.duration = (if (isFloatingLabelAnimating) 300 else 0).toLong()
return labelAnimator
}
But I cannot use a custom getter since the property is delegated.
How do I achieve this in best possible way?

You could for example use your original property as Backing Property and actually define the property as a delegation to it.
private val _labelAnimator by lazy {
ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
}
val labelAnimator: ObjectAnimator
get() {
_labelAnimator.duration = if (isFloatingLabelAnimating) 300L else 0L
return _labelAnimator
}
This gives you a getLabelAnimator() method in Java and direct property access in Kotlin.
But like it was stated several times before, it might not be the best way to implement such behavior. A simple factory could do the same with little overhead.

I am not a Kotlin expert, but it sounds wrong to me.
By delegating to lazy you want to init object on its first getter call. And then, you are trying to write custom getter.
I see a conflict there.
Maybe:
private val labelAnimator: ObjectAnimator by lazy {
val labelAnimator = ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
labelAnimator.duration = (if (isFloatingLabelAnimating) 300 else 0).toLong()
labelAnimator
}

The shortest version
private val labelAnimator: ObjectAnimator by lazy {
ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f).apply {
duration = if (isFloatingLabelAnimating) 300L else 0L
}
}

Related

Jetpack Compose correct accessing of data from ViewModel

today I'm trying about Jetpack compose and the ViewModel class.
Everything seems to be logic but I got some problems understanding the encapsulation.
// ViewModel
class MyViewModel: ViewModel() {
var uiStateList = mutableStateListOf(1,2,3)
privat set
private var _uiStateList2 = mutableStateListOf(11,22,33)
val uiState2 List<Int>
get() = _uiStateList2
fun addToUiStateList2() {
_uiStateList2.add(_uiStateList2.last()+11)
}
}
The first var is like in the Compose tutorials.
The construction of the second var looks like using MutableLiveData.
// Activity
#Composable
fun MyComposable(
modifier: Modifier = Modifier,
myViewModel: MyViewModel= viewModel()
) {
val uiStateList = myViewModel.uiStateList
val uiStateList2 = myViewModel.uiStateList2
Column {
uiStateList.forEach {
Text(it.toString())
}
Button(
onclick = {uiStateList.add(uiStateList.last()+1)},
content = {text(+1)}
)
uiStateList2.forEach {
Text(it.toString())
}
Button(
onclick = {
// uiStateList2.add(uiStateList2.last()+11) // not possible because of type List<Int>
myViewModel.addToUiStateList2() // possible, changing indirectly the value of uiStateList2
},
content = {text(+11)}
)
}
}
In the first case it's possible to modify the mutableStateList inside of the ViewModel by running just a simple function in the composable. Consequently it is possible to change the value from outside of the class directly.
In the second case I got no chance to change the data in the viewmodel. The var uiStateList2 is a imutable list which reflects the data from the private val from the viewmodel. If the function addToUiStateList2() is triggered, the original list changes and the composable will be recompositioned and everything is fine.
Why can I change the data of the var uiStateList inside the Composable, although the setter is set to private inside the ViewModel class?
In the documentation I read, that private setters could just be used inside the owning class. Do I think too complicated, or is this the usually aproach how everything is build?
Thanks for help Guys!
Private setter doesn't allow to set the value i.e myViewModel.uiStateList= //something. But you can still modify the list because it is mutable. If you want to restrict changing the state from outside viewModel second approach is preferred.

What is the use of open properties in Kotlin?

I have been learning Kotlin and have come across the concept of open properties. Coming from C++, the concept of "open" makes sense, and extending that logic to properties does as well. However, I can't think of any case where an open val/var is actually necessary or useful. I understand when they make sense for interfaces, but not concrete classes. Furthermore, overriding getters/setters makes sense, but not redefining the property with a new backing field. For example, say you have this kind of class structure:
open class Foo {
open var str = "Hello"
}
class Bar : Foo() {
override var str = "world"
init {
println(str)
println(super.str) // Shows that Bar actually contains "hello" and "world"
}
}
To me, it would seem to be a far better design to make Foo take str as a constructor argument, for instance:
open class Foo(var str = "Hello") // Maybe make a secondary constructor
class Bar : Foo("world") // Bar has only 1 string
This is both more concise, and seems to often be a better design. This is also the way it tends to be done in C++, so maybe I just don't see the benefit of the other way. The only possible time I can see overriding a val/var with a new one is if it for some reason needs to use super's value, like in
override val foo = super.foo * 2
Which still seems pretty contrived.
When have you found this useful? Does it allow for greater efficiency or ease of use?
open fields let you re-define getter and setter methods. It's practically pointless if you just return constants. However altering getter / setter behavior has (infinite) potential, so I'll just throw some ideas:
// propagate get/set to parent class
class Bar : Foo() {
override var str
get() = super.str.toUpperCase()
set(value) {
super.str = value
}
}
// creates a backing field for this property
class Bar : Foo() {
override var str = "World"
get() = field.toLowerCase()
// no need to define custom set if we don't need it in this case
// set(value) { field = value }
}
// instead of writing custom get/set, you can also use delegates
class Bar : Foo() {
override var str by Delegates.observable("world"){ prop, old, new ->
println("${prop.name} changed from $old to $new")
}
}

bind 'lateinit' modifier with condition in Kotlin

In Kotlin, we have lateinit modifier to lazy variable initialization instead of var something: Something? = null.
In my situation, I have a list of the Element, and I want to assign it to the lateinit variable when I having the first object.
So, I tried several methods to achieve this.
First, using 'firstOrNull()' methods
lateinit var applicationHolder: ApplicationHolder
applicationHolder = env.getElementsAnnotatedWith(InjectApplication::class.java)
.map {
ApplicationHolder(it, (it as TypeElement).asClassName(), it.simpleName.toString()).apply {
val component = it.getAnnotation(InjectApplication::class.java).getComponent()
componentClass = component
}
}.firstOrNull()
The first solution was failed because applicationHolder doesn't accept the Nullable type of ApplicationHolder. (Type inference failed. Expected type mismatch: inferred type is ApplicationHolder? but ApplicationHolder was expected.)
Although I can use first instead of firstOrNull to achieve this, it's too dangerous because the list can be empty.
Second, using if-condition
val item = env.getElementsAnnotatedWith(InjectApplication::class.java)
.map {
ApplicationHolder(it, (it as TypeElement).asClassName(), it.simpleName.toString()).apply {
val component = it.getAnnotation(InjectApplication::class.java).getComponent()
componentClass = component
}
}.firstOrNull()
if (item != null) {
applicationHolder = item
}
The second solution was succeeded to compile and working well.
Third, using backing properties (Actually, this solution don't use lateinit modifier)
val applicationHolder: ApplicationHolder
get() {
return _applicationHolder ?: throw NullPointerException("Not initialized")
}
private var _applicationHolder: ApplicationHolder? = null
_applicationHolder = env.getElementsAnnotatedWith(InjectApplication::class.java)
.map {
ApplicationHolder(it, (it as TypeElement).asClassName(), it.simpleName.toString()).apply {
val component = it.getAnnotation(InjectApplication::class.java).getComponent()
componentClass = component
}
}.firstOrNull()
The third solution was succeeded to compile and working well.
In short, my question is as follows.
Is there a better solution to achieve my goal than these solutions?
If another solution doesn't exist, which is a clean or better solution? I can use the second or third solution but I have no confidence which is clean or better.
Why are you using lateinit over a nullable type here?
This strikes warning bells to me :
it's too dangerous because the list can be empty.
If you try to access a lateinit object without initialising it, your application will crash. Lateinit should be use if it will definitely be initialised.
I would change the code to what you were avoiding : var something: Something? = null
Then use the firstOrNull() method. The kotlin null safety type system will enforce you deal with nulls, leading to safe code!
It is possible to check whether a lateinit has been initialised, by calling this::applicationHolder.isInitialized.  (This was introduced in Kotlin 1.2; info here.)
However, as per #user8159708's answer, it still smells a bit.  Reworking to make the property nullable will make it slightly more awkward to access, but much safer.

How to inject dependency using koin in top level function

I have top-level function like
fun sendNotification(context:Context, data:Data) {
...//a lot of code here
}
That function creates notifications, sometimes notification can contain image, so I have to download it. I`m using Glide which is wrapped over interface ImageManager, so I have to inject it. I use Koin for DI and the problem is that I cannot write
val imageManager: ImageManager by inject()
somewhere in my code, because there is no something that implements KoinComponent interface.
The most obvious solution is to pass already injected somewhere else imageManager as parameter of function but I dont want to do it, because in most cases I dont need imageManager: it depends on type of Data parameter.
Easiest way is to create KoinComponent object as wrapper and then to get variable from it:
val imageManager = object:KoinComponent {val im: ImageManager by inject()}.im
Btw its better to wrap it by some function, for example I use
inline fun <reified T> getKoinInstance(): T {
return object : KoinComponent {
val value: T by inject()
}.value
}
So if I need instance I just write
val imageManager:ImageManager = getKoinInstance()
or
val imageManager = getKoinInstance<ImageManager>()
I did it in this way
fun Route.general() {
val repo: OperationRepo by lazy { GlobalContext.get().koin.get() }
...
}

How to write a package-level static initializer in Kotlin?

A previous question shows how to put a static initializer inside a class using its companion object. I'm trying to find a way to add a static initializer at the package level, but it seems packages have no companion object.
// compiler error: Modifier 'companion' is not applicable inside 'file'
companion object { init { println("Loaded!") } }
fun main(args: Array<String>) { println("run!") }
I've tried other variations that might've made sense (init on its own, static), and I know as a workaround I can use a throwaway val as in
val static_init = {
println("ugly workaround")
}()
but is there a clean, official way to achieve the same result?
Edit: As #mfulton26's answer mentions, there is no such thing as a package-level function really in the JVM. Behind the scenes, the kotlin compiler is wrapping any free functions, including main in a class. I'm trying to add a static initializer to that class -- the class being generated by kotlin for the free functions declared in the file.
Currently there is no way to add code to the static constructor generated for Kotlin file classes, only top-level property initializers are getting there. This sounds like a feature request, so now there is an issue to track this: KT-13486 Package-level 'init' blocks
Another workaround is to place initialization in top-level private/internal object and reference that object in those functions that depend on the effect of that initialization. Objects are initialized lazily, when they are referenced first time.
fun dependsOnState(arg: Int) = State.run {
arg + value
}
private object State {
val value: Int
init {
value = 42
println("State was initialized")
}
}
As you mentioned, you need a property with something that would run on initialisation:
val x = run {
println("The package class has loaded")
}
I got around it by using a Backing Property on the top-level, under the Kotlin file. Kotlin Docs: Backing Properties
private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
get() {
if (_table == null) {
_table = HashMap() // Type parameters are inferred
// .... some other initialising code here
}
return _table ?: throw AssertionError("Set to null by another thread")
}