|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using System.Reactive; |
| 3 | +using System.Reactive.Disposables; |
| 4 | +using ByteSync.Interfaces; |
| 5 | +using ByteSync.Interfaces.Repositories; |
| 6 | +using ReactiveUI; |
| 7 | +using ReactiveUI.Fody.Helpers; |
| 8 | + |
| 9 | +namespace ByteSync.ViewModels.Announcements; |
| 10 | + |
| 11 | +public class AnnouncementViewModel : ActivatableViewModelBase |
| 12 | +{ |
| 13 | + private readonly IAnnouncementRepository _announcementRepository; |
| 14 | + private readonly ILocalizationService _localizationService; |
| 15 | + private readonly IApplicationSettingsRepository _applicationSettingsRepository; |
| 16 | + |
| 17 | + public AnnouncementViewModel() |
| 18 | + { |
| 19 | + Announcements = new ObservableCollection<AnnouncementItemViewModel>(); |
| 20 | + } |
| 21 | + |
| 22 | + public AnnouncementViewModel(IAnnouncementRepository announcementRepository, |
| 23 | + ILocalizationService localizationService, |
| 24 | + IApplicationSettingsRepository applicationSettingsRepository) : this() |
| 25 | + { |
| 26 | + _announcementRepository = announcementRepository; |
| 27 | + _localizationService = localizationService; |
| 28 | + _applicationSettingsRepository = applicationSettingsRepository; |
| 29 | + |
| 30 | + AcknowledgeAnnouncementCommand = ReactiveCommand.Create<string>(AcknowledgeAnnouncement); |
| 31 | + |
| 32 | + this.WhenActivated(disposables => |
| 33 | + { |
| 34 | + _announcementRepository.ObservableCache |
| 35 | + .Connect() |
| 36 | + .Subscribe(_ => Refresh()) |
| 37 | + .DisposeWith(disposables); |
| 38 | + |
| 39 | + _localizationService.CurrentCultureObservable |
| 40 | + .Subscribe(_ => Refresh()) |
| 41 | + .DisposeWith(disposables); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + public ObservableCollection<AnnouncementItemViewModel> Announcements { get; } |
| 46 | + |
| 47 | + public ReactiveCommand<string, Unit> AcknowledgeAnnouncementCommand { get; } |
| 48 | + |
| 49 | + [Reactive] |
| 50 | + public bool IsVisible { get; private set; } |
| 51 | + |
| 52 | + private void Refresh() |
| 53 | + { |
| 54 | + var cultureCode = _localizationService.CurrentCultureDefinition.Code; |
| 55 | + var applicationSettings = _applicationSettingsRepository.GetCurrentApplicationSettings(); |
| 56 | + var acknowledgedIds = applicationSettings.DecodedAcknowledgedAnnouncementIds; |
| 57 | + |
| 58 | + var unacknowledgedAnnouncements = _announcementRepository.Elements |
| 59 | + .Where(a => !acknowledgedIds.Contains(a.Id)) |
| 60 | + .Select(a => new AnnouncementItemViewModel |
| 61 | + { |
| 62 | + Id = a.Id, |
| 63 | + Message = a.Message.TryGetValue(cultureCode, out var msg) |
| 64 | + ? msg |
| 65 | + : a.Message.Values.FirstOrDefault() ?? string.Empty |
| 66 | + }) |
| 67 | + .ToList(); |
| 68 | + |
| 69 | + Announcements.Clear(); |
| 70 | + foreach (var announcement in unacknowledgedAnnouncements) |
| 71 | + { |
| 72 | + Announcements.Add(announcement); |
| 73 | + } |
| 74 | + |
| 75 | + IsVisible = Announcements.Count > 0; |
| 76 | + } |
| 77 | + |
| 78 | + private void AcknowledgeAnnouncement(string announcementId) |
| 79 | + { |
| 80 | + _applicationSettingsRepository.UpdateCurrentApplicationSettings(settings => |
| 81 | + { |
| 82 | + settings.AddAcknowledgedAnnouncementId(announcementId); |
| 83 | + }, true); |
| 84 | + |
| 85 | + Refresh(); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +public class AnnouncementItemViewModel |
| 90 | +{ |
| 91 | + public string Id { get; set; } = string.Empty; |
| 92 | + public string Message { get; set; } = string.Empty; |
| 93 | +} |
0 commit comments