#include "CustomPropertyProviderBase.h"
#include "DelegateCommand.h"
#include "BookmarkViewModel.h"
using namespace Windows::UI::Xaml::Input;
using namespace Windows::Foundation::Collections;
using namespace Windows::Foundation;
using namespace Windows::System;
ref class MainViewModel sealed : public CustomPropertyProviderBase
{
String^ title;
String^ editAuthor;
String^ editTitle;
String^ editUrl;
int editId;
unsigned int editIndex;
BookmarkModel bookmarkModel;
public:
virtual void AddCustomProperties() override
{
AddCustomProperty("Title", "String", MAKEPROPGET(MainViewModel, Title), MAKEPROPSET(MainViewModel, Title, String^));
AddCustomProperty("UpdateTitleCommand", "ICommand", MAKEPROPGET(MainViewModel, UpdateTitleCommand), MAKEPROPSET(MainViewModel, UpdateTitleCommand, ICommand^));
AddCustomProperty("Bookmarks", "IObservableVector<Object>", MAKEPROPGET(MainViewModel, Bookmarks), nullptr);
AddCustomProperty("OpenCommand", "ICommand", MAKEPROPGET(MainViewModel, OpenCommand), MAKEPROPSET(MainViewModel, OpenCommand, ICommand^));
AddCustomProperty("EditCommand", "ICommand", MAKEPROPGET(MainViewModel, EditCommand), MAKEPROPSET(MainViewModel, EditCommand, ICommand^));
AddCustomProperty("DeleteCommand", "ICommand", MAKEPROPGET(MainViewModel, DeleteCommand), MAKEPROPSET(MainViewModel, DeleteCommand, ICommand^));
AddCustomProperty("SaveCommand", "ICommand", MAKEPROPGET(MainViewModel, SaveCommand), MAKEPROPSET(MainViewModel, SaveCommand, ICommand^));
AddCustomProperty("AddCommand", "ICommand", MAKEPROPGET(MainViewModel, AddCommand), MAKEPROPSET(MainViewModel, AddCommand, ICommand^));
AddCustomProperty("ShowListBox", "String", MAKEPROPGET(MainViewModel, ShowListBox), MAKEPROPSET(MainViewModel, ShowListBox, String^));
AddCustomProperty("ShowEditor", "String", MAKEPROPGET(MainViewModel, ShowEditor), nullptr);
AddCustomProperty("EditAuthor", "String", MAKEPROPGET(MainViewModel, EditAuthor), MAKEPROPSET(MainViewModel, EditAuthor, String^));
AddCustomProperty("EditTitle", "String", MAKEPROPGET(MainViewModel, EditTitle), MAKEPROPSET(MainViewModel, EditTitle, String^));
AddCustomProperty("EditUrl", "String", MAKEPROPGET(MainViewModel, EditUrl), MAKEPROPSET(MainViewModel, EditUrl, String^));
}
MainViewModel()
{
title = "Bookmarks - A basic Metro app using Visual C++";
showListBox = Visibility::Visible;
editId = -1;
UpdateTitleCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Update));
OpenCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Open));
EditCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Edit));
DeleteCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Delete));
SaveCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Save));
AddCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Add));
Bookmarks = ref new Vector<Object^>();
LoadData();
}
property String^ Title
{
String^ get()
{
return title;
}
void set(String^ value)
{
if(title != value)
{
title = value;
FirePropertyChanged("Title");
}
}
}
property String^ EditAuthor
{
String^ get()
{
return editAuthor;
}
void set(String^ value)
{
if(editAuthor != value)
{
editAuthor = value;
FirePropertyChanged("EditAuthor");
}
}
}
property String^ EditTitle
{
String^ get()
{
return editTitle;
}
void set(String^ value)
{
if(editTitle != value)
{
editTitle = value;
FirePropertyChanged("EditTitle");
}
}
}
property String^ EditUrl
{
String^ get()
{
return editUrl;
}
void set(String^ value)
{
if(editUrl != value)
{
editUrl = value;
FirePropertyChanged("EditUrl");
}
}
}
Visibility showListBox;
property String^ ShowListBox
{
String^ get()
{
return showListBox == Visibility::Collapsed ? "Collapsed" : "Visible";
}
void set(String^ value)
{
Visibility visibility = value == "Collapsed" ? Visibility::Collapsed : Visibility::Visible;
if(showListBox != visibility)
{
showListBox = visibility;
FirePropertyChanged("ShowListBox");
FirePropertyChanged("ShowEditor");
}
}
}
property String^ ShowEditor
{
String^ get()
{
return showListBox == Visibility::Collapsed ? "Visible" : "Collapsed";
}
}
void Update(Object^ parameter)
{
this->Title = "Did this fire?";
this->ShowListBox = "Visible";
}
property ICommand^ UpdateTitleCommand;
property ICommand^ OpenCommand;
void Open(Object^ parameter)
{
Launcher::LaunchDefaultProgram(ref new Uri((String^)parameter), LaunchingOptions::None);
}
property ICommand^ EditCommand;
void Edit(Object^ parameter)
{
BookmarkViewModel^ bookmarkViewModel = (BookmarkViewModel^)parameter;
Bookmarks->IndexOf(bookmarkViewModel, editIndex);
editId = bookmarkViewModel->Id;
this->EditAuthor = bookmarkViewModel->Author;
this->EditTitle = bookmarkViewModel->Title;
this->EditUrl = bookmarkViewModel->Url;
this->ShowListBox = "Collapsed";
}
property ICommand^ DeleteCommand;
void Delete(Object^ parameter)
{
BookmarkViewModel^ bookmarkViewModel = (BookmarkViewModel^)parameter;
unsigned int index;
if(Bookmarks->IndexOf(bookmarkViewModel, index))
{
try
{
Bookmarks->RemoveAt(index);
bookmarkModel.Delete(bookmarkViewModel->Id);
}
catch(...)
{
}
}
}
property ICommand^ AddCommand;
void Add(Object^ parameter)
{
editId = -1;
this->EditAuthor = "";
this->EditTitle = "";
this->EditUrl = "";
this->ShowListBox = "Collapsed";
}
property ICommand^ SaveCommand;
void Save(Object^ parameter)
{
if(editId == -1)
{
auto bookmark = bookmarkModel.AddNew(this->EditAuthor->Data(), this->EditTitle->Data(), this->EditUrl->Data());
Bookmarks->Append(ref new BookmarkViewModel(bookmark));
}
else
{
BookmarkViewModel^ bookmarkViewModel = (BookmarkViewModel^)Bookmarks->GetAt(editIndex);
bookmarkViewModel->Author = this->EditAuthor;
bookmarkViewModel->Title = this->EditTitle;
bookmarkViewModel->Url = this->EditUrl;
bookmarkModel.Update(editId, bookmarkViewModel->Author->Data(), bookmarkViewModel->Title->Data(), bookmarkViewModel->Url->Data());
}
editId = -1;
this->ShowListBox = "Visible";
}
property Vector<Object^>^ Bookmarks;
private:
void LoadData()
{
auto items = bookmarkModel.GetBookmarks();
for(auto it = items.begin(); it != items.end(); it++)
{
Bookmarks->Append(ref new BookmarkViewModel(*it));
}
}
};