|
|
@@ -1,50 +1,46 @@
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
+import 'package:firebase_auth/firebase_auth.dart';
|
|
|
import 'package:geoflutterfire/geoflutterfire.dart';
|
|
|
import 'dart:math';
|
|
|
|
|
|
+import '../Services/AuthService.dart';
|
|
|
+import '../Services/logged_in_user.dart';
|
|
|
+
|
|
|
class ChallengesUtils {
|
|
|
static final geo = Geoflutterfire();
|
|
|
static final _firestore = FirebaseFirestore.instance;
|
|
|
|
|
|
- static Future<DocumentSnapshot<Map<String, dynamic>>> getChallenge() async {
|
|
|
- Future<DocumentSnapshot<Map<String, dynamic>>> challenge;
|
|
|
+ static generateChallengeIfNeeded() async {
|
|
|
if (await shouldGenerateChallenge()) {
|
|
|
- challenge = generateChallenge();
|
|
|
- } else {
|
|
|
- final user = _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx');
|
|
|
- challenge = user.get().then((value) => value.get('weekly_place').get());
|
|
|
+ generateChallenge();
|
|
|
}
|
|
|
- return challenge;
|
|
|
}
|
|
|
|
|
|
static Future<bool> shouldGenerateChallenge() async {
|
|
|
- final user = _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx');
|
|
|
+ LoggedInUser? user = await AuthenticationServices.getCurrentUser();
|
|
|
DateTime now = DateTime.now();
|
|
|
DateTime today = DateTime(now.year, now.month, now.day);
|
|
|
DateTime lastMonday = today.subtract(Duration(days: today.weekday - 1));
|
|
|
int secondsSinceEpoch = lastMonday.millisecondsSinceEpoch ~/ 1000;
|
|
|
- bool shouldGenerate = await user.get().then((value) {
|
|
|
- final dif = secondsSinceEpoch - value.get('last_challenge_date').seconds;
|
|
|
- return dif / 3600 / 24 > 7;
|
|
|
- });
|
|
|
- return Future.value(shouldGenerate);
|
|
|
+ final dif = secondsSinceEpoch - user!.lastChallengeDate.seconds;
|
|
|
+ return Future.value(dif / 3600 / 24 > 7);
|
|
|
}
|
|
|
|
|
|
- static Future<DocumentSnapshot<Map<String, dynamic>>> generateChallenge() async {
|
|
|
- final user = await _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx').get();
|
|
|
+ static void generateChallenge() async {
|
|
|
+ LoggedInUser? user = await AuthenticationServices.getCurrentUser();
|
|
|
Query<Map<String, dynamic>> places = _firestore.collection('Places');
|
|
|
- double innerRadius = 3;
|
|
|
- double outerRadius = 5;
|
|
|
+ double innerRadius = 1;
|
|
|
+ double outerRadius = 2.5;
|
|
|
String randomPlaceID = '';
|
|
|
|
|
|
Stream<List<DocumentSnapshot>> possiblePlaces = geo.collection(collectionRef: places).within(
|
|
|
- center: geo.point(latitude: user.get('address').latitude, longitude: user.get('address').longitude),
|
|
|
+ center: geo.point(latitude: user!.address.latitude, longitude: user.address.longitude),
|
|
|
radius: outerRadius,
|
|
|
field: 'location',
|
|
|
strictMode: true);
|
|
|
|
|
|
Stream<List<DocumentSnapshot>> placesTooClose = geo.collection(collectionRef: places).within(
|
|
|
- center: geo.point(latitude: user.get('address').latitude, longitude: user.get('address').longitude),
|
|
|
+ center: geo.point(latitude: user.address.latitude, longitude: user.address.longitude),
|
|
|
radius: innerRadius,
|
|
|
field: 'location',
|
|
|
strictMode: true);
|
|
|
@@ -58,10 +54,10 @@ class ChallengesUtils {
|
|
|
possiblePlacesList.retainWhere((element) => !ids.contains(element.id));
|
|
|
int numOfPlaces = possiblePlacesList.length;
|
|
|
randomPlaceID = possiblePlacesList[Random().nextInt(numOfPlaces)].id;
|
|
|
- _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx').update({
|
|
|
+ AuthenticationServices.user!.weeklyPlace = _firestore.collection('Places').doc(randomPlaceID);
|
|
|
+ _firestore.collection("profileInfo").doc(FirebaseAuth.instance.currentUser?.uid).update({
|
|
|
'weekly_place': _firestore.collection('Places').doc(randomPlaceID),
|
|
|
'last_challenge_date': Timestamp.now(),
|
|
|
});
|
|
|
- return _firestore.collection('Places').doc(randomPlaceID).get();
|
|
|
}
|
|
|
}
|