Пример #1
0
        //public ChatData CreateNewChat(AddressBook mycon)
        //{   string[] separators = { "("," "};
        //    string[] ph_num= new string[2];
        //    int num;
        //    foreach (ChatData c in Items)
        //    {
        //        if (c.ContactName == mycon.DisplayName)
        //            return c;
        //    }
        //    foreach(ContactPhoneNumber ph in mycon.PhoneNumbers)
        //    {   //To Note Only the first number of multi-phone number contact will be used
        //        ph_num = ph.ToString().Split(separators, StringSplitOptions.RemoveEmptyEntries);
        //        if (ph_num[0].Length >= 10)
        //        {
        //            num = ph_num[0].Length - 10;
        //            ph_num[0] = ph_num[0].Substring(num);
        //        }
        //        break;
        //    }
        //      ChatData result =
        //    new ChatData()
        //    {
        //        ID = (Items.Count).ToString(),
        //        ContactName = mycon.DisplayName,
        //        PhoneNumber = ph_num[0]
        //    };

        //    this.Items.Add(result);
        //    return result;
        //}

        public ChatData CreateNewChat(AddressBook mycon, int i)
        {
            if (i == 0)
            {
                foreach (ChatData c in Items)
                {
                    if (c.ContactName == mycon.Name)
                    {
                        return(c);
                    }
                }
            }
            else if (i == 1)
            {
                foreach (ChatData d in UI2Items)
                {
                    if (d.ContactName == mycon.Name)
                    {
                        return(d);
                    }
                }
                ChatData result =
                    new ChatData()
                {
                    ID          = (Items.Count).ToString(),
                    ContactName = mycon.Name,
                    PhoneNumber = mycon.Phone
                };
                this.UI2Items.Add(result);
                this.Items.Add(result);
                return(result);
            }
            ChatData res =
                new ChatData()
            {
                ID          = (Items.Count).ToString(),
                ContactName = mycon.Name,
                PhoneNumber = mycon.Phone
            };

            this.Items.Add(res);
            return(res);
        }
Пример #2
0
        public ChatData CreateNewChat(Favourites mycon)
        {
            foreach (ChatData c in Items)
            {
                if (c.ContactName == mycon.Name)
                {
                    return(c);
                }
            }

            ChatData result =
                new ChatData()
            {
                ID          = (Items.Count).ToString(),
                ContactName = mycon.Name,
                PhoneNumber = mycon.Favouritenumber
            };

            this.Items.Add(result);
            return(result);
        }
Пример #3
0
        public async Task LoadData()
        {
            //string sentby="";
            //bool found = false;
            //chatsTable = App.MobileService.GetTable<SendData>();
            //MobileServiceCollection<SendData, SendData> store;
            //store = await chatsTable
            //        .Where(x => x.Read == false)
            //        .ToCollectionAsync();
            //foreach(var msg in store)
            //{

            //    foreach(var item in Items )
            //    {
            //        if(item.PhoneNumber.ToString() == msg.Sender)
            //        {
            //            found = true;
            //            ChatPage.index = Convert.ToInt16(item.ID);
            //            sentby = item.ContactName;
            //        }
            //    }
            //    if (found == false)
            //        sentby = msg.Sender;
            //    Message message = new Message()
            //    {
            //        Sender = sentby,
            //        Text = msg.Message,
            //        SendingTime = Convert.ToDateTime(msg.Time)
            //    };
            //    Binder.Instance.Messages.Add(message);
            //    App.ViewModel.Items[ChatPage.index].FirstLine = msg.Message;
            //}

            bool error = false;

            //this.Items = new ObservableCollection<ChatData>();
            StorageFolder appStorageFolder = IORecipes.GetAppStorageFolder();
            StorageFile   dataFile         = await IORecipes.GetFileInFolder(appStorageFolder, dataFileName);

            if (dataFile != null)
            {
                if (!IsDataLoaded)
                {
                    string itemsAsXML = await IORecipes.ReadStringFromFile(dataFile);

                    this.Items = IORecipes.SerializeFromString <ObservableCollection <ChatData> >(itemsAsXML);
                }
            }
            // Sample data; replace with real data
            else
            {
                if (!IsDataLoaded)
                {
                    Items = CreateSampleItems();
                }
            }
            //await ConnectToWindowsAzure();
            if (AzureConnected)
            {
                createfav();

                //Sync();
                TableQuery <SendData>        query        = new TableQuery <SendData>().Where("PartitionKey eq 'default' and Read eq 'false'");
                TableQuerySegment <SendData> querySegment = null;
                var resultlist = new List <SendData>();

                try
                {
                    while (querySegment == null || querySegment.ContinuationToken != null)
                    {
                        querySegment = await table.ExecuteQuerySegmentedAsync(query, querySegment != null?querySegment.ContinuationToken : null);

                        resultlist.AddRange(querySegment);
                    }
                }
                catch
                {
                    error = true;
                }
                if (error)
                {
                }
                foreach (SendData s in resultlist)
                {
                    bool    found = false;
                    Message temp  = new Message();
                    temp.Id = s.RowKey;

                    temp.Text        = s.Message;
                    temp.SendingTime = s.Time;
                    foreach (ChatData i in Items)
                    {
                        if (i.PhoneNumber.Equals(s.From))
                        {
                            found          = true;
                            ChatPage.index = Convert.ToInt16(i.ID);
                        }
                    }
                    if (!found)
                    {
                        string[] separators = { "(", " " };
                        string[] ph_num     = new string[2];
                        int      num;

                        foreach (Contact c in PhoneContacts)
                        {
                            foreach (ContactPhoneNumber ph in c.PhoneNumbers)
                            {
                                ph_num = ph.ToString().Split(separators, StringSplitOptions.RemoveEmptyEntries);
                                if (ph_num[0].Length >= 10)
                                {
                                    num       = ph_num[0].Length - 10;
                                    ph_num[0] = ph_num[0].Substring(num);

                                    if (ph_num[0] == s.From)
                                    {
                                        AddressBook add = new AddressBook(c.DisplayName, ph_num[0]);
                                        CreateNewChat(add, 0);
                                        temp.Sender    = c.DisplayName;
                                        ChatPage.index = Items.Count - 1;
                                        found          = true;
                                        break;
                                    }
                                }
                            }
                            if (found == true)
                            {
                                break;
                            }
                        }
                        if (!found)
                        {
                            ChatData result =
                                new ChatData()
                            {
                                ID          = (Items.Count).ToString(),
                                ContactName = s.From,
                                PhoneNumber = s.From
                            };
                            this.Items.Add(result);
                            ChatPage.index = Items.Count - 1;
                        }
                    }

                    Binder.Instance.Messages.Add(temp);
                    App.ViewModel.Items[ChatPage.index].FirstLine = s.Message;
                    s.Read = true;
                    await UpdateChat(s);
                }
                if (resultlist.Count > 0)
                {
                    await App.ViewModel.UpdateChats();
                }
            }

            NotifyPropertyChanged("Items");
            this.IsDataLoaded = true;
        }
Пример #4
0
 public void RemoveChat(ChatData ChatToRemove)
 {
     this.Items.Remove(ChatToRemove);
     NotifyPropertyChanged("Items");
 }