diff --git a/src/Markdowser/ViewModels/MainWindowViewModel.cs b/src/Markdowser/ViewModels/MainWindowViewModel.cs index dc37d03..d561f6d 100644 --- a/src/Markdowser/ViewModels/MainWindowViewModel.cs +++ b/src/Markdowser/ViewModels/MainWindowViewModel.cs @@ -50,7 +50,7 @@ public partial class MainWindowViewModel : ViewModelBase Url = currentTab?.Tag?.ToString() ?? string.Empty; - FetchUrl(); + FetchUrl(true); } } @@ -101,8 +101,8 @@ public partial class MainWindowViewModel : ViewModelBase public bool BackEnabled => GlobalState.BackHistory.Count > 0; public bool ForwardEnabled => GlobalState.ForwardHistory.Count > 0; public bool ProgressIndeterminate => Progress == 0; - public ICommand Browse => ReactiveCommand.Create(FetchUrl); - public ICommand Reload => ReactiveCommand.Create(FetchUrlIgnoringCache); + public ICommand Browse => ReactiveCommand.Create(() => FetchUrl(true)); + public ICommand Reload => ReactiveCommand.Create(() => FetchUrl(false)); public ICommand Back => ReactiveCommand.Create(() => @@ -199,7 +199,7 @@ public partial class MainWindowViewModel : ViewModelBase return Uri.TryCreate(uriString, UriKind.Absolute, out uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps); } -private void FetchUrl() +private void FetchUrl(bool useCache = true) { if (IsBusy) { @@ -245,7 +245,7 @@ private void FetchUrl() try { ContentViewModelBase cachedContent = cacheService.Get(Url); - if (cachedContent != null) + if (cachedContent != null && useCache) { Content = cachedContent; } @@ -317,92 +317,4 @@ private void FetchUrl() }); } -private void FetchUrlIgnoringCache() -{ - if (IsBusy) - { - WindowNotificationManager.Show(new Notification("Busy", "The browser is currently busy.", NotificationType.Warning)); - return; - } - - if (string.IsNullOrWhiteSpace(Url)) - { - Content = DefaultContent; - Debug.WriteLine("URL is empty."); - CurrentTab.Header = "New Tab"; - return; - } - - if (!IsValidHttpUri(Url, out _)) - { - if (Url.StartsWith("//")) - { - Url = $"https:{Url}"; - } - else - { - // Search with duckduckgo - try - { - Url = string.Format(Settings.Current.SearchEngineUrl, Uri.EscapeDataString(Url)); - } - catch (FormatException ex) - { - WindowNotificationManager.Show(new Notification("Invalid Search Engine URL", $"{ex.Message}", NotificationType.Error)); - } - } - } - - IsBusy = true; - Progress = 0; - - _ = Task.Run(async () => - { - Debug.WriteLine("Fetching URL..."); - - try - { - HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(Url); - - string? newUrl = httpResponseMessage.RequestMessage?.RequestUri?.ToString(); - - if (newUrl is not null && newUrl != Url) - { - string oldUrl = Url; - Dispatcher.UIThread.Post(() => WindowNotificationManager.Show(new Notification("Redirected", $"Redirected from\n{oldUrl}\nto\n{newUrl}", NotificationType.Warning))); - Url = newUrl; - } - - if (!httpResponseMessage.IsSuccessStatusCode) - { - IsBusy = false; - - StringBuilder errorMessage = new(); - - _ = errorMessage.AppendLine($"# {(int)httpResponseMessage.StatusCode} {httpResponseMessage.StatusCode}"); - _ = errorMessage.AppendLine($"Failed to fetch URL: {httpResponseMessage.ReasonPhrase}"); - - Content = new MarkdownContentViewModel("Error", errorMessage.ToString()); - return; - } - - Content = await contentProcessorManager.ProcessContent(httpResponseMessage, new Progress(p => Progress = p.Percentage)); - cacheService.Set(Url, Content); - - Dispatcher.UIThread.Post(() => CurrentTab.Header = Content.Title); - } - catch (HttpRequestException e) - { - // Handle exception - } - catch (Exception e) - { - // Handle exception - } - finally - { - IsBusy = false; - } - }); -} } \ No newline at end of file diff --git a/src/Markdowser/Views/MainWindow.axaml b/src/Markdowser/Views/MainWindow.axaml index b7edad5..2235528 100644 --- a/src/Markdowser/Views/MainWindow.axaml +++ b/src/Markdowser/Views/MainWindow.axaml @@ -31,7 +31,7 @@ - +