示例#1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.SendBirdActivityChat);
            this.Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);

            InitFragment(savedInstanceState);
            InitUIComponents();
            InitSendBird(this.Intent.Extras);

            mMessageListQuery           = SendBirdSDK.QueryMessageList(mChannelUrl);
            mMessageListQuery.OnResult += (sender, e) => {
                // mSyncContext.Post for RunOnUIThread
                mSyncContext.Post(delegate {
                    foreach (var messageModel in e.Messages)
                    {
                        mSendBirdChatAdapter.AddMessageModel(messageModel);
                    }

                    mSendBirdChatFragment.mAdapter.NotifyDataSetChanged();
                    mSendBirdChatFragment.mListView.SetSelection(mSendBirdChatAdapter.Count);

                    SendBirdSDK.Join(mChannelUrl);
                    SendBirdSDK.Connect(mSendBirdChatAdapter.GetMaxMessageTimestamp());
                }, null);
            };
            mMessageListQuery.Prev(long.MaxValue, 50);              // Excute Query
        }
示例#2
0
        protected override void OnActivityResult(int requestCode, Android.App.Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (requestCode == REQUEST_CHANNEL_LIST)
            {
                if (resultCode == Android.App.Result.Ok && data != null)
                {
                    mChannelUrl = data.GetStringExtra("channelUrl");

                    mSendBirdChatAdapter.Clear();
                    mSendBirdChatAdapter.NotifyDataSetChanged();

                    mMessageListQuery           = SendBirdSDK.QueryMessageList(mChannelUrl);
                    mMessageListQuery.OnResult += (sender, e) => {
                        // mSyncContext.Post for RunOnUIThread
                        mSyncContext.Post(delegate {
                            foreach (var messageModel in e.Messages)
                            {
                                mSendBirdChatAdapter.AddMessageModel(messageModel);
                            }

                            mSendBirdChatAdapter.NotifyDataSetChanged();
                            mSendBirdChatFragment.mListView.SetSelection(mSendBirdChatAdapter.Count);

                            SendBirdSDK.Join(mChannelUrl);
                            SendBirdSDK.Connect(mSendBirdChatAdapter.GetMaxMessageTimestamp());
                        }, null);
                    };
                    mMessageListQuery.Prev(long.MaxValue, 50);                      // Excute Query
                }
            }
        }
