diff --git a/Softeq.XToolkit.PushNotifications.Droid/AppLifecycleObserver.cs b/Softeq.XToolkit.Common.Droid/AppLifecycleObserver.cs similarity index 55% rename from Softeq.XToolkit.PushNotifications.Droid/AppLifecycleObserver.cs rename to Softeq.XToolkit.Common.Droid/AppLifecycleObserver.cs index 159e6f172..ba2bdcee6 100644 --- a/Softeq.XToolkit.PushNotifications.Droid/AppLifecycleObserver.cs +++ b/Softeq.XToolkit.Common.Droid/AppLifecycleObserver.cs @@ -4,25 +4,37 @@ using AndroidX.Lifecycle; using Java.Interop; using Java.Lang; +using Softeq.XToolkit.Common.Weak; -namespace Softeq.XToolkit.PushNotifications.Droid +namespace Softeq.XToolkit.Common.Droid { public class AppLifecycleObserver : Object, ILifecycleObserver { - public bool IsForegrounded { get; private set; } + private readonly WeakAction? _startAction; + private readonly WeakAction? _stopAction; - [Lifecycle.Event.OnStop] - [Export] - public void Stopped() + public AppLifecycleObserver(WeakAction? startAction = default, WeakAction? stopAction = default) { - IsForegrounded = false; + _startAction = startAction; + _stopAction = stopAction; } + public bool IsForegrounded { get; private set; } + [Lifecycle.Event.OnStart] [Export] public void Started() { IsForegrounded = true; + _startAction?.Execute(); + } + + [Lifecycle.Event.OnStop] + [Export] + public void Stopped() + { + IsForegrounded = false; + _stopAction?.Execute(); } } } diff --git a/Softeq.XToolkit.Common.Droid/Softeq.XToolkit.Common.Droid.csproj b/Softeq.XToolkit.Common.Droid/Softeq.XToolkit.Common.Droid.csproj index 60be0cbf5..e0a9f8b4a 100644 --- a/Softeq.XToolkit.Common.Droid/Softeq.XToolkit.Common.Droid.csproj +++ b/Softeq.XToolkit.Common.Droid/Softeq.XToolkit.Common.Droid.csproj @@ -18,4 +18,8 @@ + + + + \ No newline at end of file diff --git a/Softeq.XToolkit.Connectivity.Droid/DroidConnectivityService.cs b/Softeq.XToolkit.Connectivity.Droid/DroidConnectivityService.cs new file mode 100644 index 000000000..7e1d63e7e --- /dev/null +++ b/Softeq.XToolkit.Connectivity.Droid/DroidConnectivityService.cs @@ -0,0 +1,66 @@ +// Developed by Softeq Development Corporation +// http://www.softeq.com + +using AndroidX.Lifecycle; +using Microsoft.Maui.Networking; +using Softeq.XToolkit.Common.Droid; +using Softeq.XToolkit.Common.Threading; +using Softeq.XToolkit.Common.Weak; + +namespace Softeq.XToolkit.Connectivity.Droid +{ + /// + /// Droid implementation of . + /// + public class DroidConnectivityService : EssentialsConnectivityService + { + private readonly AppLifecycleObserver _lifecycleObserver; + private readonly WeakAction _startAction; + + /// + /// Initializes a new instance of the class. + /// + /// + /// Custom instance of + /// or you can use static method. + /// + public DroidConnectivityService(IConnectivity connectivity) : base(connectivity) + { + _startAction = new WeakAction(OnAppStart); + _lifecycleObserver = new AppLifecycleObserver(startAction: _startAction); + Execute.BeginOnUIThread(() => + { + ProcessLifecycleOwner.Get().Lifecycle.AddObserver(_lifecycleObserver); + }); + } + + ~DroidConnectivityService() + { + Dispose(false); + } + + /// + /// Releases the unmanaged and optionally the managed resources. + /// + /// true to dispose managed state. + /// + /// + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + Execute.BeginOnUIThread(() => + { + ProcessLifecycleOwner.Get().Lifecycle.RemoveObserver(_lifecycleObserver); + }); + } + } + + private void OnAppStart() + { + Connectivity.ConnectivityChanged -= CurrentConnectivityChanged; + Connectivity.ConnectivityChanged += CurrentConnectivityChanged; + CurrentConnectivityChanged(this, new ConnectivityChangedEventArgs(Connectivity.NetworkAccess, Connectivity.ConnectionProfiles)); + } + } +} diff --git a/Softeq.XToolkit.Connectivity.Droid/Softeq.XToolkit.Connectivity.Droid.csproj b/Softeq.XToolkit.Connectivity.Droid/Softeq.XToolkit.Connectivity.Droid.csproj new file mode 100644 index 000000000..19d3abc2e --- /dev/null +++ b/Softeq.XToolkit.Connectivity.Droid/Softeq.XToolkit.Connectivity.Droid.csproj @@ -0,0 +1,16 @@ + + + net8.0-android + 21 + enable + enable + + + + + + + + + + \ No newline at end of file diff --git a/Softeq.XToolkit.Connectivity/EssentialsConnectivityService.cs b/Softeq.XToolkit.Connectivity/EssentialsConnectivityService.cs index e14bde167..7106afc03 100644 --- a/Softeq.XToolkit.Connectivity/EssentialsConnectivityService.cs +++ b/Softeq.XToolkit.Connectivity/EssentialsConnectivityService.cs @@ -13,7 +13,6 @@ namespace Softeq.XToolkit.Connectivity /// public class EssentialsConnectivityService : IConnectivityService, IDisposable { - private readonly IConnectivity _connectivity; /// /// Initializes a new instance of the class. @@ -24,9 +23,8 @@ public class EssentialsConnectivityService : IConnectivityService, IDisposable /// public EssentialsConnectivityService(IConnectivity connectivity) { - _connectivity = connectivity; - - _connectivity.ConnectivityChanged += CurrentConnectivityChanged; + Connectivity = connectivity; + Connectivity.ConnectivityChanged += CurrentConnectivityChanged; } ~EssentialsConnectivityService() @@ -37,13 +35,15 @@ public EssentialsConnectivityService(IConnectivity connectivity) /// public event EventHandler? ConnectivityChanged; + protected IConnectivity Connectivity { get; private set; } + /// public virtual bool IsConnected { get { - var profiles = _connectivity.ConnectionProfiles; - var access = _connectivity.NetworkAccess; + var profiles = Connectivity.ConnectionProfiles; + var access = Connectivity.NetworkAccess; var hasAnyConnection = profiles.Any(); var hasInternet = access == NetworkAccess.Internet; @@ -53,7 +53,7 @@ public virtual bool IsConnected } /// - public IEnumerable ConnectionProfiles => _connectivity.ConnectionProfiles; + public IEnumerable ConnectionProfiles => Connectivity.ConnectionProfiles; /// /// Initializes a new instance of the class @@ -79,11 +79,11 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - _connectivity.ConnectivityChanged -= CurrentConnectivityChanged; + Connectivity.ConnectivityChanged -= CurrentConnectivityChanged; } } - private void CurrentConnectivityChanged(object? sender, ConnectivityChangedEventArgs e) + protected void CurrentConnectivityChanged(object? sender, ConnectivityChangedEventArgs e) { ConnectivityChanged?.Invoke(sender, e); } diff --git a/Softeq.XToolkit.PushNotifications.Droid/Services/DroidPushNotificationsConsumer.cs b/Softeq.XToolkit.PushNotifications.Droid/Services/DroidPushNotificationsConsumer.cs index 6bb9ea248..5d38e6567 100644 --- a/Softeq.XToolkit.PushNotifications.Droid/Services/DroidPushNotificationsConsumer.cs +++ b/Softeq.XToolkit.PushNotifications.Droid/Services/DroidPushNotificationsConsumer.cs @@ -6,6 +6,7 @@ using Android.Content; using AndroidX.Lifecycle; using Firebase.Messaging; +using Softeq.XToolkit.Common.Droid; using Softeq.XToolkit.Common.Logger; using Softeq.XToolkit.Common.Threading; using Softeq.XToolkit.PushNotifications.Abstract; diff --git a/Softeq.XToolkit.PushNotifications.Droid/Softeq.XToolkit.PushNotifications.Droid.csproj b/Softeq.XToolkit.PushNotifications.Droid/Softeq.XToolkit.PushNotifications.Droid.csproj index 14bac5d70..7b9d203e3 100644 --- a/Softeq.XToolkit.PushNotifications.Droid/Softeq.XToolkit.PushNotifications.Droid.csproj +++ b/Softeq.XToolkit.PushNotifications.Droid/Softeq.XToolkit.PushNotifications.Droid.csproj @@ -15,6 +15,7 @@ + @@ -22,7 +23,6 @@ - diff --git a/XToolkit.sln b/XToolkit.sln index 11ada7ae8..6f4556356 100644 --- a/XToolkit.sln +++ b/XToolkit.sln @@ -81,6 +81,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Softeq.XToolkit.Common.iOS. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Softeq.XToolkit.Common.Droid.Tests", "Softeq.XToolkit.Common.Droid.Tests\Softeq.XToolkit.Common.Droid.Tests.csproj", "{73ABD70F-00C8-4EFC-B743-F85C1B4A50F4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Softeq.XToolkit.Connectivity.Droid", "Softeq.XToolkit.Connectivity.Droid\Softeq.XToolkit.Connectivity.Droid.csproj", "{74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Release|Any CPU = Release|Any CPU @@ -304,6 +306,14 @@ Global {73ABD70F-00C8-4EFC-B743-F85C1B4A50F4}.Debug.Droid|Any CPU.Build.0 = Debug|Any CPU {73ABD70F-00C8-4EFC-B743-F85C1B4A50F4}.Debug.iOS|iPhoneSimulator.ActiveCfg = Debug|Any CPU {73ABD70F-00C8-4EFC-B743-F85C1B4A50F4}.Debug.iOS|iPhone.ActiveCfg = Debug|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Release|Any CPU.Build.0 = Release|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Debug.Droid|Any CPU.ActiveCfg = Debug|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Debug.Droid|Any CPU.Build.0 = Debug|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Debug.iOS|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Debug.iOS|iPhoneSimulator.Build.0 = Debug|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Debug.iOS|iPhone.ActiveCfg = Debug|Any CPU + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD}.Debug.iOS|iPhone.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {24588814-B93D-4528-8917-9C2A3C4E85CA} = {2CC5305B-22A3-48B0-858A-98478AC14EF9} @@ -337,5 +347,6 @@ Global {6B7105F6-6F18-4999-BA4E-8069D92F3150} = {6A926916-1067-40F9-A266-A2F71C3B2DD4} {F0224985-D9FF-4E50-992F-C78079DC3DDB} = {2CC5305B-22A3-48B0-858A-98478AC14EF9} {73ABD70F-00C8-4EFC-B743-F85C1B4A50F4} = {2CC5305B-22A3-48B0-858A-98478AC14EF9} + {74E2AFAC-B39C-473C-8EE1-72A9F81DE5BD} = {9D45EECA-8CBD-4E56-B24F-1804F6313657} EndGlobalSection EndGlobal