示例#1
0
        public async void LoadImageAsync(object?sender, EventArgs e)
        {
            if (sender is Image image)
            {
                try
                {
                    image.Source = null;

                    if (image.DataContext is User user &&
                        image.Tag is string which)
                    {
                        CollapseHeightIfNoBanner(user);

                        var uri = which switch
                        {
                            "profile" => user.ProfileImageUrlBigger,
                            "banner" => user.ProfileBannerUrlSmall,
                            _ => null
                        };

                        if (uri.IsNotNullOrWhiteSpace())
                        {
                            image.Source = await ImageService.GetImageAsync(uri !).ConfigureAwait(true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceService.Message(ex.Message);
                }
            }
        }
        private async void UserProfileTimeline_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (TimelineView.DataContext is UserProfileTimelineViewModel vm)
                {
                    vm.StatusCollection.Clear();

                    if (DataContext is User user)
                    {
                        await Task.Delay(100).ConfigureAwait(true);

                        try
                        {
                            var statuses = await vm.GetUserTimeline(user.ScreenName !).ConfigureAwait(true);

                            foreach (var status in statuses.OrderByDescending(status => status.OriginatingStatus.CreatedDate))
                            {
                                vm.StatusCollection.Add(status);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceService.Message(ex.Message);
            }
        }
        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs ea)
        {
            try
            {
                ea.Handled = true;
                var image = (Image)sender;

                if (Retries < MaxRetries)
                {
                    Retries++;
                    var uri = new Uri(((Media)image.DataContext).MediaUrl);

                    var bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.IgnoreColorProfile;
                    bitmap.UriSource     = uri;
                    bitmap.EndInit();

                    image.Source = bitmap;
                    TraceService.Message($"Retry ({Retries}) loading image: {uri}");
                }
                else
                {
                    var loadingIndicator = (TextBlock)image.Tag;
                    loadingIndicator.ToolTip = ea.ErrorException.Message;
                    loadingIndicator.Text    = (string)Application.Current.FindResource("WarningSign");
                    TraceService.Message(ea.ErrorException.Message);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                TraceService.Message(ex.Message);
            }
        }
示例#4
0
        private async void OpenInViewer(object?sender, PointerPressedEventArgs e)
        {
            try
            {
                if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed&&
                    sender is Grid grid &&
                    grid.Children[0] is Image image)
                {
                    e.Handled = true;

                    if ((e.KeyModifiers & KeyModifiers.Control) != 0)
                    {
                        var mediaUrl = (image.DataContext as Media)?.MediaUrl;
                        App.Commands.AddToHiddenImages.Execute((mediaUrl, this.PointToScreen(new Point(5, 10))));
                    }
                    else
                    {
                        await ImageService.OpenInViewer(image);
                    }
                }
            }
            catch (Exception ex)
            {
                TraceService.Message(ex.Message);
            }
        }
示例#5
0
        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs ea)
        {
            ea.Handled = true;
            var image            = (Image)sender;
            var loadingIndicator = (TextBlock)image.Tag;

            loadingIndicator.ToolTip = ea.ErrorException.Message;
            loadingIndicator.Text    = (string)Application.Current.FindResource("WarningSign");
            TraceService.Message(ea.ErrorException.Message);
        }
示例#6
0
 private async void UpdateAsync(Unit _)
 {
     try
     {
         await likesTimeline.UpdateAsync();
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#7
0
 public static async Task FireAndForget(this ValueTask task)
 {
     try
     {
         await task.ConfigureAwait(false);
     }
     catch (Exception e)
     {
         TraceService.Message(e.Message);
     }
 }
示例#8
0
 private async void OnLoad(object sender, RoutedEventArgs e)
 {
     try
     {
         if (DataContext is UserProfileBlockViewModel vm)
         {
             await vm.GetUserInfoAsync(Tag as string).ConfigureAwait(false);
         }
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#9
0
 private async void OnIsVisibleChangedAsync(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
 {
     try
     {
         if (DataContext is ComposeControlViewModel vm)
         {
             await vm.GetUserInfoAsync().ConfigureAwait(false);
         }
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#10
0
 private async void CommandHandler(object sender, ExecutedRoutedEventArgs ea)
 {
     try
     {
         var tweet    = (TwitterStatus)ea.Parameter;
         var fromLang = tweet.Language ?? "und";
         var toLang   = CultureInfo.InstalledUICulture.TwoLetterISOLanguageName;
         tweet.TranslatedText = App.GetString("translate-text-working");
         tweet.TranslatedText = await TranslateService.Translate(tweet.FullText, fromLang, toLang, Settings.TranslateApiKey).ConfigureAwait(true);
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#11
0
 private async void CommandHandler(object sender, ExecutedRoutedEventArgs e)
 {
     try
     {
         mainViewModel.UserProfile = e.Parameter switch {
             User user => mainViewModel.UserProfile = user,
             string screenName => await UserInfo(screenName).ConfigureAwait(false),
             _ => null
         };
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#12
0
 public override async void Execute(object?parameter)
 {
     try
     {
         if (await MessageBox.Show(App.GetString("settings-sure"), MessageBox.MessageBoxButtons.YesNo) == MessageBox.MessageBoxResult.Yes)
         {
             ImageService.ClearImageCache();
             Console.Beep();
         }
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#13
0
 public override async void Execute(object?parameter)
 {
     try
     {
         if (parameter is (string url, PixelPoint pixelPoint) &&
             await MessageBox.Show(App.GetString("always-hide-image"), MessageBox.MessageBoxButtons.YesNo, pixelPoint) == MessageBox.MessageBoxResult.Yes &&
             Settings.HiddenImagesSet.Add(url))
         {
             Settings.Save();
         }
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#14
0
 public async void OnKeyDown(object?sender, KeyEventArgs e)
 {
     try
     {
         if ((e.Key == Key.Enter || e.Key == Key.Return) &&
             sender is TextBox textBox &&
             DataContext is SearchTimelineViewModel vm)
         {
             await vm.OnSearch(textBox.Text).ConfigureAwait(false);
         }
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#15
0
        private async void CommandHandler(object sender, ExecutedRoutedEventArgs ea)
        {
            try
            {
                const string filter = "Image files (*.gif;*.jpg;*.png;*.webp;*.mp4)|*.gif;*.jpg;*.png;*.webp;*.mp4";

                using var ofd = new OpenFileDialog {
                          Filter = filter
                      };

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        ComposeControlViewModel.IsUploadingMedia = true;
                        var mediaInfo = await UploadMediaAsync(ofd.FileName).ConfigureAwait(true);

                        ComposeControlViewModel.Media.Add(mediaInfo);
                    }
                    catch (WebException ex)
                    {
                        var stream = ex.Response?.GetResponseStream();
                        if (stream is null)
                        {
                            return;
                        }

                        using var reader = new StreamReader(stream);
                        var message = await reader.ReadToEndAsync().ConfigureAwait(false);

                        await MessageBoxService.ShowMessageBoxAsync(message).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        await MessageBoxService.ShowMessageBoxAsync(ex.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        ComposeControlViewModel.IsUploadingMedia = false;
                    }
                }
            }
            catch (Exception ex)
            {
                TraceService.Message(ex.Message);
            }
        }
示例#16
0
#pragma warning disable RCS1213 // (used in XAML) Remove unused member declaration.
#pragma warning disable S1144   // (used in XAML) Unused private types or members should be removed

        private async void LoadMediaAsync(object?sender, EventArgs e)
        {
            if (sender is Image image &&
                image.DataContext is Media media)
            {
                try
                {
                    image.Source = await ImageService
                                   .GetImageAsync(media.MediaUrl)
                                   .ConfigureAwait(true);
                }
                catch (Exception ex)
                {
                    TraceService.Message(ex.Message);
                }
            }
        }
示例#17
0
        private async void UpdateImage(object?sender, EventArgs _)
        {
            try
            {
                var token = cancellationToken; // make a copy
                if (token.IsCancellationRequested)
                {
                    return;
                }

                if (sender is Image {
                    DataContext : TwitterStatus status
                } image)
                {
                    try
                    {
                        image.Source = null;

                        var imageSource =
                            status.User.ProfileImageUrlBigger is { Length : > 0 } uri
                                ? await ImageService.GetImageAsync(uri, token)
                        : EmptyBitmap;

                        if (token.IsCancellationRequested)
                        {
                            return;
                        }
                        image.Source = imageSource;
                    }
                    catch (TaskCanceledException)
                    {
                        // return
                    }
                    catch (Exception ex)
                    {
                        TraceService.Message(ex.Message);
                        image.Source = EmptyBitmap;
                    }
                }
            }
            catch (Exception ex)
            {
                TraceService.Message(ex.Message);
            }
        }
示例#18
0
 private async void CommandHandler(object sender, ExecutedRoutedEventArgs e)
 {
     try
     {
         if (e.Parameter is TwitterStatus status)
         {
             var options = new JsonSerializerOptions {
                 WriteIndented = true
             };
             var json = JsonSerializer.Serialize(status, options);
             await MessageBoxService.ShowMessageBoxAsync(json).ConfigureAwait(false);
         }
     }
     catch (Exception ex)
     {
         TraceService.Message(ex.Message);
     }
 }
示例#19
0
        private async void LoadMediaAsync(object?sender, EventArgs unused)
        {
            try
            {
                var token = cancellationToken; // make a copy
                if (token.IsCancellationRequested)
                {
                    return;
                }

                if (sender is Image {
                    DataContext : Media media
                } image)
                {
                    try
                    {
                        var imageSource = await ImageService.GetImageAsync(media.MediaUrl, token);

                        if (token.IsCancellationRequested)
                        {
                            return;
                        }
                        image.Source ??= imageSource;
                    }
                    catch (TaskCanceledException)
                    {
                        // return
                    }
                    catch (Exception ex)
                    {
                        TraceService.Message(ex.Message);
                        image.Source = null;
                    }
                }
            }
            catch (Exception ex)
            {
                TraceService.Message(ex.Message);
            }
        }
示例#20
0
#pragma warning disable RCS1213 // (used in XAML) Remove unused member declaration.
#pragma warning disable S1144   // (used in XAML) Unused private types or members should be removedremoved

        private async void UpdateImage(object?sender, EventArgs e)
        {
            if (sender is Image image)
            {
                try
                {
                    image.Source =
                        DataContext is TwitterStatus status &&
                        status.User.ProfileImageUrlBigger is string uri &&
                        uri.Length > 0
                            ? await ImageService
                        .GetImageAsync(uri)
                        .ConfigureAwait(true)
                            : EmptyBitmap;
                }
                catch (Exception ex)
                {
                    image.Source = EmptyBitmap;
                    TraceService.Message(ex.Message);
                }
            }
        }
示例#21
0
        private async void OnTranslateClick(object?_, RoutedEventArgs __)
        {
            try
            {
                if (DataContext is not null)
                {
                    var tweet    = (TwitterStatus)DataContext;
                    var fromLang = tweet.Language ?? "und";
                    var toLang   = CultureInfo.InstalledUICulture.TwoLetterISOLanguageName;
                    tweet.TranslatedText = App.GetString("translate-text-working");

                    tweet.TranslatedText = await TranslateService.Translate(
                        tweet.FullText,
                        fromLang,
                        toLang,
                        (App.MainWindow.DataContext as MainWindowViewModel)?.Settings.TranslateApiKey)
                                           .ConfigureAwait(true);
                }
            }
            catch (Exception ex)
            {
                TraceService.Message(ex.Message);
            }
        }