Building the ChatPresenter

Now we move on to the next screen; this will be our chat window in which we will be passing messages between different clients connected to the server Hub. Our first step is to build the ChatPresenter:

public class ChatPresenter : BasePresenter
     {
         #region Private Properties
         private Client _client;
         private IChatView _view;
         #endregion
         #region IChatView
         public interface IChatView : IView
         {
             void NotifyChatMessageReceived(string message);
         }
         #endregion
     } 
 

We are going to start by inheriting the BasePresenter class. It will include two private properties, one for the Client selected from the previous ClientListView screen and another for the IChatView interface. The IChatView interface inherits the IView interface and it will include one function for handling messages received from the receiving Client.

Let's implement the following:

#region Constructors
         public ChatPresenter(ApplicationState state, INavigationService navigationService, Client client)
         {
             _navigationService = navigationService;
             _state = state;
             _client = client;
         }
         #endregion
         #region Public Methods
         public void SetView(IChatView view)
         {
             _view = view;
             ChatReceived -= HandleChatReceived;
             ChatReceived += HandleChatReceived;
         }
         public async Task SendChat(string message)
         {
             await _signalRClient.SendMessageToClient(_client.ConnectedId, message);
         }
 
         #endregion
         #region Private Methods
         private void HandleChatReceived(object sender, ChatEventArgs e)
         {
             _view.NotifyChatMessageReceived(e.Message);
         }
         #endregion 

It is the same set up as the ClientsListPresenter; our SetView function will take the native view object and register the events. We also have another function, SendChat which will invoke the SendChat function on the Hub. Don't forget the ReleaseView function; this will be exactly the same as the ClientsListPresenter:

public void ReleaseView()
         {
             _signalRClient.OnDataReceived -= HandleSignalRDataReceived;
         } 

Now that we have built all our presenter objects, we need to make a small update to the navigation service implementations to allow navigation for the other screens. Open the Android NavigationService.cs, and in the PushPresenter function update the if statement to the following:

if (presenter is LoginPresenter)
                 {
                     intent = new Intent(_application.CurrentActivity, typeof(LoginActivity));
                 }
                 else if (presenter is ClientsListPresenter)
                 {
                     intent = new Intent(_application.CurrentActivity, typeof(ClientsListActivity));
                 }
                 else if (presenter is ChatPresenter)
                 {
                     intent = new Intent(_application.CurrentActivity, typeof(ChatActivity));
                 }  

For the iOS NavigationService.cs, update the if statement to the following:

if (presenter is LoginPresenter)
             {
                 var viewController = new LoginViewController(presenter as LoginPresenter);
                 _navigationController.PushViewController(viewController, true);
             }
             else if (presenter is ClientsListPresenter)
             {
                 var viewController = new ClientsListViewController(presenter as ClientsListPresenter);
                 _navigationController.PushViewController(viewController, true);
             }
             else if (presenter is ChatPresenter)
             {
                 var viewController = new ChatViewController(presenter as ChatPresenter);
                 _navigationController.PushViewController(viewController, true);
             } 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.156.140