Skip to content

Commit

Permalink
refactored the whole to implement a kind of Chat app
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Dumont committed Dec 31, 2015
1 parent 5b65142 commit 372e466
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 38 deletions.
10 changes: 4 additions & 6 deletions src/Flux.Net/Flux.Net.Cmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ static void Main(string[] args)
Dispatcher dispatcher = Dispatcher.Instance;
Store store = new Store();
dispatcher.Register(store.HandleAction);
Client cli1 = new Client("client 1", store);
Client cli2 = new Client("client 2", store);
cli1.ChangeData("newData");
cli1.ChangeData("anotherData");
Api api = new Api();
api.UpperCaseData();
Client cli1 = new Client("Bill", store);
Client cli2 = new Client("Jing", store);
cli1.CreateMessage("Hey jin, how are you");
cli2.CreateMessage("Fine thanks !");

Console.ReadLine();
}
Expand Down
11 changes: 4 additions & 7 deletions src/Flux.Net/Flux.Net/Action.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
using System;
using Flux.Net.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flux.Net
{


public class Action
{
public ActionTypes Type { get; set; }

public string Type { get; set; }

public string Data { get; set; }
public Message Message { get; set; }

public void Dispatch()
{
Dispatcher.Instance.Dispatch(this);
}


}
}
7 changes: 3 additions & 4 deletions src/Flux.Net/Flux.Net/ActionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

namespace Flux.Net
{
public static class ActionTypes
public enum ActionTypes
{
public const string UPPERCASE_DATA = "UPPERCASE_DATA";

public const string CHANGE_DATA = "CHANGE_DATA";
DELETE_MESSAGE,
ADD_MESSAGE
}
}
7 changes: 4 additions & 3 deletions src/Flux.Net/Flux.Net/Api.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Flux.Net.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,9 +9,9 @@ namespace Flux.Net
{
public class Api
{
public void UpperCaseData()
public void DeleteMessage(Message msg)
{
Action action = new Action() { Type = ActionTypes.UPPERCASE_DATA, Data = null };
Action action = new Action() { Type = ActionTypes.DELETE_MESSAGE, Message = msg };
action.Dispatch();
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/Flux.Net/Flux.Net/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Flux.Net.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -21,13 +22,22 @@ public Client(String name, Store store)

public void StoreChanged(object sender, EventArgs args)
{
Message lastMessage = _store.Messages.Last();
//Kind of rendering
Console.WriteLine($"I am {Name} and Got a change event from store : new data is :" + _store.Data);
Console.WriteLine($"[{this.Name} display ] {lastMessage.Author.Name} : {lastMessage.Text}");
}

public void ChangeData(string data)
public void CreateMessage(string text)
{
Action action = new Action() { Type = ActionTypes.CHANGE_DATA, Data = data };
Author author = new Author() { Name = this.Name };
Message msg = new Message(author, text);
Action action = new Action() { Type = ActionTypes.ADD_MESSAGE, Message = msg };
action.Dispatch();
}

public void DeleteMessage(Message msg)
{
Action action = new Action() { Type = ActionTypes.DELETE_MESSAGE, Message = msg };
action.Dispatch();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Flux.Net/Flux.Net/Flux.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<Compile Include="Api.cs" />
<Compile Include="Client.cs" />
<Compile Include="Dispatcher.cs" />
<Compile Include="Model\Author.cs" />
<Compile Include="Model\Message.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Store.cs" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/Flux.Net/Flux.Net/Model/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flux.Net.Model
{
public class Author
{
public Int64 Id { get; set; }

public String Name { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/Flux.Net/Flux.Net/Model/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flux.Net.Model
{
public class Message
{
public Message(Author author, String text)
{
this.Author = author;
this.Text = text;
}

public Int64 Id { get; set; }

public Author Author { get; private set; }

public String Text { get; private set; }


}
}
43 changes: 29 additions & 14 deletions src/Flux.Net/Flux.Net/Store.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Flux.Net.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -11,34 +12,48 @@ namespace Flux.Net

public class Store
{
public string data;
private IList<Message> messages = new List<Message>();

public event ChangedEventHandler Changed;

public string Data {
public IList<Message> Messages {
get
{
return data;
return messages;
}
private set
}

public void AddMessage(Message msg)
{
this.messages.Add(msg);
NotifyChange();
}

public void RemoveMessage(Message message)
{
Message msgtobeRemoved = this.messages.Where((msg) => msg.Id == message.Id).FirstOrDefault();
this.messages.Remove(msgtobeRemoved);
NotifyChange();
}

private void NotifyChange()
{
if (Changed != null)
{
data = value;
if(Changed != null)
{
Changed(this, null);
}
Changed(this, null);
}
}


public void HandleAction(Action action)
{
switch(action.Type)
{
case ActionTypes.CHANGE_DATA:
this.Data = action.Data;
case ActionTypes.ADD_MESSAGE:
this.AddMessage(action.Message);
break;
case ActionTypes.UPPERCASE_DATA:
this.Data = this.Data.ToUpper();
case ActionTypes.DELETE_MESSAGE:
this.RemoveMessage(action.Message);
break;
default:
throw new ArgumentException();
Expand Down

0 comments on commit 372e466

Please sign in to comment.