Skip to content

Commit

Permalink
Responsive plugin uninstall flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KimihikoAkayasaki committed Mar 12, 2023
1 parent 0296887 commit 5dfc1a6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
31 changes: 25 additions & 6 deletions Amethyst/MVVM/PluginHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public class LoadAttemptedPlugin : INotifyPropertyChanged

public Version Version { get; init; } = new("0.0.0.0");

public bool UpdateFound => !_updateEnqueued && UpdateData.Found;
public bool UpdateFound => !_updateEnqueued && UpdateData.Found && !Uninstalling;

public (bool Found, string Download, Version Version, string Changelog)
UpdateData { get; private set; } = (false, null, null, null);
Expand Down Expand Up @@ -616,17 +616,31 @@ public async Task<bool> ExecuteUpdates()
public void EnqueuePluginUninstall()
{
// Enqueue a delete startup action to uninstall this plugin
StartupController.Controller.StartupTasks.Add(new StartupDeleteTask
{
Name = $"Delete plugin {Name} v{Version}",
PluginFolder = Folder
});
StartupController.Controller.StartupTasks.Add(
new StartupDeleteTask
{
Name = $"Delete plugin {Name} v{Version}",
Data = Folder + Guid + Version,
PluginFolder = Folder
});

// Show a badge that this plugin will be uninstalled
Uninstalling = true;
OnPropertyChanged();
}

public void CancelPluginUninstall()
{
// Delete the uninstall startup action
StartupController.Controller.StartupTasks.Remove(
StartupController.Controller.StartupTasks
.FirstOrDefault(x => x.Data == Folder + Guid + Version));

// Hide the uninstall badge
Uninstalling = false;
OnPropertyChanged();
}

public string TrimString(string s, int l)
{
return s?[..Math.Min(s.Length, l)] +
Expand Down Expand Up @@ -655,6 +669,11 @@ public double BoolToOpacity(bool value)
return value ? 1.0 : 0.0;
}

public string FormatResourceString(string resourceName)
{
return string.Format(LocalizedJsonString(resourceName), Name);
}

public void OnPropertyChanged(string propName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
Expand Down
21 changes: 20 additions & 1 deletion Amethyst/MVVM/StorePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public void SchedulePluginUninstall()
OnPropertyChanged(); // Refresh everything
}

public void CancelPluginUninstall()
{
// Enqueue a delete startup action to uninstall this plugin
InstalledPlugin?.CancelPluginUninstall();
OnPropertyChanged(); // Refresh everything
}

public async void InstallPlugin()
{
// TODO download action etc
Expand Down Expand Up @@ -169,6 +176,13 @@ public async void FetchPluginData()
x => x["name"]?.ToString().EndsWith(".zip") ?? false, null)?
["browser_download_url"]?.ToString()
};

// Setup property watchers
if (InstalledPlugin is not null)
{
InstalledPlugin.PropertyChanged -= InstalledPluginOnPropertyChanged;
InstalledPlugin.PropertyChanged += InstalledPluginOnPropertyChanged;
}
}
catch (HttpRequestException e)
{
Expand All @@ -182,7 +196,7 @@ public async void FetchPluginData()
LatestRelease = null;
Logger.Error(e);
}

// Refresh everything else
OnPropertyChanged();

Expand All @@ -197,6 +211,11 @@ public async void FetchPluginData()
OnPropertyChanged("FinishedLoadingData");
}

private void InstalledPluginOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Shared.Main.DispatcherQueue.TryEnqueue(() => OnPropertyChanged());
}

