@@ -5,10 +5,10 @@ import 'package:equatable/equatable.dart';
55import 'package:flutter_bloc/flutter_bloc.dart' ;
66import 'package:freezed_annotation/freezed_annotation.dart' ;
77
8+ import '../../../data/constants/strings.dart' ;
89import '../../../data/services/local/storage_service.dart' ;
910import '../../../domain/models/activity_details.dart' ;
1011import '../../../domain/models/following.dart' ;
11- import '../../../domain/models/submission.dart' ;
1212import '../../../domain/models/submission_status.dart' ;
1313import '../../../domain/models/user.dart' ;
1414import '../../../domain/repositories/user_repository.dart' ;
@@ -22,40 +22,31 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
2222 on < FetchDetails > (_onFetchDetails);
2323 on < UpdateYear > (_onUpdateYear);
2424 on < UpdateMonth > (_onUpdateMonth);
25+ on < ShowFollowing > (_onShowFollowing);
2526 }
2627
2728 void _onFetchDetails (FetchDetails event, Emitter <ProfileState > emit) async {
2829 if (! state.isLoading) emit (state.copyWith (isLoading: true ));
2930
3031 // Fetch user details
31- final User ? _user;
3232 if (event.userId.isEmpty) {
3333 _user = StorageService .user;
3434 } else {
3535 _user = await UserRepository .fetchUserDetails (uid: event.userId);
3636 }
3737
38- final _followingList = await UserRepository .getFollowingList ();
38+ _followingList = await UserRepository .getFollowingList ();
3939
4040 final _subStats = await UserRepository .getSubmissionStatusData (_user! .id! );
4141
42- final _submission = _user.recentSubmissions;
42+ final _activityDetails =
43+ await UserRepository .getActivityDetails (_user! .id! );
4344
44- final _activityDetails = await UserRepository .getActivityDetails (_user.id! );
45-
46- final _activity = < DateTime , dynamic > {};
4745 for (final activity in _activityDetails ?? < ActivityDetails > []) {
4846 if (activity.createdAt == null ) continue ;
4947 _activity[activity.createdAt! ] = activity.correct;
5048 }
5149
52- final _mostRecentSubmission = < Submission > [];
53- for (var index = 0 ;
54- index < (_submission? .length ?? 0 ) && index < 2 ;
55- index++ ) {
56- _mostRecentSubmission.add (_submission! [index]);
57- }
58-
5950 bool ? _isFollowing;
6051 for (final follower in _followingList ?? < Following > []) {
6152 if (follower.id == event.userId) {
@@ -71,12 +62,11 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
7162 user: _user,
7263 following: _followingList,
7364 submissionStatus: _subStats,
74- submission: _submission,
75- activity: _activity,
7665 personalProfile: event.userId.isEmpty,
7766 isFollowing: _isFollowing,
7867 currentYear: _currentYear,
7968 currentTriplet: _currentTriplet,
69+ showFollowing: false ,
8070 ));
8171 }
8272
@@ -107,6 +97,32 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
10797 ));
10898 }
10999
100+ void _onShowFollowing (ShowFollowing event, Emitter <ProfileState > emit) async {
101+ if (event.toShow) {
102+ _followingList = await UserRepository .getFollowingList ();
103+ }
104+ emit (state.copyWith (
105+ following: _followingList,
106+ showFollowing: event.toShow,
107+ ));
108+ }
109+
110+ static Future follow (String userId) async {
111+ final statuscode = await UserRepository .followUser (userId);
112+
113+ if (statuscode != 200 ) {
114+ throw Exception (AppStrings .genericError);
115+ }
116+ }
117+
118+ static Future unfollow (String userId) async {
119+ final statuscode = await UserRepository .unfollowUser (userId);
120+
121+ if (statuscode != 200 ) {
122+ throw Exception (AppStrings .genericError);
123+ }
124+ }
125+
110126 // Index -> Index%4
111127 static List <String > monthTriplet (int index) {
112128 switch (index) {
@@ -125,7 +141,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
125141 }
126142
127143 // Cell Color for Acceptance Graph
128- static Color getCellColor (int index) {
144+ Color getCellColor (DateTime date) {
145+ final index = _activity[date] ?? 0 ;
129146 if (index < 0 ) {
130147 return Color .fromRGBO (255 , 0 , 0 , math.min (- 0.2 * index, 1 ));
131148 } else if (index > 0 ) {
@@ -138,4 +155,7 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
138155
139156 int _currentYear = DateTime .now ().year;
140157 int _currentTriplet = DateTime .now ().month ~ / 3 ;
158+ List <Following >? _followingList;
159+ User ? _user;
160+ final Map <DateTime , dynamic > _activity = < DateTime , dynamic > {};
141161}
0 commit comments