Skip to content

Commit

Permalink
Merge pull request #41 from DmitryGolubenkov/feature/double-click-copy
Browse files Browse the repository at this point in the history
Feature: Copy code to clipboard by double clicking AccountControl
  • Loading branch information
FireCubeStudios authored Oct 2, 2022
2 parents ea35865 + 9e46cb7 commit e38ef5f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion TemplateApp/Controls/AccountControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
mc:Ignorable="d">
<StackPanel Width="300" Loaded="Content_Loaded" Unloaded="Content_Unloaded">
<StackPanel Width="300" Loaded="Content_Loaded" Unloaded="Content_Unloaded" DoubleTapped="Content_DoubleTapped">
<Grid CornerRadius="8" Padding="12, 12, 12, 0" Margin="0, 0, 0, 8" Height="80" BorderThickness="1" Canvas.ZIndex="3">
<TextBlock MaxWidth="210" Opacity="0.7" TextTrimming="CharacterEllipsis" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1" Text="{x:Bind AccountVaultItem.Name, Mode=OneWay}"/>
<Grid HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="200" Height="80" Margin="0, 0, 0, -20" >
Expand Down
45 changes: 43 additions & 2 deletions TemplateApp/Controls/AccountControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Protecc.Classes;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Protecc.Classes;
using Protecc.Helpers;
using Protecc.Services;
using System;
Expand Down Expand Up @@ -36,14 +37,26 @@ public sealed partial class AccountControl : UserControl
public VaultItem AccountVaultItem
{
get { return (VaultItem)GetValue(AccountVaultItemProperty); }
set {
set
{
SetValue(AccountVaultItemProperty, value);
TOTP = new TOTPHelper(AccountVaultItem);
}
}
public static readonly DependencyProperty AccountVaultItemProperty =
DependencyProperty.Register("AccountVaultItem", typeof(VaultItem), typeof(AccountControl), null);

public InAppNotification InAppNotificationComponent
{
get { return (InAppNotification)GetValue(InAppNotificationComponentProperty); }
set
{
SetValue(InAppNotificationComponentProperty, value);
}
}
public static readonly DependencyProperty InAppNotificationComponentProperty =
DependencyProperty.Register("InAppNotificationComponent", typeof(InAppNotification), typeof(AccountControl), null);

public AccountControl()
{
this.InitializeComponent();
Expand Down Expand Up @@ -90,10 +103,15 @@ private async void Copy_Click(object sender, RoutedEventArgs e)
dataPackage.SetText(TOTP.Code.Replace(" ", ""));
Clipboard.SetContent(dataPackage);
CopyIcon.Symbol = Fluent.Icons.FluentSymbol.Checkmark20;
// Show notification
InAppNotificationComponent.Show("Code copied to clipboard!", 3000);
}
catch
{
CopyIcon.Symbol = Fluent.Icons.FluentSymbol.ErrorCircle20;
// Inform user about error
InAppNotificationComponent.Show("An error occurred while copying to clipboard.", 3000);

}
await Task.Delay(2000);
CopyIcon.Symbol = Fluent.Icons.FluentSymbol.Copy20;
Expand All @@ -110,5 +128,28 @@ private void CodeBlock_Loaded(object sender, RoutedEventArgs e)
}

//private void Content_Loaded(object sender, RoutedEventArgs e) => TOTP = new TOTPHelper(CodeBlock, Progress, AccountVaultItem);

/// <summary>
/// Copies displayed code to clipboard and displays Windows notification.
/// </summary>
private void Content_DoubleTapped(object sender, RoutedEventArgs e)
{
try
{
// Remove spaces and copy to clipboard
var content = new DataPackage();
content.RequestedOperation = DataPackageOperation.Copy;
content.SetText(TOTP.Code.Replace(" ", ""));
Clipboard.SetContent(content);

// Show notification
InAppNotificationComponent.Show("Code copied to clipboard!", 3000);
}
catch
{
// Inform user about error
InAppNotificationComponent.Show("An error occurred while copying to clipboard.", 3000);
}
}
}
}
3 changes: 2 additions & 1 deletion TemplateApp/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<GridView x:Name="AccountsView" ItemContainerStyle="{ThemeResource CardContainer}" SelectionChanged="AccountsView_SelectionChanged" ItemsSource="{x:Bind Services:CredentialService.CredentialList, Mode=OneWay}" SelectionMode="Single" VerticalAlignment="Top" Margin="-8, 86, -50, 0" HorizontalAlignment="Left">
<GridView.ItemTemplate>
<DataTemplate x:DataType="Classes:VaultItem">
<controls:AccountControl AccountVaultItem="{x:Bind }"/>
<controls:AccountControl AccountVaultItem="{x:Bind }" InAppNotificationComponent="{Binding ElementName=InAppNotificationComponent}"/>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemContainerTransitions>
Expand All @@ -103,5 +103,6 @@
</Grid.Background>
</Grid>
</Grid>
<toolkit:InAppNotification Name="InAppNotificationComponent" />
</Grid>
</Page>

0 comments on commit e38ef5f

Please sign in to comment.