Exemplo n.º 1
0
        private void ShowChats(User user)
        {
            TextView placeholder = FindViewById <TextView> (Resource.Id.placeholder);
            var      x           = db.Table <Chat> ();

            if (x.Count() > 0)
            {
                placeholder.Visibility = ViewStates.Gone;
            }
            else
            {
                placeholder.Visibility = ViewStates.Visible;
                placeholder.Text       = "No chats found.";
            }
            LinearLayout chatList = FindViewById <LinearLayout> (Resource.Id.chatList);

            chatList.RemoveAllViews();
            foreach (var elem in x.OrderByDescending(e => e.time))
            {
                View v;
                if (setting.FontSize == Setting.Size.large)
                {
                    v = LayoutInflater.Inflate(Resource.Layout.ChatLarge, null);
                }
                else
                {
                    v = LayoutInflater.Inflate(Resource.Layout.Chat, null);
                }
                TextView  name    = v.FindViewById <TextView>(Resource.Id.chatName);
                TextView  message = v.FindViewById <TextView>(Resource.Id.chatMessage);
                TextView  time    = v.FindViewById <TextView>(Resource.Id.chatTime);
                ImageView image   = v.FindViewById <ImageView> (Resource.Id.chatImage);

                new Thread(async() => {
                    try {
                        string conv = elem.conversation;
                        if (conv.Split(',').Length == 2)
                        {
                            if (conv.Split(',')[0] == user.user)
                            {
                                conv = conv.Split(',')[1];
                            }
                            else
                            {
                                conv = conv.Split(',')[0];
                            }
                        }
                        var imageBitmap = await network.GetImageBitmapFromUrl(Resources.GetString(Resource.String.profileUrl) + conv + ".png", AsyncNetwork.MINI_PROFILE_SIZE, AsyncNetwork.MINI_PROFILE_SIZE);
                        RunOnUiThread(() => image.SetImageBitmap(imageBitmap));
                    } catch (Exception e) {
                        Log.Error("BlaChat", e.StackTrace);
                    }
                }).Start();

                name.Text = elem.name;
                var tmp     = db.Table <Message> ().Where(q => q.conversation == elem.conversation).OrderByDescending(q => q.time);
                var lastMsg = tmp.FirstOrDefault();
                if (lastMsg != null)
                {
                    var escape = lastMsg.text.Replace("&quot;", "\"");
                    escape = escape.Replace("&lt;", "<");
                    escape = escape.Replace("&gt;", ">");
                    escape = escape.Replace("&amp;", "&");
                    if (lastMsg.nick == user.user)
                    {
                        message.Text = "Du: " + escape;
                    }
                    else
                    {
                        if (elem.conversation.Split(',').Length == 2 && lastMsg.nick != "watchdog")
                        {
                            message.Text = escape;
                        }
                        else
                        {
                            message.Text = lastMsg.author + ": " + escape;
                        }
                    }
                    time.Text = TimeConverter.AutoConvert(lastMsg.time);
                }
                else
                {
                    time.Text    = "";
                    message.Text = "No message";
                }

                v.Clickable = true;
                v.Click    += delegate {
                    Finish();
                    var intent = new Intent(this, typeof(ChatActivity));
                    intent.PutExtra("conversation", elem.conversation);
                    StartActivity(intent);
                    var chat = db.Get <Chat> (elem.conversation);
                    new Thread(async() => {
                        if (textShare != null)
                        {
                            while (!await network.SendMessage(db, user, chat, "(Shared Text) " + textShare))
                            {
                                await network.Authenticate(db, user);
                            }
                        }
                        foreach (var file in fileset)
                        {
                            Bitmap img = MediaStore.Images.Media.GetBitmap(ContentResolver, file);
                            while (!await network.SendImage(db, user, chat, img))
                            {
                                await network.Authenticate(db, user);
                            }
                        }
                    }).Start();
                };
                chatList.AddView(v);
            }
            chatList.Post(() => chatList.RequestLayout());
        }
Exemplo n.º 2
0
        protected override void OnCreate(Bundle bundle)
        {
            db = new DataBaseWrapper(this.Resources);
            if ((setting = db.Table <Setting> ().FirstOrDefault()) == null)
            {
                db.Insert(setting = new Setting());
            }
            StartupTheme = setting.Theme;
            SetTheme(setting.Theme);
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.ChatActivity);

            conversation = Intent.GetStringExtra("conversation");

            chat            = db.Get <Chat> (conversation);
            visibleMessages = chat.VisibleMessages;
            if (visibleMessages <= 0)
            {
                visibleMessages = setting.VisibleMessages;
            }
            if (visibleMessages <= 0)
            {
                visibleMessages = 30;
            }
            Title = SmileyTools.GetSmiledTextUTF(chat.name);
            ActionBar.SetDisplayHomeAsUpEnabled(true);
            ActionBar.SetIcon(Resource.Drawable.Icon);

            user = db.Table <User>().FirstOrDefault();

            Button send = FindViewById <Button>(Resource.Id.send);

            send.Click += delegate {
                TextView message = FindViewById <TextView>(Resource.Id.message);
                var      msg     = message.Text;
                message.Text = "";

                if (msg.Equals(""))
                {
                    return;
                }

                LinearLayout messageList = FindViewById <LinearLayout>(Resource.Id.messageLayout);
                AddMessage(messageList, new Message()
                {
                    time = "sending", author = "Du", nick = user.user, text = msg, conversation = this.conversation
                });

                ScrollView scrollView = FindViewById <ScrollView>(Resource.Id.messageScrollView);
                scrollView.FullScroll(FocusSearchDirection.Down);
                scrollView.Post(() => scrollView.FullScroll(FocusSearchDirection.Down));

                OnBind();
                new Thread(async() => {
                    while (!await network.SendMessage(db, user, chat, msg))
                    {
                        await network.Authenticate(db, user);
                    }
                }).Start();
            };
            Button smiley = FindViewById <Button>(Resource.Id.smile);

            smiley.Click += delegate {
                // TODO implement smiley stuff...
            };
        }