Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

53 lines
1.8 KiB

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<BaseCubitType<SinglePostState>> {
List<Comment> 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<void> 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,
}