Line data Source code
1 : import 'dart:io'; 2 : import 'dart:async'; 3 : import 'package:flutter_bloc/flutter_bloc.dart'; 4 : import 'package:equatable/equatable.dart'; 5 : import 'package:memories_app/routes/activity_stream/model/activity_stream_repository.dart'; 6 : import 'package:memories_app/routes/activity_stream/model/activity_stream_response_model.dart'; 7 : 8 : part 'activity_stream_event.dart'; 9 : part 'activity_stream_state.dart'; 10 : 11 : class _Constants { 12 : static const String offlineMessage = 13 : 'You are currently offline.\n Please check your internet connection!'; 14 : } 15 : 16 : class ActivityStreamBloc 17 : extends Bloc<ActivityStreamEvent, ActivityStreamState> { 18 : final ActivityStreamRepository _repository; 19 : 20 1 : ActivityStreamBloc({required ActivityStreamRepository repository}) 21 : : _repository = repository, 22 1 : super(const ActivityStreamInitial()) { 23 2 : on<ActivityStreamLoadDisplayEvent>(_loadDisplayEvent); 24 2 : on<ActivityStreamOnPressActivityEvent>(_viewActivityEvent); 25 : } 26 : 27 : List<Activity> likedStories = <Activity>[]; 28 : List<Activity> unlikedStories = <Activity>[]; 29 : List<Activity> followedUser = <Activity>[]; 30 : List<Activity> unfollowedUser = <Activity>[]; 31 : List<Activity> commentOnStory = <Activity>[]; 32 : List<Activity> commentStoryYouCommentedBefore = <Activity>[]; 33 : List<Activity> newStories = <Activity>[]; 34 : 35 1 : Future<void> _loadDisplayEvent(ActivityStreamLoadDisplayEvent event, 36 : Emitter<ActivityStreamState> emit) async { 37 : ActivityStreamResponseModel? response; 38 1 : await _getActivities(response, emit); 39 : } 40 : 41 1 : Future<void> _getActivities(ActivityStreamResponseModel? response, 42 : Emitter<ActivityStreamState> emit) async { 43 : try { 44 2 : response = await _repository.getActivities(); 45 0 : } on SocketException { 46 0 : emit(const ActivityStreamOffline( 47 : offlineMessage: _Constants.offlineMessage)); 48 : } catch (error) { 49 0 : emit(ActivityStreamFailure(error: error.toString())); 50 : } 51 : 52 : if (response != null) { 53 2 : if (response.success == true) { 54 2 : groupActivities(response.activity); 55 2 : emit(ActivityStreamDisplayState( 56 1 : allActivities: response.activity, 57 : showLoadingAnimation: false, 58 1 : likedStories: likedStories, 59 1 : commentOnStory: commentOnStory, 60 1 : commentStoryYouCommentedBefore: commentStoryYouCommentedBefore, 61 1 : followedUser: followedUser, 62 1 : newStories: newStories, 63 1 : unfollowedUser: unfollowedUser, 64 1 : unlikedStories: unlikedStories, 65 : )); 66 : } else { 67 0 : emit(ActivityStreamFailure(error: response.msg.toString())); 68 : } 69 : } 70 : } 71 : 72 1 : void groupActivities(List<Activity> activities) { 73 2 : for (Activity element in activities) { 74 3 : if (element.activityType == "new_story" && !element.viewed) { 75 2 : newStories.add(element); 76 : } 77 3 : if (element.activityType == "story_liked" && !element.viewed) { 78 2 : likedStories.add(element); 79 : } 80 3 : if (element.activityType == "story_unliked" && !element.viewed) { 81 2 : unlikedStories.add(element); 82 : } 83 3 : if (element.activityType == "followed_user" && !element.viewed) { 84 2 : followedUser.add(element); 85 : } 86 3 : if (element.activityType == "unfollowed_user" && !element.viewed) { 87 2 : unfollowedUser.add(element); 88 : } 89 3 : if (element.activityType == "new_commented_on_story" && !element.viewed) { 90 2 : commentOnStory.add(element); 91 : } 92 3 : if (element.activityType == "new_comment_on_comment" && !element.viewed) { 93 2 : commentStoryYouCommentedBefore.add(element); 94 : } 95 : } 96 : } 97 : 98 1 : Future<void> _viewActivityEvent(ActivityStreamOnPressActivityEvent event, 99 : Emitter<ActivityStreamState> emit) async { 100 3 : await _repository.viewActivity(event.activityId); 101 : } 102 : }