Cw 514 add sort functionality for addressbook mywallets and contacts (#1309)

* add sort function to contact list

* fix UI

* prevent duplicate contact names

* dispose contact source subscription

* fix custom order issue

* update the address book UI

* fix saving custom order

* fix merge conflict issue

* review fixes [skip ci]

* revert to single scroll for entire page

* tabBarView address book

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
Serhii
2024-11-07 03:26:14 +02:00
committed by GitHub
co-authored by Omar Hatem
parent 459f0d352d
commit 109d9b458e
38 changed files with 588 additions and 236 deletions
+310 -78
View File
@@ -1,21 +1,24 @@
import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/entities/contact_base.dart';
import 'package:cake_wallet/entities/contact_record.dart';
import 'package:cake_wallet/entities/wallet_list_order_types.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/screens/dashboard/widgets/filter_list_widget.dart';
import 'package:cake_wallet/src/screens/wallet_list/filtered_list.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/src/widgets/standard_list.dart';
import 'package:cake_wallet/themes/extensions/cake_text_theme.dart';
import 'package:cake_wallet/themes/extensions/exchange_page_theme.dart';
import 'package:cake_wallet/utils/show_bar.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart';
import 'package:cake_wallet/src/widgets/collapsible_standart_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
class ContactListPage extends BasePage {
ContactListPage(this.contactListViewModel, this.authService);
@@ -74,45 +77,101 @@ class ContactListPage extends BasePage {
}
@override
Widget body(BuildContext context) {
return Container(
padding: EdgeInsets.all(20.0),
child: Observer(builder: (_) {
final contacts = contactListViewModel.contactsToShow;
final walletContacts = contactListViewModel.walletContactsToShow;
return CollapsibleSectionList(
sectionCount: 2,
sectionTitleBuilder: (int sectionIndex) {
var title = S.current.contact_list_contacts;
Widget body(BuildContext context) => ContactPageBody(contactListViewModel: contactListViewModel);
}
if (sectionIndex == 0) {
title = S.current.contact_list_wallets;
}
class ContactPageBody extends StatefulWidget {
const ContactPageBody({required this.contactListViewModel});
return Container(
padding: EdgeInsets.only(bottom: 10),
child: Text(title, style: TextStyle(fontSize: 36)));
},
itemCounter: (int sectionIndex) =>
sectionIndex == 0 ? walletContacts.length : contacts.length,
itemBuilder: (int sectionIndex, index) {
if (sectionIndex == 0) {
final walletInfo = walletContacts[index];
return generateRaw(context, walletInfo);
}
final ContactListViewModel contactListViewModel;
final contact = contacts[index];
final content = generateRaw(context, contact);
return contactListViewModel.isEditable
? Slidable(
key: Key('${contact.key}'),
endActionPane: _actionPane(context, contact),
child: content,
)
: content;
},
);
}));
@override
State<ContactPageBody> createState() => _ContactPageBodyState();
}
class _ContactPageBodyState extends State<ContactPageBody> with SingleTickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 2, vsync: this);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 24),
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: TabBar(
controller: _tabController,
splashFactory: NoSplash.splashFactory,
indicatorSize: TabBarIndicatorSize.label,
isScrollable: true,
labelStyle: TextStyle(
fontSize: 18,
fontFamily: 'Lato',
fontWeight: FontWeight.w600,
color: Theme.of(context).appBarTheme.titleTextStyle!.color,
),
unselectedLabelStyle: TextStyle(
fontSize: 18,
fontFamily: 'Lato',
fontWeight: FontWeight.w600,
color: Theme.of(context).appBarTheme.titleTextStyle!.color?.withOpacity(0.5)),
labelColor: Theme.of(context).appBarTheme.titleTextStyle!.color,
indicatorColor: Theme.of(context).appBarTheme.titleTextStyle!.color,
indicatorPadding: EdgeInsets.zero,
labelPadding: EdgeInsets.only(right: 24),
tabAlignment: TabAlignment.center,
dividerColor: Colors.transparent,
padding: EdgeInsets.zero,
tabs: [
Tab(text: S.of(context).wallets),
Tab(text: S.of(context).contact_list_contacts),
],
),
),
Expanded(
child: TabBarView(
controller: _tabController,
children: [
_buildWalletContacts(context),
ContactListBody(
contactListViewModel: widget.contactListViewModel,
tabController: _tabController),
],
),
),
],
),
);
}
Widget _buildWalletContacts(BuildContext context) {
final walletContacts = widget.contactListViewModel.walletContactsToShow;
return ListView.builder(
shrinkWrap: true,
itemCount: walletContacts.length * 2,
itemBuilder: (context, index) {
if (index.isOdd) {
return StandardListSeparator();
} else {
final walletInfo = walletContacts[index ~/ 2];
return generateRaw(context, walletInfo);
}
},
);
}
Widget generateRaw(BuildContext context, ContactBase contact) {
@@ -123,7 +182,7 @@ class ContactListPage extends BasePage {
return GestureDetector(
onTap: () async {
if (!contactListViewModel.isEditable) {
if (!widget.contactListViewModel.isEditable) {
Navigator.of(context).pop(contact);
return;
}
@@ -143,8 +202,7 @@ class ContactListPage extends BasePage {
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
currencyIcon,
Expanded(
child: Padding(
Padding(
padding: EdgeInsets.only(left: 12),
child: Text(
contact.name,
@@ -154,13 +212,215 @@ class ContactListPage extends BasePage {
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
),
),
))
),
],
),
),
);
}
Future<bool> showNameAndAddressDialog(BuildContext context, String name, String address) async {
return await showPopUp<bool>(
context: context,
builder: (BuildContext context) {
return AlertWithTwoActions(
alertTitle: name,
alertContent: address,
rightButtonText: S.of(context).copy,
leftButtonText: S.of(context).cancel,
actionRightButton: () => Navigator.of(context).pop(true),
actionLeftButton: () => Navigator.of(context).pop(false));
}) ??
false;
}
}
class ContactListBody extends StatefulWidget {
ContactListBody({required this.contactListViewModel, required this.tabController});
final ContactListViewModel contactListViewModel;
final TabController tabController;
@override
State<ContactListBody> createState() => _ContactListBodyState();
}
class _ContactListBodyState extends State<ContactListBody> {
bool _isContactsTabActive = false;
@override
void initState() {
super.initState();
widget.tabController.addListener(_handleTabChange);
}
void _handleTabChange() {
setState(() {
_isContactsTabActive = widget.tabController.index == 1;
});
}
@override
void dispose() {
widget.tabController.removeListener(_handleTabChange);
super.dispose();
}
@override
Widget build(BuildContext context) {
final contacts = widget.contactListViewModel.contacts;
return Scaffold(
body: Container(
child: FilteredList(
list: contacts,
updateFunction: widget.contactListViewModel.reorderAccordingToContactList,
canReorder: widget.contactListViewModel.isEditable,
shrinkWrap: true,
itemBuilder: (context, index) {
final contact = contacts[index];
final contactContent =
generateContactRaw(context, contact, contacts.length == index + 1);
return GestureDetector(
key: Key('${contact.name}'),
onTap: () async {
if (!widget.contactListViewModel.isEditable) {
Navigator.of(context).pop(contact);
return;
}
final isCopied =
await showNameAndAddressDialog(context, contact.name, contact.address);
if (isCopied) {
await Clipboard.setData(ClipboardData(text: contact.address));
await showBar<void>(context, S.of(context).copied_to_clipboard);
}
},
behavior: HitTestBehavior.opaque,
child: widget.contactListViewModel.isEditable
? Slidable(
key: Key('${contact.key}'),
endActionPane: _actionPane(context, contact),
child: contactContent)
: contactContent,
);
},
),
),
floatingActionButton:
_isContactsTabActive ? filterButtonWidget(context, widget.contactListViewModel) : null,
);
}
Widget generateContactRaw(BuildContext context, ContactRecord contact, bool isLast) {
final image = contact.type.iconPath;
final currencyIcon = image != null
? Image.asset(image, height: 24, width: 24)
: const SizedBox(height: 24, width: 24);
return Column(
children: [
Container(
key: Key('${contact.name}'),
padding: const EdgeInsets.only(top: 16, bottom: 16, right: 24),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
currencyIcon,
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 12),
child: Text(
contact.name,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
),
),
))
],
),
),
StandardListSeparator()
],
);
}
ActionPane _actionPane(BuildContext context, ContactRecord contact) => ActionPane(
motion: const ScrollMotion(),
extentRatio: 0.4,
children: [
SlidableAction(
onPressed: (_) async => await Navigator.of(context)
.pushNamed(Routes.addressBookAddContact, arguments: contact),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
icon: Icons.edit,
label: S.of(context).edit,
),
SlidableAction(
onPressed: (_) async {
final isDelete = await showAlertDialog(context);
if (isDelete) {
await widget.contactListViewModel.delete(contact);
}
},
backgroundColor: Colors.red,
foregroundColor: Colors.white,
icon: CupertinoIcons.delete,
label: S.of(context).delete,
),
],
);
Widget filterButtonWidget(BuildContext context, ContactListViewModel contactListViewModel) {
final filterIcon = Image.asset('assets/images/filter_icon.png',
color: Theme.of(context).appBarTheme.titleTextStyle!.color);
return MergeSemantics(
child: SizedBox(
height: 58,
width: 58,
child: ButtonTheme(
minWidth: double.minPositive,
child: Semantics(
container: true,
child: GestureDetector(
onTap: () async {
await showPopUp<void>(
context: context,
builder: (context) => FilterListWidget(
initalType: contactListViewModel.orderType,
initalAscending: contactListViewModel.ascending,
onClose: (bool ascending, FilterListOrderType type) async {
contactListViewModel.setAscending(ascending);
await contactListViewModel.setOrderType(type);
},
),
);
},
child: Semantics(
label: 'Transaction Filter',
button: true,
enabled: true,
child: Container(
height: 36,
width: 36,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).extension<ExchangePageTheme>()!.buttonBackgroundColor,
),
child: filterIcon,
),
),
),
),
),
),
);
}
Future<bool> showAlertDialog(BuildContext context) async {
return await showPopUp<bool>(
context: context,
@@ -190,32 +450,4 @@ class ContactListPage extends BasePage {
}) ??
false;
}
ActionPane _actionPane(BuildContext context, ContactRecord contact) => ActionPane(
motion: const ScrollMotion(),
extentRatio: 0.4,
children: [
SlidableAction(
onPressed: (_) async => await Navigator.of(context)
.pushNamed(Routes.addressBookAddContact, arguments: contact),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
icon: Icons.edit,
label: S.of(context).edit,
),
SlidableAction(
onPressed: (_) async {
final isDelete = await showAlertDialog(context);
if (isDelete) {
await contactListViewModel.delete(contact);
}
},
backgroundColor: Colors.red,
foregroundColor: Colors.white,
icon: CupertinoIcons.delete,
label: S.of(context).delete,
),
],
);
}
@@ -18,9 +18,9 @@ class FilterListWidget extends StatefulWidget {
required this.onClose,
});
final WalletListOrderType? initalType;
final FilterListOrderType? initalType;
final bool initalAscending;
final Function(bool, WalletListOrderType) onClose;
final Function(bool, FilterListOrderType) onClose;
@override
FilterListWidgetState createState() => FilterListWidgetState();
@@ -28,7 +28,7 @@ class FilterListWidget extends StatefulWidget {
class FilterListWidgetState extends State<FilterListWidget> {
late bool ascending;
late WalletListOrderType? type;
late FilterListOrderType? type;
@override
void initState() {
@@ -37,7 +37,7 @@ class FilterListWidgetState extends State<FilterListWidget> {
type = widget.initalType;
}
void setSelectedOrderType(WalletListOrderType? orderType) {
void setSelectedOrderType(FilterListOrderType? orderType) {
setState(() {
type = orderType;
});
@@ -72,7 +72,7 @@ class FilterListWidgetState extends State<FilterListWidget> {
),
),
),
if (type != WalletListOrderType.Custom) ...[
if (type != FilterListOrderType.Custom) ...[
sectionDivider,
SettingsChoicesCell(
ChoicesListItem<ListOrderMode>(
@@ -89,10 +89,10 @@ class FilterListWidgetState extends State<FilterListWidget> {
],
sectionDivider,
RadioListTile(
value: WalletListOrderType.CreationDate,
value: FilterListOrderType.CreationDate,
groupValue: type,
title: Text(
WalletListOrderType.CreationDate.toString(),
FilterListOrderType.CreationDate.toString(),
style: TextStyle(
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
fontSize: 16,
@@ -104,10 +104,10 @@ class FilterListWidgetState extends State<FilterListWidget> {
activeColor: Theme.of(context).primaryColor,
),
RadioListTile(
value: WalletListOrderType.Alphabetical,
value: FilterListOrderType.Alphabetical,
groupValue: type,
title: Text(
WalletListOrderType.Alphabetical.toString(),
FilterListOrderType.Alphabetical.toString(),
style: TextStyle(
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
fontSize: 16,
@@ -119,10 +119,10 @@ class FilterListWidgetState extends State<FilterListWidget> {
activeColor: Theme.of(context).primaryColor,
),
RadioListTile(
value: WalletListOrderType.GroupByType,
value: FilterListOrderType.GroupByType,
groupValue: type,
title: Text(
WalletListOrderType.GroupByType.toString(),
FilterListOrderType.GroupByType.toString(),
style: TextStyle(
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
fontSize: 16,
@@ -134,10 +134,10 @@ class FilterListWidgetState extends State<FilterListWidget> {
activeColor: Theme.of(context).primaryColor,
),
RadioListTile(
value: WalletListOrderType.Custom,
value: FilterListOrderType.Custom,
groupValue: type,
title: Text(
WalletListOrderType.Custom.toString(),
FilterListOrderType.Custom.toString(),
style: TextStyle(
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
fontSize: 16,
+30 -16
View File
@@ -7,13 +7,17 @@ class FilteredList extends StatefulWidget {
required this.list,
required this.itemBuilder,
required this.updateFunction,
this.canReorder = true,
this.shrinkWrap = false,
this.physics,
});
final ObservableList<dynamic> list;
final Widget Function(BuildContext, int) itemBuilder;
final Function updateFunction;
final bool canReorder;
final bool shrinkWrap;
final ScrollPhysics? physics;
@override
FilteredListState createState() => FilteredListState();
@@ -22,21 +26,31 @@ class FilteredList extends StatefulWidget {
class FilteredListState extends State<FilteredList> {
@override
Widget build(BuildContext context) {
return Observer(
builder: (_) => ReorderableListView.builder(
shrinkWrap: widget.shrinkWrap,
physics: const BouncingScrollPhysics(),
itemBuilder: widget.itemBuilder,
itemCount: widget.list.length,
onReorder: (int oldIndex, int newIndex) {
if (oldIndex < newIndex) {
newIndex -= 1;
}
final dynamic item = widget.list.removeAt(oldIndex);
widget.list.insert(newIndex, item);
widget.updateFunction();
},
),
);
if (widget.canReorder) {
return Observer(
builder: (_) => ReorderableListView.builder(
shrinkWrap: widget.shrinkWrap,
physics: widget.physics ?? const BouncingScrollPhysics(),
itemBuilder: widget.itemBuilder,
itemCount: widget.list.length,
onReorder: (int oldIndex, int newIndex) {
if (oldIndex < newIndex) {
newIndex -= 1;
}
final dynamic item = widget.list.removeAt(oldIndex);
widget.list.insert(newIndex, item);
widget.updateFunction();
},
),
);
} else {
return Observer(
builder: (_) => ListView.builder(
physics: widget.physics ?? const BouncingScrollPhysics(),
itemBuilder: widget.itemBuilder,
itemCount: widget.list.length,
),
);
}
}
}
@@ -59,7 +59,7 @@ class WalletListPage extends BasePage {
builder: (context) => FilterListWidget(
initalType: walletListViewModel.orderType,
initalAscending: walletListViewModel.ascending,
onClose: (bool ascending, WalletListOrderType type) async {
onClose: (bool ascending, FilterListOrderType type) async {
walletListViewModel.setAscending(ascending);
await walletListViewModel.setOrderType(type);
},
@@ -1,39 +0,0 @@
import 'package:cake_wallet/src/widgets/standard_list.dart';
import 'package:flutter/material.dart';
class CollapsibleSectionList extends SectionStandardList {
CollapsibleSectionList(
{required int sectionCount,
required int Function(int sectionIndex) itemCounter,
required Widget Function(int sectionIndex, int itemIndex) itemBuilder,
Widget Function(int sectionIndex)? sectionTitleBuilder,
bool hasTopSeparator = false})
: super(
hasTopSeparator: hasTopSeparator,
sectionCount: sectionCount,
itemCounter: itemCounter,
itemBuilder: itemBuilder,
sectionTitleBuilder: sectionTitleBuilder);
@override
Widget buildTitle(List<Widget> items, int sectionIndex) {
if (sectionTitleBuilder == null) {
throw Exception('Cannot to build title. sectionTitleBuilder is null');
}
return sectionTitleBuilder!.call(sectionIndex);
}
@override
List<Widget> buildSection(int itemCount, List<Widget> items, int sectionIndex) {
final List<Widget> section = [];
for (var itemIndex = 0; itemIndex < itemCount; itemIndex++) {
final item = itemBuilder(sectionIndex, itemIndex);
section.add(StandardListSeparator());
section.add(item);
}
return section;
}
}