This repository has been archived by the owner on Feb 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasksTableSource.cs
107 lines (90 loc) · 3.06 KB
/
TasksTableSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using Foundation;
using UIKit;
using DittoSDK;
using System.Collections;
namespace Tasks
{
public class TasksTableSource : UITableViewSource
{
DittoTask[] tasks;
NSString cellIdentifier = new NSString("taskCell");
private Ditto ditto
{
get
{
var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
return appDelegate.ditto;
}
}
public TasksTableSource(DittoTask[] taskList)
{
this.tasks = taskList;
}
public TasksTableSource()
{
}
public void updateTasks(List<DittoTask> tasks)
{
this.tasks = tasks.ToArray();
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, cellIdentifier);
}
var task = tasks[indexPath.Row];
cell.TextLabel.Text = task.Body;
var taskComplete = task.IsCompleted;
if (taskComplete)
{
cell.Accessory = UITableViewCellAccessory.Checkmark;
}
else
{
cell.Accessory = UITableViewCellAccessory.None;
}
var tapGesture = new UITapGestureRecognizer();
tapGesture.AddTarget(() =>
{
var updateQuery = $"UPDATE {DittoTask.CollectionName} " +
$"SET isCompleted = {!task.IsCompleted} " +
$"WHERE _id = '{task.Id}' AND isCompleted != {!task.IsCompleted}";
ditto.Store.ExecuteAsync(updateQuery);
});
cell.AddGestureRecognizer(tapGesture);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
if (this.tasks == null)
{
return 0;
}
return tasks.Length;
}
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
return true;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath)
{
switch (editingStyle)
{
case UITableViewCellEditingStyle.Delete:
var task = tasks[indexPath.Row];
var updateQuery = $"UPDATE {DittoTask.CollectionName} " +
"SET isDeleted = true " +
$"WHERE _id = '{task.Id}'";
ditto.Store.ExecuteAsync(updateQuery);
break;
case UITableViewCellEditingStyle.None:
Console.WriteLine("CommitEditingStyle:None called");
break;
}
}
}
}