Skip to content

Commit

Permalink
Removed redundant immutable list from the client model
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullin committed Aug 11, 2013
1 parent 79f8391 commit 30fa8b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
32 changes: 19 additions & 13 deletions Source/Gtd.ClientCore/Models/ClientModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Gtd.Client.Models
public sealed class ClientModel
{
readonly IMessageQueue _queue;
IImmutableList<ImmutableStuff> _listOfGtdStuff = ImmutableList.Create<ImmutableStuff>();

ImmutableDictionary<StuffId, ImmutableStuff> _stuffInInbox = ImmutableDictionary.Create<StuffId, ImmutableStuff>();

readonly List<MutableProject> _projectList = new List<MutableProject>();
Expand All @@ -31,12 +31,17 @@ public ImmutableProject GetProjectOrNull(ProjectId projectId)

public ImmutableInbox GetInbox()
{
return new ImmutableInbox(_listOfGtdStuff.ToImmutableList());
return new ImmutableInbox(GetOrderedInbox());
}

ImmutableList<ImmutableStuff> GetOrderedInbox()
{
return _stuffInInbox.OrderBy(p => p.Value.Order).Select(i => i.Value).ToImmutableList();
}

public int GetTheNumberOfItemsOfStuffInInbox()
{
return _listOfGtdStuff.Count;
return _stuffInInbox.Count;
}

public readonly TrustedSystemId Id;
Expand All @@ -54,7 +59,7 @@ public void LoadingCompleted()
_loadingCompleted = true;

var model = new ImmutableClientModel(
_listOfGtdStuff,
GetOrderedInbox(),
ImmutableList.Create(_projectList.Select(m => m.Freeze()).ToArray()));

Publish(new Dumb.ClientModelLoaded(model));
Expand All @@ -67,27 +72,28 @@ void Publish(Dumb.CliendModelEvent e)
_queue.Enqueue(e);
}

uint _stuffOrderCounter = 0;

public void StuffPutInInbox(StuffId stuffId, string descriptionOfStuff, DateTime date)
{
var key = "stuff-" + Id.Id;
var item = new ImmutableStuff(stuffId, descriptionOfStuff, key);
var item = new ImmutableStuff(stuffId, descriptionOfStuff, key, _stuffOrderCounter++);

_listOfGtdStuff = _listOfGtdStuff.Add(item);

_stuffInInbox = _stuffInInbox.Add(stuffId, item);

Publish(new Dumb.StuffAddedToInbox(item, _listOfGtdStuff.Count));
Publish(new Dumb.StuffAddedToInbox(item, _stuffInInbox.Count));
}

public void StuffTrashed(StuffId stuffId)
{
ImmutableStuff value;
if (!_stuffInInbox.TryGetValue(stuffId, out value))
return;

_listOfGtdStuff = _listOfGtdStuff.Remove(value);

_stuffInInbox = _stuffInInbox.Remove(stuffId);

Publish(new Dumb.StuffRemovedFromInbox(value, _listOfGtdStuff.Count));
Publish(new Dumb.StuffRemovedFromInbox(value, _stuffInInbox.Count));
}


Expand All @@ -97,10 +103,10 @@ public void StuffArchived(StuffId stuffId)
if (!_stuffInInbox.TryGetValue(stuffId, out value))
return;

_listOfGtdStuff = _listOfGtdStuff.Remove(value);

_stuffInInbox = _stuffInInbox.Remove(stuffId);

Publish(new Dumb.StuffRemovedFromInbox(value, _listOfGtdStuff.Count));
Publish(new Dumb.StuffRemovedFromInbox(value, _stuffInInbox.Count));
}

public void ProjectDefined(ProjectId projectId, string projectOutcome, ProjectType type)
Expand Down Expand Up @@ -151,7 +157,7 @@ public void DescriptionOfStuffChanged(StuffId stuffId, string newDescriptionOfSt


var newValue = oldStuff.WithDescription(newDescriptionOfStuff);
_listOfGtdStuff = _listOfGtdStuff.Replace(oldStuff, newValue);

_stuffInInbox = _stuffInInbox.SetItem(stuffId, newValue);

Publish(new Dumb.StuffUpdated(newValue));
Expand Down
6 changes: 4 additions & 2 deletions Source/Gtd.ClientCore/Models/ClientPerspective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,19 @@ public sealed class ImmutableStuff
public readonly StuffId StuffId;
public readonly string Description;
public readonly string UIKey;
public readonly uint Order;

public ImmutableStuff WithDescription(string descriptionOfStuff)
{
return new ImmutableStuff(StuffId, descriptionOfStuff, UIKey);
return new ImmutableStuff(StuffId, descriptionOfStuff, UIKey,Order);
}

public ImmutableStuff(StuffId stuffId, string descriptionOfStuff, string uiKey)
public ImmutableStuff(StuffId stuffId, string descriptionOfStuff, string uiKey, uint order)
{
StuffId = stuffId;
Description = descriptionOfStuff;
UIKey = uiKey;
Order = order;
}
}

Expand Down

0 comments on commit 30fa8b2

Please sign in to comment.