trend_controller.dart
4.7 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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../../core/network/api/friend_api.dart';
import '../../../../../core/result/app_result.dart';
import '../../../../../data/models/friend/friend_models.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';
import '../../widgets/trend/trend_friend_select_bottom_sheet.dart';
enum TrendType {
hrv,
activity,
sleep;
int get tabIndex => index;
}
/// 趋势页顶层 Controller,仅负责:
/// 顶层 HRV / 活动 / 睡眠 类型切换 (selectedTypeIndex)
class TrendController extends GetxController with HealthTrendControl {
TrendController(this._friendApi);
final FriendApi _friendApi;
bool _isPageVisible = false;
final refreshToken = 0.obs;
// 当前查看的用户。null 表示查看自己;非 null 表示查看指定用户。
final targetUserId = RxnInt();
final targetFriendInfo = Rxn<FriendItem>();
final friendsList = <FriendItem>[].obs;
final isFriendListLoaded = false.obs;
int friendsListLimit = 10;
int _friendListRequestId = 0;
bool get canSwitchFriend =>
isFriendListLoaded.value && friendsList.isNotEmpty;
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 onInit() {
super.onInit();
unawaited(_refreshFriendList());
}
@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, {bool refresh = true}) {
if (userId == targetUserId.value) return;
targetUserId.value = userId;
if (refresh) {
refreshToken.value++;
}
}
void selectFriend(FriendItem friendInfo) {
if (friendInfo.friendUserId == targetFriendInfo.value?.friendUserId) {
return;
}
targetFriendInfo.value = friendInfo;
changeTargetUser(friendInfo.friendUserId);
}
void selectSelf({bool refresh = true}) {
if (targetFriendInfo.value == null && targetUserId.value == null) {
return;
}
targetFriendInfo.value = null;
changeTargetUser(null, refresh: refresh);
}
void showFriendListBottomSheet() {
unawaited(_refreshFriendList());
if (friendsList.isEmpty) return;
Get.bottomSheet(
DraggableScrollableSheet(
maxChildSize: 0.6,
initialChildSize: 0.6,
expand: false,
snap: true,
builder: (context, scrollController) {
return TrendFriendSelectBottomSheet(
scrollController: scrollController,
friendsList: friendsList,
selectedFriend: targetFriendInfo,
onSelectSelf: selectSelf,
onSelectFriend: selectFriend,
);
},
),
barrierColor: Colors.black.withValues(alpha: 0.7),
enableDrag: true,
isScrollControlled: true,
persistent: false,
);
}
Future<void> _refreshFriendList() async {
final requestId = ++_friendListRequestId;
final res = await _friendApi.friendList(false);
if (requestId != _friendListRequestId) return;
switch (res) {
case AppSuccess(:final data):
friendsList.assignAll(data.list);
friendsListLimit = data.limit ?? 10;
case AppFailure():
break;
}
isFriendListLoaded.value = true;
}
void setPageVisible(
bool visible, {
bool resetQueryOnShow = false,
}) {
if (!visible) {
_markPageHidden();
return;
}
if (resetQueryOnShow) {
resetQueries();
}
unawaited(_refreshFriendList());
_markPageVisible();
}
void _markPageVisible() {
if (_isPageVisible) return;
_isPageVisible = true;
refreshToken.value++;
_trackEnterPage();
}
void _markPageHidden() {
_isPageVisible = false;
}
void _trackEnterPage() {
HealthTrendAnalytics.trackEnterPage(
selectedTypeIndex.value,
userRole: targetFriendInfo.value == null ? '我的' : '好友的',
);
}
}