Skip to content

Commit

Permalink
Change logic for sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
bosskmk committed Dec 19, 2020
1 parent ba66ca3 commit edcf900
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 66 deletions.
28 changes: 22 additions & 6 deletions lib/src/manager/pluto_state_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class PlutoStateManager extends PlutoState {
CreateFooterCallBack createFooter,
PlutoConfiguration configuration,
}) {
initializeRows(columns, rows);
refColumns = columns;
refRows = FilteredList(initialList: rows);
setGridFocusNode(gridFocusNode);
Expand Down Expand Up @@ -114,8 +115,9 @@ class PlutoStateManager extends PlutoState {
}

_ApplyList applyList = _ApplyList([
_ApplyCellFormat(refColumns),
_ApplyRowSortIdx(
_ApplyCellForSetColumn(refColumns),
_ApplyCellForFormat(refColumns),
_ApplyRowForSortIdx(
forceApply: forceApplySortIdx,
increase: increase,
start: start,
Expand Down Expand Up @@ -281,10 +283,24 @@ class _ApplyList implements _Apply {
}
}

class _ApplyCellFormat implements _Apply {
class _ApplyCellForSetColumn implements _Apply {
final List<PlutoColumn> refColumns;

_ApplyCellFormat(
_ApplyCellForSetColumn(this.refColumns);

bool get apply => true;

void execute(PlutoRow row) {
refColumns.forEach((element) {
row.cells[element.field].setColumn(element);
});
}
}

class _ApplyCellForFormat implements _Apply {
final List<PlutoColumn> refColumns;

_ApplyCellForFormat(
this.refColumns,
) {
assert(refColumns != null && refColumns.isNotEmpty);
Expand Down Expand Up @@ -313,7 +329,7 @@ class _ApplyCellFormat implements _Apply {
}
}

class _ApplyRowSortIdx implements _Apply {
class _ApplyRowForSortIdx implements _Apply {
final bool forceApply;

final bool increase;
Expand All @@ -322,7 +338,7 @@ class _ApplyRowSortIdx implements _Apply {

final PlutoRow firstRow;

_ApplyRowSortIdx({
_ApplyRowForSortIdx({
@required this.forceApply,
@required this.increase,
@required this.start,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/manager/state/column_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,17 @@ mixin ColumnState implements IPlutoState {
void sortAscending(PlutoColumn column) {
refRows.sort(
(a, b) => column.type.compare(
a.cells[column.field].value,
b.cells[column.field].value,
a.cells[column.field].valueForSorting,
b.cells[column.field].valueForSorting,
),
);
}

void sortDescending(PlutoColumn column) {
refRows.sort(
(b, a) => column.type.compare(
a.cells[column.field].value,
b.cells[column.field].value,
a.cells[column.field].valueForSorting,
b.cells[column.field].valueForSorting,
),
);
}
Expand Down
78 changes: 73 additions & 5 deletions lib/src/model/pluto_cell.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,82 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart' as intl;

class PlutoCell {
/// Value of cell
dynamic value;
import 'pluto_column.dart';
import 'pluto_column_type.dart';

class PlutoCell {
PlutoCell({
this.value,
}) : _key = UniqueKey();
dynamic value,
}) : _key = UniqueKey(),
_value = value;

/// cell key
final Key _key;

Key get key => _key;

/// cell value
dynamic _value;

dynamic get value => _value;

set value(dynamic changed) {
if (_value == changed) {
return;
}

_value = changed;

_valueForSorting = null;
}

dynamic _valueForSorting;

dynamic get valueForSorting {
_valueForSorting ??= _getValueForSorting();

return _valueForSorting;
}

dynamic _getValueForSorting() {
if (_valueForSorting != null) {
return _valueForSorting;
}

assert(_column != null);

if (_column.type.isText) {
_valueForSorting = _value.toString();
} else if (_column.type.isNumber) {
_valueForSorting = _value.runtimeType != num
? num.tryParse(_value.toString()) ?? 0
: _value;
} else if (_column.type.isDate) {
PlutoColumnTypeDate dateColumn = _column.type;

final dateFormat = intl.DateFormat(dateColumn.format);

DateTime dateFormatValue;

try {
dateFormatValue = dateFormat.parse(_value);
} catch (e) {
dateFormatValue = _column.type.defaultValue;
}

_valueForSorting = dateFormatValue;
} else {
_valueForSorting = _value;
}

return _valueForSorting;
}

/// cell column
PlutoColumn _column;

void setColumn(PlutoColumn column) {
_column = column;
_valueForSorting = _getValueForSorting();
}
}
40 changes: 2 additions & 38 deletions lib/src/model/pluto_column_type.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:intl/intl.dart' as intl;

import '../helper/pluto_log.dart';

abstract class PlutoColumnType {
bool readOnly;

Expand Down Expand Up @@ -154,7 +152,7 @@ class PlutoColumnTypeText implements PlutoColumnType {
}

int compare(dynamic a, dynamic b) {
return a.toString().compareTo(b.toString());
return a.compareTo(b);
}
}

Expand Down Expand Up @@ -191,14 +189,6 @@ class PlutoColumnTypeNumber
}

int compare(dynamic a, dynamic b) {
if (a.runtimeType != num) {
a = num.tryParse(a.toString()) ?? 0;
}

if (b.runtimeType != num) {
b = num.tryParse(b.toString()) ?? 0;
}

return a.compareTo(b);
}

Expand Down Expand Up @@ -296,33 +286,7 @@ class PlutoColumnTypeDate
}

int compare(dynamic a, dynamic b) {
DateTime _a;

DateTime _b;

try {
final dateFormat = intl.DateFormat(format);

_a = dateFormat.parse(a);

_b = dateFormat.parse(b);

return _a.compareTo(_b);
} on FormatException {
return _a == _b
? 0
: _a == null
? -1
: 1;
} on Exception catch (e) {
PlutoLog(
'Exception on compare function of PlutoColumnTypeDate.',
type: PlutoLogType.exception,
error: e,
);
}

return 0;
return a.compareTo(b);
}

String applyFormat(value) {
Expand Down
6 changes: 0 additions & 6 deletions lib/src/pluto_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ class _PlutoGridState extends State<PlutoGrid> {
}

void initProperties() {
initializeColumnRow();

gridFocusNode = FocusNode(onKey: handleGridFocusOnKey);

// Dispose
Expand Down Expand Up @@ -199,10 +197,6 @@ class _PlutoGridState extends State<PlutoGrid> {
});
}

void initializeColumnRow() {
PlutoStateManager.initializeRows(widget.columns, widget.rows);
}

void changeStateListener() {
if (_showFrozenColumn != stateManager.showFrozenColumn ||
_hasLeftFrozenColumns != stateManager.hasLeftFrozenColumns ||
Expand Down
3 changes: 2 additions & 1 deletion lib/src/ui/cells/mixin_popup_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ mixin MixinPopupCell<T extends AbstractMixinPopupCell> on State<T>

try {
_textController.text = widget.column.formattedValueForDisplayInEditing(
widget.stateManager.currentCell.value);
widget.stateManager.currentCell.value,
);
} catch (e) {
/**
* When the Popup is opened, the TextField is closed
Expand Down
5 changes: 3 additions & 2 deletions lib/src/ui/cells/mixin_text_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ mixin MixinTextCell<T extends AbstractMixinTextCell> on State<T> {
void initState() {
super.initState();

_textController.text =
widget.column.formattedValueForDisplayInEditing(widget.cell.value);
_textController.text = widget.column.formattedValueForDisplayInEditing(
widget.cell.value,
);

_cellEditingStatus = CellEditingStatus.init;
}
Expand Down
14 changes: 10 additions & 4 deletions test/src/model/pluto_column_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,15 @@ void main() {
},
);

// 날짜 포멧이 적용 된 경우 컬럼의 compare 함수의 동작이 의도와 다르다.
// 포멧을 바꿔 compare 함수를 호출해야 한다. compare 함수는 포멧을 적당하게
// 변환하여 호출하고 포멧의 변경은 외부에서 호출 할 때 처리 한다.
test(
'12/30/2019, 01/01/2020 인 경우 -1',
'12/30/2019, 01/01/2020 인 경우 1',
() {
final PlutoColumnTypeDate column =
PlutoColumnType.date(format: 'MM/dd/yyyy');
expect(column.compare('12/30/2019', '01/01/2020'), -1);
expect(column.compare('12/30/2019', '01/01/2020'), 1);
},
);

Expand All @@ -572,12 +575,15 @@ void main() {
},
);

// 날짜 포멧이 적용 된 경우 컬럼의 compare 함수의 동작이 의도와 다르다.
// 포멧을 바꿔 compare 함수를 호출해야 한다. compare 함수는 포멧을 적당하게
// 변환하여 호출하고 포멧의 변경은 외부에서 호출 할 때 처리 한다.
test(
'01/01/2020, 12/30/2019 인 경우 1',
'01/01/2020, 12/30/2019 인 경우 -1',
() {
final PlutoColumnTypeDate column =
PlutoColumnType.date(format: 'MM/dd/yyyy');
expect(column.compare('01/01/2020', '12/30/2019'), 1);
expect(column.compare('01/01/2020', '12/30/2019'), -1);
},
);

Expand Down

0 comments on commit edcf900

Please sign in to comment.