Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions 9.0/Data/TodoSQLite/TodoSQLite/Data/TodoItemDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,54 @@ namespace TodoSQLite.Data;

public class TodoItemDatabase
{
SQLiteAsyncConnection Database;
public TodoItemDatabase()
{
}
SQLiteAsyncConnection database;

async Task Init()
{
if (Database is not null)
if (database is not null)
return;

Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
var result = await Database.CreateTableAsync<TodoItem>();
database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
var result = await database.CreateTableAsync<TodoItem>();
}

public async Task<List<TodoItem>> GetItemsAsync()
{
await Init();
return await Database.Table<TodoItem>().ToListAsync();
return await database.Table<TodoItem>().ToListAsync();
}

public async Task<List<TodoItem>> GetItemsNotDoneAsync()
{
await Init();
return await Database.Table<TodoItem>().Where(t => t.Done).ToListAsync();
return await database.Table<TodoItem>().Where(t => t.Done).ToListAsync();

// SQL queries are also possible
//return await Database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
//return await database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
}

public async Task<TodoItem> GetItemAsync(int id)
{
await Init();
return await Database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
return await database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
}

public async Task<int> SaveItemAsync(TodoItem item)
{
await Init();
if (item.ID != 0)
{
return await Database.UpdateAsync(item);
return await database.UpdateAsync(item);
}
else
{
return await Database.InsertAsync(item);
return await database.InsertAsync(item);
}
}

public async Task<int> DeleteItemAsync(TodoItem item)
{
await Init();
return await Database.DeleteAsync(item);
return await database.DeleteAsync(item);
}
}
4 changes: 1 addition & 3 deletions 9.0/Data/TodoSQLite/TodoSQLite/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using TodoSQLite.Data;
using TodoSQLite.Data;
using TodoSQLite.Views;

namespace TodoSQLite;
Expand All @@ -19,7 +18,6 @@ public static MauiApp CreateMauiApp()

builder.Services.AddSingleton<TodoListPage>();
builder.Services.AddTransient<TodoItemPage>();

builder.Services.AddSingleton<TodoItemDatabase>();

return builder.Build();
Expand Down
4 changes: 2 additions & 2 deletions 9.0/Data/TodoSQLite/TodoSQLite/TodoSQLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
Expand All @@ -62,8 +63,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.2" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
</ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions 9.0/Data/TodoSQLite/TodoSQLite/Views/TodoItemPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ namespace TodoSQLite.Views;
[QueryProperty("Item", "Item")]
public partial class TodoItemPage : ContentPage
{
TodoItem item;
public TodoItem Item
TodoItemDatabase database;

public TodoItem Item
{
get => BindingContext as TodoItem;
set => BindingContext = value;
}
TodoItemDatabase database;

public TodoItemPage(TodoItemDatabase todoItemDatabase)
{
InitializeComponent();
Expand Down
3 changes: 2 additions & 1 deletion 9.0/Data/TodoSQLite/TodoSQLite/Views/TodoListPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ namespace TodoSQLite.Views;
public partial class TodoListPage : ContentPage
{
TodoItemDatabase database;

public ObservableCollection<TodoItem> Items { get; set; } = new();

public TodoListPage(TodoItemDatabase todoItemDatabase)
{
InitializeComponent();
database = todoItemDatabase;
BindingContext = this;
}


protected override async void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
Expand Down
Loading