Navaron Bracke

make BarcodeCorners non-null; update Barcode documentation; fix default construc…

…tor values; sort attributes alphabetically
1 import 'dart:typed_data'; 1 import 'dart:typed_data';
2 import 'dart:ui'; 2 import 'dart:ui';
3 3
4 -import 'package:mobile_scanner/src/barcode_utility.dart';  
5 import 'package:mobile_scanner/src/enums/barcode_format.dart'; 4 import 'package:mobile_scanner/src/enums/barcode_format.dart';
6 import 'package:mobile_scanner/src/enums/barcode_type.dart'; 5 import 'package:mobile_scanner/src/enums/barcode_type.dart';
7 import 'package:mobile_scanner/src/objects/calendar_event.dart'; 6 import 'package:mobile_scanner/src/objects/calendar_event.dart';
@@ -16,108 +15,147 @@ import 'package:mobile_scanner/src/objects/wifi.dart'; @@ -16,108 +15,147 @@ import 'package:mobile_scanner/src/objects/wifi.dart';
16 15
17 /// Represents a single recognized barcode and its value. 16 /// Represents a single recognized barcode and its value.
18 class Barcode { 17 class Barcode {
19 - /// Returns four corner points in clockwise direction starting with top-left.  
20 - ///  
21 - /// Due to the possible perspective distortions, this is not necessarily a rectangle.  
22 - ///  
23 - /// Returns null if the corner points can not be determined.  
24 - final List<Offset>? corners; 18 + /// Creates a new [Barcode] instance.
  19 + const Barcode({
  20 + this.calendarEvent,
  21 + this.contactInfo,
  22 + this.corners = const <Offset>[],
  23 + this.displayValue,
  24 + this.driverLicense,
  25 + this.email,
  26 + this.format = BarcodeFormat.unknown,
  27 + this.geoPoint,
  28 + this.phone,
  29 + this.rawBytes,
  30 + this.rawValue,
  31 + this.sms,
  32 + this.type = BarcodeType.unknown,
  33 + this.url,
  34 + this.wifi,
  35 + });
25 36
26 - /// Returns barcode format  
27 - final BarcodeFormat format; 37 + /// Creates a new [Barcode] instance from the given [data].
  38 + factory Barcode.fromNative(Map<Object?, Object?> data) {
  39 + final Map<Object?, Object?>? calendarEvent =
  40 + data['calendarEvent'] as Map<Object?, Object?>?;
  41 + final List<Object?>? corners = data['corners'] as List<Object?>?;
  42 + final Map<Object?, Object?>? contactInfo =
  43 + data['contactInfo'] as Map<Object?, Object?>?;
  44 + final Map<Object?, Object?>? driverLicense =
  45 + data['driverLicense'] as Map<Object?, Object?>?;
  46 + final Map<Object?, Object?>? email =
  47 + data['email'] as Map<Object?, Object?>?;
  48 + final Map<Object?, Object?>? geoPoint =
  49 + data['geoPoint'] as Map<Object?, Object?>?;
  50 + final Map<Object?, Object?>? phone =
  51 + data['phone'] as Map<Object?, Object?>?;
  52 + final Map<Object?, Object?>? sms = data['sms'] as Map<Object?, Object?>?;
  53 + final Map<Object?, Object?>? url = data['url'] as Map<Object?, Object?>?;
  54 + final Map<Object?, Object?>? wifi = data['wifi'] as Map<Object?, Object?>?;
  55 +
  56 + return Barcode(
  57 + calendarEvent: calendarEvent == null
  58 + ? null
  59 + : CalendarEvent.fromNative(calendarEvent),
  60 + contactInfo:
  61 + contactInfo == null ? null : ContactInfo.fromNative(contactInfo),
  62 + corners: corners == null
  63 + ? const <Offset>[]
  64 + : List.unmodifiable(
  65 + corners.cast<Map<String, double>>().map((Map<String, double> e) {
  66 + return Offset(e['x']!, e['y']!);
  67 + }),
  68 + ),
  69 + displayValue: data['displayValue'] as String?,
  70 + driverLicense: driverLicense == null
  71 + ? null
  72 + : DriverLicense.fromNative(driverLicense),
  73 + email: email == null ? null : Email.fromNative(email),
  74 + format: BarcodeFormat.fromRawValue(data['format'] as int? ?? -1),
  75 + geoPoint: geoPoint == null ? null : GeoPoint.fromNative(geoPoint),
  76 + phone: phone == null ? null : Phone.fromNative(phone),
  77 + rawBytes: data['rawBytes'] as Uint8List?,
  78 + rawValue: data['rawValue'] as String?,
  79 + sms: sms == null ? null : SMS.fromNative(sms),
  80 + type: BarcodeType.fromRawValue(data['type'] as int? ?? 0),
  81 + url: url == null ? null : UrlBookmark.fromNative(url),
  82 + wifi: wifi == null ? null : WiFi.fromNative(wifi),
  83 + );
  84 + }
  85 +
  86 + /// The calendar event that is embedded in the barcode.
  87 + final CalendarEvent? calendarEvent;
28 88
29 - /// Returns raw bytes as it was encoded in the barcode.  
30 - ///  
31 - /// Returns null if the raw bytes can not be determined.  
32 - final Uint8List? rawBytes; 89 + /// The contact information that is embedded in the barcode.
  90 + final ContactInfo? contactInfo;
33 91
34 - /// Returns barcode value as it was encoded in the barcode. Structured values are not parsed, for example: 'MEBKM:TITLE:Google;URL://www.google.com;;'. 92 + /// The four corner points of the barcode,
  93 + /// in clockwise order, starting with the top-left point.
35 /// 94 ///
36 - /// It's only available when the barcode is encoded in the UTF-8 format, and for non-UTF8 ones use [rawBytes] instead. 95 + /// Due to the possible perspective distortions, this is not necessarily a rectangle.
37 /// 96 ///
38 - /// Returns null if the raw value can not be determined.  
39 - final String? rawValue; 97 + /// This list is empty if the corners can not be determined.
  98 + final List<Offset> corners;
40 99
41 - /// Returns barcode value in a user-friendly format. 100 + /// The barcode value in a user-friendly format.
42 /// 101 ///
43 - /// This method may omit some of the information encoded in the barcode. For example, if [rawValue] returns 'MEBKM:TITLE:Google;URL://www.google.com;;', the display value might be '//www.google.com'. 102 + /// This value may omit some of the information encoded in the barcode.
  103 + /// For example, if [rawValue] returns `MEBKM:TITLE:Google;URL://www.google.com;;`,
  104 + /// the display value might be `//www.google.com`.
44 /// 105 ///
45 - /// This value may be multiline, for example, when line breaks are encoded into the original TEXT barcode value. May include the supplement value. 106 + /// This value may be multiline if line breaks are encoded in the barcode.
  107 + /// This value may include the supplement value.
46 /// 108 ///
47 - /// Returns null if nothing found. 109 + /// This is null if there is no user-friendly value for the given barcode.
48 final String? displayValue; 110 final String? displayValue;
49 111
50 - /// Returns format type of the barcode value.  
51 - ///  
52 - /// For example, TYPE_TEXT, TYPE_PRODUCT, TYPE_URL, etc.  
53 - ///  
54 - /// If the value structure cannot be parsed, TYPE_TEXT will be returned. If the recognized structure type is not defined in your current version of SDK, TYPE_UNKNOWN will be returned.  
55 - ///  
56 - /// Note that the built-in parsers only recognize a few popular value structures. For your specific use case, you might want to directly consume rawValue and implement your own parsing logic.  
57 - final BarcodeType type;  
58 -  
59 - /// Gets parsed calendar event details.  
60 - final CalendarEvent? calendarEvent;  
61 -  
62 - /// Gets parsed contact details.  
63 - final ContactInfo? contactInfo;  
64 -  
65 - /// Gets parsed driver license details. 112 + /// The driver license information that is embedded in the barcode.
66 final DriverLicense? driverLicense; 113 final DriverLicense? driverLicense;
67 114
68 - /// Gets parsed email details. 115 + /// The email message that is embedded in the barcode.
69 final Email? email; 116 final Email? email;
70 117
71 - /// Gets parsed geo coordinates. 118 + /// The format of the barcode.
  119 + final BarcodeFormat format;
  120 +
  121 + /// The geographic point that is embedded in the barcode.
72 final GeoPoint? geoPoint; 122 final GeoPoint? geoPoint;
73 123
74 - /// Gets parsed phone number details. 124 + /// The phone number that is embedded in the barcode.
75 final Phone? phone; 125 final Phone? phone;
76 126
77 - /// Gets parsed SMS details. 127 + /// The raw bytes of the barcode.
  128 + ///
  129 + /// This is null if the raw bytes are not available.
  130 + final Uint8List? rawBytes;
  131 +
  132 + /// The raw value of `UTF-8` encoded barcodes.
  133 + ///
  134 + /// Structured values are not parsed,
  135 + /// for example: 'MEBKM:TITLE:Google;URL://www.google.com;;'.
  136 + ///
  137 + /// For non-UTF-8 barcodes, prefer using [rawBytes] instead.
  138 + ///
  139 + /// This is null if the raw value is not available.
  140 + final String? rawValue;
  141 +
  142 + /// The SMS message that is embedded in the barcode.
78 final SMS? sms; 143 final SMS? sms;
79 144
80 - /// Gets parsed URL bookmark details. 145 + /// The type of the [format] of the barcode.
  146 + ///
  147 + /// For types that are recognized,
  148 + /// but could not be parsed correctly, [BarcodeType.text] will be returned.
  149 + ///
  150 + /// For types that are not recognised, [BarcodeType.unknown] will be returned.
  151 + ///
  152 + /// If a given barcode was not correctly identified,
  153 + /// consider parsing [rawValue] manually instead.
  154 + final BarcodeType type;
  155 +
  156 + /// The URL bookmark that is embedded in the barcode.
81 final UrlBookmark? url; 157 final UrlBookmark? url;
82 158
83 - /// Gets parsed WiFi AP details. 159 + /// The Wireless network information that is embedded in the barcode.
84 final WiFi? wifi; 160 final WiFi? wifi;
85 -  
86 - Barcode({  
87 - this.corners,  
88 - this.format = BarcodeFormat.ean13,  
89 - this.rawBytes,  
90 - this.type = BarcodeType.text,  
91 - this.calendarEvent,  
92 - this.contactInfo,  
93 - this.driverLicense,  
94 - this.email,  
95 - this.geoPoint,  
96 - this.phone,  
97 - this.sms,  
98 - this.url,  
99 - this.wifi,  
100 - this.displayValue,  
101 - required this.rawValue,  
102 - });  
103 -  
104 - /// Create a [Barcode] from native data.  
105 - Barcode.fromNative(Map data)  
106 - : corners = toCorners(  
107 - (data['corners'] as List?)?.cast<Map<Object?, Object?>>(),  
108 - ),  
109 - format = toFormat(data['format'] as int),  
110 - rawBytes = data['rawBytes'] as Uint8List?,  
111 - rawValue = data['rawValue'] as String?,  
112 - displayValue = data['displayValue'] as String?,  
113 - type = BarcodeType.values[data['type'] as int],  
114 - calendarEvent = toCalendarEvent(data['calendarEvent'] as Map?),  
115 - contactInfo = toContactInfo(data['contactInfo'] as Map?),  
116 - driverLicense = toDriverLicense(data['driverLicense'] as Map?),  
117 - email = toEmail(data['email'] as Map?),  
118 - geoPoint = toGeoPoint(data['geoPoint'] as Map?),  
119 - phone = toPhone(data['phone'] as Map?),  
120 - sms = toSMS(data['sms'] as Map?),  
121 - url = toUrl(data['url'] as Map?),  
122 - wifi = toWiFi(data['wifi'] as Map?);  
123 } 161 }