Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating of the chart #28

Merged
merged 6 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
runs-on: macos-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v2
- uses: subosito/[email protected]
- name: Run Flutter doctor.
run: flutter doctor -v
- name: Install Dependencies
run: flutter packages get
- name: Format
Expand Down
26 changes: 18 additions & 8 deletions lib/src/chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@ class ChartState extends State<Chart>

_addScrollNotifier();

final renderEndTime = widget.data.isNotEmpty
? widget.data.first.end.dateWithoutTime()
: DateTime.now();
processData(widget, renderEndTime);
processData(widget, _getFirstItemDate());
}

@override
void didUpdateWidget(covariant Chart oldWidget) {
super.didUpdateWidget(oldWidget);

if (oldWidget.data != widget.data ||
oldWidget.data.length != widget.data.length) {
processData(widget, _getFirstItemDate());
}
}

@override
Expand All @@ -146,6 +153,12 @@ class ChartState extends State<Chart>
super.dispose();
}

DateTime _getFirstItemDate({Duration addition = Duration.zero}) {
return widget.data.isEmpty
? DateTime.now()
: widget.data.first.end.dateWithoutTime().add(addition);
}

void _addScrollNotifier() {
WidgetsBinding.instance.addPostFrameCallback((_) {
final minDifference = _blockWidth!;
Expand Down Expand Up @@ -329,11 +342,8 @@ class ChartState extends State<Chart>
final scrollPositionDuration = Duration(
days: -blockIndex + (needsToAdaptScrollPosition ? 1 : 0),
);
final renderEndTime = widget.data.isNotEmpty
? widget.data.first.end.dateWithoutTime().add(scrollPositionDuration)
: DateTime.now();

processData(widget, renderEndTime);
processData(widget, _getFirstItemDate(addition: scrollPositionDuration));

if (topHour == beforeTopHour && bottomHour == beforeBottomHour) return;

Expand Down
5 changes: 2 additions & 3 deletions lib/src/components/utils/time_data_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mixin TimeDataProcessor {
///
/// [bottomHour]와 24시 사이에 있는 데이터들을 다음날로 넘어가 있다.
List<DateTimeRange> get processedData => _processedData;
final List<DateTimeRange> _processedData = [];
List<DateTimeRange> _processedData = [];

final List<DateTimeRange> _inRangeDataList = [];

Expand All @@ -59,8 +59,7 @@ mixin TimeDataProcessor {
return;
}

_processedData.clear();
_processedData.addAll(List.from(chart.data));
_processedData = [...chart.data];

_firstDataHasChanged = false;
_countDays(chart.data);
Expand Down
92 changes: 92 additions & 0 deletions test/chart_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:time_chart/src/chart.dart';
import 'package:time_chart/time_chart.dart';

import 'data_pool.dart';

void main() {
testWidgets('Chart updates when the data is replaced', (tester) async {
List<DateTimeRange> data = data1;

await tester.pumpWidget(
MaterialApp(
home: StatefulBuilder(
builder: (context, setState) {
return Column(
children: [
TimeChart(data: data),
TextButton(
onPressed: () {
setState(() {
data = data2;
});
},
child: const Text('Update'),
),
],
);
},
),
),
);

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/data1_chart.png'),
skip: !Platform.isMacOS,
);

await tester.tap(find.text('Update'));
await tester.pump(const Duration(milliseconds: 300));

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/data2_chart.png'),
skip: !Platform.isMacOS,
);
});

testWidgets('Chart updates when the data length is changed', (tester) async {
List<DateTimeRange> data = data1;

await tester.pumpWidget(
MaterialApp(
home: StatefulBuilder(
builder: (context, setState) {
return Column(
children: [
TimeChart(data: data),
TextButton(
onPressed: () {
setState(() {
data = data3;
});
},
child: const Text('Update'),
),
],
);
},
),
),
);

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/data1_chart.png'),
skip: !Platform.isMacOS,
);

await tester.tap(find.text('Update'));
await tester.pump(const Duration(milliseconds: 300));

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/data3_chart.png'),
skip: !Platform.isMacOS,
);
});
}
38 changes: 38 additions & 0 deletions test/data_pool.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';

final List<DateTimeRange> data1 = [
DateTimeRange(
start: DateTime(2021, 10, 22, 23, 55),
end: DateTime(2021, 10, 23, 8, 0),
),
DateTimeRange(
start: DateTime(2021, 10, 21, 21, 15),
end: DateTime(2021, 10, 22, 6, 10),
),
];

final List<DateTimeRange> data2 = [
DateTimeRange(
start: DateTime(2021, 2, 2, 18, 4),
end: DateTime(2021, 2, 3, 8, 0),
),
DateTimeRange(
start: DateTime(2021, 2, 1, 15, 3),
end: DateTime(2021, 2, 2, 5, 4),
),
];

final List<DateTimeRange> data3 = [
DateTimeRange(
start: DateTime(2021, 2, 3, 20, 42),
end: DateTime(2021, 2, 4, 12, 10),
),
DateTimeRange(
start: DateTime(2021, 2, 2, 18, 4),
end: DateTime(2021, 2, 3, 8, 0),
),
DateTimeRange(
start: DateTime(2021, 2, 1, 15, 3),
end: DateTime(2021, 2, 2, 5, 4),
),
];
Binary file added test/golden/data1_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/golden/data2_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/golden/data3_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.