-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_repository.dart
More file actions
159 lines (132 loc) · 4.89 KB
/
Copy pathprofile_repository.dart
File metadata and controls
159 lines (132 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// lib/features/profile/repository/profile_repository.dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:lam7a/core/models/user_model.dart';
import 'package:lam7a/features/profile/dtos/profile_dto.dart';
import 'package:lam7a/features/profile/dtos/follow_user_dto.dart';
import 'package:lam7a/features/profile/services/profile_api_service.dart';
part 'profile_repository.g.dart';
@Riverpod(keepAlive: true)
ProfileRepository profileRepository(Ref ref) {
final api = ref.watch(profileApiServiceProvider);
return ProfileRepository(api);
}
class ProfileRepository {
final ProfileApiService _api;
ProfileRepository(this._api);
//get profile/username
Future<UserModel> getProfile(String username) async {
final dto = await _api.getProfileByUsername(username);
return _fromDto(dto);
}
Future<UserModel> getMyProfile() async {
final dto = await _api.getMyProfile();
return _fromDto(dto);
}
Future<UserModel> updateMyProfile(
UserModel user, {
String? avatarPath,
String? bannerPath,
}) async {
String avatarUrl = user.profileImageUrl ?? "";
String bannerUrl = user.bannerImageUrl ?? "";
if (avatarPath != null && !avatarPath.startsWith("http")) {
avatarUrl = await _api.uploadProfilePicture(avatarPath);
}
if (bannerPath != null && !bannerPath.startsWith("http")) {
bannerUrl = await _api.uploadBanner(bannerPath);
}
final dto = await _api.updateMyProfile({
"name": user.name,
"bio": user.bio,
"location": user.location,
"website": user.website,
"birth_date": user.birthDate,
"profile_image_url": avatarUrl,
"banner_image_url": bannerUrl,
});
return _fromDto(dto);
}
Future<void> deleteAvatar() async {
await _api.deleteProfilePicture();
}
Future<void> deleteBanner() async {
await _api.deleteBanner();
}
Future<void> followUser(int id) => _api.followUser(id);
Future<void> unfollowUser(int id) => _api.unfollowUser(id);
Future<void> muteUser(int id) => _api.muteUser(id);
Future<void> unmuteUser(int id) => _api.unmuteUser(id);
Future<void> blockUser(int id) => _api.blockUser(id);
Future<void> unblockUser(int id) => _api.unblockUser(id);
Future<List<UserModel>> getFollowers(int id) async {
final list = await _api.getFollowers(id);
return list.map<UserModel>((raw) {
final f = FollowUserDto.fromJson(Map<String, dynamic>.from(raw));
return UserModel(
id: f.id,
username: f.username,
name: f.name,
bio: f.bio,
profileImageUrl: f.profileImageUrl,
followersCount: 0,
followingCount: 0,
stateFollow: (f.isFollowedByMe ?? false)
? ProfileStateOfFollow.following
: ProfileStateOfFollow.notfollowing,
stateFollowingMe: (f.isFollowingMe ?? false)
? ProfileStateFollowingMe.followingme
: ProfileStateFollowingMe.notfollowingme,
);
}).toList();
}
Future<List<UserModel>> getFollowing(int id) async {
final list = await _api.getFollowing(id);
return list.map<UserModel>((raw) {
final f = FollowUserDto.fromJson(Map<String, dynamic>.from(raw));
return UserModel(
id: f.id,
username: f.username,
name: f.name,
bio: f.bio,
profileImageUrl: f.profileImageUrl,
followersCount: 0,
followingCount: 0,
stateFollow: (f.isFollowedByMe ?? false)
? ProfileStateOfFollow.following
: ProfileStateOfFollow.notfollowing,
stateFollowingMe: (f.isFollowingMe ?? false)
? ProfileStateFollowingMe.followingme
: ProfileStateFollowingMe.notfollowingme,
);
}).toList();
}
UserModel _fromDto(ProfileDto dto) {
return UserModel(
id: dto.id,
profileId: dto.userId,
username: dto.user?["username"],
name: dto.name,
birthDate: dto.birthDate,
bio: dto.bio,
location: dto.location,
website: dto.website,
createdAt: dto.createdAt,
profileImageUrl: dto.profileImageUrl,
bannerImageUrl: dto.bannerImageUrl,
followersCount: dto.followersCount ?? 0,
followingCount: dto.followingCount ?? 0,
stateFollow: (dto.isFollowedByMe ?? false)
? ProfileStateOfFollow.following
: ProfileStateOfFollow.notfollowing,
stateMute: (dto.toJson().containsKey('is_muted_by_me') && dto.toJson()['is_muted_by_me'] == true)
? ProfileStateOfMute.muted
: ProfileStateOfMute.notmuted,
stateBlocked: (dto.toJson().containsKey('is_blocked_by_me') && dto.toJson()['is_blocked_by_me'] == true)
? ProfileStateBlocked.blocked
: ProfileStateBlocked.notblocked,
stateFollowingMe: (dto.toJson().containsKey('is_following_me') && dto.toJson()['is_following_me'] == true)
? ProfileStateFollowingMe.followingme
: ProfileStateFollowingMe.notfollowingme,
);
}
}