Skip to content

Commit 34c5bbe

Browse files
committed
feat: intialize model using freezed and repositories
1 parent 89e2e0e commit 34c5bbe

13 files changed

Lines changed: 541 additions & 68 deletions

lib/data/services/remote/api_service.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:dio/dio.dart';
22
import 'package:flutter/foundation.dart';
33

44
import '../../config/config.dart';
5+
import '../local/storage_service.dart';
56

67
class ApiService {
78
/// Service initializer
@@ -14,13 +15,14 @@ class ApiService {
1415
/// Safe method to send GET request to an endpoint **below** [Environment.baseUrl].
1516
static Future<Map<String, dynamic>> get(
1617
String endpoint, {
18+
String? baseUrl,
1719
Map<String, dynamic>? headers,
1820
Map<String, dynamic>? query,
1921
}) async {
2022
Response? response;
2123
try {
2224
response = await _channel.get(
23-
Environment.baseUrl + endpoint,
25+
(baseUrl ?? Environment.baseUrl) + endpoint,
2426
queryParameters: query,
2527
options: Options(
2628
validateStatus: (status) {
@@ -115,4 +117,9 @@ class ApiService {
115117
'data': response?.data ?? 'null',
116118
};
117119
}
120+
121+
static Future<void> addTokenToHeaders(Map<String, dynamic> headers) async {
122+
final token = StorageService.authToken;
123+
headers.addAll({'authorization': token});
124+
}
118125
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'activity_details.freezed.dart';
4+
part 'activity_details.g.dart';
5+
6+
@freezed
7+
class ActivityDetails with _$ActivityDetails {
8+
factory ActivityDetails({
9+
int? correct,
10+
int? details,
11+
@JsonKey(name: 'created_at') DateTime? createdAt,
12+
}) = _ActivityDetails;
13+
14+
factory ActivityDetails.fromJson(Map<String, dynamic> json) =>
15+
_$ActivityDetailsFromJson(json);
16+
}

lib/domain/models/contest.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'contest.freezed.dart';
4+
part 'contest.g.dart';
5+
6+
@freezed
7+
class Contest with _$Contest {
8+
factory Contest({
9+
List<Ongoing>? ongoing,
10+
String? timestamp,
11+
List<Upcoming>? upcoming,
12+
}) = _Contest;
13+
14+
factory Contest.fromJson(Map<String, dynamic> json) =>
15+
_$ContestFromJson(json);
16+
}
17+
18+
@freezed
19+
class Ongoing with _$Ongoing {
20+
factory Ongoing({
21+
@JsonKey(name: 'EndTime') required DateTime endTime,
22+
@JsonKey(name: 'Name') required String name,
23+
@JsonKey(name: 'Platform') required String platform,
24+
required String url,
25+
@JsonKey(name: 'challenge_type') required String challengeType,
26+
}) = _Ongoing;
27+
28+
factory Ongoing.fromJson(Map<String, dynamic> json) =>
29+
_$OngoingFromJson(json);
30+
}
31+
32+
@freezed
33+
class Upcoming with _$Upcoming {
34+
factory Upcoming({
35+
@JsonKey(name: 'StartTime') required DateTime startTime,
36+
@JsonKey(name: 'EndTime') required DateTime endTime,
37+
@JsonKey(name: 'Name') required String name,
38+
@JsonKey(name: 'Platform') required String platform,
39+
required String url,
40+
@JsonKey(name: 'challenge_type') required String challengeType,
41+
String? duration,
42+
}) = _Upcoming;
43+
44+
factory Upcoming.fromJson(Map<String, dynamic> json) =>
45+
_$UpcomingFromJson(json);
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'contest_filter.freezed.dart';
4+
part 'contest_filter.g.dart';
5+
6+
@freezed
7+
class ContestFilter with _$ContestFilter {
8+
factory ContestFilter({
9+
int? duration,
10+
bool? ongoing,
11+
bool? upcoming,
12+
DateTime? startDate,
13+
List<dynamic>? platform,
14+
}) = _ContestFilter;
15+
16+
factory ContestFilter.fromJson(Map<String, dynamic> json) =>
17+
_$ContestFilterFromJson(json);
18+
}

lib/domain/models/feed.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
import 'submission.dart';
4+
5+
part 'feed.freezed.dart';
6+
part 'feed.g.dart';
7+
8+
@freezed
9+
class Feed with _$Feed {
10+
factory Feed({
11+
required String userId,
12+
required String username,
13+
required String? fullname,
14+
String? picture,
15+
Submission? submission,
16+
}) = _Feed;
17+
18+
factory Feed.fromJson(Map<String, dynamic> json) => _$FeedFromJson(json);
19+
}

lib/domain/models/following.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'following.freezed.dart';
4+
part 'following.g.dart';
5+
6+
@freezed
7+
class Following with _$Following {
8+
factory Following({
9+
required String id,
10+
required String username,
11+
String? fullname,
12+
String? picture,
13+
}) = _Following;
14+
15+
factory Following.fromJson(Map<String, dynamic> json) =>
16+
_$FollowingFromJson(json);
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'grouped_feed.freezed.dart';
4+
part 'grouped_feed.g.dart';
5+
6+
@freezed
7+
class GroupedFeed with _$GroupedFeed {
8+
factory GroupedFeed({
9+
required String username,
10+
required String userId,
11+
String? fullname,
12+
String? picture,
13+
String? name,
14+
String? url,
15+
String? language,
16+
List<Submissions>? submissions,
17+
}) = _GroupedFeed;
18+
19+
factory GroupedFeed.fromJson(Map<String, dynamic> json) =>
20+
_$GroupedFeedFromJson(json);
21+
}
22+
23+
@freezed
24+
class Submissions with _$Submissions {
25+
factory Submissions({
26+
DateTime? createdAt,
27+
String? status,
28+
int? points,
29+
List<String>? tags,
30+
int? rating,
31+
}) = _Submissions;
32+
33+
factory Submissions.fromJson(Map<String, dynamic> json) =>
34+
_$SubmissionsFromJson(json);
35+
}

lib/domain/models/submission.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'submission.freezed.dart';
4+
part 'submission.g.dart';
5+
6+
@freezed
7+
class Submission with _$Submission {
8+
factory Submission({
9+
required String name,
10+
String? url,
11+
@JsonKey(name: 'created_at') DateTime? createdAt,
12+
String? status,
13+
String? language,
14+
int? points,
15+
List<String>? tags,
16+
int? rating,
17+
}) = _Submission;
18+
19+
factory Submission.fromJson(Map<String, dynamic> json) =>
20+
_$SubmissionFromJson(json);
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'submission_status.freezed.dart';
4+
part 'submission_status.g.dart';
5+
6+
@freezed
7+
class SubmissionStatus with _$SubmissionStatus {
8+
factory SubmissionStatus({
9+
int? ac,
10+
int? ce,
11+
int? mle,
12+
int? ptl,
13+
int? re,
14+
int? tle,
15+
int? wa,
16+
}) = _SubmissionStatus;
17+
18+
factory SubmissionStatus.fromJson(Map<String, dynamic> json) =>
19+
_$SubmissionStatusFromJson(json);
20+
}

lib/domain/models/user.dart

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
/// Freezed example/template.
21
import 'package:freezed_annotation/freezed_annotation.dart';
32

3+
import 'handle.dart';
4+
import 'submission.dart';
5+
import 'user_profile.dart';
6+
47
part 'user.freezed.dart';
58
part 'user.g.dart';
69

710
@freezed
811
class User with _$User {
912
factory User({
10-
String? name,
11-
int? age,
13+
required String fullname,
14+
required String email,
15+
Handle? handle,
16+
String? id,
17+
String? institute,
18+
int? noOfFollowing,
19+
String? picture,
20+
UserProfile? profiles,
21+
List<Submission>? recentSubmissions,
22+
String? username,
23+
SolvedProblemsCount? solvedProblemsCount,
1224
}) = _User;
1325

1426
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
1527
}
28+
29+
@freezed
30+
class SolvedProblemsCount with _$SolvedProblemsCount {
31+
factory SolvedProblemsCount({
32+
int? codechef,
33+
int? codeforces,
34+
int? hackerrank,
35+
int? spoj,
36+
}) = _SolvedProblemsCount;
37+
38+
factory SolvedProblemsCount.fromJson(Map<String, dynamic> json) =>
39+
_$SolvedProblemsCountFromJson(json);
40+
}

0 commit comments

Comments
 (0)