wt1.1a flutter_bloc
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:aitrainer_app/util/session.dart';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
part 'session_event.dart';
|
||||
part 'session_state.dart';
|
||||
|
||||
class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
final Session session;
|
||||
SessionBloc({this.session}) : super(SessionInitial());
|
||||
|
||||
@override
|
||||
Stream<SessionState> mapEventToState(
|
||||
SessionEvent event,
|
||||
) async* {
|
||||
try {
|
||||
if (event is SessionStart) {
|
||||
yield SessionLoading();
|
||||
await session.fetchSessionAndNavigate();
|
||||
yield SessionReady();
|
||||
}
|
||||
} on Exception catch(ex) {
|
||||
yield SessionFailure(message: ex.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await this.close(); super.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
part of 'session_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SessionEvent extends Equatable {
|
||||
const SessionEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class SessionStart extends SessionEvent {
|
||||
const SessionStart();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
part of 'session_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SessionState extends Equatable {
|
||||
const SessionState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class SessionInitial extends SessionState {
|
||||
const SessionInitial();
|
||||
}
|
||||
|
||||
class SessionLoading extends SessionState {
|
||||
const SessionLoading();
|
||||
}
|
||||
|
||||
class SessionReady extends SessionState {
|
||||
const SessionReady();
|
||||
}
|
||||
|
||||
class SessionFailure extends SessionState {
|
||||
final String message;
|
||||
const SessionFailure({this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user