Commits

Anonymous committed 0ff7c87 Draft

Add permission check using TaskCompletionSource

  • Participants
  • Parent commits 7072246
  • Branches permission

Comments (0)

Files changed (4)

File FaviconBrowser/FaviconBrowser.csproj

       <DependentUpon>MainWindow.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+    <Page Include="PermissionDialog.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="PermissionDialog.xaml.cs">
+      <DependentUpon>PermissionDialog.xaml</DependentUpon>
+    </Compile>
     <Compile Include="Properties\AssemblyInfo.cs">
       <SubType>Code</SubType>
     </Compile>

File FaviconBrowser/MainWindow.xaml.cs

 using System.Collections.Generic;
 using System.IO;
 using System.Net;
+using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media.Imaging;
             InitializeComponent();
         }
 
-        private void GetButton_OnClick(object sender, RoutedEventArgs e)
+        private async void GetButton_OnClick(object sender, RoutedEventArgs e)
         {
-            foreach (string domain in s_Domains)
+            if (await GetUserPermission())
             {
-                AddAFavicon(domain);
+                foreach (string domain in s_Domains)
+                {
+                    AddAFavicon(domain);
+                }
             }
         }
 
             m_WrapPanel.Children.Add(imageControl);
         }
 
+        private Task<bool> GetUserPermission()
+        {
+            // Make a TaskCompletionSource so we can return a puppet Task
+            TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
+
+            // Show the dialog
+            PermissionDialog dialog = new PermissionDialog();
+            dialog.Show();
+
+            // When the user is finished with the dialog, complete the Task using SetResult
+            dialog.Closed += delegate { tcs.SetResult(dialog.PermissionGranted); };
+
+            // Return the puppet Task, which isn't completed yet
+            return tcs.Task;
+        }
+
         private static Image MakeImageControl(byte[] bytes)
         {
             Image imageControl = new Image();

File FaviconBrowser/PermissionDialog.xaml

+<Window x:Class="FaviconBrowser.PermissionDialog"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        Title="PermissionDialog" Height="300" Width="300">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="*"/>
+            <RowDefinition Height="*"/>
+        </Grid.RowDefinitions>
+        <Button Grid.Row="0" Click="Button_Click_1">Yes, ok!</Button>
+        <Button Grid.Row="1" Click="Button_Click_2">No, don't do it!</Button>
+    </Grid>
+</Window>

File FaviconBrowser/PermissionDialog.xaml.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace FaviconBrowser
+{
+    /// <summary>
+    /// Interaction logic for PermissionDialog.xaml
+    /// </summary>
+    public partial class PermissionDialog : Window
+    {
+        public PermissionDialog()
+        {
+            InitializeComponent();
+        }
+
+        private void Button_Click_1(object sender, RoutedEventArgs e)
+        {
+            PermissionGranted = true;
+            Close();
+        }
+
+        private void Button_Click_2(object sender, RoutedEventArgs e)
+        {
+            PermissionGranted = false;
+            Close();
+        }
+
+        public bool PermissionGranted { get; set; }
+    }
+}