fix/clean caching

This commit is contained in:
Cameron
2024-05-15 22:01:26 -04:00
parent 87d9e7abb6
commit a9bd9c8854
2 changed files with 6 additions and 94 deletions
@@ -50,7 +50,7 @@ public partial class MainWindowViewModel : ViewModelBase
Url = currentTab?.Tag?.ToString() ?? string.Empty; 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 BackEnabled => GlobalState.BackHistory.Count > 0;
public bool ForwardEnabled => GlobalState.ForwardHistory.Count > 0; public bool ForwardEnabled => GlobalState.ForwardHistory.Count > 0;
public bool ProgressIndeterminate => Progress == 0; public bool ProgressIndeterminate => Progress == 0;
public ICommand Browse => ReactiveCommand.Create(FetchUrl); public ICommand Browse => ReactiveCommand.Create(() => FetchUrl(true));
public ICommand Reload => ReactiveCommand.Create(FetchUrlIgnoringCache); public ICommand Reload => ReactiveCommand.Create(() => FetchUrl(false));
public ICommand Back => ReactiveCommand.Create(() => 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); 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) if (IsBusy)
{ {
@@ -245,7 +245,7 @@ private void FetchUrl()
try try
{ {
ContentViewModelBase cachedContent = cacheService.Get(Url); ContentViewModelBase cachedContent = cacheService.Get(Url);
if (cachedContent != null) if (cachedContent != null && useCache)
{ {
Content = cachedContent; 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<ProcessingProgress>(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;
}
});
}
} }
+1 -1
View File
@@ -31,7 +31,7 @@
</Window.Resources> </Window.Resources>
<Window.KeyBindings> <Window.KeyBindings>
<KeyBinding Gesture="Ctrl+R" Command="{Binding Browse}" /> <KeyBinding Gesture="Ctrl+R" Command="{Binding Reload}" />
<KeyBinding Gesture="F5" Command="{Binding Reload}" /> <KeyBinding Gesture="F5" Command="{Binding Reload}" />
<KeyBinding Gesture="Alt+Home" Command="{StaticResource HomeCommand}" /> <KeyBinding Gesture="Alt+Home" Command="{StaticResource HomeCommand}" />
<KeyBinding Gesture="Alt+H" Command="{StaticResource HomeCommand}" /> <KeyBinding Gesture="Alt+H" Command="{StaticResource HomeCommand}" />