示例#3
0
            private void InitUIComponents(View rootView)
            {
                mListView = rootView.FindViewById(Resource.Id.list) as ListView;
                TurnOffListViewDecoration(mListView);
                mListView.Adapter = mAdapter;

                mBtnChannel        = rootView.FindViewById(Resource.Id.btn_channel) as ImageButton;
                mBtnChannel.Click += (sender, e) => {
                    if (OnChannelListClicked != null)
                    {
                        OnChannelListClicked(this, new EventArgs());
                    }
                };
                mBtnSend               = rootView.FindViewById(Resource.Id.btn_send) as Button;
                mBtnUpload             = rootView.FindViewById(Resource.Id.btn_upload) as ImageButton;
                mProgressBtnUpload     = rootView.FindViewById(Resource.Id.progress_btn_upload) as ProgressBar;
                mEtxtMessage           = rootView.FindViewById(Resource.Id.etxt_message) as EditText;
                mEtxtMessage.KeyPress += (object sender, View.KeyEventArgs e) => {
                    if (e.KeyCode == Keycode.Enter)
                    {
                        if (e.Event.Action == KeyEventActions.Down)
                        {
                            Send();

                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = false;
                    }
                };

                mBtnSend.Enabled = false;
                mBtnSend.Click  += (object sender, EventArgs e) => {
                    Send();
                };

                mEtxtMessage.AfterTextChanged += (object sender, AfterTextChangedEventArgs e) => {
                    mBtnSend.Enabled = e.Editable.Length() > 0 ? true : false;
                    if (e.Editable.Length() > 0)
                    {
                        SendBirdSDK.TypeStart();
                    }
                    else
                    {
                        SendBirdSDK.TypeEnd();
                    }
                };
                mListView.Touch += (object sender, View.TouchEventArgs e) => {
                    Helper.HideKeyboard(this.Activity);
                    e.Handled = false;
                };
                mListView.ScrollStateChanged += (s, args) => {
                    if (args.ScrollState == ScrollState.Idle)
                    {
                        if (args.View.FirstVisiblePosition == 0 && args.View.ChildCount > 0 && args.View.GetChildAt(0).Top == 0)
                        {
                            var mMessageListQuery = SendBirdSDK.QueryMessageList(SendBirdSDK.GetChannelUrl());
                            mMessageListQuery.OnResult += (sender, e) => {
                                mSyncContext.Post(delegate {
                                    foreach (var messageModel in e.Messages)
                                    {
                                        mAdapter.AddMessageModel(messageModel);
                                    }

                                    mAdapter.NotifyDataSetChanged();
                                    mListView.SetSelection(e.Messages.Count);
                                }, null);
                            };
                            mMessageListQuery.Prev(mAdapter.GetMinMessageTimestamp(), 30);                              // Excute Query
                        }
                        else if (args.View.FirstVisiblePosition == args.View.ChildCount - 1 && args.View.ChildCount > 0)
                        {
                            var mMessageListQuery = SendBirdSDK.QueryMessageList(SendBirdSDK.GetChannelUrl());
                            mMessageListQuery.OnResult += (sender, e) => {
                                mSyncContext.Post(delegate {
                                    foreach (var messageModel in e.Messages)
                                    {
                                        mAdapter.AddMessageModel(messageModel);
                                    }

                                    mAdapter.NotifyDataSetChanged();
                                }, null);
                            };
                            mMessageListQuery.Next(mAdapter.GetMaxMessageTimestamp(), 30);                              // Excute Query
                        }
                    }
                };
            }
示例#4
0
        private void InitSendBird(Bundle extras)
        {
            mSendBirdInfo = extras;

            String appId    = extras.GetString("appId");
            String uuid     = extras.GetString("uuid");
            String userName = extras.GetString("userName");

            mSyncContext = SynchronizationContext.Current;             // required for api queries

            #region SendBirdEventHandler

            SendBirdEventHandler seh = new SendBirdEventHandler();
            seh.OnConnect         += (sender, e) => {
            };
            seh.OnError           += (sender, e) => {
            };
            seh.OnChannelLeft     += (sender, e) => {
            };
            seh.OnMessageReceived += (sender, e) => {
                SendBirdSDK.MarkAsRead();
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.AddMessageModel(e.Message);
                }, null);
            };
            seh.OnBroadcastMessageReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.AddMessageModel(e.Message);
                }, null);
            };
            seh.OnSystemMessageReceived += (sender, e) => {
                var systemMessage = (SystemMessage)e.Message;
                mSyncContext.Post(delegate {
                    switch (systemMessage.category)
                    {
                    case SystemMessage.CATEGORY_TOO_MANY_MESSAGES:
                        systemMessage.SetMessage("Too many messages. Please try later.");
                        break;

                    case SystemMessage.CATEGORY_MESSAGING_USER_BLOCKED:
                        systemMessage.SetMessage("Blocked.");
                        break;

                    case SystemMessage.CATEGORY_MESSAGING_USER_DEACTIVATED:
                        systemMessage.SetMessage("Deactivated.");
                        break;
                    }

                    mSendBirdMessagingAdapter.AddMessageModel(systemMessage);
                }, null);
            };
            seh.OnFileReceived += (sender, e) => {
            };
            seh.OnReadReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.SetReadStatus(e.ReadStatus.GetUserId(), e.ReadStatus.timestamp);
                }, null);
            };
            seh.OnTypeStartReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.SetTypeStatus(e.TypeStatus.GetUserId(), SendBirdUtils.GetCurrentTimeMills());
                }, null);
            };
            seh.OnTypeEndReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.SetTypeStatus(e.TypeStatus.GetUserId(), 0);
                }, null);
            };
            seh.OnAllDataReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.NotifyDataSetChanged();
                    mSendBirdMessagingFragment.mListView.SetSelection(mSendBirdMessagingAdapter.Count - 1);
                }, null);
            };
            seh.OnMessageDelivery += (sender, e) => {
                if (!e.Sent)
                {
                    mSyncContext.Post(delegate {
                        mSendBirdMessagingFragment.mEtxtMessage.Text = e.Message;
                    }, null);
                }
            };
            seh.OnMessagingStarted += (sender, e) => {
                mSendBirdMessagingAdapter.Clear();
                mSyncContext.Post(delegate {
                    UpdateMessagingChannel(e.MessagingChannel);

                    var channelUrl = e.MessagingChannel.GetUrl();

                    MessageListQuery messageListQuery = SendBirdSDK.QueryMessageList(channelUrl);
                    messageListQuery.OnResult        += (sender_child, e_child) => {
                        // mSyncContext.Post for RunOnUIThread
                        mSyncContext.Post(delegate {
                            foreach (var messageModel in e_child.Messages)
                            {
                                mSendBirdMessagingAdapter.AddMessageModel(messageModel);
                            }

                            mSendBirdMessagingAdapter.NotifyDataSetChanged();
                            mSendBirdMessagingFragment.mListView.SetSelection(30);

                            SendBirdSDK.MarkAsRead(channelUrl);
                            SendBirdSDK.Join(channelUrl);
                            SendBirdSDK.Connect(mSendBirdMessagingAdapter.GetMaxMessageTimestamp());
                        }, null);
                    };
                    messageListQuery.Prev(long.MaxValue, 50);                      // Excute Query
                }, null);
            };
            seh.OnMessagingUpdated += (sender, e) => {
                mSyncContext.Post(delegate {
                    UpdateMessagingChannel(e.MessagingChannel);
                }, null);
            };
            seh.OnMessagingChannelUpdated += (sender, e) => {
                if (mMessagingChannel != null && mMessagingChannel.GetId() == e.MessagingChannel.GetId())
                {
                    mSyncContext.Post(delegate {
                        UpdateMessagingChannel(e.MessagingChannel);
                    }, null);
                }
            };
            seh.OnMentionUpdated += (sender, e) => {
            };

            #endregion

            SendBirdSDK.Init(appId);
            SendBirdSDK.Login(uuid, userName);
            SendBirdSDK.SetEventHandler(seh);
        }
