-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProfileViewModel.cs
More file actions
196 lines (161 loc) · 6.23 KB
/
Copy pathProfileViewModel.cs
File metadata and controls
196 lines (161 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using PropertyChanged;
using ReactiveUI;
using ServiceStack;
using SkillChat.Interface;
using SkillChat.Server.ServiceModel;
using SkillChat.Server.ServiceModel.Molds;
using Splat;
using System;
using System.Reactive;
using System.Threading.Tasks;
using System.Windows.Input;
using SkillChat.Client.ViewModel.Services;
namespace SkillChat.Client.ViewModel
{
public interface IHaveWidth
{
double Width { get; }
}
[AddINotifyPropertyChangedInterface]
public class ProfileViewModel : IProfile
{
private readonly ISkillChatApiClient _serviceClient;
private IChatHub _hub;
public ProfileViewModel(ISkillChatApiClient serviceClient)
{
_serviceClient = serviceClient;
//Показать/скрыть панель профиля
OpenProfilePanelCommand = ReactiveCommand.CreateFromTask(async () =>
{
var user = Locator.Current.GetService<ICurrentUser>();
await Open(user?.Id);
});
//Закрыть панель профиля
CloseProfilePanelCommand = ReactiveCommand.Create(() => Close());
//Сохранить изменения Name профиля
ApplyProfileNameCommand = ReactiveCommand.CreateFromTask(SetProfile);
//Сохранить изменения AboutMe профиля
ApplyProfileAboutMeCommand = ReactiveCommand.CreateFromTask(SetProfile);
//Скрытие окна
LayoutUpdatedWindow = ReactiveCommand.Create<object>(obj =>
{
if (obj is IHaveWidth window)
{
UpdateChatVisibility(window.Width);
}
});
//Показать/скрыть редактирование профиля
SetEditNameProfileCommand = ReactiveCommand.Create(() => ResetEditMode(UserProfileEditMode.DisplayName));
SetEditAboutMeProfileCommand = ReactiveCommand.Create(() => ResetEditMode(UserProfileEditMode.AboutMe));
//Показать/скрыть ContextMenu
ContextMenuProfile = ReactiveCommand.Create<object>(obj => { IsActiveContextMenu = !IsActiveContextMenu; });
}
private async Task SetProfile()
{
UpdateProfileProps(await _serviceClient.SaveProfileAsync(new SetProfile
{
AboutMe = AboutMe,
DisplayName = DisplayName
}));
await _hub.UpdateMyDisplayName(DisplayName);
ResetEditMode();
}
public void SetChatHub(IChatHub chatHub) => _hub = chatHub;
//Profile
/// <summary>
/// Панель профиля открыта
/// </summary>
public bool IsOpened { get; protected set; }
public bool IsActiveContextMenu { get; set; }
/// <summary>
/// Режим редактирования имени
/// </summary>
public bool IsEditNameProfile => EditMode == UserProfileEditMode.DisplayName;
/// <summary>
/// Режим редактирования о себе
/// </summary>
public bool IsEditAboutMeProfile => EditMode == UserProfileEditMode.AboutMe;
public UserProfileEditMode EditMode { get; protected set; }
protected void ResetEditMode(UserProfileEditMode mode = UserProfileEditMode.None)
{
EditMode = mode;
}
public enum UserProfileEditMode
{
None,
DisplayName,
AboutMe
}
public bool IsShowChat { get; protected set; } = false;
public static double WindowWidth { get; set; }
public bool IsMyProfile => Locator.Current.GetService<ICurrentUser>()?.Id == ProfileId;
public ICommand ApplyProfileNameCommand { get; }
public ICommand ApplyProfileAboutMeCommand { get; }
public ICommand OpenProfilePanelCommand { get; }
public ICommand CloseProfilePanelCommand { get; }
public ICommand SetEditNameProfileCommand { get; }
public ICommand SignOutCommand { get; set; }
public ICommand LoadMessageHistoryCommand { get; set; }
public ICommand SetEditAboutMeProfileCommand { get; }
public string ProfileId { get; set; }
public string Login { get; set; }
public string DisplayName { get; set; }
public string AboutMe { get; set; }
public event Action IsOpenProfileEvent;
public ReactiveCommand<object, Unit> LayoutUpdatedWindow { get; }
public ReactiveCommand<object, Unit> ContextMenuProfile { get; }
public void Close()
{
IsOpened = false;
ResetEditMode();
ContextMenuClose();
UpdateChatVisibility();
ProfileId = string.Empty;
Login = string.Empty;
DisplayName = string.Empty;
AboutMe = string.Empty;
}
public void ContextMenuClose()
{
IsActiveContextMenu = false;
}
public void UpdateUserProfile(string newUserDisplayName, string userId)
{
if (ProfileId == userId)
{
DisplayName = newUserDisplayName;
}
}
public async Task Open(string userId)
{
var lastProfileId = ProfileId;
if (lastProfileId == userId)
{
if (IsOpened) Close();
}
else
{
ResetEditMode();
UpdateProfileProps(await _serviceClient.GetProfileAsync(new GetProfile { UserId = userId }));
IsOpened = true;
UpdateChatVisibility();
IsOpenProfileEvent?.Invoke();
}
}
public void UpdateChatVisibility(double? windowWidth = null)
{
if (windowWidth.HasValue)
{
WindowWidth = windowWidth.Value;
}
IsShowChat = !IsOpened || WindowWidth > 650;
}
private void UpdateProfileProps(UserProfileMold profile)
{
ProfileId = profile.Id;
Login = profile.Login;
DisplayName = profile.DisplayName;
AboutMe = profile.AboutMe;
}
}
}