import 'package:flutter/foundation.dart'; import 'package:pshared/provider/payment/multiple/quotation.dart'; import 'package:pweb/providers/quotation/auto_refresh.dart'; class MultiQuotationController extends ChangeNotifier { static const Duration _autoRefreshLead = Duration(seconds: 5); MultiQuotationProvider? _quotation; final QuotationAutoRefreshController _autoRefreshController = QuotationAutoRefreshController(); void update(MultiQuotationProvider quotation) { if (identical(_quotation, quotation)) return; _quotation?.removeListener(_handleQuotationChanged); _quotation = quotation; _quotation?.addListener(_handleQuotationChanged); _handleQuotationChanged(); } bool get isLoading => _quotation?.isLoading ?? false; Exception? get error => _quotation?.error; bool get canRefresh => _quotation?.canRefresh ?? false; bool get isReady => _quotation?.isReady ?? false; DateTime? get quoteExpiresAt => _quotation?.quoteExpiresAt; void refreshQuotation() { _quotation?.refreshQuotation(); } void _handleQuotationChanged() { _syncAutoRefresh(); notifyListeners(); } void _syncAutoRefresh() { final quotation = _quotation; if (quotation == null) { _autoRefreshController.reset(); return; } final expiresAt = quoteExpiresAt; final scheduledAt = expiresAt == null ? null : expiresAt.subtract(_autoRefreshLead); _autoRefreshController.setEnabled(true); _autoRefreshController.sync( isLoading: quotation.isLoading, canRefresh: quotation.canRefresh, expiresAt: scheduledAt, onRefresh: _refreshQuotation, ); } Future _refreshQuotation() async { await _quotation?.refreshQuotation(); } @override void dispose() { _quotation?.removeListener(_handleQuotationChanged); _autoRefreshController.dispose(); super.dispose(); } }