浏览代码

Generating challenges

Marcin Jaborski 3 年之前
父节点
当前提交
5162e4ff20
共有 5 个文件被更改,包括 146 次插入16 次删除
  1. 32 14
      app/lib/main.dart
  2. 23 0
      app/lib/navigation/challenge_page.dart
  3. 67 0
      app/lib/navigation/utils/challenges_utils.dart
  4. 23 2
      app/pubspec.lock
  5. 1 0
      app/pubspec.yaml

+ 32 - 14
app/lib/main.dart

@@ -2,6 +2,7 @@ import 'package:firebase_core/firebase_core.dart';
 import 'package:flutter/material.dart';
 import 'package:latlong2/latlong.dart';
 import 'navigation/navigation_page.dart';
+import 'navigation/challenge_page.dart';
 
 import 'firebase_options.dart';
 
@@ -33,21 +34,38 @@ class HomePage extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return Scaffold(
-      body: Center(
-        child: TextButton(
-            onPressed: () {
-              Navigator.push(
-                context,
-                MaterialPageRoute(
-                  // Example on how to use NavigationPage
-                  builder: (context) => NavigationPage(
-                    destination: LatLng(51.78036111980833, 19.451262207821234),
+      body: Column(
+        mainAxisAlignment: MainAxisAlignment.center,
+        crossAxisAlignment: CrossAxisAlignment.center,
+        children: [Center(
+          child: TextButton(
+              onPressed: () {
+                Navigator.push(
+                  context,
+                  MaterialPageRoute(
+                    // Example on how to use NavigationPage
+                    builder: (context) => NavigationPage(
+                      destination: LatLng(51.78036111980833, 19.451262207821234),
+                    ),
                   ),
-                ),
-              );
-            },
-            child: const Text('Navigation')),
-      ),
+                );
+              },
+              child: const Text('Navigation')),
+        ),
+          Center(
+            child: TextButton(
+                onPressed: () {
+                  Navigator.push(
+                    context,
+                    MaterialPageRoute(
+                      // Example on how to use NavigationPage
+                      builder: (context) => const ChallengePage(),
+                    ),
+                  );
+                },
+                child: const Text('Challenges')),
+          ),],
+      )
     );
   }
 }

+ 23 - 0
app/lib/navigation/challenge_page.dart

@@ -0,0 +1,23 @@
+import 'package:cloud_firestore/cloud_firestore.dart';
+import 'package:flutter/material.dart';
+import 'package:physigo/navigation/utils/challenges_utils.dart';
+
+class ChallengePage extends StatelessWidget {
+
+  const ChallengePage({Key? key}) : super(key: key);
+
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      body: FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>(
+        future: ChallengesUtils.getChallenge(),
+        builder: (context, snapshot) {
+          if(snapshot.hasData) {
+            print(snapshot.data?.get('name'));
+          }
+          return const Text('Challenges are here');
+        },
+      ),
+    );
+  }
+}

+ 67 - 0
app/lib/navigation/utils/challenges_utils.dart

@@ -0,0 +1,67 @@
+import 'package:cloud_firestore/cloud_firestore.dart';
+import 'dart:math';
+
+class ChallengesUtils {
+  static Future<DocumentSnapshot<Map<String, dynamic>>> getChallenge() async {
+    Future<DocumentSnapshot<Map<String, dynamic>>> challenge;
+    if(await shouldGenerateChallenge()){
+      challenge = generateChallenge();
+    } else {
+      final user = FirebaseFirestore.instance
+          .collection('Users')
+          .doc('tlmysIvwTBaoZKWqBofx');
+      challenge = user.get().then((value) => value.get('weekly_place').get());
+    }
+    return challenge;
+  }
+
+  static Future<bool> shouldGenerateChallenge() async {
+    final user = FirebaseFirestore.instance
+        .collection('Users')
+        .doc('tlmysIvwTBaoZKWqBofx');
+    DateTime now = DateTime.now();
+    int secondsSinceEpoch = now.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);
+  }
+
+  static Future<DocumentSnapshot<Map<String, dynamic>>> generateChallenge() {
+    final user = FirebaseFirestore.instance
+        .collection('Users')
+        .doc('tlmysIvwTBaoZKWqBofx');
+    return user.get().then((value) {
+      double lat = 0.0144927536231884;
+      double lon = 0.0181818181818182;
+      double userLat = value.get('address').latitude;
+      double userLon = value.get('address').longitude;
+
+      double lowerLat = userLat - (lat * 5);
+      double lowerLon = userLon - (lon * 5);
+      double upperLat = userLat + (lat * 5);
+      double upperLon = userLon + (lon * 5);
+
+      GeoPoint lesserGeoPoint = GeoPoint(lowerLat, lowerLon);
+      GeoPoint greaterGeoPoint = GeoPoint(upperLat, upperLon);
+
+      return FirebaseFirestore.instance
+          .collection('Places')
+          .where('location', isGreaterThan: lesserGeoPoint)
+          .where('location', isLessThan: greaterGeoPoint)
+          .get()
+          .then((snapshot) {
+        int numOfPlaces = snapshot.docs.length;
+        String id = snapshot.docs[Random().nextInt(numOfPlaces)].id;
+        print('updating');
+        user.update({
+          'weekly_place':
+              FirebaseFirestore.instance.collection('Places').doc(id),
+          'last_challenge_date': Timestamp.now(),
+        });
+        return FirebaseFirestore.instance.collection('Places').doc(id).get();
+      });
+    });
+  }
+}

+ 23 - 2
app/pubspec.lock

@@ -36,6 +36,27 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "1.1.0"
+  cloud_firestore:
+    dependency: "direct main"
+    description:
+      name: cloud_firestore
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "3.1.14"
+  cloud_firestore_platform_interface:
+    dependency: transitive
+    description:
+      name: cloud_firestore_platform_interface
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "5.5.5"
+  cloud_firestore_web:
+    dependency: transitive
+    description:
+      name: cloud_firestore_web
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.6.14"
   collection:
     dependency: transitive
     description:
@@ -70,14 +91,14 @@ packages:
       name: firebase_core_platform_interface
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "4.2.5"
+    version: "4.3.0"
   firebase_core_web:
     dependency: transitive
     description:
       name: firebase_core_web
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.6.2"
+    version: "1.6.3"
   flutter:
     dependency: "direct main"
     description: flutter

+ 1 - 0
app/pubspec.yaml

@@ -38,6 +38,7 @@ dependencies:
   http: ^0.13.4
   flutter_map: ^0.14.0
   flutter_map_location_marker: ^3.1.0
+  cloud_firestore: ^3.1.14
 
 dev_dependencies:
   flutter_test: