WIP: integration with ledger

This commit is contained in:
Arseni
2026-02-04 02:01:22 +03:00
parent f1f16a30e6
commit f44ef56ff3
32 changed files with 1226 additions and 405 deletions

View File

@@ -0,0 +1,67 @@
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:pshared/models/payment/source.dart';
import 'package:pshared/provider/payment/source.dart';
class PaymentSourceController extends ChangeNotifier {
PaymentSourceProvider? _provider;
String? _selectedSourceKey;
List<PaymentSource> get sources => _provider?.sources ?? const [];
PaymentSource? get selectedSource {
final key = _selectedSourceKey;
if (key == null) return null;
return sources.firstWhereOrNull((source) => source.key == key);
}
void update(PaymentSourceProvider provider) {
_provider = provider;
final nextSources = provider.sources;
final nextSelectedKey = _resolveSelectedKey(
currentKey: _selectedSourceKey,
sources: nextSources,
);
if (nextSelectedKey == _selectedSourceKey) return;
_selectedSourceKey = nextSelectedKey;
notifyListeners();
}
void selectSource(PaymentSource source) {
if (_selectedSourceKey == source.key) return;
_selectedSourceKey = source.key;
notifyListeners();
}
void selectWalletByRef(String walletRef) {
final source = sources.firstWhereOrNull(
(s) => s.type == PaymentSourceType.wallet && s.id == walletRef,
);
if (source == null) return;
selectSource(source);
}
void selectLedgerByRef(String ledgerAccountRef) {
final source = sources.firstWhereOrNull(
(s) => s.type == PaymentSourceType.ledger && s.id == ledgerAccountRef,
);
if (source == null) return;
selectSource(source);
}
String? _resolveSelectedKey({
required String? currentKey,
required List<PaymentSource> sources,
}) {
if (sources.isEmpty) return null;
if (currentKey != null &&
sources.any((source) => source.key == currentKey)) {
return currentKey;
}
return sources.first.key;
}
}