Browse Source

Switched to using the MIcrosoft WebView2 for the MYOB authentication.
Properly staticised the global connection data.
Fixed editor for global poster settings

Kenric Nugteren 1 năm trước cách đây
mục cha
commit
6c8f1eaf46

+ 1 - 1
InABox.Poster.MYOB/InABox.Poster.MYOB.csproj

@@ -5,10 +5,10 @@
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
 	  <UseWpf>true</UseWpf>
-	  <UseWindowsForms>true</UseWindowsForms>
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2592.51" />
     <PackageReference Include="MYOB.AccountRight.API.SDK" Version="2024.4.591" />
   </ItemGroup>
 

+ 16 - 1
InABox.Poster.MYOB/MYOBGlobalPosterSettings.cs

@@ -9,9 +9,24 @@ namespace InABox.Poster.MYOB;
 
 public class MYOBGlobalPosterSettings : GlobalPosterSettings
 {
-    public Guid CompanyFileID { get; set; }
+    [EditorSequence(1)]
+    [TextBoxEditor]
+    public string CompanyFileID { get; set; }
 
+    [EditorSequence(2)]
+    [TextBoxEditor]
     public string CompanyFileUserID { get; set; }
 
+    [EditorSequence(3)]
+    [PasswordEditor(ViewButtonVisible = true)]
     public string CompanyFilePassword { get; set; }
+
+    public Guid GetCompanyFileID()
+    {
+        if(Guid.TryParse(CompanyFileID, out var id))
+        {
+            return id;
+        }
+        return Guid.Empty;
+    }
 }

+ 54 - 35
InABox.Poster.MYOB/MYOBPosterEngine.cs

@@ -1,5 +1,6 @@
 using InABox.Core;
 using InABox.Core.Postable;
+using Microsoft.Web.WebView2.Wpf;
 using MYOB.AccountRight.SDK;
 using MYOB.AccountRight.SDK.Contracts;
 using MYOB.AccountRight.SDK.Services;
@@ -12,6 +13,7 @@ using System.Threading.Tasks;
 using System.Web;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Threading;
 
 namespace InABox.Poster.MYOB;
 
@@ -30,12 +32,7 @@ public class MYOBConnectionData(ApiConfiguration configuration, CompanyFileServi
     public IOAuthKeyService AuthKey { get; set; } = authKey;
 }
 
-public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
-    PosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
-    IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>
-
-    where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
-    where TSettings : MYOBPosterSettings, new()
+public static partial class MYOBPosterEngine
 {
     internal static readonly string DEV_KEY = "f3b27f88-2ef9-4d8e-95c1-d0a045d0afee";
     internal static readonly string SECRET_KEY = "ksm0e8yo6oumcPb63A8cduaN";
@@ -45,13 +42,6 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
 
     private static MYOBConnectionData? _connectionData;
 
-    private MYOBGlobalPosterSettings GetGlobalSettings() => (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>).GetGlobalSettings();
-
-    public override bool BeforePost(IDataModel<TPostable> model)
-    {
-        return Poster.BeforePost(model);
-    }
-
     public static bool GetAuthorisationCode(IApiConfiguration config, out string? code)
     {
         var url = $"{AUTH_URL}?client_id={config.ClientId}&redirect_uri={HttpUtility.UrlEncode(config.RedirectUrl)}&scope={AUTH_SCOPE}&response_type=code";
@@ -64,24 +54,36 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
         };
 
         string? resultCode = null;
-        var browser = new System.Windows.Forms.WebBrowser();
-        browser.DocumentTitleChanged += (o, e) =>
+
+        var view = new WebView2
+        {
+        };
+        view.NavigationCompleted += (o, e) =>
         {
-            var htmlText = browser.DocumentText;
-            if(htmlText is not null)
+            view.ExecuteScriptAsync("document.documentElement.innerHTML;").ContinueWith(task =>
             {
-                var match = CodeRegex().Match(htmlText);
-                if (match.Success)
+                if (task.Result is null) return;
+
+                var str = Serialization.Deserialize<string>(task.Result);
+                if (str is null) return;
+
+                var match = CodeRegex().Match(str);
+                if (!match.Success) return;
+
+                resultCode = match.Groups[1].Value;
+                window.Dispatcher.BeginInvoke(() =>
                 {
-                    resultCode = match.Groups[1].Value;
-                    window.DialogResult = true;
-                    window.Close();
-                }
-            }
+                    if(window.DialogResult is null)
+                    {
+                        window.DialogResult = true;
+                        window.Close();
+                    }
+                });
+            });
         };
+        view.Source = new Uri(url);
+        window.Content = view;
 
-        browser.Navigate(url);
-        window.Content = browser;
         if(window.ShowDialog() == true)
         {
             code = resultCode;
@@ -94,7 +96,7 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
         }
     }
 
-    private static MYOBConnectionData GetConnectionData()
+    public static MYOBConnectionData GetConnectionData()
     {
         if(_connectionData is MYOBConnectionData data)
         {
@@ -124,23 +126,43 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
 
         return _connectionData;
     }
+
+    [GeneratedRegex("code=(.*)<")]
+    private static partial Regex CodeRegex();
+}
+
+public abstract class MYOBPosterEngine<TPostable, TSettings> :
+    PosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
+    IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>
+
+    where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+    where TSettings : MYOBPosterSettings, new()
+{
+    private MYOBGlobalPosterSettings GetGlobalSettings() => (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>).GetGlobalSettings();
+
+    public override bool BeforePost(IDataModel<TPostable> model)
+    {
+        return Poster.BeforePost(model);
+    }
+
     
     protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
     {
-        var data = GetConnectionData();
+        var data = MYOBPosterEngine.GetConnectionData();
 
         var globalSettings = GetGlobalSettings();
-        if(data.CompanyFile is null || data.CompanyFile.Id != globalSettings.CompanyFileID)
+        var companyFileID = globalSettings.GetCompanyFileID();
+        if(data.CompanyFile is null || data.CompanyFile.Id != companyFileID)
         {
-            if(globalSettings.CompanyFileID == Guid.Empty)
+            if(companyFileID == Guid.Empty)
             {
                 throw new PostFailedMessageException("No CompanyFileID has been set.");
             }
-            else if(globalSettings.CompanyFileUserID.IsNullOrWhiteSpace() || globalSettings.CompanyFilePassword.IsNullOrWhiteSpace())
+            else if(globalSettings.CompanyFileUserID.IsNullOrWhiteSpace())
             {
                 throw new PostFailedMessageException("CompanyFile credentials have not been set.");
             }
-            var companyFile = data.CompanyFileService.GetRange().FirstOrDefault(x => x.Id == globalSettings.CompanyFileID);
+            var companyFile = data.CompanyFileService.GetRange().FirstOrDefault(x => x.Id == companyFileID);
             var fileCredentials = new CompanyFileCredentials(globalSettings.CompanyFileUserID, globalSettings.CompanyFilePassword);
             data.CompanyFile = companyFile;
             data.CompanyFileCredentials = fileCredentials;
@@ -154,7 +176,4 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
     public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
     {
     }
-
-    [GeneratedRegex("code=(.*)<")]
-    private static partial Regex CodeRegex();
 }