Skip to content

Commit 2728eab

Browse files
authored
Merge pull request #13 from mpnow/feature/additional-code-cleanup
Some additional code cleanup
2 parents 2505af4 + e0c0dba commit 2728eab

File tree

5 files changed

+77
-57
lines changed

5 files changed

+77
-57
lines changed

demos/MAUITodo/Data/NodeConnector.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
using PowerSync.Common.Client;
1+
using System.Text;
2+
using System.Text.Json;
3+
using PowerSync.Common.Client;
24
using PowerSync.Common.Client.Connection;
35
using PowerSync.Common.DB.Crud;
46

57
namespace MAUITodo.Data;
68

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Net.Http;
10-
using System.Text;
11-
using System.Text.Json;
12-
using System.Threading.Tasks;
13-
149
public class NodeConnector : IPowerSyncBackendConnector
1510
{
1611
private string StorageFilePath => Path.Combine(FileSystem.AppDataDirectory, "user_id.txt");

demos/MAUITodo/Data/PowerSyncData.cs

Lines changed: 33 additions & 6 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,6 +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+
6566
public async Task SaveItemAsync(TodoItem item)
6667
{
6768
if (item.ID != "")
@@ -74,22 +75,48 @@ await Db.Execute(
7475
item.Description,
7576
item.Completed ? 1 : 0,
7677
item.CompletedAt!,
77-
UserId,
78+
item.Completed ? UserId : null,
7879
item.ID
7980
]);
8081
}
8182
else
8283
{
8384
await Db.Execute(
8485
@"INSERT INTO todos
85-
(id, list_id, description, created_at, completed, created_by, completed_at)
86-
VALUES (uuid(), ?, ?, datetime(), ?, ?, ?)",
86+
(id, list_id, description, created_at, created_by, completed, completed_at, completed_by)
87+
VALUES (uuid(), ?, ?, datetime(), ?, ?, ?, ?)",
8788
[
8889
item.ListId,
8990
item.Description,
90-
item.Completed ? 1 : 0,
9191
UserId,
92+
item.Completed ? 1 : 0,
9293
item.CompletedAt!,
94+
item.Completed ? UserId : null
95+
]);
96+
}
97+
}
98+
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
93120
]);
94121
}
95122
}

demos/MAUITodo/Models/TodoItem.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ public class TodoItem
1111
public string ListId { get; set; } = null!;
1212

1313
[JsonProperty("created_at")]
14-
public string CreatedAt { get; set; }= null!;
14+
public string CreatedAt { get; set; } = null!;
1515

1616
[JsonProperty("completed_at")]
1717
public string? CompletedAt { get; set; }
1818

1919
[JsonProperty("description")]
20-
public string Description { get; set; }= null!;
20+
public string Description { get; set; } = null!;
2121

2222
[JsonProperty("created_by")]
23-
public string CreatedBy { get; set; }= null!;
23+
public string CreatedBy { get; set; } = null!;
2424

2525
[JsonProperty("completed_by")]
26-
public string CompletedBy { get; set; }= null!;
26+
public string CompletedBy { get; set; } = null!;
2727

2828
[JsonProperty("completed")]
29-
public bool Completed { get; set; } = false;
29+
public bool Completed { get; set; }
3030
}

demos/MAUITodo/Views/ListsPage.xaml.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MAUITodo.Views;
66

7-
public partial class ListsPage : ContentPage
7+
public partial class ListsPage
88
{
99
private readonly PowerSyncData database;
1010

@@ -46,7 +46,7 @@ protected override async void OnAppearing()
4646

4747
private async void OnAddClicked(object sender, EventArgs e)
4848
{
49-
string name = await DisplayPromptAsync("New List", "Enter list name:");
49+
var name = await DisplayPromptAsync("New List", "Enter list name:");
5050
if (!string.IsNullOrWhiteSpace(name))
5151
{
5252
var list = new TodoList { Name = name };
@@ -56,16 +56,16 @@ private async void OnAddClicked(object sender, EventArgs e)
5656

5757
private async void OnDeleteClicked(object sender, EventArgs e)
5858
{
59-
var button = (Button)sender;
60-
var list = (TodoList)button.CommandParameter;
61-
62-
bool confirm = await DisplayAlert("Confirm Delete",
63-
$"Are you sure you want to delete the list '{list.Name}'?",
64-
"Yes", "No");
65-
66-
if (confirm)
59+
if (sender is Button button && button.CommandParameter is TodoList list)
6760
{
68-
await database.DeleteListAsync(list);
61+
var confirm = await DisplayAlert("Confirm Delete",
62+
$"Are you sure you want to delete the list '{list.Name}'?",
63+
"Yes", "No");
64+
65+
if (confirm)
66+
{
67+
await database.DeleteListAsync(list);
68+
}
6969
}
7070
}
7171

demos/MAUITodo/Views/TodoListPage.xaml.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MAUITodo.Views;
66

7-
public partial class TodoListPage : ContentPage
7+
public partial class TodoListPage
88
{
99
private readonly PowerSyncData database;
1010
private readonly TodoList selectedList;
@@ -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) =>
@@ -38,11 +38,11 @@ protected override async void OnAppearing()
3838

3939
private async void OnAddClicked(object sender, EventArgs e)
4040
{
41-
string description = await DisplayPromptAsync("New Todo", "Enter todo description:");
41+
var description = await DisplayPromptAsync("New Todo", "Enter todo description:");
4242
if (!string.IsNullOrWhiteSpace(description))
4343
{
44-
var todo = new TodoItem
45-
{
44+
var todo = new TodoItem
45+
{
4646
Description = description,
4747
ListId = selectedList.ID
4848
};
@@ -52,34 +52,32 @@ private async void OnAddClicked(object sender, EventArgs e)
5252

5353
private async void OnDeleteClicked(object sender, EventArgs e)
5454
{
55-
var button = (Button)sender;
56-
var todo = (TodoItem)button.CommandParameter;
57-
58-
bool confirm = await DisplayAlert("Confirm Delete",
59-
$"Are you sure you want to delete '{todo.Description}'?",
60-
"Yes", "No");
61-
62-
if (confirm)
55+
if (sender is Button button && button.CommandParameter is TodoItem todo)
6356
{
64-
await database.DeleteItemAsync(todo);
57+
var confirm = await DisplayAlert("Confirm Delete",
58+
$"Are you sure you want to delete '{todo.Description}'?",
59+
"Yes", "No");
60+
61+
if (confirm)
62+
{
63+
await database.DeleteItemAsync(todo);
64+
}
6565
}
6666
}
6767

6868
private async void OnCheckBoxChanged(object sender, CheckedChangedEventArgs e)
6969
{
70-
if (sender is CheckBox checkBox &&
71-
checkBox.Parent?.Parent?.BindingContext is TodoItem todo)
70+
if (sender is CheckBox checkBox && checkBox.Parent?.Parent?.BindingContext is TodoItem todo)
7271
{
73-
if (e.Value == true && todo.CompletedAt == null)
72+
if (e.Value && todo.CompletedAt == null)
7473
{
7574
todo.Completed = e.Value;
76-
todo.CompletedAt = DateTime.UtcNow.ToString("o");
77-
await database.SaveItemAsync(todo);
78-
} else if (e.Value == false && todo.CompletedAt != null)
75+
await database.SaveTodoCompletedAsync(todo.ID, true);
76+
}
77+
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
}
@@ -88,16 +86,16 @@ private async void OnItemSelected(object sender, SelectionChangedEventArgs e)
8886
{
8987
if (e.CurrentSelection.FirstOrDefault() is TodoItem selectedItem)
9088
{
91-
string newDescription = await DisplayPromptAsync("Edit Todo",
92-
"Enter new description:",
89+
var newDescription = await DisplayPromptAsync("Edit Todo",
90+
"Enter new description:",
9391
initialValue: selectedItem.Description);
94-
92+
9593
if (!string.IsNullOrWhiteSpace(newDescription))
9694
{
9795
selectedItem.Description = newDescription;
9896
await database.SaveItemAsync(selectedItem);
9997
}
100-
98+
10199
TodoItemsCollection.SelectedItem = null;
102100
}
103101
}

0 commit comments

Comments
 (0)