public static Task UpdateChatHistory(this ServerEventsClient client, ChatCommandHandler cmdReceiver)
 {
     return(client.ServiceClient.GetAsync(new GetChatHistory {
         Channels = client.Channels
     }).Success(chatHistory =>
     {
         cmdReceiver.FullHistory = new Dictionary <string, List <ChatMessage> >();
         foreach (var channel in client.Channels)
         {
             var currentChannel = channel;
             cmdReceiver.FullHistory.Add(channel,
                                         chatHistory.Results
                                         .Where(x => x.Channel == currentChannel)
                                         .Select(x => x).ToList());
         }
         cmdReceiver.SyncAdapter();
     }));
 }
        public static Task ChangeChannel(this ServerEventsClient client, string channel, ChatCommandHandler cmdReceiver)
        {
            if (cmdReceiver.FullHistory.ContainsKey(channel) && client.Channels.Contains(channel))
            {
                cmdReceiver.ChangeChannel(channel);
                cmdReceiver.SyncAdapter();
                return(Task.CompletedTask);
            }

            return(client.SubscribeToChannelsAsync(channel)
                   .Then(t =>
            {
                cmdReceiver.CurrentChannel = channel;
                return client.UpdateChatHistory(cmdReceiver);
            }));
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            LogManager.LogFactory = new GenericLogFactory(Console.WriteLine);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            // Get our button from the layout resource,
            // and attach an event to it
            Button sendButton = FindViewById <Button>(Resource.Id.sendMessageButton);

            messageBox = FindViewById <EditText>(Resource.Id.message);
            SupportToolbar toolbar = FindViewById <SupportToolbar>(Resource.Id.toolbar);

            drawerLayout   = FindViewById <DrawerLayout>(Resource.Id.drawer_layout);
            navigationView = FindViewById <NavigationView>(Resource.Id.nav_view);
            rightDrawer    = FindViewById <ListView>(Resource.Id.right_drawer);
            var messageHistoryList = FindViewById <ListView>(Resource.Id.messageHistory);
            var chatBackground     = FindViewById <ImageView>(Resource.Id.chat_background);

            InitDefaultBackground(chatBackground);

            navigationView.Tag = 0;
            rightDrawer.Tag    = 1;

            var messageHistoryAdapter = new MessageListViewAdapter(this, new List <ChatMessage>(), () => this.subscriberList);

            messageHistoryList.Adapter = messageHistoryAdapter;

            var channels = new[] { "home" };

            cmdReceiver = new ChatCommandHandler(this, messageHistoryAdapter, "home");
            var activity = this;

            client = new ServerEventsClient(BaseUrl, channels)
            {
                OnConnect = connectMsg =>
                {
                    client.UpdateChatHistory(cmdReceiver)
                    .ContinueWith(t =>
                                  connectMsg.UpdateUserProfile(activity));
                },
                OnCommand = command =>
                {
                    if (command is ServerEventJoin)
                    {
                        client.GetChannelSubscribersAsync()
                        .ContinueWith(t => {
                            subscriberList = t.Result;
                            Application.SynchronizationContext.Post(_ => {
                                // Refresh profile icons when users join
                                messageHistoryAdapter.NotifyDataSetChanged();
                            }, null);
                        });
                    }
                },
                OnException = error => {
                    Application.SynchronizationContext.Post(
                        _ => { Toast.MakeText(this, "Error : " + error.Message, ToastLength.Long); }, null);
                },
                //ServiceClient = new JsonHttpClient(BaseUrl),
                Resolver = new MessageResolver(cmdReceiver)
            };
            client.RegisterNamedReceiver <ChatReceiver>("cmd");
            client.RegisterNamedReceiver <TvReciever>("tv");
            client.RegisterNamedReceiver <CssReceiver>("css");

            SetSupportActionBar(toolbar);

            var rightDataSet = new List <string>(commands.Keys);
            var rightAdapter = new ActionListViewAdapter(this, rightDataSet);

            rightDrawer.Adapter    = rightAdapter;
            rightDrawer.ItemClick += (sender, args) =>
            {
                Application.SynchronizationContext.Post(_ =>
                {
                    messageBox.Text = commands[rightDataSet[args.Position]];
                    drawerLayout.CloseDrawer(rightDrawer);
                }, null);
            };

            drawerToggle = new ChatActionBarDrawerToggle(
                this,                          //Host Activity
                drawerLayout,                  //DrawerLayout
                toolbar,                       // Instance of toolbar, if you use other ctor, the hamburger icon/arrow animation won't work..
                Resource.String.openDrawer,    //Opened Message
                Resource.String.closeDrawer    //Closed Message
                );

            SupportActionBar.SetHomeButtonEnabled(true);
            SupportActionBar.SetDisplayShowTitleEnabled(true);

            drawerLayout.SetDrawerListener(drawerToggle);
            drawerToggle.SyncState();

            navigationView.NavigationItemSelected += OnChannelClick;
            sendButton.Click += async(e, args) => { await OnSendClick(e, args); };
        }
 protected override void OnDestroy()
 {
     client.Stop();
     cmdReceiver = null;
     base.OnDestroy();
 }
Пример #5
0
 public ChatReceiver(ChatCommandHandler chatMessageHandler)
 {
     this.chatMessageHandler = chatMessageHandler;
 }
Пример #6
0
 public TvReciever(ChatCommandHandler chatMessageHandler)
 {
     this.chatMessageHandler = chatMessageHandler;
 }
 public MessageResolver(ChatCommandHandler messageHandler)
 {
     this.messageHandler = messageHandler;
 }