package todomvcfx.mvvmfx.ui.additems;

import de.saxsys.mvvmfx.ViewModel;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import org.fxmisc.easybind.EasyBind;
import todomvcfx.mvvmfx.model.TodoItem;
import todomvcfx.mvvmfx.model.TodoItemStore;

/**
 * @author manuel.mauky
 */
public class AddItemsViewModel implements ViewModel {
	
	
	private BooleanProperty allSelected = new SimpleBooleanProperty();
	private StringProperty newItemValue = new SimpleStringProperty("");
	
    private ReadOnlyBooleanWrapper allSelectedVisible = new ReadOnlyBooleanWrapper();

    private boolean itemsListenerActive = false;

	public AddItemsViewModel() {
		allSelectedVisible.bind(Bindings.isEmpty(TodoItemStore.getInstance().getItems()).not());


        // create a list that will fire update events when completed flag is updated.
        ObservableList<TodoItem> items = FXCollections.observableArrayList(item -> new Observable[]{item.completedProperty()});
        EasyBind.listBind(items, TodoItemStore.getInstance().getItems());

        items.addListener((ListChangeListener<TodoItem>) change -> {
            if(itemsListenerActive) {
                allSelected.setValue(
                        TodoItemStore.getInstance().getItems().stream().allMatch(TodoItem::isCompleted));
            }
        });

        itemsListenerActive = true;
	}

    public void selectAll() {
        itemsListenerActive = false;
        allSelected.setValue(!allSelected.get());
        TodoItemStore.getInstance().getItems().forEach(item -> item.setCompleted(allSelected.getValue()));
        itemsListenerActive = true;
    }
	
	
	public void addItem() {
		final String newValue = newItemValue.get();
		if (newValue != null && !newValue.trim().isEmpty()) {
			TodoItemStore.getInstance().getItems().add(new TodoItem(newValue));
			newItemValue.set("");
		}
	}
	
	public StringProperty newItemValueProperty() {
		return newItemValue;
	}
	
	
	public BooleanProperty allSelectedProperty() {
		return allSelected;
	}
	
	public ReadOnlyBooleanProperty allSelectedVisibleProperty() {
		return allSelectedVisible;
	}
}

@@ -67,16 +67,52 @@
 wModel;%0A
+import javafx.application.Platform;%0A
 import j
@@ -607,45 +607,8 @@
 st;%0A
-import org.fxmisc.easybind.EasyBind;%0A
 impo
@@ -1021,119 +1021,34 @@
 ate 
-boolean itemsListenerActive = false;%0A%0A%09public AddItemsViewModel() %7B%0A%09%09allSelectedVisible.bind(Bindings.isEmpty(
+TodoItemStore itemStore = 
 Todo
@@ -1074,35 +1074,16 @@
 ce()
-.getItems()).not())
 ;%0A%0A%0A    
     
@@ -1082,32 +1082,54 @@
     
-   
- 
 // 
-create a
+In contrast to the ItemStore
  list
+,
  th
-a
+is lis
 t wi
@@ -1146,20 +1146,18 @@
 date
- events w
+s when t
 he
-n
  com
@@ -1168,16 +1168,32 @@
 ed flag 
+of the elements 
 is updat
@@ -1200,19 +1200,23 @@
 ed.%0A    
-   
+private
  Observa
@@ -1233,21 +1233,45 @@
 doItem%3E 
-items
+todoItemsWithCompletedUpdater
  = FXCol
@@ -1356,41 +1356,85 @@
 %7D);%0A
-        EasyBind.listBind(items, 
+%0A%09public AddItemsViewModel() %7B%0A%09%09allSelectedVisible.bind(Bindings.isNotEmpty(
 Todo
@@ -1468,16 +1468,17 @@
 Items())
+)
 ;%0A%0A     
@@ -1480,21 +1480,129 @@
         
-items
+Bindings.bindContent(todoItemsWithCompletedUpdater, itemStore.getItems());%0A%0A        todoItemsWithCompletedUpdater
 .addList
@@ -1638,21 +1638,16 @@
 Item%3E) c
-hange
  -%3E %7B%0A  
@@ -1660,30 +1660,22 @@
     
-if(itemsListenerActive
+while(c.next()
 ) %7B%0A
@@ -1694,81 +1694,40 @@
     
-allSe
+boolean allComp
 le
-c
 ted
-.setValue(%0A                        TodoItemStore.getInstance()
+ = itemStore
 .get
@@ -1778,24 +1778,10 @@
 ted)
-)
 ;%0A
-            %7D
 %0A   
@@ -1789,21 +1789,16 @@
     
-%7D);%0A%0A
         
 item
@@ -1797,156 +1797,128 @@
     
-itemsListenerActive = true;%0A%09%7D%0A%0A    public void selectAll() %7B%0A        itemsListenerActive = false;%0A        allSelected.setValue(!allSelected.get());
+Platform.runLater(() -%3E allSelected.setValue(allCompleted));%0A            %7D%0A        %7D);%0A%09%7D%0A%0A    public void selectAll() %7B
 %0A   
@@ -2025,53 +2025,14 @@
 ));%0A
-        itemsListenerActive = true;%0A
     %7D%0A
-%09%0A%09
 %0A%09pu
@@ -2334,19 +2334,16 @@
 lue;%0A%09%7D%0A
-%09%0A%09
 %0A%09public

Fix bug in mvvm implementation