Skip to content

Commit 9b81f75

Browse files
Feature: Relay beacon to new leader (#122)
* initial * user online missing * added more fields in mutation * feat: relay beacon to new leader * small fix * snackbar message improvement * fix: moved code to viewmodel
1 parent f9ef249 commit 9b81f75

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

lib/components/hike_screen_widget.dart

+10-20
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import 'package:share_plus/share_plus.dart';
1818

1919
import 'package:sizer/sizer.dart';
2020

21-
class HikeScreenWidget {
21+
class HikeScreenWidget extends ChangeNotifier {
2222
static copyPasskey(String passkey) {
2323
Clipboard.setData(ClipboardData(text: passkey));
2424
Fluttertoast.showToast(msg: 'PASSKEY: $passkey COPIED');
@@ -200,12 +200,6 @@ class HikeScreenWidget {
200200
text:
201201
'Long Press on any hiker to hand over the beacon\n',
202202
style: TextStyle(fontSize: 16)),
203-
//TODO: enable this once backend has updated.
204-
//Commented, since we dont have the neccessary mutation atm on backend to change the duration.
205-
// TextSpan(
206-
// text:
207-
// 'Double tap on beacon to change the duration\n',
208-
// style: TextStyle(fontSize: 14)),
209203
],
210204
),
211205
),
@@ -223,25 +217,21 @@ class HikeScreenWidget {
223217
itemCount: model.hikers.length,
224218
itemBuilder: (BuildContext context, int index) {
225219
return ListTile(
226-
onTap: () {
227-
model.hikers[index].id == userConfig.currentUser.id
228-
? Fluttertoast.showToast(msg: 'Yeah, that\'s you')
229-
: model.beacon.leader.id ==
230-
userConfig.currentUser.id
231-
? model.relayBeacon(model.hikers[index])
232-
: Fluttertoast.showToast(
233-
msg: 'You dont have beacon to relay');
220+
onLongPress: () async {
221+
model.relayBeacon(
222+
model.hikers[index].name, model.hikers[index].id);
234223
},
235224
leading: CircleAvatar(
236225
backgroundColor:
237226
model.isBeaconExpired ? Colors.grey : kYellow,
238227
radius: 18,
239228
child: ClipRRect(
240-
borderRadius: BorderRadius.circular(50),
241-
child: Icon(
242-
Icons.person_outline,
243-
color: Colors.white,
244-
)),
229+
borderRadius: BorderRadius.circular(50),
230+
child: Icon(
231+
Icons.person_outline,
232+
color: Colors.white,
233+
),
234+
),
245235
),
246236
title: Text(
247237
model.hikers[index].name,

lib/queries/beacon.dart

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
import 'package:graphql_flutter/graphql_flutter.dart';
22

33
class BeaconQueries {
4+
String changeLeader(String beaconID, String newLeaderID) {
5+
return '''
6+
mutation{
7+
changeLeader (beaconID:"$beaconID" ,newLeaderID: "$newLeaderID")
8+
{
9+
_id
10+
title
11+
shortcode
12+
leader {
13+
_id
14+
name
15+
}
16+
location{
17+
lat
18+
lon
19+
}
20+
followers {
21+
_id
22+
name
23+
}
24+
startsAt
25+
expiresAt
26+
landmarks {
27+
title
28+
location {
29+
lat
30+
lon
31+
}
32+
}
33+
}
34+
}
35+
}
36+
''';
37+
}
38+
439
String createBeacon(
540
String title, int startsAt, int expiresAt, String lat, String lon) {
641
return '''
@@ -188,7 +223,7 @@ class BeaconQueries {
188223
}
189224
''');
190225

191-
// Gql for oreder updated subscription.
226+
// Gql for order updated subscription.
192227
final beaconJoinedSubGql = gql(r'''
193228
subscription StreamNewlyJoinedBeacons($id: ID!){
194229
beaconJoined(id: $id){

lib/services/database_mutation_functions.dart

+19
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,23 @@ class DataBaseMutationFunctions {
370370
}
371371
return _nearbyBeacons;
372372
}
373+
374+
Future<Beacon> changeLeader(String beaconID, String newLeaderID) async {
375+
await clientAuth
376+
.mutate(MutationOptions(
377+
document: gql(_beaconQuery.changeLeader(beaconID, newLeaderID))))
378+
.then((value) {
379+
if (value.hasException) {
380+
navigationService.showSnackBar(
381+
"Something went wrong: ${value.exception.graphqlErrors.first.message}");
382+
print("Something went wrong: ${value.exception}");
383+
} else if (value.data != null && value.isConcrete) {
384+
final Beacon changedLeader =
385+
Beacon.fromJson(value.data['changeLeader'] as Map<String, dynamic>);
386+
return changedLeader;
387+
}
388+
return null;
389+
});
390+
return null;
391+
}
373392
}

lib/view_model/hike_screen_model.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,22 @@ class HikeScreenViewModel extends BaseModel {
4949
final List<StreamSubscription> mergedStreamSubscriptions = [];
5050
bool isLeader = false;
5151

52-
void relayBeacon(User newHolder) {
53-
Fluttertoast.showToast(msg: 'Beacon handed over to $newHolder');
52+
void relayBeacon(String newLeaderName, String newLeaderID) async {
53+
print(newLeaderID);
54+
if (newLeaderID == userConfig.currentUser.id)
55+
Fluttertoast.showToast(msg: 'Yeah, that\'s you');
56+
else {
57+
if (beacon.leader.id == userConfig.currentUser.id) {
58+
await databaseFunctions.init();
59+
final changedLeader =
60+
databaseFunctions.changeLeader(beacon.id, newLeaderID);
61+
if (changedLeader != null) beacon.leader.id = newLeaderID;
62+
Fluttertoast.showToast(msg: 'Beacon handed over to $newLeaderName');
63+
notifyListeners();
64+
} else {
65+
Fluttertoast.showToast(msg: 'You dont have beacon to relay');
66+
}
67+
}
5468
}
5569

5670
Future<bool> onWillPop(context) async {

0 commit comments

Comments
 (0)