Skip to content

Commit beaaa51

Browse files
committed
Teams #30
1 parent 7f81b64 commit beaaa51

6 files changed

+254
-8
lines changed

lib/codelessly_sdk.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ export 'src/data/web_data_repository.dart';
1717
export 'src/functions/functions.dart';
1818
export 'src/logging/codelessly_logger.dart';
1919
export 'src/model/asset_model.dart';
20-
export 'src/model/custom_component.dart';
2120
export 'src/model/auth_data.dart';
21+
export 'src/model/custom_component.dart';
22+
export 'src/model/font_family.dart';
2223
export 'src/model/font_file_data.dart';
2324
export 'src/model/model_http_request.dart';
2425
export 'src/model/privacy_base.dart';

lib/src/model/font_family.dart

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright (c) 2022, Codelessly.
2+
// All rights reserved. Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
import 'package:codelessly_api/codelessly_api.dart';
6+
import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
7+
import 'package:equatable/equatable.dart';
8+
9+
import '../../codelessly_sdk.dart';
10+
import 'privacy_base.dart';
11+
12+
part 'font_family.g.dart';
13+
14+
/// Holds information about font family.
15+
@JsonSerializable()
16+
class FontFamilyModel extends PrivacyBase {
17+
/// Unique ID of the font family.
18+
final String id;
19+
20+
/// Name of the font family.
21+
final String name;
22+
23+
/// Different types of font variants this family contains.
24+
final List<FontVariantModel> fontVariants;
25+
26+
/// Creates an instance of [FontFamilyModel] with given data.
27+
const FontFamilyModel({
28+
String? id,
29+
required this.name,
30+
required this.fontVariants,
31+
32+
// Privacy
33+
required super.teams,
34+
required super.users,
35+
required super.roles,
36+
required super.public,
37+
}) : id = id ?? name;
38+
39+
FontFamilyModel.private({
40+
String? id,
41+
required this.name,
42+
required this.fontVariants,
43+
44+
// Privacy
45+
required super.privacy,
46+
}) : id = id ?? name,
47+
super.private();
48+
49+
/// Factory constructor for creating a new [FontFamilyModel] instance from
50+
/// JSON data.
51+
factory FontFamilyModel.fromJson(Map<String, dynamic> json) =>
52+
_$FontFamilyModelFromJson(json);
53+
54+
/// Serializes this object into JSON data.
55+
@override
56+
Map<String, dynamic> toJson() => _$FontFamilyModelToJson(this);
57+
58+
@override
59+
List<Object?> get props => [id, name, fontVariants];
60+
}
61+
62+
/// Holds information about a font variant in a font family.
63+
@JsonSerializable()
64+
class FontVariantModel extends Equatable {
65+
/// Name of the variant. Ex. regular, thin, bold, etc.
66+
final String name;
67+
68+
/// Font weight for this variant.
69+
final FontWeightNumeric weight;
70+
71+
/// Font style for this variant.
72+
final String style;
73+
74+
/// Source URL of this font from where it can be downloaded/accessed.
75+
final String fontURL;
76+
77+
/// A URL for the preview image for this font.
78+
final String previewURL;
79+
80+
/// Creates an instance of [FontVariantModel] with given data.
81+
const FontVariantModel({
82+
this.name = 'regular',
83+
this.weight = FontWeightNumeric.w400,
84+
this.style = 'Normal',
85+
this.fontURL = '',
86+
this.previewURL = '',
87+
});
88+
89+
/// Factory constructor for creating a new [FontVariantModel] instance from
90+
/// JSON data.
91+
factory FontVariantModel.fromJson(Map<String, dynamic> json) =>
92+
_$FontVariantModelFromJson(json);
93+
94+
/// Serializes this object into JSON data.
95+
Map<String, dynamic> toJson() => _$FontVariantModelToJson(this);
96+
97+
@override
98+
List<Object?> get props => [weight, style];
99+
100+
/// A factory constructor that creates an instance of [FontVariantModel] by
101+
/// parsing given [name].
102+
factory FontVariantModel.fromName(String name,
103+
{String fontURL = '', String previewURL = ''}) {
104+
FontWeightNumeric fileFontWeight = FontWeightNumeric.w400;
105+
String fileFontStyle = 'Normal';
106+
107+
// Convert 'extra-bold' or 'extra bold' into 'extrabold'.
108+
final String modifiedName =
109+
name.toLowerCase().replaceAll('-', '').replaceAll(' ', '');
110+
111+
// Check font weight.
112+
if (modifiedName.contains('thin')) {
113+
fileFontWeight = FontWeightNumeric.w100;
114+
} else if (modifiedName.contains('extralight')) {
115+
fileFontWeight = FontWeightNumeric.w200;
116+
} else if (modifiedName.contains('light')) {
117+
fileFontWeight = FontWeightNumeric.w300;
118+
} else if (modifiedName.contains('normal')) {
119+
fileFontWeight = FontWeightNumeric.w400;
120+
} else if (modifiedName.contains('regular')) {
121+
fileFontWeight = FontWeightNumeric.w400;
122+
} else if (modifiedName.contains('medium')) {
123+
fileFontWeight = FontWeightNumeric.w500;
124+
} else if (modifiedName.contains('semibold')) {
125+
fileFontWeight = FontWeightNumeric.w600;
126+
} else if (modifiedName.contains('extrabold')) {
127+
// Order matters, extrabold must come before bold.
128+
fileFontWeight = FontWeightNumeric.w800;
129+
} else if (modifiedName.contains('ultrabold')) {
130+
// Order matters, ultrabold must come before bold.
131+
fileFontWeight = FontWeightNumeric.w900;
132+
} else if (modifiedName.contains('bold')) {
133+
fileFontWeight = FontWeightNumeric.w700;
134+
} else if (modifiedName.contains('black')) {
135+
fileFontWeight = FontWeightNumeric.w900;
136+
} else if (modifiedName.contains('heavy')) {
137+
fileFontWeight = FontWeightNumeric.w900;
138+
} else if (modifiedName.contains('solid')) {
139+
// Exception for FontAwesome font.
140+
// TODO: Might need to remove this and use 'bold' for 'solid' style.
141+
fileFontWeight = FontWeightNumeric.w700;
142+
}
143+
144+
// Check font style.
145+
if (modifiedName.contains('italic')) {
146+
fileFontStyle = 'Italic';
147+
}
148+
149+
if (modifiedName.contains('oblique')) {
150+
fileFontStyle = 'Oblique';
151+
}
152+
153+
return FontVariantModel(
154+
name: name,
155+
weight: fileFontWeight,
156+
style: fileFontStyle,
157+
fontURL: fontURL,
158+
previewURL: previewURL,
159+
);
160+
}
161+
}

