Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved. 2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. 3 : 4 : import 'package:app_finance/_classes/herald/app_locale.dart'; 5 : import 'package:app_finance/_classes/structure/account_app_data.dart'; 6 : import 'package:app_finance/_configs/account_type.dart'; 7 : import 'package:app_finance/pages/account/account_add_page.dart'; 8 : import 'package:flutter/material.dart'; 9 : 10 : class AccountEditPage extends AccountAddPage { 11 : final String uuid; 12 : 13 0 : const AccountEditPage({ 14 : super.key, 15 : required this.uuid, 16 : }); 17 : 18 0 : @override 19 0 : AccountEditPageState createState() => AccountEditPageState(); 20 : } 21 : 22 : class AccountEditPageState extends AccountAddPageState<AccountEditPage> { 23 : late String uuid; 24 : bool isFirstRun = true; 25 : 26 0 : @override 27 : void initState() { 28 0 : uuid = (widget as AccountEditPage).uuid; 29 0 : super.initState(); 30 : } 31 : 32 0 : void bindState() { 33 0 : if (!isFirstRun) { 34 : return; 35 : } 36 0 : setState(() { 37 0 : isFirstRun = false; 38 0 : final form = state.getByUuid(uuid) as AccountAppData; 39 0 : title.text = form.title; 40 0 : description.text = form.description ?? ''; 41 0 : type = form.type; 42 0 : balance.text = form.details != null ? form.details.toString() : ''; 43 0 : color = form.color; 44 0 : currency = form.currency; 45 0 : icon = form.icon; 46 0 : validTillDate = form.closedAt; 47 0 : balanceUpdateDate = DateTime.now(); 48 : }); 49 : } 50 : 51 0 : @override 52 : String getTitle() { 53 0 : return AppLocale.labels.editAccountHeader; 54 : } 55 : 56 0 : @override 57 : void updateStorage() { 58 0 : super.state.update( 59 0 : uuid, 60 0 : AccountAppData( 61 0 : uuid: uuid, 62 0 : title: title.text, 63 0 : type: type ?? AppAccountType.cash.toString(), 64 0 : description: description.text, 65 0 : details: double.tryParse(balance.text) ?? 0.0, 66 : progress: 1.0, 67 0 : color: color ?? Colors.red, 68 0 : currency: currency, 69 : hidden: false, 70 0 : icon: icon, 71 0 : closedAt: validTillDate, 72 0 : createdAt: balanceUpdateDate, 73 : )); 74 : } 75 : 76 0 : @override 77 0 : String getButtonName() => AppLocale.labels.updateAccountTooltip; 78 : 79 0 : @override 80 : Widget buildContent(BuildContext context, BoxConstraints constraints) { 81 0 : WidgetsBinding.instance.addPostFrameCallback((_) => bindState()); 82 0 : return super.buildContent(context, constraints); 83 : } 84 : }