Example #1
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            width = Resources.DisplayMetrics.WidthPixels;
            var alarmId = Intent.GetIntExtra("alarmId", 1);

            alarm = SQLiteDb.GetAlarms(this).GetAwaiter().GetResult().FirstOrDefault(a => a.Id == alarmId);
            var room = SQLiteDb.GetRooms(this).FirstOrDefault(r => r.Id == alarm.Id);

            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.ChatLayout);
            messagesListView = FindViewById <ListView>(Resource.Id.messagesListView);
            var newMessageLinearLayout = FindViewById <LinearLayout>(Resource.Id.newMessageLinearLayout);

            if (alarm.Archived == true)
            {
                newMessageLinearLayout.Visibility = ViewStates.Gone;
            }

            loggedUser = SQLiteDb.GetUser();
            LoadMessages(alarmId);
            newMessageEditText = FindViewById <EditText>(Resource.Id.newMessageEditText);
            newMessageEditText.SetWidth(Convert.ToInt32(width * 0.85));
            newMessageEditText.TextChanged += NewMessageEditText_TextChanged;

            var toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            SetSupportActionBar(toolbar);
            string title = alarm.Name;

            if (title.Length > 17)
            {
                title = alarm.Name.Substring(0, 17) + "...";
            }
            SupportActionBar.Title = title;
            // Create your application here

            if (loggedUser.Email != "failed")
            {
                var userName         = loggedUser.UserName;
                var hubConnection    = new HubConnection("https://pwszalarmwebapi.azurewebsites.net");
                var messagesHubProxy = hubConnection.CreateHubProxy("MessagesHub");

                messagesHubProxy.On <int, int, string, string, DateTime>("sendMessageToClients", (id, alarmId, userName, message, messageTime) =>
                {
                    if (messagesList.FirstOrDefault().UserName == "failed" && messagesList.FirstOrDefault().Message == "failed")
                    {
                        messagesList.Clear();
                    }
                    this.RunOnUiThread(() =>
                    {
                        var messageObj = new Messages
                        {
                            Id          = id,
                            AlarmId     = alarmId,
                            UserName    = userName,
                            Message     = message,
                            MessageTime = messageTime
                        };
                        messagesList.Add(messageObj);
                        DisplayMessages();
                    });
                });
                try
                {
                    await hubConnection.Start();
                }
                catch (Exception ex)
                {
                    SQLiteDb.ShowAlert(this, "Błąd", ex.Message);
                }

                //Sending message
                var sendImageButton = FindViewById <ImageButton>(Resource.Id.sendMessageImageButton);
                sendImageButton.Click += async(o, e) =>
                {
                    InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
                    var message            = newMessageEditText.Text;
                    if (!string.IsNullOrEmpty(message))
                    {
                        await messagesHubProxy.Invoke("SendMessage", new object[] { alarmId, userName, message });
                    }
                    message = null;
                    newMessageEditText.Text = message;
                };
                newMessageEditText.EditorAction += (sender, e) =>
                {
                    if (e.ActionId == ImeAction.Done)
                    {
                        sendImageButton.PerformClick();
                    }
                    else
                    {
                        e.Handled = false;
                    }
                };
            }
        }