Skip to content

Commit e0c0dba

Browse files
committed
Added dedicated SaveTodoCompleted method.
1 parent 5f55a43 commit e0c0dba

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

demos/MAUITodo/Data/PowerSyncData.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public PowerSyncData()
3535

3636
var nodeConnector = new NodeConnector();
3737
UserId = nodeConnector.UserId;
38-
38+
3939
Db.Connect(nodeConnector);
4040
}
41-
41+
4242
public async Task SaveListAsync(TodoList list)
4343
{
4444
if (list.ID != "")
@@ -62,7 +62,7 @@ public async Task DeleteListAsync(TodoList list)
6262
await Db.Execute("DELETE FROM todos WHERE list_id = ?", [listId]);
6363
await Db.Execute("DELETE FROM lists WHERE id = ?", [listId]);
6464
}
65-
65+
6666
public async Task SaveItemAsync(TodoItem item)
6767
{
6868
if (item.ID != "")
@@ -96,6 +96,31 @@ await Db.Execute(
9696
}
9797
}
9898

99+
public async Task SaveTodoCompletedAsync(string todoId, bool completed)
100+
{
101+
if (completed)
102+
{
103+
await Db.Execute(
104+
@"UPDATE todos
105+
SET completed = 1, completed_at = datetime(), completed_by = ?
106+
WHERE id = ?",
107+
[
108+
UserId,
109+
todoId
110+
]);
111+
}
112+
else
113+
{
114+
await Db.Execute(
115+
@"UPDATE todos
116+
SET completed = 0, completed_at = NULL, completed_by = NULL
117+
WHERE id = ?",
118+
[
119+
todoId
120+
]);
121+
}
122+
}
123+
99124
public async Task DeleteItemAsync(TodoItem item)
100125
{
101126
await Db.Execute("DELETE FROM todos WHERE id = ?", [item.ID]);

demos/MAUITodo/Views/TodoListPage.xaml.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public TodoListPage(PowerSyncData powerSyncData, TodoList list)
1818
}
1919

2020
public string ListName => selectedList?.Name ?? "";
21-
21+
2222
protected override async void OnAppearing()
2323
{
2424
base.OnAppearing();
25-
25+
2626
await database.Db.Watch("select * from todos where list_id = ?", [selectedList.ID], new WatchHandler<TodoItem>
2727
{
2828
OnResult = (results) =>
@@ -72,14 +72,12 @@ private async void OnCheckBoxChanged(object sender, CheckedChangedEventArgs e)
7272
if (e.Value && todo.CompletedAt == null)
7373
{
7474
todo.Completed = e.Value;
75-
todo.CompletedAt = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
76-
await database.SaveItemAsync(todo);
75+
await database.SaveTodoCompletedAsync(todo.ID, true);
7776
}
7877
else if (e.Value == false && todo.CompletedAt != null)
7978
{
8079
todo.Completed = e.Value;
81-
todo.CompletedAt = null; // Uncheck, clear completed time
82-
await database.SaveItemAsync(todo);
80+
await database.SaveTodoCompletedAsync(todo.ID, false);
8381
}
8482
}
8583
}

0 commit comments

Comments
 (0)