-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to standalone page for create basket
- Loading branch information
Showing
5 changed files
with
183 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'package:wealth_wave/api/apis/basket_api.dart'; | ||
import 'package:wealth_wave/core/presenter.dart'; | ||
import 'package:wealth_wave/core/single_event.dart'; | ||
import 'package:wealth_wave/domain/models/basket.dart'; | ||
|
||
class CreateBasketPresenter extends Presenter<CreateBasketViewState> { | ||
final BasketApi _basketApi; | ||
|
||
CreateBasketPresenter({final BasketApi? basketApi}) | ||
: _basketApi = basketApi ?? BasketApi(), | ||
super(CreateBasketViewState()); | ||
|
||
void createBasket({final int? basketId}) { | ||
var viewState = getViewState(); | ||
|
||
if (!viewState.isValid()) { | ||
return; | ||
} | ||
|
||
final name = viewState.name; | ||
final description = viewState.description; | ||
|
||
if (basketId != null) { | ||
_basketApi | ||
.updateBasket(id: basketId, name: name, description: description) | ||
.then((_) => updateViewState( | ||
(viewState) => viewState.onBasketCreated = SingleEvent(null))); | ||
} else { | ||
_basketApi.createBasket(name: name, description: description).then((_) => | ||
updateViewState( | ||
(viewState) => viewState.onBasketCreated = SingleEvent(null))); | ||
} | ||
} | ||
|
||
void onNameChanged(String text) { | ||
updateViewState((viewState) => viewState.name = text); | ||
} | ||
|
||
void onDescriptionChanged(String text) { | ||
updateViewState((viewState) => viewState.description = text); | ||
} | ||
|
||
void setBasket(Basket basketToUpdate) { | ||
updateViewState((viewState) { | ||
viewState.name = basketToUpdate.name; | ||
viewState.description = basketToUpdate.description; | ||
}); | ||
} | ||
} | ||
|
||
class CreateBasketViewState { | ||
String name = ''; | ||
String? description; | ||
SingleEvent<void>? onBasketCreated; | ||
|
||
bool isValid() { | ||
return name.isNotEmpty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:wealth_wave/core/page_state.dart'; | ||
import 'package:wealth_wave/domain/models/basket.dart'; | ||
import 'package:wealth_wave/presentation/create_basket_presenter.dart'; | ||
import 'package:wealth_wave/ui/app_dimen.dart'; | ||
|
||
Future<void> showCreateBasketDialog( | ||
{required final BuildContext context, final Basket? basketToUpdate}) { | ||
return showDialog( | ||
context: context, | ||
builder: (context) => | ||
_CreateBasketDialog(basketToUpdate: basketToUpdate)); | ||
} | ||
|
||
class _CreateBasketDialog extends StatefulWidget { | ||
final Basket? basketToUpdate; | ||
|
||
const _CreateBasketDialog({this.basketToUpdate}); | ||
|
||
@override | ||
State<_CreateBasketDialog> createState() => _CreateBasketPage(); | ||
} | ||
|
||
class _CreateBasketPage extends PageState<CreateBasketViewState, | ||
_CreateBasketDialog, CreateBasketPresenter> { | ||
final _nameController = TextEditingController(); | ||
final _descriptionController = TextEditingController(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
Basket? basketToUpdate = widget.basketToUpdate; | ||
if (basketToUpdate != null) { | ||
_nameController.text = basketToUpdate.name; | ||
_descriptionController.text = basketToUpdate.description ?? ''; | ||
presenter.setBasket(basketToUpdate); | ||
} | ||
|
||
_nameController.addListener(() { | ||
presenter.onNameChanged(_nameController.text); | ||
}); | ||
|
||
_descriptionController.addListener(() { | ||
presenter.onDescriptionChanged(_descriptionController.text); | ||
}); | ||
} | ||
|
||
@override | ||
Widget buildWidget(BuildContext context, CreateBasketViewState snapshot) { | ||
WidgetsBinding.instance.addPostFrameCallback((_) { | ||
snapshot.onBasketCreated?.consume((_) { | ||
Navigator.of(context).pop(); | ||
}); | ||
}); | ||
|
||
return AlertDialog( | ||
title: Text('Create Basket', | ||
style: Theme.of(context).textTheme.titleMedium), | ||
actions: [ | ||
ElevatedButton( | ||
child: const Text("Cancel"), | ||
onPressed: () { | ||
Navigator.pop(context); | ||
}), | ||
ElevatedButton( | ||
onPressed: snapshot.isValid() | ||
? () { | ||
presenter.createBasket(basketId: widget.basketToUpdate?.id); | ||
} | ||
: null, | ||
child: widget.basketToUpdate != null | ||
? const Text('Update') | ||
: const Text('Create'), | ||
), | ||
], | ||
content: SingleChildScrollView( | ||
child: Column(children: <Widget>[ | ||
TextFormField( | ||
textInputAction: TextInputAction.next, | ||
controller: _nameController, | ||
decoration: const InputDecoration( | ||
labelText: 'Name', border: OutlineInputBorder()), | ||
), | ||
const SizedBox(height: AppDimen.minPadding), | ||
TextFormField( | ||
textInputAction: TextInputAction.next, | ||
controller: _descriptionController, | ||
decoration: const InputDecoration( | ||
labelText: 'Description', border: OutlineInputBorder()), | ||
), | ||
]), | ||
)); | ||
} | ||
|
||
@override | ||
CreateBasketPresenter initializePresenter() { | ||
return CreateBasketPresenter(); | ||
} | ||
} |