예제 #1
0
        public MainViewModel()
        {
            // should be injected?
            users     = new UserService();
            companies = new CompanyService();

            Companies = new ObservableCollection <CompanyProperty>(companies.AsCompanyProperty());
            Companies.CollectionChanged += CollectionChanged;

            CompanySelectionChanged = new DelegateCommand(() =>
            {
                if (SelectedCompany != null)
                {
                    if (Users != null)
                    {
                        Users.CollectionChanged -= CollectionChanged;
                    }

                    var userProperties = users.AsUserProperty(e => e.CompanyId == SelectedCompany.Id);
                    Users = new ObservableCollection <UserProperty>(userProperties);

                    Users.CollectionChanged += CollectionChanged;
                }
            });

            CompanyEditEnd = new DelegateCommand(async() =>
            {
                if (SelectedCompany.Id == 0)
                {
                    var company        = await companies.AddAsync(SelectedCompany);
                    SelectedCompany.Id = company.Id;
                }
                else
                {
                    await companies.UpdateAsync(SelectedCompany);
                }
            });

            UserEditEnd = new DelegateCommand(async() =>
            {
                if (SelectedUser.Id == 0)
                {
                    User user      = SelectedUser;
                    user.CompanyId = SelectedCompany.Id;
                    await users.AddAsync(user);
                    SelectedUser.Id = user.Id;
                }
                else
                {
                    await users.UpdateAsync(SelectedUser);
                }
            });
        }