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

Refactor: Migra QuizStartPage de dialog para página #397

Open
wants to merge 4 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import 'package:dartz/dartz.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:mobx/mobx.dart';

import '../../../../core/error/failures.dart';
import '../../../../core/extension/mobx.dart';
import '../../../appstate/domain/entities/app_state_entity.dart';
import '../../../authentication/presentation/shared/map_failure_message.dart';
import '../../../authentication/presentation/shared/page_progress_indicator.dart';
import '../../domain/start_quiz.dart';
import 'quiz_start_state.dart';

class QuizStartController {
QuizStartController({
part 'quiz_start_controller.g.dart';

typedef QuizSessionResult = Either<Failure, QuizSessionEntity>;

class QuizStartController = _QuizStartControllerBase with _$QuizStartController;

abstract class _QuizStartControllerBase with Store, MapFailureMessage {
_QuizStartControllerBase({
required String? sessionId,
required StartQuizUseCase startQuiz,
}) : _sessionId = sessionId,
Expand All @@ -15,14 +26,27 @@ class QuizStartController {
final String? _sessionId;
final StartQuizUseCase _startQuiz;

Future<void> start() async {
final result = await _startQuiz(_sessionId);
@observable
QuizStartState state = const QuizStartState.initial();

@computed
PageProgressState get progressState => _pageProgress.state;

@observable
ObservableFuture<QuizSessionResult>? _pageProgress;

void load() async {
if (progressState == PageProgressState.loading) return;

_pageProgress = ObservableFuture(_startQuiz(_sessionId));
final result = await _pageProgress!;

result.fold(_handleFailure, _handleSuccess);
}

void _handleFailure(Failure failure) {
Modular.to.pop(left(failure));
final message = mapFailureMessage(failure);
state = QuizStartState.error(message);
}

void _handleSuccess(QuizSessionEntity session) {
Expand Down

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

Original file line number Diff line number Diff line change
@@ -1,26 +1,67 @@
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter_mobx/flutter_mobx.dart';

import '../../../../shared/design_system/colors.dart';
import '../../../authentication/presentation/shared/page_progress_indicator.dart';
import '../../../support_center/presentation/pages/support_center_general_error.dart';
import 'quiz_start_controller.dart';

class QuizStartPage extends StatefulWidget {
const QuizStartPage({Key? key}) : super(key: key);
const QuizStartPage({
Key? key,
required this.controller,
}) : super(key: key);

final QuizStartController controller;

@override
State<QuizStartPage> createState() => _QuizStartPageState();
}

class _QuizStartPageState
extends ModularState<QuizStartPage, QuizStartController> {
class _QuizStartPageState extends State<QuizStartPage> {
_QuizStartPageState();

QuizStartController get controller => widget.controller;

@override
Widget build(BuildContext context) {
controller.start();
void initState() {
super.initState();
controller.load();
}

return const PageProgressIndicator(
progressMessage: 'Carregando...',
progressState: PageProgressState.loading,
child: SizedBox.shrink(),
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0.0,
backgroundColor: DesignSystemColors.ligthPurple,
centerTitle: true,
title: const SizedBox(
width: 39,
height: 18,
child: Image(
image: AssetImage(
'assets/images/penhas_symbol/penhas_symbol.png',
),
),
),
),
body: SafeArea(
child: Observer(
builder: (_) => PageProgressIndicator(
progressMessage: 'Carregando...',
progressState: controller.progressState,
child: controller.state.when(
initial: () => const SizedBox.shrink(),
error: (message) => SupportCenterGeneralError(
message: message,
onPressed: controller.load,
),
),
),
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'quiz_start_state.freezed.dart';

@freezed
class QuizStartState with _$QuizStartState {
const factory QuizStartState.initial() = _InitialState;

const factory QuizStartState.error(String message) = _ErrorState;
}
Loading
Loading