select_friend_logic.dart
1.53 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
import 'package:doublefeel_flutter/core/network/api/friend_api.dart';
import 'package:get/get.dart';
import '../data/friends_repository.dart';
import '../models/friend_health_data.dart';
class SelectFriendLogic {
SelectFriendLogic({FriendsRepository? repository})
: _repository =
repository ?? FriendsRepositoryImpl(Get.find<FriendApi>());
final FriendsRepository _repository;
final friends = <FriendHealthData>[].obs;
final isLoading = false.obs;
final selectedIndex = (-1).obs;
Future<void> initialize() async {
if (isLoading.value) return;
isLoading.value = true;
try {
friends.assignAll(await _repository.getFriends());
final watchFaceIndex = friends.indexWhere(
(friend) => friend.isOnWatchFace,
);
selectedIndex.value = friends.isEmpty
? -1
: watchFaceIndex < 0
? 0
: watchFaceIndex;
} finally {
isLoading.value = false;
}
}
void selectFriendAt(int index) {
if (index < 0 || index >= friends.length) return;
selectedIndex.value = index;
}
Future<FriendHealthData?> syncSelectedFriendToWatchFace() async {
final index = selectedIndex.value;
if (index < 0 || index >= friends.length) return null;
final selectedFriend = friends[index];
if (selectedFriend.isOnWatchFace) {
return null;
}
final userId = selectedFriend.userId;
if (userId == null) return null;
await _repository.selectWatchFaceFriend(userId);
return selectedFriend;
}
void dispose() {}
}