Esempio n. 1
0
        public static async Task <TLUserContact> UpdateContactInternalAsync(TLUserContact contact, IFileManager fileManager, ContactStore store, bool updateOrCreate)
        {
            TLUserContact delayedContact = null;
            var           remoteId       = contact.Index.ToString(CultureInfo.InvariantCulture);
            var           phoneContact   = await store.FindContactByRemoteIdAsync(remoteId);

            if (updateOrCreate)
            {
                phoneContact = phoneContact ?? new StoredContact(store);
            }

            if (phoneContact == null)
            {
                return(delayedContact);
            }

            phoneContact.RemoteId   = remoteId;
            phoneContact.GivenName  = contact.FirstName.ToString(); //FirstName
            phoneContact.FamilyName = contact.LastName.ToString();  //LastName

            var userProfilePhoto = contact.Photo as TLUserProfilePhoto;

            if (userProfilePhoto != null)
            {
                var location = userProfilePhoto.PhotoSmall as TLFileLocation;
                if (location != null)
                {
                    var fileName = String.Format("{0}_{1}_{2}.jpg",
                                                 location.VolumeId,
                                                 location.LocalId,
                                                 location.Secret);
                    using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (isoStore.FileExists(fileName))
                        {
                            using (var file = isoStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                            {
                                await phoneContact.SetDisplayPictureAsync(file.AsInputStream());
                            }
                        }
                        else
                        {
                            fileManager.DownloadFile(location, userProfilePhoto, new TLInt(0));
                            delayedContact = contact;
                        }
                    }
                }
            }

            var emptyPhoto = contact.Photo as TLPhotoEmpty;

            if (emptyPhoto != null)
            {
                try
                {
                    await phoneContact.SetDisplayPictureAsync(null);
                }
                catch (Exception ex)
                {
                }
            }

            var props = await phoneContact.GetPropertiesAsync();

            var mobilePhone = contact.Phone.ToString();

            if (mobilePhone.Length > 0)
            {
                props[KnownContactProperties.MobileTelephone] = mobilePhone.StartsWith("+")
                    ? mobilePhone
                    : "+" + mobilePhone;
            }

            var usernameContact = contact as IUserName;

            if (usernameContact != null)
            {
                var username = usernameContact.UserName.ToString();

                if (username.Length > 0)
                {
                    props[KnownContactProperties.Nickname] = username;
                }
            }

            await phoneContact.SaveAsync();

            return(delayedContact);
        }
Esempio n. 2
0
        public static void CreateContactAsync(IFileManager fileManager, IStateService stateService, TLUserContact contact)
        {
#if WP8
            Execute.BeginOnThreadPool(() =>
                                      stateService.GetNotifySettingsAsync(
                                          async settings =>
            {
                if (settings.PeopleHub)
                {
                    var store          = await ContactStore.CreateOrOpenAsync();
                    var delayedContact = await UpdateContactInternalAsync(contact, fileManager, store, true);
                    if (delayedContact != null)
                    {
                        GetDelayedContactsAsync(contacts =>
                        {
                            contacts.Add(delayedContact.Id);
                            SaveDelayedContactsAsync(contacts);
                        });
                    }
                }
            }));
#endif
        }