friend_interaction_content.dart
9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import 'package:doublefeel_flutter/data/models/interaction/interaction_models.dart';
import 'package:doublefeel_flutter/l10n/l10n_extensions.dart';
import 'package:flutter/widgets.dart';
import '../../report_common/utils/report_date_formatter.dart';
import '../../report_common/utils/report_localization.dart';
/// The display payload of a friend-interaction sheet.
///
/// The interaction type is an API contract; this class deliberately contains
/// only what the sheet needs to render. A host page can therefore provide its
/// own date-aware title and metrics without duplicating the sheet layout.
@immutable
class FriendInteractionContent {
const FriendInteractionContent({
required this.title,
this.metrics = const [],
});
final String title;
final List<FriendInteractionMetric> metrics;
/// A title-only presentation used by trend reports, where the design has no
/// numeric detail below the title. [period] supplies the date semantics
/// from the table (today/yesterday/a date, a week range, month, or year).
factory FriendInteractionContent.forType(
BuildContext context,
FriendInteractionType type, {
String? title,
FriendInteractionPeriod? period,
List<FriendInteractionMetric> metrics = const [],
}) => FriendInteractionContent(
title: title ?? _titleFor(context, type, period),
metrics: metrics,
);
static String _titleFor(
BuildContext context,
FriendInteractionType type,
FriendInteractionPeriod? period,
) {
if (type == FriendInteractionType.yearStressTrend) {
return context.l10n.interactSceneTitle(
InteractionSceneTitle.yearStressTrend,
year: period?.start.year,
);
}
final isCurrentPeriod = period?.isCurrent ?? false;
final title = switch (type) {
FriendInteractionType.defaultInteraction =>
context.l10n.interactTodaySteps,
FriendInteractionType.todayAverageHrv => context.l10n.interactSceneTitle(
InteractionSceneTitle.todayAverageHrv,
),
FriendInteractionType.todayRestingHeartRate =>
context.l10n.interactSceneTitle(
InteractionSceneTitle.todayRestingHeartRate,
),
FriendInteractionType.todaySleepStatus ||
FriendInteractionType.dailySleepStatus => context.l10n.interactSceneTitle(
InteractionSceneTitle.todaySleepStatus,
),
FriendInteractionType.todayFitnessStatus ||
FriendInteractionType.dailyActivityBurn =>
context.l10n.interactSceneTitle(
type == FriendInteractionType.todayFitnessStatus
? InteractionSceneTitle.todayFitnessStatus
: InteractionSceneTitle.todayActivityBurn,
),
FriendInteractionType.weekStressTrend => context.l10n.interactSceneTitle(
isCurrentPeriod
? InteractionSceneTitle.weekStressTrend
: InteractionSceneTitle.stressTrend,
),
FriendInteractionType.monthStressTrend => context.l10n.interactSceneTitle(
isCurrentPeriod
? InteractionSceneTitle.monthStressTrend
: InteractionSceneTitle.stressTrend,
),
FriendInteractionType.weeklyActivityBurn ||
FriendInteractionType.monthlyActivityBurn =>
context.l10n.interactSceneTitle(
type == FriendInteractionType.weeklyActivityBurn
? (isCurrentPeriod
? InteractionSceneTitle.weekActivityBurn
: InteractionSceneTitle.activityBurn)
: (isCurrentPeriod
? InteractionSceneTitle.monthActivityBurn
: InteractionSceneTitle.activityBurn),
),
FriendInteractionType.weeklySleepTime ||
FriendInteractionType.monthlySleepTime => context.l10n.interactSceneTitle(
type == FriendInteractionType.weeklySleepTime
? (isCurrentPeriod
? InteractionSceneTitle.weekSleepTime
: InteractionSceneTitle.sleepTime)
: (isCurrentPeriod
? InteractionSceneTitle.monthSleepTime
: InteractionSceneTitle.sleepTime),
),
FriendInteractionType.pkSteps => context.l10n.interactSceneTitle(
InteractionSceneTitle.pkSteps,
),
FriendInteractionType.pkCalories => context.l10n.interactSceneTitle(
InteractionSceneTitle.pkCalories,
),
FriendInteractionType.pkStandCount => context.l10n.interactSceneTitle(
InteractionSceneTitle.pkStandTime,
),
FriendInteractionType.pkMonthlyResult => context.l10n.interactSceneTitle(
InteractionSceneTitle.pkMonthlyResult,
),
FriendInteractionType.yearStressTrend => '',
};
final periodLabel = period?.labelFor(context);
return periodLabel == null || periodLabel.isEmpty
? title
: '$title ($periodLabel)';
}
}
/// Describes the selected report time without coupling the sheet to a page
/// controller. This covers every date form in the interaction-scene table.
@immutable
class FriendInteractionPeriod {
const FriendInteractionPeriod.day(this.start, {this.referenceDate})
: kind = FriendInteractionPeriodKind.day,
end = null,
periodName = null;
const FriendInteractionPeriod.week(
this.start,
this.end, {
this.periodName,
this.referenceDate,
}) : kind = FriendInteractionPeriodKind.week;
const FriendInteractionPeriod.month(
this.start, {
this.periodName,
this.referenceDate,
}) : kind = FriendInteractionPeriodKind.month,
end = null;
const FriendInteractionPeriod.year(
this.start, {
this.periodName,
this.referenceDate,
}) : kind = FriendInteractionPeriodKind.year,
end = null;
final FriendInteractionPeriodKind kind;
final DateTime start;
final DateTime? end;
/// Localized page label when one exists, e.g. “This week”. It is optional
/// because a locale-aware date range remains meaningful on its own.
final String? periodName;
final DateTime? referenceDate;
/// Whether this period is the current natural period relative to the
/// supplied reference date (or today). Historical periods intentionally use
/// neutral titles rather than “this week” or “this month”.
bool get isCurrent {
final reference = _dateOnly(referenceDate ?? DateTime.now());
final date = _dateOnly(start);
return switch (kind) {
FriendInteractionPeriodKind.day => date == reference,
FriendInteractionPeriodKind.week => _isCurrentWeek(date, reference),
FriendInteractionPeriodKind.month => _isCurrentMonth(date, reference),
FriendInteractionPeriodKind.year => date.year == reference.year,
};
}
String labelFor(BuildContext context) {
final reference = _dateOnly(referenceDate ?? DateTime.now());
final locale = reportLocaleName(context);
final date = _dateOnly(start);
return switch (kind) {
FriendInteractionPeriodKind.day => _dayLabel(
context,
date,
reference,
locale,
),
FriendInteractionPeriodKind.week => _join(
periodName ??
(_isCurrentWeek(date, reference)
? context.l10n.interactThisWeek
: null),
ReportDateFormatter.weekRange(
date,
_dateOnly(end ?? start),
locale,
referenceDate: reference,
),
),
FriendInteractionPeriodKind.month => _join(
periodName ??
(_isCurrentMonth(date, reference)
? context.l10n.interactThisMonth
: null),
ReportDateFormatter.weekRange(
date,
DateTime(date.year, date.month + 1, 0),
locale,
referenceDate: reference,
),
),
FriendInteractionPeriodKind.year => _join(
periodName,
ReportDateFormatter.year(date, locale),
),
};
}
static String _dayLabel(
BuildContext context,
DateTime date,
DateTime reference,
String locale,
) {
final difference = reference.difference(date).inDays;
if (difference == 0) return context.l10n.today;
if (difference == 1) return context.l10n.yesterday;
return date.year == reference.year
? ReportDateFormatter.monthDay(date, locale)
: ReportDateFormatter.yearMonthDay(date, locale);
}
static DateTime _dateOnly(DateTime value) =>
DateTime(value.year, value.month, value.day);
static bool _isCurrentWeek(DateTime date, DateTime reference) {
final currentWeekStart = reference.subtract(
Duration(days: reference.weekday - DateTime.monday),
);
return date == currentWeekStart;
}
static bool _isCurrentMonth(DateTime date, DateTime reference) =>
date.year == reference.year && date.month == reference.month;
static String _join(String? name, String date) =>
name == null || name.isEmpty ? date : '$name, $date';
}
enum FriendInteractionPeriodKind { day, week, month, year }
/// One horizontally arranged data item in [FriendInteractionContent].
@immutable
class FriendInteractionMetric {
const FriendInteractionMetric({
required this.value,
this.unit,
this.label,
this.isText = false,
});
final String value;
final String? unit;
/// Optional compact pill (for example, “Workout”, “Stand”, or “Quality”).
final String? label;
/// Textual states such as sleep quality use the smaller value treatment in
/// the Figma design instead of the large numeric treatment.
final bool isText;
}