Skip to content

Commit

Permalink
feat: Add invoice expiry countdown to lighting qr code
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed May 28, 2024
1 parent 625f942 commit cdcdc38
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
52 changes: 52 additions & 0 deletions mobile/lib/common/countdown.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'dart:async';

import 'package:flutter/material.dart';

class Countdown extends StatefulWidget {
final int start;

const Countdown({super.key, required this.start});

@override
CountdownState createState() => CountdownState();
}

class CountdownState extends State<Countdown> {
Timer? _timer;
int _start = 0;

@override
void initState() {
super.initState();
_start = widget.start;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
if (_start > 0) {
_start--;
} else {
_timer?.cancel();
}
});
});
}

@override
void dispose() {
_timer?.cancel();
super.dispose();
}

String get _formattedTime {
final minutes = _start ~/ 60;
final seconds = _start % 60;
return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
}

@override
Widget build(BuildContext context) {
return Text(
_formattedTime,
style: const TextStyle(fontSize: 14),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_10101/common/bitcoin_balance_field.dart';
import 'package:get_10101/common/color.dart';
import 'package:get_10101/common/countdown.dart';
import 'package:get_10101/common/custom_qr_code.dart';
import 'package:get_10101/common/domain/funding_channel_task.dart';
import 'package:get_10101/common/domain/model.dart';
Expand Down Expand Up @@ -60,6 +61,9 @@ class ChannelFunding extends StatefulWidget {
class _ChannelFunding extends State<ChannelFunding> {
FundingType selectedBox = FundingType.onchain;

// TODO(holzeis): It would be nicer if this would come directly from the invoice.
final expiry = DateTime.timestamp().second + 300;

@override
Widget build(BuildContext context) {
final status = context.watch<FundingChannelChangeNotifier>().status;
Expand Down Expand Up @@ -206,12 +210,24 @@ class _ChannelFunding extends State<ChannelFunding> {
"Copied: $qrCodeSubTitle");
});
},
child: Text(
truncateWithEllipsis(44, qrCodeSubTitle),
style: const TextStyle(fontSize: 14),
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: Column(
children: [
Text(
truncateWithEllipsis(44, qrCodeSubTitle),
style: const TextStyle(fontSize: 14),
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
if (selectedBox == FundingType.lightning)
Padding(
padding: const EdgeInsets.only(top: 2),
child: Countdown(
start: expiry > DateTime.timestamp().second
? expiry - DateTime.timestamp().second
: 0),
),
],
),
),
),
Expand Down

0 comments on commit cdcdc38

Please sign in to comment.