public class PluginRepository
{
public string Name { get; set; }
Expand Down
49 changes: 43 additions & 6 deletions Amethyst/Pages/Plugins.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
xmlns:mvvm="using:Amethyst.MVVM"
xmlns:util="using:Amethyst.Utils"
xmlns:classes="using:Amethyst.Classes"
xmlns:icons="using:WinUI.System.Icons"
Loaded="Page_Loaded" NavigationCacheMode="Required"
mc:Ignorable="d" Background="Transparent">

Expand Down Expand Up @@ -284,11 +285,16 @@
IsOpen="{x:Bind Uninstalling, Mode=OneWay}"
Title="{x:Bind FormatResourceString('/PluginStore/Titles/Uninstalling/Notice'), Mode=OneWay}"
Message="{x:Bind util:Translator.Get.String('/PluginStore/Captions/Uninstalling/Notice'), Mode=OneWay}"
IsClosable="True" CornerRadius="0"
IsClosable="false" CornerRadius="0"
BorderThickness="0,0,0,1">
<InfoBar.OpacityTransition>
<ScalarTransition />
</InfoBar.OpacityTransition>
<InfoBar.ActionButton>
<Button HorizontalAlignment="Right" Margin="0,0,-8,0"
Content="{x:Bind util:Translator.Get.String('/GeneralPage/Buttons/Cancel'), Mode=OneWay}"
Click="{x:Bind CancelPluginUninstall, Mode=OneWay}" />
</InfoBar.ActionButton>
</InfoBar>

<InfoBar
Expand Down Expand Up @@ -685,29 +691,43 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<StackPanel Grid.Column="0" Margin="-5,-3,0,0"
<StackPanel Grid.Column="0" Margin="-5,0,0,0"
Orientation="Horizontal">

<Button VerticalAlignment="Center" Margin="-10,0,4,0"
Visibility="{x:Bind CanUninstall, Mode=OneWay}"
Style="{ThemeResource AlternateCloseButtonStyle}"
Click="{x:Bind EnqueuePluginUninstall, Mode=OneWay}">
<icons:FluentSymbolIcon Symbol="Delete20" />
</Button>

<Button VerticalAlignment="Center" Margin="-10,0,4,0"
Visibility="{x:Bind Uninstalling, Mode=OneWay}"
Style="{ThemeResource AlternateCloseButtonStyle}"
Click="{x:Bind CancelPluginUninstall, Mode=OneWay}">
<icons:FluentSymbolIcon Symbol="DeleteArrowBack20" />
</Button>

<InfoBadge
Background="{ThemeResource SystemFillColorCriticalBrush}"
HorizontalAlignment="Left" VerticalAlignment="Top"
Visibility="{x:Bind LoadError, Mode=OneWay}"
Margin="0,15,10,0" Width="17" Height="17">
Margin="0,14,10,0" Width="17" Height="17">
<InfoBadge.IconSource>
<FontIconSource Glyph="&#xEDAE;"
Foreground="{ThemeResource NoThemeColorSolidColorBrush}" />
</InfoBadge.IconSource>
</InfoBadge>

<TextBlock FontWeight="SemiBold" FontSize="14"
<TextBlock FontWeight="SemiBold" FontSize="14" Margin="0,2,0,0"
Text="{x:Bind TrimString(Name, 45), Mode=OneWay}"
ToolTipService.ToolTip="{x:Bind Name, Mode=OneWay}"
HorizontalAlignment="Left"
VerticalAlignment="Center" />

<StackPanel Orientation="Horizontal"
VerticalAlignment="Bottom"
Margin="7,0,0,10"
Margin="7,0,0,8"
Visibility="{x:Bind PublisherValid, Mode=OneWay}">
<TextBlock VerticalAlignment="Center"
FontWeight="SemiBold" Opacity="0.5"
Expand Down Expand Up @@ -740,6 +760,24 @@

<Expander.Content>
<StackPanel Orientation="Vertical" Width="650" Padding="0,8,0,10">
<InfoBar
Severity="Informational" Margin="-14,-10,-14,5"
Opacity="{x:Bind BoolToOpacity(Uninstalling), Mode=OneWay}"
IsOpen="{x:Bind Uninstalling, Mode=OneWay}"
Title="{x:Bind FormatResourceString('/PluginStore/Titles/Uninstalling/Notice'), Mode=OneWay}"
Message="{x:Bind util:Translator.Get.String('/PluginStore/Captions/Uninstalling/Notice'), Mode=OneWay}"
IsClosable="false" CornerRadius="0"
BorderThickness="0,0,0,1">
<InfoBar.OpacityTransition>
<ScalarTransition />
</InfoBar.OpacityTransition>
<InfoBar.ActionButton>
<Button HorizontalAlignment="Right" Margin="0,0,-8,0"
Content="{x:Bind util:Translator.Get.String('/GeneralPage/Buttons/Cancel'), Mode=OneWay}"
Click="{x:Bind CancelPluginUninstall, Mode=OneWay}" />
</InfoBar.ActionButton>
</InfoBar>

<InfoBar Severity="Informational" Margin="-14,-10,-14,5"
Opacity="{x:Bind BoolToOpacity(UpdateFound), Mode=OneWay}"
IsOpen="{x:Bind UpdateFound, Mode=OneWay}"
Expand Down Expand Up @@ -874,7 +912,6 @@
ToolTipService.ToolTip="{x:Bind Website, Mode=OneWay}"
ToolTipService.Placement="Bottom" />
</Grid>

</StackPanel>
</Expander.Content>
</Expander>
Expand Down

0 comments on commit 5dfc1a6

Please sign in to comment.