#define GAC_HEADER_USE_NAMESPACE #include "UI/Source/Demo.h" using namespace vl::collections; using namespace vl::stream; using namespace vl::reflection::description; using namespace demo; Ptr folderImage; Ptr contactBigImage; Ptr contactSmallImage; class Contact; class ViewModel; class Category : public Object, public ICategory { friend class ViewModel; friend class Contact; protected: ICategory* parent; WString name; ObservableList> folders; ObservableList> contacts; public: Category(ICategory* _parent) :parent(_parent) { } ICategory* GetParent()override { return parent; } WString GetName()override { return name; } vl::Ptr GetImage()override { return folderImage; } vl::Ptr GetFolders()override { return UnboxValue>(BoxParameter(folders)); } vl::Ptr GetContacts()override { return UnboxValue>(BoxParameter(contacts)); } }; class Contact : public Object, public IContact { protected: Category* category; WString name; DateTime birthday; WString phone; WString address; public: Contact(Category* _category) :category(_category) { } Category* GetCategory() { return category; } WString GetName()override { return name; } vl::Ptr GetBigImage()override { return contactBigImage; } vl::Ptr GetSmallImage()override { return contactSmallImage; } DateTime GetBirthday()override { return birthday; } WString GetBirthdayText()override { List formats; Locale::UserDefault().GetShortDateFormats(formats); if (formats.Count() > 0) { return Locale::UserDefault().FormatDate(formats[0], birthday); } return L""; } WString GetPhone()override { return phone; } WString GetAddress() { return address; } void Update(const WString& _name, DateTime _birthday, const WString& _phone, const WString& _address)override { name = _name; birthday = _birthday; phone = _phone; address = _address; vint index = category->contacts.IndexOf(this); if (index != -1) { category->contacts.NotifyUpdate(index, 1); } } }; class StaticCategory : public Category { public: StaticCategory() :Category(nullptr) { name = L"My Address Book"; } }; class RootCategory : public Object, public ICategory { protected: ObservableList> folders; public: RootCategory() { folders.Add(new StaticCategory); } ICategory* GetParent()override { return nullptr; } WString GetName()override { return L""; } vl::Ptr GetImage()override { return nullptr; } vl::Ptr GetFolders()override { return UnboxValue>(BoxParameter(folders)); } vl::Ptr GetContacts()override { return nullptr; } }; class ViewModel : public Object, public IViewModel { protected: vl::Ptr rootCategory; vl::Ptr selectedCategory; vl::Ptr selectedContact; public: ViewModel() { rootCategory = new RootCategory; } vl::Ptr GetRootCategory()override { return rootCategory; } vl::Ptr GetSelectedCategory()override { return selectedCategory; } void SetSelectedCategory(Ptr value)override { if (selectedCategory != value) { selectedCategory = value; SelectedCategoryChanged(); } } vl::Ptr GetSelectedContact()override { return selectedContact; } void SetSelectedContact(Ptr value)override { if (selectedContact != value) { selectedContact = value; SelectedContactChanged(); } } void AddCategory(const WString& name) { if (auto current = dynamic_cast(selectedCategory.Obj())) { auto category = new Category(current); category->name = name; current->folders.Add(category); } } void RemoveCategory() { if (auto parent = dynamic_cast(selectedCategory->GetParent())) { parent->folders.Remove(selectedCategory.Obj()); SetSelectedCategory(nullptr); } } vl::Ptr CreateContact()override { if (auto category = dynamic_cast(selectedCategory.Obj())) { auto contact = new Contact(category); contact->Update(L"Mr. New Contact", DateTime::LocalTime(), L"(123)-456-7890", L"110 st, New York, NY 10118"); return contact; } return nullptr; } void AddContact(Ptr contact)override { dynamic_cast(contact.Obj())->GetCategory()->contacts.Add(contact); } void RemoveContact()override { if (auto contact = dynamic_cast(selectedContact.Obj())) { contact->GetCategory()->contacts.Remove(selectedContact.Obj()); } } }; void GuiMain() { { FileStream fileStream(L"../UIRes/AddressBook.bin", FileStream::ReadOnly); auto resource = GuiResource::LoadPrecompiledBinary(fileStream); GuiResourceError::List errors; GetResourceManager()->SetResource(resource, errors); folderImage = resource->GetImageByPath(L"Images/Folder"); contactBigImage = resource->GetImageByPath(L"Images/ContactBig"); contactSmallImage = resource->GetImageByPath(L"Images/ContactSmall"); } MainWindow window(new ViewModel); window.MoveToScreenCenter(); GetApplication()->Run(&window); folderImage = nullptr; contactBigImage = nullptr; contactSmallImage = nullptr; }