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

Data Driven TreeView #1356

Open
toughstumpryan opened this issue May 13, 2022 · 1 comment
Open

Data Driven TreeView #1356

toughstumpryan opened this issue May 13, 2022 · 1 comment

Comments

@toughstumpryan
Copy link

toughstumpryan commented May 13, 2022

Hello TornadoFx community!

I was hoping to get some clarification on a point in the Guide w.r.t. a data drive TreeView. I'm having trouble getting this working, and am wondering what I might be missing.

Here is my ViewModel:

class ContactViewModel : ViewModel() {

    private val activeMapProperty = mapProperty(observableMapOf<String, ServiceAnnouncement>())
    private val activeProperty = listProperty(observableListOf<ServiceAnnouncement>())
    val active: ObservableList<ServiceAnnouncement> by activeProperty  // explicit type due to platform call
    
    init { 
        AddressBook.ContactAdded += ::contactAdded
        activeProperty.bind(nonNullObjectBinding(activeMapProperty) { observableListOf(values) } )
    }
    
    private fun contactAdded(sender: Any, contact: ServiceAnnouncement) {
        activeMapProperty.putIfAbsent(contact.uid, contact)
    }
}

And in my View class:

class ContactTree : View() { 
    private val contactViewModel: ContactViewModel by inject()
    
    override val root: Parent = treeview<Any> {
        root = TreeItem("Active")
        root.isExpanded = true

        populate { parent ->
            when (val v = parent.value) {
                "Active" -> contactViewModel.active
                else -> null
            }
        }

        onUserSelect { println("$it") }
    }
}

From the Guide section on Data Driven TreeView:

If the child list you return from populate is an ObservableList, any changes to that list will automatically be reflected in the TreeView. The populate function will be called for any new children that appears, and removed items will result in removed TreeItems as well.

As you can see, my ContactViewModel::active property is type ObservableList<ServiceAnnouncement>, which meets the "if the child list you return is an ObservableList" criteria. And I was able to verify through the IntelliJ debugger that when ContactViewModel::contactAdded is called that all of activeMapProperty, activeProperty, and active reflect the update, but nothing happens in my UI.

Any advice would be greatly appreciated!

@toughstumpryan
Copy link
Author

Update: I found that by using activeProperty directly things work as I expected. I wonder if anyone could offer insight as to why?

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