Skip to content

Commit

Permalink
Merge pull request #2 from kirannonstop/master
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectAJ14 authored Sep 27, 2023
2 parents fd6fb9f + 08214c7 commit 937564c
Show file tree
Hide file tree
Showing 29 changed files with 942 additions and 363 deletions.
26 changes: 26 additions & 0 deletions lib/app/init/init_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import '../../ui/routes/route_constants.dart';
import '../../ui/routes/routes.dart';
import '../../ui/theme/theme.dart';
import 'init_dependencies.dart';

Future<void> initApp() async {
await initDependencies();
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return GetMaterialApp(
theme: theme,
builder: EasyLoading.init(),
getPages: appScreens(),
initialRoute: RouteConstants.splashScreen,
);
}
}
16 changes: 16 additions & 0 deletions lib/app/init/init_dependencies.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import '../../firebase_options.dart';
import '../../ui/utils/app_loader.dart';
import '../repo/repos.dart';
import '../services/services.dart';

Future<void> initDependencies() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await AppLoader.init();
initRepos();
initServices();
}
9 changes: 9 additions & 0 deletions lib/app/repo/firestore_repo/firestore_repo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import '../../../data/models/app_response.dart';
import '../../../data/models/user.dart';

abstract class FireStoreRepo {
addUser({
required User user,
});
Future<AppResponse> getAllTopScoredUsers();
}
54 changes: 54 additions & 0 deletions lib/app/repo/firestore_repo/firestore_repo_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import '../../../ui/utils/app_loader.dart';
import '../../../ui/utils/constant.dart';
import '../../../data/models/app_response.dart';
import '../../../data/models/user.dart';
import 'firestore_repo.dart';

class FireStoreRepoImpl extends FireStoreRepo {
CollectionReference users = FirebaseFirestore.instance.collection('users');
@override
addUser({
required User user,
}) {
// Call the user's CollectionReference to add a new user
users.doc(user.userId).set({
Constants.email: user.email,
Constants.displayName: user.displayName,
Constants.score: user.score,
}).catchError((error) => debugPrint('Error adding user $error'));
}

@override
Future<AppResponse> getAllTopScoredUsers() async {
try {
AppLoader.show();
List<User> userList = await users
//.where(Constants.score, isGreaterThan: 40)
.orderBy(Constants.score, descending: true)
.limit(10)
.get()
.then(
(value) => value.docs
.map(
(e) => User.fromFireStore(e.data() as Map<String, dynamic>),
)
.toList(),
);

return AppResponse.success(
id: 'getAllTopScoredUsers',
data: {Constants.userList: userList},
);
} catch (e, s) {
return AppResponse.error(
id: 'getAllTopScoredUsers',
error: e,
stackTrace: s,
);
} finally {
AppLoader.hide();
}
}
}
10 changes: 10 additions & 0 deletions lib/app/repo/repos.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:get/get.dart';

import 'firestore_repo/firestore_repo.dart';
import 'firestore_repo/firestore_repo_impl.dart';

initRepos() {
Get.put<FireStoreRepo>(FireStoreRepoImpl());
}

FireStoreRepo get fireStoreRepo => Get.find<FireStoreRepo>();
10 changes: 10 additions & 0 deletions lib/app/services/auth/auth_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../../../data/models/app_response.dart';

