예제 #1
0
        public static async Task <bool> SendChat(int ReceiverID, String Message)
        {
            JObject  DecodedJson  = null;
            DateTime Now          = DateTime.Now;
            String   ResponseJson = "";

            Dictionary <int, String[]> Data = new Dictionary <int, string[]>
            {
                { 0, new String[] { "sender", Utilities.ID.ToString() } },
                { 1, new String[] { "receiver", ReceiverID.ToString() } },
                { 2, new String[] { "message", Message } },
                { 3, new String[] { "time", Now.ToString("yyyy-MM-dd hh:mm:ss") } },
            };

            try
            {
                ResponseJson = await ChatModel.Chat(Utilities.PostDataEncoder(Data), "chat/");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }


            if (CrossConnectivity.IsSupported && CrossConnectivity.Current.IsConnected)
            {
                DecodedJson = JObject.Parse(ResponseJson);
            }
            else
            {
                DecodedJson = JObject.Parse(@"{ status : false }");

                //store the message in local db for upload when there is internet connection
                //not implemented yet
                await ChatHandler.InsertMessage(Message, Now.ToString("yyyy-MM-dd hh:mm:ss"), true, ReceiverID, MessageType.Text, false, LastServerChatId, Utilities.ID + "-" + ReceiverID);
            }


            return(Convert.ToBoolean(DecodedJson["status"]));
        }
예제 #2
0
        public static async Task <int> GetCountOfNewMessages(int ReceiverId, int LastServerChatID)
        {
            int Counter = 0;

            String ConversationResponseJson = await ChatModel.Chat(Utilities.PostDataEncoder(new Dictionary <int, string[]>
            {
                { 0, new String[] { "sender", Utilities.ID.ToString() } },
                { 1, new String[] { "receiver", ReceiverId.ToString() } },
            }), "chat/conversations");

            var ConversationDecodedJson = JObject.Parse(ConversationResponseJson);

            foreach (var Child in ConversationDecodedJson["conversations"])
            {
                if ((int)Child["id"] > LastServerChatID)
                {
                    Counter++;
                }
            }

            return(Counter);
        }
