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

VSCode Extension for Bloc Generation #118

Closed
felangel opened this issue Feb 27, 2019 · 6 comments
Closed

VSCode Extension for Bloc Generation #118

felangel opened this issue Feb 27, 2019 · 6 comments
Assignees
Labels
enhancement New feature or request
Projects

Comments

@felangel
Copy link
Owner

Is your feature request related to a problem? Please describe.
Currently the VSCode extension for bloc just supports snippets for creating a bloc but it doesn't:

  • Create the bloc state file
  • Populate the bloc states
  • Create the bloc event file
  • Populate the bloc events
  • Create the bloc file
  • Populate the bloc
  • Create the bloc barrel
  • Populate the bloc barrel (with all exports)

Describe the solution you'd like
A full-blown VSCode extension that:

  • asks for the bloc name (Todo)
  • asks for the bloc events (Add, Remove)
  • asks for the bloc states (Loading, Loaded, Failure)

And then generates:

 bloc
    ├── bloc.dart
    ├── todo_bloc.dart
    ├── todo_event.dart
    └── todo_state.dart
// bloc.dart
export './todo_bloc.dart';
export './todo_event.dart';
export './todo_state.dart';
// todo_bloc.dart

import 'package:bloc/bloc.dart';
import './bloc/bloc.dart';

class TodoBloc extends Bloc<TodoEvent, TodoState> {
  @override
  int get initialState => // TODO: Implement Initial State;

  @override
  Stream<TodoState> mapEventToState(TodoState currentState, TodoEvent event) async* {
    if (event is AddTodoEvent) {
      yield* _mapAddTodoEventToState(currentState, event);
    } else if (event is RemoveTodoEvent) {
      yield* _mapRemoveTodoEventToState(currentState, event);
    } else {
      yield currentState;
    }
  }
  
  Stream<TodoState> _mapAddTodoEventToState(
    TodoState currentState,
    AddTodoEvent event,
  ) async* {
    // TODO: Handle AddTodoEvent
  }

  Stream<TodoState> _mapRemoveTodoEventToState(
    TodoState currentState,
    AddTodoEvent event,
  ) async* {
    // TODO: Handle RemoveTodoEvent
  }
}
// todo_state.dart

abstract class TodoState {}

class LoadingTodoState extends TodoState {}

class LoadedTodoState extends TodoState {}

class FailureTodoState extends TodoState {}
// todo_event.dart

abstract class TodoEvent {}

class AddTodoEvent extends TodoEvent {}

class RemoveTodoEvent extends TodoEvent {}

Additional context
#117

@felangel felangel added the enhancement New feature or request label Feb 27, 2019
@felangel felangel self-assigned this Feb 27, 2019
@felangel felangel added this to To do in bloc Feb 27, 2019
@felangel felangel moved this from To do to In progress in bloc Feb 27, 2019
@felangel felangel changed the title VSCode Extension to Generate all Bloc Code VSCode Extension for Bloc Generation Mar 1, 2019
@GiancarloCode
Copy link
Contributor

Hello
Because normally apps have more than 1 bloc, I think the following structure should be generated:

A full-blown VSCode extension that:

  • asks for the bloc name (Todo)
  • asks for the bloc events (Add, Remove)
  • asks for the bloc states (Loading, Loaded, Failure)

And then generates:

├── blocs
    ├── blocs.dart
    ├── todo
        ├── todo.dart
        ├── todo_bloc.dart
        ├── todo_event.dart
        └── todo_state.dart
// blocs.dart
export 'todo/todo.dart';

And when we want to add new bloc:

A full-blown VSCode extension that:

  • asks for the bloc name (Theme)
  • asks for the bloc events (Change)
  • asks for the bloc states (Theme)

And then the blocs folder have:

├── blocs
    ├── blocs.dart
    ├── theme
        ├── theme.dart
        ├── theme_bloc.dart
        ├── theme_event.dart
        └── theme_state.dart
    ├── todo
        ├── todo.dart
        ├── todo_bloc.dart
        ├── todo_event.dart
        └── todo_state.dart

And add at the end of line of blocs.dart

// blocs.dart
export 'todo/todo.dart';
export 'theme/theme.dart';

Also you recommend use Equatable, and I used too, so you can create the next flow

A full-blown VSCode extension that:

  • asks for the bloc name (Todo)
  • asks for the bloc events (Add, Remove)
  • asks for the bloc states (Loading, Loaded, Failure)
  • asks for use Equatable (Select: Yes/No)

So now, if the user select Yes for use Equatable, Event and State files are like this:

// todo_state.dart
import 'package:equatable/equatable.dart';

abstract class TodoState extends Equatable {
  TodoState([List props = const []]) : super(props);
}

class LoadingTodoState extends TodoState {}

class LoadedTodoState extends TodoState {}

class FailureTodoState extends TodoState {}
// todo_event.dart
import 'package:equatable/equatable.dart';

abstract class TodoEvent extends Equatable {
  TodoEvent ([List props = const []]) : super(props);
}

class AddTodoEvent extends TodoEvent {}

class RemoveTodoEvent extends TodoEvent {}

Thank you and congratulations for your great work with this library.

@felangel
Copy link
Owner Author

felangel commented Mar 9, 2019

v0.2.0 of the Bloc VSCode Extension is published 🎉

@felangel felangel closed this as completed Mar 9, 2019
@felangel felangel moved this from In progress to Done in bloc Mar 9, 2019
@agordeev
Copy link

@felangel Thanks for the awesome lib and extension. I actually came to this issue from the search. It is unclear to me how the extension could help if I want to have more than one bloc (which is 99.999% of the cases I suppose)? Renaming each file doesn't seem a good solution.
@GiancarloCode suggested a good approach. It'd be great to know your thoughts about that.

@felangel
Copy link
Owner Author

@agordeev thanks for the input. I would generally recommend structuring your project by feature and not by component type. That way instead of having a blocs directory you would have a bloc directory in the necessary feature directories. Let me know if that makes sense. I have also been meaning to make the vscode extension more configurable via a bloc_options.yaml (or something similar) so that might be a case where we can make this behavior customizable. Thoughts?

@agordeev
Copy link

@felangel clearly makes sense, thanks for your prompt answer. I didn't think that way (feature-based architecture), will try.

Wouldn't it be better to have extension settings instead of bloc_options.yaml?

@felangel
Copy link
Owner Author

No problem! Yeah we could do extension settings but it’d be nice to be able to have a configuration per repo so that different projects can have different structures/preferences and to be able to reuse the configuration for the intellij plugin as well 😛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
bloc
  
Done
Development

No branches or pull requests

3 participants