public TaskItemViewModel (TaskItem taskItem)
		{
			task = taskItem;

			this.SaveCommand = new Command (async (temp) => {
//				App.Database.SaveTask(task);
				bool success = await App.Client.SaveTask(task);
				if (success) Navigation.PopAsync();

				// Broadcast this message for other viewmodel
				MessagingCenter.Send<TaskItemViewModel, TaskItem>(this, "TaskAdded", task);
			});

			this.DeleteCommand = new Command (async (temp) => {
//				App.Database.DeleteTask(task.ID);
				bool success = await App.Client.DeleteTask(task.ID);
				if (success) Navigation.PopAsync();

				MessagingCenter.Send<TaskItemViewModel, TaskItem>(this, "TaskDeleted", task);
			});
				
			this.ResetCommand = new Command (() => {
				this.Task = "";
				this.Description = "";
				this.DueDate = DateTime.Today;
				this.Done = false;
			});
		}
Esempio n. 2
0
		/// <summary>
		/// Creates a new task on the Todo web server.
		/// </summary>
		/// <returns>Success or not.</returns>
		/// <param name="task">TaskItem instance.</param>
		public async Task<bool> NewTask(TaskItem task) {
			var json = JsonConvert.SerializeObject (task, Formatting.Indented, new JsonSerializerSettings {
				ContractResolver = new LowercaseContractResolver()	// turn property names into lowercase
			});
			var response = await _restClient.Post<TodoResponse> ("tasks/", json);

			dispDialog (response);
			return response.Ok;
		}
		public int SaveTask (TaskItem task) {
			lock (locker) {
				if (task.ID != 0) {
					database.Update (task);
					return task.ID;
				} else {
					return database.Insert (task);
				}
			}
		}
Esempio n. 4
0
		/// <summary>
		/// Creates or updates a task.
		/// </summary>
		/// <param name="task">Task.</param>
		public async Task<bool> SaveTask(TaskItem task) {
			bool b;
			if (task.ID == 0) {
				b = await NewTask (task);
			}
			else {
				b = await UpdateTask (task);
			}
			return b;
		}
		public TaskCellViewModel (TaskItem taskItem)
		{
			task = taskItem;

			this.SetDoneCommand = new Command (() => {
				Done = !Done;
			});

			this.TaskEditCommand = new Command (() => {
				MessagingCenter.Send<TaskCellViewModel, TaskItem>(this, "TaskTapped", task);
			});
		}
Esempio n. 6
0
		/// <summary>
		/// Fetches the task with corresponding ID.
		/// </summary>
		/// <returns>The task.</returns>
		public async Task<TaskItem> FetchTask(int id) {
			string parameters = string.Format ("tasks/{0}", id);
			var response = await _restClient.Get<TodoResponse> (parameters);

			TaskItem task = new TaskItem();
			if (response.Ok)
				task = JsonConvert.DeserializeObject<TaskItem> (response.Body);
			else {
				dispDialog (response);
			}
			return task;
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="CloneDo.Mvvm.ViewModels.TodoListViewModel"/> class.
		/// </summary>
		public TodoListViewModel ()
		{
			
			// Initialize
			LoadTasks ();

			// Commands
			this.NewTaskCommand = new Command (() => {
				TaskItem newTask = new TaskItem();
				TaskItemViewModel vm = new TaskItemViewModel(newTask);
				Navigation.PushAsync(ViewFactory.CreatePage(vm));
			});

			this.ReloadCommand = new Command (() => {
				LoadTasks();
			});

			// Messages
			MessagingCenter.Subscribe<TaskItemViewModel, TaskItem> (this, "TaskAdded", (sender, task) => {
				LoadTasks();
			});

			MessagingCenter.Subscribe<TaskItemViewModel, TaskItem> (this, "TaskDeleted", (sender, task) => {
				LoadTasks();
			});

			MessagingCenter.Subscribe<TaskCellViewModel, TaskItem> (this, "TaskSetDone", (sender, task) => {
				System.Diagnostics.Debug.WriteLine("TodoListViewModel::Task set done.");
				LoadTasks();
			});

			MessagingCenter.Subscribe<TaskCellViewModel, TaskItem> (this, "TaskTapped", (sender, task) => {
				TaskTapped(task.ID);
			});

			MessagingCenter.Subscribe<TodoClient, TodoResponse> (this, "StatusMessage", (sender, response) => {
				System.Diagnostics.Debug.WriteLine("TodoListViewModel::Status dialog here");
				Navigation.DisplayAlert(response.Ok?"Success":"Error", response.Message, "Ok");
				Navigation.PopToRootAsync();
				
			});

			MessagingCenter.Subscribe<TodoClient, string> (this, "ErrMessage", (sender, message) => {

				Navigation.DisplayAlert("Error!", message, "Ok");
//				Navigation.PopAsync(); 
			});
		}
Esempio n. 8
0
		/// <summary>
		/// Updates the task on the web server.
		/// </summary>
		/// <returns>The task.</returns>
		/// <param name="task">Task.</param>
		public async Task<bool> UpdateTask(TaskItem task) {
			var json = JsonConvert.SerializeObject (task, Formatting.Indented, new JsonSerializerSettings {
				ContractResolver = new LowercaseContractResolver()	// turn property names into lowercase
			});
			string parameters = string.Format ("tasks/{0}", task.ID);
			var response = await _restClient.Put<TodoResponse> (parameters, json);

			dispDialog (response);

			return response.Ok;
		}