gecko/lib/screens/activity.dart

366 lines
13 KiB
Dart
Raw Normal View History

2022-09-05 04:15:27 +02:00
// ignore_for_file: must_be_immutable
2022-06-18 01:50:06 +02:00
import 'package:easy_localization/easy_localization.dart';
2021-11-30 10:28:13 +01:00
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
import 'package:gecko/models/queries_indexer.dart';
import 'package:gecko/models/widgets_keys.dart';
import 'package:gecko/providers/cesium_plus.dart';
import 'package:gecko/providers/duniter_indexer.dart';
import 'package:gecko/providers/home.dart';
2022-05-28 00:17:50 +02:00
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:gecko/providers/wallets_profiles.dart';
2021-11-30 10:28:13 +01:00
import 'package:flutter/material.dart';
import 'package:gecko/screens/wallet_view.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:provider/provider.dart';
class ActivityScreen extends StatelessWidget with ChangeNotifier {
ActivityScreen({required this.address, required this.avatar, this.username})
: super(key: keyActivityScreen);
2021-11-30 10:28:13 +01:00
final ScrollController scrollController = ScrollController();
final double avatarsSize = 80;
final String address;
2021-12-23 12:36:09 +01:00
final String? username;
final Image avatar;
2021-11-30 10:28:13 +01:00
2021-12-23 12:36:09 +01:00
FetchMore? fetchMore;
FetchMoreOptions? opts;
2021-11-30 10:28:13 +01:00
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
WalletsProfilesProvider walletProfile =
Provider.of<WalletsProfilesProvider>(context, listen: false);
HomeProvider homeProvider =
Provider.of<HomeProvider>(context, listen: false);
2021-12-01 08:14:07 +01:00
2021-11-30 10:28:13 +01:00
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
elevation: 0,
toolbarHeight: 60 * ratio,
2022-06-18 01:50:06 +02:00
title: SizedBox(
2021-11-30 10:28:13 +01:00
height: 22,
2022-06-18 01:50:06 +02:00
child: Text('accountActivity'.tr()),
2021-11-30 10:28:13 +01:00
),
),
bottomNavigationBar: homeProvider.bottomAppBar(context),
2021-11-30 10:28:13 +01:00
body: Column(children: <Widget>[
walletProfile.headerProfileView(context, address, username),
historyQuery(context),
2021-11-30 10:28:13 +01:00
]));
}
Widget historyQuery(context) {
DuniterIndexer duniterIndexer =
Provider.of<DuniterIndexer>(context, listen: false);
if (indexerEndpoint == '') {
2022-06-18 01:50:06 +02:00
Column(children: <Widget>[
const SizedBox(height: 50),
Text(
2022-06-18 01:50:06 +02:00
"noNetworkNoHistory".tr(),
textAlign: TextAlign.center,
2022-06-18 01:50:06 +02:00
style: const TextStyle(fontSize: 18),
)
]);
}
final httpLink = HttpLink(
'$indexerEndpoint/v1beta1/relay',
);
final client = ValueNotifier(
GraphQLClient(
cache: GraphQLCache(),
link: httpLink,
),
);
2021-11-30 10:28:13 +01:00
return GraphQLProvider(
client: client,
child: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Query(
options: QueryOptions(
document: gql(getHistoryByAddressQ),
variables: <String, dynamic>{
'address': address,
'number': 20,
'cursor': null
},
),
builder: (QueryResult result, {fetchMore, refetch}) {
if (result.isLoading && result.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (result.hasException) {
log.e('Error Indexer: ${result.exception}');
2022-06-18 01:50:06 +02:00
return Column(children: <Widget>[
const SizedBox(height: 50),
Text(
2022-06-18 01:50:06 +02:00
"noNetworkNoHistory".tr(),
textAlign: TextAlign.center,
2022-06-18 01:50:06 +02:00
style: const TextStyle(fontSize: 18),
)
]);
} else if (result
.data?['transaction_connection']?['edges'].isEmpty) {
2022-06-18 01:50:06 +02:00
return Column(children: <Widget>[
const SizedBox(height: 50),
Text(
2022-06-18 01:50:06 +02:00
"noDataToDisplay".tr(),
style: const TextStyle(fontSize: 18),
)
]);
}
2021-11-30 10:28:13 +01:00
if (result.isNotLoading) {
// log.d(result.data);
opts = duniterIndexer.checkQueryResult(result, opts, address);
}
2021-11-30 10:28:13 +01:00
// Build history list
return NotificationListener(
child: Builder(
builder: (context) => Expanded(
child: ListView(
key: keyListTransactions,
controller: scrollController,
children: <Widget>[historyView(context, result)],
),
2021-11-30 10:28:13 +01:00
),
),
onNotification: (dynamic t) {
if (t is ScrollEndNotification &&
scrollController.position.pixels >=
scrollController.position.maxScrollExtent * 0.7 &&
duniterIndexer.pageInfo!['hasNextPage'] &&
result.isNotLoading) {
fetchMore!(opts!);
}
return true;
});
},
),
],
)),
);
2021-11-30 10:28:13 +01:00
}
Widget historyView(context, result) {
DuniterIndexer duniterIndexer =
Provider.of<DuniterIndexer>(context, listen: false);
2021-11-30 10:28:13 +01:00
return duniterIndexer.transBC == null
2022-06-18 01:50:06 +02:00
? Column(children: <Widget>[
const SizedBox(height: 50),
2021-12-01 08:14:07 +01:00
Text(
2022-06-18 01:50:06 +02:00
"noTransactionToDisplay".tr(),
style: const TextStyle(fontSize: 18),
2021-12-01 08:14:07 +01:00
)
])
2021-11-30 10:28:13 +01:00
: Column(children: <Widget>[
getTransactionTile(context, duniterIndexer),
if (result.isLoading && duniterIndexer.pageInfo!['hasPreviousPage'])
2021-11-30 10:28:13 +01:00
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
CircularProgressIndicator(),
],
),
if (!duniterIndexer.pageInfo!['hasNextPage'])
2021-11-30 10:28:13 +01:00
Column(
children: const <Widget>[
SizedBox(height: 15),
Text("Début de l'historique.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20)),
SizedBox(height: 15)
],
)
]);
}
Widget getTransactionTile(
BuildContext context, DuniterIndexer duniterIndexer) {
CesiumPlusProvider cesiumPlusProvider =
2021-11-30 10:28:13 +01:00
Provider.of<CesiumPlusProvider>(context, listen: false);
2022-09-11 13:14:52 +02:00
2021-11-30 10:28:13 +01:00
int keyID = 0;
2021-12-23 12:36:09 +01:00
String? dateDelimiter;
String? lastDateDelimiter;
const double avatarSize = 200;
2021-11-30 10:28:13 +01:00
2021-12-01 08:14:07 +01:00
bool isTody = false;
bool isYesterday = false;
bool isThisWeek = false;
2022-06-18 01:50:06 +02:00
final Map<int, String> monthsInYear = {
1: "month1".tr(),
2: "month2".tr(),
3: "month3".tr(),
4: "month4".tr(),
5: "month5".tr(),
6: "month6".tr(),
7: "month7".tr(),
8: "month8".tr(),
9: "month9".tr(),
10: "month10".tr(),
11: "month11".tr(),
12: "month12".tr()
2021-11-30 10:28:13 +01:00
};
return Column(
children: duniterIndexer.transBC!.map((repository) {
// log.d('bbbbbbbbbbbbbbbbbbbbbb: ' + repository.toString());
2021-11-30 10:28:13 +01:00
DateTime now = DateTime.now();
DateTime date = repository[0];
2021-11-30 10:28:13 +01:00
String dateForm;
if ({4, 10, 11, 12}.contains(date.month)) {
2021-12-23 12:36:09 +01:00
dateForm = "${date.day} ${monthsInYear[date.month]!.substring(0, 3)}.";
2021-11-30 10:28:13 +01:00
} else if ({1, 2, 7, 9}.contains(date.month)) {
2021-12-23 12:36:09 +01:00
dateForm = "${date.day} ${monthsInYear[date.month]!.substring(0, 4)}.";
2021-11-30 10:28:13 +01:00
} else {
dateForm = "${date.day} ${monthsInYear[date.month]}";
}
int weekNumber(DateTime date) {
int dayOfYear = int.parse(DateFormat("D").format(date));
return ((dayOfYear - date.weekday + 10) / 7).floor();
}
final transactionDate = DateTime(date.year, date.month, date.day);
final todayDate = DateTime(now.year, now.month, now.day);
final yesterdayDate = DateTime(now.year, now.month, now.day - 1);
if (transactionDate == todayDate && !isTody) {
2022-06-18 01:50:06 +02:00
dateDelimiter = lastDateDelimiter = "today".tr();
2021-12-01 08:14:07 +01:00
isTody = true;
} else if (transactionDate == yesterdayDate && !isYesterday) {
2022-06-18 01:50:06 +02:00
dateDelimiter = lastDateDelimiter = "yesterday".tr();
2021-12-01 08:14:07 +01:00
isYesterday = true;
2021-11-30 10:28:13 +01:00
} else if (weekNumber(date) == weekNumber(now) &&
date.year == now.year &&
2022-06-18 01:50:06 +02:00
lastDateDelimiter != "thisWeek".tr() &&
transactionDate != yesterdayDate &&
transactionDate != todayDate &&
2021-12-01 08:14:07 +01:00
!isThisWeek) {
2022-06-18 01:50:06 +02:00
dateDelimiter = lastDateDelimiter = "thisWeek".tr();
2021-12-01 08:14:07 +01:00
isThisWeek = true;
2021-11-30 10:28:13 +01:00
} else if (lastDateDelimiter != monthsInYear[date.month] &&
2021-12-01 08:14:07 +01:00
lastDateDelimiter != "${monthsInYear[date.month]} ${date.year}" &&
transactionDate != todayDate &&
transactionDate != yesterdayDate &&
2021-12-01 08:14:07 +01:00
!(weekNumber(date) == weekNumber(now) && date.year == now.year)) {
2021-11-30 10:28:13 +01:00
if (date.year == now.year) {
dateDelimiter = lastDateDelimiter = monthsInYear[date.month];
} else {
dateDelimiter =
lastDateDelimiter = "${monthsInYear[date.month]} ${date.year}";
}
} else {
dateDelimiter = null;
}
2022-09-11 13:14:52 +02:00
final bool isUdUnit = configBox.get('isUdUnit') ?? false;
late double amount;
late String finalAmount;
amount = repository[4] == 'RECEIVED' ? repository[3] : repository[3] * -1;
if (isUdUnit) {
2022-09-12 12:04:08 +02:00
amount = round(amount / balanceRatio);
2022-09-11 14:04:52 +02:00
finalAmount = 'ud'.tr(args: ['$amount ']);
2022-09-11 13:14:52 +02:00
} else {
finalAmount = '$amount $currencyName';
}
2021-11-30 10:28:13 +01:00
return Column(children: <Widget>[
if (dateDelimiter != null)
Padding(
padding: const EdgeInsets.symmetric(vertical: 30),
child: Text(
2021-12-23 12:36:09 +01:00
dateDelimiter!,
2022-09-12 12:38:32 +02:00
style: const TextStyle(
2021-11-30 10:28:13 +01:00
fontSize: 23, color: orangeC, fontWeight: FontWeight.w300),
),
),
Padding(
padding: const EdgeInsets.only(right: 0),
child:
// Row(children: [Column(children: [],)],)
ListTile(
key: keyTransaction(keyID++),
2021-11-30 10:28:13 +01:00
contentPadding: const EdgeInsets.only(
left: 20, right: 30, top: 15, bottom: 15),
leading: ClipOval(
child: cesiumPlusProvider.defaultAvatar(avatarSize),
),
2021-11-30 10:28:13 +01:00
title: Padding(
padding: const EdgeInsets.only(bottom: 5),
child: Text(getShortPubkey(repository[1]),
2021-11-30 10:28:13 +01:00
style: const TextStyle(
fontSize: 18, fontFamily: 'Monospace')),
),
subtitle: RichText(
text: TextSpan(
style: TextStyle(
fontSize: 16,
color: Colors.grey[700],
),
children: <TextSpan>[
TextSpan(
text: dateForm,
),
if (repository[2] != '')
2021-11-30 10:28:13 +01:00
TextSpan(
text: ' · ',
style: TextStyle(
fontSize: 20,
color: Colors.grey[550],
),
),
TextSpan(
text: repository[2],
2021-11-30 10:28:13 +01:00
style: TextStyle(
fontStyle: FontStyle.italic,
color: Colors.grey[600],
),
),
],
),
),
2022-09-11 13:14:52 +02:00
trailing: Text(finalAmount,
2021-11-30 10:28:13 +01:00
style: const TextStyle(
fontSize: 18, fontWeight: FontWeight.w500),
textAlign: TextAlign.justify),
dense: false,
isThreeLine: false,
onTap: () {
duniterIndexer.nPage = 1;
// _cesiumPlusProvider.avatarCancelToken.cancel('cancelled');
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return WalletViewScreen(address: repository[1]);
}),
);
2021-12-01 08:14:07 +01:00
// Navigator.pop(context);
2021-11-30 10:28:13 +01:00
}),
),
]);
}).toList());
}
}