Skip to content

Commit

Permalink
Expired beacons fix. (#33)
Browse files Browse the repository at this point in the history
* Expired Duration Fix: Part 1.
Show expired beacons at the very last in your beacons section + No streams should be running (including the one that tracks location)
Tried to implement this.
* Cancelling StreamSubs if beacon expires when subs are active.
* minor miss: setting isBeaconExpired to true.
* Upd: Showing snackBar if user tries to join an expired beacon.
* fix: Updated setting up subscriptions logic.
* fix: setting state so that UI updates if beacon expires.
* FIX: Close the circular loading spinner if there was some error in joining any beacon.
* Update lib/views/hike_screen.dart
* fix: Change the msg to terminate hike if beacon has expired.

Co-authored-by: Nishtha Bodani <nbodani8@gmail.com>
  • Loading branch information
Aadeesh11 and nb9960 committed Dec 30, 2021
1 parent 81788d7 commit 1421f5c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
3 changes: 1 addition & 2 deletions lib/components/beacon_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class BeaconCustomWidgets {
if (_beacon != null) {
navigationService.pushScreen('/hikeScreen',
arguments: HikeScreen(beacon, isLeader: false));
} else {
navigationService.showSnackBar('Something went wrong');
}
//Snackbar is displayed by joinBeacon itself on any error or trying to join expired beacon.
}
},
child: Container(
Expand Down
10 changes: 6 additions & 4 deletions lib/components/dialog_boxes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import 'package:sizer/sizer.dart';

class DialogBoxes {
static AlertDialog showExitDialog(
BuildContext context, bool isLeader, int X) {
BuildContext context, bool isLeader, int X, bool isBeaconExpired) {
return AlertDialog(
title: Text(
'This will terminate the hike, Confirm?',
style: TextStyle(fontSize: 25, color: kYellow),
),
content: Text(
isLeader && (X - 1 > 0)
? 'There are ${X - 1} followers and you are carrying the beacon. Do you want to terminate the hike?'
: 'Are you sure you want to terminate the hike?',
isBeaconExpired
? 'Are you sure you want to exit?'
: isLeader && (X - 1 > 0)
? 'There are ${X - 1} followers and you are carrying the beacon. Do you want to terminate the hike?'
: 'Are you sure you want to terminate the hike?',
style: TextStyle(fontSize: 16, color: kBlack),
),
actionsAlignment: MainAxisAlignment.spaceEvenly,
Expand Down
17 changes: 16 additions & 1 deletion lib/services/database_mutation_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class DataBaseMutationFunctions {
Future<List<Beacon>> fetchUserBeacons() async {
List<Beacon> beacons = [];
Set<String> beaconIds = {};
List<Beacon> expiredBeacons = [];
final QueryResult result = await clientAuth
.query(QueryOptions(document: gql(_authQuery.fetchUserInfo())));
if (result.hasException) {
Expand All @@ -199,10 +200,15 @@ class DataBaseMutationFunctions {
for (var i in userInfo.beacon) {
if (!beaconIds.contains(i.id)) {
beaconIds.add(i.id);
beacons.add(i);
if (DateTime.fromMillisecondsSinceEpoch(i.expiresAt)
.isBefore(DateTime.now()))
expiredBeacons.add(i);
else
beacons.add(i);
}
}
}
beacons.addAll(expiredBeacons);
return beacons;
}

Expand Down Expand Up @@ -261,8 +267,17 @@ class DataBaseMutationFunctions {
final Beacon beacon = Beacon.fromJson(
result.data['joinBeacon'] as Map<String, dynamic>,
);
if (DateTime.fromMillisecondsSinceEpoch(beacon.expiresAt)
.isBefore(DateTime.now())) {
navigationService.showSnackBar(
"Looks like the beacon you are trying join has expired");
return null;
}
beacon.route.add(beacon.leader.location);
return beacon;
} else {
navigationService
.showSnackBar("Something went wrong while trying to join Beacon");
}
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/view_model/home_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ class HomeViewModel extends BaseModel {
navigationService.pushScreen('/hikeScreen',
arguments: HikeScreen(beacon, isLeader: false));
} else {
navigationService.showSnackBar('SomeThing went wrong');
//there was some error, go back to homescreen.
setState(ViewState.idle);
}
//Snackbar is displayed by joinBeacon itself on any error or trying to join expired beacon.
} else {
navigationService.showSnackBar('Enter valid passkey');
}
Expand Down
42 changes: 30 additions & 12 deletions lib/views/hike_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ class _HikeScreenState extends State<HikeScreen> {
});
}

Future<void> setupSubscriptions() async {
Future<void> setupSubscriptions(bool isExpired) async {
if (isBeaconExpired || isExpired) return;
if (widget.isLeader) {
// distanceFilter (in m) can be changed to reduce the backend calls
await loc.changeSettings(interval: 3000, distanceFilter: 0.0);
_leaderLocation =
loc.onLocationChanged.listen((LocationData currentLocation) async {
if (DateTime.fromMillisecondsSinceEpoch(beacon.expiresAt)
.isBefore(DateTime.now())) _leaderLocation.cancel();
Coordinates coordinates =
Coordinates(currentLocation.latitude, currentLocation.longitude);
var addresses =
Expand Down Expand Up @@ -125,7 +128,15 @@ class _HikeScreenState extends State<HikeScreen> {
} else {
mergedStream = beaconJoinedStream;
}
final mergeStreamSubscription = mergedStream.listen((event) async {
StreamSubscription<dynamic> mergeStreamSubscription;
mergeStreamSubscription = mergedStream.listen((event) async {
if (DateTime.fromMillisecondsSinceEpoch(beacon.expiresAt)
.isBefore(DateTime.now())) {
mergeStreamSubscription.cancel();
setState(() {
isBeaconExpired = true;
});
}
if (event.data != null) {
print('${event.data}');
if (event.data.containsKey('beaconJoined')) {
Expand Down Expand Up @@ -194,12 +205,13 @@ class _HikeScreenState extends State<HikeScreen> {

@override
void dispose() {
if (widget.isLeader) {
if (widget.isLeader && !isBeaconExpired) {
_leaderLocation.cancel();
}
for (var streamSub in mergedStreamSubscriptions) {
streamSub.cancel();
}
if (!isBeaconExpired)
for (var streamSub in mergedStreamSubscriptions) {
streamSub.cancel();
}
super.dispose();
}

Expand Down Expand Up @@ -234,6 +246,8 @@ class _HikeScreenState extends State<HikeScreen> {
await databaseFunctions.fetchBeaconInfo(widget.beacon.id).then((value) {
beacon = value;
setState(() {
isBeaconExpired = DateTime.fromMillisecondsSinceEpoch(beacon.expiresAt)
.isBefore(DateTime.now());
hikers.add(value.leader);
for (var i in value.followers) {
if (!followerId.contains(i.id)) {
Expand Down Expand Up @@ -294,7 +308,8 @@ class _HikeScreenState extends State<HikeScreen> {
beacon = widget.beacon;
fetchData();
graphQlClient = GraphQLConfig().graphQlClient();
setupSubscriptions();
setupSubscriptions(DateTime.fromMillisecondsSinceEpoch(beacon.expiresAt)
.isBefore(DateTime.now()));
isBusy = false;
}

Expand Down Expand Up @@ -365,8 +380,9 @@ class _HikeScreenState extends State<HikeScreen> {
'Total Followers: ${hikers.length - 1} (Swipe up to view the list of followers)\n',
style: TextStyle(fontSize: 12)),
TextSpan(
text:
'Share this passkey to add user: ${widget.beacon.shortcode}\n',
text: isBeaconExpired
? ''
: 'Share this passkey to add user: ${widget.beacon.shortcode}\n',
style: TextStyle(fontSize: 12)),
]),
),
Expand Down Expand Up @@ -502,8 +518,10 @@ class _HikeScreenState extends State<HikeScreen> {
),
Align(
alignment: Alignment(0.87, -0.85),
child: HikeScreenWidget.shareButton(
context, widget.beacon.shortcode)),
child: isBeaconExpired
? Container()
: HikeScreenWidget.shareButton(
context, widget.beacon.shortcode)),
Align(
alignment: Alignment(-0.8, -0.9),
child: GestureDetector(
Expand Down Expand Up @@ -642,7 +660,7 @@ class _HikeScreenState extends State<HikeScreen> {
return (await showDialog(
context: context,
builder: (context) => DialogBoxes.showExitDialog(
context, widget.isLeader, hikers.length),
context, widget.isLeader, hikers.length, isBeaconExpired),
)) ??
false;
}
Expand Down

0 comments on commit 1421f5c

Please sign in to comment.