Commit f45a4bdc9f626208abe76901973e9793af0edc2e

Authored by 刘宏哲
1 parent 4602f5da

feat(app): bug fixed

@@ -10,10 +10,9 @@ DateTime _today() { @@ -10,10 +10,9 @@ DateTime _today() {
10 } 10 }
11 11
12 class _ReportPeriodAnchor { 12 class _ReportPeriodAnchor {
13 - const _ReportPeriodAnchor({required this.date, this.sourceMonth}); 13 + const _ReportPeriodAnchor({required this.date});
14 14
15 final DateTime date; 15 final DateTime date;
16 - final DateTime? sourceMonth;  
17 } 16 }
18 17
19 abstract class ReportPeriodLogic { 18 abstract class ReportPeriodLogic {
@@ -140,13 +139,9 @@ abstract class ReportPeriodLogic { @@ -140,13 +139,9 @@ abstract class ReportPeriodLogic {
140 return Future.value(); 139 return Future.value();
141 } 140 }
142 final nextDate = _dateForPeriodTransition(nextPeriod); 141 final nextDate = _dateForPeriodTransition(nextPeriod);
143 - final sourceMonth = selectedPeriod.value == ReportPeriod.month &&  
144 - nextPeriod == ReportPeriod.week  
145 - ? monthStart  
146 - : null;  
147 selectedPeriod.value = nextPeriod; 142 selectedPeriod.value = nextPeriod;
148 selectedDate.value = nextDate; 143 selectedDate.value = nextDate;
149 - _setAnchorFor(nextPeriod, nextDate, sourceMonth: sourceMonth); 144 + _setAnchorFor(nextPeriod, nextDate);
150 return loadReport(); 145 return loadReport();
151 } 146 }
152 147
@@ -253,23 +248,12 @@ abstract class ReportPeriodLogic { @@ -253,23 +248,12 @@ abstract class ReportPeriodLogic {
253 248
254 void dispose() {} 249 void dispose() {}
255 250
256 - void _setAnchorFor(  
257 - ReportPeriod period,  
258 - DateTime date, {  
259 - DateTime? sourceMonth,  
260 - }) { 251 + void _setAnchorFor(ReportPeriod period, DateTime date) {
261 final normalizedPeriod = normalizePeriod(period); 252 final normalizedPeriod = normalizePeriod(period);
262 final normalizedDate = normalizeDate(normalizedPeriod, date); 253 final normalizedDate = normalizeDate(normalizedPeriod, date);
263 - final weekSourceMonth =  
264 - DateTime(normalizedDate.year, normalizedDate.month);  
265 _anchor = switch (normalizedPeriod) { 254 _anchor = switch (normalizedPeriod) {
266 ReportPeriod.day => _ReportPeriodAnchor(date: normalizedDate), 255 ReportPeriod.day => _ReportPeriodAnchor(date: normalizedDate),
267 - ReportPeriod.week => _ReportPeriodAnchor(  
268 - date: ReportDateRangeConfig.clampDate(  
269 - normalizedDate.add(const Duration(days: 6)),  
270 - ),  
271 - sourceMonth: sourceMonth ?? weekSourceMonth,  
272 - ), 256 + ReportPeriod.week => _ReportPeriodAnchor(date: normalizedDate),
273 ReportPeriod.month => _ReportPeriodAnchor( 257 ReportPeriod.month => _ReportPeriodAnchor(
274 date: ReportDateRangeConfig.clampDate( 258 date: ReportDateRangeConfig.clampDate(
275 _lastDayOfMonth(normalizedDate), 259 _lastDayOfMonth(normalizedDate),
@@ -284,21 +268,6 @@ abstract class ReportPeriodLogic { @@ -284,21 +268,6 @@ abstract class ReportPeriodLogic {
284 } 268 }
285 269
286 DateTime _dateForPeriodTransition(ReportPeriod nextPeriod) { 270 DateTime _dateForPeriodTransition(ReportPeriod nextPeriod) {
287 - final currentPeriod = selectedPeriod.value;  
288 -  
289 - if (currentPeriod == ReportPeriod.week &&  
290 - nextPeriod == ReportPeriod.month) {  
291 - // Preserve the source month when entering from a month. A directly  
292 - // selected week uses the month containing its Monday as the source.  
293 - return normalizeDate(nextPeriod, _anchor.sourceMonth ?? weekStart);  
294 - }  
295 -  
296 - if (currentPeriod == ReportPeriod.week &&  
297 - nextPeriod == ReportPeriod.year) {  
298 - // A cross-year week belongs to the year containing its Monday.  
299 - return normalizeDate(nextPeriod, weekStart);  
300 - }  
301 -  
302 return normalizeDate(nextPeriod, _anchor.date); 271 return normalizeDate(nextPeriod, _anchor.date);
303 } 272 }
304 273
1 -import 'package:doublefeel_flutter/app/modules/report_common/config/report_date_range_config.dart';  
2 import 'package:doublefeel_flutter/app/modules/report_common/controllers/report_period_logic.dart'; 1 import 'package:doublefeel_flutter/app/modules/report_common/controllers/report_period_logic.dart';
3 import 'package:doublefeel_flutter/app/modules/report_common/models/report_period.dart'; 2 import 'package:doublefeel_flutter/app/modules/report_common/models/report_period.dart';
4 import 'package:flutter_test/flutter_test.dart'; 3 import 'package:flutter_test/flutter_test.dart';
@@ -10,26 +9,6 @@ class _TestReportPeriodLogic extends ReportPeriodLogic { @@ -10,26 +9,6 @@ class _TestReportPeriodLogic extends ReportPeriodLogic {
10 Future<void> loadReport() async {} 9 Future<void> loadReport() async {}
11 } 10 }
12 11
13 -class _MonthBoundaryReportPeriodLogic extends _TestReportPeriodLogic {  
14 - _MonthBoundaryReportPeriodLogic(this.sourceMonth);  
15 -  
16 - final DateTime sourceMonth;  
17 -  
18 - @override  
19 - DateTime normalizeDate(ReportPeriod period, DateTime date) {  
20 - if (period == ReportPeriod.week &&  
21 - date.year == sourceMonth.year &&  
22 - date.month == sourceMonth.month) {  
23 - // Simulates a month whose latest selectable week starts in the  
24 - // preceding month.  
25 - return sourceMonth.subtract(  
26 - Duration(days: sourceMonth.weekday - DateTime.monday),  
27 - );  
28 - }  
29 - return super.normalizeDate(period, date);  
30 - }  
31 -}  
32 -  
33 void main() { 12 void main() {
34 group('ReportPeriodLogic period transitions', () { 13 group('ReportPeriodLogic period transitions', () {
35 test('year to month selects December for a completed year', () async { 14 test('year to month selects December for a completed year', () async {
@@ -113,7 +92,7 @@ void main() { @@ -113,7 +92,7 @@ void main() {
113 ); 92 );
114 }); 93 });
115 94
116 - test('month to week to month keeps the source month', 95 + test('month to week to month uses the week start month',
117 () async { 96 () async {
118 final logic = _TestReportPeriodLogic(); 97 final logic = _TestReportPeriodLogic();
119 final year = DateTime.now().year - 1; 98 final year = DateTime.now().year - 1;
@@ -126,52 +105,19 @@ void main() { @@ -126,52 +105,19 @@ void main() {
126 expect(logic.monthStart, DateTime(year, DateTime.december)); 105 expect(logic.monthStart, DateTime(year, DateTime.december));
127 }); 106 });
128 107
129 - test('clamped cross-month week returns to the original source month',  
130 - () async {  
131 - final year = DateTime.now().year - 1;  
132 - final sourceMonth = List<DateTime>.generate(  
133 - 12,  
134 - (index) => DateTime(year, index + 1),  
135 - ).firstWhere((month) => month.weekday != DateTime.monday);  
136 - final logic = _MonthBoundaryReportPeriodLogic(sourceMonth);  
137 - final clampedWeekStart = sourceMonth.subtract(  
138 - Duration(days: sourceMonth.weekday - DateTime.monday),  
139 - );  
140 - logic.initializeQuery(ReportPeriod.month, sourceMonth);  
141 -  
142 - await logic.selectPeriod(ReportPeriod.week);  
143 - expect(logic.weekStart, clampedWeekStart);  
144 -  
145 - await logic.selectPeriod(ReportPeriod.month);  
146 -  
147 - expect(logic.monthStart, sourceMonth);  
148 - });  
149 -  
150 - test('week to day selects Sunday when the whole week is selectable', 108 + test('week to day selects Monday',
151 () async { 109 () async {
152 final logic = _TestReportPeriodLogic(); 110 final logic = _TestReportPeriodLogic();
153 logic.initializeQuery( 111 logic.initializeQuery(
154 ReportPeriod.week, 112 ReportPeriod.week,
155 DateTime(DateTime.now().year - 1, 10, 20), 113 DateTime(DateTime.now().year - 1, 10, 20),
156 ); 114 );
157 - final expectedDay = logic.weekEnd; 115 + final expectedDay = logic.weekStart;
158 116
159 await logic.selectPeriod(ReportPeriod.day); 117 await logic.selectPeriod(ReportPeriod.day);
160 118
161 expect(logic.selectedDate.value, expectedDay); 119 expect(logic.selectedDate.value, expectedDay);
162 }); 120 });
163 121
164 - test('week to day clamps a partial latest week to the latest date',  
165 - () async {  
166 - final logic = _TestReportPeriodLogic();  
167 - logic.initializeQuery(  
168 - ReportPeriod.week,  
169 - ReportDateRangeConfig.lastWeekStart(),  
170 - );  
171 -  
172 - await logic.selectPeriod(ReportPeriod.day);  
173 -  
174 - expect(logic.selectedDate.value, ReportDateRangeConfig.lastDate());  
175 - });  
176 }); 122 });
177 } 123 }