-
Notifications
You must be signed in to change notification settings - Fork 149
/
group_cubit.dart
106 lines (88 loc) · 2.84 KB
/
group_cubit.dart
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
import 'package:beacon/Bloc/core/constants/location.dart';
import 'package:beacon/Bloc/core/resources/data_state.dart';
import 'package:beacon/Bloc/domain/entities/beacon/beacon_entity.dart';
import 'package:beacon/Bloc/domain/usecase/group_usecase.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:geolocator/geolocator.dart';
abstract class GroupState {}
class ShrimmerGroupState extends GroupState {}
class GroupLoadingState extends GroupState {}
class GroupErrorState extends GroupState {
final String error;
GroupErrorState({required this.error});
}
class GroupReloadState extends GroupState {
List<BeaconEntity>? beacons;
BeaconEntity? beacon;
GroupReloadState({this.beacon, this.beacons});
}
class GroupCubit extends Cubit<GroupState> {
final GroupUseCase _groupUseCase;
GroupCubit(this._groupUseCase) : super(ShrimmerGroupState());
int page = 1;
int pageSize = 4;
bool isLoadingMore = false;
bool isCompletelyFetched = false;
List<BeaconEntity> _beacons = [];
List<BeaconEntity> get beacons => _beacons;
Position? position;
Future<void> createHike(String title, int startsAt, int expiresAt, String lat,
String lon, String groupID) async {
emit(GroupLoadingState());
final state = await _groupUseCase.createHike(
title, startsAt, expiresAt, lat, lon, groupID);
if (state is DataFailed) {
emit(GroupErrorState(error: state.error!));
} else {
BeaconEntity? newHike = state.data;
newHike != null ? _beacons.insert(0, newHike) : null;
emit(GroupReloadState());
}
}
Future<void> joinHike(String shortcode) async {
emit(GroupLoadingState());
final state = await _groupUseCase.joinHike(shortcode);
if (state is DataFailed) {
emit(GroupErrorState(error: state.error!));
} else {
BeaconEntity? newHike = state.data;
newHike != null ? _beacons.insert(0, newHike) : null;
emit(GroupReloadState());
}
}
Future<void> fetchGroupHikes(String groupID) async {
if (isLoadingMore == true) return;
if (page == 1) {
emit(ShrimmerGroupState());
}
isLoadingMore = true;
final state = await _groupUseCase.fetchHikes(groupID, page, pageSize);
if (state is DataFailed) {
isLoadingMore = false;
emit(GroupErrorState(error: state.error!));
} else {
final hikes = state.data ?? [];
isLoadingMore = false;
page++;
if (hikes.isEmpty) {
isCompletelyFetched = true;
emit(GroupReloadState());
return;
}
for (var hike in hikes) {
_beacons.add(hike);
}
emit(GroupReloadState());
}
}
Future<void> fetchPosition() async {
position = await LocationService.getCurrentLocation();
}
clear() {
page = 1;
pageSize = 4;
_beacons.clear();
isCompletelyFetched = false;
emit(ShrimmerGroupState());
}
}