Skip to content

Commit 5f55a43

Browse files
committed
Some additional code cleanup
1 parent 2505af4 commit 5f55a43

File tree

5 files changed

+47
-50
lines changed

5 files changed

+47
-50
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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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,23 @@ 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
9395
]);
9496
}
9597
}

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: 22 additions & 22 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;
@@ -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,30 +52,30 @@ 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");
75+
todo.CompletedAt = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
7776
await database.SaveItemAsync(todo);
78-
} else if (e.Value == false && todo.CompletedAt != null)
77+
}
78+
else if (e.Value == false && todo.CompletedAt != null)
7979
{
8080
todo.Completed = e.Value;
8181
todo.CompletedAt = null; // Uncheck, clear completed time
@@ -88,16 +88,16 @@ private async void OnItemSelected(object sender, SelectionChangedEventArgs e)
8888
{
8989
if (e.CurrentSelection.FirstOrDefault() is TodoItem selectedItem)
9090
{
91-
string newDescription = await DisplayPromptAsync("Edit Todo",
92-
"Enter new description:",
91+
var newDescription = await DisplayPromptAsync("Edit Todo",
92+
"Enter new description:",
9393
initialValue: selectedItem.Description);
94-
94+
9595
if (!string.IsNullOrWhiteSpace(newDescription))
9696
{
9797
selectedItem.Description = newDescription;
9898
await database.SaveItemAsync(selectedItem);
9999
}
100-
100+
101101
TodoItemsCollection.SelectedItem = null;
102102
}
103103
}

0 commit comments

Comments
 (0)