diff --git a/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Controls/AdDialog.axaml.cs b/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Controls/AdDialog.axaml.cs index a2c367204..d49a13ff5 100644 --- a/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Controls/AdDialog.axaml.cs +++ b/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Controls/AdDialog.axaml.cs @@ -1,6 +1,9 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.LogicalTree; using Avalonia.Markup.Xaml; +using DynamicData.Binding; +using System.Application.Services; namespace System.Application.UI.Views.Controls { @@ -36,14 +39,44 @@ namespace System.Application.UI.Views.Controls closeButton.Click += (s, e) => { - this.IsVisible = false; + RemoveAd(); }; } + + UserService.Current.WhenValueChanged(x => x.User, false) + .Subscribe(_ => Check()); + + AdvertiseService.Current.WhenValueChanged(x => x.Advertisements, false) + .Subscribe(_ => Check()); } private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + + Check(); + } + + void Check() + { + if (UserService.Current.User != null && UserService.Current.IsSponsorUser) + { + RemoveAd(); + } + if (AdvertiseService.Current.IsInitialized && !AdvertiseService.Current.Advertisements.Any_Nullable()) + { + RemoveAd(); + } + } + + void RemoveAd() + { + this.IsVisible = false; + } } } diff --git a/src/ST.Client.Desktop.Avalonia/Application/UI/Views/MainView.axaml b/src/ST.Client.Desktop.Avalonia/Application/UI/Views/MainView.axaml index f077bee28..c7ed604e0 100644 --- a/src/ST.Client.Desktop.Avalonia/Application/UI/Views/MainView.axaml +++ b/src/ST.Client.Desktop.Avalonia/Application/UI/Views/MainView.axaml @@ -141,7 +141,7 @@ Foreground="{DynamicResource HighlightForegroundBrushKey}" Classes="h5" VerticalAlignment="Center"/> - + diff --git a/src/ST.Client/Services/Mvvm/AdvertiseService.cs b/src/ST.Client/Services/Mvvm/AdvertiseService.cs index 56d1abb32..46054758a 100644 --- a/src/ST.Client/Services/Mvvm/AdvertiseService.cs +++ b/src/ST.Client/Services/Mvvm/AdvertiseService.cs @@ -8,6 +8,7 @@ using DynamicData; using System.Collections.ObjectModel; using System.Reactive.Linq; using DynamicData.Binding; +using ReactiveUI.Fody.Helpers; namespace System.Application.Services { @@ -23,6 +24,9 @@ namespace System.Application.Services public ReadOnlyObservableCollection Advertisements => _Advertisements; + [Reactive] + public bool IsInitialized { get; set; } + private AdvertiseService() { mCurrent = this; @@ -41,17 +45,22 @@ namespace System.Application.Services public async void InitAdvertise() { - var client = ICloudServiceClient.Instance.Advertisement; - var result = await client.All(); + if (IsInitialized == false) + { + var client = ICloudServiceClient.Instance.Advertisement; + var result = await client.All(); - if (result.IsSuccess && result.Content != null) - { - AdvertisementsSource.Clear(); - AdvertisementsSource.AddRange(result.Content); - } - else - { - Log.Error(nameof(InitAdvertise), result.Message); + if (result.IsSuccess && result.Content != null) + { + AdvertisementsSource.Clear(); + AdvertisementsSource.AddRange(result.Content); + } + else + { + Log.Error(nameof(InitAdvertise), result.Message); + } + + IsInitialized = true; } }