Files
submanager/lib/services/currency_util.dart
JiWoong Sul 0f0b02bf08 feat: 다국어 지원 및 다중 통화 환율 변환 기능 확대
- ExchangeRateService에 JPY, CNY 환율 지원 추가
- 구독 서비스별 다국어 표시 이름 지원
- 분석 화면 차트 및 UI/UX 개선
- 설정 화면 전면 리팩토링
- SMS 스캔 기능 사용성 개선
- 전체 앱 다국어 번역 확대

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-16 17:34:32 +09:00

244 lines
8.2 KiB
Dart

import 'package:intl/intl.dart';
import '../models/subscription_model.dart';
import 'exchange_rate_service.dart';
/// 통화 단위 변환 및 포맷팅을 위한 유틸리티 클래스
class CurrencyUtil {
static final ExchangeRateService _exchangeRateService = ExchangeRateService();
/// 언어에 따른 기본 통화 반환
static String getDefaultCurrency(String locale) {
switch (locale) {
case 'ko':
return 'KRW';
case 'ja':
return 'JPY';
case 'zh':
return 'CNY';
case 'en':
default:
return 'USD';
}
}
/// 언어에 따른 서브 통화 반환 (영어 제외 모두 USD)
static String? getSecondaryCurrency(String locale, String? selectedCurrency) {
if (locale == 'en' && selectedCurrency == 'KRW') {
return 'KRW';
}
return locale != 'en' ? 'USD' : null;
}
/// 통화 기호 반환
static String getCurrencySymbol(String currency) {
switch (currency) {
case 'KRW':
return '';
case 'USD':
return '\$';
case 'JPY':
return '¥';
case 'CNY':
return '¥';
default:
return currency;
}
}
/// 통화별 locale 반환
static String _getLocaleForCurrency(String currency) {
switch (currency) {
case 'KRW':
return 'ko_KR';
case 'USD':
return 'en_US';
case 'JPY':
return 'ja_JP';
case 'CNY':
return 'zh_CN';
default:
return 'en_US';
}
}
/// 단일 통화 포맷팅
static String _formatSingleCurrency(double amount, String currency) {
final locale = _getLocaleForCurrency(currency);
final symbol = getCurrencySymbol(currency);
final decimals = (currency == 'KRW' || currency == 'JPY') ? 0 : 2;
return NumberFormat.currency(
locale: locale,
symbol: symbol,
decimalDigits: decimals,
).format(amount);
}
/// 금액 포맷팅 (기본 통화 + 서브 통화)
static Future<String> formatAmountWithLocale(
double amount,
String currency,
String locale,
) async {
final defaultCurrency = getDefaultCurrency(locale);
// 입력 통화가 기본 통화인 경우
if (currency == defaultCurrency) {
return _formatSingleCurrency(amount, currency);
}
// USD 입력인 경우 - 기본 통화로 변환하여 표시
if (currency == 'USD' && defaultCurrency != 'USD') {
final convertedAmount = await _exchangeRateService.convertUsdToTarget(amount, defaultCurrency);
if (convertedAmount != null) {
final primaryFormatted = _formatSingleCurrency(convertedAmount, defaultCurrency);
final usdFormatted = _formatSingleCurrency(amount, 'USD');
return '$primaryFormatted ($usdFormatted)';
}
}
// 영어 사용자가 KRW 선택한 경우
if (locale == 'en' && currency == 'KRW') {
return _formatSingleCurrency(amount, currency);
}
// 기타 통화 입력인 경우
return _formatSingleCurrency(amount, currency);
}
/// 구독 목록의 총 월 비용을 계산 (언어별 기본 통화로)
static Future<double> calculateTotalMonthlyExpenseInDefaultCurrency(
List<SubscriptionModel> subscriptions,
String locale,
) async {
final defaultCurrency = getDefaultCurrency(locale);
double total = 0.0;
for (var subscription in subscriptions) {
final price = subscription.currentPrice;
if (subscription.currency == defaultCurrency) {
// 기본 통화면 그대로 합산
total += price;
} else if (subscription.currency == 'USD') {
// USD면 기본 통화로 변환
final converted = await _exchangeRateService.convertUsdToTarget(price, defaultCurrency);
if (converted != null) {
total += converted;
}
} else if (defaultCurrency == 'USD') {
// 기본 통화가 USD인 경우 다른 통화를 USD로 변환
final converted = await _exchangeRateService.convertTargetToUsd(price, subscription.currency);
if (converted != null) {
total += converted;
}
}
}
return total;
}
/// 구독 목록의 총 월 비용을 계산 (원화로 환산, 이벤트 가격 반영) - 기존 호환성 유지
static Future<double> calculateTotalMonthlyExpense(
List<SubscriptionModel> subscriptions) async {
return calculateTotalMonthlyExpenseInDefaultCurrency(subscriptions, 'ko');
}
/// 구독의 월 비용을 표시 형식에 맞게 변환 (언어별 통화)
static Future<String> formatSubscriptionAmountWithLocale(
SubscriptionModel subscription, String locale) async {
final price = subscription.currentPrice;
return formatAmountWithLocale(price, subscription.currency, locale);
}
/// 구독의 월 비용을 표시 형식에 맞게 변환 (원화 변환 포함, 이벤트 가격 반영) - 기존 호환성 유지
static Future<String> formatSubscriptionAmount(
SubscriptionModel subscription) async {
return formatSubscriptionAmountWithLocale(subscription, 'ko');
}
/// 총액을 언어별 기본 통화로 표시
static String formatTotalAmountWithLocale(double amount, String locale) {
final defaultCurrency = getDefaultCurrency(locale);
return _formatSingleCurrency(amount, defaultCurrency);
}
/// 총액을 원화로 표시 - 기존 호환성 유지
static String formatTotalAmount(double amount) {
return formatTotalAmountWithLocale(amount, 'ko');
}
/// 환율 정보 텍스트 가져오기
static Future<String> getExchangeRateInfo() {
return _exchangeRateService.getFormattedExchangeRateInfo();
}
/// 언어별 환율 정보 텍스트 가져오기
static Future<String> getExchangeRateInfoForLocale(String locale) {
return _exchangeRateService.getFormattedExchangeRateInfoForLocale(locale);
}
/// 이벤트로 인한 총 절약액 계산 (언어별 기본 통화로)
static Future<double> calculateTotalEventSavingsInDefaultCurrency(
List<SubscriptionModel> subscriptions, String locale) async {
final defaultCurrency = getDefaultCurrency(locale);
double total = 0.0;
for (var subscription in subscriptions) {
if (subscription.isCurrentlyInEvent) {
final savings = subscription.eventSavings;
if (subscription.currency == defaultCurrency) {
total += savings;
} else if (subscription.currency == 'USD') {
final converted = await _exchangeRateService.convertUsdToTarget(savings, defaultCurrency);
if (converted != null) {
total += converted;
}
} else if (defaultCurrency == 'USD') {
// 기본 통화가 USD인 경우 다른 통화를 USD로 변환
final converted = await _exchangeRateService.convertTargetToUsd(savings, subscription.currency);
if (converted != null) {
total += converted;
}
}
}
}
return total;
}
/// 이벤트로 인한 총 절약액 계산 (원화로 환산) - 기존 호환성 유지
static Future<double> calculateTotalEventSavings(
List<SubscriptionModel> subscriptions) async {
return calculateTotalEventSavingsInDefaultCurrency(subscriptions, 'ko');
}
/// 이벤트 절약액을 표시 형식에 맞게 변환 (언어별)
static Future<String> formatEventSavingsWithLocale(
SubscriptionModel subscription, String locale) async {
if (!subscription.isCurrentlyInEvent) {
return '';
}
final savings = subscription.eventSavings;
return formatAmountWithLocale(savings, subscription.currency, locale);
}
/// 이벤트 절약액을 표시 형식에 맞게 변환 - 기존 호환성 유지
static Future<String> formatEventSavings(
SubscriptionModel subscription) async {
return formatEventSavingsWithLocale(subscription, 'ko');
}
/// 금액과 통화를 받아 포맷팅하여 반환 (언어별)
static Future<String> formatAmountWithCurrencyAndLocale(
double amount, String currency, String locale) async {
return formatAmountWithLocale(amount, currency, locale);
}
/// 금액과 통화를 받아 포맷팅하여 반환 - 기존 호환성 유지
static Future<String> formatAmount(double amount, String currency) async {
return formatAmountWithCurrencyAndLocale(amount, currency, 'ko');
}
}