feat(apps): 重构 UI 架构为 presentation 层并新增 l10n 国际化支持
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import 'package:social_app/core/network/i_api_client.dart';
|
||||
|
||||
class FriendsApi {
|
||||
final IApiClient _client;
|
||||
static const _prefix = '/api/v1/friends';
|
||||
|
||||
FriendsApi(this._client);
|
||||
|
||||
Future<List<FriendResponse>> getFriends() async {
|
||||
final response = await _client.get(_prefix);
|
||||
final List<dynamic> data = response.data;
|
||||
return data.map((json) => FriendResponse.fromJson(json)).toList();
|
||||
}
|
||||
|
||||
Future<List<FriendRequestResponse>> getOutgoingRequests() async {
|
||||
final response = await _client.get('$_prefix/requests/outgoing');
|
||||
final List<dynamic> data = response.data;
|
||||
return data.map((json) => FriendRequestResponse.fromJson(json)).toList();
|
||||
}
|
||||
|
||||
Future<FriendRequestResponse> sendRequest(
|
||||
String targetUserId, {
|
||||
String? content,
|
||||
}) async {
|
||||
final data = {'target_user_id': targetUserId, 'content': content};
|
||||
final response = await _client.post('$_prefix/requests', data: data);
|
||||
return FriendRequestResponse.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<FriendRequestResponse> acceptRequest(String friendshipId) async {
|
||||
final response = await _client.post(
|
||||
'$_prefix/requests/$friendshipId/accept',
|
||||
);
|
||||
return FriendRequestResponse.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<FriendRequestResponse> declineRequest(String friendshipId) async {
|
||||
final response = await _client.post(
|
||||
'$_prefix/requests/$friendshipId/decline',
|
||||
);
|
||||
return FriendRequestResponse.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<void> removeFriend(String friendshipId) async {
|
||||
await _client.delete('$_prefix/$friendshipId');
|
||||
}
|
||||
|
||||
Future<FriendRequestResponse> getRequestById(String friendshipId) async {
|
||||
final response = await _client.get('$_prefix/requests/$friendshipId');
|
||||
return FriendRequestResponse.fromJson(response.data);
|
||||
}
|
||||
}
|
||||
|
||||
class FriendResponse {
|
||||
final String id;
|
||||
final UserBasicInfo friend;
|
||||
final String status;
|
||||
final DateTime createdAt;
|
||||
final DateTime? acceptedAt;
|
||||
|
||||
FriendResponse({
|
||||
required this.id,
|
||||
required this.friend,
|
||||
required this.status,
|
||||
required this.createdAt,
|
||||
this.acceptedAt,
|
||||
});
|
||||
|
||||
factory FriendResponse.fromJson(Map<String, dynamic> json) {
|
||||
return FriendResponse(
|
||||
id: json['id'] as String,
|
||||
friend: UserBasicInfo.fromJson(json['friend'] as Map<String, dynamic>),
|
||||
status: json['status'] as String,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
acceptedAt: json['accepted_at'] != null
|
||||
? DateTime.parse(json['accepted_at'] as String)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UserBasicInfo {
|
||||
final String id;
|
||||
final String username;
|
||||
final String? avatarUrl;
|
||||
|
||||
UserBasicInfo({required this.id, required this.username, this.avatarUrl});
|
||||
|
||||
factory UserBasicInfo.fromJson(Map<String, dynamic> json) {
|
||||
return UserBasicInfo(
|
||||
id: json['id'] as String,
|
||||
username: json['username'] as String,
|
||||
avatarUrl: json['avatar_url'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class FriendRequestResponse {
|
||||
final String id;
|
||||
final UserBasicInfo sender;
|
||||
final UserBasicInfo recipient;
|
||||
final String? content;
|
||||
final String status;
|
||||
final DateTime createdAt;
|
||||
|
||||
FriendRequestResponse({
|
||||
required this.id,
|
||||
required this.sender,
|
||||
required this.recipient,
|
||||
this.content,
|
||||
required this.status,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
factory FriendRequestResponse.fromJson(Map<String, dynamic> json) {
|
||||
return FriendRequestResponse(
|
||||
id: json['id'] as String,
|
||||
sender: UserBasicInfo.fromJson(json['sender'] as Map<String, dynamic>),
|
||||
recipient: UserBasicInfo.fromJson(
|
||||
json['recipient'] as Map<String, dynamic>,
|
||||
),
|
||||
content: json['content'] as String?,
|
||||
status: json['status'] as String,
|
||||
createdAt: DateTime.parse(json['created_at'] as String),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
class UserResponse {
|
||||
final String id;
|
||||
final String username;
|
||||
final String? phone;
|
||||
final String? avatarUrl;
|
||||
final String? bio;
|
||||
|
||||
const UserResponse({
|
||||
required this.id,
|
||||
required this.username,
|
||||
this.phone,
|
||||
this.avatarUrl,
|
||||
this.bio,
|
||||
});
|
||||
|
||||
factory UserResponse.fromJson(Map<String, dynamic> json) {
|
||||
return UserResponse(
|
||||
id: json['id'] as String,
|
||||
username: json['username'] as String,
|
||||
phone: json['phone'] as String?,
|
||||
avatarUrl: json['avatar_url'] as String?,
|
||||
bio: json['bio'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UserUpdateRequest {
|
||||
final String? username;
|
||||
final String? avatarUrl;
|
||||
final String? bio;
|
||||
|
||||
const UserUpdateRequest({this.username, this.avatarUrl, this.bio});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (username != null) 'username': username,
|
||||
if (avatarUrl != null) 'avatar_url': avatarUrl,
|
||||
if (bio != null) 'bio': bio,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import 'dart:io';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:social_app/core/network/i_api_client.dart';
|
||||
import 'models/user_response.dart';
|
||||
|
||||
class UserBasicInfo {
|
||||
final String id;
|
||||
final String username;
|
||||
final String? avatarUrl;
|
||||
|
||||
UserBasicInfo({required this.id, required this.username, this.avatarUrl});
|
||||
|
||||
factory UserBasicInfo.fromJson(Map<String, dynamic> json) {
|
||||
return UserBasicInfo(
|
||||
id: json['id'] as String,
|
||||
username: json['username'] as String,
|
||||
avatarUrl: json['avatar_url'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UsersApi {
|
||||
final IApiClient _client;
|
||||
static const _prefix = '/api/v1/users';
|
||||
|
||||
UsersApi(this._client);
|
||||
|
||||
Future<UserBasicInfo> getById(String userId) async {
|
||||
final response = await _client.get('$_prefix/$userId');
|
||||
return UserBasicInfo.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<UserResponse> getMe() async {
|
||||
final response = await _client.get('$_prefix/me');
|
||||
return UserResponse.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<UserResponse> updateMe(UserUpdateRequest request) async {
|
||||
final response = await _client.patch('$_prefix/me', data: request.toJson());
|
||||
return UserResponse.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<String> uploadAvatar(File file) async {
|
||||
final formData = FormData.fromMap({
|
||||
'file': await MultipartFile.fromFile(
|
||||
file.path,
|
||||
filename: file.path.split('/').last,
|
||||
),
|
||||
});
|
||||
final response = await _client.post<Map<String, dynamic>>(
|
||||
'$_prefix/me/avatar',
|
||||
data: formData,
|
||||
);
|
||||
final payload = response.data;
|
||||
if (payload is! Map<String, dynamic>) {
|
||||
throw StateError('Invalid /users/me/avatar response');
|
||||
}
|
||||
final url = payload['url'];
|
||||
if (url is! String) {
|
||||
throw StateError('Missing url in /users/me/avatar response');
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
Future<List<UserResponse>> searchUsers(String query) async {
|
||||
final response = await _client.post(
|
||||
'$_prefix/search',
|
||||
data: {'query': query},
|
||||
);
|
||||
final List<dynamic> data = response.data;
|
||||
return data.map((json) => UserResponse.fromJson(json)).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import 'models/user_response.dart';
|
||||
|
||||
abstract class UsersRepository {
|
||||
Future<UserResponse> getMe();
|
||||
Future<UserResponse> updateMe(UserUpdateRequest request);
|
||||
Future<List<UserResponse>> searchUsers(String query);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'users_api.dart';
|
||||
import 'users_repository.dart';
|
||||
import 'models/user_response.dart';
|
||||
|
||||
class UsersRepositoryImpl implements UsersRepository {
|
||||
final UsersApi _api;
|
||||
|
||||
UsersRepositoryImpl({required UsersApi api}) : _api = api;
|
||||
|
||||
@override
|
||||
Future<UserResponse> getMe() {
|
||||
return _api.getMe();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserResponse> updateMe(UserUpdateRequest request) {
|
||||
return _api.updateMe(request);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<UserResponse>> searchUsers(String query) {
|
||||
return _api.searchUsers(query);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user