Skip to content

Commit

Permalink
Added optional 'sortBy' parameter to BindList methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMarr committed Apr 1, 2024
1 parent 3a4c628 commit 3620772
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ReactiveElmish.Avalonia/ReactiveElmish.Avalonia.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/JordanMarr/ReactiveElmish.Avalonia</PackageProjectUrl>
<PackageTags>Avalonia F# fsharp Elmish Elm</PackageTags>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<!--Turn on warnings for unused values (arguments and let bindings) -->
<OtherFlags>$(OtherFlags) --warnon:1182</OtherFlags>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
Expand Down
6 changes: 4 additions & 2 deletions src/ReactiveElmish/ReactiveBindingsCS.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ type ReactiveBindingsCS(onPropertyChanged: Action<string>) =
member this.BindList<'Model, 'ModelProjection>(
store: IStore<'Model>,
modelProjectionSeq: Func<'Model, 'ModelProjection seq>,
[<Optional>] sortBy: Func<'ModelProjection, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] vmPropertyName
) = vm.BindList(store, modelProjectionSeq.Invoke, vmPropertyName)
) = vm.BindList(store, modelProjectionSeq.Invoke, ?sortBy = opt sortBy, vmPropertyName = vmPropertyName)

member this.BindList<'Model, 'ModelProjection, 'Mapped>(
store: IStore<'Model>,
modelProjectionSeq: Func<'Model, 'ModelProjection seq>,
map: Func<'ModelProjection, 'Mapped>,
[<Optional>] sortBy: Func<'Mapped, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] vmPropertyName
) = vm.BindList(store, modelProjectionSeq.Invoke, map.Invoke, vmPropertyName)
) = vm.BindList(store, modelProjectionSeq.Invoke, map.Invoke, ?sortBy = opt sortBy, vmPropertyName = vmPropertyName)

member this.BindKeyedList<'Model, 'Key, 'Value, 'Mapped when 'Value : equality and 'Mapped : not struct and 'Key : comparison>(
store: IStore<'Model>,
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveElmish/ReactiveElmish.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/JordanMarr/ReactiveElmish</PackageProjectUrl>
<PackageTags>F# C# fsharp Elmish MVU MVVM</PackageTags>
<Version>1.3.0</Version>
<Version>1.4.0</Version>
<!--Turn on warnings for unused values (arguments and let bindings) -->
<OtherFlags>$(OtherFlags) --warnon:1182</OtherFlags>
</PropertyGroup>
Expand Down
14 changes: 12 additions & 2 deletions src/ReactiveElmish/ReactiveElmishViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@ type ReactiveElmishViewModel(onPropertyChanged: string -> unit) =
member this.BindList<'Model, 'ModelProjection>(
store: IStore<'Model>,
modelProjectionSeq: 'Model -> 'ModelProjection seq,
?sortBy: Func<'ModelProjection, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
if not (propertyCollections.ContainsKey vmPropertyName.Value) then
let mutable readOnlyList = readOnlyCollection<'ModelProjection>()
let sourceList = SourceList.createFrom (modelProjectionSeq store.Model)
sourceList
.Connect()
.Bind(&readOnlyList)
|> fun x ->
match sortBy with
| Some sortBy -> x.Sort(DynamicData.Binding.SortExpressionComparer.Ascending(sortBy))
| None -> x.Sort(Comparer.Create(fun _ _ -> 0))
|> _.Bind(&readOnlyList)
.Subscribe()
|> this.AddDisposable

Expand All @@ -130,6 +135,7 @@ type ReactiveElmishViewModel(onPropertyChanged: string -> unit) =
store: IStore<'Model>,
modelProjectionSeq: 'Model -> 'ModelProjection seq,
map: 'ModelProjection -> 'Mapped,
?sortBy: Func<'Mapped, IComparable>,
[<CallerMemberName; Optional; DefaultParameterValue("")>] ?vmPropertyName
) =
let vmPropertyName = vmPropertyName.Value
Expand All @@ -140,7 +146,11 @@ type ReactiveElmishViewModel(onPropertyChanged: string -> unit) =
sourceList
.Connect()
.Transform(map)
.Bind(&readOnlyList)
|> fun x ->
match sortBy with
| Some sortBy -> x.Sort(DynamicData.Binding.SortExpressionComparer.Ascending(sortBy))
| None -> x.Sort(Comparer.Create(fun _ _ -> 0))
|> _.Bind(&readOnlyList)
.Subscribe()
|> this.AddDisposable

Expand Down
8 changes: 7 additions & 1 deletion src/Samples/AvaloniaExample/ViewModels/CounterViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ type CounterViewModel() =
|> Program.mkStore

member this.Count = this.Bind(local, _.Count)
member this.Actions = this.BindList(local, _.Actions, map = fun a -> { a with Description = $"** {a.Description} **" })
member this.Actions =
this.BindList(
local
, _.Actions
, map = fun a -> { a with Description = $"** {a.Description} **" }
, sortBy = _.Timestamp
)
member this.Increment() = local.Dispatch Increment
member this.Decrement() = local.Dispatch Decrement
member this.Reset() = local.Dispatch Reset
Expand Down

0 comments on commit 3620772

Please sign in to comment.