public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
 {
     App.Initialize();
     if (!NSUserDefaults.StandardUserDefaults.BoolForKey("DBCreated"))
     {
         ChatterDatabase.CreateDatabase();
     }
     Console.WriteLine("Path: " + ChatterDatabase.DatabasePath());
     return(true);
 }
        private void ManageMessages()
        {
            var conn  = new SQLiteConnection(ChatterDatabase.DatabasePath());
            var query = conn.Query <ChatUserModel>("select * from ChatUserModel");

            if (messages == null)
            {
                messages = new List <Message>();
            }

            foreach (var chatMessage in query)
            {
                if (!string.IsNullOrEmpty(chatMessage.Text))
                {
                    messages.Add(new Message(chatMessage.Id, chatMessage.DisplayName, DateExtention.DateTimeToNSDate(chatMessage.ChatDate), chatMessage.Text));
                }
            }
        }
        void ClientMqttMsgReceived(object sender, MqttMsgPublishEventArgs e)
        {
            UIApplication.SharedApplication.InvokeOnMainThread(async() =>
            {
                if (e.Message != null && !string.IsNullOrEmpty(Encoding.UTF8.GetString(e.Message)))
                {
                    ShowTypingIndicator = true;

                    ScrollToBottom(true);

                    var delay = Task.Delay(1500);

                    var message = new Message(friend.Id, friend.DisplayName, NSDate.Now, Encoding.UTF8.GetString(e.Message));
                    if (message != null)
                    {
                        messages.Add(message);
                    }

                    await delay;

                    ScrollToBottom(true);

                    SystemSoundPlayer.PlayMessageReceivedSound();

                    FinishReceivingMessage(true);

                    if (message != null)
                    {
                        var conn = new SQLiteConnection(ChatterDatabase.DatabasePath());
                        var s    = conn.Insert(new ChatUserModel()
                        {
                            Id          = message.SenderId,
                            DisplayName = friend.DisplayName,
                            ChatDate    = DateTime.Now,
                            Text        = Encoding.UTF8.GetString(e.Message)
                        });
                    }
                }
            });
        }
        public override async void PressedSendButton(UIButton button, string text, string senderId, string senderDisplayName, NSDate date)
        {
            if (!Reachability.IsNetworkAvailable())
            {
                new UIAlertView("Alert", "Please check your internet connection!", null, "Ok", null).Show();
                return;
            }

            SystemSoundPlayer.PlayMessageSentSound();

            if (client == null)
            {
                CreateMqttConnection();
            }

            client.Publish(TOPIC_PUBLISH, Encoding.UTF8.GetBytes(text), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);

            var message = new Message(SenderId, SenderDisplayName, NSDate.Now, text);

            if (message != null)
            {
                messages.Add(message);

                var conn = new SQLiteConnection(ChatterDatabase.DatabasePath());
                var s    = conn.Insert(new ChatUserModel()
                {
                    Id          = SenderId,
                    DisplayName = SenderDisplayName,
                    ChatDate    = DateTime.Now,
                    Text        = text
                });
            }

            FinishSendingMessage(true);

            await Task.Delay(500);
        }
        protected void FinishedPickingImageFromMedia(object sender, UIImagePickerMediaPickedEventArgs e)
        {
            var galleryController = sender as UIImagePickerController;

            bool isImage = false;

            switch (e.Info[UIImagePickerController.MediaType].ToString())
            {
            case "public.image":
                isImage = true;
                break;

            case "public.video":
                break;
            }

            if (isImage)
            {
                var image = e.Info[UIImagePickerController.OriginalImage] as UIImage;
                if (image != null)
                {
                    string imageName = null;
                    string extension = null;

                    var refUrl = e.Info[UIImagePickerController.ReferenceUrl] as NSUrl;
                    if (refUrl != null)
                    {
                        var assetsLibrary = new ALAssetsLibrary();
                        assetsLibrary.AssetForUrl(refUrl,
                                                  async resultBlock =>
                        {
                            var imageRepresentation = resultBlock.DefaultRepresentation;
                            imageName = imageRepresentation.Filename;
                            imageName = imageName.ToLower();
                            extension = imageName.Split('.')[1];

                            var imageData = image.AsJPEG(0.3f);

                            var photoMediaItem = new PhotoMediaItem(image);


                            if (!Reachability.IsNetworkAvailable())
                            {
                                new UIAlertView("Alert", "Please check your internet connection!", null, "Ok", null).Show();
                                return;
                            }

                            SystemSoundPlayer.PlayMessageSentSound();

                            if (client == null)
                            {
                                CreateMqttConnection();
                            }

                            client.Publish(TOPIC_PUBLISH, imageData.ToArray(), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);

                            var message1 = new Message(SenderId, SenderDisplayName, NSDate.Now, photoMediaItem);
                            if (message1 != null)
                            {
                                messages.Add(message1);

                                var conn = new SQLiteConnection(ChatterDatabase.DatabasePath());
                                var s    = conn.Insert(new ChatUserModel()
                                {
                                    Id          = SenderId,
                                    DisplayName = SenderDisplayName,
                                    ChatDate    = DateTime.Now,
                                    Text        = imageName
                                });
                            }

                            FinishSendingMessage(true);

                            await Task.Delay(500);


                            var message2 = new Message(friend.Id, friend.DisplayName, NSDate.Now, "Hello, Ramakant!!! your State: Maharashtra, Town: Sangali, Fruit: Mango, Current Rate: 150/Kg, Logistics: Rajeev Transport 400 per trip");
                            if (message2 != null)
                            {
                                messages.Add(message2);

                                var conn = new SQLiteConnection(ChatterDatabase.DatabasePath());
                                var s    = conn.Insert(new ChatUserModel()
                                {
                                    Id          = friend.Id,
                                    DisplayName = friend.DisplayName,
                                    ChatDate    = DateTime.Now,
                                    Text        = "Hello, Ramakant!!! your State: Maharashtra, Town: Sangali, Fruit: Mango, Current Rate: 150/Kg, Logistics: Rajeev Transport 400 per trip"
                                });
                            }

                            FinishSendingMessage(true);

                            await Task.Delay(500);
                        },
                                                  failureBlock =>
                        {
                        });
                    }
                    else
                    {
                    }
                }
            }

            galleryController.DismissViewController(true, null);
        }