Skip to content

Commit 48ecb2e

Browse files
committed
QrCodeBuilder experimentation
1 parent ba16519 commit 48ecb2e

22 files changed

+692
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IConfigurableEccLevel
4+
{
5+
QRCodeGenerator.ECCLevel EccLevel { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IConfigurableEciMode
4+
{
5+
QRCodeGenerator.EciMode EciMode { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IConfigurableVersion
4+
{
5+
int Version { get; set; }
6+
}
7+
}

QRCoder/Builders/Payloads/IPayload.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IPayload
4+
{
5+
QRCodeData ToMatrix();
6+
}
7+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace QRCoder.Builders.Payloads.Implementations
4+
{
5+
public class EmailPayload : PayloadBase
6+
{
7+
public EmailPayload(string address)
8+
{
9+
_address = address;
10+
}
11+
12+
private string _address { get; set; }
13+
private string _subject { get; set; }
14+
private string _body { get; set; }
15+
private PayloadGenerator.Mail.MailEncoding _encoding { get; set; } = PayloadGenerator.Mail.MailEncoding.MAILTO;
16+
17+
public EmailPayload WithSubject(string subject)
18+
{
19+
_subject = subject;
20+
return this;
21+
}
22+
23+
public EmailPayload WithBody(string body)
24+
{
25+
_body = body;
26+
return this;
27+
}
28+
29+
public EmailPayload WithEncoding(PayloadGenerator.Mail.MailEncoding encoding)
30+
{
31+
if (encoding != PayloadGenerator.Mail.MailEncoding.MAILTO && encoding != PayloadGenerator.Mail.MailEncoding.SMTP && encoding != PayloadGenerator.Mail.MailEncoding.MATMSG)
32+
{
33+
throw new ArgumentOutOfRangeException(nameof(encoding));
34+
}
35+
_encoding = encoding;
36+
return this;
37+
}
38+
39+
protected override string Value => new PayloadGenerator.Mail(_address, _subject, _body, _encoding).ToString();
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace QRCoder.Builders.Payloads.Implementations
2+
{
3+
public class StringPayload : PayloadBase
4+
{
5+
private string _data;
6+
7+
public StringPayload(string data)
8+
{
9+
_data = data;
10+
}
11+
12+
protected override string Value => _data;
13+
}
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace QRCoder.Builders.Payloads.Implementations
2+
{
3+
public class WiFiPayload : PayloadBase
4+
{
5+
private string _ssid { get; set; }
6+
private string _password { get; set; }
7+
private PayloadGenerator.WiFi.Authentication _authentication { get; set; }
8+
private bool _isHiddenSSID { get; set; }
9+
private bool _isHexStrings { get; set; }
10+
11+
public WiFiPayload(string ssid)
12+
{
13+
_ssid = ssid;
14+
_password = "";
15+
_authentication = PayloadGenerator.WiFi.Authentication.nopass;
16+
}
17+
18+
public WiFiPayload(string ssid, string password, PayloadGenerator.WiFi.Authentication authentication)
19+
{
20+
_ssid = ssid;
21+
_password = password;
22+
_authentication = authentication;
23+
}
24+
25+
public WiFiPayload WithHiddenSSID()
26+
{
27+
_isHiddenSSID = true;
28+
return this;
29+
}
30+
31+
public WiFiPayload WithHexStrings()
32+
{
33+
_isHexStrings = true;
34+
return this;
35+
}
36+
37+
protected override string Value
38+
{
39+
get
40+
{
41+
var wifi = new PayloadGenerator.WiFi(_ssid, _password, _authentication, _isHiddenSSID, _isHexStrings);
42+
return wifi.ToString();
43+
}
44+
}
45+
}
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public abstract class PayloadBase : IPayload, IConfigurableEccLevel, IConfigurableEciMode, IConfigurableVersion
4+
{
5+
protected virtual QRCodeGenerator.ECCLevel EccLevel { get; set; } = QRCodeGenerator.ECCLevel.Default;
6+
QRCodeGenerator.ECCLevel IConfigurableEccLevel.EccLevel { get => EccLevel; set => EccLevel = value; }
7+
8+
protected virtual QRCodeGenerator.EciMode EciMode { get; set; } = QRCodeGenerator.EciMode.Default;
9+
QRCodeGenerator.EciMode IConfigurableEciMode.EciMode { get => EciMode; set => EciMode = value; }
10+
11+
protected virtual int Version { get; set; } = -1;
12+
int IConfigurableVersion.Version { get => Version; set => Version = value; }
13+
14+
protected abstract string Value { get; }
15+
16+
public virtual QRCodeData ToMatrix()
17+
{
18+
return QRCodeGenerator.GenerateQrCode(Value, EccLevel, false, false, EciMode, Version);
19+
}
20+
}
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using QRCoder.Builders.Payloads;
2+
using QRCoder.Builders.Renderers;
3+
using QRCoder.Builders.Renderers.Implementations;
4+
5+
namespace QRCoder
6+
{
7+
public static class PayloadExtensions
8+
{
9+
public static T WithErrorCorrection<T>(this T payload, QRCodeGenerator.ECCLevel eccLevel)
10+
where T : IConfigurableEccLevel
11+
{
12+
payload.EccLevel = eccLevel;
13+
return payload;
14+
}
15+
16+
public static T WithEciMode<T>(this T payload, QRCodeGenerator.EciMode eciMode)
17+
where T : IConfigurableEciMode
18+
{
19+
payload.EciMode = eciMode;
20+
return payload;
21+
}
22+
23+
public static T WithVersion<T>(this T payload, int version)
24+
where T : IConfigurableVersion
25+
{
26+
payload.Version = version;
27+
return payload;
28+
}
29+
30+
public static T RenderWith<T>(this IPayload payload)
31+
where T : IRenderer, new()
32+
{
33+
var renderer = new T();
34+
renderer.Payload = payload;
35+
return renderer;
36+
}
37+
38+
public static T RenderWith<T>(this IPayload payload, int pixelsPerModule)
39+
where T : IRenderer, IConfigurablePixelsPerModule, new()
40+
{
41+
var renderer = new T();
42+
renderer.Payload = payload;
43+
renderer.PixelsPerModule = pixelsPerModule;
44+
return renderer;
45+
}
46+
}
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Renderers
2+
{
3+
public interface IConfigurablePixelsPerModule
4+
{
5+
int PixelsPerModule { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)