BOTAContentViewModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using Prism.Mvvm;
  2. using Prism.Regions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ComPDFKitViewer.PdfViewer;
  9. using PDF_Master.Model;
  10. using Prism.Commands;
  11. using System.Windows.Controls;
  12. using DryIoc;
  13. using Prism.Services.Dialogs;
  14. using PDF_Master.Views.PageEdit;
  15. using PDF_Master.Properties;
  16. using PDF_Master.Helper;
  17. using PDFReader_WPF.Helper;
  18. namespace PDF_Master.ViewModels.BOTA
  19. {
  20. public class BOTAContentViewModel : BindableBase, INavigationAware
  21. {
  22. public CPDFViewer pdfViewer { get; set; }
  23. private IRegionManager regions { get; set; }
  24. private IDialogService dialogs { get; set; }
  25. public ViewContentViewModel viewContentViewModel { get; set; }
  26. private Dictionary<string, string> viewNameByTabItem;
  27. private string botaContentRegionName;
  28. public string BOTAContentRegionName
  29. {
  30. get { return botaContentRegionName; }
  31. set
  32. {
  33. SetProperty(ref botaContentRegionName, value);
  34. }
  35. }
  36. private int selectedIndex;
  37. public int SelectedIndex
  38. {
  39. get { return selectedIndex; }
  40. set
  41. {
  42. SetProperty(ref selectedIndex, value);
  43. }
  44. }
  45. private string tip_Thumbnail;
  46. public string Tip_Thumbnail
  47. {
  48. get { return tip_Thumbnail; }
  49. set
  50. {
  51. SetProperty(ref tip_Thumbnail, value);
  52. }
  53. }
  54. private string tip_OutLine;
  55. public string Tip_OutLine
  56. {
  57. get { return tip_OutLine; }
  58. set
  59. {
  60. SetProperty(ref tip_OutLine, value);
  61. }
  62. }
  63. private string tip_BookMark;
  64. public string Tip_BookMark
  65. {
  66. get { return tip_BookMark; }
  67. set
  68. {
  69. SetProperty(ref tip_BookMark, value);
  70. }
  71. }
  72. private string tip_Search;
  73. public string Tip_Search
  74. {
  75. get { return tip_Search; }
  76. set
  77. {
  78. SetProperty(ref tip_Search, value);
  79. }
  80. }
  81. private string tip_Annote;
  82. public string Tip_Annote
  83. {
  84. get { return tip_Annote; }
  85. set
  86. {
  87. SetProperty(ref tip_Annote, value);
  88. }
  89. }
  90. public string CurrentBar = "";
  91. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  92. public BOTAContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  93. {
  94. regions = regionManager;
  95. dialogs = dialogService;
  96. BOTAContentRegionName = Guid.NewGuid().ToString();
  97. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectionChangedEvent);
  98. viewNameByTabItem = new Dictionary<string, string>();
  99. InitDictionartViewNameByTabItem(ref viewNameByTabItem);
  100. InitString();
  101. }
  102. private void TabControlSelectionChangedEvent(object e)
  103. {
  104. var args = e as SelectionChangedEventArgs;
  105. if (args != null && args.AddedItems.Count > 0)
  106. {
  107. if (!string.IsNullOrEmpty((args.AddedItems[0] as TabItem).Name))
  108. {
  109. TabControlDataTracking((args.AddedItems[0] as TabItem).Name);
  110. EnterSelectedBar((args.AddedItems[0] as TabItem).Name);
  111. if ((args.AddedItems[0] as TabItem).IsSelected)
  112. {
  113. viewContentViewModel.OpenBOTA = true;
  114. }
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// 侧边按钮埋点
  120. /// </summary>
  121. /// <param name="tag">按钮名字</param>
  122. private void TabControlDataTracking(string tag)
  123. {
  124. switch (tag)
  125. {
  126. case "TabItemOutLine":
  127. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Outline");
  128. break;
  129. case "TabItemThumbnail":
  130. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Thumbnail");
  131. break;
  132. case "TabItemBookMark":
  133. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_BookMark");
  134. break;
  135. case "TabItemAnnotation":
  136. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Annotation");
  137. break;
  138. case "TabItemSearch":
  139. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Search");
  140. break;
  141. }
  142. }
  143. private void InitString()
  144. {
  145. Tip_Annote = App.MainPageLoader.GetString("ViewTopToolbar_Annotation");
  146. Tip_OutLine = App.MainPageLoader.GetString("Outline_Title");
  147. Tip_BookMark = App.MainPageLoader.GetString("Bookmark_Title");
  148. Tip_Search = App.MainPageLoader.GetString("ViewRightMenu_Search");
  149. Tip_Thumbnail = App.MainPageLoader.GetString("Thumbnails_Title");
  150. }
  151. /// <summary>
  152. /// 初始化名称-视图字典
  153. /// </summary>
  154. private void InitDictionartViewNameByTabItem(ref Dictionary<string, string> viewNameByTabItem)
  155. {
  156. //绑定tabitem名字和对应的View控件名称
  157. viewNameByTabItem.Add("TabItemThumbnail", "PageEditContent");
  158. viewNameByTabItem.Add("TabItemOutLine", "OutLineControl");
  159. viewNameByTabItem.Add("TabItemBookMark", "BookmarkContent");
  160. viewNameByTabItem.Add("TabItemAnnotation", "AnnotationContent");
  161. viewNameByTabItem.Add("TabItemSearch", "SearchContent");
  162. viewNameByTabItem.Add("TabItemForm", "");
  163. viewNameByTabItem.Add("TabItemSign", "");
  164. }
  165. /// <summary>
  166. /// 导航至目标TabItem
  167. /// </summary>
  168. /// <param name="currentBar"></param>
  169. private void EnterSelectedBar(string currentBar)
  170. {
  171. //增加判空逻辑
  172. if (!string.IsNullOrEmpty(currentBar))
  173. {
  174. CurrentBar = currentBar;
  175. NavigationParameters param = new NavigationParameters();
  176. param.Add(ParameterNames.PDFViewer, pdfViewer);
  177. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  178. if (currentBar == "TabItemThumbnail")
  179. {
  180. param.Add(ParameterNames.BOTAThumb, true);
  181. }
  182. if (viewNameByTabItem.ContainsKey(currentBar))
  183. {
  184. regions.RequestNavigate(BOTAContentRegionName, viewNameByTabItem[currentBar], param);
  185. }
  186. //增加判空逻辑
  187. if (pdfViewer != null && pdfViewer.Document != null)
  188. {
  189. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  190. if (info != null)
  191. {
  192. info.BOTASelectedTab = currentBar;
  193. info.BOTASelectedInex = SelectedIndex;
  194. SettingHelper.SetFileInfo(info);
  195. }
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 根据缓存判断,如果记录需要展开BOTA 则还原上一次的展开状态
  201. /// </summary>
  202. public void OpenBOTA()
  203. {
  204. //记录上一次BOTA展开情况
  205. if (Settings.Default.AppProperties.InitialVIew.RememberBOTA)
  206. {
  207. //如果是加密文档 则先不展开,解密后再展开
  208. if (pdfViewer.Document.IsLocked)
  209. {
  210. return;
  211. }
  212. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  213. //避免新文档因打开文件没有selectedTab信息导致崩溃
  214. if (info != null && !string.IsNullOrEmpty(info.BOTASelectedTab))
  215. {
  216. SelectedIndex = info.BOTASelectedInex;
  217. EnterSelectedBar(info.BOTASelectedTab);
  218. viewContentViewModel.OpenBOTA = true;
  219. }
  220. }
  221. }
  222. #region Navigate
  223. public bool IsNavigationTarget(NavigationContext navigationContext)
  224. {
  225. return true;
  226. }
  227. public void OnNavigatedFrom(NavigationContext navigationContext)
  228. {
  229. }
  230. public void OnNavigatedTo(NavigationContext navigationContext)
  231. {
  232. var contentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  233. if (contentViewModel != null)
  234. {
  235. viewContentViewModel = contentViewModel;
  236. contentViewModel.botaViewModel = this;
  237. }
  238. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  239. if (pdfview != null)
  240. {
  241. pdfViewer = pdfview;
  242. }
  243. //大纲和文档同时打开
  244. if (Settings.Default.AppProperties.InitialVIew.ShowOutLine && pdfViewer.Document.GetOutlineList().Count > 0)
  245. {
  246. SelectedIndex = 2;
  247. EnterSelectedBar("TabItemOutLine");
  248. viewContentViewModel.OpenBOTA = true;
  249. }
  250. //确认是否要展开BOTA
  251. OpenBOTA();
  252. }
  253. #endregion Navigate
  254. }
  255. }