-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #556 from Shane32/movefiles
Split PayloadGenerator into a separate file for each class
- Loading branch information
Showing
41 changed files
with
3,229 additions
and
3,054 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
QRCoder/PayloadGenerator/BitcoinLikeCryptoCurrencyAddress.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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};;"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.