Ejemplo n.º 1
0
        public ChatPage(ViewModel.ChatViewModel viewModel)
        {
            InitializeComponent();
            SetupComponents(viewModel);

            App.Instance.Connector.StateChanged += OnBackendStateChanged;
        }
Ejemplo n.º 2
0
        private void StartSession(string sessionId, byte[] memberKey)
        {
            if (memberKey == null)
            {
                memberKey = model.GetMemberKey(sessionId);
            }
            Task.Run(async() =>
            {
                chatViewModel = new ViewModel.ChatViewModel();
                bool success  = await chatViewModel.Model.Join(memberKey);

                Device.BeginInvokeOnMainThread(async() =>
                {
                    if (success)
                    {
                        //await Navigation.PopModalAsync();
                        await Navigation.PushModalAsync(chatViewModel.View, true);
                    }
                    else
                    {
                        IMessage messageHandler = DependencyService.Get <IMessage>();
                        messageHandler?.ShowToast("Unable open chat session");
                        chatViewModel.Model.CloseSession();
                    }
                });
            });
        }
Ejemplo n.º 3
0
        private void SetupComponents(ViewModel.ChatViewModel viewModel)
        {
            model = viewModel.Model;
            messageList.ItemsSource = viewModel.Messages;

            Appearing += delegate
            {
                textArea.Focus();
            };

            messageList.ItemTapped += (object sender, ItemTappedEventArgs e) =>
            {
                if (e.Item == null)
                {
                    return;
                }
                messageList.SelectedItem = null;
            };

            textArea.Completed += delegate
            {
                SubmitMessage();
            };

            sendButton.Clicked += delegate
            {
                SubmitMessage();
            };

            shareButton.Clicked += async(object sender, EventArgs args) =>
            {
                try
                {
                    FileData fileData = await CrossFilePicker.Current.PickFile();

                    if (fileData == null)
                    {
                        return; // user canceled file picking
                    }
                    if (fileData.FileName.EndsWith("jpg", StringComparison.Ordinal) ||
                        fileData.FileName.EndsWith("png", StringComparison.Ordinal))
                    {
                        model.PushImage(fileData.DataArray);
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("Exception choosing file: " + ex.ToString());
                }
            };

            viewModel.Messages.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs e) =>
            {
                for (int i = e.NewStartingIndex; i < e.NewItems.Count; ++i)
                {
                    (e.NewItems[i] as ViewModel.ChatMessage).PropertyChanged += (object s, PropertyChangedEventArgs ev) =>
                    {
                        if (ev.PropertyName == "IsForeign") // Trigger a template update
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                // Trigger an update
                                messageList.ItemsSource = null;
                                messageList.ItemsSource = viewModel.Messages;
                            });
                        }
                    };
                }

                if (e.NewItems != null && (e.OldItems == null || e.NewItems.Count > e.OldItems.Count))
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        //ForceLayout();
                        messageList.ScrollTo(viewModel.Messages.LastOrDefault(), ScrollToPosition.End, true);
                    });
                }
            };
        }