|
| 1 | +// Copyright (c) 2025 Sendbird, Inc. All rights reserved. |
| 2 | + |
| 3 | +import 'dart:async'; |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:provider/provider.dart'; |
| 8 | +import 'package:sendbird_uikit/sendbird_uikit.dart'; |
| 9 | +import 'package:sendbird_uikit/src/internal/component/base/sbu_base_component.dart'; |
| 10 | +import 'package:sendbird_uikit/src/internal/component/basic/sbu_text_button_component.dart'; |
| 11 | +import 'package:sendbird_uikit/src/internal/component/basic/sbu_text_component.dart'; |
| 12 | +import 'package:sendbird_uikit/src/internal/resource/sbu_text_styles.dart'; |
| 13 | +import 'package:sendbird_uikit/src/internal/utils/sbu_time_extensions.dart'; |
| 14 | + |
| 15 | +class SBUDelayedConnectingDialog extends SBUStatefulComponent { |
| 16 | + final int retryAfter; |
| 17 | + final bool showCloseButton; |
| 18 | + final void Function()? onCloseButtonClicked; |
| 19 | + final String? closeButtonText; |
| 20 | + |
| 21 | + const SBUDelayedConnectingDialog({ |
| 22 | + required this.retryAfter, |
| 23 | + this.showCloseButton = false, |
| 24 | + this.onCloseButtonClicked, |
| 25 | + this.closeButtonText, |
| 26 | + super.key, |
| 27 | + }); |
| 28 | + |
| 29 | + @override |
| 30 | + State<StatefulWidget> createState() => SBUDelayedConnectingDialogState(); |
| 31 | +} |
| 32 | + |
| 33 | +class SBUDelayedConnectingDialogState |
| 34 | + extends State<SBUDelayedConnectingDialog> { |
| 35 | + late int currentRetryAfter; |
| 36 | + late final DateTime startTime; |
| 37 | + Timer? _timer; |
| 38 | + |
| 39 | + @override |
| 40 | + void initState() { |
| 41 | + super.initState(); |
| 42 | + currentRetryAfter = widget.retryAfter; |
| 43 | + startTime = DateTime.now(); |
| 44 | + _startTimer(); |
| 45 | + } |
| 46 | + |
| 47 | + void _startTimer() { |
| 48 | + _timer = Timer.periodic(const Duration(seconds: 1), (timer) { |
| 49 | + updateRetryAfter(); |
| 50 | + if (currentRetryAfter <= 0) { |
| 51 | + _timer?.cancel(); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + void dispose() { |
| 58 | + _timer?.cancel(); |
| 59 | + super.dispose(); |
| 60 | + } |
| 61 | + |
| 62 | + void updateRetryAfter() { |
| 63 | + final now = DateTime.now(); |
| 64 | + final elapsedTime = now.difference(startTime).inMilliseconds; |
| 65 | + final updatedRetryAfter = widget.retryAfter - elapsedTime / 1000; |
| 66 | + |
| 67 | + setState(() { |
| 68 | + currentRetryAfter = updatedRetryAfter > 0 ? updatedRetryAfter.ceil() : 0; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + @override |
| 73 | + Widget build(BuildContext context) { |
| 74 | + final isLightTheme = context.watch<SBUThemeProvider>().isLight(); |
| 75 | + final strings = context.watch<SBUStringProvider>().strings; |
| 76 | + |
| 77 | + return PopScope( |
| 78 | + canPop: false, |
| 79 | + child: Container( |
| 80 | + width: double.maxFinite, |
| 81 | + height: double.maxFinite, |
| 82 | + color: SBUColors.overlayLight, |
| 83 | + child: Center( |
| 84 | + child: Container( |
| 85 | + width: 280, |
| 86 | + decoration: BoxDecoration( |
| 87 | + color: isLightTheme |
| 88 | + ? SBUColors.background50 |
| 89 | + : SBUColors.background500, |
| 90 | + borderRadius: const BorderRadius.all(Radius.circular(4)), |
| 91 | + ), |
| 92 | + child: Column( |
| 93 | + mainAxisSize: MainAxisSize.min, |
| 94 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 95 | + children: [ |
| 96 | + const SizedBox(height: 24), |
| 97 | + Padding( |
| 98 | + padding: const EdgeInsets.symmetric(horizontal: 24), |
| 99 | + child: DefaultTextStyle.merge( |
| 100 | + style: kIsWeb |
| 101 | + ? const TextStyle(decoration: TextDecoration.none) |
| 102 | + : null, |
| 103 | + child: SBUTextComponent( |
| 104 | + text: strings.youWillBeReconnectedShortly, |
| 105 | + textType: SBUTextType.heading1, |
| 106 | + textColorType: SBUTextColorType.text01, |
| 107 | + maxLines: 3, |
| 108 | + ), |
| 109 | + ), |
| 110 | + ), |
| 111 | + const SizedBox(height: 16), |
| 112 | + Padding( |
| 113 | + padding: const EdgeInsets.symmetric(horizontal: 24), |
| 114 | + child: DefaultTextStyle.merge( |
| 115 | + style: kIsWeb |
| 116 | + ? const TextStyle(decoration: TextDecoration.none) |
| 117 | + : null, |
| 118 | + child: Row( |
| 119 | + mainAxisSize: MainAxisSize.min, |
| 120 | + children: [ |
| 121 | + SBUTextComponent( |
| 122 | + text: currentRetryAfter > 0 |
| 123 | + ? strings.estimatedWaitingTime |
| 124 | + : '', |
| 125 | + textType: SBUTextType.body3, |
| 126 | + textColorType: SBUTextColorType.text02, |
| 127 | + ), |
| 128 | + const SizedBox(width: 4), |
| 129 | + SBUTextComponent( |
| 130 | + text: currentRetryAfter > 0 |
| 131 | + ? currentRetryAfter.toTimeString() |
| 132 | + : '', |
| 133 | + textType: SBUTextType.body3Bold, |
| 134 | + textColorType: SBUTextColorType.text02, |
| 135 | + ), |
| 136 | + ], |
| 137 | + ), |
| 138 | + ), |
| 139 | + ), |
| 140 | + const SizedBox(height: 24), |
| 141 | + if (widget.showCloseButton) |
| 142 | + Row( |
| 143 | + mainAxisAlignment: MainAxisAlignment.end, |
| 144 | + children: [ |
| 145 | + Container( |
| 146 | + margin: const EdgeInsets.only(right: 8, bottom: 4), |
| 147 | + child: DefaultTextStyle.merge( |
| 148 | + style: kIsWeb |
| 149 | + ? const TextStyle( |
| 150 | + decoration: TextDecoration.none) |
| 151 | + : null, |
| 152 | + child: SBUTextButtonComponent( |
| 153 | + height: 32, |
| 154 | + padding: const EdgeInsets.all(8), |
| 155 | + text: SBUTextComponent( |
| 156 | + text: (widget.closeButtonText != null) |
| 157 | + ? widget.closeButtonText! |
| 158 | + : strings.close, |
| 159 | + textType: SBUTextType.button, |
| 160 | + textColorType: SBUTextColorType.primary, |
| 161 | + ), |
| 162 | + onButtonClicked: () async { |
| 163 | + Navigator.pop(context); |
| 164 | + |
| 165 | + if (widget.onCloseButtonClicked != null) { |
| 166 | + widget.onCloseButtonClicked!(); |
| 167 | + } |
| 168 | + }, |
| 169 | + ), |
| 170 | + ), |
| 171 | + ), |
| 172 | + ], |
| 173 | + ), |
| 174 | + const SizedBox(height: 12), |
| 175 | + ], |
| 176 | + ), |
| 177 | + ), |
| 178 | + ), |
| 179 | + )); |
| 180 | + } |
| 181 | +} |
0 commit comments