Skip to content

Commit e36bd19

Browse files
committed
Split PayloadGenerator into a separate file for each class
1 parent 8bfcf40 commit e36bd19

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

+3310
-3054
lines changed

QRCoder/PayloadGenerator.cs

Lines changed: 1 addition & 3054 deletions
Large diffs are not rendered by default.

QRCoder/PayloadGenerator/BezahlCode.cs

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

QRCoder/PayloadGenerator/Bookmark.cs

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

0 commit comments

Comments
 (0)