Skip to content

Commit 831c0e7

Browse files
[Feature] Added image kit integration to upload images to Image Kit
[Feature] Removed `dart_json_mapper` to simplify json parsing [Feature] Refactored code
1 parent d4e7cff commit 831c0e7

24 files changed

+589
-181
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ app.*.map.json
4545
/android/app/profile
4646
/android/app/release
4747
*.g.dart
48-
*.reflectable.dart
48+
*.reflectable.dart
49+
/.run/main.dart.run.xml

assets/svg/ic_plus.svg

Lines changed: 2 additions & 2 deletions
Loading

lib/core/supabase/build_config.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,14 @@ class BuildConfig {
4444

4545
static String _getBaseKey() => const String.fromEnvironment('BASE_KEY');
4646

47-
static String _getOAuthClientId() => const String.fromEnvironment('OAUTH_CLIENT_ID');
47+
static String _getOAuthClientId() =>
48+
const String.fromEnvironment('OAUTH_CLIENT_ID');
49+
50+
// Image Kit
51+
static String get imageKitUrlEndpoint => _getImageKitUrlEndpoint();
52+
static String get imageKitPublicKey => _getImageKitPublicKey();
53+
static String _getImageKitUrlEndpoint() =>
54+
const String.fromEnvironment('IMAGE_KIT_URL_ENDPOINT');
55+
static String _getImageKitPublicKey() =>
56+
const String.fromEnvironment('IMAGE_KIT_PUBLIC_KEY');
4857
}

lib/data/models/post_response.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'post_response.g.dart';
4+
5+
@JsonSerializable()
6+
class PostDatum {
7+
final int id;
8+
@JsonKey(name: 'inserted_at')
9+
final DateTime insertedAt;
10+
@JsonKey(name: 'updated_at')
11+
final DateTime updatedAt;
12+
final String post;
13+
@JsonKey(name: 'user_id')
14+
final String userId;
15+
@JsonKey(name: 'user_meta')
16+
final Map<String, dynamic>? userMeta;
17+
18+
const PostDatum({
19+
required this.id,
20+
required this.insertedAt,
21+
required this.updatedAt,
22+
required this.post,
23+
required this.userId,
24+
this.userMeta,
25+
});
26+
27+
PostDatum copyWith({
28+
int? id,
29+
DateTime? insertedAt,
30+
DateTime? updatedAt,
31+
String? post,
32+
String? userId,
33+
Map<String, dynamic>? userMeta,
34+
}) {
35+
return PostDatum(
36+
id: id ?? this.id,
37+
insertedAt: insertedAt ?? this.insertedAt,
38+
updatedAt: updatedAt ?? this.updatedAt,
39+
post: post ?? this.post,
40+
userId: userId ?? this.userId,
41+
userMeta: userMeta ?? this.userMeta,
42+
);
43+
}
44+
45+
factory PostDatum.fromJson(Map<String, dynamic> json) =>
46+
_$PostDatumFromJson(json);
47+
48+
Map<String, dynamic> toJson() => _$PostDatumToJson(this);
49+
}

lib/models/user_profile.dart renamed to lib/data/models/user_profile.dart

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,22 @@
2525
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
28-
import 'package:dart_json_mapper/dart_json_mapper.dart';
29-
import 'package:mobx/mobx.dart';
28+
import 'package:json_annotation/json_annotation.dart';
3029

3130
part 'user_profile.g.dart';
3231

33-
@jsonSerializable
34-
class UserProfile extends _UserProfile with _$UserProfile {
35-
UserProfile({
36-
final String? id,
37-
String? email,
38-
String? username,
39-
String? avatarUrl,
40-
String? about,
41-
String? name,
42-
}) : super(
43-
id: id,
44-
email: email,
45-
username: username,
46-
avatarUrl: avatarUrl,
47-
about: about,
48-
name: name,
49-
);
50-
}
51-
52-
@jsonSerializable
53-
abstract class _UserProfile with Store {
54-
@JsonProperty(name: 'id')
32+
@JsonSerializable(includeIfNull: false)
33+
class UserProfile {
5534
late final String? id;
56-
57-
@JsonProperty(name: 'email')
5835
String? email;
59-
60-
@JsonProperty(name: 'user_name')
36+
@JsonKey(name: 'user_name')
6137
String? username;
62-
63-
@JsonProperty(name: 'name')
6438
String? name;
65-
66-
@JsonProperty(name: 'avatar_url')
39+
@JsonKey(name: 'avatar_url')
6740
String? avatarUrl;
68-
69-
@JsonProperty(name: 'about')
7041
String? about;
7142

72-
_UserProfile({
43+
UserProfile({
7344
this.id,
7445
this.email,
7546
this.username,
@@ -78,7 +49,7 @@ abstract class _UserProfile with Store {
7849
this.name,
7950
});
8051

81-
copyWith({
52+
UserProfile copyWith({
8253
String? email,
8354
String? username,
8455
String? name,
@@ -94,4 +65,9 @@ abstract class _UserProfile with Store {
9465
about: this.about = about ?? this.about,
9566
);
9667
}
68+
69+
factory UserProfile.fromJson(Map<String, dynamic> json) =>
70+
_$UserProfileFromJson(json);
71+
72+
Map<String, dynamic> toJson() => _$UserProfileToJson(this);
9773
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import 'package:manch/data/models/post_response.dart';
3+
import 'package:supabase_flutter/supabase_flutter.dart';
4+
5+
class PostRepository {
6+
Future<List<PostDatum>> fetchPosts() async {
7+
try {
8+
final response =
9+
await Supabase.instance.client.from('post').select();
10+
final postList = <PostDatum>[];
11+
response.forEach((element) {
12+
postList.add(PostDatum.fromJson(element));
13+
});
14+
return postList;
15+
} catch (e) {
16+
rethrow;
17+
}
18+
}
19+
20+
Future<PostDatum> sharePost({
21+
required String post,
22+
required String userId,
23+
Map<String, dynamic>? userMeta,
24+
}) async {
25+
try {
26+
final response = await Supabase.instance.client.from('post').insert(
27+
{
28+
'post': post.trim(),
29+
'user_id': Supabase.instance.client.auth.currentUser?.id,
30+
'user_meta': userMeta,
31+
},
32+
).select();
33+
return PostDatum.fromJson(response.first);
34+
} catch (e) {
35+
rethrow;
36+
}
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import 'package:manch/data/models/user_profile.dart';
3+
import 'package:supabase_flutter/supabase_flutter.dart';
4+
5+
class UserProfileRepository {
6+
Future<UserProfile> getProfile() async {
7+
try {
8+
final userId = Supabase.instance.client.auth.currentUser?.id;
9+
final response = await Supabase.instance.client
10+
.from('profiles')
11+
.select()
12+
.eq('id', '$userId')
13+
.single();
14+
UserProfile userProfile = UserProfile.fromJson(response);
15+
return userProfile;
16+
} catch (e) {
17+
rethrow;
18+
}
19+
}
20+
21+
Future<UserProfile> updateProfile(UserProfile userProfile) async {
22+
try {
23+
final userId = Supabase.instance.client.auth.currentUser?.id;
24+
final response = await Supabase.instance.client
25+
.from('profiles')
26+
.update(userProfile.toJson())
27+
.eq('id', '$userId')
28+
.select();
29+
return UserProfile.fromJson(response.first);
30+
} catch (e) {
31+
rethrow;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)