Example #1
0
        public FeedbackFlyoutVM()
        {
            RefreshCommand = new RelayCommand(() =>
            {
                this.IsBusy = true;
                var tasks   = FeedbackThreadList.Where(t => !t.FeedbackThread.IsNewThread)
                              .Select(t => FeedbackManager.Current.RefreshFeedbackThreadVMAsync(SelectedFeedbackThread));
                Task.WaitAll(tasks.ToArray(), 20);
                this.IsBusy = false;
            });

            AddThreadCommand = new RelayCommand(() =>
            {
                var threadVM = new FeedbackThreadVM();
                this.FeedbackThreadList.Add(threadVM);
                FeedbackManager.Current.AddFeedbackThread(threadVM);
                this.SelectedFeedbackThread = threadVM;
            });

            CloseThreadCommand = new RelayCommand(async() =>
            {
                var msg = this.SelectedFeedbackThread.IsNewThread ?
                          LocalizedStrings.LocalizedResources.CloseNewThreadQuestion
                    : LocalizedStrings.LocalizedResources.CloseActiveThreadQuestion;
                var dialog = new MessageDialog(msg);
                dialog.Commands.Add(new UICommand()
                {
                    Id = true, Label = LocalizedStrings.LocalizedResources.Yes
                });
                dialog.Commands.Add(new UICommand()
                {
                    Id = false, Label = LocalizedStrings.LocalizedResources.No
                });
                var result = await dialog.ShowAsync();
                FeedbackFlyoutVM.ShowFlyout();
                if ((bool)result.Id)
                {
                    if (this.SelectedFeedbackThread.IsNewThread)
                    {
                        FeedbackManager.Current.SaveFeedbackThreadTokens();
                    }
                    this.FeedbackThreadList.Remove(this.SelectedFeedbackThread);
                    if (!this.FeedbackThreadList.Any())
                    {
                        this.FeedbackThreadList.Add(new FeedbackThreadVM());
                    }
                    this.SelectedFeedbackThread = this.FeedbackThreadList.First();
                }
            });
        }
        private void SetCommands()
        {
            AddAttachmentCommand = new RelayCommand(async() =>
            {
                var picker = new FileOpenPicker();

                picker.ViewMode = PickerViewMode.List;
                picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                picker.FileTypeFilter.Add("*");

                var files = await picker.PickMultipleFilesAsync();
                foreach (var file in files)
                {
                    byte[] bytes = null;
                    using (var stream = await file.OpenReadAsync()) {
                        IBuffer buffer = WindowsRuntimeBuffer.Create((int)stream.Size);
                        await stream.ReadAsync(buffer, (uint)stream.Size, InputStreamOptions.None);
                        bytes = buffer.ToArray();
                    }
                    var attach = new FeedbackAttachment(file.Name, bytes, file.ContentType);
                    this.Attachments.Add(new FeedbackAttachmentVM(attach)
                    {
                        FeedbackMessageVM = this
                    });
                }
            });

            OpenAttachmentCommand = new RelayCommand(async(o) =>
            {
                await((FeedbackAttachmentVM)o).OpenAttachmentAsync();
            });

            SendMessageCommand = new RelayCommand(async(o) =>
            {
                bool success = true;
                if (await ValidateInputAsync())
                {
                    if (NetworkInterface.GetIsNetworkAvailable())
                    {
                        this.IsBusy = true;
                        try
                        {
                            IFeedbackMessage sentMessage = await this.SendFeedbackAsync();
                            this.FeedbackThreadVM.HandleSentMessage(sentMessage);
                        }
                        catch (Exception e)
                        {
                            HockeyClient.Current.AsInternal().HandleInternalUnhandledException(e);
                            success = false;
                        }
                        finally
                        {
                            this.IsBusy = false;
                        }
                        if (!success)
                        {
                            await new MessageDialog(LocalizedStrings.LocalizedResources.FeedbackSendError).ShowAsync();
                            FeedbackFlyoutVM.ShowFlyout();
                        }
                    }
                    else
                    {
                        await new MessageDialog(LocalizedStrings.LocalizedResources.FeedbackNoInternet).ShowAsync();
                        FeedbackFlyoutVM.ShowFlyout();
                    }
                }
                else
                {
                    FeedbackFlyoutVM.ShowFlyout();
                }
            });

            CancelMessageCommand = new RelayCommand((o) => {
                this.Message = null;
                this.Attachments.Clear();
            });

            if (!String.IsNullOrWhiteSpace(this.Email))
            {
                Task t = this.ReLoadGravatar();
            }
            else
            {
                this.Gravatar = GravatarHelper.DefaultGravatar;
            }
        }