Skip to content

Commit

Permalink
feat: add dashboard bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jhionan committed Jul 3, 2023
1 parent 7a36bda commit 5d90185
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/core/domain/models/action_statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class TotalActionResults {
required this.completedRounds,
});

final double totalShown;
final int totalShown;
final double averangeCorrect;
final double completedRounds;
final int completedRounds;
}
50 changes: 25 additions & 25 deletions lib/core/navigation/abc_router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions lib/features/dashboard/presentation/bloc/dashboard_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'dart:async';

import 'package:abc_fun/core/db/schemas/game_session_dto.dart';
import 'package:abc_fun/features/game/domain/game_session_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:meta/meta.dart';

part 'dashboard_event.dart';
part 'dashboard_state.dart';

class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
DashboardBloc({required this.gameSessionRepository}) : super(DashboardLoadingState()) {
on<DashboardEvent>(_handleEvent);
_init();
}

final GameSessionRepository gameSessionRepository;

FutureOr<void> _handleEvent(DashboardEvent event, Emitter<DashboardState> emit) async {
if(event is DashboardEmptyEvent) {
emit(DasboardEmptyState());
return;
}
if(event is DashboardSessionLoaded) {
emit(DashboardLoadedState(
totalSessions: event.totalSessions,
totalStages: event.totalStages,
totalMoves: event.totalMoves,
totalWrong: event.totalWrong,
totalCorrect: event.totalCorrect,
));
return;
}
}

Future<void> _init() async {
List<GameSessionDto>? sessions = await gameSessionRepository.getAll();
if (sessions == null) {
add(DashboardEmptyEvent());
}

sessions as List<GameSessionDto>;

int totalSessions = sessions.length;
int totalStages = sessions.fold<int>(0, (previousValue, element) => previousValue + element.totalStages);
int totalMoves = sessions.fold<int>(0, (previousValue, element) => previousValue + element.totalMoves);
int totalWrong = sessions.fold<int>(0, (previousValue, element) => previousValue + element.totalWrongAnswers);
int totalCorrect = totalMoves - totalWrong;
add(DashboardSessionLoaded(
totalSessions: totalSessions,
totalStages: totalStages,
totalMoves: totalMoves,
totalWrong: totalWrong,
totalCorrect: totalCorrect,
));
}
}
22 changes: 22 additions & 0 deletions lib/features/dashboard/presentation/bloc/dashboard_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
part of 'dashboard_bloc.dart';

@immutable
abstract class DashboardEvent {}

class DashboardEmptyEvent extends DashboardEvent {}

class DashboardSessionLoaded extends DashboardEvent {
DashboardSessionLoaded({
required this.totalSessions,
required this.totalStages,
required this.totalMoves,
required this.totalWrong,
required this.totalCorrect,
});

final int totalSessions;
final int totalStages;
final int totalMoves;
final int totalWrong;
final int totalCorrect;
}
24 changes: 24 additions & 0 deletions lib/features/dashboard/presentation/bloc/dashboard_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of 'dashboard_bloc.dart';

@immutable
abstract class DashboardState {}

class DashboardLoadingState extends DashboardState {}

class DasboardEmptyState extends DashboardState {}

class DashboardLoadedState extends DashboardState {
final int totalSessions;
final int totalStages;
final int totalMoves;
final int totalWrong;
final int totalCorrect;

DashboardLoadedState({
required this.totalSessions,
required this.totalStages,
required this.totalMoves,
required this.totalWrong,
required this.totalCorrect,
});
}
3 changes: 0 additions & 3 deletions lib/features/dashboard/presentation/view/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import 'package:abc_fun/core/domain/view/widgets/adaptative_screen_builder.dart'
import 'package:abc_fun/features/dashboard/presentation/view/widgets/play_card.dart';
import 'package:abc_fun/features/dashboard/presentation/view/widgets/results_widget.dart';
import 'package:abc_fun/core/domain/view/widgets/abc_divider.dart';
import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

@RoutePage()
class DashboardPage extends StatelessWidget {
const DashboardPage({super.key});

Expand Down
21 changes: 21 additions & 0 deletions lib/features/dashboard/presentation/view/dashboard_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:abc_fun/core/providers/providers.dart';
import 'package:abc_fun/features/dashboard/presentation/bloc/dashboard_bloc.dart';
import 'package:abc_fun/features/dashboard/presentation/view/dashboard_page.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

@RoutePage()
class DashboardProvider extends StatelessWidget {
const DashboardProvider({super.key});

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => DashboardBloc(
gameSessionRepository: provider.read(Providers.gameSessionRepository),
),
child:const DashboardPage(),
);
}
}

0 comments on commit 5d90185

Please sign in to comment.