-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
lib/features/dashboard/presentation/bloc/dashboard_bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
lib/features/dashboard/presentation/bloc/dashboard_event.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
lib/features/dashboard/presentation/bloc/dashboard_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
lib/features/dashboard/presentation/view/dashboard_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |