ing connected, makign some features
This commit is contained in:
@@ -5,13 +5,23 @@ import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/bank.dart';
|
||||
import '../services/ingtrak_api.dart';
|
||||
import '../services/storage_service.dart';
|
||||
import '../utils/constants.dart';
|
||||
|
||||
/// App-wide banking state backed by the local ingtrak API.
|
||||
class BankController extends ChangeNotifier {
|
||||
BankController({IngtrakApi? api}) : _api = api ?? IngtrakApi();
|
||||
BankController({IngtrakApi? api, this._storage})
|
||||
: _api = api ?? IngtrakApi() {
|
||||
final cached = _storage?.loadBankCache();
|
||||
if (cached != null && cached.status.linked) {
|
||||
_status = cached.status;
|
||||
_balance = cached.balance;
|
||||
_transactions = cached.transactions;
|
||||
}
|
||||
}
|
||||
|
||||
final IngtrakApi _api;
|
||||
final StorageService? _storage;
|
||||
|
||||
ConnectStatus _status = ConnectStatus.disconnected;
|
||||
BankBalance? _balance;
|
||||
@@ -32,6 +42,22 @@ class BankController extends ChangeNotifier {
|
||||
String get apiBaseUrl => _api.baseUrl;
|
||||
String? get pendingState => _pendingState;
|
||||
|
||||
Future<void> _persistCache() async {
|
||||
final storage = _storage;
|
||||
if (storage == null) return;
|
||||
if (!_status.linked) {
|
||||
await storage.clearBankCache();
|
||||
return;
|
||||
}
|
||||
await storage.saveBankCache(
|
||||
BankCache(
|
||||
status: _status,
|
||||
balance: _balance,
|
||||
transactions: _transactions,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Load status; if linked, also load balance + transactions.
|
||||
Future<void> refresh() async {
|
||||
_isLoading = true;
|
||||
@@ -42,15 +68,26 @@ class BankController extends ChangeNotifier {
|
||||
_status = await _api.fetchStatus();
|
||||
if (_status.linked) {
|
||||
await _loadLinkedData();
|
||||
await _persistCache();
|
||||
} else {
|
||||
_balance = null;
|
||||
_transactions = const [];
|
||||
await _persistCache();
|
||||
}
|
||||
} on IngtrakApiException catch (error, stackTrace) {
|
||||
_error = error.message;
|
||||
// Keep cached linked data on transient API errors.
|
||||
if (!isLinked || (_balance == null && _transactions.isEmpty)) {
|
||||
_error = error.message;
|
||||
} else {
|
||||
_error = 'Using saved data · ${error.message}';
|
||||
}
|
||||
debugPrint('BankController.refresh failed: $error\n$stackTrace');
|
||||
} catch (error, stackTrace) {
|
||||
_error = 'Could not reach $apiBaseUrl';
|
||||
if (!isLinked || (_balance == null && _transactions.isEmpty)) {
|
||||
_error = 'Could not reach $apiBaseUrl';
|
||||
} else {
|
||||
_error = 'Using saved data · offline/unreachable API';
|
||||
}
|
||||
debugPrint('BankController.refresh failed: $error\n$stackTrace');
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
@@ -73,8 +110,12 @@ class BankController extends ChangeNotifier {
|
||||
_balance = null;
|
||||
_transactions = const [];
|
||||
_error = error.message;
|
||||
} else {
|
||||
await _persistCache();
|
||||
} else if (_balance == null && _transactions.isEmpty) {
|
||||
_error = error.message;
|
||||
} else {
|
||||
// Keep previous balance/txs if refresh of data fails but still linked.
|
||||
_error = 'Using saved data · ${error.message}';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,6 +197,7 @@ class BankController extends ChangeNotifier {
|
||||
_pendingState = null;
|
||||
if (_status.linked) {
|
||||
await _loadLinkedData();
|
||||
await _persistCache();
|
||||
}
|
||||
return _status.linked;
|
||||
} on IngtrakApiException catch (error, stackTrace) {
|
||||
|
||||
Reference in New Issue
Block a user