Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split PayloadGenerator into a separate file for each class #556

Merged
merged 2 commits into from
May 29, 2024
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
3,055 changes: 1 addition & 3,054 deletions QRCoder/PayloadGenerator.cs

Large diffs are not rendered by default.

570 changes: 570 additions & 0 deletions QRCoder/PayloadGenerator/BezahlCode.cs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions QRCoder/PayloadGenerator/BitcoinAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace QRCoder
{
public static partial class PayloadGenerator
{
public class BitcoinAddress : BitcoinLikeCryptoCurrencyAddress
{
public BitcoinAddress(string address, double? amount, string label = null, string message = null)
: base(BitcoinLikeCryptoCurrencyType.Bitcoin, address, amount, label, message) { }
}
}
}
11 changes: 11 additions & 0 deletions QRCoder/PayloadGenerator/BitcoinCashAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace QRCoder
{
public static partial class PayloadGenerator
{
public class BitcoinCashAddress : BitcoinLikeCryptoCurrencyAddress
{
public BitcoinCashAddress(string address, double? amount, string label = null, string message = null)
: base(BitcoinLikeCryptoCurrencyType.BitcoinCash, address, amount, label, message) { }
}
}
}
71 changes: 71 additions & 0 deletions QRCoder/PayloadGenerator/BitcoinLikeCryptoCurrencyAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;

namespace QRCoder
{
public static partial class PayloadGenerator
{
public class BitcoinLikeCryptoCurrencyAddress : Payload
{
private readonly BitcoinLikeCryptoCurrencyType currencyType;
private readonly string address, label, message;
private readonly double? amount;

/// <summary>
/// Generates a Bitcoin like cryptocurrency payment payload. QR Codes with this payload can open a payment app.
/// </summary>
/// <param name="currencyName">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="address">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="amount">Amount of coins to transfer</param>
/// <param name="label">Reference label</param>
/// <param name="message">Referece text aka message</param>
public BitcoinLikeCryptoCurrencyAddress(BitcoinLikeCryptoCurrencyType currencyType, string address, double? amount, string label = null, string message = null)
{
this.currencyType = currencyType;
this.address = address;

if (!string.IsNullOrEmpty(label))
{
this.label = Uri.EscapeDataString(label);
}

if (!string.IsNullOrEmpty(message))
{
this.message = Uri.EscapeDataString(message);
}

this.amount = amount;
}

public override string ToString()
{
string query = null;

var queryValues = new KeyValuePair<string,string>[]{
new KeyValuePair<string, string>(nameof(label), label),
new KeyValuePair<string, string>(nameof(message), message),
new KeyValuePair<string, string>(nameof(amount), amount.HasValue ? amount.Value.ToString("#.########", CultureInfo.InvariantCulture) : null)
};

if (queryValues.Any(keyPair => !string.IsNullOrEmpty(keyPair.Value)))
{
query = "?" + string.Join("&", queryValues
.Where(keyPair => !string.IsNullOrEmpty(keyPair.Value))
.Select(keyPair => $"{keyPair.Key}={keyPair.Value}")
.ToArray());
}

return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), currencyType).ToLower()}:{address}{query}";
}

public enum BitcoinLikeCryptoCurrencyType
{
Bitcoin,
BitcoinCash,
Litecoin
}
}
}
}
26 changes: 26 additions & 0 deletions QRCoder/PayloadGenerator/Bookmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace QRCoder
{
public static partial class PayloadGenerator
{
public class Bookmark : Payload
{
private readonly string url, title;

/// <summary>
/// Generates a bookmark payload. Scanned by an QR Code reader, this one creates a browser bookmark.
/// </summary>
/// <param name="url">Url of the bookmark</param>
/// <param name="title">Title of the bookmark</param>
public Bookmark(string url, string title)
{
this.url = EscapeInput(url);
this.title = EscapeInput(title);
}

public override string ToString()
{
return $"MEBKM:TITLE:{this.title};URL:{this.url};;";
}
}
}
}
78 changes: 78 additions & 0 deletions QRCoder/PayloadGenerator/CalendarEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;

namespace QRCoder
{
public static partial class PayloadGenerator
{
public class CalendarEvent : Payload
{
private readonly string subject, description, location, start, end;
private readonly EventEncoding encoding;

/// <summary>
/// Generates a calender entry/event payload.
/// </summary>
/// <param name="subject">Subject/title of the calender event</param>
/// <param name="description">Description of the event</param>
/// <param name="location">Location (lat:long or address) of the event</param>
/// <param name="start">Start time (incl. UTC offset) of the event</param>
/// <param name="end">End time (incl. UTC offset) of the event</param>
/// <param name="allDayEvent">Is it a full day event?</param>
/// <param name="encoding">Type of encoding (universal or iCal)</param>
public CalendarEvent(string subject, string description, string location, DateTimeOffset start, DateTimeOffset end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal) : this(subject, description, location, start.UtcDateTime, end.UtcDateTime, allDayEvent, encoding)
{
}

/// <summary>
/// Generates a calender entry/event payload.
/// </summary>
/// <param name="subject">Subject/title of the calender event</param>
/// <param name="description">Description of the event</param>
/// <param name="location">Location (lat:long or address) of the event</param>
/// <param name="start">Start time of the event</param>
/// <param name="end">End time of the event</param>
/// <param name="allDayEvent">Is it a full day event?</param>
/// <param name="encoding">Type of encoding (universal or iCal)</param>
public CalendarEvent(string subject, string description, string location, DateTime start, DateTime end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal)
{
this.subject = subject;
this.description = description;
this.location = location;
this.encoding = encoding;
string dtFormatStart = "yyyyMMdd", dtFormatEnd = "yyyyMMdd";
if (!allDayEvent)
{
dtFormatStart = dtFormatEnd = "yyyyMMddTHHmmss";
if (start.Kind == DateTimeKind.Utc)
dtFormatStart = "yyyyMMddTHHmmssZ";
if (end.Kind == DateTimeKind.Utc)
dtFormatEnd = "yyyyMMddTHHmmssZ";
}
this.start = start.ToString(dtFormatStart);
this.end = end.ToString(dtFormatEnd);
}

public override string ToString()
{
var vEvent = $"BEGIN:VEVENT{Environment.NewLine}";
vEvent += $"SUMMARY:{this.subject}{Environment.NewLine}";
vEvent += !string.IsNullOrEmpty(this.description) ? $"DESCRIPTION:{this.description}{Environment.NewLine}" : "";
vEvent += !string.IsNullOrEmpty(this.location) ? $"LOCATION:{this.location}{Environment.NewLine}" : "";
vEvent += $"DTSTART:{this.start}{Environment.NewLine}";
vEvent += $"DTEND:{this.end}{Environment.NewLine}";
vEvent += "END:VEVENT";

if (this.encoding == EventEncoding.iCalComplete)
vEvent = $@"BEGIN:VCALENDAR{Environment.NewLine}VERSION:2.0{Environment.NewLine}{vEvent}{Environment.NewLine}END:VCALENDAR";

return vEvent;
}

public enum EventEncoding
{
iCalComplete,
Universal
}
}
}
}
Loading