Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions PhoneAssistant.WPF/Features/Phones/EmailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using PhoneAssistant.Model;
using PhoneAssistant.WPF.Shared;
using System.Collections.ObjectModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;

Expand Down Expand Up @@ -41,37 +40,38 @@ public OrderDetails OrderDetails
GenerateEmailHtml();
}
}
[ObservableProperty]
private bool _envelopePrinted = false;

[ObservableProperty]
private string _imei = string.Empty;

[ObservableProperty]
private string _phoneNumber = string.Empty;
public partial string AssetTag { get; set; } = string.Empty;

[ObservableProperty]
private string _assetTag = string.Empty;
public partial bool EnvelopePrinted { get; set; } = false;

[ObservableProperty]
private string? _ticket;
public partial string Imei { get; set; } = string.Empty;

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(PrintEnvelopeCommand))]
private OrderType _orderType;
public partial OrderType OrderType { get; set; }
partial void OnOrderTypeChanged(OrderType value)
{
OrderDetails.OrderType = OrderType;
GenerateEmailHtml();
}

[ObservableProperty]
public partial string PhoneNumber { get; set; } = string.Empty;

[ObservableProperty]
public partial string? Ticket { get; set; }

[RelayCommand]
private async Task CloseAsync()
{
await _phonesRepository.UpdateAsync(_orderDetails!.Phone);
GeneratingEmail = false;
}

private bool CanPrintEnvelope() => OrderType != OrderType.None;
[RelayCommand(CanExecute = nameof(CanPrintEnvelope))]
private async Task PrintEnvelope()
Expand Down Expand Up @@ -104,14 +104,14 @@ await Task.Run(() =>
public ObservableCollection<Location> Locations { get; set; } = [];

[ObservableProperty]
private Location? _selectedLocation;
public partial Location? SelectedLocation { get; set; }
partial void OnSelectedLocationChanged(Location? value)
{
if (value is null) return;

string deliveryAddress = value.Address;
deliveryAddress = deliveryAddress.Replace("{NewUser}", _orderDetails!.Phone.NewUser);
deliveryAddress = deliveryAddress.Replace("{SR}", Ticket);
deliveryAddress = deliveryAddress.Replace("{SR}", Ticket + " " + _orderDetails.OrderType + " " + _orderDetails.DeviceType);
deliveryAddress = deliveryAddress.Replace("{PhoneNumber}", PhoneNumber);

DeliveryAddress = deliveryAddress;
Expand All @@ -120,8 +120,7 @@ partial void OnSelectedLocationChanged(Location? value)

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(EmailHtml))]
private string _deliveryAddress = string.Empty;

