Refactors
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
import 'package:dartboard_resume/models/dartboard_misc.dart';
|
||||
import 'package:dartboard_resume/models/date_range.dart';
|
||||
import 'package:dartboard_resume/models/document_text.dart';
|
||||
import 'package:dartboard_resume/models/experience.dart';
|
||||
import 'package:dartboard_resume/models/theme.dart';
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart';
|
||||
import 'package:toml/toml.dart';
|
||||
|
||||
class DartboardData {
|
||||
DartboardData({
|
||||
required this.fullName,
|
||||
required this.phoneNumber,
|
||||
required this.email,
|
||||
// required this.address,
|
||||
required this.imagePath,
|
||||
required this.dartboardTheme,
|
||||
required this.experiences,
|
||||
required this.miscList,
|
||||
});
|
||||
|
||||
factory DartboardData.fromToml(String tomlFilePath) {
|
||||
final tomlData = TomlDocument.loadSync(tomlFilePath).toMap();
|
||||
|
||||
final List<Experience> exps = tomlData.entries
|
||||
.where((e) => Experience.filter(e.value))
|
||||
.map((MapEntry<String, dynamic> expsEntry) {
|
||||
final String subsection =
|
||||
tomlData['${expsEntry.key}_name'] as String? ?? _getSubsectionFromKey(expsEntry.key);
|
||||
final exps = (expsEntry.value as List).map((e) => e as Map<String, dynamic>).toList();
|
||||
return exps.map((exp) {
|
||||
final TomlLocalDate? start = exp['start'] as TomlLocalDate?;
|
||||
final TomlLocalDate? end = exp['end'] as TomlLocalDate?;
|
||||
return Experience(
|
||||
title: exp['title'] as String,
|
||||
subsection: subsection,
|
||||
range: DateRange(
|
||||
start: start == null ? null : DateTime(start.date.year, start.date.month),
|
||||
end: end == null ? null : DateTime(end.date.year, end.date.month),
|
||||
),
|
||||
attributes: (exp['attributes'] as List)
|
||||
.where((e) => e is String && e.isNotEmpty)
|
||||
.map((e) => DocumentText(content: e as String))
|
||||
.toList(),
|
||||
location: exp['location'] as String?,
|
||||
);
|
||||
}).toList();
|
||||
})
|
||||
.expand((i) => i)
|
||||
.toList();
|
||||
final List<DartboardMisc> misc =
|
||||
tomlData.entries.where((e) => DartboardMisc.filter(e.value)).map((MapEntry<String, dynamic> listEntry) {
|
||||
final String subsection = tomlData['${listEntry.key}_name'] as String? ?? _getSubsectionFromKey(listEntry.key);
|
||||
return DartboardMisc(
|
||||
subsection: subsection,
|
||||
attributes: (listEntry.value as List)
|
||||
.map((e) => (e as Map<String, dynamic>)['attributes'] as List)
|
||||
.expand((i) => i)
|
||||
.where((e) => e is String && e.isNotEmpty)
|
||||
.map((e) => DocumentText(content: e as String))
|
||||
.toList(),
|
||||
);
|
||||
}).toList();
|
||||
return DartboardData(
|
||||
fullName: tomlData['full_name'] as String,
|
||||
phoneNumber: tomlData['phone_number'] as String?,
|
||||
email: tomlData['email'] as String?,
|
||||
imagePath: tomlData['image'] as String?,
|
||||
dartboardTheme: DocumentTheme.fromToml(tomlData),
|
||||
experiences: exps,
|
||||
miscList: misc,
|
||||
);
|
||||
}
|
||||
|
||||
final String fullName;
|
||||
final String? phoneNumber;
|
||||
final String? email;
|
||||
final String? imagePath;
|
||||
final DocumentTheme dartboardTheme;
|
||||
final List<Experience> experiences;
|
||||
final List<DartboardMisc> miscList;
|
||||
|
||||
Font get font => dartboardTheme.font;
|
||||
|
||||
PdfColor get primaryColor => dartboardTheme.primaryColor;
|
||||
PdfColor get accentColor => dartboardTheme.accentColor;
|
||||
PdfColor get backgroundColor => dartboardTheme.backgroundColor;
|
||||
|
||||
TextStyle get headerTextStyle =>
|
||||
TextStyle(fontSize: 18, font: font, fontWeight: FontWeight.bold, color: const PdfColorGrey(0.18));
|
||||
TextStyle get subheaderTextStyle => TextStyle(fontSize: 14, font: font, color: const PdfColorGrey(0.3));
|
||||
TextStyle get defaultTextStyle => TextStyle(fontSize: 10, font: font, color: const PdfColorGrey(0.45));
|
||||
|
||||
Map<String, List<Experience>> get groupedExperiences {
|
||||
final Map<String, List<Experience>> exps = {};
|
||||
for (final Experience exp in experiences) {
|
||||
if (!exps.containsKey(exp.subsection)) {
|
||||
exps[exp.subsection] = <Experience>[];
|
||||
}
|
||||
exps[exp.subsection]!.add(exp);
|
||||
}
|
||||
return exps;
|
||||
}
|
||||
|
||||
Map<String, List<DartboardMisc>> get groupedMisc {
|
||||
final Map<String, List<DartboardMisc>> miscs = {};
|
||||
for (final DartboardMisc misc in miscList) {
|
||||
if (!miscs.containsKey(misc.subsection)) {
|
||||
miscs[misc.subsection] = <DartboardMisc>[];
|
||||
}
|
||||
miscs[misc.subsection]!.add(misc);
|
||||
}
|
||||
return miscs;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hashAll([fullName, phoneNumber, email, imagePath, dartboardTheme, ...experiences, ...miscList]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return super.hashCode == other.hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
String _getSubsectionFromKey(String key) {
|
||||
switch (key) {
|
||||
case 'exp':
|
||||
return 'Experience';
|
||||
case 'misc':
|
||||
return 'Miscelaneous';
|
||||
case 'edu':
|
||||
return 'Education';
|
||||
case '':
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:dartboard_resume/models/document_text.dart';
|
||||
|
||||
class DartboardMisc {
|
||||
DartboardMisc({required this.subsection, required this.attributes});
|
||||
|
||||
final String subsection;
|
||||
final List<DocumentText> attributes;
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hashAll([subsection, ...attributes]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return super.hashCode == other.hashCode;
|
||||
}
|
||||
|
||||
static bool filter(dynamic e) {
|
||||
if (e is! List || e.firstOrNull is! Map) {
|
||||
return false;
|
||||
}
|
||||
final entries = (e.first as Map).entries;
|
||||
return entries.length == 1 && entries.first.key == 'attributes';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class DateRange {
|
||||
DateRange({required this.start, required this.end});
|
||||
final DateTime? start;
|
||||
final DateTime? end;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
final DateFormat dateFormat = DateFormat("MMM yyyy");
|
||||
if (start == null && end == null) {
|
||||
return '';
|
||||
}
|
||||
if (start == null && end != null) {
|
||||
return dateFormat.format(end!);
|
||||
}
|
||||
if (start != null && end == null) {
|
||||
return "${dateFormat.format(start!)} - Current";
|
||||
}
|
||||
return "${dateFormat.format(start!)} - ${dateFormat.format(end!)}";
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hashAll([start, end]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return super.hashCode == other.hashCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
enum DocumentTextType { normal, linkText }
|
||||
|
||||
typedef DocumentTextLinkData = ({String text, String? url, DocumentTextType type});
|
||||
|
||||
/// Automatically detects and parses strings with hypertext in a markdown format
|
||||
class DocumentText {
|
||||
DocumentText({required this.content});
|
||||
|
||||
final String content;
|
||||
static final _markdownLinkRegex = RegExp(r'\[(.*?)\]\((.*?)\)');
|
||||
|
||||
bool get hasLinkMarkup => _markdownLinkRegex.hasMatch(content);
|
||||
|
||||
List<DocumentTextLinkData> toTextLinkList() {
|
||||
final markdownLinkRegex = RegExp(r'\[(.*?)\]\((.*?)\)');
|
||||
if (markdownLinkRegex.hasMatch(content)) {
|
||||
final matches = markdownLinkRegex.allMatches(content).toList();
|
||||
final List<DocumentTextLinkData> stringSections = [];
|
||||
int prevStartIndex = 0;
|
||||
while (matches.isNotEmpty) {
|
||||
final match = matches.removeAt(0);
|
||||
stringSections
|
||||
.add((text: content.substring(prevStartIndex, match.start), url: null, type: DocumentTextType.normal));
|
||||
stringSections.add((text: match.group(1)!, url: match.group(2), type: DocumentTextType.linkText));
|
||||
prevStartIndex = match.end;
|
||||
}
|
||||
stringSections
|
||||
.add((text: content.substring(prevStartIndex, content.length), url: null, type: DocumentTextType.normal));
|
||||
return stringSections;
|
||||
} else {
|
||||
return [(text: content, url: null, type: DocumentTextType.normal)];
|
||||
// return Text("${bulletString != null ? '$bulletString ' : ''}$content", style: style);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hashAll([content]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return super.hashCode == other.hashCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:dartboard_resume/models/date_range.dart';
|
||||
import 'package:dartboard_resume/models/document_text.dart';
|
||||
|
||||
class Experience {
|
||||
Experience({
|
||||
required this.subsection,
|
||||
required this.title,
|
||||
required this.attributes,
|
||||
this.range,
|
||||
this.location,
|
||||
});
|
||||
final String subsection;
|
||||
final String title;
|
||||
final List<DocumentText> attributes;
|
||||
final DateRange? range;
|
||||
String? location;
|
||||
|
||||
static bool filter(dynamic e) {
|
||||
if (e is! List || !e.every((f) => f is Map)) {
|
||||
return false;
|
||||
}
|
||||
final List<Map<String, dynamic>> entries = e.map((f) => f as Map<String, dynamic>).toList();
|
||||
return entries.every((f) => f['title'] is String && f['attributes'] is List && f.keys.length >= 2);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hashAll([subsection, title, ...attributes, range]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return super.hashCode == other.hashCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart';
|
||||
|
||||
class DocumentTheme {
|
||||
DocumentTheme({
|
||||
required this.primaryHex,
|
||||
required this.accentHex,
|
||||
required this.backgroundHex,
|
||||
required this.fontPath,
|
||||
required this.bulletPoint,
|
||||
});
|
||||
|
||||
factory DocumentTheme.fromToml(Map<String, dynamic> toml) => DocumentTheme(
|
||||
primaryHex: (toml['theme'] as Map)['primary_hex'] as String,
|
||||
accentHex: (toml['theme'] as Map)['accent_hex'] as String,
|
||||
backgroundHex: (toml['theme'] as Map)['background_hex'] as String,
|
||||
fontPath: (toml['theme'] as Map)['font'] as String,
|
||||
bulletPoint: (toml['theme'] as Map)['bullet_point'] as String? ?? '-',
|
||||
);
|
||||
|
||||
factory DocumentTheme.retro() => DocumentTheme(
|
||||
primaryHex: "00AA00",
|
||||
accentHex: "44EE66",
|
||||
backgroundHex: "FFFFFF",
|
||||
fontPath: "nerd.ttf",
|
||||
bulletPoint: '-',
|
||||
);
|
||||
|
||||
static const double inch = 72.0;
|
||||
static const double cm = inch / 2.54;
|
||||
static const double mm = inch / 25.4;
|
||||
|
||||
static const width = 21.0 * cm;
|
||||
static const height = 29.7 * cm;
|
||||
static const margin = 2.0 * cm;
|
||||
|
||||
final String primaryHex;
|
||||
final String accentHex;
|
||||
final String backgroundHex;
|
||||
final String fontPath;
|
||||
final String bulletPoint;
|
||||
Font? _font;
|
||||
|
||||
PdfColor get primaryColor => PdfColor.fromHex(primaryHex);
|
||||
PdfColor get accentColor => PdfColor.fromHex(accentHex);
|
||||
PdfColor get backgroundColor => PdfColor.fromHex(backgroundHex);
|
||||
|
||||
Font get font {
|
||||
_font ??= Font.ttf(File(fontPath).readAsBytesSync().buffer.asByteData());
|
||||
return _font!;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return Object.hashAll([primaryHex, accentHex, backgroundHex, fontPath, bulletPoint]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return super.hashCode == other.hashCode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user