Skip to content

Commit

Permalink
removed wait time for first photo
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyknight-me committed Jan 23, 2021
1 parent 0febc2a commit 5aa5355
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions PhotoCollageWeb/Pages/Collage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
if (firstRender)
{
this.InitializeCollage();
this.ShowNextPhoto();
this.StateHasChanged();
this.TryStartTimer();
}

Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -64,35 +84,21 @@ 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();
}
}

private void TryStopTimer()
{
if (this.timer != null)
{
this.timer.Enabled = false;
this.timer.Stop();
}
}
}
Expand Down

0 comments on commit 5aa5355

Please sign in to comment.