Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connectivity android #568

Open
wants to merge 4 commits into
base: project/PH1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
<ProjectReference Include="..\Softeq.XToolkit.Common\Softeq.XToolkit.Common.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Process" Version="2.6.2.1" />
Copy link
Member

@wcoder wcoder Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason for Common projects is 'no 3rd-party dependencies'

</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions Softeq.XToolkit.Connectivity.Droid/DroidConnectivityService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Droid implementation of <see cref="DroidConnectivityService"/>.
/// </summary>
public class DroidConnectivityService : EssentialsConnectivityService
{
private readonly AppLifecycleObserver _lifecycleObserver;
private readonly WeakAction _startAction;

/// <summary>
/// Initializes a new instance of the <see cref="DroidConnectivityService"/> class.
/// </summary>
/// <param name="connectivity">
/// Custom instance of <see cref="T:Microsoft.Maui.Networking.IConnectivity"/>
/// or you can use <see cref="Default"/> static method.
/// </param>
public DroidConnectivityService(IConnectivity connectivity) : base(connectivity)
{
_startAction = new WeakAction(OnAppStart);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need Lifecycle.Event.ON_CREATE or ON_RESUME also?

_lifecycleObserver = new AppLifecycleObserver(startAction: _startAction);
Execute.BeginOnUIThread(() =>
{
ProcessLifecycleOwner.Get().Lifecycle.AddObserver(_lifecycleObserver);
});
}

~DroidConnectivityService()
{
Dispose(false);
}

/// <summary>
/// Releases the unmanaged and optionally the managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to dispose managed state.</param>
/// <seealso cref="Dispose"/>
/// <seealso cref="T:System.IDisposable"/>
protected virtual void Dispose(bool disposing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add additional public method:

public void Dispose()
{
    Dispose(true);

    GC.SuppressFinalize(this);
}

to be able to remove observer from outside

{
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary to manually trigger an event only on Start at this place?

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Softeq.XToolkit.Common.Droid\Softeq.XToolkit.Common.Droid.csproj" />
<ProjectReference Include="..\Softeq.XToolkit.Connectivity\Softeq.XToolkit.Connectivity.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Essentials" Version="$(MauiVersion)" />
</ItemGroup>
</Project>
18 changes: 9 additions & 9 deletions Softeq.XToolkit.Connectivity/EssentialsConnectivityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Softeq.XToolkit.Connectivity
/// </summary>
public class EssentialsConnectivityService : IConnectivityService, IDisposable
{
private readonly IConnectivity _connectivity;

/// <summary>
/// Initializes a new instance of the <see cref="EssentialsConnectivityService"/> class.
Expand All @@ -24,9 +23,8 @@ public class EssentialsConnectivityService : IConnectivityService, IDisposable
/// </param>
public EssentialsConnectivityService(IConnectivity connectivity)
{
_connectivity = connectivity;

_connectivity.ConnectivityChanged += CurrentConnectivityChanged;
Connectivity = connectivity;
Connectivity.ConnectivityChanged += CurrentConnectivityChanged;
}

~EssentialsConnectivityService()
Expand All @@ -37,13 +35,15 @@ public EssentialsConnectivityService(IConnectivity connectivity)
/// <inheritdoc />
public event EventHandler<ConnectivityChangedEventArgs>? ConnectivityChanged;

protected IConnectivity Connectivity { get; private set; }

/// <inheritdoc />
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;
Expand All @@ -53,7 +53,7 @@ public virtual bool IsConnected
}

/// <inheritdoc />
public IEnumerable<ConnectionProfile> ConnectionProfiles => _connectivity.ConnectionProfiles;
public IEnumerable<ConnectionProfile> ConnectionProfiles => Connectivity.ConnectionProfiles;

/// <summary>
/// Initializes a new instance of the <see cref="EssentialsConnectivityService"/> class
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Softeq.XToolkit.Common.Droid\Softeq.XToolkit.Common.Droid.csproj" />
<ProjectReference Include="..\Softeq.XToolkit.Common\Softeq.XToolkit.Common.csproj" />
<ProjectReference Include="..\Softeq.XToolkit.PushNotifications\Softeq.XToolkit.PushNotifications.csproj" />
<ProjectReference Include="..\Softeq.XToolkit.WhiteLabel\Softeq.XToolkit.WhiteLabel.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.5.0" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Process" Version="2.6.2.1" />
<PackageReference Include="Xamarin.Firebase.Messaging" Version="123.1.2.1" />
<PackageReference Include="Xamarin.ShortcutBadger" Version="1.1.21.80" />
</ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions XToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Loading