lib/src/model/font_family.g.dart

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/model/model_http_request.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ class HttpApiData extends PrivacyBase {
7171
bool isDraft;
7272
@DateTimeConverter()
7373
final DateTime created;
74-
final String? directory;
74+
final String directory;
7575

7676
HttpApiData({
77+
required this.directory,
7778
this.method = HttpMethod.get,
7879
this.url = '',
7980
this.headers = const <HttpKeyValuePair>[],
@@ -90,7 +91,6 @@ class HttpApiData extends PrivacyBase {
9091
this.requestBodyContentType = RequestBodyTextType.json,
9192
this.isDraft = false,
9293
DateTime? created,
93-
this.directory,
9494

9595
// Privacy
9696
required super.teams,
@@ -102,6 +102,7 @@ class HttpApiData extends PrivacyBase {
102102
created = created ?? DateTime.now();
103103

104104
HttpApiData.private({
105+
required this.directory,
105106
this.method = HttpMethod.get,
106107
this.url = '',
107108
this.headers = const <HttpKeyValuePair>[],
@@ -118,7 +119,6 @@ class HttpApiData extends PrivacyBase {
118119
this.requestBodyContentType = RequestBodyTextType.json,
119120
this.isDraft = false,
120121
DateTime? created,
121-
this.directory,
122122

123123
// Privacy
124124
required super.privacy,
@@ -166,7 +166,7 @@ class HttpApiData extends PrivacyBase {
166166
created: created ?? this.created,
167167
bodyType: bodyType ?? this.bodyType,
168168
isDraft: isDraft ?? this.isDraft,
169-
directory: forceDirectory ? directory : directory ?? this.directory,
169+
directory: directory ?? this.directory,
170170
requestBodyContentType:
171171
requestBodyContentType ?? this.requestBodyContentType,
172172
privacy: privacy ?? this,
@@ -179,7 +179,7 @@ class HttpApiData extends PrivacyBase {
179179
Map<String, dynamic> toJson() => _$HttpApiDataToJson(this);
180180

181181
@override
182-
List<Object?> get props => [id];
182+
List<Object?> get props => [...super.props, id];
183183
}
184184

185185
@JsonSerializable()

lib/src/model/model_http_request.g.dart

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/utils/font_parser.dart

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'dart:typed_data';
44

55
import 'package:codelessly_api/codelessly_api.dart';
66

7+
import '../model/font_family.dart';
8+
79
void main(List<String> args) {
810
final file = File(args[0]);
911
final result = FontParser.parse(file.readAsBytesSync());

0 commit comments

Comments
 (0)