Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #26 from pvdthings/fix/list-error-handling
Browse files Browse the repository at this point in the history
Fix: List error handling
  • Loading branch information
dillonfagan authored Jun 20, 2023
2 parents 545eb84 + 4d835d8 commit 4256d08
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:librarian_app/src/features/borrowers/data/borrowers_view_model.dart';
import 'package:librarian_app/src/features/borrowers/presentation/borrower_details_pane.dart';
import 'package:librarian_app/src/features/borrowers/presentation/borrowers_list.dart';
import 'package:librarian_app/src/features/borrowers/views/borrowers_view.dart';
import 'package:librarian_app/src/features/common/widgets/dashboard/pane_header.dart';
import 'package:librarian_app/src/features/common/widgets/search_field.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -41,12 +41,11 @@ class _BorrowersDesktopLayoutState extends State<BorrowersDesktopLayout> {
},
),
),
BorrowersList(
borrowers: model.filtered(_searchFilter),
selected: model.selectedBorrower,
onTap: (borrower) {
model.selectedBorrower = borrower;
},
Expanded(
child: BorrowersView(
model: model,
searchFilter: _searchFilter,
),
),
],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:librarian_app/src/features/borrowers/data/borrowers_view_model.dart';
import 'package:librarian_app/src/features/borrowers/presentation/borrowers_list.dart';
import 'package:librarian_app/src/features/borrowers/views/borrowers_view.dart';
import 'package:provider/provider.dart';

import '../data/borrower_model.dart';
Expand All @@ -19,28 +19,10 @@ class ConnectedBorrowersList extends StatelessWidget {
Widget build(BuildContext context) {
return Consumer<BorrowersViewModel>(
builder: (context, model, child) {
if (model.errorMessage != null) {
return Center(child: Text(model.errorMessage!));
}

if (model.isLoading) {
return const Center(child: CircularProgressIndicator());
}

var localBorrowers = model.borrowers;
if (filter != null) {
localBorrowers = localBorrowers
.where((b) => b.name.toLowerCase().contains(filter!))
.toList();
}

return BorrowersList(
borrowers: localBorrowers,
onTap: (b) {
model.selectedBorrower = b;
onTap?.call(b);
},
selected: onTap == null ? model.selectedBorrower : null,
return BorrowersView(
model: model,
searchFilter: filter ?? '',
onTap: onTap,
);
},
);
Expand Down
44 changes: 44 additions & 0 deletions lib/src/features/borrowers/views/borrowers_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:flutter/material.dart';
import 'package:librarian_app/src/features/borrowers/data/borrowers_view_model.dart';

import '../data/borrower_model.dart';
import '../presentation/borrowers_list.dart';

class BorrowersView extends StatelessWidget {
const BorrowersView({
super.key,
required this.model,
required this.searchFilter,
this.onTap,
});

final BorrowersViewModel model;
final String searchFilter;
final void Function(BorrowerModel)? onTap;

@override
Widget build(BuildContext context) {
if (model.errorMessage != null) {
return Center(child: Text(model.errorMessage!));
}

if (model.isLoading) {
return const Center(child: CircularProgressIndicator());
}

final borrowers = model.filtered(searchFilter);

if (borrowers.isEmpty) {
return const Center(child: Text('No results found'));
}

return BorrowersList(
borrowers: model.filtered(searchFilter),
selected: model.selectedBorrower,
onTap: (borrower) {
model.selectedBorrower = borrower;
onTap?.call(borrower);
},
);
}
}
31 changes: 5 additions & 26 deletions lib/src/features/loans/presentation/connected_loans_list.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:librarian_app/src/features/loans/presentation/loans_list.dart';
import 'package:librarian_app/src/features/loans/views/loans_view.dart';
import 'package:provider/provider.dart';

import '../data/loan_model.dart';
Expand All @@ -19,31 +19,10 @@ class ConnectedLoansList extends StatelessWidget {
Widget build(BuildContext context) {
return Consumer<LoansViewModel>(
builder: (context, model, child) {
if (model.errorMessage != null) {
return Center(child: Text(model.errorMessage!));
}

if (model.isLoading) {
return const Center(child: CircularProgressIndicator());
}

var localLoans = model.loans;

if (localLoans.isNotEmpty && filter != null && filter!.isNotEmpty) {
localLoans = localLoans.where((loan) {
final borrowerName = loan.borrower.name.toLowerCase();
final filterText = filter!.toLowerCase();
return borrowerName.contains(filterText);
}).toList();
}

return LoansList(
loans: localLoans,
selected: model.selectedLoan,
onTap: (l) {
model.selectedLoan = l;
onTap?.call(l);
},
return LoansView(
model: model,
searchFilter: filter ?? '',
onTap: onTap,
);
},
);
Expand Down
11 changes: 6 additions & 5 deletions lib/src/features/loans/presentation/loans_desktop_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:librarian_app/src/features/common/widgets/dashboard/pane_header.dart';
import 'package:librarian_app/src/features/common/widgets/search_field.dart';
import 'package:librarian_app/src/features/loans/presentation/loan_details_pane.dart';
import 'package:librarian_app/src/features/loans/presentation/loans_list.dart';
import 'package:librarian_app/src/features/loans/views/loans_view.dart';
import 'package:provider/provider.dart';

import '../data/loans_view_model.dart';
Expand Down Expand Up @@ -40,10 +40,11 @@ class _LoansDesktopLayoutState extends State<LoansDesktopLayout> {
},
),
),
LoansList(
loans: model.filtered(_searchFilter),
selected: model.selectedLoan,
onTap: (loan) => model.selectedLoan = loan,
Expanded(
child: LoansView(
model: model,
searchFilter: _searchFilter,
),
),
],
);
Expand Down
44 changes: 44 additions & 0 deletions lib/src/features/loans/views/loans_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:flutter/material.dart';

import '../data/loan_model.dart';
import '../data/loans_view_model.dart';
import '../presentation/loans_list.dart';

class LoansView extends StatelessWidget {
const LoansView({
super.key,
required this.model,
required this.searchFilter,
this.onTap,
});

final LoansViewModel model;
final String searchFilter;
final void Function(LoanModel)? onTap;

@override
Widget build(BuildContext context) {
if (model.errorMessage != null) {
return Center(child: Text(model.errorMessage!));
}

if (model.isLoading) {
return const Center(child: CircularProgressIndicator());
}

final loans = model.filtered(searchFilter);

if (loans.isEmpty) {
return const Center(child: Text('No results found'));
}

return LoansList(
loans: loans,
selected: model.selectedLoan,
onTap: (loan) {
model.selectedLoan = loan;
onTap?.call(loan);
},
);
}
}

1 comment on commit 4256d08

@vercel
Copy link

@vercel vercel bot commented on 4256d08 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

librarian-app – ./

librarian-app.vercel.app
librarian-app-pvdthings.vercel.app
librarian-app-git-main-pvdthings.vercel.app

Please sign in to comment.