Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dante291 committed Nov 13, 2024
1 parent 75e2675 commit 3a3775d
Show file tree
Hide file tree
Showing 10 changed files with 1,733 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/views/after_auth_screens/funds/campaigns_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO Implement this library.
2 changes: 0 additions & 2 deletions lib/views/after_auth_screens/funds/fund_pledges_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ class _PledgesScreenState extends State<PledgesScreen> {
/// **returns**:
/// * `Widget`: a list view of all pledges.
Widget _buildPledgesList(FundViewModel model) {
print(model.filteredPledges.length);
if (model.isFetchingPledges) {
return const Center(child: CircularProgressIndicator());
}
Expand All @@ -326,7 +325,6 @@ class _PledgesScreenState extends State<PledgesScreen> {
return ListView.builder(
itemCount: model.filteredPledges.length,
itemBuilder: (context, index) {
print(model.filteredPledges.length);
final pledge = model.filteredPledges[index];
return PledgeCard(
pledge: pledge,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/pledge_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PledgeCard extends StatelessWidget {
style: Theme.of(context).textTheme.bodySmall,
),
Text(
'\$${pledge.amount?.toStringAsFixed(2) ?? 'N/A'}',
'\$${pledge.amount!.toStringAsFixed(2)}',
style: Theme.of(context)
.textTheme
.titleLarge
Expand Down
86 changes: 74 additions & 12 deletions lib/widgets/update_pledge_dialogue_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class _UpdatePledgeDialogState extends State<UpdatePledgeDialog> {
super.initState();
_amountController =
TextEditingController(text: widget.pledge.amount?.toString() ?? '');
_amountController.addListener(_onAmountChanged);
_startDate = widget.pledge.startDate;
_endDate = widget.pledge.endDate;
_selectedPledgers = widget.pledge.pledgers ?? [];
Expand All @@ -56,6 +57,24 @@ class _UpdatePledgeDialogState extends State<UpdatePledgeDialog> {
_originalEndDate = widget.pledge.endDate;
}

/// Changes state if amout is changed.
///
/// **params**:
/// None
///
/// **returns**:
/// None
void _onAmountChanged() {
setState(() {});
}

@override
void dispose() {
_amountController.removeListener(_onAmountChanged);
_amountController.dispose();
super.dispose();
}

/// Checks if there are any changes in the current pledge details compared to the original values.
///
/// **params**:
Expand All @@ -64,14 +83,56 @@ class _UpdatePledgeDialogState extends State<UpdatePledgeDialog> {
/// **returns**:
/// * `bool`: `true` if any detail has changed, `false` otherwise.
bool _hasChanges() {
return _originalAmount != double.parse(_amountController.text) ||
double currentAmount = _originalAmount;
if (_amountController.text.isNotEmpty) {
try {
currentAmount = double.tryParse(_amountController.text) ?? 0;
} catch (e) {
// Handle invalid input gracefully if necessary
}
}

return currentAmount != _originalAmount ||
_originalCurrency != widget.model.donationCurrency ||
_originalPledgers.length != _selectedPledgers.length ||
_selectedPledgers.any((user) => !_originalPledgers.contains(user)) ||
_startDate != _originalStartDate ||
_endDate != _originalEndDate;
}

/// Method to get fields that are updated.
///
/// **params**:
/// None
///
/// **returns**:
/// * `Map<String, dynamic>`: Object which include fields which are changed.
Map<String, dynamic> _getChangedFields() {
final Map<String, dynamic> changes = {'id': widget.pledge.id};
try {
final double currentAmount = double.tryParse(_amountController.text) ?? 0;
if (currentAmount != _originalAmount) {
changes['amount'] = currentAmount;
}
} catch (e) {
// Handle parse error if needed
}
if (widget.model.donationCurrency != _originalCurrency) {
changes['currency'] = widget.model.donationCurrency;
}
if (_startDate != _originalStartDate && _startDate != null) {
changes['startDate'] = DateFormat('yyyy-MM-dd').format(_startDate!);
}
if (_endDate != _originalEndDate && _endDate != null) {
changes['endDate'] = DateFormat('yyyy-MM-dd').format(_endDate!);
}
if (_selectedPledgers.length != _originalPledgers.length ||
_selectedPledgers.any((user) => !_originalPledgers.contains(user))) {
changes['users'] = _selectedPledgers.map((user) => user.id).toList();
}
return changes;
}

@override
Widget build(BuildContext context) {
return AlertDialog(
Expand Down Expand Up @@ -177,6 +238,7 @@ class _UpdatePledgeDialogState extends State<UpdatePledgeDialog> {
),
Expanded(
child: TextFormField(
key: const Key('amount_field'),
controller: _amountController,
decoration: InputDecoration(
labelText: 'Amount',
Expand All @@ -187,7 +249,15 @@ class _UpdatePledgeDialogState extends State<UpdatePledgeDialog> {
if (value == null || value.isEmpty) {
return 'Please enter an amount';
}
return null;
final parsedValue = double.tryParse(value);
if (parsedValue == null) {
return 'Amount must be a number';
}
if (parsedValue <= 0) {
return 'Amount must be greater than zero';
}

return null; // Input is valid
},
),
),
Expand Down Expand Up @@ -252,22 +322,14 @@ class _UpdatePledgeDialogState extends State<UpdatePledgeDialog> {
child: const Text('Cancel'),
),
ElevatedButton(
key: const Key('update_btn'),
onPressed: _hasChanges()
? () {
if (_formKey.currentState!.validate() &&
_startDate != null &&
_endDate != null &&
_selectedPledgers.isNotEmpty) {
widget.onSubmit({
'id': widget.pledge.id,
'amount': double.parse(_amountController.text),
'startDate': DateFormat('yyyy-MM-dd').format(_startDate!),
'endDate': DateFormat('yyyy-MM-dd').format(_endDate!),
'users':
_selectedPledgers.map((user) => user.id).toList(),
'currency': widget.model.donationCurrency,
});
Navigator.of(context).pop();
widget.onSubmit(_getChangedFields());
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please fill all fields')),
Expand Down
Loading

0 comments on commit 3a3775d

Please sign in to comment.