2026-03-22 00:21:21 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import '../data/models/coin.dart';
|
|
|
|
|
|
import '../data/services/market_service.dart';
|
|
|
|
|
|
|
2026-04-07 01:05:05 +08:00
|
|
|
|
/// 行情狀態管理
|
2026-03-22 00:21:21 +08:00
|
|
|
|
class MarketProvider extends ChangeNotifier {
|
|
|
|
|
|
final MarketService _marketService;
|
|
|
|
|
|
|
|
|
|
|
|
List<Coin> _allCoins = [];
|
|
|
|
|
|
bool _isLoading = false;
|
|
|
|
|
|
String? _error;
|
2026-03-28 18:57:45 +08:00
|
|
|
|
bool _coinsLoaded = false;
|
2026-03-22 00:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
MarketProvider(this._marketService);
|
|
|
|
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
|
List<Coin> get allCoins => _allCoins;
|
|
|
|
|
|
bool get isLoading => _isLoading;
|
|
|
|
|
|
String? get error => _error;
|
2026-03-28 18:57:45 +08:00
|
|
|
|
|
2026-04-07 01:05:05 +08:00
|
|
|
|
/// BTC 和 ETH(上半區展示)
|
2026-03-28 18:57:45 +08:00
|
|
|
|
List<Coin> get featuredCoins =>
|
|
|
|
|
|
_allCoins.where((c) => c.code == 'BTC' || c.code == 'ETH').toList();
|
|
|
|
|
|
|
2026-04-07 01:05:05 +08:00
|
|
|
|
/// 排除 BTC、ETH、USDT 的代幣列表(下半區展示)
|
2026-03-28 18:57:45 +08:00
|
|
|
|
List<Coin> get otherCoins => _allCoins
|
|
|
|
|
|
.where((c) => !{'BTC', 'ETH', 'USDT'}.contains(c.code))
|
|
|
|
|
|
.toList();
|
2026-03-22 00:21:21 +08:00
|
|
|
|
|
2026-04-07 01:05:05 +08:00
|
|
|
|
/// 加載幣種列表
|
2026-03-23 00:43:19 +08:00
|
|
|
|
Future<void> loadCoins({bool force = false}) async {
|
|
|
|
|
|
if (_coinsLoaded && !force && _allCoins.isNotEmpty) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 00:21:21 +08:00
|
|
|
|
_isLoading = true;
|
|
|
|
|
|
_error = null;
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
final response = await _marketService.getCoinList();
|
|
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
|
_allCoins = response.data ?? [];
|
2026-03-23 00:43:19 +08:00
|
|
|
|
_coinsLoaded = true;
|
2026-03-22 00:21:21 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
_error = response.message;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
_error = '加載失敗: $e';
|
2026-03-22 00:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-07 01:05:05 +08:00
|
|
|
|
/// 根據代碼獲取幣種
|
2026-03-22 00:21:21 +08:00
|
|
|
|
Coin? getCoinByCode(String code) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return _allCoins.firstWhere((c) => c.code == code);
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 刷新
|
|
|
|
|
|
Future<void> refresh() async {
|
2026-03-23 00:43:19 +08:00
|
|
|
|
await loadCoins(force: true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-07 01:05:05 +08:00
|
|
|
|
/// 重置加載狀態(用於退出登錄時)
|
2026-03-23 00:43:19 +08:00
|
|
|
|
void resetLoadState() {
|
|
|
|
|
|
_coinsLoaded = false;
|
|
|
|
|
|
_allCoins = [];
|
|
|
|
|
|
_error = null;
|
|
|
|
|
|
notifyListeners();
|
2026-03-22 00:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|