Skip to content

Commit

Permalink
Switch to structs, change icon signature (#2)
Browse files Browse the repository at this point in the history
This pull request includes several changes to the codebase. The main changes are:

Switching to using structs instead of classes for improved performance and memory efficiency.

Updating the icon signature to match the new struct implementation.

Switch to a valid protocol

This pull request updates the protocol used in the codebase from "mandadin-web" to "web+mandadin". This change ensures that the protocol follows the correct format and is compatible with other systems.

Add FsToolkit, add The Blunt Parser

This pull request adds the FsToolkit.ErrorHandling and TheBlunt packages to the project. These packages provide additional functionality and error handling capabilities.

Add Parser module for checkbox parsing

This pull request introduces a new Parser module for parsing checkboxes in the codebase. This module allows for more efficient and accurate parsing of checkbox data.

Add logging
  • Loading branch information
AngelMunoz committed Nov 23, 2023
1 parent 8d9bb22 commit feec017
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 87 deletions.
7 changes: 6 additions & 1 deletion src/Mandadin.Client/Mandadin.Client.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -8,6 +8,8 @@
<ItemGroup>
<Compile Include="Types.fs" />
<Compile Include="Components/Navbar.fs" />
<Compile Include="Parser.fsi" />
<Compile Include="Parser.fs" />
<Compile Include="Modals.fs" />
<Compile Include="Views/Import.fs" />
<Compile Include="Views/Notes.fs" />
Expand All @@ -20,6 +22,9 @@
<PackageReference Include="Bolero" Version="0.*" />
<PackageReference Include="Bolero.Build" Version="0.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.*" />

<PackageReference Include="FsToolkit.ErrorHandling" Version="4.11.0" />
<PackageReference Include="TheBlunt" Version="1.0.1" />
</ItemGroup>
<!-- <ItemGroup>
<Content Remove="wwwroot\**" />
Expand Down
12 changes: 6 additions & 6 deletions src/Mandadin.Client/Modals.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Modals =
label {
attr.``class`` "btn-close"
on.click (fun _ -> dispatch Dismiss)
Icon.Get Close None
Icon.Get Close
}

h4 {
Expand Down Expand Up @@ -90,14 +90,14 @@ module Modals =

on.click (fun _ -> dispatch Import)

Icon.Get Check None
Icon.Get Check
text "Importar contenido"
}

button {
attr.``class`` "paper-btn btn-danger-outline"
on.click (fun _ -> dispatch Dismiss)
Icon.Get Trash None
Icon.Get Trash
text "Cancelar"
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ module Modals =
label {
attr.``class`` "btn-close"
on.click (fun _ -> Error() |> action)
Icon.Get Close None
Icon.Get Close
}

h4 {
Expand All @@ -167,14 +167,14 @@ module Modals =
button {
attr.``class`` "paper-btn btn-danger-outline"
on.click (fun _ -> Ok true |> action)
Icon.Get Check None
Icon.Get Check
text "Si, Continuar"
}

button {
attr.``class`` "paper-btn btn-success-outline"
on.click (fun _ -> Ok false |> action)
Icon.Get Trash None
Icon.Get Trash
text "Cancelar"
}
}
Expand Down
76 changes: 76 additions & 0 deletions src/Mandadin.Client/Parser.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace Mandadin.Client

open TheBlunt
open FsToolkit.ErrorHandling

module Parse =
let openCheckBox =
pchar (fun c -> c = '[') (fun c ->
$"'{c}' is not a valid character at this position")

let closeCheckBox =
pchar (fun c -> c = ']') (fun c ->
$"'{c}' is not a valid character at this position")

let completedCheckBox =
pchar (fun c -> c = 'x') (fun c ->
$"'{c}' is not a valid character at this position")

let checkBox =
parse {
let! ``open`` = openCheckBox
let! em1 = ptry blanks

let! isChecked =
ptry completedCheckBox
|> map (fun check ->
match check with
| Some _ -> true
| None -> false)

let! em2 = ptry blanks
let! ``close`` = closeCheckBox

return
{ range =
Range.merge
[ ``open``.range
em1.range
isChecked.range
em2.range
``close``.range ]
result = isChecked.result }
}

let eol =
pchoice [ pstr "\r\n"; pstr "\n"; pstr "\r" ]

let content =
parse {
let! init = blanks
let! content = many anyChar |> pconcat
let! endl = eoi

return
{ range =
Range.merge
[ init.range
content.range
endl.range ]
result = content.result }
}

let mandadinEntry =
content |> andThen checkBox

let inline parseLine line =
match run line mandadinEntry with
| POk { result = isChecked, content } ->
Ok([| box isChecked; box content |])
| PError e -> Error(line, e)

let entries lines =
lines
|> List.ofArray
|> List.traverseResultA parseLine
|> Result.map List.toArray
7 changes: 7 additions & 0 deletions src/Mandadin.Client/Parser.fsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Mandadin.Client

open TheBlunt

[<RequireQualifiedAccess>]
module Parse =
val entries: lines: string array -> Result<obj array array, (string * ParseError) list>
29 changes: 23 additions & 6 deletions src/Mandadin.Client/Startup.fs
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
namespace Mandadin.Client

open Microsoft.Extensions.Logging
open Microsoft.AspNetCore.Components.WebAssembly.Hosting

module Program =

[<EntryPoint>]
let Main args =
let builder =
WebAssemblyHostBuilder.CreateDefault(args)
task {
let builder =
WebAssemblyHostBuilder.CreateDefault(args)

builder.RootComponents.Add<Main.Mandadin>("#main")
builder.RootComponents.Add<Main.Mandadin>("#main")

builder.Build().RunAsync()
|> Async.AwaitTask
|> Async.Start
let level =
if builder.HostEnvironment.Environment = "Production" then
LogLevel.Information
else
LogLevel.Debug

builder.Logging.AddFilter(
"Microsoft.AspNetCore.Components.RenderTree.*",
LogLevel.Warning
)
|> ignore

builder.Logging.SetMinimumLevel(level) |> ignore

let app = builder.Build()

do! app.RunAsync()
}
|> ignore

0
33 changes: 19 additions & 14 deletions src/Mandadin.Client/Types.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Mandadin.Client

open Bolero
open Bolero.Html

[<RequireQualifiedAccess>]
type View =
Expand All @@ -10,29 +9,33 @@ type View =
| [<EndPoint "lists/{listId}">] ListDetail of listId: string
| [<EndPoint "/import">] Import

[<RequireQualifiedAccess>]
[<RequireQualifiedAccess; Struct>]
type Theme =
| Light
| Dark
| Custom

[<Struct>]
type SaveResult = { Id: string; Ok: bool; Rev: string }

[<Struct>]
type Note =
{ Id: string
Content: string
Rev: string }


[<Struct>]
type TrackList = { Id: string; Rev: string }

[<Struct>]
type TrackListItem =
{ Id: string
IsDone: bool
ListId: string
Name: string
Rev: string }

[<Struct>]
type Icon =
| Copy
| Share
Expand All @@ -58,17 +61,19 @@ module Icon =
type Check = Template<"wwwroot/icons/check.html">
type Back = Template<"wwwroot/icons/back.html">

let Get (icon: Icon) (color: Option<string>) : Node =
type Icon with

static member Get(icon: Icon, ?color: string) =
let color = defaultArg color "currentColor"

match icon with
| Copy -> Copy().Fill(color).Elt()
| Share -> Share().Fill(color).Elt()
| Trash -> Trash().Fill(color).Elt()
| Clipboard -> Clipboard().Fill(color).Elt()
| Save -> Save().Fill(color).Elt()
| Text -> Text().Fill(color).Elt()
| Import -> Import().Fill(color).Elt()
| Close -> Close().Fill(color).Elt()
| Check -> Check().Fill(color).Elt()
| Back -> Back().Fill(color).Elt()
| Copy -> Icon.Copy().Fill(color).Elt()
| Share -> Icon.Share().Fill(color).Elt()
| Trash -> Icon.Trash().Fill(color).Elt()
| Clipboard -> Icon.Clipboard().Fill(color).Elt()
| Save -> Icon.Save().Fill(color).Elt()
| Text -> Icon.Text().Fill(color).Elt()
| Import -> Icon.Import().Fill(color).Elt()
| Close -> Icon.Close().Fill(color).Elt()
| Check -> Icon.Check().Fill(color).Elt()
| Back -> Icon.Back().Fill(color).Elt()
Loading

0 comments on commit feec017

Please sign in to comment.