Files
eryao/apps/lib/features/settings/data/models/profile_settings.dart
T

182 lines
4.7 KiB
Dart
Raw Normal View History

2026-04-03 16:56:47 +08:00
import 'package:flutter/material.dart';
import '../../../../l10n/app_localizations.dart';
String displayLanguageLabel(AppLocalizations l10n, String languageTag) {
return switch (languageTag) {
'en-US' => l10n.english,
'zh-Hant' => '繁體中文',
'zh-CN' => '简体中文',
_ => '简体中文',
2026-04-03 16:56:47 +08:00
};
}
class PreferenceSettings {
const PreferenceSettings({
this.interfaceLanguage = 'zh-CN',
this.aiLanguage = 'zh-CN',
this.timezone = 'Asia/Shanghai',
2026-04-16 16:11:09 +08:00
this.country = 'US',
2026-04-03 16:56:47 +08:00
});
final String interfaceLanguage;
final String aiLanguage;
final String timezone;
final String country;
PreferenceSettings copyWith({
String? interfaceLanguage,
String? aiLanguage,
String? timezone,
String? country,
}) {
return PreferenceSettings(
interfaceLanguage: interfaceLanguage ?? this.interfaceLanguage,
aiLanguage: aiLanguage ?? this.aiLanguage,
timezone: timezone ?? this.timezone,
country: country ?? this.country,
);
}
}
class NotificationSettings {
const NotificationSettings({
this.allowNotifications = true,
this.allowVibration = true,
});
final bool allowNotifications;
final bool allowVibration;
NotificationSettings copyWith({
bool? allowNotifications,
bool? allowVibration,
}) {
return NotificationSettings(
allowNotifications: allowNotifications ?? this.allowNotifications,
allowVibration: allowVibration ?? this.allowVibration,
);
}
}
class DivinationTutorialSettings {
const DivinationTutorialSettings({
this.divinationEntryShown = true,
this.autoDivinationShown = true,
this.manualDivinationShown = true,
});
final bool divinationEntryShown;
final bool autoDivinationShown;
final bool manualDivinationShown;
DivinationTutorialSettings copyWith({
bool? divinationEntryShown,
bool? autoDivinationShown,
bool? manualDivinationShown,
}) {
return DivinationTutorialSettings(
divinationEntryShown: divinationEntryShown ?? this.divinationEntryShown,
autoDivinationShown: autoDivinationShown ?? this.autoDivinationShown,
manualDivinationShown:
manualDivinationShown ?? this.manualDivinationShown,
);
}
}
class PrivacySettings {
const PrivacySettings({
this.canSell = false,
this.profileVisibility = 'public',
});
final bool canSell;
final String profileVisibility;
PrivacySettings copyWith({bool? canSell, String? profileVisibility}) {
return PrivacySettings(
canSell: canSell ?? this.canSell,
profileVisibility: profileVisibility ?? this.profileVisibility,
);
}
}
2026-04-03 16:56:47 +08:00
class ProfileSettingsV1 {
const ProfileSettingsV1({
this.version = 1,
this.displayName = '',
this.bio = '',
this.avatarPath,
this.avatarUrl,
2026-04-03 16:56:47 +08:00
this.preferences = const PreferenceSettings(),
this.privacy = const PrivacySettings(),
this.notification = const NotificationSettings(),
this.divinationTutorial = const DivinationTutorialSettings(),
2026-04-03 16:56:47 +08:00
});
final int version;
final String displayName;
final String bio;
final String? avatarPath;
final String? avatarUrl;
2026-04-03 16:56:47 +08:00
final PreferenceSettings preferences;
final PrivacySettings privacy;
final NotificationSettings notification;
final DivinationTutorialSettings divinationTutorial;
2026-04-03 16:56:47 +08:00
ProfileSettingsV1 copyWith({
int? version,
String? displayName,
String? bio,
String? avatarPath,
String? avatarUrl,
2026-04-03 16:56:47 +08:00
PreferenceSettings? preferences,
PrivacySettings? privacy,
NotificationSettings? notification,
DivinationTutorialSettings? divinationTutorial,
2026-04-03 16:56:47 +08:00
}) {
return ProfileSettingsV1(
version: version ?? this.version,
displayName: displayName ?? this.displayName,
bio: bio ?? this.bio,
avatarPath: avatarPath ?? this.avatarPath,
avatarUrl: avatarUrl ?? this.avatarUrl,
2026-04-03 16:56:47 +08:00
preferences: preferences ?? this.preferences,
privacy: privacy ?? this.privacy,
notification: notification ?? this.notification,
divinationTutorial: divinationTutorial ?? this.divinationTutorial,
2026-04-03 16:56:47 +08:00
);
}
factory ProfileSettingsV1.defaultsForLocale(Locale locale) {
final tag = languageTagFromLocale(locale);
return ProfileSettingsV1(
preferences: PreferenceSettings(interfaceLanguage: tag, aiLanguage: tag),
);
}
}
String languageTagFromLocale(Locale locale) {
switch (locale.languageCode) {
case 'en':
return 'en-US';
case 'zh':
if (locale.scriptCode == 'Hant') {
return 'zh-Hant';
}
return 'zh-CN';
2026-04-03 16:56:47 +08:00
default:
return 'zh-CN';
}
}
Locale localeFromLanguageTag(String tag) {
if (tag.toLowerCase().startsWith('en')) {
return const Locale('en');
}
if (tag == 'zh-Hant') {
return const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant');
}
2026-04-03 16:56:47 +08:00
return const Locale('zh');
}