diff --git a/PhotoCollageWeb/Pages/Collage.razor.cs b/PhotoCollageWeb/Pages/Collage.razor.cs index 7b71dcb..671db52 100644 --- a/PhotoCollageWeb/Pages/Collage.razor.cs +++ b/PhotoCollageWeb/Pages/Collage.razor.cs @@ -31,6 +31,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender) if (firstRender) { this.InitializeCollage(); + this.ShowNextPhoto(); + this.StateHasChanged(); this.TryStartTimer(); } @@ -40,6 +42,25 @@ protected override async Task OnAfterRenderAsync(bool firstRender) private async void OnTimerInterval(object sender, ElapsedEventArgs e) { this.TryStopTimer(); + this.ShowNextPhoto(); + this.TryStartTimer(); + await this.InvokeAsync(() => this.StateHasChanged()); + } + + private void InitializeCollage() + { + this.photoRepository = new PhotoRepositoryFactory(this.Settings).Make(); + var speed = (int)this.Settings.Speed; + this.timer ??= new Timer + { + Interval = TimeSpan.FromSeconds(speed).TotalMilliseconds, + AutoReset = true + }; + this.timer.Elapsed += this.OnTimerInterval; + } + + private void ShowNextPhoto() + { if (this.photoRepository.HasPhotos) { var path = this.photoRepository.GetNextPhotoFilePath(); @@ -54,8 +75,7 @@ private async void OnTimerInterval(object sender, ElapsedEventArgs e) if (this.images.Count > (this.Settings.NumberOfPhotos + 1)) { - var removed = this.images.Dequeue(); - removed = null; + _ = this.images.Dequeue(); } if (this.images.Count > this.Settings.NumberOfPhotos) @@ -64,27 +84,13 @@ private async void OnTimerInterval(object sender, ElapsedEventArgs e) faded.IsRemoved = true; } } - this.TryStartTimer(); - await this.InvokeAsync(() => this.StateHasChanged()); - } - - private void InitializeCollage() - { - this.photoRepository = new PhotoRepositoryFactory(this.Settings).Make(); - var speed = (int)this.Settings.Speed; - this.timer ??= new Timer - { - Interval = TimeSpan.FromSeconds(speed).TotalMilliseconds, - AutoReset = true - }; - this.timer.Elapsed += this.OnTimerInterval; } private void TryStartTimer() { if (this.timer != null) { - this.timer.Enabled = true; + this.timer.Start(); } } @@ -92,7 +98,7 @@ private void TryStopTimer() { if (this.timer != null) { - this.timer.Enabled = false; + this.timer.Stop(); } } }