stack_wallet/lib/widgets/crypto_notifications.dart

111 lines
2.8 KiB
Dart

/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/providers.dart';
import '../services/notifications_api.dart';
import '../themes/coin_icon_provider.dart';
import '../wallets/crypto_currency/crypto_currency.dart';
abstract class CryptoNotificationsEventBus {
static final instance = EventBus();
}
class CryptoNotificationEvent {
final String title;
final String walletId;
final String walletName;
final DateTime date;
final bool shouldWatchForUpdates;
final CryptoCurrency coin;
final String? txid;
final int? confirmations;
final int? requiredConfirmations;
final String? changeNowId;
final String? payload;
CryptoNotificationEvent({
required this.title,
required this.walletId,
required this.walletName,
required this.date,
required this.shouldWatchForUpdates,
required this.coin,
this.txid,
this.confirmations,
this.requiredConfirmations,
this.changeNowId,
this.payload,
});
}
class CryptoNotifications extends ConsumerStatefulWidget {
const CryptoNotifications({
super.key,
required this.child,
});
final Widget child;
@override
ConsumerState<CryptoNotifications> createState() =>
_CryptoNotificationsState();
}
class _CryptoNotificationsState extends ConsumerState<CryptoNotifications> {
late final StreamSubscription<dynamic>? _streamSubscription;
Future<void> _showNotification(CryptoNotificationEvent event) async {
await NotificationApi.showNotification(
title: event.title,
body: event.walletName,
walletId: event.walletId,
iconAssetName: ref.read(coinIconProvider(event.coin)),
date: event.date,
shouldWatchForUpdates: event.shouldWatchForUpdates,
coinName: event.coin.identifier,
txid: event.txid,
confirmations: event.confirmations,
requiredConfirmations: event.requiredConfirmations,
changeNowId: event.changeNowId,
payload: event.payload,
);
}
@override
void initState() {
NotificationApi.prefs = ref.read(prefsChangeNotifierProvider);
NotificationApi.notificationsService = ref.read(notificationsProvider);
_streamSubscription = CryptoNotificationsEventBus.instance
.on<CryptoNotificationEvent>()
.listen(
(event) async {
unawaited(_showNotification(event));
},
);
super.initState();
}
@override
void dispose() {
_streamSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}