Skip to content

Commit 1897550

Browse files
committed
Added InfoBar-related code to VM
1 parent 9c64dba commit 1897550

4 files changed

Lines changed: 122 additions & 90 deletions

File tree

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Windows.Input;
2-
3-
namespace Codebreaker.ViewModels;
1+
namespace Codebreaker.ViewModels.Components;
42

53
public enum InfoMessageSeverity
64
{
@@ -10,82 +8,38 @@ public enum InfoMessageSeverity
108
Error
119
}
1210

13-
public partial class InfoMessageViewModel : ObservableObject
11+
public partial class InfoMessageViewModel(Action closeAction) : ObservableObject
1412
{
15-
public static InfoMessageViewModel Error(string content)
16-
{
17-
InfoMessageViewModel message = new()
18-
{
19-
Title = "Error",
20-
Message = content,
21-
Severity = InfoMessageSeverity.Error,
22-
ActionTitle = "OK"
23-
};
24-
message.ActionCommand = new RelayCommand(() => message.Close());
25-
return message;
26-
}
27-
28-
public static InfoMessageViewModel Warning(string content)
29-
{
30-
InfoMessageViewModel message = new()
31-
{
32-
Title = "Warning",
33-
Message = content,
34-
Severity = InfoMessageSeverity.Warning,
35-
ActionTitle = "OK"
36-
};
37-
message.ActionCommand = new RelayCommand(() => message.Close());
38-
return message;
39-
}
40-
41-
public static InfoMessageViewModel Information(string content)
42-
{
43-
InfoMessageViewModel message = new()
44-
{
45-
Title = "Information",
46-
Message = content,
47-
Severity = InfoMessageSeverity.Info,
48-
ActionTitle = "OK"
49-
};
50-
message.ActionCommand = new RelayCommand(() => message.Close());
51-
return message;
52-
}
53-
54-
public static InfoMessageViewModel Success(string content)
55-
{
56-
InfoMessageViewModel message = new()
57-
{
58-
Title = "Success",
59-
Message = content,
60-
Severity = InfoMessageSeverity.Success,
61-
ActionTitle = "OK"
62-
};
63-
message.ActionCommand = new RelayCommand(() => message.Close());
64-
return message;
65-
}
66-
67-
internal ICollection<InfoMessageViewModel>? ContainingCollection { get; set; }
68-
6913
[ObservableProperty]
70-
private InfoMessageSeverity _severity = InfoMessageSeverity.Info;
14+
private InfoMessageSeverity _severity;
7115

7216
[ObservableProperty]
7317
private string _message = string.Empty;
7418

7519
[ObservableProperty]
76-
private string _title = string.Empty;
20+
private string? _title;
7721

7822
[ObservableProperty]
7923
[NotifyPropertyChangedFor(nameof(HasAction))]
80-
private ICommand? _actionCommand;
24+
[NotifyCanExecuteChangedFor(nameof(ExecuteActionCommand))]
25+
private Action? _action;
26+
27+
[RelayCommand]
28+
public void ExecuteAction() => Action?.Invoke();
8129

8230
[ObservableProperty]
8331
[NotifyPropertyChangedFor(nameof(HasAction))]
84-
private string? _actionTitle = "OK";
32+
private string? _actionText = "OK";
8533

86-
public bool HasAction =>
87-
ActionCommand is not null && ActionTitle is not null;
34+
public bool HasAction => ExecuteActionCommand is not null && ActionText is not null;
8835

89-
public void Close() =>
90-
ContainingCollection?.Remove(this);
91-
}
36+
[ObservableProperty]
37+
[NotifyCanExecuteChangedFor(nameof(CloseCommand))]
38+
private Action _closeAction = closeAction;
39+
40+
[ObservableProperty]
41+
private bool _isClosable;
42+
43+
[RelayCommand]
44+
public void Close() => CloseAction();
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Codebreaker.ViewModels.Components;
2+
3+
namespace Codebreaker.ViewModels.Contracts.Services;
4+
5+
public interface IInfoBarService
6+
{
7+
ObservableCollection<InfoMessageViewModel> Messages { get; }
8+
9+
InfoMessageBuilder New { get; }
10+
11+
void Clear();
12+
}

src/Codebreaker.ViewModels/Services/InfoBarMessageService.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Codebreaker.ViewModels.Components;
2+
using Codebreaker.ViewModels.Contracts.Services;
3+
4+
namespace Codebreaker.ViewModels.Services;
5+
6+
public class InfoBarService : IInfoBarService
7+
{
8+
public ObservableCollection<InfoMessageViewModel> Messages { get; } = [];
9+
10+
public InfoMessageBuilder New => new(this);
11+
12+
public void Clear() =>
13+
Messages.Clear();
14+
}
15+
16+
public class InfoMessageBuilder
17+
{
18+
private readonly IInfoBarService _infoBarService;
19+
20+
private readonly InfoMessageViewModel _message;
21+
22+
public InfoMessageBuilder(IInfoBarService infoBarService)
23+
{
24+
_infoBarService = infoBarService;
25+
_message = new(() => infoBarService.Messages.Remove(_message!))
26+
{
27+
IsClosable = true
28+
};
29+
}
30+
31+
public InfoMessageBuilder WithSeverity(InfoMessageSeverity severity)
32+
{
33+
_message.Severity = severity;
34+
return this;
35+
}
36+
37+
public InfoMessageBuilder IsInformationMessage() => WithSeverity(InfoMessageSeverity.Info);
38+
39+
public InfoMessageBuilder IsWarningMessage() => WithSeverity(InfoMessageSeverity.Warning);
40+
41+
public InfoMessageBuilder IsErrorMessage() => WithSeverity(InfoMessageSeverity.Error);
42+
43+
public InfoMessageBuilder IsSuccessMessage() => WithSeverity(InfoMessageSeverity.Success);
44+
45+
public InfoMessageBuilder WithTitle(string title)
46+
{
47+
_message.Title = title;
48+
return this;
49+
}
50+
51+
public InfoMessageBuilder WithMessage(string message)
52+
{
53+
_message.Message = message;
54+
return this;
55+
}
56+
57+
public InfoMessageBuilder WithAction(Action<InfoMessageViewModel> action, string? text = null)
58+
{
59+
_message.Action = () => action(_message);
60+
61+
if (text is not null)
62+
_message.ActionText = text;
63+
64+
return this;
65+
}
66+
67+
public InfoMessageBuilder WithAction(Action action, string? text = null)
68+
{
69+
70+
_message.Action = action;
71+
72+
if (text is not null)
73+
_message.ActionText = text;
74+
75+
return this;
76+
}
77+
78+
public InfoMessageBuilder IsClosable(bool closeable = true)
79+
{
80+
_message.IsClosable = closeable;
81+
return this;
82+
}
83+
84+
public InfoMessageViewModel Show()
85+
{
86+
_infoBarService.Messages.Add(_message);
87+
return _message;
88+
}
89+
}

0 commit comments

Comments
 (0)