예제 #3
0
        /// <summary>
        /// LoadConversationToStack Loads the users conversation
        /// </summary>
        /// <param name="Stack"> the parent stack</param>
        /// <param name="RecepientID"> the ID of the receiver</param>
        public static async Task LoadConversationToStack(StackLayout Stack, int RecepientID, bool IsLocalLoaded, Grid ChatControlGrid, int LastID = 0)
        {
            int LastMessageID = LastID;

            JObject ConversationDecodedJson  = new JObject();
            String  ConversationResponseJson = "";

            //if false, then local db is empty else info is loaded from local db
            if (!IsLocalLoaded)
            {
                Device.BeginInvokeOnMainThread(delegate { Stack.Children.Clear(); });

                System.Diagnostics.Debug.WriteLine("IN IF, Old Last Server " + LastServerChatId);

                ConversationResponseJson = await ChatModel.Chat(Utilities.PostDataEncoder(new Dictionary <int, string[]>
                {
                    { 0, new String[] { "sender", Utilities.ID.ToString() } },
                    { 1, new String[] { "receiver", RecepientID.ToString() } },
                }), "chat/conversations");

                ConversationDecodedJson = JObject.Parse(ConversationResponseJson);

                LastMessageID = (int)ConversationDecodedJson["conversations"].Last["id"];

                var LastMessageDate = DateTime.Parse(ConversationDecodedJson["conversations"].Last["time"].ToString());

                if ((DateTime.Now.Day - LastMessageDate.Day) >= 7)///one week since last chat disabled messaging
                {
                    Device.BeginInvokeOnMainThread(delegate
                    {
                        ChatControlGrid.IsVisible = false;
                    });
                }

                await Utilities.RunTask(async delegate
                {
                    foreach (var conversation in ConversationDecodedJson["conversations"])
                    {
                        await ChatHandler.InsertMessage(conversation["message"].ToString(), conversation["time"].ToString(), (bool)conversation["sent"], RecepientID, MessageType.Text, true, (int)conversation["id"], Utilities.ID.ToString() + "-" + RecepientID);

                        if (conversation["message"].ToString() == "{Appointment Accepted, Default Message}")
                        {
                            var ChildStack = new Grid {
                                HorizontalOptions = LayoutOptions.FillAndExpand, Padding = new Thickness(10, 2, 10, 2)
                            };

                            ChildStack.Children.Add(new Label
                            {
                                Text                    = conversation["message"].ToString().Replace(", Default Message", "").Replace("{", "").Replace("}", ""),
                                BackgroundColor         = (Color)App.Current.Resources["_MedAppBlack"],
                                TextColor               = Color.White,
                                HorizontalTextAlignment = TextAlignment.Center,
                                VerticalTextAlignment   = TextAlignment.Center,
                                HeightRequest           = 30,
                                HorizontalOptions       = LayoutOptions.FillAndExpand
                            });

                            Device.BeginInvokeOnMainThread(() =>
                            {
                                Stack.Children.Add(ChildStack);
                                ControlTagger <String> .SetTag(Stack, conversation["id"].ToString());
                            });
                        }
                        else
                        {
                            DateTime MessageTime     = DateTime.Parse(conversation["time"].ToString());
                            String MessageTimeString = "";
                            if (MessageTime.Day != DateTime.Now.Day && MessageTime.Month == DateTime.Now.Month)
                            {
                                MessageTimeString = MessageTime.ToString("");
                            }
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                Stack.Children.Add(SingleMessageTemplate.CreateMessage01((Convert.ToBoolean(conversation["sent"]) ? 1 : 0), conversation["message"].ToString(), Utilities.GetValidatedTime(MessageTime, ChatSectionType.Chat)));
                                ControlTagger <String> .SetTag(Stack, conversation["id"].ToString());
                            });
                        }
                    }
                });
            }

            ///Update chat in Background
            while (ListenForNewConversation)
            {
                ConversationResponseJson = await ChatModel.Chat(Utilities.PostDataEncoder(new Dictionary <int, string[]>
                {
                    { 0, new String[] { "sender", Utilities.ID.ToString() } },
                    { 1, new String[] { "receiver", RecepientID.ToString() } },
                }), "chat/conversations");

                ConversationDecodedJson = JObject.Parse(ConversationResponseJson);

                if (LastMessageID < (int)ConversationDecodedJson["conversations"].Last["id"])
                {
                    await Utilities.RunTask(async delegate
                    {
                        foreach (var conversation in ConversationDecodedJson["conversations"])
                        {
                            if ((int)conversation["id"] > LastMessageID)
                            {
                                LastServerChatId = (int)conversation["id"];

                                ///save updates locally
                                await ChatHandler.InsertMessage(conversation["message"].ToString(), conversation["time"].ToString(), (bool)conversation["sent"], RecepientID, MessageType.Text, true, (int)conversation["id"], Utilities.ID + "-" + RecepientID);

                                if (conversation["message"].ToString() == "{Appointment Accepted, Default Message}")
                                {
                                    var ChildStack = new Grid {
                                        HorizontalOptions = LayoutOptions.FillAndExpand, Padding = new Thickness(10, 2, 10, 2)
                                    };

                                    ChildStack.Children.Add(new Label
                                    {
                                        Text                    = conversation["message"].ToString().Replace(", Default Message", "").Replace("{", "").Replace("}", ""),
                                        BackgroundColor         = (Color)App.Current.Resources["_MedAppBlack"],
                                        TextColor               = Color.White,
                                        HorizontalTextAlignment = TextAlignment.Center,
                                        VerticalTextAlignment   = TextAlignment.Center,
                                        HeightRequest           = 30,
                                        HorizontalOptions       = LayoutOptions.FillAndExpand
                                    });

                                    Device.BeginInvokeOnMainThread(() =>
                                    {
                                        Stack.Children.Add(ChildStack);
                                        ControlTagger <int> .SetTag(Stack, (int)conversation["id"]);
                                    });
                                }
                                else
                                {
                                    DateTime MessageTime     = DateTime.Parse(conversation["time"].ToString());
                                    String MessageTimeString = "";
                                    if (MessageTime.Day != DateTime.Now.Day && MessageTime.Month == DateTime.Now.Month)
                                    {
                                        MessageTimeString = MessageTime.ToString("");
                                    }
                                    Device.BeginInvokeOnMainThread(() =>
                                    {
                                        Stack.Children.Add(SingleMessageTemplate.CreateMessage01((Convert.ToBoolean(conversation["sent"]) ? 1 : 0), conversation["message"].ToString(), Utilities.GetValidatedTime(MessageTime, ChatSectionType.Chat)));
                                        ControlTagger <int> .SetTag(Stack, (int)conversation["id"]);
                                    });
                                }
                            }
                        }
                    });

                    LastMessageID = (int)ConversationDecodedJson["conversations"].Last["id"];
                }
            }
        }
