Skip to content

Commit

Permalink
Return On Update Successfull
Browse files Browse the repository at this point in the history
  • Loading branch information
Teifun2 committed Aug 14, 2020
1 parent 90d8eeb commit 83da98c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/src/blocs/recipe/recipe_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
RecipeUpdated recipeUpdated) async* {
try {
yield RecipeUpdateInProgress();
await dataRepository.updateRecipe(recipeUpdated.recipe);
yield RecipeUpdateSuccess();
int recipeId = await dataRepository.updateRecipe(recipeUpdated.recipe);
yield RecipeUpdateSuccess(recipeId);
} catch (_) {
yield RecipeUpdateFailure(_.toString());
}
Expand Down
9 changes: 8 additions & 1 deletion lib/src/blocs/recipe/recipe_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class RecipeUpdateFailure extends RecipeFailure {
RecipeUpdateFailure(String errorMsg) : super(errorMsg);
}

class RecipeUpdateSuccess extends RecipeState {}
class RecipeUpdateSuccess extends RecipeState {
final int recipeId;

const RecipeUpdateSuccess(this.recipeId);

@override
List<Object> get props => [recipeId];
}

class RecipeUpdateInProgress extends RecipeState {}
43 changes: 28 additions & 15 deletions lib/src/screens/recipe_edit_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,35 @@ class RecipeEditScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: BlocListener<RecipeBloc, RecipeState>(
listener: (BuildContext context, RecipeState state) {
if (state is RecipeUpdateFailure) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text("Update Failed: ${state.errorMsg}"),
backgroundColor: Colors.red,
),
);
}
},
child: Text("Edit Recipe")),
return WillPopScope(
onWillPop: () {
RecipeBloc recipeBloc = BlocProvider.of<RecipeBloc>(context);
if (recipeBloc.state is RecipeUpdateFailure) {
recipeBloc.add(RecipeLoaded(recipeId: recipe.id));
}
return Future(() => true);
},
child: Scaffold(
appBar: AppBar(
title: BlocListener<RecipeBloc, RecipeState>(
listener: (BuildContext context, RecipeState state) {
if (state is RecipeUpdateFailure) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text("Update Failed: ${state.errorMsg}"),
backgroundColor: Colors.red,
),
);
} else if (state is RecipeUpdateSuccess) {
BlocProvider.of<RecipeBloc>(context)
.add(RecipeLoaded(recipeId: state.recipeId));
Navigator.pop(context);
}
},
child: Text("Edit Recipe")),
),
body: RecipeForm(recipe),
),
body: RecipeForm(recipe),
);
}
}
4 changes: 2 additions & 2 deletions lib/src/services/data_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class DataRepository {
return recipeProvider.fetchRecipe(id);
}

Future<void> updateRecipe(Recipe recipe) {
recipeProvider.updateRecipe(recipe);
Future<int> updateRecipe(Recipe recipe) {
return recipeProvider.updateRecipe(recipe);
}

Future<List<Category>> fetchCategories() {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/services/recipe_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RecipeProvider {
}
}

Future<void> updateRecipe(Recipe recipe) async {
Future<int> updateRecipe(Recipe recipe) async {
AppAuthentication appAuthentication =
UserRepository().currentAppAuthentication;

Expand All @@ -42,6 +42,7 @@ class RecipeProvider {
headers: {
"authorization": appAuthentication.basicAuth,
}));
return response.data;
} catch (e) {
throw Exception(e);
}
Expand Down

0 comments on commit 83da98c

Please sign in to comment.