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

Feature/nas 13 code cleanup #28

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void main() async {
),
// Add more BlocProvider.value as needed
],
child: MyApp(),
child: const MyApp(),
));
final log = Logger("Main");
if (firstRun) log.info("First Run detected.");
Expand All @@ -52,6 +52,8 @@ void main() async {
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();
}
Expand Down Expand Up @@ -88,7 +90,6 @@ class _MyAppState extends State<MyApp> {
home: Scaffold(
body: BlocBuilder<AuthCubit, AuthState>(
builder: (context, state) {
final log = Logger('MainAccount');
if (firstRun) {
dev.log("First Run");
const cacheIntroImages = [
Expand Down
11 changes: 3 additions & 8 deletions lib/src/config/router/app_router.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import 'package:Nott_A_Student/src/config/router/no_animation_material_page_route.dart';
import 'package:Nott_A_Student/src/features/timetable/presentation/views/TimeTable.dart';
import 'package:Nott_A_Student/src/features/timetable/presentation/views/dayView.dart';
import 'package:Nott_A_Student/src/features/timetable/presentation/views/day_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:Nott_A_Student/src/features/auth/domain/auth_repo.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/cubit/login_cubit.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/cubit/signup_cubit.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/view/account-view.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/view/account_view.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/view/intro.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/view/login.dart';
import 'package:Nott_A_Student/src/features/auth/presentation/view/signup.dart';
import 'package:Nott_A_Student/src/features/bus/presentation/views/bus.dart';
import 'package:Nott_A_Student/src/features/dashboard/presentation/cubit/news_type_cubit.dart';
import 'package:Nott_A_Student/src/features/dashboard/presentation/views/dashboard.dart';
import 'package:Nott_A_Student/src/features/timetable/presentation/views/TimeTable.dart';
import 'package:Nott_A_Student/src/features/timetable/presentation/views/dayView.dart';
import 'package:Nott_A_Student/src/features/bus/presentation/views/search_result.dart';

class AppRouter {
final LoginCubit _loginCubit = LoginCubit(authRepo: AuthRepository());
final SignupCubit _signupCubit = SignupCubit(authRepo: AuthRepository());
final NewsTypeCubit _newsTypeCubit = NewsTypeCubit();
Route onGenerateRoute(RouteSettings settings) {
//final Object? key = settings.arguments;
switch (settings.name) {
Expand Down Expand Up @@ -52,7 +47,7 @@ class AppRouter {
);
case '/timetable':
return NoAnimationMaterialPageRoute(
builder: (_) => const dayView(),
builder: (_) => const DayView(),
);
case '/account':
return NoAnimationMaterialPageRoute(
Expand Down
1 change: 0 additions & 1 deletion lib/src/config/themes/app_theme.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class AppTheme {
static ThemeData style() {
Expand Down
8 changes: 0 additions & 8 deletions lib/src/features/auth/domain/auth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:Nott_A_Student/src/features/auth/domain/auth_status.dart';
import 'package:Nott_A_Student/src/features/auth/domain/session.dart';
import 'package:logging/logging.dart';
import 'dart:developer';
part 'auth_state.dart';

Expand All @@ -15,7 +14,6 @@ class AuthCubit extends Cubit<AuthState> {
: super(const AuthState(
authStatus: AuthInitial(),
));
// final log = Logger('AuthCubit');

final client = Client()
.setEndpoint('https://cloud.appwrite.io/v1')
Expand Down Expand Up @@ -59,12 +57,6 @@ class AuthCubit extends Cubit<AuthState> {
log("Auto Login Failed.");
}
} else {
// Show splash screen for just 3 seconds
// final timer = Timer(const Duration(seconds: 1), () {
// emit(state.copyWith(
// authStatus: const AuthUnauthorized(),
// ));
// });
emit(state.copyWith(
authStatus: const AuthUnauthorized(),
));
Expand Down
66 changes: 33 additions & 33 deletions lib/src/features/auth/domain/auth_repo.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart';
import 'package:Nott_A_Student/src/features/auth/domain/auth_cubit.dart';
import 'package:Nott_A_Student/src/features/auth/domain/session.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';

class AuthRepository {
final dialogKey = GlobalKey();
var logger = Logger("AuthRepo");

Future<String> login({
required String email,
Expand All @@ -23,42 +22,43 @@ class AuthRepository {

showLoadingDialogBar(context, "Logging you in");

print('attempting login');
print(email);
print(password);

var account = Account(client);
final session =
await account.createEmailSession(email: email, password: password);
saveData(session);

if (session.current) {
if (year != null && school != null && program != null) {
print("Update preferences start");

var userPrefs = {
'Year': year,
'School': school,
'Program': program,
};

// savePrefs(userPrefs);
try {
final session = await account.createEmailPasswordSession(
email: email, password: password);
saveData(session);

// Use async/await for cleaner asynchronous code
try {
await account.updatePrefs(prefs: userPrefs);
var prefs = await account.getPrefs() as Preferences;
print(prefs.data);
print("Preferences updated successfully in login function");
} catch (error) {
print(error);
print('Preference update error');
if (session.userId.isNotEmpty) {
if (year != null && school != null && program != null) {
setUserPref(account, year, school, program);
}
Navigator.of(dialogKey.currentContext!).pop();
return session.userId;
} else {
throw Exception("Login failed: Invalid session");
}
} catch (error) {
logger.info(error.toString());
logger.info("Session empty or login failed");
Navigator.of(dialogKey.currentContext!).pop();
return session.userId;
} else {
throw Exception('Login failed');
throw Exception("Login failed: $error");
}
}

Future<void> setUserPref(Account account, year, school, program) async {
try {
await account.updatePrefs(prefs: {
'Year': year,
'School': school,
'Program': program,
});
var prefs = await account.getPrefs();
logger.info(Level.INFO, prefs.data);
logger.info(
Level.INFO, "Preferences updated successfully in login function");
} catch (error) {
logger.info(Level.SHOUT, "Preferences Update Error: ${error.toString()}");
}
}

Expand Down
14 changes: 3 additions & 11 deletions lib/src/features/auth/presentation/cubit/account_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:developer';

import 'package:appwrite/appwrite.dart';

import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:Nott_A_Student/src/features/auth/domain/session.dart';
import 'package:shared_preferences/shared_preferences.dart';

part 'account_state.dart';

Expand All @@ -18,14 +18,6 @@ class AccountCubit extends Cubit<AccountState> {
Account account = Account(client);
var response = await account.get();

/* var userPrefs = {
'Name': response.name,
'Email': response.email,
'Year': response.prefs.data['Year'],
'School': response.prefs.data['School'],
'Program': response.prefs.data['Program'],
}; */

emit(state.copyWith(
name: response.name,
email: response.email,
Expand All @@ -35,7 +27,7 @@ class AccountCubit extends Cubit<AccountState> {
));
// savePrefs(userPrefs);
} catch (error) {
print('Error while fetching account info: $error');
log('Error while fetching account info: $error');
// Handle the error, e.g., show an error message to the user.
}
}
Expand Down
18 changes: 8 additions & 10 deletions lib/src/features/auth/presentation/cubit/login_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ class LoginCubit extends Cubit<LoginState> {
LoginCubit({required this.authRepo}) : super(LoginState());

void onUserNameChanged(String email) {
print(email);
if (email != state.email) {
emit(state.copyWith(email: email));
}
}

void onPasswordChanged(String password) {
print(password);
if (password != state.password) {
emit(state.copyWith(password: password));
}
Expand All @@ -29,16 +27,16 @@ class LoginCubit extends Cubit<LoginState> {
emit(LoginFailed(errorMessage: "Username or Password is empty"));
} else {
try {
final userId = authRepo.login(
email: state.email, password: state.password, context: ctx);
try {
final userId = await authRepo.login(
email: state.email, password: state.password, context: ctx);

userId.then((value) {
if (value.isNotEmpty) {
emit(LoginSuccess(userId: value));
} else {
emit(LoginFailed(errorMessage: "Username or Password Incorrect"));
if (userId.isNotEmpty) {
emit(LoginSuccess(userId: userId));
}
});
} catch (e) {
emit(LoginFailed(errorMessage: e.toString()));
}
} catch (e) {
emit(LoginFailed(errorMessage: e.toString()));
}
Expand Down
Loading