Bloc: Android Studio Plugin for Bloc Generation

Created on 27 Feb 2019  路  13Comments  路  Source: felangel/bloc

For Android Studio user, it would be fine to have template for bloc generation.
Is it in your plans ?

enhancement

Most helpful comment

I have some experience with code templates in IntelliJ... I can work on it ;)

All 13 comments

@fvisticot There is currently support for VSCode and I am planning on extending the extension to create the bloc as well as the states and events.

Not sure when I'll get to Android Studio but you're more than welcome to contribute 馃槃

Ideally, the Android Studio Plugin functionality should align with the VSCode extension (#118).

Use Uber's RIBs Plugin for reference.

I have some experience with code templates in IntelliJ... I can work on it ;)

@felangel we should agree on some conventions first before we do any coding, so we keep the IntelliJ plugin and VS Code in sync. What about?

Files generated (XXX represents the name of your bloc, we will have to sanitize this in the generators to be camelCase on code and snake_case for file names)

  • XXX_bloc.dart
  • XXX_event.dart
  • XXX_state.dart
  • bloc.dart

and contents:

bloc.dart

export 'XXX_bloc.dart';
export 'XXX_event.dart';
export 'XXX_state.dart';

XXX_event.dart

import 'package:equatable/equatable.dart';

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

XXX_state.dart

import 'package:equatable/equatable.dart';

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

XXX_bloc.dart

import 'dart:async';

import 'package:bloc/bloc.dart';

class XXXBloc
    extends Bloc<XXXEvent, XXXState> {

  @override
  ResetPasswordUiState get initialState => throw Exception('Complete your initialState');

  @override
  Stream<XXXState> mapEventToState(
      XXXState state, XXXEvent event) async* {
    throw Exception('Complete your mapEventToState');
  }
}

I think that might be all we need, right?

@jorgecoca that looks good to me 馃憤
I鈥檒l let you know if I run into anything we haven鈥檛 considered as I make progress on the VSCode extension.

Hello everyone,
While there is no plugin for android studio you can simply do following:

  • Add bloc template in settings:
    image

  • Add Template text:

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

class $NAME$Bloc extends Bloc<$NAME$Event, $NAME$State> {
  @override
  $NAME$State get initialState => null;// TODO: initial state

  @override
  Stream<$NAME$State> mapEventToState($NAME$State currentState, $NAME$Event event) async* {
    // TODO: implement mapEventToState
  }
}

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

abstract class $NAME$Event extends Equatable {
  $NAME$Event([List props = const []]) : super(props);
}
  • Go back to editor and start typing blo:
    image

  • Hit enter, give NAME for your bloc and you'll have following:
    image

Have a nice day!

Good job,
The solution is working but I think original proposal allowing to generate multiple files with proposed convention will result in better code organisation / evolution

Good job,
The solution is working but I think original proposal allowing to generate multiple files with proposed convention will result in better code organisation / evolution

Totally agree.
I've posted temporary solution.
But you know how it is... permanent temporary solution :laughing:

@jorgecoca will most likely have the permanent solution done relatively quickly 馃帀馃挴

By the end of this I hope to have something up and running ;)

You guys are awesome!

@jorgecoca is awesome 馃

I opened #148 ;)

merged in #148

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  路  3Comments

timtraversy picture timtraversy  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

zjjt picture zjjt  路  3Comments

rsnider19 picture rsnider19  路  3Comments