|
| 1 | +import 'dart:math' as math; |
| 2 | +import 'dart:ui'; |
| 3 | + |
| 4 | +import 'package:equatable/equatable.dart'; |
| 5 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 6 | +import 'package:freezed_annotation/freezed_annotation.dart'; |
| 7 | + |
| 8 | +import '../../../data/services/local/storage_service.dart'; |
| 9 | +import '../../../domain/models/activity_details.dart'; |
| 10 | +import '../../../domain/models/following.dart'; |
| 11 | +import '../../../domain/models/submission.dart'; |
| 12 | +import '../../../domain/models/submission_status.dart'; |
| 13 | +import '../../../domain/models/user.dart'; |
| 14 | +import '../../../domain/repositories/user_repository.dart'; |
| 15 | + |
| 16 | +part 'profile_event.dart'; |
| 17 | +part 'profile_state.dart'; |
| 18 | +part 'profile_bloc.freezed.dart'; |
| 19 | + |
| 20 | +class ProfileBloc extends Bloc<ProfileEvent, ProfileState> { |
| 21 | + ProfileBloc() : super(const ProfileState()) { |
| 22 | + on<FetchDetails>(_onFetchDetails); |
| 23 | + on<UpdateYear>(_onUpdateYear); |
| 24 | + on<UpdateMonth>(_onUpdateMonth); |
| 25 | + } |
| 26 | + |
| 27 | + void _onFetchDetails(FetchDetails event, Emitter<ProfileState> emit) async { |
| 28 | + if (!state.isLoading) emit(state.copyWith(isLoading: true)); |
| 29 | + |
| 30 | + // Fetch user details |
| 31 | + final User? _user; |
| 32 | + if (event.userId.isEmpty) { |
| 33 | + _user = StorageService.user; |
| 34 | + } else { |
| 35 | + _user = await UserRepository.fetchUserDetails(uid: event.userId); |
| 36 | + } |
| 37 | + |
| 38 | + final _followingList = await UserRepository.getFollowingList(); |
| 39 | + |
| 40 | + final _subStats = await UserRepository.getSubmissionStatusData(_user!.id!); |
| 41 | + |
| 42 | + final _submission = _user.recentSubmissions; |
| 43 | + |
| 44 | + final _activityDetails = await UserRepository.getActivityDetails(_user.id!); |
| 45 | + |
| 46 | + final _activity = <DateTime, dynamic>{}; |
| 47 | + for (final activity in _activityDetails ?? <ActivityDetails>[]) { |
| 48 | + if (activity.createdAt == null) continue; |
| 49 | + _activity[activity.createdAt!] = activity.correct; |
| 50 | + } |
| 51 | + |
| 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 | + |
| 59 | + bool? _isFollowing; |
| 60 | + for (final follower in _followingList ?? <Following>[]) { |
| 61 | + if (follower.id == event.userId) { |
| 62 | + _isFollowing = true; |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + _isFollowing ??= false; |
| 68 | + |
| 69 | + emit(state.copyWith( |
| 70 | + isLoading: false, |
| 71 | + user: _user, |
| 72 | + following: _followingList, |
| 73 | + submissionStatus: _subStats, |
| 74 | + submission: _submission, |
| 75 | + activity: _activity, |
| 76 | + personalProfile: event.userId.isEmpty, |
| 77 | + isFollowing: _isFollowing, |
| 78 | + currentYear: _currentYear, |
| 79 | + currentTriplet: _currentTriplet, |
| 80 | + )); |
| 81 | + } |
| 82 | + |
| 83 | + void _onUpdateYear(UpdateYear event, Emitter<ProfileState> emit) { |
| 84 | + if (event.increment) { |
| 85 | + _currentYear++; |
| 86 | + } else { |
| 87 | + _currentYear--; |
| 88 | + } |
| 89 | + |
| 90 | + emit(state.copyWith(currentYear: _currentYear)); |
| 91 | + } |
| 92 | + |
| 93 | + void _onUpdateMonth(UpdateMonth event, Emitter<ProfileState> emit) { |
| 94 | + if (event.increment) { |
| 95 | + if (_currentTriplet == 3) _currentYear++; |
| 96 | + _currentTriplet++; |
| 97 | + } else { |
| 98 | + if (_currentTriplet == 0) _currentYear--; |
| 99 | + _currentTriplet--; |
| 100 | + } |
| 101 | + |
| 102 | + _currentTriplet %= 4; |
| 103 | + |
| 104 | + emit(state.copyWith( |
| 105 | + currentTriplet: _currentTriplet, |
| 106 | + currentYear: _currentYear, |
| 107 | + )); |
| 108 | + } |
| 109 | + |
| 110 | + // Index -> Index%4 |
| 111 | + static List<String> monthTriplet(int index) { |
| 112 | + switch (index) { |
| 113 | + case 0: |
| 114 | + return ['Jan', 'Feb', 'Mar']; |
| 115 | + |
| 116 | + case 1: |
| 117 | + return ['Apr', 'May', 'Jun']; |
| 118 | + |
| 119 | + case 2: |
| 120 | + return ['Jul', 'Aug', 'Sep']; |
| 121 | + |
| 122 | + default: |
| 123 | + return ['Oct', 'Nov', 'Dec']; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Cell Color for Acceptance Graph |
| 128 | + static Color getCellColor(int index) { |
| 129 | + if (index < 0) { |
| 130 | + return Color.fromRGBO(255, 0, 0, math.min(-0.2 * index, 1)); |
| 131 | + } else if (index > 0) { |
| 132 | + return Color.fromRGBO(0, 255, 0, math.min(0.2 * index, 1)); |
| 133 | + } |
| 134 | + |
| 135 | + // Default cell color |
| 136 | + return const Color(0xFFEEEEEE); |
| 137 | + } |
| 138 | + |
| 139 | + int _currentYear = DateTime.now().year; |
| 140 | + int _currentTriplet = DateTime.now().month ~/ 3; |
| 141 | +} |
0 commit comments