예제 #4
0
        /// <summary>
        /// ChatsListToStack loads all the users conversation onto the specified stacklayout
        /// </summary>
        /// <param name="Stack"></param>
        /// <param name="Con"></param>
        public static async Task ChatsListToStack(StackLayout Stack, ContentPage Con, bool IsLocalLoaded = false)
        {
            if (!IsLocalLoaded)
            {
                Device.BeginInvokeOnMainThread(() => { Stack.Children.Clear(); });
            }

            do
            {
                System.Diagnostics.Debug.WriteLine("Stack Child COunt" + Stack.Children.Count);
                try
                {
                    Dictionary <int, String[]> ChatData = new Dictionary <int, string[]>();

                    Dictionary <int, String[]> Data = new Dictionary <int, string[]>
                    {
                        { 0, new String[] { "sender", Utilities.ID.ToString() } },
                    };

                    String ChatListResponseJson = await ChatModel.Chat(Utilities.PostDataEncoder(Data), "chat/get-list");

                    var ChatListDecodedJson = JObject.Parse(ChatListResponseJson);

                    foreach (var Contact in ChatListDecodedJson["contacts"])
                    {
                        ChatData.Add((int)Contact["info"]["id"], new string[5]);                      // key = receivers id
                        ChatData[(int)Contact["info"]["id"]][0] = Contact["info"]["name"].ToString(); //index 0 = name

                        String ConversationResponseJson = await ChatModel.Chat(Utilities.PostDataEncoder(new Dictionary <int, string[]>
                        {
                            { 0, new String[] { "sender", Utilities.ID.ToString() } },
                            { 1, new String[] { "receiver", Contact["info"]["id"].ToString() } },
                        }), "chat/conversations");

                        //System.Diagnostics.Debug.WriteLine(ConversationResponseJson);

                        var ConversationDecodedJson = JObject.Parse(ConversationResponseJson);

                        //System.Diagnostics.Debug.WriteLine("JSON" + ConversationDecodedJson["conversations"].Last);
                        //System.Diagnostics.Debug.WriteLine("JSON" + ConversationDecodedJson["conversations"].Last["message"]);

                        if (ConversationDecodedJson["conversations"].Last["message"].ToString() == "{Appointment Accepted, Default Message}")
                        {
                            ChatData[(int)Contact["info"]["id"]][1] = ConversationDecodedJson["conversations"].Last["message"].ToString();//index 1 = message
                        }
                        else
                        {
                            ChatData[(int)Contact["info"]["id"]][1]  = ((bool)ConversationDecodedJson["conversations"].Last["sent"] ? "You: " : "");
                            ChatData[(int)Contact["info"]["id"]][1] += ConversationDecodedJson["conversations"].Last["message"].ToString();
                        }

                        //System.Diagnostics.Debug.WriteLine("DICT" + ChatData[(int)Contact["info"]["id"]][1]);


                        ChatData[(int)Contact["info"]["id"]][2] = ConversationDecodedJson["conversations"].Last["time"].ToString(); // index 2 = date

                        try
                        {
                            ChatData[(int)Contact["info"]["id"]][3] = Contact["info"]["medic_id"].ToString(); //index 3 = medic id
                        }
                        catch
                        {
                            ///exception thrown and caught as the current receiver doesnt have a medic id since he/she is a normal user(patient)
                            ChatData[(int)Contact["info"]["id"]][3] = "0";
                        }

                        //last conversation id, index 4 = id of the last message for the current convo on the online db
                        ChatData[(int)Contact["info"]["id"]][4] = ConversationDecodedJson["conversations"].Last["id"].ToString();

                        ///if local receiver doesnt exist, add to local db
                        int Result = await ReceiverHandler.CheckReceiver((int)Contact["info"]["id"]);

                        if (Result == 0)
                        {
                            await ReceiverHandler.NewReceiver(ChatData[(int)Contact["info"]["id"]][0], (int)Contact["info"]["id"], "", Convert.ToInt32(ChatData[(int)Contact["info"]["id"]][3]));
                        }
                    }

                    await Utilities.RunTask(delegate
                    {
                        if (ChatData.Count > 0)
                        {
                            foreach (int key in ChatData.Keys)
                            {
                                if (Stack.Children.Count >= 4)
                                {
                                    continue;
                                }
                                System.Diagnostics.Debug.WriteLine(" Count " + ChatData.Count + " [0] " + ChatData[key][0] + " [1] " + ChatData[key][1] + " [2] " + ChatData[key][2] + " [3] " + ChatData[key][3] + " [4] " + ChatData[key][4]);

                                Device.BeginInvokeOnMainThread(async() =>
                                {
                                    StackLayout ChildStack;

                                    if (Stack.Children.Count > 0 && Stack.Children[0].GetType() == typeof(Label))
                                    {
                                        ///clear message stack if default 'no chat message is shown'
                                        Stack.Children.Clear();
                                    }

                                    if (Stack.Children.Count > 0)
                                    {
                                        ///IsNewConversation is true by default,  the value is changed if the conversation exists on the chat list stack
                                        bool IsNewConversation = true;

                                        for (int i = 0; i < Stack.Children.Count; i++)
                                        {
                                            var child = Stack.Children[i];

                                            //if the child is already on stack, recreate that child if different values
                                            String[] ChildStackTag = ControlTagger <String> .GetTag(child).Split('-');


                                            ///receiver exists in stack
                                            if (key == Convert.ToInt32(ChildStackTag[1]))
                                            {
                                                System.Diagnostics.Debug.WriteLine("Is Old Convo :- False");

                                                int NewMessageCount = await GetCountOfNewMessages(key, Convert.ToInt32(ChildStackTag[2]));

                                                System.Diagnostics.Debug.WriteLine("Child in Stack With Tag [" + ChildStackTag[0] + "-" + ChildStackTag[1] + "-" + ChildStackTag[2] + "-" + ChildStackTag[3] + "] Tag [" + Utilities.ID + "-" + key + "-" + ChatData[key][4] + "-" + NewMessageCount + "] Last Server ID " + Convert.ToInt32(ChatData[key][4]));

                                                //receiver
                                                if ((Convert.ToInt32(ChildStackTag[2]) < Convert.ToInt32(ChatData[key][4])))
                                                {
                                                    /// create notification to show mew message
                                                    List <Object> ListForNotification = new List <object>();
                                                    if (ChatData[key][1].Equals("{Appointment Accepted, Default Message}"))
                                                    {
                                                        ListForNotification.Add(ChatData[key][1].Replace(", Default Message", "").Replace("{", "").Replace("}", ""));//index 0 message
                                                    }
                                                    else
                                                    {
                                                        //ListForNotification.Add(ChatData[key][1]);//index 0 message
                                                        ListForNotification.Add("" + NewMessageCount + " new messages from " + ChatData[key][0]);//index 0 message
                                                    }

                                                    ListForNotification.Add(ChatData[key][2]);      //index 1 time
                                                    ListForNotification.Add(NotificationType.Chat); //index 2 notificationtype
                                                    ListForNotification.Add(key);                   // index 3 id


                                                    NotificationPage.NotificationData.Add(NotificationPage.NotificationData.Count, ListForNotification);

                                                    Stack.Children.RemoveAt(i);

                                                    ChildStack = ListAllChatTemplate.CreateNewStackType01(ChatData[key][0], "", ChatData[key][1], Utilities.GetValidatedTime(DateTime.Parse(ChatData[key][2]), ChatSectionType.ChatList), Con, key, NewMessageCount, Convert.ToInt32(ChatData[key][3]));
                                                    //ChildStack = new StackLayout { BackgroundColor = Color.Tomato };
                                                    ControlTagger <String> .SetTag(ChildStack, Utilities.ID + "-" + key + "-" + ChatData[key][4] + "-" + NewMessageCount);

                                                    //Stack.Children.Add(ChildStack);
                                                    Stack.Children.Insert(0, ChildStack);
                                                }


                                                ///IsNewConversation is false as the current key exists in the chat list stack
                                                IsNewConversation = false;
                                            }
                                        }
                                        System.Diagnostics.Debug.WriteLine("Done, Status :- " + IsNewConversation);
                                        ///If IsNewConversation is true, this key is a new conversaton, create new childstack
                                        if (IsNewConversation)
                                        {
                                            ///{Appointment Accepted, Default Message} is the default sent message, create different view for it
                                            if (ChatData[key][1].Equals("{Appointment Accepted, Default Message}"))
                                            {
                                                ChatData[key][1] = ChatData[key][1].Replace(", Default Message", "").Replace("{", "").Replace("}", "");
                                                ChildStack       = ListAllChatTemplate.CreateNewStackType01(ChatData[key][0], "", ChatData[key][1], Utilities.GetValidatedTime(DateTime.Parse(ChatData[key][2]), ChatSectionType.ChatList), Con, key, 0, Convert.ToInt32(ChatData[key][3]));
                                                ControlTagger <String> .SetTag(ChildStack, Utilities.ID + "-" + key + "-" + ChatData[key][4] + "-" + 0);
                                                Stack.Children.Insert(Stack.Children.Count, ChildStack);
                                            }
                                            else
                                            {
                                                ChildStack = ListAllChatTemplate.CreateNewStackType01(ChatData[key][0], "", ChatData[key][1], Utilities.GetValidatedTime(DateTime.Parse(ChatData[key][2]), ChatSectionType.ChatList), Con, key, 0, Convert.ToInt32(ChatData[key][3]));
                                                ControlTagger <String> .SetTag(ChildStack, Utilities.ID + "-" + key + "-" + ChatData[key][4] + "-" + 0);
                                                Stack.Children.Insert(Stack.Children.Count, ChildStack);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ///{Appointment Accepted, Default Message} is the default sent message, create different view for it
                                        if (ChatData[key][1].Equals("{Appointment Accepted, Default Message}"))
                                        {
                                            /// create notification to show the appointment has been accepted
                                            List <Object> ListForNotification = new List <object>();
                                            ListForNotification.Add(ChatData[key][1].Replace(", Default Message", "").Replace("{", "").Replace("}", "")); //index 0 message
                                            ListForNotification.Add(ChatData[key][2]);                                                                    //index 1 time
                                            ListForNotification.Add(NotificationType.Appointment);                                                        //index 2 notificationtype
                                            ListForNotification.Add(key);                                                                                 // index 3 id


                                            NotificationPage.NotificationData.Add(NotificationPage.NotificationData.Count, ListForNotification);

                                            System.Diagnostics.Debug.WriteLine("Adding");

                                            ChatData[key][1] = ChatData[key][1].Replace(", Default Message", "").Replace("{", "").Replace("}", "");
                                            ChildStack       = ListAllChatTemplate.CreateNewStackType01(ChatData[key][0], "", ChatData[key][1], Utilities.GetValidatedTime(DateTime.Parse(ChatData[key][2]), ChatSectionType.ChatList), Con, key, 0, Convert.ToInt32(ChatData[key][3]));
                                            ControlTagger <String> .SetTag(ChildStack, Utilities.ID + "-" + key + "-" + ChatData[key][4] + "-" + 0);
                                            Stack.Children.Insert(Stack.Children.Count, ChildStack);
                                        }
                                        else
                                        {
                                            ChildStack = ListAllChatTemplate.CreateNewStackType01(ChatData[key][0], "", ChatData[key][1], Utilities.GetValidatedTime(DateTime.Parse(ChatData[key][2]), ChatSectionType.ChatList), Con, key, 0, Convert.ToInt32(ChatData[key][3]));
                                            ControlTagger <String> .SetTag(ChildStack, Utilities.ID + "-" + key + "-" + ChatData[key][4] + "-" + 0);
                                            Stack.Children.Insert(Stack.Children.Count, ChildStack);
                                        }
                                    }
                                });
                            }
                        }
                        else
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                if (Stack.Children.Count == 0)
                                {
                                    Stack.Children.Add(new Label {
                                        Text = (!Utilities.IsMedic) ? "Chats will appear after booking an appointment." : "Chats will appear after accepting an appointment.", Style = App.Current.Resources["_EmptyLabelTemplate"] as Style
                                    });
                                }
                            });
                        }
                    });
                }
                catch (Exception ex)
                {
                    await App.Current.MainPage.DisplayAlert("Alert", ex.StackTrace, "Ok");
                }
            } while (ListenForChatListUpdate);
        }