import 'dart:math'; import 'package:data/app_api_data/app_api_repository_impl.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:network_core/initializer/base_initializer.dart'; import 'package:network_core/network/networking/custom_exception.dart'; import 'package:repositories/app_api_domain/models/single_post_model.dart'; import 'package:repositories/app_api_domain/repository/app_api_repository.dart'; import 'package:sonnat/core/utils/base_cubit_type.dart'; import 'package:sonnat/features/single_post/view_models/comment.dart'; class SinglePostCubit extends Cubit> { List commentList = []; SinglePostModel? model; final AppApiRepository _remoteRepository = AppApiRepositoryImpl(apiService: BaseInitializer.instance.getApiService()); SinglePostCubit() : super(BaseCubitType(eventName: SinglePostState.empty)); void empty() => emit(BaseCubitType(eventName: SinglePostState.empty)); void addComment(String comment) { Comment newComment = Comment( date: DateTime.now().millisecondsSinceEpoch, text: comment, author: 'محسن زمانی', id: Random.secure().nextInt(10000), ); commentList.insert(0, newComment); emit(BaseCubitType(eventName: SinglePostState.data)); } void deleteComment(int id) { commentList.removeWhere((element) => element.id == id); emit(BaseCubitType(eventName: SinglePostState.data)); } Future getSinglePostData({required int postId}) async { try { emit(BaseCubitType(eventName: SinglePostState.loading)); model = await _remoteRepository.getSinglePost(postId: postId); emit(BaseCubitType(eventName: SinglePostState.data)); } on CustomException { emit(BaseCubitType(eventName: SinglePostState.error)); } } } enum SinglePostState { empty, loading, error, data, }