address book using mvvm
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
using AddressBook_MVVMSampleV2.Interface;
|
||||
using AddressBook_MVVMSampleV2.Model;
|
||||
using ReactiveUI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace AddressBook_MVVMSampleV2.ViewModel
|
||||
{
|
||||
public class MainViewModel : ReactiveObject
|
||||
{
|
||||
public ObservableCollection<Person> Persons { get; set; }
|
||||
|
||||
public ICommand AddCommand { get; set; }
|
||||
public ICommand DeleteCommand { get; set; }
|
||||
public ICommand ModifyCommand { get; set; }
|
||||
public ICommand ExitCommand { get; set; }
|
||||
|
||||
private readonly IMessageBoxService MessageBoxService;
|
||||
private readonly Func<Person, AddViewModel.ViewType, IDialogView> CreateAddView;
|
||||
|
||||
public MainViewModel() : this(null, null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MainViewModel(IMessageBoxService messageBoxService, Func<Person, AddViewModel.ViewType, IDialogView> createAddView)
|
||||
{
|
||||
this.MessageBoxService = messageBoxService;
|
||||
this.CreateAddView = createAddView;
|
||||
|
||||
this.Persons = new ObservableCollection<Person>();
|
||||
|
||||
InitInstance();
|
||||
}
|
||||
|
||||
private void InitInstance()
|
||||
{
|
||||
// Init Commands
|
||||
this.AddCommand = ReactiveCommand.Create<Unit>(CommandAdd);
|
||||
this.ModifyCommand = ReactiveCommand.Create<int, Unit>(index => CommandModify(index));
|
||||
this.DeleteCommand = ReactiveCommand.Create<int, Unit>(index => CommandDelete(index));
|
||||
this.ExitCommand = ReactiveCommand.Create<IWindowView, Unit>(view => CommandExit(view));
|
||||
|
||||
// Init Default Data
|
||||
this.Persons.Add(new Person() { Name = "Kim", Gender = true, PhoneNumber = "123-4567", Address = "KOR" });
|
||||
this.Persons.Add(new Person() { Name = "Max", Gender = true, PhoneNumber = "123-4567", Address = "USA" });
|
||||
this.Persons.Add(new Person() { Name = "John", Gender = true, PhoneNumber = "123-4567", Address = "UK" });
|
||||
this.Persons.Add(new Person() { Name = "Richard", Gender = true, PhoneNumber = "123-4567", Address = "UK" });
|
||||
this.Persons.Add(new Person() { Name = "Shinoda", Gender = true, PhoneNumber = "123-4567", Address = "JPN" });
|
||||
}
|
||||
|
||||
private Unit CommandAdd()
|
||||
{
|
||||
Person addData = new Person();
|
||||
IDialogView view = this.CreateAddView(addData, AddViewModel.ViewType.Add);
|
||||
if (view.ShowDialog() != true)
|
||||
return Unit.Default;
|
||||
|
||||
this.Persons.Add(addData);
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
private Unit CommandDelete(int selectedIndex)
|
||||
{
|
||||
if (selectedIndex < 0)
|
||||
{
|
||||
this.MessageBoxService.Show("No selected data.", "AddressBook", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
MessageBoxResult result = this.MessageBoxService.Show("Are you sure to delete?", "AddressBook", MessageBoxButton.OKCancel, MessageBoxImage.Question);
|
||||
if (result != MessageBoxResult.OK)
|
||||
return Unit.Default;
|
||||
|
||||
this.Persons.RemoveAt(selectedIndex);
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
private Unit CommandModify(int selectedIndex)
|
||||
{
|
||||
if (selectedIndex < 0)
|
||||
{
|
||||
this.MessageBoxService.Show("No selected data.", "AddressBook", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
Person modifyData = new Person(this.Persons[selectedIndex]);
|
||||
IDialogView view = this.CreateAddView(modifyData, AddViewModel.ViewType.Modify);
|
||||
if (view.ShowDialog() != true)
|
||||
return Unit.Default;
|
||||
|
||||
this.Persons[selectedIndex] = modifyData;
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
private Unit CommandExit(IWindowView view)
|
||||
{
|
||||
view.Close();
|
||||
return Unit.Default;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user