Skip to content

Commit

Permalink
Refactor avoid late keyword as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
sunboyy committed Jun 20, 2024
1 parent 6168585 commit 81f0be9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 98 deletions.
71 changes: 26 additions & 45 deletions lib/src/excel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ Excel _newExcel(Archive archive) {

/// Decode a excel file.
class Excel {
late bool _styleChanges;
late bool _mergeChanges;
late bool _rtlChanges;

late Archive _archive;

late Map<String, XmlNode> _sheets;
late Map<String, XmlDocument> _xmlFiles;
late Map<String, String> _xmlSheetId;
late Map<String, Map<String, int>> _cellStyleReferenced;
late Map<String, Sheet> _sheetMap;

late List<CellStyle> _cellStyleList;
late List<String> _patternFill;
late List<String> _mergeChangeLook;
late List<String> _rtlChangeLook;
late List<_FontStyle> _fontStyleList;
late List<int> _numFmtIds;
late NumFormatMaintainer _numFormats = NumFormatMaintainer();
late List<_BorderSet> _borderSetList;

late _SharedStringsMaintainer _sharedStrings;

late String _stylesTarget;
late String _sharedStringsTarget;
bool _styleChanges = false;
bool _mergeChanges = false;
bool _rtlChanges = false;

Archive _archive;

final Map<String, XmlNode> _sheets = {};
final Map<String, XmlDocument> _xmlFiles = {};
final Map<String, String> _xmlSheetId = {};
final Map<String, Map<String, int>> _cellStyleReferenced = {};
final Map<String, Sheet> _sheetMap = {};

List<CellStyle> _cellStyleList = [];
List<String> _patternFill = [];
final List<String> _mergeChangeLook = [];
final List<String> _rtlChangeLook = [];
List<_FontStyle> _fontStyleList = [];
final List<int> _numFmtIds = [];
final NumFormatMaintainer _numFormats = NumFormatMaintainer();
List<_BorderSet> _borderSetList = [];

_SharedStringsMaintainer _sharedStrings = _SharedStringsMaintainer._();

String _stylesTarget = '';
String _sharedStringsTarget = '';
String get _absSharedStringsTarget {
if (_sharedStringsTarget.isNotEmpty && _sharedStringsTarget[0] == "/") {
return _sharedStringsTarget.substring(1);
Expand All @@ -58,26 +58,7 @@ class Excel {
String? _defaultSheet;
late Parser parser;

Excel._(Archive archive) {
_styleChanges = false;
_mergeChanges = false;
_rtlChanges = false;
_sheets = <String, XmlNode>{};
_xmlFiles = <String, XmlDocument>{};
_xmlSheetId = <String, String>{};
_cellStyleReferenced = <String, Map<String, int>>{};
_sheetMap = <String, Sheet>{};
_cellStyleList = <CellStyle>[];
_patternFill = <String>[];
_mergeChangeLook = <String>[];
_rtlChangeLook = <String>[];
_fontStyleList = <_FontStyle>[];
_numFmtIds = <int>[];
_stylesTarget = '';
_sharedStringsTarget = '';

_archive = archive;
_sharedStrings = _SharedStringsMaintainer._();
Excel._(this._archive) {
parser = Parser._(this);
parser._startParsing();
}
Expand Down
13 changes: 5 additions & 8 deletions lib/src/parser/parse.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
part of excel;

class Parser {
late Excel _excel;
late List<String> _rId;
late Map<String, String> _worksheetTargets;
Parser._(Excel excel) {
this._excel = excel;
this._rId = <String>[];
this._worksheetTargets = <String, String>{};
}
final Excel _excel;
final List<String> _rId = [];
final Map<String, String> _worksheetTargets = {};

Parser._(this._excel);

void _startParsing() {
_putContentXml();
Expand Down
12 changes: 5 additions & 7 deletions lib/src/save/save_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ part of excel;

class Save {
final Excel _excel;
late Map<String, ArchiveFile> _archiveFiles;
late List<CellStyle> _innerCellStyle;
final Map<String, ArchiveFile> _archiveFiles = {};
final List<CellStyle> _innerCellStyle = [];
final Parser parser;
Save._(this._excel, this.parser) {
_archiveFiles = <String, ArchiveFile>{};
_innerCellStyle = <CellStyle>[];
}

Save._(this._excel, this.parser);

void _addNewColumn(XmlElement columns, int min, int max, double width) {
columns.children.add(XmlElement(XmlName('col'), [
Expand Down Expand Up @@ -172,7 +170,7 @@ class Save {
/// Writing Font Color in [xl/styles.xml] from the Cells of the sheets.
void _processStylesFile() {
_innerCellStyle = <CellStyle>[];
_innerCellStyle.clear();
List<String> innerPatternFill = <String>[];
List<_FontStyle> innerFontStyle = <_FontStyle>[];
List<_BorderSet> innerBorderSet = <_BorderSet>[];
Expand Down
7 changes: 3 additions & 4 deletions lib/src/sharedStrings/shared_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ class _SharedStringsMaintainer {

class _IndexingHolder {
final int index;
late int count;
_IndexingHolder(this.index, [int _count = 1]) {
this.count = _count;
}
int count;

_IndexingHolder(this.index, [int _count = 1]) : count = _count;

void increaseCount() {
this.count += 1;
Expand Down
29 changes: 14 additions & 15 deletions lib/src/sheet/border_style.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
part of excel;

class Border extends Equatable {
late final BorderStyle? borderStyle;
late final String? borderColorHex;
final BorderStyle? borderStyle;
final String? borderColorHex;

Border({BorderStyle? borderStyle, ExcelColor? borderColorHex}) {
this.borderStyle = borderStyle == BorderStyle.None ? null : borderStyle;
this.borderColorHex = borderColorHex != null
? _isColorAppropriate(borderColorHex.colorHex)
: null;
}
Border({BorderStyle? borderStyle, ExcelColor? borderColorHex})
: borderStyle = borderStyle == BorderStyle.None ? null : borderStyle,
borderColorHex = borderColorHex != null
? _isColorAppropriate(borderColorHex.colorHex)
: null;

@override
String toString() {
Expand All @@ -24,13 +23,13 @@ class Border extends Equatable {
}

class _BorderSet extends Equatable {
late final Border leftBorder;
late final Border rightBorder;
late final Border topBorder;
late final Border bottomBorder;
late final Border diagonalBorder;
late final bool diagonalBorderUp;
late final bool diagonalBorderDown;
final Border leftBorder;
final Border rightBorder;
final Border topBorder;
final Border bottomBorder;
final Border diagonalBorder;
final bool diagonalBorderUp;
final bool diagonalBorderDown;

_BorderSet({
required this.leftBorder,
Expand Down
28 changes: 10 additions & 18 deletions lib/src/sheet/sheet.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
part of excel;

class Sheet {
late Excel _excel;
late String _sheet;
late bool _isRTL;
late int _maxRows;
late int _maxColumns;
final Excel _excel;
final String _sheet;
bool _isRTL = false;
int _maxRows = 0;
int _maxColumns = 0;
double? _defaultColumnWidth;
double? _defaultRowHeight;
Map<int, double> _columnWidths = {};
Map<int, double> _rowHeights = {};
Map<int, bool> _columnAutoFit = {};
late FastList<String> _spannedItems;
late List<_Span?> _spanList;
late Map<int, Map<int, Data>> _sheetData;
late HeaderFooter? _headerFooter;
FastList<String> _spannedItems = FastList<String>();
List<_Span?> _spanList = [];
Map<int, Map<int, Data>> _sheetData = {};
HeaderFooter? _headerFooter;

///
/// It will clone the object by changing the `this` reference of previous oldSheetObject and putting `new this` reference, with copying the values too
Expand All @@ -32,7 +32,7 @@ class Sheet {
isRTLVal: oldSheetObject._isRTL,
headerFooter: oldSheetObject._headerFooter);

Sheet._(Excel excel, String sheetName,
Sheet._(this._excel, this._sheet,
{Map<int, Map<int, Data>>? sh,
List<_Span?>? spanL_,
FastList<String>? spanI_,
Expand All @@ -43,14 +43,6 @@ class Sheet {
Map<int, double>? rowHeightsVal,
Map<int, bool>? columnAutoFitVal,
HeaderFooter? headerFooter}) {
_excel = excel;
_sheet = sheetName;
_sheetData = <int, Map<int, Data>>{};
_spanList = <_Span?>[];
_spannedItems = FastList<String>();
_isRTL = false;
_maxRows = 0;
_maxColumns = 0;
_headerFooter = headerFooter;

if (spanL_ != null) {
Expand Down
2 changes: 1 addition & 1 deletion test/excel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void main() {
var file = './test/test_resources/example.xlsx';
var bytes = File(file).readAsBytesSync();
var excel = Excel.decodeBytes(bytes);
Sheet? sheetObject = excel.tables['Sheet1']!;
Sheet sheetObject = excel.tables['Sheet1']!;

sheetObject.headerFooter!.oddHeader = "Foo";
sheetObject.headerFooter!.oddFooter = "Bar";
Expand Down

0 comments on commit 81f0be9

Please sign in to comment.