Skip to content

Commit ba16519

Browse files
authored
Merge pull request #556 from Shane32/movefiles
Split PayloadGenerator into a separate file for each class
2 parents 8bfcf40 + f06eae2 commit ba16519

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3229
-3054
lines changed

QRCoder/PayloadGenerator.cs

+1-3,054
Large diffs are not rendered by default.

QRCoder/PayloadGenerator/BezahlCode.cs

+570
Large diffs are not rendered by default.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace QRCoder
2+
{
3+
public static partial class PayloadGenerator
4+
{
5+
public class BitcoinAddress : BitcoinLikeCryptoCurrencyAddress
6+
{
7+
public BitcoinAddress(string address, double? amount, string label = null, string message = null)
8+
: base(BitcoinLikeCryptoCurrencyType.Bitcoin, address, amount, label, message) { }
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace QRCoder
2+
{
3+
public static partial class PayloadGenerator
4+
{
5+
public class BitcoinCashAddress : BitcoinLikeCryptoCurrencyAddress
6+
{
7+
public BitcoinCashAddress(string address, double? amount, string label = null, string message = null)
8+
: base(BitcoinLikeCryptoCurrencyType.BitcoinCash, address, amount, label, message) { }
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Globalization;
5+
6+
namespace QRCoder
7+
{
8+
public static partial class PayloadGenerator
9+
{
10+
public class BitcoinLikeCryptoCurrencyAddress : Payload
11+
{
12+
private readonly BitcoinLikeCryptoCurrencyType currencyType;
13+
private readonly string address, label, message;
14+
private readonly double? amount;
15+
16+
/// <summary>
17+
/// Generates a Bitcoin like cryptocurrency payment payload. QR Codes with this payload can open a payment app.
18+
/// </summary>
19+
/// <param name="currencyName">Bitcoin like cryptocurrency address of the payment receiver</param>
20+
/// <param name="address">Bitcoin like cryptocurrency address of the payment receiver</param>
21+
/// <param name="amount">Amount of coins to transfer</param>
22+
/// <param name="label">Reference label</param>
23+
/// <param name="message">Referece text aka message</param>
24+
public BitcoinLikeCryptoCurrencyAddress(BitcoinLikeCryptoCurrencyType currencyType, string address, double? amount, string label = null, string message = null)
25+
{
26+
this.currencyType = currencyType;
27+
this.address = address;
28+
29+
if (!string.IsNullOrEmpty(label))
30+
{
31+
this.label = Uri.EscapeDataString(label);
32+
}
33+
34+
if (!string.IsNullOrEmpty(message))
35+
{
36+
this.message = Uri.EscapeDataString(message);
37+
}
38+
39+
this.amount = amount;
40+
}
41+
42+
public override string ToString()
43+
{
44+
string query = null;
45+
46+
var queryValues = new KeyValuePair<string,string>[]{
47+
new KeyValuePair<string, string>(nameof(label), label),
48+
new KeyValuePair<string, string>(nameof(message), message),
49+
new KeyValuePair<string, string>(nameof(amount), amount.HasValue ? amount.Value.ToString("#.########", CultureInfo.InvariantCulture) : null)
50+
};
51+
52+
if (queryValues.Any(keyPair => !string.IsNullOrEmpty(keyPair.Value)))
53+
{
54+
query = "?" + string.Join("&", queryValues
55+
.Where(keyPair => !string.IsNullOrEmpty(keyPair.Value))
56+
.Select(keyPair => $"{keyPair.Key}={keyPair.Value}")
57+
.ToArray());
58+
}
59+
60+
return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), currencyType).ToLower()}:{address}{query}";
61+
}
62+
63+
public enum BitcoinLikeCryptoCurrencyType
64+
{
65+
Bitcoin,
66+
BitcoinCash,
67+
Litecoin
68+
}
69+
}
70+
}
71+
}

QRCoder/PayloadGenerator/Bookmark.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace QRCoder
2+
{
3+
public static partial class PayloadGenerator
4+
{
5+
public class Bookmark : Payload
6+
{
7+
private readonly string url, title;
8+
9+
/// <summary>
10+
/// Generates a bookmark payload. Scanned by an QR Code reader, this one creates a browser bookmark.
11+
/// </summary>
12+
/// <param name="url">Url of the bookmark</param>
13+
/// <param name="title">Title of the bookmark</param>
14+
public Bookmark(string url, string title)
15+
{
16+
this.url = EscapeInput(url);
17+
this.title = EscapeInput(title);
18+
}
19+
20+
public override string ToString()
21+
{
22+
return $"MEBKM:TITLE:{this.title};URL:{this.url};;";
23+
}
24+
}
25+
}
26+
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
3+
namespace QRCoder
4+
{
5+
public static partial class PayloadGenerator
6+
{
7+
public class CalendarEvent : Payload
8+
{
9+
private readonly string subject, description, location, start, end;
10+
private readonly EventEncoding encoding;
11+
12+
/// <summary>
13+
/// Generates a calender entry/event payload.
14+
/// </summary>
15+
/// <param name="subject">Subject/title of the calender event</param>
16+
/// <param name="description">Description of the event</param>
17+
/// <param name="location">Location (lat:long or address) of the event</param>
18+
/// <param name="start">Start time (incl. UTC offset) of the event</param>
19+
/// <param name="end">End time (incl. UTC offset) of the event</param>
20+
/// <param name="allDayEvent">Is it a full day event?</param>
21+
/// <param name="encoding">Type of encoding (universal or iCal)</param>
22+
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)
23+
{
24+
}
25+
26+
/// <summary>
27+
/// Generates a calender entry/event payload.
28+
/// </summary>
29+
/// <param name="subject">Subject/title of the calender event</param>
30+
/// <param name="description">Description of the event</param>
31+
/// <param name="location">Location (lat:long or address) of the event</param>
32+
/// <param name="start">Start time of the event</param>
33+
/// <param name="end">End time of the event</param>
34+
/// <param name="allDayEvent">Is it a full day event?</param>
35+
/// <param name="encoding">Type of encoding (universal or iCal)</param>
36+
public CalendarEvent(string subject, string description, string location, DateTime start, DateTime end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal)
37+
{
38+
this.subject = subject;
39+
this.description = description;
40+
this.location = location;
41+
this.encoding = encoding;
42+
string dtFormatStart = "yyyyMMdd", dtFormatEnd = "yyyyMMdd";
43+
if (!allDayEvent)
44+
{
45+
dtFormatStart = dtFormatEnd = "yyyyMMddTHHmmss";
46+
if (start.Kind == DateTimeKind.Utc)
47+
dtFormatStart = "yyyyMMddTHHmmssZ";
48+
if (end.Kind == DateTimeKind.Utc)
49+
dtFormatEnd = "yyyyMMddTHHmmssZ";
50+
}
51+
this.start = start.ToString(dtFormatStart);
52+
this.end = end.ToString(dtFormatEnd);
53+
}
54+
55+
public override string ToString()
56+
{
57+
var vEvent = $"BEGIN:VEVENT{Environment.NewLine}";
58+
vEvent += $"SUMMARY:{this.subject}{Environment.NewLine}";
59+
vEvent += !string.IsNullOrEmpty(this.description) ? $"DESCRIPTION:{this.description}{Environment.NewLine}" : "";
60+
vEvent += !string.IsNullOrEmpty(this.location) ? $"LOCATION:{this.location}{Environment.NewLine}" : "";
61+
vEvent += $"DTSTART:{this.start}{Environment.NewLine}";
62+
vEvent += $"DTEND:{this.end}{Environment.NewLine}";
63+
vEvent += "END:VEVENT";
64+
65+
if (this.encoding == EventEncoding.iCalComplete)
66+
vEvent = $@"BEGIN:VCALENDAR{Environment.NewLine}VERSION:2.0{Environment.NewLine}{vEvent}{Environment.NewLine}END:VCALENDAR";
67+
68+
return vEvent;
69+
}
70+
71+
public enum EventEncoding
72+
{
73+
iCalComplete,
74+
Universal
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)