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

Task List for Admin #91

Merged
merged 9 commits into from
Nov 25, 2023
8 changes: 6 additions & 2 deletions app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_fonts/google_fonts.dart';

import 'features/authentication/register/bloc/user_register_bloc.dart';
import 'features/main/presentation/bloc/main_cubit.dart';
import 'features/main/presentation/bloc/home_cubit.dart';
import 'features/onboarding/presentation/bloc/user_initialization_bloc.dart';
import 'features/quiz_exercise/presentation/bloc/quiz_exercise_cubit.dart';
import 'features/quiz_registration/presentation/bloc/quiz_registration_cubit.dart';
import 'features/quiz_result/presentation/bloc/quiz_result_cubit.dart';
import 'features/quiz_start/presentation/bloc/quiz_start_cubit.dart';
import 'features/task_detail/presentation/bloc/task_detail_cubit.dart';
import 'features/task_list/presentation/bloc/task_list_cubit.dart';
import 'services/di.dart';
import 'services/router_service.dart';

Expand All @@ -30,7 +32,9 @@ class App extends StatelessWidget {
OnboardingAuthEvent(),
),
),
BlocProvider(create: (context) => MainCubit()),
BlocProvider(create: (context) => HomeCubit()),
BlocProvider(create: (context) => TaskListCubit()),
BlocProvider(create: (context) => TaskDetailCubit()),
BlocProvider(create: (context) => QuizExerciseCubit()),
BlocProvider(create: (context) => QuizResultCubit()),
BlocProvider(create: (context) => QuizStartCubit()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class RegisteredUserModel {
final String school;
final String province;
final String bebrasBiro;
final bool isAdmin;

RegisteredUserModel({
required this.email,
Expand All @@ -13,16 +14,18 @@ class RegisteredUserModel {
required this.school,
required this.province,
required this.bebrasBiro,
required this.isAdmin,
});

factory RegisteredUserModel.fromJson(dynamic json) {
factory RegisteredUserModel.fromJson(Map<String, dynamic> json) {
return RegisteredUserModel(
email: json.data()['email'].toString(),
name: json.data()['name'].toString(),
birthDate: json.data()['birth_date'].toString(),
school: json.data()['school'].toString(),
province: json.data()['province'].toString(),
bebrasBiro: json.data()['bebras_biro'].toString(),
email: json['email'].toString(),
name: json['name'].toString(),
birthDate: json['birth_date'].toString(),
school: json['school'].toString(),
province: json['province'].toString(),
bebrasBiro: json['bebras_biro'].toString(),
isAdmin: json['is_admin'] != null && json['is_admin']! as bool,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../../../../../core/bases/widgets/layout/bebras_scaffold.dart';
import '../../../../../core/constants/assets.dart';
import '../../../../../core/theme/font_theme.dart';
import '../../../../../services/di.dart';
import '../../../../main/presentation/bloc/main_cubit.dart';
import '../../../../main/presentation/bloc/home_cubit.dart';
import '../../bloc/user_register_bloc.dart';
import '../../model/form_item.dart';
import '../widgets/biro_bebras_dropdown.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _RegisterPageState extends State<RegisterPage> {
),
);

context.read<MainCubit>().fetchUser();
context.read<HomeCubit>().fetchUser();

// add notification
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,10 @@ class RegisterUserRepository {
final result = await FirebaseFirestore.instance
.collection('registered_user')
.doc(userId)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
return documentSnapshot;
}
});
if (result != null) {
return RegisteredUserModel.fromJson(result);
.get();
final data = result.data();
if (result.exists && data != null) {
return RegisteredUserModel.fromJson(data);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:meta/meta.dart';

import '../../../../services/firebase_service.dart';
import '../../../authentication/register/model/registered_user.dart';

part 'main_state.dart';
part 'home_state.dart';

class MainCubit extends Cubit<MainState> {
MainCubit() : super(MainInitial());
class HomeCubit extends Cubit<HomeState> {
HomeCubit() : super(HomeInitial());

Future<void> fetchUser() async {
try {
Expand All @@ -16,13 +17,13 @@ class MainCubit extends Cubit<MainState> {
.doc(FirebaseService.auth().currentUser?.uid)
.get();
final userData = userSnapshot.data();
if (userData != null) {
emit(MainSuccess(userData));
if (userSnapshot.exists && userData != null) {
emit(HomeSuccess(RegisteredUserModel.fromJson(userData)));
} else {
throw Exception('User Data cannot be extracted');
}
} catch (e) {
emit(MainFailed(e.toString()));
emit(HomeFailed(e.toString()));
}
}
}
23 changes: 23 additions & 0 deletions app/lib/features/main/presentation/bloc/home_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
part of 'home_cubit.dart';

@immutable
abstract class HomeState {}

class HomeInitial extends HomeState {}

class HomeSuccess extends HomeState {
final RegisteredUserModel user;

HomeSuccess(
this.user,
);

List<Object> get props => [user];
}

class HomeFailed extends HomeState {
final String error;

HomeFailed(this.error);
List<Object> get props => [error];
}
28 changes: 0 additions & 28 deletions app/lib/features/main/presentation/bloc/main_state.dart

This file was deleted.

4 changes: 2 additions & 2 deletions app/lib/features/main/presentation/pages/_pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import '../../../../core/bases/widgets/atoms/button.dart';
import '../../../../core/bases/widgets/layout/bebras_scaffold.dart';
import '../../../../core/constants/assets.dart';
import '../../../../core/theme/font_theme.dart';
import '../bloc/main_cubit.dart';
import '../bloc/home_cubit.dart';

part 'main_page.dart';
part 'home_page.dart';
part 'setting_page.dart';
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
part of '_pages.dart';

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

@override
State<MainPage> createState() => _MainPageState();
State<HomePage> createState() => _HomePageState();
}

class Course {
Expand All @@ -14,10 +14,10 @@ class Course {
Course(this.title, this.description);
}

class _MainPageState extends State<MainPage> {
class _HomePageState extends State<HomePage> {
@override
void initState() {
context.read<MainCubit>().fetchUser();
context.read<HomeCubit>().fetchUser();
super.initState();
}

Expand All @@ -37,23 +37,19 @@ class _MainPageState extends State<MainPage> {
const SizedBox(
height: 40,
),
BlocBuilder<MainCubit, MainState>(builder: (context, state) {
if (state is MainSuccess) {
return Align(
alignment: Alignment.centerLeft,
child: RichText(
text: TextSpan(
text: 'Selamat Datang\n',
style: FontTheme.blackTitle(),
children: <TextSpan>[
TextSpan(
BlocBuilder<HomeCubit, HomeState>(builder: (context, state) {
if (state is HomeSuccess) {
return RichText(
text: TextSpan(
text: 'Selamat Datang\n',
style: FontTheme.blackTitle(),
children: <TextSpan>[
TextSpan(
text: toBeginningOfSentenceCase(
'${state.userData['name']}!',
'${state.user.name}!',
),
style: FontTheme.blackTitleBold(),
),
],
),
style: FontTheme.blackTitleBold()),
],
),
);
}
Expand Down
31 changes: 25 additions & 6 deletions app/lib/features/main/presentation/pages/setting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ class SettingPage extends StatefulWidget {
}

class _SettingPageState extends State<SettingPage> {
final nama = 'dummy';

@override
Widget build(BuildContext context) {
// final size = MediaQuery.of(context).size;

return BebrasScaffold(
body: SingleChildScrollView(
child: Stack(
Expand Down Expand Up @@ -89,8 +85,31 @@ class _SettingPageState extends State<SettingPage> {
customTextColor: Colors.white,
text: 'Bantu Donasi 💌',
),
SizedBox(
height: MediaQuery.of(context).size.height - 695,
BlocBuilder<HomeCubit, HomeState>(
builder: (context, state) {
if (state is HomeSuccess && state.user.isAdmin) {
return Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height - 625,
),
Button(
onTap: () {
context.push('/task_list');
},
buttonType: ButtonType.primary,
text: 'Lihat Bebras Task',
),
const SizedBox(
height: 25,
),
],
);
}
return SizedBox(
height: MediaQuery.of(context).size.height - 695,
);
},
),
Button(
onTap: () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
currentProblemIndex = 0;

// Fetch all quiz data for it to be available when offline
await quizExerciseRepository.getListQuizExercise(problemIdList);
await quizExerciseRepository
.getListQuizExerciseByTaskIdList(problemIdList);

currentProblem =
await quizExerciseRepository.getQuizExercise(problemIdList.first);
Expand All @@ -91,25 +92,7 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
throw Exception('Duration for selected Challenge Group not found');
}
remainingDuration = (duration * 60.0).toInt();
timer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (state is! QuizExerciseShow) {
timer.cancel();
} else if (remainingDuration > 0) {
remainingDuration--;
emit(
QuizExerciseShow(
quiz: quiz,
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
shortAnswer: shortAnswer,
),
);
} else {
timer.cancel();
finishExerciseTimeUp();
}
});
timer = Timer.periodic(const Duration(seconds: 1), timerCallback);
emit(
QuizExerciseShow(
quiz: quiz,
Expand Down Expand Up @@ -247,6 +230,44 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
quizParticipantId!, attempt);
}

void timerCallback(Timer timer) {
if (state is! QuizExerciseShow) {
timer.cancel();
} else if (remainingDuration > 0) {
remainingDuration--;
emit(
QuizExerciseShow(
quiz: quiz,
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
shortAnswer: shortAnswer,
),
);
} else {
timer.cancel();
finishExerciseTimeUp();
}
}

Future<void> pause() async {
timer?.cancel();
emit(QuizExercisePaused());
}

Future<void> resume() async {
timer = Timer.periodic(const Duration(seconds: 1), timerCallback);
emit(
QuizExerciseShow(
quiz: quiz,
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
shortAnswer: shortAnswer,
),
);
}

@override
Future<void> close() {
timer?.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class QuizExerciseFinished extends QuizExerciseState {
List<Object> get props => [quizParticipantId];
}

class QuizExercisePaused extends QuizExerciseState {}

class QuizExerciseFailed extends QuizExerciseState {
final String error;

Expand Down
Loading
Loading