trend_controller.dart
1.94 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
import 'package:get/get.dart';
import '../../../health_trend/controllers/health_trend_analytics.dart';
import '../../../health_trend/controllers/health_trend_control.dart';
import '../../../report_common/models/health_report_query.dart';
import '../../../report_common/models/report_period.dart';
enum TrendType {
hrv,
activity,
sleep;
int get tabIndex => index;
}
/// 趋势页顶层 Controller,仅负责:
/// 顶层 HRV / 活动 / 睡眠 类型切换 (selectedTypeIndex)
class TrendController extends GetxController with HealthTrendControl {
bool _isPageVisible = false;
// 当前查看的用户。null 表示查看自己;非 null 表示查看指定用户。
final targetUserId = RxnInt();
HealthReportQuery get query => HealthReportQuery(
targetUserId: targetUserId.value,
period: selectedPeriod.value,
date: selectedDate.value,
);
HealthReportQuery queryForType(int typeIndex) => HealthReportQuery(
targetUserId: targetUserId.value,
period: periodForType(typeIndex),
date: dateForType(typeIndex),
);
@override
void changeType(int index) {
final previousIndex = selectedTypeIndex.value;
super.changeType(index);
if (_isPageVisible && selectedTypeIndex.value != previousIndex) {
_trackEnterPage();
}
}
void selectType(TrendType type, {DateTime? date, ReportPeriod? period}) {
changeType(type.tabIndex);
if (period != null) {
changePeriod(period);
}
if (date != null) {
changeDate(date);
}
}
void changeTargetUser(int? userId) {
if (userId == targetUserId.value) return;
targetUserId.value = userId;
}
void markPageVisible() {
if (_isPageVisible) return;
_isPageVisible = true;
_trackEnterPage();
}
void markPageHidden() {
_isPageVisible = false;
}
void _trackEnterPage() {
HealthTrendAnalytics.trackEnterPage(
selectedTypeIndex.value,
userRole: '我的',
);
}
}