using PDF_Master.Model.Dialog.ToolsDialogs;
using ComPDFKit.PDFDocument;
using PDF_Master.Model;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PDF_Master.Helper;
using Prism.Commands;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.CompressDialogs
{
public class CompresProgressBarDialogViewModel : BindableBase, IDialogAware
{
#region 参数和属性
private IntPtr compressingIntpr = IntPtr.Zero;
private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
private CPDFDocument tempDocument;
private string _pageIndex = "0";
///
/// 当前页
///
public string PageIndex
{
get { return _pageIndex; }
set
{
SetProperty(ref _pageIndex, value);
}
}
private string _pageNumber = "";
///
/// 总页数
///
public string PageNumber
{
get { return _pageNumber; }
set
{
SetProperty(ref _pageNumber, value);
}
}
#endregion
public DelegateCommand CloseCompressCommand { get; set; }
public CompresProgressBarDialogViewModel() {
CloseCompressCommand = new DelegateCommand(Close);
}
public void Close() {
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
}
#region 逻辑函数
private int GetIndex(int pageindex)
{
PageIndex = pageindex.ToString();
return 0;
}
#endregion
#region 框架行为
public string Title => "";
public event Action RequestClose;
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
tempDocument.CompressFile_Cancel(compressingIntpr);
}
public async void OnDialogOpened(IDialogParameters parameters)
{
CPDFDocument doc = null;
string filepath = "";
string password = "";
int compressquality = 45;
parameters.TryGetValue(ParameterNames.PDFDocument, out doc);
parameters.TryGetValue(ParameterNames.FilePath, out filepath);
parameters.TryGetValue(ParameterNames.PassWord, out password);
parameters.TryGetValue("compressDialogModel.CompressQuality", out compressquality);
if (doc != null)
{
PageNumber = doc.PageCount.ToString();
CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
document.UnlockWithPassword(password);
indexDelegate += GetIndex;
compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
//GC.KeepAlive(indexDelegate);
tempDocument = document;
Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
await Task.Run(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
CommonHelper.ShowFileBrowser(filepath);
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
document.Release();
}
}
#endregion
}
}