Skip to content

Commit eb658d3

Browse files
committed
Changing datamodel to look similar to PowerSync App schema.
1 parent 2c78052 commit eb658d3

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

demos/TodoSQLite/Data/TodoItemDatabase.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,30 @@ public async Task<int> SaveListAsync(TodoList list)
4646
public async Task<int> DeleteListAsync(TodoList list)
4747
{
4848
await Init();
49+
string listId = list.ID.ToString();
4950
// First delete all todo items in this list
50-
await database.Table<TodoItem>().Where(t => t.ListId == list.ID).DeleteAsync();
51+
await database.Table<TodoItem>().Where(t => t.ListId == listId).DeleteAsync();
5152
return await database.DeleteAsync(list);
5253
}
5354

5455
// Todo item operations
5556
public async Task<List<TodoItem>> GetItemsAsync(int listId)
5657
{
5758
await Init();
59+
string listIdStr = listId.ToString();
5860
return await database.Table<TodoItem>()
59-
.Where(t => t.ListId == listId)
60-
.OrderByDescending(t => t.ID)
61+
.Where(t => t.ListId == listIdStr)
62+
.OrderByDescending(t => t.CreatedAt)
6163
.ToListAsync();
6264
}
6365

6466
public async Task<List<TodoItem>> GetItemsNotDoneAsync(int listId)
6567
{
6668
await Init();
69+
string listIdStr = listId.ToString();
6770
return await database.Table<TodoItem>()
68-
.Where(t => t.ListId == listId && !t.Done)
69-
.OrderByDescending(t => t.ID)
71+
.Where(t => t.ListId == listIdStr && !t.Completed)
72+
.OrderByDescending(t => t.CreatedAt)
7073
.ToListAsync();
7174
}
7275

demos/TodoSQLite/Models/TodoItem.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ public class TodoItem
66
{
77
[PrimaryKey, AutoIncrement]
88
public int ID { get; set; }
9-
public string Name { get; set; }
10-
public bool Done { get; set; }
11-
public int ListId { get; set; }
9+
public string ListId { get; set; }
10+
public string CreatedAt { get; set; } = DateTime.UtcNow.ToString("o");
11+
public string CompletedAt { get; set; }
12+
public string Description { get; set; }
13+
public string CreatedBy { get; set; }
14+
public string CompletedBy { get; set; }
15+
public bool Completed { get; set; }
1216
}

demos/TodoSQLite/Models/TodoList.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class TodoList
66
{
77
[PrimaryKey, AutoIncrement]
88
public int ID { get; set; }
9+
public string CreatedAt { get; set; } = DateTime.UtcNow.ToString("o");
910
public string Name { get; set; }
10-
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
11+
public string OwnerId { get; set; }
1112
}

demos/TodoSQLite/Views/TodoListPage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
Padding="10"
2121
CornerRadius="5">
2222
<Grid ColumnDefinitions="*,Auto">
23-
<Label Text="{Binding Name}"
23+
<Label Text="{Binding Description}"
2424
FontSize="16"
2525
VerticalOptions="Center"/>
2626
<HorizontalStackLayout Grid.Column="1"
2727
Spacing="10"
2828
VerticalOptions="Center">
29-
<CheckBox IsChecked="{Binding Done}"
29+
<CheckBox IsChecked="{Binding Completed}"
3030
CheckedChanged="OnCheckBoxChanged"
3131
VerticalOptions="Center"/>
3232
<Button Text="Delete"

demos/TodoSQLite/Views/TodoListPage.xaml.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ protected override async void OnAppearing()
2727

2828
private async void OnAddClicked(object sender, EventArgs e)
2929
{
30-
string name = await DisplayPromptAsync("New Todo", "Enter todo name:");
31-
if (!string.IsNullOrWhiteSpace(name))
30+
string description = await DisplayPromptAsync("New Todo", "Enter todo description:");
31+
if (!string.IsNullOrWhiteSpace(description))
3232
{
3333
var todo = new TodoItem
3434
{
35-
Name = name,
36-
ListId = _list.ID
35+
Description = description,
36+
ListId = _list.ID.ToString(),
37+
CreatedBy = "user" // TODO: Replace with actual user ID
3738
};
3839
await _database.SaveItemAsync(todo);
3940
TodoItemsCollection.ItemsSource = await _database.GetItemsAsync(_list.ID);
@@ -46,7 +47,7 @@ private async void OnDeleteClicked(object sender, EventArgs e)
4647
var todo = (TodoItem)button.CommandParameter;
4748

4849
bool confirm = await DisplayAlert("Confirm Delete",
49-
$"Are you sure you want to delete '{todo.Name}'?",
50+
$"Are you sure you want to delete '{todo.Description}'?",
5051
"Yes", "No");
5152

5253
if (confirm)
@@ -61,7 +62,9 @@ private async void OnCheckBoxChanged(object sender, CheckedChangedEventArgs e)
6162
if (sender is CheckBox checkBox &&
6263
checkBox.Parent?.Parent?.BindingContext is TodoItem todo)
6364
{
64-
todo.Done = e.Value;
65+
todo.Completed = e.Value;
66+
todo.CompletedAt = e.Value ? DateTime.UtcNow.ToString("o") : null;
67+
todo.CompletedBy = e.Value ? "user" : null; // TODO: Replace with actual user ID
6568
await _database.SaveItemAsync(todo);
6669
}
6770
}
@@ -70,13 +73,13 @@ private async void OnItemSelected(object sender, SelectionChangedEventArgs e)
7073
{
7174
if (e.CurrentSelection.FirstOrDefault() is TodoItem selectedItem)
7275
{
73-
string newName = await DisplayPromptAsync("Edit Todo",
74-
"Enter new name:",
75-
initialValue: selectedItem.Name);
76+
string newDescription = await DisplayPromptAsync("Edit Todo",
77+
"Enter new description:",
78+
initialValue: selectedItem.Description);
7679

77-
if (!string.IsNullOrWhiteSpace(newName))
80+
if (!string.IsNullOrWhiteSpace(newDescription))
7881
{
79-
selectedItem.Name = newName;
82+
selectedItem.Description = newDescription;
8083
await _database.SaveItemAsync(selectedItem);
8184
TodoItemsCollection.ItemsSource = await _database.GetItemsAsync(_list.ID);
8285
}

0 commit comments

Comments
 (0)