Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Relay beacon to new leader #122

Merged
merged 9 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions lib/components/hike_screen_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:share_plus/share_plus.dart';

import 'package:sizer/sizer.dart';

class HikeScreenWidget {
class HikeScreenWidget extends ChangeNotifier {
static copyPasskey(String passkey) {
Clipboard.setData(ClipboardData(text: passkey));
Fluttertoast.showToast(msg: 'PASSKEY: $passkey COPIED');
Expand Down Expand Up @@ -200,12 +200,6 @@ class HikeScreenWidget {
text:
'Long Press on any hiker to hand over the beacon\n',
style: TextStyle(fontSize: 16)),
//TODO: enable this once backend has updated.
//Commented, since we dont have the neccessary mutation atm on backend to change the duration.
// TextSpan(
// text:
// 'Double tap on beacon to change the duration\n',
// style: TextStyle(fontSize: 14)),
],
),
),
Expand All @@ -223,25 +217,21 @@ class HikeScreenWidget {
itemCount: model.hikers.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
onTap: () {
model.hikers[index].id == userConfig.currentUser.id
? Fluttertoast.showToast(msg: 'Yeah, that\'s you')
: model.beacon.leader.id ==
userConfig.currentUser.id
? model.relayBeacon(model.hikers[index])
: Fluttertoast.showToast(
msg: 'You dont have beacon to relay');
onLongPress: () async {
model.relayBeacon(
model.hikers[index].name, model.hikers[index].id);
},
leading: CircleAvatar(
backgroundColor:
model.isBeaconExpired ? Colors.grey : kYellow,
radius: 18,
child: ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Icon(
Icons.person_outline,
color: Colors.white,
)),
borderRadius: BorderRadius.circular(50),
child: Icon(
Icons.person_outline,
color: Colors.white,
),
),
),
title: Text(
model.hikers[index].name,
Expand Down
37 changes: 36 additions & 1 deletion lib/queries/beacon.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
import 'package:graphql_flutter/graphql_flutter.dart';

class BeaconQueries {
String changeLeader(String beaconID, String newLeaderID) {
return '''
mutation{
changeLeader (beaconID:"$beaconID" ,newLeaderID: "$newLeaderID")
{
_id
title
shortcode
leader {
_id
name
}
location{
lat
lon
}
followers {
_id
name
}
startsAt
expiresAt
landmarks {
title
location {
lat
lon
}
}
}
}
}
''';
}

String createBeacon(
String title, int startsAt, int expiresAt, String lat, String lon) {
return '''
Expand Down Expand Up @@ -188,7 +223,7 @@ class BeaconQueries {
}
''');

// Gql for oreder updated subscription.
// Gql for order updated subscription.
final beaconJoinedSubGql = gql(r'''
subscription StreamNewlyJoinedBeacons($id: ID!){
beaconJoined(id: $id){
Expand Down
19 changes: 19 additions & 0 deletions lib/services/database_mutation_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,23 @@ class DataBaseMutationFunctions {
}
return _nearbyBeacons;
}

Future<Beacon> changeLeader(String beaconID, String newLeaderID) async {
await clientAuth
.mutate(MutationOptions(
document: gql(_beaconQuery.changeLeader(beaconID, newLeaderID))))
.then((value) {
if (value.hasException) {
navigationService.showSnackBar(
"Something went wrong: ${value.exception.graphqlErrors.first.message}");
print("Something went wrong: ${value.exception}");
} else if (value.data != null && value.isConcrete) {
final Beacon changedLeader =
Beacon.fromJson(value.data['changeLeader'] as Map<String, dynamic>);
return changedLeader;
}
return null;
});
return null;
}
}
18 changes: 16 additions & 2 deletions lib/view_model/hike_screen_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,22 @@ class HikeScreenViewModel extends BaseModel {
final List<StreamSubscription> mergedStreamSubscriptions = [];
bool isLeader = false;

void relayBeacon(User newHolder) {
Fluttertoast.showToast(msg: 'Beacon handed over to $newHolder');
void relayBeacon(String newLeaderName, String newLeaderID) async {
print(newLeaderID);
if (newLeaderID == userConfig.currentUser.id)
Fluttertoast.showToast(msg: 'Yeah, that\'s you');
else {
if (beacon.leader.id == userConfig.currentUser.id) {
await databaseFunctions.init();
final changedLeader =
databaseFunctions.changeLeader(beacon.id, newLeaderID);
if (changedLeader != null) beacon.leader.id = newLeaderID;
Fluttertoast.showToast(msg: 'Beacon handed over to $newLeaderName');
notifyListeners();
} else {
Fluttertoast.showToast(msg: 'You dont have beacon to relay');
}
}
}

Future<bool> onWillPop(context) async {
Expand Down
9 changes: 8 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -1030,7 +1037,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.3"
version: "0.4.8"
test_core:
dependency: transitive
description:
Expand Down