Caching initialization attempt, bool.Parse in WASM

This commit is contained in:
Martin Zikmund 2019-06-13 11:31:01 +02:00
Родитель 9780c611b9
Коммит cc87a487a8
2 изменённых файлов: 11 добавлений и 8 удалений

Просмотреть файл

@ -12,17 +12,17 @@ namespace Windows.Phone.Devices.Notification
public partial class VibrationDevice public partial class VibrationDevice
{ {
private const string Permission = "android.permission.VIBRATE"; private const string Permission = "android.permission.VIBRATE";
private readonly Vibrator _vibrator;
private static VibrationDevice _instance; private static VibrationDevice _instance;
private static bool _initializationAttempted;
private readonly Vibrator _vibrator;
private VibrationDevice(Vibrator vibrator) => private VibrationDevice(Vibrator vibrator) =>
_vibrator = vibrator; _vibrator = vibrator;
public static VibrationDevice GetDefault() public static VibrationDevice GetDefault()
{ {
if (_instance == null) if (!_initializationAttempted && _instance == null)
{ {
if (ContextCompat.CheckSelfPermission(Application.Context, Permission) == Android.Content.PM.Permission.Denied) if (ContextCompat.CheckSelfPermission(Application.Context, Permission) == Android.Content.PM.Permission.Denied)
{ {
@ -32,7 +32,8 @@ namespace Windows.Phone.Devices.Notification
if (vibrator != null && vibrator.HasVibrator) if (vibrator != null && vibrator.HasVibrator)
{ {
_instance = new VibrationDevice(vibrator); _instance = new VibrationDevice(vibrator);
} }
_initializationAttempted = true;
} }
return _instance; return _instance;
} }

Просмотреть файл

@ -9,6 +9,7 @@ namespace Windows.Phone.Devices.Notification
{ {
private const string JsType = "Windows.Phone.Devices.Notification.VibrationDevice"; private const string JsType = "Windows.Phone.Devices.Notification.VibrationDevice";
private static VibrationDevice _instance = null; private static VibrationDevice _instance = null;
private static bool _initializationAttempted = false;
private VibrationDevice() private VibrationDevice()
{ {
@ -16,14 +17,15 @@ namespace Windows.Phone.Devices.Notification
public static VibrationDevice GetDefault() public static VibrationDevice GetDefault()
{ {
if (_instance == null) if (!_initializationAttempted && _instance == null)
{ {
var command = $"{JsType}.initialize()"; var command = $"{JsType}.initialize()";
var initialized = Uno.Foundation.WebAssemblyRuntime.InvokeJS(command); var initialized = Uno.Foundation.WebAssemblyRuntime.InvokeJS(command);
if (initialized.Equals("true", StringComparison.InvariantCultureIgnoreCase)) if (bool.Parse(initialized) == true)
{ {
_instance = new VibrationDevice(); _instance = new VibrationDevice();
} }
_initializationAttempted = true;
} }
return _instance; return _instance;
} }