示例#5
0
            private void InitUIComponents(View rootView)
            {
                mListView = rootView.FindViewById(Resource.Id.list_view) as ListView;
                TurnOffListViewDecoration(mListView);
                mListView.Adapter = mAdapter;

                mBtnChannel        = rootView.FindViewById(Resource.Id.btn_channel) as ImageButton;
                mBtnChannel.Click += (sender, e) => {
                    if (OnChannelListClicked != null)
                    {
                        OnChannelListClicked(this, new EventArgs());
                    }
                };
                mBtnSend          = rootView.FindViewById(Resource.Id.btn_send) as Button;
                mBtnUpload        = rootView.FindViewById(Resource.Id.btn_upload) as ImageButton;
                mBtnUpload.Click += delegate {
                    Intent intent = new Intent();
                    intent.SetType("image/*");
                    intent.SetAction(Intent.ActionGetContent);
                    StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), REQUEST_PICK_IMAGE);
                };
                mProgressBtnUpload     = rootView.FindViewById(Resource.Id.progress_btn_upload) as ProgressBar;
                mEtxtMessage           = rootView.FindViewById(Resource.Id.etxt_message) as EditText;
                mEtxtMessage.KeyPress += (object sender, View.KeyEventArgs e) => {
                    if (e.KeyCode == Keycode.Enter)
                    {
                        if (e.Event.Action == KeyEventActions.Down)
                        {
                            Send();

                            e.Handled = true;
                        }
                    }
                    else
                    {
                        e.Handled = false;
                    }
                };

                mBtnSend.Enabled = false;
                mBtnSend.Click  += (object sender, EventArgs e) => {
                    Send();
                };

                mEtxtMessage.AfterTextChanged += (object sender, AfterTextChangedEventArgs e) => {
                    mBtnSend.Enabled = true;
                };
                mListView.Touch += (object sender, View.TouchEventArgs e) => {
                    Helper.HideKeyboard(this.Activity);
                    e.Handled = false;
                };
                mListView.ScrollStateChanged += (s, args) => {
                    if (args.ScrollState == ScrollState.Idle)
                    {
                        if (args.View.FirstVisiblePosition == 0 && args.View.ChildCount > 0 && args.View.GetChildAt(0).Top == 0)
                        {
                            var mMessageListQuery = SendBirdSDK.QueryMessageList(SendBirdSDK.GetChannelUrl());
                            mMessageListQuery.OnResult += (sender, e) => {
                                // mSyncContext.Post for RunOnUIThread
                                mSyncContext.Post(delegate {
                                    foreach (var messageModel in e.Messages)
                                    {
                                        mAdapter.AddMessageModel(messageModel);
                                    }

                                    mAdapter.NotifyDataSetChanged();
                                    mListView.SetSelection(e.Messages.Count);
                                }, null);
                            };
                            mMessageListQuery.Prev(mAdapter.GetMinMessageTimestamp(), 30);                              // Excute Query
                        }
                    }
                };

                // Register Cache
                // Get max available VM memory, exceeding this amount will throw an OutOfMemory exception.
                // Stored in kilobytes as LruCache takes an int in its constructor.
                var cacheSize = (int)(Java.Lang.Runtime.GetRuntime().MaxMemory() / 16);

                mMemoryCache = new ImageUtils.MemoryLimitedLruCache(cacheSize);
            }
