PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PermissionsProvider.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Microsoft.JSInterop;
  2. using System;
  3. using System.Threading.Tasks;
  4. namespace cwebplusApp.Shared.Services {
  5. public class PermissionsProvider {
  6. private bool _isGeoLocationAllowed;
  7. public delegate void GeoLocationPermissionChangeCallBack(bool allowed);
  8. private GeoLocationPermissionChangeCallBack _geoLocationPermissionChangeCallBack;
  9. public IJSRuntime JSRuntime { get; set; }
  10. public bool IsGeoLocationAllowed {
  11. get {
  12. Console.WriteLine("IsGeoLocationAllowed.Get {0}", _isGeoLocationAllowed);
  13. return _isGeoLocationAllowed;
  14. }
  15. set {
  16. Console.WriteLine("IsGeoLocationAllowed.Set {0}", value);
  17. _isGeoLocationAllowed = value;
  18. }
  19. }
  20. public PermissionsProvider(IJSRuntime _jSRuntime) {
  21. this.JSRuntime = _jSRuntime;
  22. initialize();
  23. }
  24. [JSInvokable]
  25. public void GeoLocationPermissionChanged(string geoLocationStatus) {
  26. IsGeoLocationAllowed = (geoLocationStatus.Equals("granted") || geoLocationStatus.Equals("prompt")) ? true : false;
  27. this._geoLocationPermissionChangeCallBack(IsGeoLocationAllowed);
  28. }
  29. public void SetGeoLocationPermissionChangeCallBack(GeoLocationPermissionChangeCallBack callback) {
  30. this._geoLocationPermissionChangeCallBack = callback;
  31. }
  32. public void RemoveOnlineStatusChangeCallBack() {
  33. this._geoLocationPermissionChangeCallBack = null;
  34. }
  35. private async void initialize() {
  36. Console.WriteLine("PermissionsProvider.initialize");
  37. await GeoLocationAllowed();
  38. }
  39. private async Task<bool> GeoLocationAllowed() {
  40. Console.WriteLine("PermissionsProvider.GeoLocationAllowed");
  41. var dotNetObjRef = DotNetObjectReference.Create(this);
  42. string result = await JSRuntime.InvokeAsync<string>("IsGeoLocationAllowed", dotNetObjRef);
  43. this.IsGeoLocationAllowed = result.Equals("granted") || result.Equals("prompt");
  44. Console.WriteLine("PermissionsProvider.GeoLocationAllowed result={0}", result);
  45. return IsGeoLocationAllowed;
  46. }
  47. }
  48. }