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

Expired beacons fix. #33

Merged
merged 24 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7619bb5
rfrac: removed unused imports from multiple files.
Aadeesh11 Dec 26, 2021
98c21a0
rfrac: migrated deprecated widgets.
Aadeesh11 Dec 26, 2021
1e0ebcd
fix: fixed the alignment of buttons on the dialogue box when user ter…
Aadeesh11 Dec 26, 2021
22f0474
fix: used sizer to fix #16
Aadeesh11 Dec 26, 2021
31f9845
rfrac: Used proper duration picker import.
Aadeesh11 Dec 26, 2021
cb95ef1
Merge branch 'main' into MisalignedButtonFix
Aadeesh11 Dec 27, 2021
3597005
Merge branch 'MisalignedButtonFix'
Aadeesh11 Dec 27, 2021
eecd365
Merge remote-tracking branch 'upstream/main'
Aadeesh11 Dec 27, 2021
0b0dfa2
rfrac: duplicate imports removed.
Aadeesh11 Dec 27, 2021
1b5420f
Expired Duration Fix: Part 1.
Aadeesh11 Dec 27, 2021
b39b060
Cancelling StreamSubs if beacon expires when subs are active.
Aadeesh11 Dec 27, 2021
7c94114
minor miss: setting isBeaconExpired to true.
Aadeesh11 Dec 27, 2021
3b57de8
Upd: Showing snackBar if user tries to join an expired beacon.
Aadeesh11 Dec 27, 2021
29fb19a
Merge branch 'main' into Expired-Beacons-Fix
Aadeesh11 Dec 28, 2021
5b01415
rfrac: Ran flutter format so the CI wont fail.
Aadeesh11 Dec 28, 2021
8120c18
Merge remote-tracking branch 'upstream/main'
Aadeesh11 Dec 28, 2021
ea6601d
fix: Updated setting up subscriptions logic.
Aadeesh11 Dec 28, 2021
3f67541
fix: setting state so that UI updates if beacon expires.
Aadeesh11 Dec 28, 2021
37f9ce4
FIX: Close the circular loading spinner if there was some error in jo…
Aadeesh11 Dec 28, 2021
4434e27
Merge branch 'main' into Expired-Beacons-Fix
Aadeesh11 Dec 28, 2021
757258e
Merge remote-tracking branch 'upstream/main' into Expired-Beacons-Fix
Aadeesh11 Dec 28, 2021
1a304bd
Update lib/views/hike_screen.dart
Aadeesh11 Dec 29, 2021
cd90924
Merge remote-tracking branch 'upstream/main' into Expired-Beacons-Fix
Aadeesh11 Dec 30, 2021
8076cad
fix: Change the msg to terminate hike if beacon has expired.
Aadeesh11 Dec 30, 2021
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
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