示例#6
0
    public void InitMessaging()
    {
        currentUserName = inputUserName.text;
        SendBirdSDK.Login(userId, currentUserName);

        SendBirdEventHandler seh = new SendBirdEventHandler();

        seh.OnConnect += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                selectedChannelUrl = e.Channel.url;
            }));
        };
        seh.OnError           += (sender, e) => {
        };
        seh.OnChannelLeft     += (sender, e) => {
        };
        seh.OnMessageReceived += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                Message message          = e.Message as Message;
                txtMessagingContent.text = txtMessagingContent.text + (MessageRichText(message) + "\n");
                ScrollToBottom();
            }));
            // markasread
        };
        seh.OnBroadcastMessageReceived += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                BroadcastMessage bm      = e.Message as BroadcastMessage;
                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(bm.message) + "\n");
            }));
        };
        seh.OnSystemMessageReceived += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                SystemMessage sm         = e.Message as SystemMessage;
                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(sm.message) + "\n");
            }));
        };
        seh.OnAllDataReceived   += (sender, e) => {
        };
        seh.OnMessageDelivery   += (sender, e) => {
        };
        seh.OnReadReceived      += (sender, e) => {
        };
        seh.OnTypeStartReceived += (sender, e) => {
        };
        seh.OnTypeEndReceived   += (sender, e) => {
        };
        seh.OnMessagingStarted  += (sender, e) => {
            UpdateMessagingChannel(e.MessagingChannel);

            var messagingChannelUrl = e.MessagingChannel.GetUrl();
            // message query
            MessageListQuery messageListQuery = SendBirdSDK.QueryMessageList(messagingChannelUrl);
            messageListQuery.OnResult += (sender_child, e_child) => {
                mEventProcessor.QueueEvent(new Action(() => {
                    foreach (var messageModel in e_child.Messages)
                    {
                        if (messageModel is Message)
                        {
                            var message = messageModel as Message;
                            if (message.IsPast())
                            {
                                txtMessagingContent.text = (MessageRichText(message) + "\n") + txtMessagingContent.text;
                            }
                            else
                            {
                                txtMessagingContent.text = txtMessagingContent.text + (MessageRichText(message) + "\n");
                            }
                            UpdateMessageTimestamp(message);
                        }
                        else if (messageModel is SystemMessage)
                        {
                            var message = messageModel as SystemMessage;
                            if (message.IsPast())
                            {
                                txtMessagingContent.text = (SystemMessageRichText(message.message) + "\n") + txtMessagingContent.text;
                            }
                            else
                            {
                                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(message.message) + "\n");
                            }
                            UpdateMessageTimestamp(message);
                        }
                        else if (messageModel is BroadcastMessage)
                        {
                            var message = messageModel as BroadcastMessage;
                            if (message.IsPast())
                            {
                                txtMessagingContent.text = (SystemMessageRichText(message.message) + "\n") + txtMessagingContent.text;
                            }
                            else
                            {
                                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(message.message) + "\n");
                            }
                            UpdateMessageTimestamp(message);
                        }
                    }

                    SendBirdSDK.MarkAsRead(messagingChannelUrl);
                    SendBirdSDK.Join(messagingChannelUrl);
                    SendBirdSDK.Connect(GetMaxMessageTimestamp());
                }));
            };
            messageListQuery.Prev(long.MaxValue, 50);
        };
        seh.OnMessagingUpdated += (sender, e) => {
            UpdateMessagingChannel(e.MessagingChannel);
        };
        seh.OnMessagingChannelUpdated += (sender, e) => {
            if (mMessagingChannel != null && mMessagingChannel.GetId() == e.MessagingChannel.GetId())
            {
                mEventProcessor.QueueEvent(new Action(() => {
                    UpdateMessagingChannel(e.MessagingChannel);
                }));
            }
        };

        SendBirdSDK.SetEventHandler(seh);
    }