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

Substituir ModularState por State e injetar FeedRoutingProfileChatController #360

Open
shonorio opened this issue Jan 11, 2025 · 0 comments
Labels
dependencies Pull requests that update a dependency file deprecation Para destacar o uso de componentes obsoletos good first issue Good for newcomers refactor Envolve melhorias no código, como substituir o componentes obsoletos

Comments

@shonorio
Copy link
Collaborator

Descrição

A classe FeedRoutingProfileChatPage atualmente utiliza ModularState para gerenciar o estado e o controlador. A proposta é substituir ModularState por State e injetar o FeedRoutingProfileChatController como dependência via passagem de parâmetro no construtor.

Alterações Propostas

  1. Substituir ModularState por State.
  2. Injetar FeedRoutingProfileChatController via construtor.
  3. Remover a dependência do flutter_modular para gerenciamento de estado.

Código Atual

import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_modular/flutter_modular.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 '../../domain/states/feed_routing_state.dart';
import 'feed_routing_profile_chat_controller.dart';

class FeedRoutingProfileChatPage extends StatefulWidget {
  const FeedRoutingProfileChatPage({Key? key}) : super(key: key);

  @override
  _FeedRoutingProfileChatPageState createState() =>
      _FeedRoutingProfileChatPageState();
}

class _FeedRoutingProfileChatPageState extends ModularState<
    FeedRoutingProfileChatPage, FeedRoutingProfileChatController> {
  @override
  Widget build(BuildContext context) {
    return Observer(builder: (context) => pageBuilder(controller.routingState));
  }
}

extension _Bodybuilder on _FeedRoutingProfileChatPageState {
  Widget pageBuilder(FeedRoutingState state) {
    return state.when(
      initial: (title) => loadingPage(title),
      error: (title, msg) => errorPage(title, msg),
    );
  }

  Widget loadingPage(String title) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(title),
        backgroundColor: DesignSystemColors.easterPurple,
      ),
      body: SafeArea(
        child: PageProgressIndicator(
          progressState: PageProgressState.loading,
          progressMessage: 'Carregando...',
          child: Container(color: DesignSystemColors.white),
        ),
      ),
    );
  }

  Widget errorPage(String title, String message) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(title),
        backgroundColor: DesignSystemColors.easterPurple,
      ),
      body: SafeArea(
        child: SupportCenterGeneralError(
          message: message,
          onPressed: controller.retry,
        ),
      ),
    );
  }
}

Código Proposto

import 'package:flutter/material.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 '../../domain/states/feed_routing_state.dart';
import 'feed_routing_profile_chat_controller.dart';

class FeedRoutingProfileChatPage extends StatefulWidget {
  final FeedRoutingProfileChatController controller;

  const FeedRoutingProfileChatPage({
    Key? key,
    required this.controller,
  }) : super(key: key);

  @override
  _FeedRoutingProfileChatPageState createState() =>
      _FeedRoutingProfileChatPageState();
}

class _FeedRoutingProfileChatPageState extends State<FeedRoutingProfileChatPage> {
  late final FeedRoutingProfileChatController controller;

  @override
  void initState() {
    super.initState();
    controller = widget.controller;
  }

  @override
  Widget build(BuildContext context) {
    return Observer(builder: (context) => pageBuilder(controller.routingState));
  }
}

extension _Bodybuilder on _FeedRoutingProfileChatPageState {
  Widget pageBuilder(FeedRoutingState state) {
    return state.when(
      initial: (title) => loadingPage(title),
      error: (title, msg) => errorPage(title, msg),
    );
  }

  Widget loadingPage(String title) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(title),
        backgroundColor: DesignSystemColors.easterPurple,
      ),
      body: SafeArea(
        child: PageProgressIndicator(
          progressState: PageProgressState.loading,
          progressMessage: 'Carregando...',
          child: Container(color: DesignSystemColors.white),
        ),
      ),
    );
  }

  Widget errorPage(String title, String message) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(title),
        backgroundColor: DesignSystemColors.easterPurple,
      ),
      body: SafeArea(
        child: SupportCenterGeneralError(
          message: message,
          onPressed: controller.retry,
        ),
      ),
    );
  }
}

Impacto

  • Remoção da dependência do flutter_modular para gerenciamento de estado.
  • Maior controle sobre a injeção de dependências.
  • Código mais limpo e testável.

Testes

Certifique-se de que todos os testes existentes continuem passando após a refatoração. Adicione novos testes, se necessário, para cobrir a injeção de dependência via construtor.

Observações

  • A injeção de dependência via construtor facilita a testabilidade e a manutenção do código.
  • A remoção do ModularState pode exigir ajustes em outros locais onde ele é utilizado.
@shonorio shonorio added dependencies Pull requests that update a dependency file deprecation Para destacar o uso de componentes obsoletos refactor Envolve melhorias no código, como substituir o componentes obsoletos good first issue Good for newcomers labels Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file deprecation Para destacar o uso de componentes obsoletos good first issue Good for newcomers refactor Envolve melhorias no código, como substituir o componentes obsoletos
Projects
None yet
Development

No branches or pull requests

1 participant