Skip to content

Commit

Permalink
#13 related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
skclusive committed Dec 7, 2020
1 parent c643dfd commit e6dd289
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public static void TryAddFlightFinderStateServices(this IServiceCollection servi
services.TryAddJsonTypeConverter<IItinerarySnapshot, ItinerarySnapshot>();
services.TryAddJsonTypeConverter<IAppStateSnapshot, AppStateSnapshot>();

services.TryAddJsonTypeConverter<IRootSnapshot, RootSnapshot>();
services.TryAddJsonTypeConverter<IBranchSnapshot, BranchSnapshot>();
services.TryAddJsonTypeConverter<ITreeSnapshot, TreeSnapshot>();

services.TryAddScoped((_) => AppStateType.Create(new AppStateSnapshot
{
SearchInProgress = false,
Expand All @@ -33,6 +37,19 @@ public static void TryAddFlightFinderStateServices(this IServiceCollection servi
OutboundDate = DateTime.Now.Date,

ReturnDate = DateTime.Now.Date.AddDays(7)
},

Root = new RootSnapshot
{
Tree = new TreeSnapshot
{
Branches = new IBranchSnapshot []
{
new BranchSnapshot { Name = "branch 1" },

new BranchSnapshot { Name = "branch 2" }
}
}
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IAppStateSnapshot
IItinerarySnapshot[] Shortlist { get; set; }

IAirportSnapshot[] Airports { get; set; }

IRootSnapshot Root { get; set; }
}

public interface IAppStateActions
Expand Down Expand Up @@ -55,6 +57,8 @@ public interface IAppState : IAppStateActions
IList<IAirport> Airports { get; set; }

IList<IItinerary> SortedSearchResults { get; }

IRoot Root { get; set; }
}

public class AppStateSnapshot : IAppStateSnapshot
Expand All @@ -70,6 +74,8 @@ public class AppStateSnapshot : IAppStateSnapshot
public IItinerarySnapshot[] Shortlist { get; set; }

public IAirportSnapshot[] Airports { get; set; }

public IRootSnapshot Root { get; set; }
}

internal class AppStateProxy : ObservableProxy<IAppState, INode>, IAppState
Expand Down Expand Up @@ -118,6 +124,12 @@ public IList<IAirport> Airports
set => Write(nameof(Airports), value);
}

public IRoot Root
{
get => Read<IRoot>(nameof(Root));
set => Write(nameof(Root), value);
}

public void AddToShortlist(IItinerarySnapshot itinerary)
{
(Target as dynamic).AddToShortlist(itinerary);
Expand Down Expand Up @@ -164,6 +176,7 @@ public partial class AppTypes
.Snapshot(() => new AppStateSnapshot())
.Mutable(o => o.SearchInProgress, Types.Boolean)
.Mutable(o => o.Airports, AirportListType)
.Mutable(o => o.Root, Types.Late("LateRoot", () => RootType))
.Mutable(o => o.SearchResults, ItineraryListType)
.Mutable(o => o.Shortlist, ItineraryListType)
.Mutable(o => o.SearchCriteria, SearchCriteriaType)
Expand All @@ -180,6 +193,24 @@ public partial class AppTypes
o.SearchInProgress = false;
foreach(var airport in airports)
o.Airports.Add(AirportType.Create(airport));

o.Root = RootType.Create
(
new RootSnapshot
{
Tree = new TreeSnapshot
{
Branches = new IBranchSnapshot []
{
new BranchSnapshot { Name = "branch 1" },

new BranchSnapshot { Name = "branch 2" },

new BranchSnapshot { Name = "branch 3" }
}
}
}
);
})
.Action((o) => o.BeginItinerarySearch(), (o) => o.SearchInProgress = true)
.Action<IItinerarySnapshot[]>((o) => o.EndItinerarySearch(null), (o, itineraries) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Skclusive.Mobx.Observable;
using Skclusive.Mobx.StateTree;
using System.Collections.Generic;
using System.Linq;

namespace Skclusive.FlightFinder.App.State
{
public interface IBranchSnapshot
{
string Name { set; get; }
}

public interface IBranchActions
{
void EditName(string name);
}

public interface IBranch : IBranchSnapshot, IBranchActions
{
}

internal class BranchSnapshot : IBranchSnapshot
{
public string Name { set; get; }
}

internal class BranchProxy : ObservableProxy<IBranch, INode>, IBranch
{
public override IBranch Proxy => this;

public BranchProxy(IObservableObject<IBranch, INode> target) : base(target)
{
}

public new string Name
{
get => Read<string>(nameof(Name));
set => Write(nameof(Name), value);
}

public void EditName(string name)
{
(Target as dynamic).EditName(name);
}
}


public partial class AppTypes
{
public readonly static IObjectType<IBranchSnapshot, IBranch> BranchType = Types
.Object<IBranchSnapshot, IBranch>("IBranch")
.Proxy(x => new BranchProxy(x))
.Snapshot(() => new BranchSnapshot())
.Mutable(o => o.Name, Types.String)
.Action<string>(o => o.EditName(default), (o, name) => o.Name = name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Skclusive.Core.Collection;
using Skclusive.Mobx.Observable;
using Skclusive.Mobx.StateTree;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Skclusive.FlightFinder.App.State
{
#region IRoot

public interface IRootSnapshot
{
ITreeSnapshot Tree { set; get; }
}

public interface IRootActions
{
}

public interface IRoot : IRootActions
{
ITree Tree { set; get; }
}

internal class RootSnapshot : IRootSnapshot
{
public ITreeSnapshot Tree { set; get; }
}

internal class RootProxy : ObservableProxy<IRoot, INode>, IRoot
{
public override IRoot Proxy => this;

public RootProxy(IObservableObject<IRoot, INode> target) : base(target)
{
}

public ITree Tree
{
get => Read<ITree>(nameof(Tree));
set => Write(nameof(Tree), value);
}
}

#endregion

public partial class AppTypes
{
public readonly static IObjectType<IRootSnapshot, IRoot> RootType = Types.
Object<IRootSnapshot, IRoot>("Root")
.Proxy(x => new RootProxy(x))
.Snapshot(() => new RootSnapshot())
.Mutable(o => o.Tree, Types.Late("LateTree", () => TreeType));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Skclusive.Core.Collection;
using Skclusive.Mobx.Observable;
using Skclusive.Mobx.StateTree;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Skclusive.FlightFinder.App.State
{
#region ITree

public interface ITreeSnapshot
{
IBranchSnapshot[] Branches { set; get; }
}

public interface ITreeActions
{
void AddBranch(IBranchSnapshot branch);
}

public interface ITree : ITreeActions
{
IList<IBranch> Branches { set; get; }
}

internal class TreeSnapshot : ITreeSnapshot
{
public IBranchSnapshot[] Branches { set; get; }
}

internal class TreeProxy : ObservableProxy<ITree, INode>, ITree
{
public override ITree Proxy => this;

public TreeProxy(IObservableObject<ITree, INode> target) : base(target)
{
}

public IList<IBranch> Branches
{
get => Read<IList<IBranch>>(nameof(Branches));
set => Write(nameof(Branches), value);
}

public void AddBranch(IBranchSnapshot branch)
{
(Target as dynamic).AddBranch(branch);
}
}

#endregion

public partial class AppTypes
{
public readonly static IObjectType<ITreeSnapshot, ITree> TreeType = Types.
Object<ITreeSnapshot, ITree>("Tree")
.Proxy(x => new TreeProxy(x))
.Snapshot(() => new TreeSnapshot())
.Mutable(o => o.Branches, Types.List(Types.Late("LateBranch", () => BranchType)))
.Action<IBranchSnapshot>(o => o.AddBranch(default), (o, branch) => o.Branches.Add(BranchType.Create(branch)));
}
}

0 comments on commit e6dd289

Please sign in to comment.