private ObservableCollection <AttachmentViewModel> _GetAttachmentsViewModel(Message message) { Debug.Assert(message != null); var items = new ObservableCollection <AttachmentViewModel>(); try { if (message.GeoLocation != null) { //NumberFormatInfo provider = new NumberFormatInfo();provider. string uri = String.Format(AppResources.GeolocationMapUriFormatMessage, message.GeoLocation.Latitude.ToString("0.00", CultureInfo.InvariantCulture), message.GeoLocation.Longitude.ToString("0.00", CultureInfo.InvariantCulture)); var avm = new AttachmentViewModel(message.Mid, -1, -1, AttachmentType.Location, uri, AppResources.LocContentLabel, _defaultAvatar, null, null); // Try to load image from cache. string filename = CommonHelper.DoDigest(avm.Uri); BitmapImage image = _imageCache.GetItem(filename); if (image != null) { avm.AttachPhoto = image; } else // ...if it doesn't exists - load from web. { _photosToLoad.Add(avm, new AvatarLoadItem(_photosToLoad.Count, uri, filename)); } items.Add(avm); } if (message.FwdMessages != null && message.FwdMessages.Any()) { items.Add(new AttachmentViewModel(message.Mid, -1, -1, AttachmentType.ForwardMessage, null, "", null, _GetMessagesViewModel(message.FwdMessages), null)); } if (message.Attachments != null) { foreach (var attach in message.Attachments) { AttachmentViewModel avm = null; if (attach.DocAttachment != null) // Todo. Use special document photo here? { items.Add(new AttachmentViewModel(message.Mid, attach.DocAttachment.OwnerId, attach.DocAttachment.Did, AttachmentType.Document, attach.DocAttachment.Url, AppResources.DocumentContentLabel, _defaultAvatar, null, null)); } else if (attach.VideoAttachment != null) { avm = new AttachmentViewModel(message.Mid, attach.VideoAttachment.OwnerId, attach.VideoAttachment.Vid, AttachmentType.Video, attach.VideoAttachment.Image, AppResources.VideoContentLabel, _defaultAvatar, null, null, attach.VideoAttachment.Title, string.Empty, attach.VideoAttachment.Duration); } else if (attach.AudioAttachment != null) { avm = new AttachmentViewModel(message.Mid, attach.AudioAttachment.OwnerId, attach.AudioAttachment.Aid, AttachmentType.Audio, attach.AudioAttachment.Url, AppResources.AudioContentLabel, null, null, null, attach.AudioAttachment.Title, attach.AudioAttachment.Author, attach.AudioAttachment.Duration); } else if (attach.PhotoAttachment != null) { avm = new AttachmentViewModel(message.Mid, attach.PhotoAttachment.OwnerId, attach.PhotoAttachment.Pid, AttachmentType.Photo, attach.PhotoAttachment.SourceBig, AppResources.PhotoContentLabel, _defaultAvatar, null, null); } else { Debug.Assert(false); } if (avm != null) { if (avm.Type != AttachmentType.Audio) { // Try to load image from cache. string filename = CommonHelper.DoDigest(avm.Uri); BitmapImage image = _imageCache.GetItem(filename); if (image != null) { avm.AttachPhoto = image; } else // ...if it doesn't exists - load from web. { _photosToLoad.Add(avm, new AvatarLoadItem(_photosToLoad.Count, avm.Uri, filename)); } } // Add to current items items.Add(avm); } } } } catch (Exception ex) { Debug.WriteLine("_GetAttachmentsViewModel failed: " + ex.Message); } return(items); }
private void PlayAudio_Tap(object sender, EventArgs e) { Button cur = sender as Button; if (cur != null) { AttachmentViewModel currentTrack = cur.DataContext as AttachmentViewModel; if (currentTrack != null) { if (AudioPlayer.CurrentState == MediaElementState.Playing) { AudioPlayer.Stop(); if (_currentTrack.Mid == currentTrack.Mid) { Image content = cur.Content as Image; if (null != content) content.Source = ResourceHelper.GetBitmap(@"/SlXnaApp1;component/Images/Play.png"); _currentPlayButton = null; _currentTrack = null; } else // Handle the case when user don't stop current audio, but start another one... { Image content = cur.Content as Image; if (null != content) content.Source = ResourceHelper.GetBitmap(@"/SlXnaApp1;component/Images/Pause.png"); Image contentOld = _currentPlayButton.Content as Image; if (null != content) contentOld.Source = ResourceHelper.GetBitmap(@"/SlXnaApp1;component/Images/Play.png"); _currentTrack = currentTrack; AudioPlayer.Source = new Uri(_currentTrack.Uri, UriKind.Absolute); //_currentTrack.Progress = 0; AudioPlayer.Play(); _currentPlayButton = cur; } } else { Image content = cur.Content as Image; if (null != content) content.Source = ResourceHelper.GetBitmap(@"/SlXnaApp1;component/Images/Pause.png"); _currentTrack = currentTrack; AudioPlayer.Source = new Uri(_currentTrack.Uri, UriKind.Absolute); //_currentTrack.Progress = 0; AudioPlayer.Play(); _currentPlayButton = cur; } } } }
private ObservableCollection<AttachmentViewModel> _GetAttachmentsViewModel(Message message) { Debug.Assert(message != null); var items = new ObservableCollection<AttachmentViewModel>(); try { if (message.GeoLocation != null) { //NumberFormatInfo provider = new NumberFormatInfo();provider. string uri = String.Format(AppResources.GeolocationMapUriFormatMessage, message.GeoLocation.Latitude.ToString("0.00", CultureInfo.InvariantCulture), message.GeoLocation.Longitude.ToString("0.00", CultureInfo.InvariantCulture)); var avm = new AttachmentViewModel(message.Mid, -1, -1, AttachmentType.Location, uri, AppResources.LocContentLabel, _defaultAvatar, null, null); // Try to load image from cache. string filename = CommonHelper.DoDigest(avm.Uri); BitmapImage image = _imageCache.GetItem(filename); if (image != null) avm.AttachPhoto = image; else // ...if it doesn't exists - load from web. _photosToLoad.Add(avm, new AvatarLoadItem(_photosToLoad.Count, uri, filename)); items.Add(avm); } if (message.FwdMessages != null && message.FwdMessages.Any()) items.Add(new AttachmentViewModel(message.Mid, -1, -1, AttachmentType.ForwardMessage, null, "", null, _GetMessagesViewModel(message.FwdMessages), null)); if (message.Attachments != null) { foreach (var attach in message.Attachments) { AttachmentViewModel avm = null; if (attach.DocAttachment != null) // Todo. Use special document photo here? { items.Add(new AttachmentViewModel(message.Mid, attach.DocAttachment.OwnerId, attach.DocAttachment.Did, AttachmentType.Document, attach.DocAttachment.Url, AppResources.DocumentContentLabel, _defaultAvatar, null, null)); } else if (attach.VideoAttachment != null) { avm = new AttachmentViewModel(message.Mid, attach.VideoAttachment.OwnerId, attach.VideoAttachment.Vid, AttachmentType.Video, attach.VideoAttachment.Image, AppResources.VideoContentLabel, _defaultAvatar, null, null, attach.VideoAttachment.Title, string.Empty, attach.VideoAttachment.Duration); } else if (attach.AudioAttachment != null) { avm = new AttachmentViewModel(message.Mid, attach.AudioAttachment.OwnerId, attach.AudioAttachment.Aid, AttachmentType.Audio, attach.AudioAttachment.Url, AppResources.AudioContentLabel, null, null, null, attach.AudioAttachment.Title, attach.AudioAttachment.Author, attach.AudioAttachment.Duration); } else if (attach.PhotoAttachment != null) { avm = new AttachmentViewModel(message.Mid, attach.PhotoAttachment.OwnerId, attach.PhotoAttachment.Pid, AttachmentType.Photo, attach.PhotoAttachment.SourceBig, AppResources.PhotoContentLabel, _defaultAvatar, null, null); } else Debug.Assert(false); if (avm != null) { if (avm.Type != AttachmentType.Audio) { // Try to load image from cache. string filename = CommonHelper.DoDigest(avm.Uri); BitmapImage image = _imageCache.GetItem(filename); if (image != null) avm.AttachPhoto = image; else // ...if it doesn't exists - load from web. _photosToLoad.Add(avm, new AvatarLoadItem(_photosToLoad.Count, avm.Uri, filename)); } // Add to current items items.Add(avm); } } } } catch (Exception ex) { Debug.WriteLine("_GetAttachmentsViewModel failed: " + ex.Message); } return items; }
private void Messages_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (AudioPlayer.CurrentState == MediaElementState.Playing) { AudioPlayer.Stop(); _currentTrack = null; } if (!App.Current.EntityService.IsLoadingMessagesHistory) _ScrollIntoView(); }
private bool _AddLocationAttachment() { if (!string.IsNullOrEmpty(App.Current.EntityService.AttachedLatitude) && !string.IsNullOrEmpty(App.Current.EntityService.AttachedLongitude)) { try { string uri = String.Format(AppResources.GeolocationMapUriFormatMessage, App.Current.EntityService.AttachedLatitude.Replace(",", "."), App.Current.EntityService.AttachedLongitude.Replace(",", ".")); var avm = new AttachmentViewModel(-1, -1, -1, AttachmentType.Location, uri, null, null, null, null); _model.Add(avm); string filename = CommonHelper.DoDigest(uri); var photosToLoad = new List<AvatarLoadItem>(); photosToLoad.Add(new AvatarLoadItem(1, uri, filename)); var service = new AsyncAvatarsLoader(); if (photosToLoad.Any()) service.LoadAvatars(photosToLoad.ToList(), id => { Deployment.Current.Dispatcher.BeginInvoke(() => { try { ImageCache cache = new ImageCache(); BitmapImage image = cache.GetItem(filename); if (image != null && image.PixelHeight > 0 && image.PixelWidth > 0) { avm.AttachPhoto = image; } } catch (Exception ex) { Debug.WriteLine("_AddLocationAttachment failed in AttachmentsPage: " + ex.Message); } }); }, null); } catch (Exception ex) { Debug.WriteLine("Load attached location photo failed " + ex.Message); } return true; } return false; }