mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2025-12-20 07:24:26 +01:00
Update example app on Flutter plugin (#220)
* Update example app on Flutter plugin * Expose `empty_wallet_cache` through Dart bindings (#224)
This commit is contained in:
@@ -1,179 +1,56 @@
|
||||
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';
|
||||
import 'package:flutter_breez_liquid_example/routes/connect/connect_page.dart';
|
||||
import 'package:flutter_breez_liquid_example/routes/home/home_page.dart';
|
||||
import 'package:flutter_breez_liquid_example/services/credentials_manager.dart';
|
||||
import 'package:flutter_breez_liquid_example/services/keychain.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await initialize();
|
||||
BindingLiquidSdk liquidSDK = await initializeWallet();
|
||||
runApp(MyApp(liquidSDK));
|
||||
final credentialsManager = CredentialsManager(keyChain: KeyChain());
|
||||
final mnemonic = await credentialsManager.restoreMnemonic();
|
||||
BindingLiquidSdk? liquidSDK;
|
||||
if (mnemonic.isNotEmpty) {
|
||||
liquidSDK = await reconnect(mnemonic: mnemonic);
|
||||
}
|
||||
runApp(App(credentialsManager: credentialsManager, liquidSDK: liquidSDK));
|
||||
}
|
||||
|
||||
const String mnemonic = "";
|
||||
|
||||
Future<BindingLiquidSdk> initializeWallet() async {
|
||||
assert(mnemonic.isNotEmpty, "Please enter your mnemonic.");
|
||||
Future<BindingLiquidSdk> reconnect({
|
||||
required String mnemonic,
|
||||
Network network = Network.liquid,
|
||||
}) async {
|
||||
final dataDir = await getApplicationDocumentsDirectory();
|
||||
final req = ConnectRequest(
|
||||
mnemonic: mnemonic,
|
||||
dataDir: dataDir.path,
|
||||
network: Network.liquid,
|
||||
network: network,
|
||||
);
|
||||
return await connect(req: req);
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
final BindingLiquidSdk liquidSDK;
|
||||
class App extends StatefulWidget {
|
||||
final CredentialsManager credentialsManager;
|
||||
final BindingLiquidSdk? liquidSDK;
|
||||
const App({super.key, required this.credentialsManager, this.liquidSDK});
|
||||
|
||||
const MyApp(this.liquidSDK, {super.key});
|
||||
static const title = 'Breez Liquid SDK Demo';
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
State<App> createState() => _AppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
class _AppState extends State<App> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Breez Liquid Native Packages'),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
FutureBuilder<GetInfoResponse>(
|
||||
future: widget.liquidSDK.getInfo(
|
||||
req: const GetInfoRequest(
|
||||
withScan: true,
|
||||
),
|
||||
),
|
||||
initialData: null,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Text('Error: ${snapshot.error}');
|
||||
}
|
||||
|
||||
if (!snapshot.hasData) {
|
||||
return const Text('Loading...');
|
||||
}
|
||||
|
||||
if (snapshot.requireData.balanceSat.isNaN) {
|
||||
return const Text('No balance.');
|
||||
}
|
||||
final walletInfo = snapshot.data!;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Text(
|
||||
"Balance",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 32.0),
|
||||
child: Center(
|
||||
child: Text(
|
||||
"${walletInfo.balanceSat} sats",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
title: Text(
|
||||
"pubKey: ${walletInfo.pubkey}",
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16.0),
|
||||
FutureBuilder<PrepareReceiveResponse>(
|
||||
future: widget.liquidSDK.prepareReceivePayment(
|
||||
req: const PrepareReceiveRequest(payerAmountSat: 1000),
|
||||
),
|
||||
initialData: null,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Text('Error: ${snapshot.error}');
|
||||
}
|
||||
|
||||
if (!snapshot.hasData) {
|
||||
return const Text('Loading...');
|
||||
}
|
||||
|
||||
final prepareReceiveResponse = snapshot.data!;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Text(
|
||||
"Preparing a receive payment of 1000 sats",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Payer Amount: ${prepareReceiveResponse.payerAmountSat} (in sats)"),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Fees: ${prepareReceiveResponse.feesSat} (in sats)"),
|
||||
),
|
||||
const SizedBox(height: 16.0),
|
||||
FutureBuilder<ReceivePaymentResponse>(
|
||||
future: widget.liquidSDK.receivePayment(req: prepareReceiveResponse),
|
||||
initialData: null,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Text('Error: ${snapshot.error}');
|
||||
}
|
||||
|
||||
if (!snapshot.hasData) {
|
||||
return const Text('Loading...');
|
||||
}
|
||||
|
||||
if (snapshot.requireData.id.isEmpty) {
|
||||
return const Text('Missing invoice id');
|
||||
}
|
||||
|
||||
final receivePaymentResponse = snapshot.data!;
|
||||
debugPrint("Invoice ID: ${receivePaymentResponse.id}");
|
||||
debugPrint("Invoice: ${receivePaymentResponse.invoice}");
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Text(
|
||||
"Invoice for receive payment of 1000 sats",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Invoice ID: ${receivePaymentResponse.id}"),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Invoice: ${receivePaymentResponse.invoice}"),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
title: App.title,
|
||||
theme: ThemeData.from(colorScheme: ColorScheme.fromSeed(seedColor: Colors.white), useMaterial3: true),
|
||||
home: widget.liquidSDK == null
|
||||
? ConnectPage(credentialsManager: widget.credentialsManager)
|
||||
: HomePage(credentialsManager: widget.credentialsManager, liquidSDK: widget.liquidSDK!),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user