using Microsoft.Win32;
using PDF_Office.CustomControl.SystemControl;
using PDF_Office.Model;
using PDF_Office.EventAggregators;
using PDF_Office.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using ComPDFKitViewer.PdfViewer;
using Dragablz;
using System.Windows;
using Prism.Ioc;
using PDF_Office.CustomControl;
using Prism.Services.Dialogs;
using PDF_Office.Properties;
using PDF_Office.Helper;
using PDFSettings.Settings;
namespace PDF_Office.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private MainContent selectedItem;
public MainContent SelectedItem
{
get { return selectedItem; }
set
{
SetProperty(ref selectedItem, value);
if (SelectedItem != null)
{
var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
if (pdfviewer != null)
{
CurrentPDFViewer = pdfviewer;
}
else
{
CurrentPDFViewer = null;
}
}
}
}
private string progresstitle;
///
/// 进度控件标题
///
public string ProgressTitle
{
get { return progresstitle; }
set
{
SetProperty(ref progresstitle, value);
}
}
private double processvalue = 0;
///
/// 进度条当前值
///
public double Value
{
get { return processvalue; }
set
{
SetProperty(ref processvalue, value);
}
}
private double maxValue = 100;
///
/// 进度条最大值
///
public double MaxValue
{
get { return maxValue; }
set
{
SetProperty(ref maxValue, value);
}
}
///
///提示登录高级功能
///
private Visibility _OphVis = Visibility.Collapsed;
public Visibility OphVis
{
get { return _OphVis; }
set
{
SetProperty(ref _OphVis, value);
}
}
private Visibility _RegisterVis = Visibility.Collapsed;
public Visibility RegisterVis
{
get { return _RegisterVis; }
set
{
SetProperty(ref _RegisterVis, value);
}
}
private Visibility _LoginVis = Visibility.Collapsed;
public Visibility LoginVis
{
get { return _LoginVis; }
set
{
SetProperty(ref _LoginVis, value);
}
}
private Visibility _UserVis = Visibility.Collapsed;
public Visibility UserVis
{
get { return _UserVis; }
set
{
SetProperty(ref _UserVis, value);
}
}
private Visibility isProcessVisible = Visibility.Collapsed;
///
/// OCR进度条是否显示
///
public Visibility IsProcessVisible
{
get { return isProcessVisible; }
set
{
SetProperty(ref isProcessVisible, value);
}
}
///
/// 进度条关闭的事件
///
public Action ProcessCloseAction;
private bool isCloseAllEnable = false;
public bool IsCloseAllEnable
{
get { return isCloseAllEnable; }
set
{
SetProperty(ref isCloseAllEnable, value);
}
}
public DelegateCommand CloseAllTabCommand { get; set; }
private CPDFViewer pdfViewer;
///
/// 当前页签的PdfViewer对象
///
public CPDFViewer CurrentPDFViewer
{
get { return pdfViewer; }
set { pdfViewer = value; }
}
public DelegateCommand AddTab { get; set; }
///
/// 关闭OCR进度条
///
public DelegateCommand CloseOCRCommand { get; set; }
public DelegateCommand OpenRegisterCommand { get; set; }
public DelegateCommand OpenLoginCommand { get; set; }
public DelegateCommand OpenUserCommand { get; set; }
public DelegateCommand LoadCommand { get; set; }
public IRegionManager region;
public IEventAggregator eventer;
public IDialogService dialogs;
private IContainerProvider containerProvider;
public IInterTabClient InterTabClient { get; }
public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
{
if (App.mainWindowViewModel == null)
{
//加载第一个窗体时 先赋值静态对象
App.mainWindowViewModel = this;
}
dialogs = dialogService;
region = regionManager;
eventer = eventAggregator;
containerProvider = container;
InterTabClient = new InterTabClient(container);
AddTab = new DelegateCommand(AddTabItem);
CloseOCRCommand = new DelegateCommand(closeocr);
OpenRegisterCommand = new DelegateCommand(OpenRegister);
OpenLoginCommand = new DelegateCommand(OpenLogin);
OpenUserCommand = new DelegateCommand(OpenUser);
CloseAllTabCommand = new DelegateCommand(() => { if (closeAllTabItem())
{
App.Current.MainWindow.Close();
}
});
//第一次打开时需要自动加载Home页
if (App.IsFirstOpen)
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
{
if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
{
List files = Settings.Default.AppProperties.NeedToOpenFileList;
foreach (var file in files)
{
this.AddTabItem(file);
}
Settings.Default.AppProperties.NeedToOpenFileList.Clear();
Settings.Default.Save();
return;
}
else
{
region.RequestNavigate(RegionNames.MainRegion, "MainContent");
}
try
{
//避免注册弹窗和新手引导弹窗一起弹出
if (App.IsShowRegist && !Settings.Default.IsNewUser)
{
OpenRegister();
}
//新用户弹出引导弹窗
if (Settings.Default.IsNewUser)
{
ShowGuidDialog();
Settings.Default.IsNewUser = false;
Settings.Default.Save();
}
}
catch { }
}
));
App.IsFirstOpen = false;
if (Settings.Default.AppProperties.LoginToken != "")
{
if(ServiceHelper.GetUser()=="false")
{
LoginVis = Visibility.Visible;
OphVis = Visibility.Visible;
}
else
{
UserVis = Visibility.Visible;
}
}
else
{
LoginVis = Visibility.Visible;
OphVis = Visibility.Visible;
}
}
else
{
if (ServiceHelper.GetUser() == "false")
{
LoginVis = Visibility.Visible;
}
else
{
UserVis = Visibility.Visible;
}
}
}
//显示新手引导弹窗
private void ShowGuidDialog()
{
dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
{
});
}
//打开注册弹窗
public void OpenRegister()
{
dialogs.ShowDialog(DialogNames.RegisterDialog, new DialogParameters(), r =>
{
});
}
//打开登录弹窗
public void OpenLogin()
{
dialogs.ShowDialog(DialogNames.LoginDialog, new DialogParameters(""), r =>
{
});
}
//打开用户弹窗
public void OpenUser()
{
dialogs.ShowDialog(DialogNames.UserDialog, new DialogParameters(""), r =>
{
});
}
///
/// 关闭OCR
///
private void closeocr()
{
if (ProcessCloseAction != null)
{
ProcessCloseAction.Invoke();
}
}
///
/// 单击+号时添加页签
///
private void AddTabItem()
{
region.RequestNavigate(RegionNames.MainRegion, "MainContent");
CheckViewsCount();
}
///
/// 选择多文档打开时
///
///
public void AddTabItem(string filePath)
{
NavigationParameters parameters = new NavigationParameters
{
{ ParameterNames.FilePath, filePath }
};
region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
CheckViewsCount();
}
///
/// 关闭所有页签
///
public bool closeAllTabItem()
{
//下一次重新打开未关闭的文档
if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
{
Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
Settings.Default.Save();
}
while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
{
if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
{
ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
if (result == ContentResult.Cancel)
return false;
App.OpenedFileList.Remove(CurrentPDFViewer.Document.FilePath);
CurrentPDFViewer.CloseDocument();
}
if (selectedItem != null)
{
region.Regions[RegionNames.MainRegion].Remove(selectedItem);
}
//多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
if (selectedItem == null)
{
return true;
}
}
return true;
}
public void CloseTabItem(object item)
{
//获取x号所在的maincontentviewmodel对象
var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
//如果已经有打开文档,先切换到目标页签
if (maincontentViewModel.PDFViewer != null)
{
SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
}
if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
{
ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
if (result == ContentResult.Cancel)
{
return;
}
App.OpenedFileList.Remove(maincontentViewModel.PDFViewer.Document.FilePath);
if (maincontentViewModel.PDFViewer.Document != null)
{
maincontentViewModel.PDFViewer.CloseDocument();
}
}
if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
{
region.Regions[RegionNames.MainRegion].Remove(item);
CheckViewsCount();
}
else
{
if (maincontentViewModel.PDFViewer != null)
{
//如果是打开文档的页签 则回到首页
region.Regions[RegionNames.MainRegion].Remove(item);
region.RequestNavigate(RegionNames.MainRegion, "MainContent");
}
else
{
//如果是关闭首页则关闭软件
region.Regions[RegionNames.MainRegion].Remove(item);
App.Current.MainWindow.Close();
}
}
return;
}
///
/// 检查页签个数 更新close allcommand 命令状态
///
private void CheckViewsCount()
{
if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
{
IsCloseAllEnable = true;
}
else
{
IsCloseAllEnable = false;
}
}
public void SelectItem(string filepath)
{
var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
if (item == null)
return;
region.Regions[RegionNames.MainRegion].Activate(item);
var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
if (behavior != null)
{
behavior.ActivateView(item);
}
}
///
/// 显示关闭询问弹窗
///
///
///
private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
{
if(Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
{
//偏好设置里选择自动保存时
return ContentResult.Ok;
}
AlertsMessage alertsMessage = new AlertsMessage();
alertsMessage.ShowDialog("关闭提示", "当前文档有为保存的操作,是否需要保存?", "Cancel", "No", "Ok");
var result = alertsMessage.result;
if (result == ContentResult.Ok)
{
var isSave = viewContentViewModel.saveFile();
if (isSave == false)
result = ContentResult.Cancel;
}
return result;
}
}
}