| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeZoneChangedBehavior.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Globalization.DateTime; |
| | | 9 | | using MyNet.Observable.Behaviors.Metadata.Features.Events; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Observable.Behaviors; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Defines a behavior that listens for changes in the system time zone and raises an event when the time zone changes. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class TimeZoneChangedBehavior : EventBehavior<ObservableObject, TimeZoneChangedEvent> |
| | | 17 | | { |
| | | 18 | | private readonly ITimeZoneService _service; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="TimeZoneChangedBehavior"/> class with the specified owner and time |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="owner">The owner object of this behavior.</param> |
| | | 24 | | /// <param name="timeZoneService">The time zone service to listen for changes.</param> |
| | | 25 | | /// <exception cref="ArgumentNullException">Thrown if the time zone service is null.</exception> |
| | | 26 | | public TimeZoneChangedBehavior(ObservableObject owner, ITimeZoneService timeZoneService) |
| | 6 | 27 | | : base(owner) |
| | | 28 | | { |
| | 6 | 29 | | _service = timeZoneService ?? throw new ArgumentNullException(nameof(timeZoneService)); |
| | 6 | 30 | | _service.TimeZoneChanged += OnTimeZoneChangedCallback; |
| | 6 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Callback method that is invoked when the system time zone changes. This method checks if the behavior is dispose |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="sender">The source of the event.</param> |
| | | 37 | | /// <param name="e">The event data containing the new time zone information.</param> |
| | | 38 | | private void OnTimeZoneChangedCallback(object? sender, TimeZoneChangedEventArgs e) |
| | | 39 | | { |
| | 3 | 40 | | if (IsDisposed || IsSuspended) |
| | 0 | 41 | | return; |
| | | 42 | | |
| | 3 | 43 | | RaiseEvent(new(e.NewTimeZone)); |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | | 47 | | protected override void DisposeManagedResources() |
| | | 48 | | { |
| | 3 | 49 | | _service.TimeZoneChanged -= OnTimeZoneChangedCallback; |
| | 3 | 50 | | base.DisposeManagedResources(); |
| | 3 | 51 | | } |
| | | 52 | | } |
| | | 53 | | |