From 372e466cfee22ca65ec716ec02233f954481b67b Mon Sep 17 00:00:00 2001 From: Cedric Dumont Date: Thu, 31 Dec 2015 11:43:53 +0100 Subject: [PATCH] refactored the whole to implement a kind of Chat app --- src/Flux.Net/Flux.Net.Cmd/Program.cs | 10 +++--- src/Flux.Net/Flux.Net/Action.cs | 11 +++---- src/Flux.Net/Flux.Net/ActionTypes.cs | 7 ++--- src/Flux.Net/Flux.Net/Api.cs | 7 +++-- src/Flux.Net/Flux.Net/Client.cs | 18 ++++++++--- src/Flux.Net/Flux.Net/Flux.Net.csproj | 2 ++ src/Flux.Net/Flux.Net/Model/Author.cs | 15 +++++++++ src/Flux.Net/Flux.Net/Model/Message.cs | 25 +++++++++++++++ src/Flux.Net/Flux.Net/Store.cs | 43 +++++++++++++++++--------- 9 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 src/Flux.Net/Flux.Net/Model/Author.cs create mode 100644 src/Flux.Net/Flux.Net/Model/Message.cs diff --git a/src/Flux.Net/Flux.Net.Cmd/Program.cs b/src/Flux.Net/Flux.Net.Cmd/Program.cs index 36c3b0b..d55e881 100644 --- a/src/Flux.Net/Flux.Net.Cmd/Program.cs +++ b/src/Flux.Net/Flux.Net.Cmd/Program.cs @@ -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(); } diff --git a/src/Flux.Net/Flux.Net/Action.cs b/src/Flux.Net/Flux.Net/Action.cs index d5a346f..8b9b731 100644 --- a/src/Flux.Net/Flux.Net/Action.cs +++ b/src/Flux.Net/Flux.Net/Action.cs @@ -1,4 +1,5 @@ -using System; +using Flux.Net.Model; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,20 +7,16 @@ 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); } - } } diff --git a/src/Flux.Net/Flux.Net/ActionTypes.cs b/src/Flux.Net/Flux.Net/ActionTypes.cs index 0a83a74..3b0d51d 100644 --- a/src/Flux.Net/Flux.Net/ActionTypes.cs +++ b/src/Flux.Net/Flux.Net/ActionTypes.cs @@ -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 } } diff --git a/src/Flux.Net/Flux.Net/Api.cs b/src/Flux.Net/Flux.Net/Api.cs index 8f4a1c8..af69b58 100644 --- a/src/Flux.Net/Flux.Net/Api.cs +++ b/src/Flux.Net/Flux.Net/Api.cs @@ -1,4 +1,5 @@ -using System; +using Flux.Net.Model; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -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(); } } diff --git a/src/Flux.Net/Flux.Net/Client.cs b/src/Flux.Net/Flux.Net/Client.cs index 4d0cd2d..7bcdd5a 100644 --- a/src/Flux.Net/Flux.Net/Client.cs +++ b/src/Flux.Net/Flux.Net/Client.cs @@ -1,4 +1,5 @@ -using System; +using Flux.Net.Model; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -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(); } } diff --git a/src/Flux.Net/Flux.Net/Flux.Net.csproj b/src/Flux.Net/Flux.Net/Flux.Net.csproj index c00e28a..e711909 100644 --- a/src/Flux.Net/Flux.Net/Flux.Net.csproj +++ b/src/Flux.Net/Flux.Net/Flux.Net.csproj @@ -45,6 +45,8 @@ + + diff --git a/src/Flux.Net/Flux.Net/Model/Author.cs b/src/Flux.Net/Flux.Net/Model/Author.cs new file mode 100644 index 0000000..2e6eb51 --- /dev/null +++ b/src/Flux.Net/Flux.Net/Model/Author.cs @@ -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; } + } +} diff --git a/src/Flux.Net/Flux.Net/Model/Message.cs b/src/Flux.Net/Flux.Net/Model/Message.cs new file mode 100644 index 0000000..dae6a66 --- /dev/null +++ b/src/Flux.Net/Flux.Net/Model/Message.cs @@ -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; } + + + } +} diff --git a/src/Flux.Net/Flux.Net/Store.cs b/src/Flux.Net/Flux.Net/Store.cs index f0a4b55..721526a 100644 --- a/src/Flux.Net/Flux.Net/Store.cs +++ b/src/Flux.Net/Flux.Net/Store.cs @@ -1,4 +1,5 @@ -using System; +using Flux.Net.Model; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,34 +12,48 @@ namespace Flux.Net public class Store { - public string data; + private IList messages = new List(); public event ChangedEventHandler Changed; - public string Data { + public IList 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();