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 7 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
46 changes: 26 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 @@ -195,12 +195,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 @@ -218,25 +212,37 @@ 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 {
print(model.hikers[index].id);
if (model.hikers[index].id == userConfig.currentUser.id)
Fluttertoast.showToast(msg: 'Yeah, that\'s you');
else {
if (model.beacon.leader.id ==
userConfig.currentUser.id) {
await databaseFunctions.init();
final changedLeader =
databaseFunctions.changeLeader(
model.beacon.id, model.hikers[index].id);
if (changedLeader != null)
model.relayBeacon(model.hikers[index].toString(),
model.hikers[index].id);
} else {
Fluttertoast.showToast(
msg: 'You dont have beacon to relay');
}
}
ItsAdityaKSingh marked this conversation as resolved.
Show resolved Hide resolved
},
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 @@ -340,4 +340,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;
}
}
6 changes: 4 additions & 2 deletions lib/view_model/hike_screen_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ 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 {
beacon.leader.id = newLeaderID;
Fluttertoast.showToast(msg: 'Beacon handed over to $newLeaderName');
notifyListeners();
}

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 @@ -597,6 +597,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 @@ -965,7 +972,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.3"
version: "0.4.8"
timezone:
dependency: transitive
description:
Expand Down