clean up
This commit is contained in:
49
lib/pages/payment_methods/model/payment_method_model.dart
Normal file
49
lib/pages/payment_methods/model/payment_method_model.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/data/services/services.dart';
|
||||
import 'package:fcs/domain/entities/payment_method.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class PaymentMethodModel extends BaseModel {
|
||||
final log = Logger('PaymentMethodModel');
|
||||
|
||||
List<PaymentMethod> paymentMethods = [];
|
||||
|
||||
PaymentMethod getPaymentMethod(String id) {
|
||||
return paymentMethods.firstWhere((e) => e.id == id, orElse: () => null);
|
||||
}
|
||||
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
|
||||
PaymentMethodModel() {
|
||||
if (listener != null) listener.cancel();
|
||||
try {
|
||||
listener = Firestore.instance
|
||||
.collection("/payment_methods")
|
||||
.snapshots()
|
||||
.listen((snaps) {
|
||||
paymentMethods.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
paymentMethods.add(PaymentMethod.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("error:$e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addPaymentMethod(PaymentMethod paymentMethod) async {
|
||||
Services.instance.commonService.createPaymentMethod(paymentMethod);
|
||||
}
|
||||
|
||||
Future<void> updatePaymentMethod(PaymentMethod paymentMethod) async {
|
||||
Services.instance.commonService.updatePaymentMethod(paymentMethod);
|
||||
}
|
||||
|
||||
Future<void> deletePaymentMethod(String id) async {
|
||||
Services.instance.commonService.deletePayment(id);
|
||||
}
|
||||
}
|
||||
212
lib/pages/payment_methods/payment_method_editor.dart
Normal file
212
lib/pages/payment_methods/payment_method_editor.dart
Normal file
@@ -0,0 +1,212 @@
|
||||
import 'package:fcs/domain/entities/payment_method.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/main/model/main_model.dart';
|
||||
import 'package:fcs/pages/payment_methods/model/payment_method_model.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/widgets/input_text.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/progress.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_icons/flutter_icons.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class PaymentMethodEditor extends StatefulWidget {
|
||||
final PaymentMethod paymentMethod;
|
||||
|
||||
const PaymentMethodEditor({Key key, this.paymentMethod}) : super(key: key);
|
||||
@override
|
||||
_PaymentMethodEditorState createState() => _PaymentMethodEditorState();
|
||||
}
|
||||
|
||||
class _PaymentMethodEditorState extends State<PaymentMethodEditor> {
|
||||
bool _isLoading = false;
|
||||
PaymentMethod _paymentMethod;
|
||||
TextEditingController _nameController = new TextEditingController();
|
||||
TextEditingController _accountNameController = new TextEditingController();
|
||||
TextEditingController _accountNumberController = new TextEditingController();
|
||||
TextEditingController _mailController = new TextEditingController();
|
||||
TextEditingController _phoneController = new TextEditingController();
|
||||
TextEditingController _linkController = new TextEditingController();
|
||||
|
||||
bool isNew;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
isNew = widget.paymentMethod == null;
|
||||
|
||||
if (widget.paymentMethod != null) {
|
||||
_paymentMethod = widget.paymentMethod;
|
||||
_nameController.text = _paymentMethod.name;
|
||||
_accountNameController.text = _paymentMethod.accountName;
|
||||
_accountNumberController.text = _paymentMethod.account;
|
||||
_mailController.text = _paymentMethod.email;
|
||||
_phoneController.text = _paymentMethod.phone;
|
||||
_linkController.text = _paymentMethod.link;
|
||||
} else {
|
||||
_paymentMethod = new PaymentMethod();
|
||||
_nameController.text = '';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(Icons.close, color: primaryColor),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
title: LocalText(
|
||||
context,
|
||||
isNew ? "pm.new" : "pm.update",
|
||||
fontSize: 20,
|
||||
color: primaryColor,
|
||||
),
|
||||
shadowColor: Colors.transparent,
|
||||
backgroundColor: Colors.white,
|
||||
actions: <Widget>[
|
||||
isNew
|
||||
? Container()
|
||||
: IconButton(
|
||||
icon: Icon(
|
||||
Icons.delete,
|
||||
color: primaryColor,
|
||||
),
|
||||
onPressed: _delete,
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.only(left: 20.0, right: 10),
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
InputText(
|
||||
labelTextKey: "pm.name",
|
||||
iconData: Octicons.tag,
|
||||
controller: _nameController,
|
||||
),
|
||||
InputText(
|
||||
labelTextKey: "pm.account.name",
|
||||
iconData: MaterialCommunityIcons.bank,
|
||||
controller: _accountNameController,
|
||||
),
|
||||
InputText(
|
||||
labelTextKey: "pm.account.no",
|
||||
iconData: MaterialCommunityIcons.checkbook,
|
||||
controller: _accountNumberController,
|
||||
),
|
||||
InputText(
|
||||
labelTextKey: "pm.phone",
|
||||
iconData: Icons.phone,
|
||||
controller: _phoneController,
|
||||
),
|
||||
InputText(
|
||||
labelTextKey: "pm.email",
|
||||
iconData: Icons.mail,
|
||||
controller: _mailController,
|
||||
),
|
||||
InputText(
|
||||
labelTextKey: "pm.link",
|
||||
iconData: Icons.link,
|
||||
controller: _linkController,
|
||||
),
|
||||
SizedBox(
|
||||
height: 20,
|
||||
),
|
||||
isNew
|
||||
? fcsButton(context, getLocalString(context, "pm.add.btn"),
|
||||
callack: _add)
|
||||
: fcsButton(context, getLocalString(context, "pm.save.btn"),
|
||||
callack: _save),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
)
|
||||
],
|
||||
),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
_add() async {
|
||||
if (_nameController.text == "") {
|
||||
showMsgDialog(context, "Error", "Need a name for a payment method");
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
PaymentMethod pm = PaymentMethod(
|
||||
name: _nameController.text,
|
||||
accountName: _accountNameController.text,
|
||||
account: _accountNumberController.text,
|
||||
email: _mailController.text,
|
||||
phone: _phoneController.text,
|
||||
link: _linkController.text);
|
||||
await Provider.of<PaymentMethodModel>(context, listen: false)
|
||||
.addPaymentMethod(pm);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_save() async {
|
||||
if (_nameController.text == "") {
|
||||
showMsgDialog(context, "Error", "Need a name for a payment method");
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
PaymentMethod pm = PaymentMethod(
|
||||
id: _paymentMethod.id,
|
||||
name: _nameController.text,
|
||||
accountName: _accountNameController.text,
|
||||
account: _accountNumberController.text,
|
||||
email: _mailController.text,
|
||||
phone: _phoneController.text,
|
||||
link: _linkController.text);
|
||||
await Provider.of<PaymentMethodModel>(context, listen: false)
|
||||
.updatePaymentMethod(pm);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_delete() {
|
||||
showConfirmDialog(context, "pm.delete.confirm", () => _deleteConfirmed());
|
||||
}
|
||||
|
||||
_deleteConfirmed() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
await Provider.of<PaymentMethodModel>(context, listen: false)
|
||||
.deletePaymentMethod(_paymentMethod.id);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
showMsgDialog(context, "Error", e.toString());
|
||||
} finally {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
162
lib/pages/payment_methods/payment_method_page.dart
Normal file
162
lib/pages/payment_methods/payment_method_page.dart
Normal file
@@ -0,0 +1,162 @@
|
||||
import 'package:fcs/domain/entities/payment_method.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/localization/app_translations.dart';
|
||||
import 'package:fcs/pages/main/model/main_model.dart';
|
||||
import 'package:fcs/pages/payment_methods/model/payment_method_model.dart';
|
||||
import 'package:fcs/pages/payment_methods/payment_method_editor.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
|
||||
import 'package:fcs/pages/widgets/display_text.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/progress.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_icons/flutter_icons.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class PaymentMethodPage extends StatefulWidget {
|
||||
@override
|
||||
_PaymentMethodPageState createState() => _PaymentMethodPageState();
|
||||
}
|
||||
|
||||
class _PaymentMethodPageState extends State<PaymentMethodPage> {
|
||||
GlobalKey key = GlobalKey();
|
||||
bool _isLoading = false;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
PaymentMethodModel mainModel = Provider.of<PaymentMethodModel>(context);
|
||||
bool isEditable =
|
||||
context.select((MainModel m) => m.paymentMethodsEditable());
|
||||
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
key: key,
|
||||
appBar: AppBar(
|
||||
leading: new IconButton(
|
||||
icon: new Icon(
|
||||
Icons.close,
|
||||
color: primaryColor,
|
||||
),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
centerTitle: true,
|
||||
title: LocalText(
|
||||
context,
|
||||
"pm.title",
|
||||
fontSize: 20,
|
||||
color: primaryColor,
|
||||
),
|
||||
shadowColor: Colors.transparent,
|
||||
backgroundColor: Colors.white,
|
||||
actions: <Widget>[],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: ListView.separated(
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
),
|
||||
itemCount: mainModel.paymentMethods.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
var method = mainModel.paymentMethods[index];
|
||||
return _item(method, isEditable);
|
||||
},
|
||||
),
|
||||
),
|
||||
floatingActionButton: isEditable
|
||||
? FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
BottomUpPageRoute(PaymentMethodEditor()),
|
||||
);
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
label: LocalText(context, "pm.new", color: Colors.white),
|
||||
backgroundColor: primaryColor,
|
||||
)
|
||||
: Container(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_item(PaymentMethod method, bool isEditable) {
|
||||
final accountName = _itemRow(method.accountName, "pm.account.name",
|
||||
iconData: MaterialCommunityIcons.bank);
|
||||
final accountNumber = _itemRow(method.account, "pm.account.no",
|
||||
iconData: MaterialCommunityIcons.checkbook);
|
||||
final phone = _itemRow(method.phone, "pm.phone", iconData: Icons.phone);
|
||||
final email = _itemRow(method.email, "pm.email", iconData: Icons.mail);
|
||||
final link = _itemRow(method.link, "pm.link", iconData: Icons.link);
|
||||
|
||||
return InkWell(
|
||||
onTap: isEditable
|
||||
? () {
|
||||
Navigator.push(
|
||||
context,
|
||||
BottomUpPageRoute(PaymentMethodEditor(
|
||||
paymentMethod: method,
|
||||
)),
|
||||
);
|
||||
}
|
||||
: null,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
method.name,
|
||||
style: TextStyle(
|
||||
color: primaryColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18),
|
||||
),
|
||||
),
|
||||
accountName,
|
||||
accountNumber,
|
||||
phone,
|
||||
email,
|
||||
link
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_itemRow(String text, String labelKey, {IconData iconData}) {
|
||||
return text == null || text == ""
|
||||
? Container()
|
||||
: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: DisplayText(
|
||||
text: text,
|
||||
labelText: getLocalString(context, labelKey),
|
||||
iconData: iconData,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.content_copy, color: Colors.grey),
|
||||
onPressed: () => _copy(getLocalString(context, labelKey), text),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
_copy(String title, String data) {
|
||||
Clipboard.setData(ClipboardData(text: data));
|
||||
_showToast(title);
|
||||
}
|
||||
|
||||
void _showToast(String title) {
|
||||
final ScaffoldState scaffold = key.currentState;
|
||||
scaffold.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('copied "$title" data to clipboard'),
|
||||
backgroundColor: secondaryColor,
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user