Skip to content

Some additional code cleanup #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mpnow
Copy link
Contributor

@mpnow mpnow commented Jul 8, 2025

Additional code cleanup

NodeConnector.cs

  • Cleaned up usings.

PowerSyncData.cs

  • SaveItemAsync()
    • Null out completed_by if item is marked as incomplete.
    • Write completed_by on INSERT for consistency's sake.
    • Fields rearrranged on INSERT for more logical ordering.

TodoItem.cs

  • Formatting cleanup.

ListsPage.xaml.cs and TodoListPage.xaml.cs

  • Refactored OnDeleteClicked() to avoid possible null reference.
  • Changed variable declarations to all use implicit typing for consistency's sake.

@mpnow
Copy link
Contributor Author

mpnow commented Jul 8, 2025

Thanks for merging my last PR!

This one doesn't have any bug fixes in it per se, so it's of course up to you whether you want to merge it in or not (or cherry pick some of the changes that you want in).

@Chriztiaan
Copy link
Collaborator

Chriztiaan commented Jul 8, 2025

Thanks for merging my last PR!

This one doesn't have any bug fixes in it per se, so it's of course up to you whether you want to merge it in or not (or cherry pick some of the changes that you want in).

Cleanup and fixes are appreciated and encouraged! Thanks for spending the time to help!
I'll give it a test.

{
todo.Completed = e.Value;
todo.CompletedAt = DateTime.UtcNow.ToString("o");
todo.CompletedAt = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
Copy link
Collaborator

@Chriztiaan Chriztiaan Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely the only issue I have. Since we use the ISO format across our demos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, maybe - but then it seems like you'd have to do something like this:

    public async Task SaveItemAsync(TodoItem item)
    {
        if (item.ID != "")
        {
            await Db.Execute(
                @"UPDATE todos 
                  SET description = ?, 
                  completed = ?, 
                  completed_at = CASE 
                    WHEN ? = 1 AND ? IS NULL THEN datetime() 
                    ELSE NULL 
                  END,
                  completed_by = ?
                  WHERE id = ?",
                [
                    item.Description,
                    item.Completed ? 1 : 0,
                    item.Completed ? 1 : 0,
                    item.CompletedAt,
                    item.Completed ? UserId : null,
                    item.ID
                ]);
        }
        else
        {
            await Db.Execute(
                @"INSERT INTO todos 
                  (id, list_id, description, created_at, created_by, completed, completed_at, completed_by)
                  VALUES (uuid(), ?, ?, datetime(), ?, ?, 
                  CASE 
                    WHEN ? = 1 AND ? IS NULL THEN datetime() 
                    ELSE NULL 
                  END, ?)",
                [
                    item.ListId,
                    item.Description,
                    UserId,
                    item.Completed ? 1 : 0,
                    item.Completed ? 1 : 0,
                    item.CompletedAt,
                    item.Completed ? UserId : null
                ]);
        }
    }

because you only want to set CompletedAt if the item is newly marked as Completed; and to my mind that's too much business logic in the database class.

FYI I changed that date formatting in the code-behind to make it consistent with the format that datetime() is writing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to add a dedicated method that has sql intended only for marking as complete I guess (which would also use datetime()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants