Răsfoiți Sursa

Ranking with fixed bugs

iwa-42 3 ani în urmă
părinte
comite
1fb110bf8e

+ 0 - 4
app/android/app/build.gradle

@@ -24,7 +24,6 @@ if (flutterVersionName == null) {
 apply plugin: 'com.android.application'
 apply plugin: 'kotlin-android'
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
-apply plugin: 'com.google.gms.google-services'
 
 android {
     compileSdkVersion 31
@@ -48,7 +47,6 @@ android {
         minSdkVersion 23
         multiDexEnabled true
         targetSdkVersion 31
-        minSdkVersion 19
         versionCode flutterVersionCode.toInteger()
         versionName flutterVersionName
     }
@@ -68,6 +66,4 @@ flutter {
 
 dependencies {
     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
-    implementation platform('com.google.firebase:firebase-bom:30.0.0')
-    implementation 'com.google.firebase:firebase-analytics'
 }

+ 0 - 1
app/android/build.gradle

@@ -8,7 +8,6 @@ buildscript {
     dependencies {
         classpath 'com.android.tools.build:gradle:4.1.0'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-        classpath 'com.google.gms:google-services:4.3.10'
     }
 }
 

+ 69 - 0
app/lib/currentUser.dart

@@ -0,0 +1,69 @@
+import 'package:flutter/cupertino.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/widgets.dart';
+import 'Services/AuthServiec.dart';
+
+class CurrentUser extends StatefulWidget {
+  const CurrentUser( {
+    required this.name, Key? key}):super(key: key);
+  final String name;
+  @override
+  State<CurrentUser> createState() => _CurrentUserState();
+}
+
+class _CurrentUserState extends State<CurrentUser> {
+  final AuthenticationServices _auth = AuthenticationServices();
+  late dynamic user = _auth.getCurrentUser();
+
+  @override
+  Widget build(BuildContext context) {
+    return FutureBuilder<dynamic>(
+      future: user,
+      builder: (context, snapshot) {
+        if(snapshot.connectionState == ConnectionState.waiting){
+          return const Text('Wait a moment');
+        }
+        else if(snapshot.connectionState == ConnectionState.done) {
+          dynamic user2 = snapshot.data.docs[0];
+          // DatabaseManager manager = DatabaseManager(widget.name);
+           Stream<int> place = _auth.getPlace(user2);
+          return Row(
+            children: [
+              SizedBox(
+                height: 100,
+                width: 55,
+              ),
+              SizedBox(
+                height: 100,
+                width: 50,
+              child: StreamBuilder(
+                stream: place,
+                builder: (context, snapshot){
+                  return Text(
+                      "${snapshot.data}");
+                },
+              ),
+              ),
+              SizedBox(
+                height: 100,
+                width: 135,
+                child: Text(user2["shared_id"]),
+              ),
+              SizedBox(
+                height: 100,
+                width: 60,
+                child: Text(user2["name"]),
+              ),
+              SizedBox(
+                height: 100,
+                width: 50,
+                child: Text("${user2[widget.name]}"),
+              ),
+            ],
+          );
+        }
+        return const Text('Something went wrong');
+      },
+    );
+  }
+}

+ 0 - 38
app/lib/database.dart

@@ -1,38 +0,0 @@
-import 'package:cloud_firestore/cloud_firestore.dart';
-import 'models/user.dart';
-
-class DatabaseManager {
-  CollectionReference usersList =
-      FirebaseFirestore.instance.collection("Users");
-
-  // Get stream with users ordered by total points from firestore
-  Stream<List> getUsersList() async* {
-    List<dynamic> users = [];
-    try {
-      await usersList.orderBy("total_points", descending: true).snapshots().
-        listen((snap){
-        snap.docs.forEach((element){
-        users.add(element.data());
-        });
-      });
-      yield users;
-    } catch(e){
-      print(e.toString());
-      yield [];
-    }
-  }
-  //It is not necessary.At least right now
-  // List<User> _userListFromSnapshot(QuerySnapshot snapshot){
-  //   return snapshot.docs.map((doc){
-  //     return User(
-  //       shared_id: doc.get('shared_id'),
-  //       name: doc.get('name'),
-  //       total_points: doc.get('total_ponts'),
-  //     );
-  //   }).toList();
-  // }
-  //
-  //  dynamic get users {
-  //   return usersList.orderBy("total_points", descending: true).snapshots();
-  // }
-}

+ 3 - 6
app/lib/main.dart

@@ -5,6 +5,7 @@ import 'firebase_options.dart';
 import 'push_notifications_initializer.dart';
 import 'package:physigo/logIn.dart';
 import 'package:physigo/profilePage.dart';
+import 'package:physigo/widgets/rankingNavigation.dart';
 import 'package:physigo/SignupPage.dart';
 import 'package:physigo/mainPage.dart';
 import 'package:physigo/welcomeScreen.dart';
@@ -30,16 +31,12 @@ class PhysiGo extends StatelessWidget {
       ),
       initialRoute: '/',
       routes: {
-        '/': (context) => MyHomePage(
-              title: '',
-            ),
+        '/': (context) => MyHomePage(title: ''),
         '/register': (context) => SignupPage(),
         //'/register2': (context) => RegisterScreen2(title: ''),
         //'/register3': (context) => RegisterScreen3(title: ''),
         '/login': (context) => LogIn(title: ''),
-        '/profilePage': (context) => ProfilePage(
-              title: '',
-            ),
+        '/profilePage': (context) => ProfilePage(title: ''),
         '/mainPage': (context) => MainPage()
       },
       //home: const MyHomePage(title: 'Flutter Demo Home Page'),

+ 75 - 43
app/lib/widgets/datatable.dart

@@ -1,96 +1,128 @@
+import 'dart:async';
+
 import 'package:cloud_firestore/cloud_firestore.dart';
 import 'package:flutter/material.dart';
-import 'package:physigo/database.dart';
-import '../models/user.dart';
 
 class MyDataTable extends StatefulWidget {
-  const MyDataTable({Key? key}) : super(key: key);
 
+  const MyDataTable( {
+    required this.name, Key? key}):super(key: key);
+  final String name;
   @override
   State<MyDataTable> createState() => _MyDataTableState();
 }
 
 class _MyDataTableState extends State<MyDataTable> {
-
-
+  CollectionReference usersList =
+  FirebaseFirestore.instance.collection("Users");
   @override
   Widget build(BuildContext context) {
+    Future<List> userList = getUsersList();
 
-    return  Scaffold(
-    body: Center(
-      child: StreamBuilder<dynamic>(
-        stream: DatabaseManager().getUsersList(),
-        builder: (context, snapshot) {
-          if(snapshot.connectionState == ConnectionState.waiting){
-            return const Text('Wait a moment');
-          }
-          else if(snapshot.hasData) {
-            return buildDataTable(snapshot.data);
-          }
-          return const Text("Something went wrong");
-          },
-    ),
-    ),
-    );
+    return  Container(
+      height: 450,
+      alignment: const Alignment(0,-1),
+      child: Scrollbar(
+        child:SingleChildScrollView(
+            child: FutureBuilder<dynamic>(
+              future: userList,
+              builder: (context, snapshot) {
+                if(snapshot.connectionState == ConnectionState.waiting){
+                  return const Text('Wait a moment');
+                }
+                if(snapshot.connectionState == ConnectionState.done) {
+                  return buildDataTable(snapshot.data, widget.name);
+                }
+                if(snapshot.hasError) {
+                  return const Text('Something went wrong');
+                }
+                return const Text("  ");
+                },
+          ),
+        ),
+      ),
+      );
   }
   @override
   void initState() {
     super.initState();
   }
 
-  // fetchUsers() async{
-  //     dynamic users = await DatabaseManager().getUsersList();
-  //
-  //     if(users == null){
-  //       print("Wrong");
-  //     }
-  //     else{
-  //       setState(() {
-  //         userList = users;
-  //       });
-  //     }
+
+  Future<List> getUsersList() async {
+    var querySnapshot = await usersList.orderBy(widget.name, descending: true).get();
+      return querySnapshot.docs;
+  }
   }
 
-  Widget buildDataTable(snapshotData) {
-    final columns = ['ID', 'Name', 'points'];
+  Widget buildDataTable(snapshotData, String name) {
+    final columns = ['Place', 'ID', 'Name', 'points'];
 
     return DataTable(
+      columnSpacing: 10.0,
       columns: getColumns(columns),
-      rows: getRows(snapshotData),
+      rows: getRows(snapshotData, name),
     );
   }
-  // Columns of ranking
   List<DataColumn> getColumns(List<String> columns) {
     return columns.map((String column) {
       return DataColumn(
+
         label: Text(column),
       );
     }).toList();
   }
 
-  //Rows of ranking(id, name, total_points)
-  List<DataRow> getRows(dynamic snapshotData) {
+  List<DataRow> getRows(dynamic snapshotData, String name) {
     List<DataRow> rows = [];
-
-    snapshotData.forEach((user) {
+    int length = snapshotData.length;
+    for(int i = 0; i < length; i++){
+      dynamic user = snapshotData[i];
+      bool anonymous = user["anonymous"];
+      if (anonymous) {
+        rows.add(
+            DataRow(
+                cells: [
+                  DataCell(
+                      Text("${i+1}")
+                  ),
+                  const DataCell(
+                    Text("anonym"),
+                  ),
+                  const DataCell(
+                    Text("name"),
+                  ),
+                  DataCell(
+                    Text("${user[name]}"),
+                  ),
+                ]
+            )
+        );
+      }
+      else {
         rows.add(
             DataRow(
                 cells: [
+                  DataCell(
+                      Text("${i+1}")
+                  ),
                   DataCell(
                     Text("${user["shared_id"]}"),
                   ),
                   DataCell(
-                    Text(user["name"],)
+                      Text(user["name"]),
                   ),
                   DataCell(
-                    Text("${user["total_points"]}"),
+                    Text("${user[name]}"),
                   ),
                 ]
             )
         );
-      });
+      }
+    }
     return rows;
   }
 
 
 
+

+ 77 - 0
app/lib/widgets/rankingNavigation.dart

@@ -0,0 +1,77 @@
+import 'package:flutter/cupertino.dart';
+import 'package:flutter/material.dart';
+import '../currentUser.dart';
+import 'datatable.dart';
+
+
+class RankingNavigation extends StatefulWidget {
+  const RankingNavigation({Key? key}) : super(key: key);
+
+  @override
+  State<RankingNavigation> createState() => _RankingNavigationState();
+}
+
+class _RankingNavigationState extends State<RankingNavigation> {
+  int _selectedIndex = 0;
+  static const List<Widget> _widgetOptions = <Widget>[
+    MyDataTable(name: 'daily_points'),
+    MyDataTable(name: 'weekly_points'),
+    MyDataTable(name: 'total_points'),
+    ];
+  static const List<Widget> _widgetOptions2 = <Widget>[
+    CurrentUser(name: 'daily_points'),
+    CurrentUser(name: 'weekly_points'),
+    CurrentUser(name: 'total_points'),
+  ];
+
+  void _onItemTapped(int index) {
+    setState(() {
+      _selectedIndex = index;
+    });
+  }
+
+
+  @override
+  Widget build(BuildContext context) {
+
+    return Scaffold(
+      appBar: AppBar(
+        toolbarHeight: 50,
+        title: const Text('Ranking'),
+      ),
+      body: SafeArea(
+        child: Column(
+        children: [
+          SizedBox(
+            height: 350,
+          child:_widgetOptions.elementAt(_selectedIndex),
+        ),
+        Container(
+          child:_widgetOptions2.elementAt(_selectedIndex),
+        )
+      ],
+      )
+      ),
+
+      bottomNavigationBar: BottomNavigationBar(
+        items: const <BottomNavigationBarItem>[
+          BottomNavigationBarItem(
+            icon: Icon(Icons.event),
+            label: 'Daily',
+          ),
+          BottomNavigationBarItem(
+            icon: Icon(Icons.event),
+            label: 'Weekly',
+          ),
+          BottomNavigationBarItem(
+            icon: Icon(Icons.event),
+            label: 'Monthly',
+          ),
+        ],
+        currentIndex: _selectedIndex,
+        selectedItemColor: Colors.amber[800],
+        onTap: _onItemTapped,
+      ),
+    );
+  }
+}