-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from neozhu/feature/onofflinestate
Add Online/Offline Status Display
- Loading branch information
Showing
11 changed files
with
367 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/CleanAspire.ClientApp/Services/JsInterop/OnlineStatusService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.JSInterop; | ||
|
||
namespace CleanAspire.ClientApp.Services.JsInterop; | ||
|
||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
public class OnlineStatusService : IAsyncDisposable | ||
{ | ||
private readonly IJSRuntime _jsRuntime; | ||
private IJSObjectReference? _jsModule; | ||
private DotNetObjectReference<OnlineStatusService>? _dotNetRef; | ||
|
||
public event Action<bool>? OnlineStatusChanged; | ||
|
||
public OnlineStatusService(IJSRuntime jsRuntime) | ||
{ | ||
_jsRuntime = jsRuntime; | ||
} | ||
public async Task InitializeAsync() | ||
{ | ||
_jsModule = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "/js/onlinestatus.js"); | ||
_dotNetRef = DotNetObjectReference.Create(this); | ||
} | ||
|
||
public async Task<bool> GetOnlineStatusAsync() | ||
{ | ||
if (_jsModule == null) | ||
{ | ||
throw new InvalidOperationException("JavaScript module is not initialized. Call InitializeAsync first."); | ||
} | ||
return await _jsModule.InvokeAsync<bool>("getOnlineStatus"); | ||
} | ||
public async Task RegisterOnlineStatusListenerAsync() | ||
{ | ||
if (_jsModule == null) | ||
{ | ||
throw new InvalidOperationException("JavaScript module is not initialized. Call InitializeAsync first."); | ||
} | ||
await _jsModule.InvokeVoidAsync("addOnlineStatusListener", _dotNetRef); | ||
} | ||
|
||
[JSInvokable] | ||
public void UpdateOnlineStatus(bool isOnline) | ||
{ | ||
OnlineStatusChanged?.Invoke(isOnline); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (_dotNetRef != null) | ||
{ | ||
_dotNetRef.Dispose(); | ||
} | ||
if (_jsModule != null) | ||
{ | ||
await _jsModule.DisposeAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function getOnlineStatus() { | ||
return navigator.onLine; | ||
} | ||
export function addOnlineStatusListener(dotNetObjectRef) { | ||
window.addEventListener('online', () => { | ||
dotNetObjectRef.invokeMethodAsync('UpdateOnlineStatus', true); | ||
}); | ||
window.addEventListener('offline', () => { | ||
dotNetObjectRef.invokeMethodAsync('UpdateOnlineStatus', false); | ||
}); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.