stack_wallet/lib/widgets/crypto_notifications.dart

112 lines
2.8 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* 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
*
*/
2023-05-09 17:54:15 +00:00
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';
2023-05-09 17:54:15 +00:00
abstract class CryptoNotificationsEventBus {
static final instance = EventBus();
}
class CryptoNotificationEvent {
final String title;
final String walletId;
final String walletName;
final DateTime date;
final bool shouldWatchForUpdates;
2024-05-15 21:20:45 +00:00
final CryptoCurrency coin;
2023-05-09 17:54:15 +00:00
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({
2024-05-15 21:20:45 +00:00
super.key,
2023-05-09 17:54:15 +00:00
required this.child,
2024-05-15 21:20:45 +00:00
});
2023-05-09 17:54:15 +00:00
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,
2024-05-15 21:20:45 +00:00
coinName: event.coin.identifier,
2023-05-09 17:54:15 +00:00
txid: event.txid,
confirmations: event.confirmations,
requiredConfirmations: event.requiredConfirmations,
changeNowId: event.changeNowId,
payload: event.payload,
);
}
@override
void initState() {
2023-07-25 18:12:10 +00:00
NotificationApi.prefs = ref.read(prefsChangeNotifierProvider);
2023-07-25 19:48:19 +00:00
NotificationApi.notificationsService = ref.read(notificationsProvider);
2023-05-09 17:54:15 +00:00
_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) {
2023-05-09 23:16:12 +00:00
return widget.child;
2023-05-09 17:54:15 +00:00
}
}