feat(settings): 添加通知偏好设置和后端 API 集成
This commit is contained in:
@@ -36,6 +36,38 @@ class ProfileApi {
|
||||
return _toSettings(data);
|
||||
}
|
||||
|
||||
Future<ProfileSettingsV1> updateSettings(ProfileSettingsV1 settings) async {
|
||||
final payload = <String, dynamic>{
|
||||
'settings': {
|
||||
'version': settings.version,
|
||||
'preferences': {
|
||||
'interface_language': settings.preferences.interfaceLanguage,
|
||||
'ai_language': settings.preferences.aiLanguage,
|
||||
'timezone': settings.preferences.timezone,
|
||||
'country': settings.preferences.country,
|
||||
},
|
||||
'privacy': settings.privacy,
|
||||
'notification': {
|
||||
'allow_notifications': settings.notification.allowNotifications,
|
||||
'allow_vibration': settings.notification.allowVibration,
|
||||
},
|
||||
},
|
||||
};
|
||||
final json = await _apiClient.rawDio.patch<Map<String, dynamic>>(
|
||||
'/api/v1/users/me/settings',
|
||||
data: payload,
|
||||
);
|
||||
final data = json.data;
|
||||
if (data is! Map<String, dynamic>) {
|
||||
throw ApiProblem(
|
||||
status: 502,
|
||||
title: 'Invalid settings payload',
|
||||
detail: 'Expected settings response object',
|
||||
);
|
||||
}
|
||||
return _toSettings(data);
|
||||
}
|
||||
|
||||
Future<ProfileSettingsV1> uploadAvatar(String filePath) async {
|
||||
final formData = FormData.fromMap({
|
||||
'file': await MultipartFile.fromFile(filePath),
|
||||
@@ -71,6 +103,18 @@ class ProfileApi {
|
||||
)
|
||||
: const PreferenceSettings();
|
||||
|
||||
final notificationRaw = settingsRaw is Map<String, dynamic>
|
||||
? settingsRaw['notification']
|
||||
: null;
|
||||
final notification = notificationRaw is Map<String, dynamic>
|
||||
? NotificationSettings(
|
||||
allowNotifications:
|
||||
(notificationRaw['allow_notifications'] as bool? ?? true),
|
||||
allowVibration:
|
||||
(notificationRaw['allow_vibration'] as bool? ?? true),
|
||||
)
|
||||
: const NotificationSettings();
|
||||
|
||||
return ProfileSettingsV1(
|
||||
displayName: (json['display_name'] as String?) ?? '',
|
||||
bio: (json['bio'] as String?) ?? '',
|
||||
@@ -81,10 +125,7 @@ class ProfileApi {
|
||||
? (settingsRaw['privacy'] as Map<String, dynamic>? ??
|
||||
const <String, dynamic>{})
|
||||
: const <String, dynamic>{},
|
||||
notification: settingsRaw is Map<String, dynamic>
|
||||
? (settingsRaw['notification'] as Map<String, dynamic>? ??
|
||||
const <String, dynamic>{})
|
||||
: const <String, dynamic>{},
|
||||
notification: notification,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user