Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem committed Mar 6, 2024
1 parent 1fdb74b commit b40ca6b
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 155 deletions.
112 changes: 112 additions & 0 deletions Masa.Blazor.Pro.Components/Data/ProDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Masa.Blazor.Pro.Components.Models;
using SQLite;

namespace Masa.Blazor.Pro.Components;

public class ProDatabase
{
public const string DatabaseFilename = "proapp.db";

public const SQLiteOpenFlags Flags =
// open the database in read/write mode
SQLiteOpenFlags.ReadWrite |
// create the database if it doesn't exist
SQLiteOpenFlags.Create |
// enable multi-threaded database access
SQLiteOpenFlags.SharedCache;

// public static string DatabasePath =>
// Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);

private SQLiteAsyncConnection? Database { get; set; }

[MemberNotNull(nameof(Database))]
async Task InitAsync()
{
if (Database is not null)
{
return;
}

Database = new SQLiteAsyncConnection(DatabaseFilename, Flags);
await Database.CreateTableAsync<TodoTask>();
await Database.CreateTableAsync<TodoTag>();
}

#region Todo task

public async Task<int> CreateTaskAsync(TodoTask task)
{
await InitAsync();
return await Database.InsertAsync(task);
}

public async Task UpdateTaskAsync(TodoTask task)
{
await InitAsync();
await Database.UpdateAsync(task);
}

public async Task<List<TodoTask>> GetTasksAsync(
int page,
int pageSize,
DateTime dateTime = default,
int tag = 0,
TodoTaskPriority? priority = null)
{
await InitAsync();

var sqlBuilder = new StringBuilder();
sqlBuilder.Append("SELECT * FROM [TodoTask]");

var hasWhere = false;

if (dateTime != default)
{
hasWhere = true;

var tick = dateTime.Ticks;
var nextDay = tick + TimeSpan.TicksPerDay;
sqlBuilder.Append(" WHERE [DueAt] >= ").Append(tick).Append(" AND [DueAt] < ").Append(nextDay);
}

if (tag != 0)
{
sqlBuilder.Append(hasWhere ? " AND" : " WHERE");
sqlBuilder.Append(" [Tags] LIKE '%").Append(tag).Append(";%'");
}

if (priority is not null)
{
sqlBuilder.Append(hasWhere ? " AND" : " WHERE");
sqlBuilder.Append(" [Priority] = ").Append((int)priority);
}

sqlBuilder.Append(" ORDER BY [DueAt] DESC");
sqlBuilder.Append(" LIMIT ").Append(pageSize).Append(" OFFSET ").Append((page - 1) * pageSize);

return await Database.QueryAsync<TodoTask>(sqlBuilder.ToString());
}

#endregion

#region Todo tag

public async Task<int> CreateTagAsync(TodoTag tag)
{
await InitAsync();

return await this.Database.InsertAsync(tag);
}

public async Task<List<TodoTag>> GetTagsAsync()
{
await InitAsync();

return await Database.Table<TodoTag>().ToListAsync();
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.2"/>
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Masa.Blazor.Pro.Components/Models/TodoTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using SQLite;

namespace Masa.Blazor.Pro.Components.Models;

public class TodoTag
{
[PrimaryKey] [AutoIncrement] public int Id { get; set; }

public string? Name { get; set; }

public string? Color { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.ComponentModel.DataAnnotations;
using SQLite;

namespace Masa.Blazor.ProApp.Rcl.Models;
namespace Masa.Blazor.Pro.Components.Models;

public class TodoTask
{
private int[] _tagIds = [];

public TodoTask()
{
DueAt = DateTime.Today;
Expand All @@ -24,7 +26,21 @@ public TodoTask()

public bool Completed { get; set; }

public string? Tags { get; set; }
[Ignore]
public int[] TagIds
{
get => Tags?.Split(';').Where(t => !string.IsNullOrEmpty(t)).Select(int.Parse).ToArray() ?? [];
set
{
Tags = string.Join(';', value);
if (Tags.Length > 0)
{
Tags += ";";
}
}
}

public string? Tags { get; private set; }
}

public enum TodoTaskPriority
Expand Down
31 changes: 31 additions & 0 deletions Masa.Blazor.Pro.Components/RenderFragments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using BlazorComponent;
using Masa.Blazor.Pro.Components.Models;
using Microsoft.AspNetCore.Components;

namespace Masa.Blazor.Pro.Components;

public static class RenderFragments
{
public static RenderFragment GenTagItem(TodoTag tag) => builder =>
{
builder.OpenComponent(0, typeof(MListItemIcon));
builder.AddAttribute(1, "Class", "mr-4");
builder.AddAttribute(2, "ChildContent", (RenderFragment)(sub =>
{
sub.OpenComponent(0, typeof(MIcon));
sub.AddAttribute(1, "Icon", (Icon)"mdi-circle");
sub.AddAttribute(2, "Color", tag.Color);
sub.CloseComponent();
}));
builder.CloseComponent();

builder.OpenComponent<MListItemContent>(3);
builder.AddAttribute(4, "ChildContent", (RenderFragment)(sub =>
{
sub.OpenComponent<MListItemTitle>(0);
sub.AddAttribute(1, "ChildContent", (RenderFragment)(c => c.AddContent(0, tag.Name)));
sub.CloseComponent();
}));
builder.CloseComponent();
};
}
69 changes: 24 additions & 45 deletions Masa.Blazor.Pro.Components/Todo/TodoNav.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@namespace Masa.Blazor.Pro.Components
@using Masa.Blazor.Pro.Components.Models
@namespace Masa.Blazor.Pro.Components

<MNavigationDrawer Value="@Value"
ValueChanged="@ValueChanged"
<MNavigationDrawer Value="@Show"
ValueChanged="@ShowChanged"
Permanent="@Permanent"
Width="300"
Fixed>
Expand All @@ -10,7 +11,7 @@
<MButton Block Rounded Color="primary" Class="ma-6">Add Task</MButton>
}

<MList Routable Dense>
<MList Routable>
@foreach (var item in _categories)
{
<MListItem ActiveClass="primary" Href="@item.Href" Exact>
Expand All @@ -27,67 +28,45 @@

<MSubheader>Tags</MSubheader>

@foreach (var item in _tags)
@foreach (var item in Tags)
{
<MListItem ActiveClass="primary" Href="@item.Href" Exact>
<ItemContent>
<MListItemIcon Class="mr-4">
<MIcon Color="@item.IconOrColor">mdi-circle</MIcon>
</MListItemIcon>
<MListItemContent>
<MListItemTitle>@item.Title</MListItemTitle>
</MListItemContent>
</ItemContent>
</MListItem>
}

<MSubheader>Priority</MSubheader>

@foreach (var item in _priority)
{
<MListItem ActiveClass="primary" Href="@item.Href" Exact>
<ItemContent>
<MListItemIcon Class="mr-4">
<MIcon Color="@item.IconOrColor">mdi-flag-variant</MIcon>
</MListItemIcon>
<MListItemContent>
<MListItemTitle>@item.Title</MListItemTitle>
</MListItemContent>
</ItemContent>
<MListItem ActiveClass="primary" Href="@($"/todo/tag/{item.Id}")" Exact>
@RenderFragments.GenTagItem(item)
</MListItem>
}
<MListItem>
<MButton Block Small
Outlined
Style="border-style: dashed;"
Color="secondary"
OnClick="@OnAddTagClick">
<MIcon Left>mdi-plus</MIcon>
Add Tag
</MButton>
</MListItem>
</MList>
</MNavigationDrawer>

@code {

[Parameter] public bool? Value { get; set; }
[Parameter] public bool? Show { get; set; }

[Parameter] public EventCallback<bool?> ValueChanged { get; set; }
[Parameter] public EventCallback<bool?> ShowChanged { get; set; }

[Parameter] public bool Permanent { get; set; }

[Parameter] public bool ShowAddTask { get; set; }

[Parameter] public EventCallback OnAddTagClick { get; set; }

[Parameter] public List<TodoTag> Tags { get; set; } = [];

private static TodoNavItem[] _categories =
[
new TodoNavItem("Today", "mdi-calendar-today-outline", "/todo"),
new TodoNavItem("Inbox", "mdi-inbox-outline", "/todo/inbox")
];

private static TodoNavItem[] _tags =
[
new TodoNavItem("Team", "purple", "/todo/team"),
new TodoNavItem("Family", "pink", "/todo/family"),
];

private static TodoNavItem[] _priority =
[
new TodoNavItem("Low", "blue", "/todo/low"),
new TodoNavItem("Medium", "orange", "/todo/medium"),
new TodoNavItem("High", "red", "/todo/high"),
];

record TodoNavItem(string Title, string IconOrColor, string Href);

}
64 changes: 0 additions & 64 deletions Masa.Blazor.ProApp.Rcl/Data/ProDatabase.cs

This file was deleted.

4 changes: 0 additions & 4 deletions Masa.Blazor.ProApp.Rcl/Masa.Blazor.ProApp.Rcl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@
<ProjectReference Include="..\Masa.Blazor.Pro.Components\Masa.Blazor.Pro.Components.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
</ItemGroup>

</Project>
Loading

0 comments on commit b40ca6b

Please sign in to comment.