Skip to content

Commit cfbfe07

Browse files
committed
Added setup for MAUI app, before PS is working on it.
1 parent c62c6ab commit cfbfe07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1371
-48
lines changed

demos/TodoSQLite/App.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:TodoSQLite"
5+
x:Class="TodoSQLite.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>

demos/TodoSQLite/App.xaml.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace TodoSQLite;
2+
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}

demos/TodoSQLite/AppShell.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="TodoSQLite.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:views="clr-namespace:TodoSQLite.Views"
7+
Shell.FlyoutBehavior="Disabled">
8+
9+
<ShellContent
10+
Title="Todo"
11+
ContentTemplate="{DataTemplate views:TodoListPage}"
12+
Route="TodoList" />
13+
14+
</Shell>

demos/TodoSQLite/AppShell.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using TodoSQLite.Views;
2+
3+
namespace TodoSQLite;
4+
5+
public partial class AppShell : Shell
6+
{
7+
public AppShell()
8+
{
9+
InitializeComponent();
10+
Routing.RegisterRoute(nameof(TodoItemPage), typeof(TodoItemPage));
11+
}
12+
}

demos/TodoSQLite/Constants.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace TodoSQLite;
2+
3+
public static class Constants
4+
{
5+
public const string DatabaseFilename = "TodoSQLite.db3";
6+
7+
public const SQLite.SQLiteOpenFlags Flags =
8+
// open the database in read/write mode
9+
SQLite.SQLiteOpenFlags.ReadWrite |
10+
// create the database if it doesn't exist
11+
SQLite.SQLiteOpenFlags.Create |
12+
// enable multi-threaded database access
13+
SQLite.SQLiteOpenFlags.SharedCache;
14+
15+
public static string DatabasePath =>
16+
Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
17+
}

demos/TodoSQLite/Data/AppSchema.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using TodoSQLite.Models;
2+
3+
using PowerSync.Common.DB.Schema;
4+
5+
class AppSchema
6+
{
7+
public static Table Todos = new Table(new Dictionary<string, ColumnType>
8+
{
9+
{ "list_id", ColumnType.TEXT },
10+
{ "created_at", ColumnType.TEXT },
11+
{ "completed_at", ColumnType.TEXT },
12+
{ "description", ColumnType.TEXT },
13+
{ "created_by", ColumnType.TEXT },
14+
{ "completed_by", ColumnType.TEXT },
15+
{ "completed", ColumnType.INTEGER }
16+
}, new TableOptions
17+
{
18+
Indexes = new Dictionary<string, List<string>> { { "list", new List<string> { "list_id" } } }
19+
});
20+
21+
public static Table Lists = new Table(new Dictionary<string, ColumnType>
22+
{
23+
{ "created_at", ColumnType.TEXT },
24+
{ "name", ColumnType.TEXT },
25+
{ "owner_id", ColumnType.TEXT }
26+
});
27+
28+
public static Schema PowerSyncSchema = new Schema(new Dictionary<string, Table>
29+
{
30+
{ "todos", Todos },
31+
{ "lists", Lists }
32+
});
33+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Microsoft.Extensions.Logging;
2+
using Newtonsoft.Json;
3+
using PowerSync.Common.Client;
4+
using SQLite;
5+
using TodoSQLite.Models;
6+
7+
namespace TodoSQLite.Data;
8+
9+
public class TodoItemDatabase
10+
{
11+
SQLiteAsyncConnection Database;
12+
public TodoItemDatabase()
13+
{
14+
}
15+
async Task Init()
16+
{
17+
18+
var db = new PowerSyncDatabase(new PowerSyncDatabaseOptions
19+
{
20+
Database = new SQLOpenOptions { DbFilename = "cli-example.db" },
21+
Schema = AppSchema.PowerSyncSchema,
22+
});
23+
await db.Init();
24+
var x = await db.GetAll<object>("select * from lists;");
25+
await Application.Current.MainPage.DisplayAlert("Title", JsonConvert.SerializeObject(x), "OK");
26+
27+
if (Database is not null)
28+
return;
29+
30+
31+
Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
32+
var result = await Database.CreateTableAsync<TodoItem>();
33+
34+
35+
36+
}
37+
38+
public async Task<List<TodoItem>> GetItemsAsync()
39+
{
40+
await Init();
41+
return await Database.Table<TodoItem>().ToListAsync();
42+
}
43+
44+
public async Task<List<TodoItem>> GetItemsNotDoneAsync()
45+
{
46+
await Init();
47+
return await Database.Table<TodoItem>().Where(t => t.Done).ToListAsync();
48+
49+
// SQL queries are also possible
50+
//return await Database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
51+
}
52+
53+
public async Task<TodoItem> GetItemAsync(int id)
54+
{
55+
await Init();
56+
return await Database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
57+
}
58+
59+
public async Task<int> SaveItemAsync(TodoItem item)
60+
{
61+
await Init();
62+
if (item.ID != 0)
63+
{
64+
return await Database.UpdateAsync(item);
65+
}
66+
else
67+
{
68+
return await Database.InsertAsync(item);
69+
}
70+
}
71+
72+
public async Task<int> DeleteItemAsync(TodoItem item)
73+
{
74+
await Init();
75+
return await Database.DeleteAsync(item);
76+
}
77+
}

demos/TodoSQLite/MauiProgram.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.Extensions.DependencyInjection.Extensions;
2+
using Microsoft.Extensions.Logging;
3+
using TodoSQLite.Data;
4+
using TodoSQLite.Views;
5+
6+
namespace TodoSQLite;
7+
8+
public static class MauiProgram
9+
{
10+
public static MauiApp CreateMauiApp()
11+
{
12+
var builder = MauiApp.CreateBuilder();
13+
builder
14+
.UseMauiApp<App>()
15+
.ConfigureFonts(fonts =>
16+
{
17+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
18+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
19+
});
20+
builder.Logging.SetMinimumLevel(LogLevel.Debug);
21+
builder.Logging.AddDebug();
22+
builder.Services.AddSingleton<TodoListPage>();
23+
builder.Services.AddTransient<TodoItemPage>();
24+
25+
builder.Services.AddSingleton<TodoItemDatabase>();
26+
27+
return builder.Build();
28+
}
29+
}

demos/TodoSQLite/Models/TodoItem.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using SQLite;
2+
3+
namespace TodoSQLite.Models;
4+
5+
public class TodoItem
6+
{
7+
[PrimaryKey, AutoIncrement]
8+
public int ID { get; set; }
9+
public string Name { get; set; }
10+
public string Notes { get; set; }
11+
public bool Done { get; set; }
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
</manifest>

0 commit comments

Comments
 (0)