using Microsoft.Win32;
using PDF_Master.CustomControl.SystemControl;
using PDF_Master.Model;
using PDF_Master.EventAggregators;
using PDF_Master.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_Master.CustomControl;
using Prism.Services.Dialogs;
using PDF_Master.Properties;
using PDF_Master.Helper;
using PDFSettings;
using PDF_Master.ViewModels.Dialog.ServiceDialog;
using System.Diagnostics;
using PDFReader_WPF.Helper;
namespace PDF_Master.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private MainContent selectedItem;
#region 文案
private string T_rename;
public string T_Rename
{
get { return T_rename; }
set
{
SetProperty(ref T_rename, value);
}
}
private string _Msgtologin = "";
public string Msgtologin
{
get { return _Msgtologin; }
set
{
SetProperty(ref _Msgtologin, value);
}
}
private void InitString()
{
Msgtologin = App.ServiceLoader.GetString("Msgtologin");
}
#endregion
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 string _Useremailchar = "#";
public string Useremailchar
{
get { return _Useremailchar; }
set
{
SetProperty(ref _Useremailchar, value);
}
}
///
/// UpgradeVis按钮只在订阅期中显示
///
private Visibility _UpgradeVis = Visibility.Visible;
public Visibility UpgradeVis
{
get { return _UpgradeVis; }
set
{
SetProperty(ref _UpgradeVis, 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);
if (value == Visibility.Visible)
{
//还原关闭按钮的显示状态,修复:自动保存后,转档功能的关闭按钮不显示
ProcessCloseBtnVisible = Visibility.Visible;
}
}
}
private Visibility processCloseBtnVisible = Visibility.Visible;
///
/// 进度条控件的关闭按钮是否可见
///
public Visibility ProcessCloseBtnVisible
{
get { return processCloseBtnVisible; }
set
{
SetProperty(ref processCloseBtnVisible, 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 DelegateCommand UpgradelinkCommand { 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);
UpgradelinkCommand= new DelegateCommand(Upgradelink);
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
{
//如果有拖拽文件到图标或者设为默认阅读器的情况,双击文件打开软件时,不加载首页
if ((App.Current.MainWindow as MainWindow) != null && (App.Current.MainWindow as MainWindow).TabablzControl.Items.Count <= 0)
{
region.RequestNavigate(RegionNames.MainRegion, "MainContent");
}
}
try
{
//避免注册弹窗和新手引导弹窗一起弹出
//if (App.IsShowRegist && !Settings.Default.IsNewUser)
//{
// OpenRegister();
//}
//新用户弹出引导弹窗
if (Settings.Default.IsNewUser)
{
if (ShowGuidDialog())
{
//未点击下次再看时,下次启动窗体不再显示
Settings.Default.IsNewUser = false;
Settings.Default.Save();
}
}
}
catch { }
}
));
//因为远程广告模块没有时间测试正式环境,暂时取消
//ADServiceHelper.GetAD("PDFMasterWindows");
}
InitString();
}
private void Upgradelink()
{
DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Tbr, "Tbr_Btn", "Btn_Tbr_Upgrade");
Process.Start(new ProcessStartInfo(ServiceHelper.WebHost + "/windows/store/master?email=" + Settings.Default.UserDate.Email));
}
//显示新手引导弹窗
private bool ShowGuidDialog()
{
bool result = true;
dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
{
if (r.Result == ButtonResult.Ignore)
{
result = false;
}
});
return result;
}
//打开订阅成功弹窗
public void OpenSubSubscriptionOK()
{
dialogs.ShowDialog(DialogNames.SubscriptionOKDialog);
//优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
App.Current.MainWindow.Activate();
}
//打开注册弹窗
public void OpenRegister()
{
dialogs.ShowDialog(DialogNames.RegisterDialog);
//优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
App.Current.MainWindow.Activate();
}
//打开登录弹窗
public void OpenLogin(String Loginoff=null)
{
DialogParameters value = new DialogParameters();
value.Add(ParameterNames.Loginoff, Loginoff);
App.LoginOpen = true;
dialogs.ShowDialog(DialogNames.LoginDialog, value, dialogResult =>
{
});
}
///
/// 注册成功弹窗
///
public void OpenRegisterOK()
{
dialogs.ShowDialog(DialogNames.RegisterOKRegion);
}
///
/// 登录超出设备数限制,重新登录提示弹窗
///
public void OpenLoginoff()
{
dialogs.ShowDialog(DialogNames.LoginoffDialog);
}
//打开用户弹窗
public void OpenUser()
{ //判断本地有没有token,没有显示登录,有再判断登录状态
if (Settings.Default.AppProperties.LoginToken != "")
{
string code = ServiceHelper.GetUser();
if (code == "330" || code == "304")
{
UserVis = Visibility.Collapsed;
LoginVis = Visibility.Visible;
RegisterVis = Visibility.Collapsed;
OphVis = Visibility.Visible;
if (Settings.Default.UserDate.IsLoginoff == false)
{
App.mainWindowViewModel.OpenLoginoff();
Settings.Default.UserDate.IsLoginoff = true;
Settings.Default.Save();
}
}
else
{
Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
App.mainWindowViewModel.UserVis = Visibility.Visible;
App.UserOpen = true;
dialogs.ShowDialog(DialogNames.UserDialog);
}
}
else
{
LoginVis = Visibility.Visible;
RegisterVis = Visibility.Collapsed;
OphVis = Visibility.Visible;
OpenLogin();
}
}
///
/// 关闭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)
{
//如果是设置了,保存前不显示弹窗提示的功能,则点击取消时也可以关闭
//否则,会变成:用户必须要点击保存按钮才能关闭窗体,不合理
if (Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
{
return true;
}
else
{
//普通模式下,点击了取消按钮,则不进行后续的关闭等操作
return false;
}
}
}
if (selectedItem != null)
{
region.Regions[RegionNames.MainRegion].Remove(selectedItem);
}
//多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
if (selectedItem == null)
{
return true;
}
}
return true;
}
///
/// 设置value值小于MaxValue值时 自动显示
/// Value值 大于等于MaxValue值时,自动隐藏
///
public async void SetProcessValue(string title, double value, double maxvalue, bool showClose = true)
{
ProgressTitle = title;
if (value < 0)
{
//异常处理
this.Value = 0;
}
if (maxvalue < 0)
{
//异常处理
this.MaxValue = 0;
}
if (value < maxvalue)
{
this.Value = value;
this.MaxValue = maxvalue;
if (IsProcessVisible != Visibility.Visible)
{
IsProcessVisible = Visibility.Visible;
}
}
else
{
//进度完成后自动隐藏
this.Value = value;
this.MaxValue = maxvalue;
//为了能看到进度条实际到100的效果
await Task.Delay(150);
IsProcessVisible = Visibility.Collapsed;
}
ProcessCloseBtnVisible = showClose ? Visibility.Visible : Visibility.Collapsed;
}
public void CloseTabItem(object item)
{
//获取x号所在的maincontentviewmodel对象
var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
//如果已经有打开文档,先切换到目标页签
if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.Document != null)
{
SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
}
//可以保存时,弹窗询问提示弹窗
if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
{
ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
//此处要加上第二个判断,如果去除第二个判断,则新建文件后,点击关闭按钮,不选择保存路径时,无法关闭页签
if (result == ContentResult.Cancel && !Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
{
//非自动保存且取消了另存为时
return;
}
}
//这里不能加Else 需要分开写If 否则会出现无法关闭页签的情况
//不要保存 可以直接关闭页签时(有多个页签时)
if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
{
region.Regions[RegionNames.MainRegion].Remove(item);
CheckViewsCount();
//关闭子窗体的最后一个页签时,子窗体关闭(不显示首页),参考福昕逻辑
if ((App.Current.MainWindow as MainWindow).TabablzControl.Items.Count == 0)
{
App.Current.MainWindow.Close();
}
}
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();
}
}
}
///
/// 检查页签个数 更新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)
{
ContentResult result = ContentResult.Ok;
if (!Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
{
//偏好设置里未设置直接保存时,需要弹窗提示
AlertsMessage alertsMessage = new AlertsMessage();
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"));
result = alertsMessage.result;
}
if (result == ContentResult.Ok)
{
var isSave = viewContentViewModel.saveFile();
if (isSave == false)
result = ContentResult.Cancel;
}
return result;
}
}
}