GeneratePageViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.Collections.Generic;
  3. using ReactiveUI;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.Input.Platform;
  7. using Avalonia.Interactivity;
  8. using Avalonia.Markup.Xaml;
  9. using MsBox.Avalonia;
  10. using System.Windows;
  11. using System.Text;
  12. using System.IO;
  13. using LoginGenerator.Models;
  14. using System.Linq;
  15. using System.Drawing.Imaging;
  16. using Avalonia.Input;
  17. using Avalonia.Media;
  18. using System.Drawing;
  19. using TextCopy;
  20. namespace LoginGenerator.ViewModels
  21. {
  22. public class GeneratePageViewModel : ReactiveObject
  23. {
  24. public Transliteration transliteration = new Transliteration();
  25. public PasswordGenerator passwordGenerator = new PasswordGenerator();
  26. public bool Theme = false;
  27. private SolidColorBrush background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(236, 233, 255));
  28. public SolidColorBrush Background
  29. {
  30. get => background;
  31. set => this.RaiseAndSetIfChanged(ref background, value);
  32. }
  33. private string? fio;
  34. public string? FIO
  35. {
  36. get => fio;
  37. set => this.RaiseAndSetIfChanged(ref fio, value);
  38. }
  39. private string? login;
  40. public string? Login
  41. {
  42. get => login;
  43. set => this.RaiseAndSetIfChanged(ref login, value);
  44. }
  45. private string? pass;
  46. public string? Pass
  47. {
  48. get => pass;
  49. set => this.RaiseAndSetIfChanged(ref pass, value);
  50. }
  51. public void CopyAll_Click()
  52. {
  53. ClipboardService.SetText($"{FIO}\n{Login}\n{Pass}");
  54. }
  55. public void WriteIn()
  56. {
  57. try
  58. {
  59. if (string.IsNullOrEmpty(FIO))
  60. {
  61. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  62. box.ShowAsync();
  63. return;
  64. }
  65. if (!CheckOnSmoothly())
  66. {
  67. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  68. box.ShowAsync();
  69. return;
  70. }
  71. if (!CheckOnExisting())
  72. {
  73. var box = MessageBoxManager.GetMessageBoxStandard("Îøèáêà", "Ïîëüçîâàòåëü ñ òàêèì ëîãèíîì óæå ñóùåñòâóåò");
  74. box.ShowAsync();
  75. return;
  76. }
  77. DateTime dateTime = DateTime.Now;
  78. File.AppendAllText(FilePath.PATH, $"{FIO} {Login} {Pass} {dateTime.ToShortDateString()}\n");
  79. }
  80. catch (Exception) { }
  81. }
  82. public void OSurnameCopy_Click()
  83. {
  84. try
  85. {
  86. if (string.IsNullOrEmpty(FIO))
  87. {
  88. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  89. box.ShowAsync();
  90. return;
  91. }
  92. if (!CheckOnSmoothly())
  93. {
  94. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  95. box.ShowAsync();
  96. return;
  97. }
  98. string[] parts = FIO.Split(' ');
  99. ClipboardService.SetText(parts[0]);
  100. }
  101. catch (Exception) { }
  102. }
  103. public void ONameCopy_Click()
  104. {
  105. try
  106. {
  107. if (string.IsNullOrEmpty(FIO))
  108. {
  109. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  110. box.ShowAsync();
  111. return;
  112. }
  113. if (!CheckOnSmoothly())
  114. {
  115. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  116. box.ShowAsync();
  117. return;
  118. }
  119. string[] parts = FIO.Split(' ');
  120. ClipboardService.SetText(parts[1]);
  121. }
  122. catch (Exception) { }
  123. }
  124. public void OPatronymicCopy_Click()
  125. {
  126. try
  127. {
  128. if (string.IsNullOrEmpty(FIO))
  129. {
  130. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  131. box.ShowAsync();
  132. return;
  133. }
  134. if (!CheckOnSmoothly())
  135. {
  136. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  137. box.ShowAsync();
  138. return;
  139. }
  140. string[] parts = FIO.Split(' ');
  141. ClipboardService.SetText(parts[2]);
  142. }
  143. catch (Exception) { }
  144. }
  145. public void ONamePatronymicCopy_Click()
  146. {
  147. try
  148. {
  149. if (string.IsNullOrEmpty(FIO))
  150. {
  151. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  152. box.ShowAsync();
  153. return;
  154. }
  155. if (!CheckOnSmoothly())
  156. {
  157. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  158. box.ShowAsync();
  159. return;
  160. }
  161. string[] parts = FIO.Split(' ');
  162. ClipboardService.SetText(parts[1] + " " + parts[2]);
  163. }
  164. catch (Exception) { }
  165. }
  166. public void RegenLogin()
  167. {
  168. try
  169. {
  170. if (string.IsNullOrEmpty(FIO))
  171. {
  172. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  173. box.ShowAsync();
  174. return;
  175. }
  176. if (!CheckOnSmoothly())
  177. {
  178. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  179. box.ShowAsync();
  180. return;
  181. }
  182. Random r = new Random();
  183. string[] parts = FIO.Split(' ');
  184. string newTransliteration = transliteration.Execute(parts[0]) + ".";
  185. for (int i = 0; i < r.Next(5); i++)
  186. {
  187. newTransliteration += transliteration.Execute(parts[1][i].ToString());
  188. }
  189. for (int i = 0; i < r.Next(1, 5); i++)
  190. {
  191. newTransliteration += transliteration.Execute(parts[2][i].ToString());
  192. }
  193. Login = newTransliteration;
  194. }
  195. catch (Exception) { }
  196. }
  197. public void GenerateAll()
  198. {
  199. try
  200. {
  201. if (string.IsNullOrEmpty(FIO))
  202. {
  203. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  204. box.ShowAsync();
  205. return;
  206. }
  207. if (!CheckOnSmoothly())
  208. {
  209. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  210. box.ShowAsync();
  211. return;
  212. }
  213. string[] parts = FIO.Split(' ');
  214. Login = transliteration.Execute($"{parts[0]}.{parts[1][0]}{parts[2][0]}");
  215. Pass = passwordGenerator.Execute();
  216. }
  217. catch (Exception) { }
  218. }
  219. public void RegenPass()
  220. {
  221. if (string.IsNullOrEmpty(FIO))
  222. {
  223. var box = MessageBoxManager.GetMessageBoxStandard("Ïóñòîå ïîëå", "Ââåäèòå ÔÈÎ äëÿ ãåíåðàöèè äàííûõ");
  224. box.ShowAsync();
  225. return;
  226. }
  227. if (!CheckOnSmoothly())
  228. {
  229. var box = MessageBoxManager.GetMessageBoxStandard("ÔÈÎ íåïðàâèëüíîå", "ÔÈÎ äîëæíî ñîñòîÿòü èç òð¸õ ÷àñòåé (1-Ô,2-È,3-Î)");
  230. box.ShowAsync();
  231. return;
  232. }
  233. Pass = passwordGenerator.Execute();
  234. }
  235. public void LoginCopy()
  236. {
  237. ClipboardService.SetText(Login);
  238. }
  239. public void PassCopy()
  240. {
  241. ClipboardService.SetText(Pass);
  242. }
  243. public bool CheckOnExisting()
  244. {
  245. string[] OneUser;
  246. Users u;
  247. string[] AllData = File.ReadAllLines(FilePath.PATH);
  248. if (AllData.Count() == 0)
  249. {
  250. return true;
  251. }
  252. List<Users>? newUsers = new List<Users>();
  253. foreach (string user in AllData)
  254. {
  255. if (!string.IsNullOrEmpty(user))
  256. {
  257. u = new Users();
  258. OneUser = user.Split(' ');
  259. u.Surname = OneUser[0].ToString();
  260. u.Name = OneUser[1].ToString();
  261. u.Patronymic = OneUser[2].ToString();
  262. u.Login = OneUser[3].ToString();
  263. u.Password = OneUser[4].ToString();
  264. u.DateReg = Convert.ToDateTime(OneUser[5]);
  265. newUsers.Add(u);
  266. }
  267. }
  268. Users? maybeNew = newUsers.Where(x => x.Login == Login).FirstOrDefault();
  269. if (maybeNew == null)
  270. {
  271. return true;
  272. }
  273. else
  274. {
  275. return false;
  276. }
  277. }
  278. public bool CheckOnSmoothly()
  279. {
  280. string[] parts = FIO.Split(' ');
  281. if (parts.Count() != 3)
  282. {
  283. return false;
  284. }
  285. else
  286. {
  287. return true;
  288. }
  289. }
  290. public void SwitchTheme()
  291. {
  292. if(Theme == false)
  293. {
  294. Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(80, 80, 80));
  295. Theme = true;
  296. }
  297. else
  298. {
  299. Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(236, 233, 255));
  300. Theme = false;
  301. }
  302. }
  303. }
  304. }