public partial string DeliveryAddress { get; set; } = string.Empty;
partial void OnDeliveryAddressChanged(string value)
{
if (_orderDetails is null) return;
Expand All @@ -135,7 +134,7 @@ public static string ReformatDeliveryAddress(string address)
{
Regex regex = AddressReformat();

string reformatted = regex.Replace(address,string.Empty);
string reformatted = regex.Replace(address, string.Empty);

return reformatted;
}
Expand Down
6 changes: 0 additions & 6 deletions PhoneAssistant.WPF/Shared/IPrintDymoLabel.cs

This file was deleted.

149 changes: 76 additions & 73 deletions PhoneAssistant.WPF/Shared/PrintDymoLabel.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,96 @@
using System.Drawing;
using PhoneAssistant.Model;
using System.Drawing;
using System.Drawing.Printing;

using PhoneAssistant.Model;
namespace PhoneAssistant.WPF.Shared;

namespace PhoneAssistant.WPF.Shared
public interface IPrintDymoLabel
{
public sealed class PrintDymoLabel(IApplicationSettingsRepository appSettings) : IPrintDymoLabel
void Execute(string address, string? includeDate);
}

public sealed class PrintDymoLabel(IApplicationSettingsRepository appSettings) : IPrintDymoLabel
{
// For Dymo 450 printer and Label 30256 Shipping w231 x h400
// the largest rectangle we can draw is
// graphics.DrawRectangle(_linePen, 2, 20, 365, 193);
const int BodyHeight = 193;
const int BodyWidth = 365;
const int MarginTop = 20;
const int MarginLeft = 2;

private readonly IApplicationSettingsRepository _appSettings = appSettings ?? throw new ArgumentNullException(nameof(appSettings));
private string? _address;
private string? _includeDate;

public void Execute(string address, string? includeDate)
{
// For Dymo 450 printer and Label 30256 Shipping w231 x h400
// the largest rectangle we can draw is
// graphics.DrawRectangle(_linePen, 2, 20, 365, 193);
const int BodyHeight = 193;
const int BodyWidth = 365;
const int MarginTop = 20;
const int MarginLeft = 2;

private readonly IApplicationSettingsRepository _appSettings = appSettings ?? throw new ArgumentNullException(nameof(appSettings));
private string? _address;
private string? _includeDate;

public void Execute(string address, string? includeDate)
_address = address.Trim();
_includeDate = includeDate;

PrintDocument pd = new();
pd.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.Color = false;
pd.DefaultPageSettings.PaperSize = new PaperSize("30256 Shipping", 231, 400);

if (_appSettings.ApplicationSettings.DymoPrintToFile)
{
_address = address.Trim();
_includeDate = includeDate;

PrintDocument pd = new();
pd.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.Color = false;
pd.DefaultPageSettings.PaperSize = new PaperSize("30256 Shipping", 231, 400);

if (_appSettings.ApplicationSettings.DymoPrintToFile)
{
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
pd.DefaultPageSettings.PrinterSettings.PrintToFile = true;
pd.DefaultPageSettings.PrinterSettings.PrintFileName = _appSettings.ApplicationSettings.DymoPrintFile;
}
else
{
pd.PrinterSettings.PrinterName = _appSettings.ApplicationSettings.DymoPrinter;
pd.DefaultPageSettings.PrinterSettings.PrintToFile = false;
}

pd.PrintPage += new PrintPageEventHandler(PrintPage);

if (pd.PrinterSettings.IsValid)
pd.Print();
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
pd.DefaultPageSettings.PrinterSettings.PrintToFile = true;
pd.DefaultPageSettings.PrinterSettings.PrintFileName = _appSettings.ApplicationSettings.DymoPrintFile;
}

private void PrintPage(object sender, PrintPageEventArgs ev)
else
{
if (ev.Graphics is null) return;
Graphics graphics = ev.Graphics;
pd.PrinterSettings.PrinterName = _appSettings.ApplicationSettings.DymoPrinter;
pd.DefaultPageSettings.PrinterSettings.PrintToFile = false;
}

Font dateFont = new("Segoe UI", 10);
int dateFontHeight = (int)dateFont.GetHeight(graphics);
pd.PrintPage += new PrintPageEventHandler(PrintPage);

int maxHeight = BodyHeight;
if (_includeDate is not null)
maxHeight -= dateFontHeight;
if (pd.PrinterSettings.IsValid)
pd.Print();
}

float fontSize = 22;
Font font = new("Segoe UI", fontSize);
while (true)
{
SizeF stringSize = new SizeF();
stringSize = ev.Graphics.MeasureString(_address, font, BodyWidth);
private void PrintPage(object sender, PrintPageEventArgs ev)
{
if (ev.Graphics is null) return;
Graphics graphics = ev.Graphics;

Font dateFont = new("Segoe UI", 10);
int dateFontHeight = (int)dateFont.GetHeight(graphics);

if (stringSize.Height <= maxHeight)
break;
int maxHeight = BodyHeight;
if (_includeDate is not null)
maxHeight -= dateFontHeight;

fontSize = (float)(fontSize - 0.5);
font = new("Segoe UI", fontSize);
}
float fontSize = 22;
Font font = new("Segoe UI", fontSize);
while (true)
{
SizeF stringSize = new SizeF();
stringSize = ev.Graphics.MeasureString(_address, font, BodyWidth);

Brush brush = new SolidBrush(Color.Black);
Rectangle rectangle = new(MarginLeft, MarginTop, BodyWidth, maxHeight);
graphics.DrawString(_address, font, brush, rectangle);
if (stringSize.Height <= maxHeight)
break;

if (_includeDate is not null)
{
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Far;
sf.Alignment = StringAlignment.Far;
fontSize = (float)(fontSize - 0.5);
font = new("Segoe UI", fontSize);
}

rectangle = new(MarginLeft, MarginTop + BodyHeight - dateFontHeight, BodyWidth, dateFontHeight);
graphics.DrawString(_includeDate, dateFont, brush, rectangle, sf);
}
Brush brush = new SolidBrush(Color.Black);
Rectangle rectangle = new(MarginLeft, MarginTop, BodyWidth, maxHeight);
graphics.DrawString(_address, font, brush, rectangle);

ev.HasMorePages = false;
if (_includeDate is not null)
{
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Far;
sf.Alignment = StringAlignment.Far;

rectangle = new(MarginLeft, MarginTop + BodyHeight - dateFontHeight, BodyWidth, dateFontHeight);
graphics.DrawString(_includeDate, dateFont, brush, rectangle, sf);
}

ev.HasMorePages = false;
}
}
Loading