Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 11 - On Commit Callback - maybe issue (or the issue is me) #55

Open
mFrankowicz opened this issue May 18, 2018 · 0 comments
Open

Comments

@mFrankowicz
Copy link

Hello, I'm quite new to programming (but not new to attempt to programming), and just started migrating from JavaFX. i'm new to Kotlin and don't know if is a issue, it's about the code example below:

class PersonModel : ItemViewModel<Person>() {

    val firstname = bind(Person::firstName)
    val lastName = bind(Person::lastName)

    override val onCommit(commits: List<Commit>) {
       // The println will only be called if findChanged is not null 
       commits.findChanged(firstName)?.let { println("First-Name changed from ${it.first} to ${it.second}")}
       commits.findChanged(lastName)?.let { println("Last-Name changed from ${it.first} to ${it.second}")}
    }

    private fun <T> List<Commit>.findChanged(ref: Property<T>): Pair<T, T>? {
        val commit = find { it.property == ref && it.changed}
        return commit?.let { (it.newValue as T) to (it.oldValue as T) }
    }
}

if i'm right, the logic say's that it.first value references from it.newValue (the first value of the Pair<T, T>), and it.second from the second value, stored as it.oldValue. So, i think the print message should say that the commit value changed from a old value to a new value, but in my case the message prints the other way wound (from old to new):

text changed from 1 to 
text changed from 12 to 1
text changed from 123 to 12
text changed from 1234 to 123
text changed from 12345 to 1234
text changed from 123456 to 12345

should not the .findChanged function store the it.oldValue in the first pair's argument, or the println message reference from the it.second and then it.first?

if i'm not doing something very wrong, here are my suggestion:

class PersonModel : ItemViewModel<Person>() {

    val firstname = bind(Person::firstName)
    val lastName = bind(Person::lastName)

    override val onCommit(commits: List<Commit>) {
       // The println will only be called if findChanged is not null 
       commits.findChanged(firstName)?.let { println("First-Name changed from ${it.first} to ${it.second}")}
       commits.findChanged(lastName)?.let { println("Last-Name changed from ${it.first} to ${it.second}")}
    }

    private fun <T> List<Commit>.findChanged(ref: Property<T>): Pair<T, T>? {
        val commit = find { it.property == ref && it.changed}
        return commit?.let { (it.oldValue as T) to (it.newValue as T) }
    }
}

or:

class PersonModel : ItemViewModel<Person>() {

    val firstname = bind(Person::firstName)
    val lastName = bind(Person::lastName)

    override val onCommit(commits: List<Commit>) {
       // The println will only be called if findChanged is not null 
       commits.findChanged(firstName)?.let { println("First-Name changed from ${it.second} to ${it.first}")}
       commits.findChanged(lastName)?.let { println("Last-Name changed from ${it.second} to ${it.first}")}
    }

    private fun <T> List<Commit>.findChanged(ref: Property<T>): Pair<T, T>? {
        val commit = find { it.property == ref && it.changed}
        return commit?.let { (it.newValue as T) to (it.oldValue as T) }
    }
}

in my case i got it right:

text changed from  to 1
text changed from 1 to 12
text changed from 12 to 123
text changed from 123 to 1234
text changed from 1234 to 12345
text changed from 12345 to 123456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant