Skip to content

Commit

Permalink
Merge pull request #5 from duc-ios/main
Browse files Browse the repository at this point in the history
refactor: error handling
  • Loading branch information
cogivn authored Jul 19, 2024
2 parents 9e5b472 + 3ad68fd commit 143af47
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:enum_annotation/enum_annotation.dart';
import 'package:injectable/injectable.dart';
{{#is_bloc}}import 'package:bloc/bloc.dart';{{/is_bloc}}{{#is_riverpod}}import 'package:riverbloc/riverbloc.dart';{{/is_riverpod}}
{{#is_bloc}}import 'package:bloc/bloc.dart';{{/is_bloc}}{{#is_riverpod}}import 'package:riverbloc/riverbloc.dart';
import '../../../../common/utils/getit_utils.dart';{{/is_riverpod}}

import '../../../../common/utils/getit_utils.dart';
import '../../../../common/mixin/cancelable_base_bloc.dart';
import '../../../../core/infrastructure/datasources/remote/api/base/api_error.dart';
import '../../domain/entities/{{name.snakeCase()}}.dart';
import '../../domain/repositories/{{name.snakeCase()}}_repository.dart';

part '{{name.snakeCase()}}_state.dart';
part '{{name.snakeCase()}}_cubit.freezed.dart';
part '{{name.snakeCase()}}_cubit.g.dart';

{{#is_riverpod}}final {{name.camelCase()}}Provider = BlocProvider<{{name.pascalCase()}}Cubit, {{name.pascalCase()}}State>((_) {
return getIt<{{name.pascalCase()}}Cubit>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
part of '{{name.snakeCase()}}_cubit.dart';

@generate
enum {{name.pascalCase()}}Status {
initial,
loading,
error,
loaded;
@freezed
class {{name.pascalCase()}}Status with _${{name.pascalCase()}}Status {
const factory {{name.pascalCase()}}Status.initial() = _InitialStatus;
const factory {{name.pascalCase()}}Status.loading() = _LoadingStatus;
const factory {{name.pascalCase()}}Status.error(ApiError error) = _ErrorStatus;
const factory {{name.pascalCase()}}Status.loaded() = _LoadedStatus;
}

@freezed
class {{name.pascalCase()}}State with _${{name.pascalCase()}}State {
const {{name.pascalCase()}}State._();

const factory {{name.pascalCase()}}State({
@Default({{name.pascalCase()}}Status.initial) {{name.pascalCase()}}Status status,
@Default(_InitialStatus()) {{name.pascalCase()}}Status status,
{{name.pascalCase()}}? data,
ApiError? error,
}) = _{{name.pascalCase()}}State;
}

extension {{name.pascalCase()}}StateX on {{name.pascalCase()}}State {
{{name.pascalCase()}}State get loading => copyWith(status: {{name.pascalCase()}}Status.loading);
bool get isLoading => status == const _LoadingStatus();

{{name.pascalCase()}}State get loading => copyWith(status: const _LoadingStatus());

{{name.pascalCase()}}State onError(ApiError error) => copyWith(
status: {{name.pascalCase()}}Status.error,
error: error,
status: _ErrorStatus(error),
);

{{name.pascalCase()}}State onLoaded({{name.pascalCase()}} data) => copyWith(
status: {{name.pascalCase()}}Status.loaded,
status: const _LoadedStatus(),
data: data,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import 'package:auto_route/annotations.dart';
import 'package:flutter/material.dart';
{{#is_bloc}}import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../common/utils/getit_utils.dart';
import '../../application/{{name.snakeCase()}}_cubit/{{name.snakeCase()}}_cubit.dart';{{/is_bloc}}
{{/is_bloc}}{{^is_bloc}}import 'package:flutter_riverpod/flutter_riverpod.dart';
{{/is_bloc}}
import '../../application/{{name.snakeCase()}}_cubit/{{name.snakeCase()}}_cubit.dart';
import '../../../../common/extensions/build_context_dialog.dart';
import '../widgets/{{name.snakeCase()}}_body.dart';

@RoutePage()
Expand All @@ -15,15 +18,24 @@ import '../widgets/{{name.snakeCase()}}_body.dart';
create: (context) => getIt<{{name.pascalCase()}}Cubit>(),
child: Scaffold(
appBar: AppBar(),
body: const {{name.pascalCase()}}Body(),
body: BlocListener<{{name.pascalCase()}}Cubit, {{name.pascalCase()}}State>(
listener: (context, state) => state.status
.whenOrNull(error: (error) => context.showApiError(error)),
child: const {{name.pascalCase()}}Body(),
),
),
);
}
}{{/is_bloc}}{{^is_bloc}}class {{name.pascalCase()}}Page extends StatelessWidget {
}{{/is_bloc}}{{^is_bloc}}class {{name.pascalCase()}}Page extends ConsumerWidget {
const {{name.pascalCase()}}Page({super.key});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
ref.listen(
{{name.camelCase()}}Provider,
(_, current) => current.status
.whenOrNull(error: (error) => context.showApiError(error)),
);
return Scaffold(
appBar: AppBar(),
body: const {{name.pascalCase()}}Body(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class {{name.pascalCase()}}Body extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Container();
return Center(
child: Text('{{name.pascalCase()}}'),
);
}
}

0 comments on commit 143af47

Please sign in to comment.