From 78bb7e2a71639e33fa4805d1b2844a55befcb9fe Mon Sep 17 00:00:00 2001 From: justerzhu Date: Sun, 17 Nov 2024 07:15:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DMaui=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/c#/GeneralUpdate.Client/Program.cs | 8 +- .../Hubs/IUpgradeHubService.cs | 8 +- .../Hubs/UpgradeHubService.cs | 8 +- .../Internal/Exception/OSSDownloadArgs.cs | 16 +++ src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs | 5 - .../Internal/OSSDownloadArgs.cs | 8 -- .../ContentProvider/.gitkeep | 0 .../GeneralUpdate.Maui.OSS/Domain/DO/.gitkeep | 0 .../Domain/Entity/ParamsAndroid.cs | 24 ----- .../Domain/Enum/.gitkeep | 0 .../Domain/PO/Assembler/.gitkeep | 0 .../GeneralUpdate.Maui.OSS.csproj | 99 +------------------ .../GeneralUpdateOSS.cs | 17 ++-- .../Internal/ParamsAndroid.cs | 15 +++ .../Internal/VersionInfo.cs | 37 +++++++ .../Platforms/Android/Strategy.cs | 26 ++--- .../Strategys/AbstractStrategy.cs | 1 - .../GeneralUpdate.OSSClient/MainPage.xaml.cs | 20 ++-- 18 files changed, 108 insertions(+), 184 deletions(-) create mode 100644 src/c#/GeneralUpdate.Common/Internal/Exception/OSSDownloadArgs.cs delete mode 100644 src/c#/GeneralUpdate.Core/Internal/OSSDownloadArgs.cs delete mode 100644 src/c#/GeneralUpdate.Maui.OSS/ContentProvider/.gitkeep delete mode 100644 src/c#/GeneralUpdate.Maui.OSS/Domain/DO/.gitkeep delete mode 100644 src/c#/GeneralUpdate.Maui.OSS/Domain/Entity/ParamsAndroid.cs delete mode 100644 src/c#/GeneralUpdate.Maui.OSS/Domain/Enum/.gitkeep delete mode 100644 src/c#/GeneralUpdate.Maui.OSS/Domain/PO/Assembler/.gitkeep create mode 100644 src/c#/GeneralUpdate.Maui.OSS/Internal/ParamsAndroid.cs create mode 100644 src/c#/GeneralUpdate.Maui.OSS/Internal/VersionInfo.cs diff --git a/src/c#/GeneralUpdate.Client/Program.cs b/src/c#/GeneralUpdate.Client/Program.cs index c2aa86e7..5bb23a66 100644 --- a/src/c#/GeneralUpdate.Client/Program.cs +++ b/src/c#/GeneralUpdate.Client/Program.cs @@ -78,10 +78,10 @@ private static void Main(string[] args) GeneralClientOSS.Start(paramsOSS);*/ - IUpgradeHubService hub = new UpgradeHubService("http://localhost:5008/UpgradeHub", null, "GeneralUpdate"); - hub.AddReceiveListener(Receive); - hub.StartAsync().Wait(); - + /*IUpgradeHubService hub = new UpgradeHubService("http://localhost:5008/UpgradeHub", null, "GeneralUpdate"); + hub.AddListenerReceive(Receive); + hub.StartAsync().Wait();*/ + Console.Read(); } diff --git a/src/c#/GeneralUpdate.ClientCore/Hubs/IUpgradeHubService.cs b/src/c#/GeneralUpdate.ClientCore/Hubs/IUpgradeHubService.cs index 44e3e390..199ccc99 100644 --- a/src/c#/GeneralUpdate.ClientCore/Hubs/IUpgradeHubService.cs +++ b/src/c#/GeneralUpdate.ClientCore/Hubs/IUpgradeHubService.cs @@ -9,25 +9,25 @@ public interface IUpgradeHubService /// Add a listener to receive upgrade information pushed from the server. /// /// string : group name , string : received message content. - public void AddReceiveListener(Action receiveMessageCallback); + public void AddListenerReceive(Action receiveMessageCallback); /// /// Add a listener to receive online and offline notifications. /// /// string : Offline or online information. - public void AddOnlineListener(Action onlineMessageCallback); + public void AddListenerOnline(Action onlineMessageCallback); /// /// Add a listener to receive reconnection notifications. /// /// string? : Reconnection information. - public void AddReconnectedListener(Func? reconnectedCallback); + public void AddListenerReconnected(Func? reconnectedCallback); /// /// Add a listener to receive disconnection notifications. /// /// Exception? : Offline exception information. - public void AddClosedListener(Func closeCallback); + public void AddListenerClosed(Func closeCallback); /// /// Start subscribing to upgrade push notifications, and the content of the notifications should be agreed upon independently (it is recommended to use JSON data format). diff --git a/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs b/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs index 08dc1407..f45ace84 100644 --- a/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs +++ b/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs @@ -27,16 +27,16 @@ public class UpgradeHubService(string url, string? token = null, string? args = .WithAutomaticReconnect(new RandomRetryPolicy()) .Build(); - public void AddReceiveListener(Action receiveMessageCallback) + public void AddListenerReceive(Action receiveMessageCallback) => _connection?.On(ReceiveMessageflag, receiveMessageCallback); - public void AddOnlineListener(Action onlineMessageCallback) + public void AddListenerOnline(Action onlineMessageCallback) => _connection?.On(Onlineflag, onlineMessageCallback); - public void AddReconnectedListener(Func? reconnectedCallback) + public void AddListenerReconnected(Func? reconnectedCallback) => _connection!.Reconnected += reconnectedCallback; - public void AddClosedListener(Func closeCallback) + public void AddListenerClosed(Func closeCallback) => _connection!.Closed += closeCallback; public async Task StartAsync() diff --git a/src/c#/GeneralUpdate.Common/Internal/Exception/OSSDownloadArgs.cs b/src/c#/GeneralUpdate.Common/Internal/Exception/OSSDownloadArgs.cs new file mode 100644 index 00000000..7aed4480 --- /dev/null +++ b/src/c#/GeneralUpdate.Common/Internal/Exception/OSSDownloadArgs.cs @@ -0,0 +1,16 @@ +using System; + +namespace GeneralUpdate.Common.Internal; + +public class OSSDownloadArgs : EventArgs +{ + public long ReadLength { get; set; } + + public long TotalLength { get; set; } + + public OSSDownloadArgs(long readLength, long totalLength) + { + ReadLength = readLength; + TotalLength = totalLength; + } +} \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs b/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs index d30b167d..29970e1c 100644 --- a/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs +++ b/src/c#/GeneralUpdate.Core/GeneralUpdateOSS.cs @@ -1,13 +1,8 @@ using System; -using System.Diagnostics; using System.Text.Json; using System.Threading.Tasks; using GeneralUpdate.Common.AOT.JsonContext; -using GeneralUpdate.Common.Download; -using GeneralUpdate.Common.Internal; -using GeneralUpdate.Common.Internal.Event; using GeneralUpdate.Common.Shared.Object; -using GeneralUpdate.Core.Internal; using GeneralUpdate.Core.Strategys; namespace GeneralUpdate.Core diff --git a/src/c#/GeneralUpdate.Core/Internal/OSSDownloadArgs.cs b/src/c#/GeneralUpdate.Core/Internal/OSSDownloadArgs.cs deleted file mode 100644 index 1e4fe03a..00000000 --- a/src/c#/GeneralUpdate.Core/Internal/OSSDownloadArgs.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace GeneralUpdate.Core.Internal; - -public class OSSDownloadArgs : EventArgs -{ - -} \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Maui.OSS/ContentProvider/.gitkeep b/src/c#/GeneralUpdate.Maui.OSS/ContentProvider/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/c#/GeneralUpdate.Maui.OSS/Domain/DO/.gitkeep b/src/c#/GeneralUpdate.Maui.OSS/Domain/DO/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/c#/GeneralUpdate.Maui.OSS/Domain/Entity/ParamsAndroid.cs b/src/c#/GeneralUpdate.Maui.OSS/Domain/Entity/ParamsAndroid.cs deleted file mode 100644 index 015a9ea7..00000000 --- a/src/c#/GeneralUpdate.Maui.OSS/Domain/Entity/ParamsAndroid.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace GeneralUpdate.Maui.OSS.Domain.Entity -{ - public class ParamsAndroid : GeneralUpdate.Core.Domain.Entity.Entity - { - public string Url { get; set; } - - public string Apk { get; set; } - - public string CurrentVersion { get; set; } - - public string Authority { get; set; } - - public string VersionFileName { get; set; } - - public ParamsAndroid(string url, string apk, string authority, string currentVersion, string versionFileName) - { - Url = IsURL(url) ? url : throw new ArgumentNullException(nameof(url)); - Apk = apk ?? throw new ArgumentNullException(nameof(apk)); - CurrentVersion = IsVersion(currentVersion) ? currentVersion : throw new ArgumentNullException(nameof(currentVersion)); - Authority = authority ?? throw new ArgumentNullException(nameof(authority)); - VersionFileName = versionFileName ?? "versions.json"; - } - } -} \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Maui.OSS/Domain/Enum/.gitkeep b/src/c#/GeneralUpdate.Maui.OSS/Domain/Enum/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/c#/GeneralUpdate.Maui.OSS/Domain/PO/Assembler/.gitkeep b/src/c#/GeneralUpdate.Maui.OSS/Domain/PO/Assembler/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj b/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj index ed412e67..c4bb3a02 100644 --- a/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj +++ b/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj @@ -1,19 +1,11 @@ - - net8.0;net8.0-maccatalyst;net8.0-android - - + net8.0;net8.0-android true true enable false - 11.0 - 14.0 21.0 - 10.0.17763.0 - 10.0.17763.0 - 6.5 juster.zhu Based on. Automatic updates for NET MAUI multiplatform. Copyright © 2023 @@ -23,94 +15,7 @@ https://github.com/JusterZhu/GeneralUpdate False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - True - \ - - - - - - - - - - - - diff --git a/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdateOSS.cs b/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdateOSS.cs index 30e16c3c..fbb644b3 100644 --- a/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdateOSS.cs +++ b/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdateOSS.cs @@ -1,7 +1,6 @@ -using GeneralUpdate.Core.Events; -using GeneralUpdate.Core.Events.CommonArgs; -using GeneralUpdate.Core.Events.OSSArgs; -using GeneralUpdate.Maui.OSS.Domain.Entity; +using GeneralUpdate.Common.Internal; +using GeneralUpdate.Common.Internal.Event; +using GeneralUpdate.Maui.OSS.Internal; using GeneralUpdate.Maui.OSS.Strategys; namespace GeneralUpdate.Maui.OSS @@ -11,12 +10,7 @@ namespace GeneralUpdate.Maui.OSS /// public sealed class GeneralUpdateOSS { - #region Constructors - - private GeneralUpdateOSS() - { } - - #endregion Constructors + private GeneralUpdateOSS() { } #region Public Methods @@ -69,7 +63,8 @@ public static void AddListenerException(Action callb private static void AddListener(Action callbackAction) where TArgs : EventArgs { - if (callbackAction != null) EventManager.Instance.AddListener(callbackAction); + if (callbackAction != null) + EventManager.Instance.AddListener(callbackAction); } #endregion Private Methods diff --git a/src/c#/GeneralUpdate.Maui.OSS/Internal/ParamsAndroid.cs b/src/c#/GeneralUpdate.Maui.OSS/Internal/ParamsAndroid.cs new file mode 100644 index 00000000..769dd89b --- /dev/null +++ b/src/c#/GeneralUpdate.Maui.OSS/Internal/ParamsAndroid.cs @@ -0,0 +1,15 @@ +namespace GeneralUpdate.Maui.OSS.Internal +{ + public class ParamsAndroid + { + public string Url { get; set; } + + public string Apk { get; set; } + + public string CurrentVersion { get; set; } + + public string Authority { get; set; } + + public string VersionFileName { get; set; } + } +} \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Maui.OSS/Internal/VersionInfo.cs b/src/c#/GeneralUpdate.Maui.OSS/Internal/VersionInfo.cs new file mode 100644 index 00000000..bcd62ffa --- /dev/null +++ b/src/c#/GeneralUpdate.Maui.OSS/Internal/VersionInfo.cs @@ -0,0 +1,37 @@ +using System.Text.Json.Serialization; + +namespace GeneralUpdate.Maui.OSS.Internal +{ + public class VersionInfo + { + /// + /// Update package release time. + /// + [JsonPropertyName("PubTime")] + public long PubTime { get; set; } + + /// + /// Update package name. + /// + [JsonPropertyName("Name")] + public string Name { get; set; } + + /// + /// Compare and verify with the downloaded update package. + /// + [JsonPropertyName("Hash")] + public string Hash { get; set; } + + /// + /// The version number. + /// + [JsonPropertyName("Version")] + public string Version { get; set; } + + /// + /// Remote service url address. + /// + [JsonPropertyName("Url")] + public string Url { get; set; } + } +} diff --git a/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs b/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs index 0921f25d..130b6341 100644 --- a/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs +++ b/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs @@ -1,16 +1,14 @@ using Android.Content; using Android.OS; -using GeneralUpdate.Core.Domain.PO; -using GeneralUpdate.Core.Events; -using GeneralUpdate.Core.Events.CommonArgs; -using GeneralUpdate.Core.Events.OSSArgs; -using GeneralUpdate.Maui.OSS.Domain.Entity; +using GeneralUpdate.Common.Internal; +using GeneralUpdate.Common.Internal.Event; +using GeneralUpdate.Maui.OSS.Internal; using GeneralUpdate.Maui.OSS.Strategys; using Java.IO; using Java.Math; using Java.Security; -using Newtonsoft.Json; using System.Text; +using System.Text.Json; namespace GeneralUpdate.Maui.OSS { @@ -19,19 +17,13 @@ namespace GeneralUpdate.Maui.OSS /// public class Strategy : AbstractStrategy { - #region Private Members - private readonly string _appPath = FileSystem.AppDataDirectory; private ParamsAndroid _parameter; - #endregion Private Members - #region Public Methods public override void Create(T parameter) - { - _parameter = parameter as ParamsAndroid; - } + => _parameter = parameter as ParamsAndroid; public override async Task Execute() { @@ -41,14 +33,14 @@ public override async Task Execute() var jsonUrl = $"{_parameter.Url}/{_parameter.VersionFileName}"; var jsonPath = Path.Combine(_appPath, _parameter.VersionFileName); await DownloadFileAsync(jsonUrl, jsonPath, (readLength, totalLength) - => EventManager.Instance.Dispatch>(this, new OSSDownloadArgs(readLength, totalLength))); + => EventManager.Instance.Dispatch(this, new OSSDownloadArgs(readLength, totalLength))); var jsonFile = new Java.IO.File(jsonPath); if (!jsonFile.Exists()) throw new Java.IO.FileNotFoundException(jsonPath); //2.Parse the JSON version configuration file content. byte[] jsonBytes = ReadFile(jsonFile); string json = Encoding.Default.GetString(jsonBytes); - var versionConfig = JsonConvert.DeserializeObject(json); + var versionConfig = JsonSerializer.Deserialize(json); if (versionConfig == null) throw new NullReferenceException(nameof(versionConfig)); //3.Compare with the latest version. @@ -59,7 +51,7 @@ await DownloadFileAsync(jsonUrl, jsonPath, (readLength, totalLength) //4.Download the apk file. var file = Path.Combine(_appPath, _parameter.Apk); await DownloadFileAsync(versionConfig.Url, file, (readLength, totalLength) - => EventManager.Instance.Dispatch>(this, new OSSDownloadArgs(readLength, totalLength))); + => EventManager.Instance.Dispatch(this, new OSSDownloadArgs(readLength, totalLength))); var apkFile = new Java.IO.File(file); if (!apkFile.Exists()) throw new Java.IO.FileNotFoundException(jsonPath); if (!versionConfig.Hash.Equals(GetFileMD5(apkFile, 64))) throw new Exception("The apk MD5 value does not match !"); @@ -81,7 +73,7 @@ await DownloadFileAsync(versionConfig.Url, file, (readLength, totalLength) } catch (Exception ex) { - EventManager.Instance.Dispatch>(this, new ExceptionEventArgs(ex)); + EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex)); } } diff --git a/src/c#/GeneralUpdate.Maui.OSS/Strategys/AbstractStrategy.cs b/src/c#/GeneralUpdate.Maui.OSS/Strategys/AbstractStrategy.cs index 6a4c6036..c0b2280e 100644 --- a/src/c#/GeneralUpdate.Maui.OSS/Strategys/AbstractStrategy.cs +++ b/src/c#/GeneralUpdate.Maui.OSS/Strategys/AbstractStrategy.cs @@ -11,7 +11,6 @@ public abstract class AbstractStrategy : IStrategy /// remote service address /// download file path. /// progress report. - /// progress report. /// public async Task DownloadFileAsync(string url, string filePath, Action action) { diff --git a/src/c#/GeneralUpdate.OSSClient/MainPage.xaml.cs b/src/c#/GeneralUpdate.OSSClient/MainPage.xaml.cs index 3e88d1bc..34b75621 100644 --- a/src/c#/GeneralUpdate.OSSClient/MainPage.xaml.cs +++ b/src/c#/GeneralUpdate.OSSClient/MainPage.xaml.cs @@ -1,7 +1,6 @@ -using GeneralUpdate.Core.Events.CommonArgs; -using GeneralUpdate.Core.Events.OSSArgs; +using GeneralUpdate.Common.Internal; using GeneralUpdate.Maui.OSS; -using GeneralUpdate.Maui.OSS.Domain.Entity; +using GeneralUpdate.Maui.OSS.Internal; namespace GeneralUpdate.OSSClient { @@ -16,14 +15,17 @@ private void OnCounterClicked(object sender, EventArgs e) { Task.Run(async () => { - var url = "http://192.168.50.203"; - var apk = "com.companyname.generalupdate.ossclient.apk"; - var authority = "com.generalupdate.oss.fileprovider"; - var currentVersion = "1.0.0.0"; - var versionFileName = "version.json"; + var paramsAndroid = new ParamsAndroid + { + Url = "http://192.168.50.203", + Apk = "com.companyname.generalupdate.ossclient.apk", + Authority = "com.generalupdate.oss.fileprovider", + CurrentVersion = "1.0.0.0", + VersionFileName = "version.json" + }; GeneralUpdateOSS.AddListenerDownloadProcess(OnOSSDownload); GeneralUpdateOSS.AddListenerException(OnException); - await GeneralUpdateOSS.Start(new ParamsAndroid(url, apk, authority, currentVersion, versionFileName)); + await GeneralUpdateOSS.Start(paramsAndroid); }); }