Skip to content

Commit 4664110

Browse files
committed
Use new IInfoBarService in GamePageViewModel
1 parent 1897550 commit 4664110

1 file changed

Lines changed: 24 additions & 31 deletions

File tree

src/Codebreaker.ViewModels/Pages/GamePageViewModel.cs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.Extensions.Options;
1+
using Codebreaker.ViewModels.Components;
2+
using Codebreaker.ViewModels.Contracts.Services;
3+
using Microsoft.Extensions.Options;
24

35
namespace Codebreaker.ViewModels;
46

@@ -18,29 +20,30 @@ public enum GameMoveValue
1820
}
1921

2022
/// <summary>
21-
/// Configure to enable dialogs (via <see cref="IDialogService"/>), or use the <see cref="InfoBarMessageService"/>.
23+
/// Configure to enable dialogs (via <see cref="IDialogService"/>), or use the <see cref="InfoBarService"/>.
2224
/// </summary>
2325
public class GamePageViewModelOptions
2426
{
25-
public bool EnableDialogs { get; set; } = false;
2627
}
2728

2829
public partial class GamePageViewModel : ObservableObject
2930
{
3031
private readonly IGamesClient _client;
3132
private int _moveNumber = 0;
3233

33-
private readonly bool _enableDialogs = false;
3434
private readonly IDialogService _dialogService;
35+
private readonly IInfoBarService _infoBarService;
3536

3637
public GamePageViewModel(
3738
IGamesClient client,
3839
IOptions<GamePageViewModelOptions> options,
39-
IDialogService dialogService)
40+
IDialogService dialogService,
41+
IInfoBarService infoBarService
42+
)
4043
{
4144
_client = client;
4245
_dialogService = dialogService;
43-
_enableDialogs = options.Value.EnableDialogs;
46+
_infoBarService = infoBarService;
4447

4548
PropertyChanged += (sender, e) =>
4649
{
@@ -52,13 +55,13 @@ public GamePageViewModel(
5255
/// <summary>
5356
/// Information on the game - messages, errors, etc. See <see cref="InfoBarMessageService"/>.
5457
/// </summary>
55-
public InfoBarMessageService InfoBarMessageService { get; } = new();
58+
public InfoBarService InfoBarMessageService { get; } = new();
5659

57-
private Models.Game? _game;
60+
private Game? _game;
5861
/// <summary>
5962
/// <see cref="Models.Game"/> instance."/>
6063
/// </summary>
61-
public Models.Game? Game
64+
public Game? Game
6265
{
6366
get => _game;
6467
set
@@ -141,16 +144,15 @@ private async Task StartGameAsync()
141144
}
142145
catch (Exception ex)
143146
{
144-
InfoMessageViewModel message = InfoMessageViewModel.Error(ex.Message);
145-
message.ActionCommand = new RelayCommand(() =>
146-
{
147-
GameStatus = GameMode.NotRunning;
148-
message.Close();
149-
});
150-
InfoBarMessageService.ShowMessage(message);
151-
152-
if (_enableDialogs)
153-
await _dialogService.ShowMessageAsync(ex.Message);
147+
_infoBarService.New
148+
.IsErrorMessage()
149+
.WithMessage(ex.Message)
150+
.WithAction((message) =>
151+
{
152+
GameStatus = GameMode.NotRunning;
153+
message.Close();
154+
})
155+
.Show();
154156
}
155157
finally
156158
{
@@ -217,26 +219,17 @@ private async Task SetMoveAsync()
217219
if (isVictory)
218220
{
219221
GameStatus = GameMode.Won;
220-
InfoBarMessageService.ShowInformation("Congratulations - you won!");
221-
222-
if (_enableDialogs)
223-
await _dialogService.ShowMessageAsync("Congratulations - you won!");
222+
InfoBarMessageService.New.IsSuccessMessage().WithMessage("Congratulations - you won!").Show();
224223
}
225224
else if (ended)
226225
{
227226
GameStatus = GameMode.Lost;
228-
InfoBarMessageService.ShowInformation("Sorry, you didn't find the matching colors!");
229-
230-
if (_enableDialogs)
231-
await _dialogService.ShowMessageAsync("Sorry, you didn't find the matching colors!");
227+
InfoBarMessageService.New.WithMessage("Sorry, you didn't find the matching colors!").Show();
232228
}
233229
}
234230
catch (Exception ex)
235231
{
236-
InfoBarMessageService.ShowError(ex.Message);
237-
238-
if (_enableDialogs)
239-
await _dialogService.ShowMessageAsync(ex.Message);
232+
InfoBarMessageService.New.WithMessage(ex.Message).IsErrorMessage().Show();
240233
}
241234
finally
242235
{

0 commit comments

Comments
 (0)