abstract class AuthService {
bool get isAuthenticated;
String get userId;
String get userEmail;
String get userName;
Future<AppResponse> signInWithGoogle();
Future<void> signOut();
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import '../../../data/models/app_response.dart';
import '../../../ui/utils/constant.dart';
import 'auth_service.dart';
import 'dart:developer' as developer;

import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
import 'package:get/get.dart';
import 'package:google_sign_in/google_sign_in.dart';

import '../../../ui/screens/sign_in/sign_in_screen.dart';
import '../../../ui/utils/app_loader.dart';
import '../../models/app_response.dart';

const noCredentialsWereFound = 'No credentials were found';

AuthService get auth => Get.find<AuthService>();

class AuthService {
class AuthServiceImpl extends AuthService {
final _firebaseAuth = firebase_auth.FirebaseAuth.instance;
final _googleSignIn = GoogleSignIn();

@override
bool get isAuthenticated => _firebaseAuth.currentUser != null;

String get userName =>
@override
String get userId => isAuthenticated ? _firebaseAuth.currentUser!.uid : '';
@override
String get userEmail =>
isAuthenticated ? _firebaseAuth.currentUser!.email ?? '' : '';

@override
String get userName =>
isAuthenticated ? _firebaseAuth.currentUser!.displayName ?? '' : '';
@override
Future<AppResponse> signInWithGoogle() async {
developer.log('signInWithGoogle');
try {
Expand Down Expand Up @@ -60,10 +61,11 @@ class AuthService {

return AppResponse.error(
id: 'signInWithGoogle',
message: noCredentialsWereFound,
message: Constants.noCredentialsWereFound,
);
}

@override
Future<void> signOut() async {
try {
AppLoader.show();
Expand Down
10 changes: 10 additions & 0 deletions lib/app/services/services.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:get/get.dart';

import 'auth/auth_service.dart';
import 'auth/auth_service_impl.dart';

initServices() {
Get.put<AuthService>(AuthServiceImpl());
}

AuthService get authService => Get.find<AuthService>();
2 changes: 1 addition & 1 deletion lib/data/models/app_response.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:developer' as developer;

import '../utils/methods.dart';
import '../../ui/utils/methods.dart';

/// Custom status code for the app.
enum AppStatusCode {
Expand Down
25 changes: 25 additions & 0 deletions lib/data/models/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:ns_utils/extensions/map.dart';

import '../../ui/utils/constant.dart';

class User {
final String userId;
final String displayName;
final String email;
final int score;

User({
required this.userId,
required this.displayName,
required this.email,
required this.score,
});
factory User.fromFireStore(Map<String, dynamic> json) {
return User(
userId: json.getString(Constants.userId),
displayName: json.getString(Constants.displayName),
email: json.getString(Constants.email),
score: json.getInt(Constants.score),
);
}
}
13 changes: 0 additions & 13 deletions lib/data/services/auth/sp_service.dart

This file was deleted.

38 changes: 3 additions & 35 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'app/init/init_app.dart';

import 'data/services/auth/auth_service.dart';
import 'data/services/auth/sp_service.dart';
import 'firebase_options.dart';
import 'ui/routes/route_constants.dart';
import 'ui/routes/routes.dart';
import 'ui/theme/theme.dart';
import 'ui/utils/app_loader.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await AppLoader.init();
await SPService.init();
Get.put(AuthService());
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return GetMaterialApp(
theme: theme,
builder: EasyLoading.init(),
getPages: appScreens(),
initialRoute: RouteConstants.splashScreen,
);
}
void main() {
initApp();
}
1 change: 1 addition & 0 deletions lib/ui/routes/route_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class RouteConstants {
static const String splashScreen = "/";
static const String signIn = "/sign_in";
static const String home = "/home";
static const String topUsers = "/top_users";
}
5 changes: 5 additions & 0 deletions lib/ui/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:get/get.dart';
import '../screens/home/home_screen.dart';
import '../screens/sign_in/sign_in_screen.dart';
import '../screens/splash/splash_screen.dart';
import '../screens/top_users/top_users_screen.dart';
import 'route_constants.dart';

List<GetPage<dynamic>> appScreens() => [
Expand All @@ -18,4 +19,8 @@ List<GetPage<dynamic>> appScreens() => [
name: RouteConstants.home,
page: () => const HomeScreen(),
),
GetPage(
name: RouteConstants.topUsers,
page: () => const TopUserScreen(),
),
];
22 changes: 21 additions & 1 deletion lib/ui/screens/home/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import 'package:confetti/confetti.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../app/repo/repos.dart';
import '../../../app/services/services.dart';
import '../../../data/models/subjects.dart';
import '../../../data/utils/methods.dart';
import '../../../data/models/user.dart';
import '../../utils/methods.dart';
import '../../routes/route_constants.dart';

const int totalScore = 100;
Expand Down Expand Up @@ -165,6 +168,23 @@ class HomeController extends GetxController {
return score.toTwoDigits();
}

String userWonGetScore() {
int score = _calculateScore();
fireStoreRepo.addUser(
user: User(
userId: authService.userId,
displayName: authService.userName,
score: score,
email: authService.userEmail,
),
);
return score.toTwoDigits();
}

void leaderBoardClick() {
Get.toNamed(RouteConstants.topUsers);
}

int _calculateScore() {
if (startTime == null) {
return 0;
Expand Down
Loading

0 comments on commit 937564c

Please sign in to comment.