Skip to content

Commit

Permalink
Merge pull request #80 from ia-toki/fix-shuffle
Browse files Browse the repository at this point in the history
Fix shuffle and main page
  • Loading branch information
gmochid authored Nov 11, 2023
2 parents 4275ef9 + f43e188 commit 3dbe2fb
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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/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';
Expand All @@ -29,6 +30,7 @@ class App extends StatelessWidget {
OnboardingAuthEvent(),
),
),
BlocProvider(create: (context) => MainCubit()),
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 @@ -6,6 +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 '../../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,6 +45,8 @@ class _RegisterPageState extends State<RegisterPage> {
),
);

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

// add notification
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
Expand Down
28 changes: 28 additions & 0 deletions app/lib/features/main/presentation/bloc/main_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:bloc/bloc.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:meta/meta.dart';

import '../../../../services/firebase_service.dart';

part 'main_state.dart';

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

Future<void> fetchUser() async {
try {
final userSnapshot = await FirebaseFirestore.instance
.collection('registered_user')
.doc(FirebaseService.auth().currentUser?.uid)
.get();
final userData = userSnapshot.data();
if (userData != null) {
emit(MainSuccess(userData));
} else {
throw Exception('User Data cannot be extracted');
}
} catch (e) {
emit(MainFailed(e.toString()));
}
}
}
25 changes: 25 additions & 0 deletions app/lib/features/main/presentation/bloc/main_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of 'main_cubit.dart';

@immutable
abstract class MainState {}

class MainInitial extends MainState {}

class MainSuccess extends MainState {
final Map<String, dynamic> userData;

MainSuccess(
this.userData,
);

@override
List<Object> get props => [userData];
}

class MainFailed extends MainState {
final String error;

MainFailed(this.error);
@override
List<Object> get props => [error];
}
4 changes: 2 additions & 2 deletions app/lib/features/main/presentation/pages/_pages.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:intl/intl.dart';
Expand All @@ -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 '../../../../services/firebase_service.dart';
import '../bloc/main_cubit.dart';

part 'main_page.dart';
part 'setting_page.dart';
22 changes: 11 additions & 11 deletions app/lib/features/main/presentation/pages/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class Course {
}

class _MainPageState extends State<MainPage> {
final registeredUserSnapshot = FirebaseFirestore.instance
.collection('registered_user')
.doc(FirebaseService.auth().currentUser?.uid)
.get();
@override
void initState() {
context.read<MainCubit>().fetchUser();
super.initState();
}

@override
Widget build(BuildContext context) {
Expand All @@ -37,26 +38,25 @@ class _MainPageState extends State<MainPage> {
const SizedBox(
height: 40,
),
FutureBuilder(
future: registeredUserSnapshot,
builder: (context, AsyncSnapshot<dynamic> snapshot) {
final useData = snapshot.data?.data();
BlocBuilder<MainCubit, MainState>(builder: (context, state) {
if (state is MainSuccess) {
return RichText(
text: TextSpan(
text: 'Selamat Datang\n',
style: FontTheme.blackTitle(),
children: <TextSpan>[
TextSpan(
text: toBeginningOfSentenceCase(
'${useData?['name']}!',
'${state.userData['name']}!',
),
style: FontTheme.blackTitleBold(),
),
],
),
);
},
),
}
return Container();
}),
const SizedBox(
height: 30,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
'Task set for `${participation.challenge_group}` is empty');
}

// Shuffle Problem List
problemIdList.shuffle();

// TODO(someone): fix the check logic later
// if (weeklyQuizParticipant.attempts.isEmpty) {
attempt = QuizExerciseAttempt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class _QuizExercisePageState extends State<QuizExercisePage> {
return state is QuizExerciseShow;
}, builder: (context, state) {
if (state is QuizExerciseShow) {
// Shuffle Quiz Options
state.quizExercise.question.options?.shuffle();
return Scaffold(
backgroundColor: Colors.transparent,
body: AlertDialog(
Expand Down

0 comments on commit 3dbe2fb

Please sign in to comment.