zwallet/lib/rescan.dart

98 lines
3.1 KiB
Dart
Raw Normal View History

2022-03-31 22:21:28 -07:00
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:warp_api/warp_api.dart';
import 'generated/l10n.dart';
import 'main.dart';
final rescanKey = GlobalKey<RescanFormState>();
Future<int?> rescanDialog(BuildContext context) async {
final approved = await showDialog<bool>(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: Text(S.of(context).rescanFrom),
content: RescanForm(key: rescanKey),
actions: confirmButtons(
context, () => Navigator.of(context).pop(true),
cancelValue: false))) ??
false;
if (approved) {
final date = rescanKey.currentState!.startDate;
2022-09-04 04:21:12 -07:00
final heightText = rescanKey.currentState!.heightController.text;
2022-04-15 23:51:13 -07:00
final approved = await confirmWifi(context);
if (approved) {
showSnackBar(S.of(context).rescanning, autoClose: true);
2022-09-04 04:21:12 -07:00
final height = int.tryParse(heightText);
if (height != null) return height;
final height2 = await WarpApi.getBlockHeightByTime(date);
return height2;
2022-04-15 23:51:13 -07:00
}
2022-03-31 22:21:28 -07:00
}
return null;
}
class RescanForm extends StatefulWidget {
RescanForm({Key? key}) : super(key: key);
@override
RescanFormState createState() => RescanFormState();
}
class RescanFormState extends State<RescanForm> {
2022-06-07 10:00:08 -07:00
DateTime minDate = WarpApi.getActivationDate();
2022-03-31 22:21:28 -07:00
DateTime maxDate = DateTime.now();
late DateTime startDate;
2022-09-04 04:21:12 -07:00
var heightController = TextEditingController();
2022-03-31 22:21:28 -07:00
@override
void initState() {
super.initState();
startDate = minDate;
}
@override
Widget build(BuildContext context) {
final dateFormat = DateFormat('yyyy-MM-dd');
return SingleChildScrollView(
2022-09-04 04:21:12 -07:00
child: Column(children: [
OutlinedButton(onPressed: _showDatePicker, child: Text(dateFormat.format(startDate))),
Padding(padding: EdgeInsets.symmetric(vertical: 8), child: Text('OR')),
TextFormField(
decoration: InputDecoration(labelText: S.of(context).height),
keyboardType: TextInputType.number,
controller: heightController,
),
]));
2022-03-31 22:21:28 -07:00
}
_showDatePicker() async {
final date = await showDatePicker(context: context, firstDate: minDate, initialDate: startDate, lastDate: maxDate);
if (date != null) {
setState(() {
startDate = date;
});
}
}
}
Future<bool> confirmWifi(BuildContext context) async {
if (!isMobile()) return true;
final connectivity = await Connectivity().checkConnectivity();
if (connectivity == ConnectivityResult.mobile) {
return await showDialog<bool?>(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: Text(S.of(context).rescan),
content: Text(
S.of(context).mobileCharges),
actions: confirmButtons(
context, () => Navigator.of(context).pop(true),
cancelValue: false))) ??
false;
}
return true;
}