MainWindowViewModel.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. using Microsoft.Win32;
  2. using PDF_Master.CustomControl.SystemControl;
  3. using PDF_Master.Model;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Controls;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Dragablz;
  19. using System.Windows;
  20. using Prism.Ioc;
  21. using PDF_Master.CustomControl;
  22. using Prism.Services.Dialogs;
  23. using PDF_Master.Properties;
  24. using PDF_Master.Helper;
  25. using PDFSettings;
  26. using PDF_Master.ViewModels.Dialog.ServiceDialog;
  27. using System.Diagnostics;
  28. using PDFReader_WPF.Helper;
  29. namespace PDF_Master.ViewModels
  30. {
  31. public class MainWindowViewModel : BindableBase
  32. {
  33. private MainContent selectedItem;
  34. #region 文案
  35. private string T_rename;
  36. public string T_Rename
  37. {
  38. get { return T_rename; }
  39. set
  40. {
  41. SetProperty(ref T_rename, value);
  42. }
  43. }
  44. private string _Msgtologin = "";
  45. public string Msgtologin
  46. {
  47. get { return _Msgtologin; }
  48. set
  49. {
  50. SetProperty(ref _Msgtologin, value);
  51. }
  52. }
  53. private void InitString()
  54. {
  55. Msgtologin = App.ServiceLoader.GetString("Msgtologin");
  56. }
  57. #endregion
  58. public MainContent SelectedItem
  59. {
  60. get { return selectedItem; }
  61. set
  62. {
  63. SetProperty(ref selectedItem, value);
  64. if (SelectedItem != null)
  65. {
  66. var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
  67. if (pdfviewer != null)
  68. {
  69. CurrentPDFViewer = pdfviewer;
  70. }
  71. else
  72. {
  73. CurrentPDFViewer = null;
  74. }
  75. }
  76. }
  77. }
  78. private string progresstitle;
  79. /// <summary>
  80. /// 进度控件标题
  81. /// </summary>
  82. public string ProgressTitle
  83. {
  84. get { return progresstitle; }
  85. set
  86. {
  87. SetProperty(ref progresstitle, value);
  88. }
  89. }
  90. private double processvalue = 0;
  91. /// <summary>
  92. /// 进度条当前值
  93. /// </summary>
  94. public double Value
  95. {
  96. get { return processvalue; }
  97. set
  98. {
  99. SetProperty(ref processvalue, value);
  100. }
  101. }
  102. private double maxValue = 100;
  103. /// <summary>
  104. /// 进度条最大值
  105. /// </summary>
  106. public double MaxValue
  107. {
  108. get { return maxValue; }
  109. set
  110. {
  111. SetProperty(ref maxValue, value);
  112. }
  113. }
  114. private string _Useremailchar = "#";
  115. public string Useremailchar
  116. {
  117. get { return _Useremailchar; }
  118. set
  119. {
  120. SetProperty(ref _Useremailchar, value);
  121. }
  122. }
  123. /// <summary>
  124. /// UpgradeVis按钮只在订阅期中显示
  125. /// </summary>
  126. private Visibility _UpgradeVis = Visibility.Visible;
  127. public Visibility UpgradeVis
  128. {
  129. get { return _UpgradeVis; }
  130. set
  131. {
  132. SetProperty(ref _UpgradeVis, value);
  133. }
  134. }
  135. /// <summary>
  136. ///提示登录高级功能
  137. /// </summary>
  138. private Visibility _OphVis = Visibility.Collapsed;
  139. public Visibility OphVis
  140. {
  141. get { return _OphVis; }
  142. set
  143. {
  144. SetProperty(ref _OphVis, value);
  145. }
  146. }
  147. private Visibility _RegisterVis = Visibility.Collapsed;
  148. public Visibility RegisterVis
  149. {
  150. get { return _RegisterVis; }
  151. set
  152. {
  153. SetProperty(ref _RegisterVis, value);
  154. }
  155. }
  156. private Visibility _LoginVis = Visibility.Collapsed;
  157. public Visibility LoginVis
  158. {
  159. get { return _LoginVis; }
  160. set
  161. {
  162. SetProperty(ref _LoginVis, value);
  163. }
  164. }
  165. private Visibility _UserVis = Visibility.Collapsed;
  166. public Visibility UserVis
  167. {
  168. get { return _UserVis; }
  169. set
  170. {
  171. SetProperty(ref _UserVis, value);
  172. }
  173. }
  174. private Visibility isProcessVisible = Visibility.Collapsed;
  175. /// <summary>
  176. /// OCR进度条是否显示
  177. /// </summary>
  178. public Visibility IsProcessVisible
  179. {
  180. get { return isProcessVisible; }
  181. set
  182. {
  183. SetProperty(ref isProcessVisible, value);
  184. if (value == Visibility.Visible)
  185. {
  186. //还原关闭按钮的显示状态,修复:自动保存后,转档功能的关闭按钮不显示
  187. ProcessCloseBtnVisible = Visibility.Visible;
  188. }
  189. }
  190. }
  191. private Visibility processCloseBtnVisible = Visibility.Visible;
  192. /// <summary>
  193. /// 进度条控件的关闭按钮是否可见
  194. /// </summary>
  195. public Visibility ProcessCloseBtnVisible
  196. {
  197. get { return processCloseBtnVisible; }
  198. set
  199. {
  200. SetProperty(ref processCloseBtnVisible, value);
  201. }
  202. }
  203. /// <summary>
  204. /// 进度条关闭的事件
  205. /// </summary>
  206. public Action ProcessCloseAction;
  207. private bool isCloseAllEnable = false;
  208. public bool IsCloseAllEnable
  209. {
  210. get { return isCloseAllEnable; }
  211. set
  212. {
  213. SetProperty(ref isCloseAllEnable, value);
  214. }
  215. }
  216. public DelegateCommand CloseAllTabCommand { get; set; }
  217. private CPDFViewer pdfViewer;
  218. /// <summary>
  219. /// 当前页签的PdfViewer对象
  220. /// </summary>
  221. public CPDFViewer CurrentPDFViewer
  222. {
  223. get { return pdfViewer; }
  224. set { pdfViewer = value; }
  225. }
  226. public DelegateCommand AddTab { get; set; }
  227. /// <summary>
  228. /// 关闭OCR进度条
  229. /// </summary>
  230. public DelegateCommand CloseOCRCommand { get; set; }
  231. public DelegateCommand OpenRegisterCommand { get; set; }
  232. public DelegateCommand<string> OpenLoginCommand { get; set; }
  233. public DelegateCommand OpenUserCommand { get; set; }
  234. public DelegateCommand LoadCommand { get; set; }
  235. public DelegateCommand UpgradelinkCommand { get; set; }
  236. public IRegionManager region;
  237. public IEventAggregator eventer;
  238. public IDialogService dialogs;
  239. private IContainerProvider containerProvider;
  240. public IInterTabClient InterTabClient { get; }
  241. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
  242. {
  243. if (App.mainWindowViewModel == null)
  244. {
  245. //加载第一个窗体时 先赋值静态对象
  246. App.mainWindowViewModel = this;
  247. }
  248. dialogs = dialogService;
  249. region = regionManager;
  250. eventer = eventAggregator;
  251. containerProvider = container;
  252. InterTabClient = new InterTabClient(container);
  253. AddTab = new DelegateCommand(AddTabItem);
  254. CloseOCRCommand = new DelegateCommand(closeocr);
  255. OpenRegisterCommand = new DelegateCommand(OpenRegister);
  256. OpenLoginCommand = new DelegateCommand<string>(OpenLogin);
  257. OpenUserCommand = new DelegateCommand(OpenUser);
  258. UpgradelinkCommand= new DelegateCommand(Upgradelink);
  259. CloseAllTabCommand = new DelegateCommand(() =>
  260. {
  261. if (closeAllTabItem())
  262. {
  263. App.Current.MainWindow.Close();
  264. }
  265. });
  266. //第一次打开时需要自动加载Home页
  267. if (App.IsFirstOpen)
  268. {
  269. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  270. {
  271. //重新打开窗口关闭前 未关闭的页签
  272. if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
  273. {
  274. List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
  275. foreach (var file in files)
  276. {
  277. this.AddTabItem(file);
  278. }
  279. Settings.Default.AppProperties.NeedToOpenFileList.Clear();
  280. Settings.Default.Save();
  281. return;
  282. }
  283. else
  284. {
  285. //如果有拖拽文件到图标或者设为默认阅读器的情况,双击文件打开软件时,不加载首页
  286. if ((App.Current.MainWindow as MainWindow) != null && (App.Current.MainWindow as MainWindow).TabablzControl.Items.Count <= 0)
  287. {
  288. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  289. }
  290. }
  291. try
  292. {
  293. //避免注册弹窗和新手引导弹窗一起弹出
  294. //if (App.IsShowRegist && !Settings.Default.IsNewUser)
  295. //{
  296. // OpenRegister();
  297. //}
  298. //新用户弹出引导弹窗
  299. if (Settings.Default.IsNewUser)
  300. {
  301. if (ShowGuidDialog())
  302. {
  303. //未点击下次再看时,下次启动窗体不再显示
  304. Settings.Default.IsNewUser = false;
  305. Settings.Default.Save();
  306. }
  307. }
  308. }
  309. catch { }
  310. }
  311. ));
  312. App.IsFirstOpen = false;
  313. //因为远程广告模块没有时间测试正式环境,暂时取消
  314. //ADServiceHelper.GetAD("PDFMasterWindows");
  315. }
  316. //判断本地有没有token,没有显示登录,有再判断登录状态
  317. if (Settings.Default.AppProperties.LoginToken != "")
  318. {
  319. string code = ServiceHelper.GetUser();
  320. if (code == "330" || code == "304")
  321. {
  322. LoginVis = Visibility.Visible;
  323. RegisterVis = Visibility.Collapsed;
  324. OphVis = Visibility.Visible;
  325. }
  326. else
  327. {
  328. App.IsLogin = true;
  329. Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  330. UserVis = Visibility.Visible;
  331. }
  332. }
  333. else if(Settings.Default.UserDate.Email!="")
  334. {
  335. LoginVis = Visibility.Visible;
  336. RegisterVis = Visibility.Collapsed;
  337. OphVis = Visibility.Visible;
  338. }
  339. else
  340. {
  341. RegisterVis = Visibility.Visible;
  342. LoginVis = Visibility.Collapsed;
  343. OphVis = Visibility.Visible;
  344. }
  345. InitString();
  346. }
  347. private void Upgradelink()
  348. {
  349. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Tbr, "Tbr_Btn", "Btn_Tbr_Upgrade");
  350. Process.Start(new ProcessStartInfo("http://test-pdf-pro.kdan.cn:3021/windows/store/master?email=" + Settings.Default.UserDate.Email));
  351. }
  352. //显示新手引导弹窗
  353. private bool ShowGuidDialog()
  354. {
  355. bool result = true;
  356. dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
  357. {
  358. if (r.Result == ButtonResult.Ignore)
  359. {
  360. result = false;
  361. }
  362. });
  363. return result;
  364. }
  365. //打开订阅成功弹窗
  366. public void OpenSubSubscriptionOK()
  367. {
  368. dialogs.ShowDialog(DialogNames.SubscriptionOKDialog);
  369. //优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
  370. App.Current.MainWindow.Activate();
  371. }
  372. //打开注册弹窗
  373. public void OpenRegister()
  374. {
  375. dialogs.ShowDialog(DialogNames.RegisterDialog);
  376. //优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
  377. App.Current.MainWindow.Activate();
  378. }
  379. //打开登录弹窗
  380. public void OpenLogin(String Loginoff=null)
  381. {
  382. DialogParameters value = new DialogParameters();
  383. value.Add(ParameterNames.Loginoff, Loginoff);
  384. dialogs.ShowDialog(DialogNames.LoginDialog, value, dialogResult =>
  385. {
  386. });
  387. }
  388. public void OpenRegisterOK()
  389. {
  390. dialogs.ShowDialog(DialogNames.RegisterOKRegion);
  391. }
  392. public void OpenLoginoff()
  393. {
  394. dialogs.ShowDialog(DialogNames.LoginoffDialog);
  395. }
  396. //打开用户弹窗
  397. public void OpenUser()
  398. { //判断本地有没有token,没有显示登录,有再判断登录状态
  399. if (Settings.Default.AppProperties.LoginToken != "")
  400. {
  401. string code = ServiceHelper.GetUser();
  402. if (code == "330" || code == "304")
  403. {
  404. UserVis = Visibility.Collapsed;
  405. LoginVis = Visibility.Visible;
  406. RegisterVis = Visibility.Collapsed;
  407. OphVis = Visibility.Visible;
  408. if (Settings.Default.UserDate.IsLoginoff == false)
  409. {
  410. App.mainWindowViewModel.OpenLoginoff();
  411. Settings.Default.UserDate.IsLoginoff = true;
  412. Settings.Default.Save();
  413. }
  414. }
  415. else
  416. {
  417. Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  418. App.mainWindowViewModel.UserVis = Visibility.Visible;
  419. dialogs.ShowDialog(DialogNames.UserDialog);
  420. }
  421. }
  422. else
  423. {
  424. LoginVis = Visibility.Visible;
  425. RegisterVis = Visibility.Collapsed;
  426. OphVis = Visibility.Visible;
  427. OpenLogin();
  428. }
  429. }
  430. /// <summary>
  431. /// 关闭OCR
  432. /// </summary>
  433. private void closeocr()
  434. {
  435. if (ProcessCloseAction != null)
  436. {
  437. ProcessCloseAction.Invoke();
  438. }
  439. }
  440. /// <summary>
  441. /// 单击+号时添加页签
  442. /// </summary>
  443. private void AddTabItem()
  444. {
  445. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  446. CheckViewsCount();
  447. }
  448. /// <summary>
  449. /// 选择多文档打开时
  450. /// </summary>
  451. /// <param name="filePath"></param>
  452. public void AddTabItem(string filePath)
  453. {
  454. NavigationParameters parameters = new NavigationParameters
  455. {
  456. { ParameterNames.FilePath, filePath }
  457. };
  458. region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
  459. CheckViewsCount();
  460. }
  461. /// <summary>
  462. /// 关闭所有页签
  463. /// </summary>
  464. public bool closeAllTabItem()
  465. {
  466. //下一次重新打开未关闭的文档
  467. if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
  468. {
  469. Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
  470. Settings.Default.Save();
  471. }
  472. while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
  473. {
  474. if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
  475. {
  476. ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
  477. if (result == ContentResult.Cancel)
  478. {
  479. //如果是设置了,保存前不显示弹窗提示的功能,则点击取消时也可以关闭
  480. //否则,会变成:用户必须要点击保存按钮才能关闭窗体,不合理
  481. if (Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  482. {
  483. return true;
  484. }
  485. else
  486. {
  487. //普通模式下,点击了取消按钮,则不进行后续的关闭等操作
  488. return false;
  489. }
  490. }
  491. }
  492. if (selectedItem != null)
  493. {
  494. region.Regions[RegionNames.MainRegion].Remove(selectedItem);
  495. }
  496. //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
  497. if (selectedItem == null)
  498. {
  499. return true;
  500. }
  501. }
  502. return true;
  503. }
  504. /// <summary>
  505. /// 设置value值小于MaxValue值时 自动显示
  506. /// Value值 大于等于MaxValue值时,自动隐藏
  507. /// </summary>
  508. public async void SetProcessValue(string title, double value, double maxvalue, bool showClose = true)
  509. {
  510. ProgressTitle = title;
  511. if (value < 0)
  512. {
  513. //异常处理
  514. this.Value = 0;
  515. }
  516. if (maxvalue < 0)
  517. {
  518. //异常处理
  519. this.MaxValue = 0;
  520. }
  521. if (value < maxvalue)
  522. {
  523. this.Value = value;
  524. this.MaxValue = maxvalue;
  525. if (IsProcessVisible != Visibility.Visible)
  526. {
  527. IsProcessVisible = Visibility.Visible;
  528. }
  529. }
  530. else
  531. {
  532. //进度完成后自动隐藏
  533. this.Value = value;
  534. this.MaxValue = maxvalue;
  535. //为了能看到进度条实际到100的效果
  536. await Task.Delay(150);
  537. IsProcessVisible = Visibility.Collapsed;
  538. }
  539. ProcessCloseBtnVisible = showClose ? Visibility.Visible : Visibility.Collapsed;
  540. }
  541. public void CloseTabItem(object item)
  542. {
  543. //获取x号所在的maincontentviewmodel对象
  544. var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
  545. //如果已经有打开文档,先切换到目标页签
  546. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.Document != null)
  547. {
  548. SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
  549. }
  550. //可以保存时,弹窗询问提示弹窗
  551. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
  552. {
  553. ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
  554. //此处要加上第二个判断,如果去除第二个判断,则新建文件后,点击关闭按钮,不选择保存路径时,无法关闭页签
  555. if (result == ContentResult.Cancel && !Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  556. {
  557. //非自动保存且取消了另存为时
  558. return;
  559. }
  560. }
  561. //这里不能加Else 需要分开写If 否则会出现无法关闭页签的情况
  562. //不要保存 可以直接关闭页签时(有多个页签时)
  563. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  564. {
  565. region.Regions[RegionNames.MainRegion].Remove(item);
  566. CheckViewsCount();
  567. //关闭子窗体的最后一个页签时,子窗体关闭(不显示首页),参考福昕逻辑
  568. if ((App.Current.MainWindow as MainWindow).TabablzControl.Items.Count == 0)
  569. {
  570. App.Current.MainWindow.Close();
  571. }
  572. }
  573. else
  574. {
  575. //只剩一个页签的情况
  576. if (maincontentViewModel.PDFViewer != null)
  577. {
  578. // 如果是关闭文档的页签 则回到首页
  579. region.Regions[RegionNames.MainRegion].Remove(item);
  580. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  581. }
  582. else
  583. {
  584. // 如果是关闭首页则关闭软件
  585. region.Regions[RegionNames.MainRegion].Remove(item);
  586. App.Current.MainWindow.Close();
  587. }
  588. }
  589. }
  590. /// <summary>
  591. /// 检查页签个数 更新close allcommand 命令状态
  592. /// </summary>
  593. private void CheckViewsCount()
  594. {
  595. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  596. {
  597. IsCloseAllEnable = true;
  598. }
  599. else
  600. {
  601. IsCloseAllEnable = false;
  602. }
  603. }
  604. public void SelectItem(string filepath)
  605. {
  606. var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
  607. if (item == null)
  608. return;
  609. region.Regions[RegionNames.MainRegion].Activate(item);
  610. var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
  611. if (behavior != null)
  612. {
  613. behavior.ActivateView(item);
  614. }
  615. }
  616. /// <summary>
  617. /// 显示关闭询问弹窗
  618. /// </summary>
  619. /// <param name="pdfViewer"></param>
  620. /// <returns></returns>
  621. private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
  622. {
  623. ContentResult result = ContentResult.Ok;
  624. if (!Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  625. {
  626. //偏好设置里未设置直接保存时,需要弹窗提示
  627. AlertsMessage alertsMessage = new AlertsMessage();
  628. alertsMessage.ShowDialog("", string.Format(App.MainPageLoader.GetString("Main_ModifiedFileWarning"), System.IO.Path.GetFileName(CurrentPDFViewer.Document.FilePath)), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_no"), App.ServiceLoader.GetString("Text_ok"));
  629. result = alertsMessage.result;
  630. }
  631. if (result == ContentResult.Ok)
  632. {
  633. var isSave = viewContentViewModel.saveFile();
  634. if (isSave == false)
  635. result = ContentResult.Cancel;
  636. }
  637. return result;
  638. }
  639. }
  640. }