LCOV - code coverage report
Current view: top level - recommendations/bloc - recommendations_bloc.dart (source / functions) Hit Total Coverage
Test: SWE574 - Fall2023 - Group1 - Mobile Test Coverage Lines: 36 40 90.0 %
Date: 2023-12-31 10:28:05 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : // ignore_for_file: avoid_function_literals_in_foreach_calls
       2             : 
       3             : import 'dart:io';
       4             : import 'dart:async';
       5             : 
       6             : import 'package:flutter_bloc/flutter_bloc.dart';
       7             : import 'package:equatable/equatable.dart';
       8             : import 'package:intl/intl.dart';
       9             : import 'package:memories_app/routes/home/model/story_model.dart';
      10             : import 'package:memories_app/routes/recommendations/model/recommendations_repository.dart';
      11             : import 'package:memories_app/routes/recommendations/model/recommendations_response.dart';
      12             : 
      13             : part 'recommendations_event.dart';
      14             : part 'recommendations_state.dart';
      15             : 
      16             : class _Constants {
      17             :   static const String offlineMessage =
      18             :       'You are currently offline.\n Please check your internet connection!';
      19             : }
      20             : 
      21             : class RecommendationsBloc
      22             :     extends Bloc<RecommendationsEvent, RecommendationsState> {
      23             :   final RecommendationsRepository _repository;
      24             : 
      25           1 :   RecommendationsBloc({required RecommendationsRepository repository})
      26             :       : _repository = repository,
      27           1 :         super(const RecommendationsInitial()) {
      28           2 :     on<RecommendationsLoadDisplayEvent>(_recommendationsEvent);
      29           2 :     on<RecommendationsEventRefreshStories>(_refreshStories);
      30             :   }
      31             : 
      32           1 :   Future<void> _recommendationsEvent(RecommendationsLoadDisplayEvent event,
      33             :       Emitter<RecommendationsState> emit) async {
      34             :     RecommendationsResponseModel? response;
      35           1 :     await _getRecommendations(response, emit);
      36             :   }
      37             : 
      38           1 :   Future<void> _refreshStories(RecommendationsEventRefreshStories event,
      39             :       Emitter<RecommendationsState> emit) async {
      40             :     RecommendationsResponseModel? response;
      41             : 
      42           1 :     await _getRecommendations(response, emit);
      43             :   }
      44             : 
      45           1 :   Future<void> _getRecommendations(RecommendationsResponseModel? response,
      46             :       Emitter<RecommendationsState> emit) async {
      47             :     try {
      48           2 :       response = await _repository.getRecommendations();
      49           0 :     } on SocketException {
      50           0 :       emit(const RecommendationsOffline(
      51             :           offlineMessage: _Constants.offlineMessage));
      52             :     } catch (error) {
      53           0 :       emit(RecommendationsFailure(error: error.toString()));
      54             :     }
      55             :     if (response != null) {
      56           2 :       if (response.success == true &&
      57           1 :           response.recommendations != null &&
      58           2 :           response.recommendations!.isNotEmpty) {
      59           1 :         response.recommendations!
      60           2 :             .forEach((Recommendation recommendation) async {
      61           2 :           recommendation.story!.dateText =
      62           2 :               getFormattedDate(recommendation.story!);
      63             :         });
      64             : 
      65           2 :         emit(RecommendationsDisplayState(
      66           1 :             recommendations: response.recommendations!,
      67             :             showLoadingAnimation: false));
      68             :       } else {
      69           0 :         emit(RecommendationsFailure(error: response.msg.toString()));
      70             :       }
      71             :     }
      72             :   }
      73             : 
      74           1 :   String getFormattedDate(StoryModel story) {
      75           1 :     switch (story.dateType) {
      76           1 :       case 'year':
      77           3 :         return 'Year: ${story.year?.toString() ?? ''}';
      78           1 :       case 'decade':
      79           3 :         return 'Decade: ${story.decade?.toString() ?? ''}';
      80           1 :       case 'year_interval':
      81           5 :         return 'Start: ${story.startYear.toString()} \nEnd: ${story.endYear.toString()}';
      82           1 :       case 'normal_date':
      83           2 :         return formatDate(story.date) ?? '';
      84           1 :       case 'interval_date':
      85           5 :         return 'Start: ${formatDate(story.startDate)} \nEnd: ${formatDate(story.endDate)}';
      86             :       default:
      87             :         return '';
      88             :     }
      89             :   }
      90             : 
      91           1 :   String? formatDate(String? dateString) {
      92             :     if (dateString == null) {
      93             :       return null;
      94             :     }
      95             : 
      96             :     try {
      97           1 :       DateTime dateTime = DateTime.parse(dateString);
      98             : 
      99             :       bool isMidnight =
     100           6 :           dateTime.hour == 0 && dateTime.minute == 0 && dateTime.second == 0;
     101             : 
     102             :       String pattern = isMidnight ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm';
     103             : 
     104           3 :       String formattedDate = DateFormat(pattern).format(dateTime.toLocal());
     105             : 
     106             :       return formattedDate;
     107             :     } catch (e) {
     108             :       // ignore: avoid_print
     109           2 :       print('Error parsing date: $e');
     110             :       return null;
     111             :     }
     112             :   }
     113             : }

Generated by: LCOV version 1.14