-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: project/PH1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need |
||
_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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
There was a problem hiding this comment.
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'