elCaribe app - customization and branding
This commit is contained in:
86
news-app/lib/cubits/AddNewsCubit.dart
Normal file
86
news-app/lib/cubits/AddNewsCubit.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/AddNews/addNewsRepository.dart';
|
||||
|
||||
abstract class AddNewsState {}
|
||||
|
||||
class AddNewsInitial extends AddNewsState {}
|
||||
|
||||
class AddNewsFetchInProgress extends AddNewsState {}
|
||||
|
||||
class AddNewsFetchSuccess extends AddNewsState {
|
||||
var addNews;
|
||||
|
||||
AddNewsFetchSuccess({required this.addNews});
|
||||
}
|
||||
|
||||
class AddNewsFetchFailure extends AddNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
AddNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class AddNewsCubit extends Cubit<AddNewsState> {
|
||||
final AddNewsRepository _addNewsRepository;
|
||||
|
||||
AddNewsCubit(this._addNewsRepository) : super(AddNewsInitial());
|
||||
|
||||
void addNews(
|
||||
{required BuildContext context,
|
||||
required String actionType,
|
||||
required String catId,
|
||||
required String title,
|
||||
required String conTypeId,
|
||||
required String conType,
|
||||
required String langId,
|
||||
File? image,
|
||||
String? newsId,
|
||||
String? subCatId,
|
||||
String? showTill,
|
||||
String? tagId,
|
||||
String? url,
|
||||
String? desc,
|
||||
String? summDescription,
|
||||
String? locationId,
|
||||
File? videoUpload,
|
||||
List<File>? otherImage,
|
||||
String? publishDate,
|
||||
required String metaTitle,
|
||||
required String metaDescription,
|
||||
required String metaKeyword,
|
||||
required String slug,
|
||||
required int isDraft}) async {
|
||||
try {
|
||||
emit(AddNewsFetchInProgress());
|
||||
final result = await _addNewsRepository.addNews(
|
||||
context: context,
|
||||
actionType: actionType,
|
||||
newsId: newsId,
|
||||
title: title,
|
||||
image: image,
|
||||
conTypeId: conTypeId,
|
||||
conType: conType,
|
||||
langId: langId,
|
||||
catId: catId,
|
||||
videoUpload: videoUpload,
|
||||
url: url,
|
||||
tagId: tagId,
|
||||
otherImage: otherImage,
|
||||
desc: desc,
|
||||
showTill: showTill,
|
||||
subCatId: subCatId,
|
||||
locationId: locationId,
|
||||
metaTitle: metaTitle,
|
||||
metaDescription: metaDescription,
|
||||
metaKeyword: metaKeyword,
|
||||
slug: slug,
|
||||
publishDate: publishDate ?? null,
|
||||
summDescription: summDescription,
|
||||
isDraft: isDraft);
|
||||
emit(AddNewsFetchSuccess(addNews: result));
|
||||
} catch (e) {
|
||||
emit(AddNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
98
news-app/lib/cubits/Auth/authCubit.dart
Normal file
98
news-app/lib/cubits/Auth/authCubit.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/AuthModel.dart';
|
||||
import 'package:news/data/repositories/Auth/authRepository.dart';
|
||||
|
||||
const String loginEmail = "email";
|
||||
const String loginGmail = "gmail";
|
||||
const String loginFb = "fb";
|
||||
const String loginMbl = "mobile";
|
||||
const String loginApple = "apple";
|
||||
|
||||
enum AuthProviders { gmail, fb, apple, mobile, email }
|
||||
|
||||
@immutable
|
||||
abstract class AuthState {}
|
||||
|
||||
class AuthInitial extends AuthState {}
|
||||
|
||||
class Authenticated extends AuthState {
|
||||
//to store authDetails
|
||||
final AuthModel authModel;
|
||||
|
||||
Authenticated({required this.authModel});
|
||||
}
|
||||
|
||||
class Unauthenticated extends AuthState {}
|
||||
|
||||
class AuthCubit extends Cubit<AuthState> {
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
AuthCubit(this._authRepository) : super(AuthInitial()) {
|
||||
checkAuthStatus();
|
||||
}
|
||||
|
||||
AuthRepository get authRepository => _authRepository;
|
||||
|
||||
void checkAuthStatus() {
|
||||
//authDetails is map. keys are isLogin,userId,authProvider
|
||||
final authDetails = _authRepository.getLocalAuthDetails();
|
||||
|
||||
(authDetails['isLogIn']) ? emit(Authenticated(authModel: AuthModel.fromJson(authDetails))) : emit(Unauthenticated());
|
||||
}
|
||||
|
||||
String getUserId() {
|
||||
return (state is Authenticated) ? (state as Authenticated).authModel.id! : "0";
|
||||
}
|
||||
|
||||
String getProfile() {
|
||||
return (state is Authenticated)
|
||||
? ((state as Authenticated).authModel.profile!.trim().isNotEmpty)
|
||||
? (state as Authenticated).authModel.profile!
|
||||
: ""
|
||||
: "";
|
||||
}
|
||||
|
||||
String getMobile() {
|
||||
return (state is Authenticated) ? (state as Authenticated).authModel.mobile! : "";
|
||||
}
|
||||
|
||||
String getType() {
|
||||
return (state is Authenticated) ? (state as Authenticated).authModel.type! : "";
|
||||
}
|
||||
|
||||
String getAuthorBio() {
|
||||
return (state is Authenticated && (state as Authenticated).authModel.authorDetails != null) ? ((state as Authenticated).authModel.authorDetails?.bio ?? "") : "";
|
||||
}
|
||||
|
||||
String getAuthorWhatsappLink() {
|
||||
return (state is Authenticated && (state as Authenticated).authModel.authorDetails != null) ? (state as Authenticated).authModel.authorDetails?.whatsappLink ?? "" : "0";
|
||||
}
|
||||
|
||||
String getAuthorTelegramLink() {
|
||||
return (state is Authenticated && (state as Authenticated).authModel.authorDetails != null) ? (state as Authenticated).authModel.authorDetails?.telegramLink ?? "" : "0";
|
||||
}
|
||||
|
||||
String getAuthorFacebookLink() {
|
||||
return (state is Authenticated && (state as Authenticated).authModel.authorDetails != null) ? (state as Authenticated).authModel.authorDetails?.facebookLink ?? "" : "0";
|
||||
}
|
||||
|
||||
String getAuthorLinkedInLink() {
|
||||
return (state is Authenticated && (state as Authenticated).authModel.authorDetails != null) ? (state as Authenticated).authModel.authorDetails?.linkedinLink ?? "" : "0";
|
||||
}
|
||||
|
||||
void updateDetails({required AuthModel authModel}) {
|
||||
emit(Authenticated(authModel: authModel));
|
||||
}
|
||||
|
||||
Future signOut(AuthProviders authProvider) async {
|
||||
if (state is Authenticated) {
|
||||
_authRepository.signOut(authProvider);
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
}
|
||||
|
||||
bool isAuthor() {
|
||||
return (state is Authenticated) ? ((state as Authenticated).authModel.isAuthor == 1) : false;
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/Auth/deleteUserCubit.dart
Normal file
37
news-app/lib/cubits/Auth/deleteUserCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/Auth/authRepository.dart';
|
||||
|
||||
abstract class DeleteUserState {}
|
||||
|
||||
class DeleteUserInitial extends DeleteUserState {}
|
||||
|
||||
class DeleteUserFetchInProgress extends DeleteUserState {}
|
||||
|
||||
class DeleteUserFetchSuccess extends DeleteUserState {
|
||||
dynamic deleteUser;
|
||||
|
||||
DeleteUserFetchSuccess({required this.deleteUser});
|
||||
}
|
||||
|
||||
class DeleteUserFetchFailure extends DeleteUserState {
|
||||
final String errorMessage;
|
||||
|
||||
DeleteUserFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class DeleteUserCubit extends Cubit<DeleteUserState> {
|
||||
final AuthRepository _deleteUserRepository;
|
||||
|
||||
DeleteUserCubit(this._deleteUserRepository) : super(DeleteUserInitial());
|
||||
|
||||
Future<dynamic> deleteUser({String? name, String? mobile, String? email, String? filePath}) async {
|
||||
try {
|
||||
emit(DeleteUserFetchInProgress());
|
||||
final result = await _deleteUserRepository.deleteUser();
|
||||
emit(DeleteUserFetchSuccess(deleteUser: result));
|
||||
return result;
|
||||
} catch (e) {
|
||||
emit(DeleteUserFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/Auth/registerTokenCubit.dart
Normal file
37
news-app/lib/cubits/Auth/registerTokenCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/Auth/authRepository.dart';
|
||||
|
||||
@immutable
|
||||
abstract class RegisterTokenState {}
|
||||
|
||||
class RegisterTokenInitial extends RegisterTokenState {}
|
||||
|
||||
class RegisterTokenProgress extends RegisterTokenState {
|
||||
RegisterTokenProgress();
|
||||
}
|
||||
|
||||
class RegisterTokenSuccess extends RegisterTokenState {
|
||||
RegisterTokenSuccess();
|
||||
}
|
||||
|
||||
class RegisterTokenFailure extends RegisterTokenState {
|
||||
final String errorMessage;
|
||||
|
||||
RegisterTokenFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class RegisterTokenCubit extends Cubit<RegisterTokenState> {
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
RegisterTokenCubit(this._authRepository) : super(RegisterTokenInitial());
|
||||
|
||||
void registerToken({required String fcmId, required BuildContext context}) {
|
||||
emit(RegisterTokenProgress());
|
||||
_authRepository.registerToken(fcmId: fcmId, context: context).then((result) {
|
||||
emit(RegisterTokenSuccess());
|
||||
}).catchError((e) {
|
||||
emit(RegisterTokenFailure(e.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
40
news-app/lib/cubits/Auth/socialSignUpCubit.dart
Normal file
40
news-app/lib/cubits/Auth/socialSignUpCubit.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
import 'package:news/data/models/AuthModel.dart';
|
||||
import 'package:news/data/repositories/Auth/authRepository.dart';
|
||||
import 'package:news/cubits/Auth/authCubit.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SocialSignUpState {}
|
||||
|
||||
class SocialSignUpInitial extends SocialSignUpState {}
|
||||
|
||||
class SocialSignUpProgress extends SocialSignUpState {}
|
||||
|
||||
class SocialSignUpSuccess extends SocialSignUpState {
|
||||
final AuthModel authModel;
|
||||
|
||||
SocialSignUpSuccess({required this.authModel});
|
||||
}
|
||||
|
||||
class SocialSignUpFailure extends SocialSignUpState {
|
||||
final String errorMessage;
|
||||
|
||||
SocialSignUpFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SocialSignUpCubit extends Cubit<SocialSignUpState> {
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
SocialSignUpCubit(this._authRepository) : super(SocialSignUpInitial());
|
||||
|
||||
void socialSignUpUser({required AuthProviders authProvider, required BuildContext context, String? email, String? password, String? otp, String? verifiedId}) {
|
||||
emit(SocialSignUpProgress());
|
||||
_authRepository.signInUser(email: email, otp: otp, password: password, verifiedId: verifiedId, authProvider: authProvider, context: context).then((result) {
|
||||
emit(SocialSignUpSuccess(authModel: AuthModel.fromJson(result[DATA])));
|
||||
}).catchError((e) {
|
||||
emit(SocialSignUpFailure(e.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
61
news-app/lib/cubits/Auth/updateUserCubit.dart
Normal file
61
news-app/lib/cubits/Auth/updateUserCubit.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/cubits/Auth/authCubit.dart';
|
||||
import 'package:news/data/models/AuthModel.dart';
|
||||
import 'package:news/data/repositories/Auth/authRepository.dart';
|
||||
|
||||
abstract class UpdateUserState {}
|
||||
|
||||
class UpdateUserInitial extends UpdateUserState {}
|
||||
|
||||
class UpdateUserFetchInProgress extends UpdateUserState {}
|
||||
|
||||
class UpdateUserFetchSuccess extends UpdateUserState {
|
||||
AuthModel? updatedUser;
|
||||
String? imgUpdatedPath;
|
||||
|
||||
UpdateUserFetchSuccess({this.updatedUser, this.imgUpdatedPath});
|
||||
}
|
||||
|
||||
class UpdateUserFetchFailure extends UpdateUserState {
|
||||
final String errorMessage;
|
||||
|
||||
UpdateUserFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class UpdateUserCubit extends Cubit<UpdateUserState> {
|
||||
final AuthRepository _updateUserRepository;
|
||||
|
||||
UpdateUserCubit(this._updateUserRepository) : super(UpdateUserInitial());
|
||||
|
||||
void setUpdateUser(
|
||||
{String? name,
|
||||
String? mobile,
|
||||
String? email,
|
||||
String? filePath,
|
||||
required BuildContext context,
|
||||
String? authorBio,
|
||||
String? whatsappLink,
|
||||
String? facebookLink,
|
||||
String? telegramLink,
|
||||
String? linkedInLink}) async {
|
||||
try {
|
||||
emit(UpdateUserFetchInProgress());
|
||||
final Map<String, dynamic> result = await _updateUserRepository.updateUserData(
|
||||
mobile: mobile,
|
||||
name: name,
|
||||
email: email,
|
||||
filePath: filePath,
|
||||
authorBio: authorBio,
|
||||
whatsappLink: whatsappLink,
|
||||
facebookLink: facebookLink,
|
||||
telegramLink: telegramLink,
|
||||
linkedInLink: linkedInLink);
|
||||
//only incase of name,mobile & mail, not Profile Picture
|
||||
context.read<AuthCubit>().updateDetails(authModel: AuthModel.fromJson(result["data"]));
|
||||
emit(UpdateUserFetchSuccess(updatedUser: AuthModel.fromJson(result["data"])));
|
||||
} catch (e) {
|
||||
emit(UpdateUserFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
43
news-app/lib/cubits/Author/authorCubit.dart
Normal file
43
news-app/lib/cubits/Author/authorCubit.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class AuthorState {}
|
||||
|
||||
class AuthorInitial extends AuthorState {}
|
||||
|
||||
class AuthorInProgress extends AuthorState {}
|
||||
|
||||
class AuthorApproved extends AuthorState {
|
||||
AuthorApproved();
|
||||
}
|
||||
|
||||
class AuthorRequestSent extends AuthorState {
|
||||
final String responseMessage;
|
||||
AuthorRequestSent(this.responseMessage);
|
||||
}
|
||||
|
||||
class AuthorPending extends AuthorState {
|
||||
final String errorMessage;
|
||||
|
||||
AuthorPending(this.errorMessage);
|
||||
}
|
||||
|
||||
class AuthorRejected extends AuthorState {
|
||||
final String errorMessage;
|
||||
|
||||
AuthorRejected(this.errorMessage);
|
||||
}
|
||||
|
||||
class AuthorCubit extends Cubit<AuthorState> {
|
||||
AuthorCubit() : super(AuthorInitial());
|
||||
|
||||
void requestToBecomeAuthor() async {
|
||||
try {
|
||||
final result = await Api.sendApiRequest(body: {}, url: Api.becomeAnAuthorApi);
|
||||
(!result[ERROR]) ? emit(AuthorRequestSent(result[MESSAGE])) : emit(AuthorPending(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(AuthorPending(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
49
news-app/lib/cubits/Author/authorNewsCubit.dart
Normal file
49
news-app/lib/cubits/Author/authorNewsCubit.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class AuthorNewsState {}
|
||||
|
||||
class AuthorNewsInitial extends AuthorNewsState {}
|
||||
|
||||
class AuthorNewsFetchInProgress extends AuthorNewsState {}
|
||||
|
||||
class AuthorNewsFetchSuccess extends AuthorNewsState {
|
||||
final List<NewsModel> AuthorNewsList;
|
||||
final UserAuthorModel authorData;
|
||||
final int totalAuthorNewsCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
AuthorNewsFetchSuccess({required this.AuthorNewsList, required this.authorData, required this.totalAuthorNewsCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class AuthorNewsFetchFailed extends AuthorNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
AuthorNewsFetchFailed(this.errorMessage);
|
||||
}
|
||||
|
||||
class AuthorNewsCubit extends Cubit<AuthorNewsState> {
|
||||
AuthorNewsCubit() : super(AuthorNewsInitial());
|
||||
|
||||
void getAuthorNews({required String authorId}) async {
|
||||
try {
|
||||
emit(AuthorNewsInitial());
|
||||
final apiUrl = "${Api.getAuthorNewsApi}/${authorId}";
|
||||
final result = await Api.sendApiRequest(body: null, url: apiUrl, isGet: true);
|
||||
|
||||
(!result[ERROR])
|
||||
? emit(AuthorNewsFetchSuccess(
|
||||
AuthorNewsList: (result[DATA][NEWS][DATA] as List).map((e) => NewsModel.fromJson(e)).toList(),
|
||||
authorData: UserAuthorModel.fromJson(result[DATA][USER]),
|
||||
totalAuthorNewsCount: result[DATA][NEWS][TOTAL],
|
||||
hasMore: false,
|
||||
hasMoreFetchError: false))
|
||||
: emit(AuthorNewsFetchFailed(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(AuthorNewsFetchFailed(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
38
news-app/lib/cubits/Bookmark/UpdateBookmarkCubit.dart
Normal file
38
news-app/lib/cubits/Bookmark/UpdateBookmarkCubit.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/Bookmark/bookmarkRepository.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
|
||||
abstract class UpdateBookmarkStatusState {}
|
||||
|
||||
class UpdateBookmarkStatusInitial extends UpdateBookmarkStatusState {}
|
||||
|
||||
class UpdateBookmarkStatusInProgress extends UpdateBookmarkStatusState {}
|
||||
|
||||
class UpdateBookmarkStatusSuccess extends UpdateBookmarkStatusState {
|
||||
final NewsModel news;
|
||||
final bool wasBookmarkNewsProcess; //to check that process of Bookmark done or not
|
||||
UpdateBookmarkStatusSuccess(this.news, this.wasBookmarkNewsProcess);
|
||||
}
|
||||
|
||||
class UpdateBookmarkStatusFailure extends UpdateBookmarkStatusState {
|
||||
final String errorMessage;
|
||||
|
||||
UpdateBookmarkStatusFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class UpdateBookmarkStatusCubit extends Cubit<UpdateBookmarkStatusState> {
|
||||
final BookmarkRepository bookmarkRepository;
|
||||
|
||||
UpdateBookmarkStatusCubit(this.bookmarkRepository) : super(UpdateBookmarkStatusInitial());
|
||||
|
||||
void setBookmarkNews({required NewsModel news, required String status}) {
|
||||
emit(UpdateBookmarkStatusInProgress());
|
||||
bookmarkRepository.setBookmark(newsId: (news.newsId != null) ? news.newsId! : news.id!, status: status).then((value) {
|
||||
emit(UpdateBookmarkStatusSuccess(news, status == "1" ? true : false));
|
||||
}).catchError((e) {
|
||||
ApiMessageAndCodeException apiMessageAndCodeException = e;
|
||||
emit(UpdateBookmarkStatusFailure(apiMessageAndCodeException.errorMessage.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
111
news-app/lib/cubits/Bookmark/bookmarkCubit.dart
Normal file
111
news-app/lib/cubits/Bookmark/bookmarkCubit.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/Bookmark/bookmarkRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class BookmarkState {}
|
||||
|
||||
class BookmarkInitial extends BookmarkState {}
|
||||
|
||||
class BookmarkFetchInProgress extends BookmarkState {}
|
||||
|
||||
class BookmarkFetchSuccess extends BookmarkState {
|
||||
final List<NewsModel> bookmark;
|
||||
final int totalBookmarkCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
BookmarkFetchSuccess({required this.bookmark, required this.totalBookmarkCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class BookmarkFetchFailure extends BookmarkState {
|
||||
final String errorMessage;
|
||||
|
||||
BookmarkFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class BookmarkCubit extends Cubit<BookmarkState> {
|
||||
final BookmarkRepository bookmarkRepository;
|
||||
int perPageLimit = 25;
|
||||
|
||||
BookmarkCubit(this.bookmarkRepository) : super(BookmarkInitial());
|
||||
|
||||
void getBookmark({required String langId}) async {
|
||||
emit(BookmarkFetchInProgress());
|
||||
|
||||
try {
|
||||
final result = await bookmarkRepository.getBookmark(limit: perPageLimit.toString(), offset: "0", langId: langId);
|
||||
|
||||
if (result[ERROR]) {
|
||||
final message = result[MESSAGE];
|
||||
if (message == "No Data Found") {
|
||||
emit(BookmarkFetchSuccess(bookmark: [], totalBookmarkCount: 0, hasMoreFetchError: false, hasMore: false));
|
||||
} else {
|
||||
emit(BookmarkFetchFailure(message));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final bookmarks = result['Bookmark'] as List<NewsModel>;
|
||||
final total = result[TOTAL] as int;
|
||||
|
||||
emit(BookmarkFetchSuccess(bookmark: bookmarks, totalBookmarkCount: total, hasMoreFetchError: false, hasMore: bookmarks.length < total));
|
||||
} catch (e) {
|
||||
final error = e.toString();
|
||||
emit(error == "No Data Found" ? BookmarkFetchSuccess(bookmark: [], totalBookmarkCount: 0, hasMoreFetchError: false, hasMore: false) : BookmarkFetchFailure(error));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreBookmark() {
|
||||
return (state is BookmarkFetchSuccess) ? (state as BookmarkFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreBookmark({required String langId}) async {
|
||||
if (state is BookmarkFetchSuccess) {
|
||||
try {
|
||||
final result = await bookmarkRepository.getBookmark(limit: perPageLimit.toString(), offset: (state as BookmarkFetchSuccess).bookmark.length.toString(), langId: langId);
|
||||
List<NewsModel> updatedResults = (state as BookmarkFetchSuccess).bookmark;
|
||||
updatedResults.addAll(result['Bookmark'] as List<NewsModel>);
|
||||
emit(BookmarkFetchSuccess(bookmark: updatedResults, totalBookmarkCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(BookmarkFetchSuccess(
|
||||
bookmark: (state as BookmarkFetchSuccess).bookmark,
|
||||
hasMoreFetchError: (e.toString() == "No Data Found") ? false : true,
|
||||
totalBookmarkCount: (state as BookmarkFetchSuccess).totalBookmarkCount,
|
||||
hasMore: (state as BookmarkFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void addBookmarkNews(NewsModel model) {
|
||||
if (state is BookmarkFetchSuccess) {
|
||||
List<NewsModel> bookmarklist = [];
|
||||
bookmarklist.insert(0, model);
|
||||
bookmarklist.addAll((state as BookmarkFetchSuccess).bookmark);
|
||||
|
||||
emit(BookmarkFetchSuccess(
|
||||
bookmark: List.from(bookmarklist), hasMoreFetchError: true, totalBookmarkCount: (state as BookmarkFetchSuccess).totalBookmarkCount, hasMore: (state as BookmarkFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
|
||||
void removeBookmarkNews(NewsModel model) {
|
||||
if (state is BookmarkFetchSuccess) {
|
||||
final bookmark = (state as BookmarkFetchSuccess).bookmark;
|
||||
bookmark.removeWhere(((element) => (element.id == model.id || element.newsId == model.id)));
|
||||
emit(BookmarkFetchSuccess(
|
||||
bookmark: List.from(bookmark), hasMoreFetchError: true, totalBookmarkCount: (state as BookmarkFetchSuccess).totalBookmarkCount, hasMore: (state as BookmarkFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
|
||||
bool isNewsBookmark(String newsId) {
|
||||
if (state is BookmarkFetchSuccess) {
|
||||
final bookmark = (state as BookmarkFetchSuccess).bookmark;
|
||||
return (bookmark.isNotEmpty) ? (bookmark.indexWhere((element) => (element.newsId == newsId || element.id == newsId)) != -1) : false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void resetState() {
|
||||
emit(BookmarkFetchInProgress());
|
||||
}
|
||||
}
|
||||
79
news-app/lib/cubits/ConnectivityCubit.dart
Normal file
79
news-app/lib/cubits/ConnectivityCubit.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class ConnectivityCubit extends Cubit<ConnectivityState> {
|
||||
final ConnectivityService _connectivityService;
|
||||
late StreamSubscription _subscription;
|
||||
|
||||
ConnectivityCubit(this._connectivityService) : super(ConnectivityInitial()) {
|
||||
_startMonitoring();
|
||||
}
|
||||
|
||||
void _startMonitoring() {
|
||||
_subscription = _connectivityService.connectivityStream.listen((result) {
|
||||
if (result == ConnectivityResult.none) {
|
||||
emit(ConnectivityDisconnected());
|
||||
} else {
|
||||
emit(ConnectivityConnected(result));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> checkInitialConnection() async {
|
||||
final result = await _connectivityService.checkConnectivity();
|
||||
if (result == ConnectivityResult.none) {
|
||||
emit(ConnectivityDisconnected());
|
||||
} else {
|
||||
emit(ConnectivityConnected(result as ConnectivityResult));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_subscription.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ConnectivityState extends Equatable {
|
||||
const ConnectivityState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class ConnectivityInitial extends ConnectivityState {}
|
||||
|
||||
class ConnectivityConnected extends ConnectivityState {
|
||||
final ConnectivityResult result;
|
||||
|
||||
const ConnectivityConnected(this.result);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [result];
|
||||
}
|
||||
|
||||
class ConnectivityDisconnected extends ConnectivityState {}
|
||||
|
||||
class ConnectivityService {
|
||||
static final ConnectivityService _instance = ConnectivityService._internal();
|
||||
final Connectivity _connectivity = Connectivity();
|
||||
final StreamController<ConnectivityResult> _controller = StreamController<ConnectivityResult>.broadcast();
|
||||
|
||||
factory ConnectivityService() => _instance;
|
||||
|
||||
ConnectivityService._internal() {
|
||||
_connectivity.onConnectivityChanged.listen((result) {
|
||||
_controller.add(result.first); // Notify all listeners
|
||||
});
|
||||
}
|
||||
|
||||
Stream<ConnectivityResult> get connectivityStream => _controller.stream;
|
||||
|
||||
Future<List<ConnectivityResult>> checkConnectivity() async {
|
||||
return await _connectivity.checkConnectivity();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/LikeAndDisLikeNews/LikeAndDisLikeNewsRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class LikeAndDisLikeState {}
|
||||
|
||||
class LikeAndDisLikeInitial extends LikeAndDisLikeState {}
|
||||
|
||||
class LikeAndDisLikeFetchInProgress extends LikeAndDisLikeState {}
|
||||
|
||||
class LikeAndDisLikeFetchSuccess extends LikeAndDisLikeState {
|
||||
final List<NewsModel> likeAndDisLike;
|
||||
final int totalLikeAndDisLikeCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
LikeAndDisLikeFetchSuccess({required this.likeAndDisLike, required this.totalLikeAndDisLikeCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class LikeAndDisLikeFetchFailure extends LikeAndDisLikeState {
|
||||
final String errorMessage;
|
||||
|
||||
LikeAndDisLikeFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class LikeAndDisLikeCubit extends Cubit<LikeAndDisLikeState> {
|
||||
final LikeAndDisLikeRepository likeAndDisLikeRepository;
|
||||
int perPageLimit = 25;
|
||||
|
||||
LikeAndDisLikeCubit(this.likeAndDisLikeRepository) : super(LikeAndDisLikeInitial());
|
||||
|
||||
void getLike({required String langId}) async {
|
||||
try {
|
||||
emit(LikeAndDisLikeFetchInProgress());
|
||||
final result = await likeAndDisLikeRepository.getLike(limit: perPageLimit.toString(), offset: "0", langId: langId);
|
||||
emit(LikeAndDisLikeFetchSuccess(
|
||||
likeAndDisLike: result['LikeAndDisLike'], totalLikeAndDisLikeCount: result[TOTAL], hasMoreFetchError: false, hasMore: (result['LikeAndDisLike'] as List<NewsModel>).length < result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(LikeAndDisLikeFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool isNewsLikeAndDisLike(String newsId) {
|
||||
if (state is LikeAndDisLikeFetchSuccess) {
|
||||
final likeAndDisLike = (state as LikeAndDisLikeFetchSuccess).likeAndDisLike;
|
||||
return likeAndDisLike.indexWhere((element) => (element.id == newsId || element.newsId == newsId)) != -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void resetState() {
|
||||
emit(LikeAndDisLikeFetchInProgress());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:news/data/repositories/LikeAndDisLikeNews/LikeAndDisLikeNewsRepository.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
|
||||
abstract class UpdateLikeAndDisLikeStatusState {}
|
||||
|
||||
class UpdateLikeAndDisLikeStatusInitial extends UpdateLikeAndDisLikeStatusState {}
|
||||
|
||||
class UpdateLikeAndDisLikeStatusInProgress extends UpdateLikeAndDisLikeStatusState {}
|
||||
|
||||
class UpdateLikeAndDisLikeStatusSuccess extends UpdateLikeAndDisLikeStatusState {
|
||||
final NewsModel news;
|
||||
final bool wasLikeAndDisLikeNewsProcess; //to check that process of favorite done or not
|
||||
UpdateLikeAndDisLikeStatusSuccess(this.news, this.wasLikeAndDisLikeNewsProcess);
|
||||
}
|
||||
|
||||
class UpdateLikeAndDisLikeStatusFailure extends UpdateLikeAndDisLikeStatusState {
|
||||
final String errorMessage;
|
||||
|
||||
UpdateLikeAndDisLikeStatusFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class UpdateLikeAndDisLikeStatusCubit extends Cubit<UpdateLikeAndDisLikeStatusState> {
|
||||
final LikeAndDisLikeRepository likeAndDisLikeRepository;
|
||||
|
||||
UpdateLikeAndDisLikeStatusCubit(this.likeAndDisLikeRepository) : super(UpdateLikeAndDisLikeStatusInitial());
|
||||
|
||||
void setLikeAndDisLikeNews({required NewsModel news, required String status}) {
|
||||
emit(UpdateLikeAndDisLikeStatusInProgress());
|
||||
likeAndDisLikeRepository.setLike(newsId: (news.newsId != null) ? news.newsId! : news.id!, status: status).then((value) {
|
||||
emit(UpdateLikeAndDisLikeStatusSuccess(news, status == "1" ? true : false));
|
||||
}).catchError((e) {
|
||||
ApiMessageAndCodeException apiMessageAndCodeException = e;
|
||||
emit(UpdateLikeAndDisLikeStatusFailure(apiMessageAndCodeException.errorMessage.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
39
news-app/lib/cubits/NewsByIdCubit.dart
Normal file
39
news-app/lib/cubits/NewsByIdCubit.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/NewsById/NewsByIdRepository.dart';
|
||||
|
||||
abstract class NewsByIdState {}
|
||||
|
||||
class NewsByIdInitial extends NewsByIdState {}
|
||||
|
||||
class NewsByIdFetchInProgress extends NewsByIdState {}
|
||||
|
||||
class NewsByIdFetchSuccess extends NewsByIdState {
|
||||
final List<NewsModel> newsById;
|
||||
|
||||
NewsByIdFetchSuccess({required this.newsById});
|
||||
}
|
||||
|
||||
class NewsByIdFetchFailure extends NewsByIdState {
|
||||
final String errorMessage;
|
||||
|
||||
NewsByIdFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class NewsByIdCubit extends Cubit<NewsByIdState> {
|
||||
final NewsByIdRepository _newsByIdRepository;
|
||||
|
||||
NewsByIdCubit(this._newsByIdRepository) : super(NewsByIdInitial());
|
||||
|
||||
Future<List<NewsModel>> getNewsById({required String newsId, required String langId}) async {
|
||||
try {
|
||||
emit(NewsByIdFetchInProgress());
|
||||
final result = await _newsByIdRepository.getNewsById(langId: langId, newsId: newsId);
|
||||
emit(NewsByIdFetchSuccess(newsById: result['NewsById']));
|
||||
return result['NewsById'];
|
||||
} catch (e) {
|
||||
emit(NewsByIdFetchFailure(e.toString()));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/NewsComment/deleteCommentCubit.dart
Normal file
37
news-app/lib/cubits/NewsComment/deleteCommentCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/NewsComment/DeleteComment/deleteCommRepository.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
|
||||
abstract class DeleteCommState {}
|
||||
|
||||
class DeleteCommInitial extends DeleteCommState {}
|
||||
|
||||
class DeleteCommInProgress extends DeleteCommState {}
|
||||
|
||||
class DeleteCommSuccess extends DeleteCommState {
|
||||
final String message;
|
||||
|
||||
DeleteCommSuccess(this.message);
|
||||
}
|
||||
|
||||
class DeleteCommFailure extends DeleteCommState {
|
||||
final String errorMessage;
|
||||
|
||||
DeleteCommFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class DeleteCommCubit extends Cubit<DeleteCommState> {
|
||||
final DeleteCommRepository _deleteCommRepository;
|
||||
|
||||
DeleteCommCubit(this._deleteCommRepository) : super(DeleteCommInitial());
|
||||
|
||||
void setDeleteComm({required String commId}) {
|
||||
emit(DeleteCommInProgress());
|
||||
_deleteCommRepository.setDeleteComm(commId: commId).then((value) {
|
||||
emit(DeleteCommSuccess(value["message"]));
|
||||
}).catchError((e) {
|
||||
ApiMessageAndCodeException apiMessageAndCodeException = e;
|
||||
emit(DeleteCommFailure(apiMessageAndCodeException.errorMessage.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
36
news-app/lib/cubits/NewsComment/flagCommentCubit.dart
Normal file
36
news-app/lib/cubits/NewsComment/flagCommentCubit.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/NewsComment/FlagComment/flagCommRepository.dart';
|
||||
|
||||
abstract class SetFlagState {}
|
||||
|
||||
class SetFlagInitial extends SetFlagState {}
|
||||
|
||||
class SetFlagFetchInProgress extends SetFlagState {}
|
||||
|
||||
class SetFlagFetchSuccess extends SetFlagState {
|
||||
String message;
|
||||
|
||||
SetFlagFetchSuccess({required this.message});
|
||||
}
|
||||
|
||||
class SetFlagFetchFailure extends SetFlagState {
|
||||
final String errorMessage;
|
||||
|
||||
SetFlagFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SetFlagCubit extends Cubit<SetFlagState> {
|
||||
final SetFlagRepository _setFlagRepository;
|
||||
|
||||
SetFlagCubit(this._setFlagRepository) : super(SetFlagInitial());
|
||||
|
||||
void setFlag({required String commId, required String newsId, required String message}) async {
|
||||
try {
|
||||
emit(SetFlagFetchInProgress());
|
||||
final result = await _setFlagRepository.setFlag(message: message, newsId: newsId, commId: commId);
|
||||
emit(SetFlagFetchSuccess(message: result['message']));
|
||||
} catch (e) {
|
||||
emit(SetFlagFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
41
news-app/lib/cubits/NewsComment/likeAndDislikeCommCubit.dart
Normal file
41
news-app/lib/cubits/NewsComment/likeAndDislikeCommCubit.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/CommentModel.dart';
|
||||
import 'package:news/data/repositories/NewsComment/LikeAndDislikeComment/likeAndDislikeCommRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class LikeAndDislikeCommState {}
|
||||
|
||||
class LikeAndDislikeCommInitial extends LikeAndDislikeCommState {}
|
||||
|
||||
class LikeAndDislikeCommInProgress extends LikeAndDislikeCommState {}
|
||||
|
||||
class LikeAndDislikeCommSuccess extends LikeAndDislikeCommState {
|
||||
final CommentModel comment;
|
||||
final bool wasLikeAndDislikeCommNewsProcess;
|
||||
final bool fromLike;
|
||||
|
||||
LikeAndDislikeCommSuccess(this.comment, this.wasLikeAndDislikeCommNewsProcess, this.fromLike);
|
||||
}
|
||||
|
||||
class LikeAndDislikeCommFailure extends LikeAndDislikeCommState {
|
||||
final String errorMessage;
|
||||
|
||||
LikeAndDislikeCommFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class LikeAndDislikeCommCubit extends Cubit<LikeAndDislikeCommState> {
|
||||
final LikeAndDislikeCommRepository _likeAndDislikeCommRepository;
|
||||
|
||||
LikeAndDislikeCommCubit(this._likeAndDislikeCommRepository) : super(LikeAndDislikeCommInitial());
|
||||
|
||||
void setLikeAndDislikeComm({required String langId, required String commId, required String status, required bool fromLike}) async {
|
||||
try {
|
||||
emit(LikeAndDislikeCommInProgress());
|
||||
final result = await _likeAndDislikeCommRepository.setLikeAndDislikeComm(langId: langId, commId: commId, status: status);
|
||||
|
||||
(!result[ERROR]) ? emit(LikeAndDislikeCommSuccess(result['updatedComment'], true, fromLike)) : emit(LikeAndDislikeCommFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(LikeAndDislikeCommFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
39
news-app/lib/cubits/NewsComment/setCommentCubit.dart
Normal file
39
news-app/lib/cubits/NewsComment/setCommentCubit.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/CommentModel.dart';
|
||||
import 'package:news/data/repositories/NewsComment/SetComment/setComRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class SetCommentState {}
|
||||
|
||||
class SetCommentInitial extends SetCommentState {}
|
||||
|
||||
class SetCommentFetchInProgress extends SetCommentState {}
|
||||
|
||||
class SetCommentFetchSuccess extends SetCommentState {
|
||||
List<CommentModel> setComment;
|
||||
int total;
|
||||
|
||||
SetCommentFetchSuccess({required this.setComment, required this.total});
|
||||
}
|
||||
|
||||
class SetCommentFetchFailure extends SetCommentState {
|
||||
final String errorMessage;
|
||||
|
||||
SetCommentFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SetCommentCubit extends Cubit<SetCommentState> {
|
||||
final SetCommentRepository _setCommentRepository;
|
||||
|
||||
SetCommentCubit(this._setCommentRepository) : super(SetCommentInitial());
|
||||
|
||||
void setComment({required String parentId, required String newsId, required String message}) async {
|
||||
emit(SetCommentFetchInProgress());
|
||||
try {
|
||||
final result = await _setCommentRepository.setComment(message: message, newsId: newsId, parentId: parentId);
|
||||
emit(SetCommentFetchSuccess(setComment: result['SetComment'], total: result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(SetCommentFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/SetUserPreferenceCat/setUserPrefCatRepository.dart';
|
||||
|
||||
abstract class SetUserPrefCatState {}
|
||||
|
||||
class SetUserPrefCatInitial extends SetUserPrefCatState {}
|
||||
|
||||
class SetUserPrefCatFetchInProgress extends SetUserPrefCatState {}
|
||||
|
||||
class SetUserPrefCatFetchSuccess extends SetUserPrefCatState {
|
||||
var setUserPrefCat;
|
||||
|
||||
SetUserPrefCatFetchSuccess({required this.setUserPrefCat});
|
||||
}
|
||||
|
||||
class SetUserPrefCatFetchFailure extends SetUserPrefCatState {
|
||||
final String errorMessage;
|
||||
|
||||
SetUserPrefCatFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SetUserPrefCatCubit extends Cubit<SetUserPrefCatState> {
|
||||
final SetUserPrefCatRepository _setUserPrefCatRepository;
|
||||
|
||||
SetUserPrefCatCubit(this._setUserPrefCatRepository) : super(SetUserPrefCatInitial());
|
||||
|
||||
void setUserPrefCat({required String catId}) async {
|
||||
emit(SetUserPrefCatFetchInProgress());
|
||||
try {
|
||||
final result = await _setUserPrefCatRepository.setUserPrefCat(catId: catId);
|
||||
|
||||
emit(SetUserPrefCatFetchSuccess(setUserPrefCat: result['SetUserPrefCat']));
|
||||
} catch (e) {
|
||||
emit(SetUserPrefCatFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
36
news-app/lib/cubits/UserPreferences/userByCategoryCubit.dart
Normal file
36
news-app/lib/cubits/UserPreferences/userByCategoryCubit.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/UserByCategory/userByCatRepository.dart';
|
||||
|
||||
abstract class UserByCatState {}
|
||||
|
||||
class UserByCatInitial extends UserByCatState {}
|
||||
|
||||
class UserByCatFetchInProgress extends UserByCatState {}
|
||||
|
||||
class UserByCatFetchSuccess extends UserByCatState {
|
||||
var userByCat;
|
||||
|
||||
UserByCatFetchSuccess({required this.userByCat});
|
||||
}
|
||||
|
||||
class UserByCatFetchFailure extends UserByCatState {
|
||||
final String errorMessage;
|
||||
|
||||
UserByCatFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class UserByCatCubit extends Cubit<UserByCatState> {
|
||||
final UserByCatRepository _userByCatRepository;
|
||||
|
||||
UserByCatCubit(this._userByCatRepository) : super(UserByCatInitial());
|
||||
|
||||
void getUserById() async {
|
||||
emit(UserByCatFetchInProgress());
|
||||
try {
|
||||
final result = await _userByCatRepository.getUserById();
|
||||
emit(UserByCatFetchSuccess(userByCat: result['UserByCat']['user_category']));
|
||||
} catch (e) {
|
||||
emit(UserByCatFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
41
news-app/lib/cubits/adSpacesNewsDetailsCubit.dart
Normal file
41
news-app/lib/cubits/adSpacesNewsDetailsCubit.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/adSpaceModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class AdSpacesNewsDetailsState {}
|
||||
|
||||
class AdSpacesNewsDetailsInitial extends AdSpacesNewsDetailsState {}
|
||||
|
||||
class AdSpacesNewsDetailsFetchInProgress extends AdSpacesNewsDetailsState {}
|
||||
|
||||
class AdSpacesNewsDetailsFetchSuccess extends AdSpacesNewsDetailsState {
|
||||
final AdSpaceModel? adSpaceTopData;
|
||||
final AdSpaceModel? adSpaceBottomData;
|
||||
|
||||
AdSpacesNewsDetailsFetchSuccess({this.adSpaceTopData, this.adSpaceBottomData});
|
||||
}
|
||||
|
||||
class AdSpacesNewsDetailsFetchFailure extends AdSpacesNewsDetailsState {
|
||||
final String errorMessage;
|
||||
|
||||
AdSpacesNewsDetailsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class AdSpacesNewsDetailsCubit extends Cubit<AdSpacesNewsDetailsState> {
|
||||
AdSpacesNewsDetailsCubit() : super(AdSpacesNewsDetailsInitial());
|
||||
|
||||
void getAdspaceForNewsDetails({required String langId}) async {
|
||||
emit(AdSpacesNewsDetailsFetchInProgress());
|
||||
try {
|
||||
final body = {LANGUAGE_ID: langId};
|
||||
final Map<String, dynamic> result = await Api.sendApiRequest(body: body, url: Api.getAdsNewsDetailsApi);
|
||||
final Map<String, dynamic> resultData = result[DATA];
|
||||
emit(AdSpacesNewsDetailsFetchSuccess(
|
||||
adSpaceTopData: (resultData.containsKey('ad_spaces_top')) ? (AdSpaceModel.fromJson(resultData['ad_spaces_top'])) : null,
|
||||
adSpaceBottomData: (resultData.containsKey('ad_spaces_bottom')) ? AdSpaceModel.fromJson(resultData['ad_spaces_bottom']) : null));
|
||||
} catch (e) {
|
||||
emit(AdSpacesNewsDetailsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
26
news-app/lib/cubits/appLocalizationCubit.dart
Normal file
26
news-app/lib/cubits/appLocalizationCubit.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/Settings/settingsLocalDataRepository.dart';
|
||||
|
||||
class AppLocalizationState {
|
||||
String languageCode;
|
||||
String id;
|
||||
int isRTL;
|
||||
|
||||
AppLocalizationState(
|
||||
this.languageCode,
|
||||
this.id,
|
||||
this.isRTL,
|
||||
);
|
||||
}
|
||||
|
||||
class AppLocalizationCubit extends Cubit<AppLocalizationState> {
|
||||
final SettingsLocalDataRepository _settingsRepository;
|
||||
|
||||
AppLocalizationCubit(this._settingsRepository)
|
||||
: super(AppLocalizationState(_settingsRepository.getCurrentLanguageCode(), _settingsRepository.getCurrentLanguageId(), _settingsRepository.getCurrentLanguageRTL()));
|
||||
|
||||
void changeLanguage(String lanCode, String lanId, int lanRTL) {
|
||||
_settingsRepository.setLanguagePreferences(code: lanCode, id: lanId, rtl: lanRTL);
|
||||
emit(AppLocalizationState(lanCode, lanId, lanRTL));
|
||||
}
|
||||
}
|
||||
254
news-app/lib/cubits/appSystemSettingCubit.dart
Normal file
254
news-app/lib/cubits/appSystemSettingCubit.dart
Normal file
@@ -0,0 +1,254 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/app/app.dart';
|
||||
import 'package:news/data/models/AppSystemSettingModel.dart';
|
||||
import 'package:news/data/repositories/AppSystemSetting/systemRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/uiUtils.dart';
|
||||
|
||||
abstract class AppConfigurationState {}
|
||||
|
||||
class AppConfigurationInitial extends AppConfigurationState {
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AppConfigurationFetchInProgress extends AppConfigurationState {
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AppConfigurationFetchSuccess extends AppConfigurationState {
|
||||
final AppSystemSettingModel appConfiguration;
|
||||
|
||||
AppConfigurationFetchSuccess({required this.appConfiguration});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [appConfiguration];
|
||||
}
|
||||
|
||||
class AppConfigurationFetchFailure extends AppConfigurationState {
|
||||
final String errorMessage;
|
||||
|
||||
AppConfigurationFetchFailure(this.errorMessage);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [errorMessage];
|
||||
}
|
||||
|
||||
class AppConfigurationCubit extends Cubit<AppConfigurationState> {
|
||||
final SystemRepository _systemRepository;
|
||||
|
||||
AppConfigurationCubit(this._systemRepository) : super(AppConfigurationInitial());
|
||||
|
||||
fetchAppConfiguration() async {
|
||||
emit(AppConfigurationFetchInProgress());
|
||||
try {
|
||||
final appConfiguration = AppSystemSettingModel.fromJson(await _systemRepository.fetchSettings());
|
||||
emit(AppConfigurationFetchSuccess(appConfiguration: appConfiguration));
|
||||
} catch (e) {
|
||||
emit(AppConfigurationFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
AppSystemSettingModel getAppConfiguration() {
|
||||
return (state is AppConfigurationFetchSuccess) ? (state as AppConfigurationFetchSuccess).appConfiguration : AppSystemSettingModel.fromJson({});
|
||||
}
|
||||
|
||||
String? getBreakingNewsMode() {
|
||||
return ((state is AppConfigurationFetchSuccess)) ? getAppConfiguration().breakNewsMode : "";
|
||||
}
|
||||
|
||||
String? getLiveStreamMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().liveStreamMode : "";
|
||||
}
|
||||
|
||||
String? getCategoryMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().catMode : "";
|
||||
}
|
||||
|
||||
String? getSubCatMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().subCatMode : "";
|
||||
}
|
||||
|
||||
String? getCommentsMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().commentMode : "";
|
||||
}
|
||||
|
||||
String? getInAppAdsMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? ((Platform.isAndroid) ? getAppConfiguration().inAppAdsMode : getAppConfiguration().iosInAppAdsMode) : "";
|
||||
}
|
||||
|
||||
String? getLocationWiseNewsMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().locationWiseNewsMode : "";
|
||||
}
|
||||
|
||||
String? getWeatherMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().weatherMode : "";
|
||||
}
|
||||
|
||||
String? getMaintenanceMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().maintenanceMode : "0";
|
||||
}
|
||||
|
||||
String? checkAdsType() {
|
||||
if (getInAppAdsMode() == "1") {
|
||||
return (Platform.isIOS) ? getIOSAdsType() : getAdsType();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String? getAdsType() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
switch (getAppConfiguration().adsType) {
|
||||
case "1":
|
||||
return "google";
|
||||
case "3":
|
||||
return "unity";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String? getIOSAdsType() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
switch (getAppConfiguration().iosAdsType) {
|
||||
case "1":
|
||||
return "google";
|
||||
case "3":
|
||||
return "unity";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String? bannerId() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
if (Platform.isAndroid && getInAppAdsMode() != "0") {
|
||||
if (getAdsType() == "google") return getAppConfiguration().goBannerId;
|
||||
if (getAdsType() == "unity") return getAppConfiguration().unityBannerId;
|
||||
}
|
||||
if (Platform.isIOS && getInAppAdsMode() != "0") {
|
||||
if (getIOSAdsType() == "google") return getAppConfiguration().goIOSBannerId;
|
||||
if (getIOSAdsType() == "unity") return getAppConfiguration().unityIOSBannerId;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String? rewardId() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
if (Platform.isAndroid && getInAppAdsMode() != "0") {
|
||||
if (getAdsType() == "google") return getAppConfiguration().goRewardedId;
|
||||
if (getAdsType() == "unity") return getAppConfiguration().unityRewardedId;
|
||||
}
|
||||
if (Platform.isIOS && getInAppAdsMode() != "0") {
|
||||
if (getIOSAdsType() == "google") return getAppConfiguration().goIOSRewardedId;
|
||||
if (getIOSAdsType() == "unity") return getAppConfiguration().unityIOSRewardedId;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String? nativeId() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
if (Platform.isAndroid && getInAppAdsMode() != "0") {
|
||||
if (getAdsType() == "google") return getAppConfiguration().goNativeId;
|
||||
if (getAdsType() == "unity") return ""; //no native ads in unity
|
||||
}
|
||||
if (Platform.isIOS && getInAppAdsMode() != "0") {
|
||||
if (getIOSAdsType() == "google") return getAppConfiguration().goIOSNativeId;
|
||||
if (getIOSAdsType() == "unity") return ""; //no native ads in unity
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String? interstitialId() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
if (Platform.isAndroid && getInAppAdsMode() != "0") {
|
||||
if (getAdsType() == "google") return getAppConfiguration().goInterId;
|
||||
if (getAdsType() == "unity") return getAppConfiguration().unityInterId;
|
||||
}
|
||||
if (Platform.isIOS && getInAppAdsMode() != "0") {
|
||||
if (getIOSAdsType() == "google") return getAppConfiguration().goIOSInterId;
|
||||
if (getIOSAdsType() == "unity") return getAppConfiguration().unityIOSInterId;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String? unityGameId() {
|
||||
return (Platform.isAndroid) ? getAppConfiguration().gameId : getAppConfiguration().iosGameId;
|
||||
}
|
||||
|
||||
String? getRSSFeedMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().rssFeedMode : "";
|
||||
}
|
||||
|
||||
String? getMobileLoginMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().mobileLoginMode : "";
|
||||
}
|
||||
|
||||
String? getCountryCode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().countryCode : "IN"; //India bydefault
|
||||
}
|
||||
|
||||
String? getShareAppText() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().shareAppText : "";
|
||||
}
|
||||
|
||||
String? getAppstoreId() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().appstoreId : "";
|
||||
}
|
||||
|
||||
String? getiOSAppLink() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().iosAppLink : "";
|
||||
}
|
||||
|
||||
String? getAndroidAppLink() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().androidAppLink : "";
|
||||
}
|
||||
|
||||
VideoViewType? getVideoTypePreference() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().videoTypePreference : VideoViewType.normal;
|
||||
}
|
||||
|
||||
String? getForceUpdateMode() {
|
||||
return (state is AppConfigurationFetchSuccess) ? getAppConfiguration().forceUpdateMode : "";
|
||||
}
|
||||
|
||||
bool needsUpdate(String enforceVersion) {
|
||||
final List<int> currentVersion = packageInfo.version.split('.').map((String number) => int.parse(number)).toList();
|
||||
final List<int> enforcedVersion = enforceVersion.split('.').map((String number) => int.parse(number)).toList();
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (enforcedVersion[i] > currentVersion[i]) {
|
||||
return true;
|
||||
} else if (currentVersion[i] > enforcedVersion[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String getGeminiAPiKey() {
|
||||
return ((state is AppConfigurationFetchSuccess)) ? UiUtils.decryptKey(geminiKey: getAppConfiguration().googleGeminiApiKey) : "";
|
||||
}
|
||||
|
||||
bool isUpdateRequired() {
|
||||
if (state is AppConfigurationFetchSuccess) {
|
||||
AppSystemSettingModel appConfig = (state as AppConfigurationFetchSuccess).appConfiguration;
|
||||
if (defaultTargetPlatform == TargetPlatform.android && needsUpdate(appConfig.androidAppVersion ?? "") ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS && needsUpdate(appConfig.iosAppVersion ?? "")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
40
news-app/lib/cubits/breakingNewsCubit.dart
Normal file
40
news-app/lib/cubits/breakingNewsCubit.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/BreakingNewsModel.dart';
|
||||
import 'package:news/data/repositories/BreakingNews/breakNewsRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class BreakingNewsState {}
|
||||
|
||||
class BreakingNewsInitial extends BreakingNewsState {}
|
||||
|
||||
class BreakingNewsFetchInProgress extends BreakingNewsState {}
|
||||
|
||||
class BreakingNewsFetchSuccess extends BreakingNewsState {
|
||||
final List<BreakingNewsModel> breakingNews;
|
||||
|
||||
BreakingNewsFetchSuccess({required this.breakingNews});
|
||||
}
|
||||
|
||||
class BreakingNewsFetchFailure extends BreakingNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
BreakingNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class BreakingNewsCubit extends Cubit<BreakingNewsState> {
|
||||
final BreakingNewsRepository _breakingNewsRepository;
|
||||
|
||||
BreakingNewsCubit(this._breakingNewsRepository) : super(BreakingNewsInitial());
|
||||
|
||||
Future<List<BreakingNewsModel>> getBreakingNews({required String langId}) async {
|
||||
emit(BreakingNewsFetchInProgress());
|
||||
try {
|
||||
final result = await _breakingNewsRepository.getBreakingNews(langId: langId);
|
||||
(!result[ERROR]) ? emit(BreakingNewsFetchSuccess(breakingNews: result['BreakingNews'])) : emit(BreakingNewsFetchFailure(result[MESSAGE]));
|
||||
return (!result[ERROR]) ? result['BreakingNews'] : [];
|
||||
} catch (e) {
|
||||
emit(BreakingNewsFetchFailure(e.toString()));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
112
news-app/lib/cubits/categoryCubit.dart
Normal file
112
news-app/lib/cubits/categoryCubit.dart
Normal file
@@ -0,0 +1,112 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/CategoryModel.dart';
|
||||
import 'package:news/data/repositories/Category/categoryRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class CategoryState {}
|
||||
|
||||
class CategoryInitial extends CategoryState {}
|
||||
|
||||
class CategoryFetchInProgress extends CategoryState {}
|
||||
|
||||
class CategoryFetchSuccess extends CategoryState {
|
||||
final List<CategoryModel> category;
|
||||
final int totalCategoryCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
CategoryFetchSuccess(
|
||||
{required this.category,
|
||||
required this.totalCategoryCount,
|
||||
required this.hasMoreFetchError,
|
||||
required this.hasMore});
|
||||
}
|
||||
|
||||
class CategoryFetchFailure extends CategoryState {
|
||||
final String errorMessage;
|
||||
|
||||
CategoryFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class CategoryCubit extends Cubit<CategoryState> {
|
||||
final CategoryRepository _categoryRepository;
|
||||
|
||||
CategoryCubit(this._categoryRepository) : super(CategoryInitial());
|
||||
|
||||
void getCategory({required String langId}) async {
|
||||
try {
|
||||
emit(CategoryFetchInProgress());
|
||||
int catLimit = 20;
|
||||
final result = await _categoryRepository.getCategory(
|
||||
limit: catLimit.toString(), offset: "0", langId: langId);
|
||||
(!result[ERROR])
|
||||
? emit(CategoryFetchSuccess(
|
||||
category: result['Category'],
|
||||
totalCategoryCount: result[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: (result['Category'] as List<CategoryModel>).length <
|
||||
result[TOTAL]))
|
||||
: emit(CategoryFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(CategoryFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Load categories if not already loaded or failed
|
||||
void loadIfFailed({required String langId}) {
|
||||
if (state is CategoryInitial || state is CategoryFetchFailure) {
|
||||
getCategory(langId: langId);
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreCategory() {
|
||||
return (state is CategoryFetchSuccess)
|
||||
? (state as CategoryFetchSuccess).hasMore
|
||||
: false;
|
||||
}
|
||||
|
||||
void getMoreCategory({required String langId}) async {
|
||||
if (state is CategoryFetchSuccess) {
|
||||
try {
|
||||
final result = await _categoryRepository.getCategory(
|
||||
limit: limitOfAPIData.toString(),
|
||||
langId: langId,
|
||||
offset: (state as CategoryFetchSuccess).category.length.toString());
|
||||
if (!result[ERROR]) {
|
||||
List<CategoryModel> updatedResults =
|
||||
(state as CategoryFetchSuccess).category;
|
||||
updatedResults.addAll(result['Category'] as List<CategoryModel>);
|
||||
emit(CategoryFetchSuccess(
|
||||
category: updatedResults,
|
||||
totalCategoryCount: result[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: updatedResults.length < result[TOTAL]));
|
||||
} else {
|
||||
emit(CategoryFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(CategoryFetchSuccess(
|
||||
category: (state as CategoryFetchSuccess).category,
|
||||
hasMoreFetchError: true,
|
||||
totalCategoryCount:
|
||||
(state as CategoryFetchSuccess).totalCategoryCount,
|
||||
hasMore: (state as CategoryFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<CategoryModel> getCatList() {
|
||||
return (state is CategoryFetchSuccess)
|
||||
? (state as CategoryFetchSuccess).category
|
||||
: [];
|
||||
}
|
||||
|
||||
int getCategoryIndex({required String categoryName}) {
|
||||
return (state is CategoryFetchSuccess)
|
||||
? (state as CategoryFetchSuccess)
|
||||
.category
|
||||
.indexWhere((element) => element.categoryName == categoryName)
|
||||
: 0;
|
||||
}
|
||||
}
|
||||
111
news-app/lib/cubits/commentNewsCubit.dart
Normal file
111
news-app/lib/cubits/commentNewsCubit.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/CommentModel.dart';
|
||||
import 'package:news/data/repositories/CommentNews/commNewsRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class CommentNewsState {}
|
||||
|
||||
class CommentNewsInitial extends CommentNewsState {}
|
||||
|
||||
class CommentNewsFetchInProgress extends CommentNewsState {}
|
||||
|
||||
class CommentNewsFetchSuccess extends CommentNewsState {
|
||||
final List<CommentModel> commentNews;
|
||||
final int totalCommentNewsCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
CommentNewsFetchSuccess({required this.commentNews, required this.totalCommentNewsCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
CommentNewsFetchSuccess copyWith({List<CommentModel>? commentNews, int? totalCommentNewsCount, bool? hasMoreFetchError, bool? hasMore}) {
|
||||
return CommentNewsFetchSuccess(
|
||||
commentNews: commentNews ?? this.commentNews,
|
||||
totalCommentNewsCount: totalCommentNewsCount ?? this.totalCommentNewsCount,
|
||||
hasMoreFetchError: hasMoreFetchError ?? this.hasMoreFetchError,
|
||||
hasMore: hasMore ?? this.hasMore);
|
||||
}
|
||||
}
|
||||
|
||||
class CommentNewsFetchFailure extends CommentNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
CommentNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class CommentNewsCubit extends Cubit<CommentNewsState> {
|
||||
final CommentNewsRepository _commentNewsRepository;
|
||||
|
||||
CommentNewsCubit(this._commentNewsRepository) : super(CommentNewsInitial());
|
||||
|
||||
void getCommentNews({required String newsId}) async {
|
||||
try {
|
||||
emit(CommentNewsFetchInProgress());
|
||||
final result = await _commentNewsRepository.getCommentNews(limit: limitOfAPIData.toString(), offset: "0", newsId: newsId);
|
||||
|
||||
(!result[ERROR])
|
||||
? emit(CommentNewsFetchSuccess(
|
||||
commentNews: result['CommentNews'], totalCommentNewsCount: result[TOTAL], hasMoreFetchError: false, hasMore: ((result['CommentNews'] as List<CommentModel>).length) < result[TOTAL]))
|
||||
: emit(CommentNewsFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(CommentNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreCommentNews() {
|
||||
return (state is CommentNewsFetchSuccess) ? (state as CommentNewsFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreCommentNews({required String newsId}) async {
|
||||
if (state is CommentNewsFetchSuccess) {
|
||||
try {
|
||||
final result = await _commentNewsRepository.getCommentNews(limit: limitOfAPIData.toString(), newsId: newsId, offset: (state as CommentNewsFetchSuccess).commentNews.length.toString());
|
||||
List<CommentModel> updatedResults = (state as CommentNewsFetchSuccess).commentNews;
|
||||
updatedResults.addAll(result['CommentNews'] as List<CommentModel>);
|
||||
emit(CommentNewsFetchSuccess(commentNews: updatedResults, totalCommentNewsCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(CommentNewsFetchSuccess(
|
||||
commentNews: (state as CommentNewsFetchSuccess).commentNews,
|
||||
hasMoreFetchError: true,
|
||||
totalCommentNewsCount: (state as CommentNewsFetchSuccess).totalCommentNewsCount,
|
||||
hasMore: (state as CommentNewsFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emitSuccessState(List<CommentModel> commments) {
|
||||
emit((state as CommentNewsFetchSuccess).copyWith(commentNews: commments));
|
||||
}
|
||||
|
||||
void commentUpdateList(List<CommentModel> commentList, int total) {
|
||||
if (state is CommentNewsFetchSuccess || state is CommentNewsFetchFailure) {
|
||||
bool haseMore = (state is CommentNewsFetchSuccess) ? (state as CommentNewsFetchSuccess).hasMore : false;
|
||||
emit(CommentNewsFetchSuccess(commentNews: commentList, hasMore: haseMore, hasMoreFetchError: false, totalCommentNewsCount: total));
|
||||
}
|
||||
}
|
||||
|
||||
void deleteComment(int index) {
|
||||
if (state is CommentNewsFetchSuccess) {
|
||||
List<CommentModel> commentList = List.from((state as CommentNewsFetchSuccess).commentNews)..removeAt(index);
|
||||
|
||||
emit(CommentNewsFetchSuccess(
|
||||
commentNews: commentList,
|
||||
hasMore: (state as CommentNewsFetchSuccess).hasMore,
|
||||
hasMoreFetchError: false,
|
||||
totalCommentNewsCount: (state as CommentNewsFetchSuccess).totalCommentNewsCount - 1));
|
||||
}
|
||||
}
|
||||
|
||||
void deleteCommentReply(String commentId, int index) {
|
||||
if (state is CommentNewsFetchSuccess) {
|
||||
List<CommentModel> commentList = (state as CommentNewsFetchSuccess).commentNews;
|
||||
commentList.forEach((element) {
|
||||
if (element.id! == commentId) {
|
||||
int commIndex = commentList.indexWhere((model) => model.id == commentId);
|
||||
commentList[commIndex].replyComList!.removeAt(index);
|
||||
}
|
||||
});
|
||||
emit(CommentNewsFetchSuccess(
|
||||
commentNews: commentList, hasMore: (state as CommentNewsFetchSuccess).hasMore, hasMoreFetchError: false, totalCommentNewsCount: (state as CommentNewsFetchSuccess).totalCommentNewsCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/deleteImageId.dart
Normal file
37
news-app/lib/cubits/deleteImageId.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../data/repositories/DeleteImageId/deleteImageRepository.dart';
|
||||
import '../utils/api.dart';
|
||||
|
||||
abstract class DeleteImageState {}
|
||||
|
||||
class DeleteImageInitial extends DeleteImageState {}
|
||||
|
||||
class DeleteImageInProgress extends DeleteImageState {}
|
||||
|
||||
class DeleteImageSuccess extends DeleteImageState {
|
||||
final String message;
|
||||
|
||||
DeleteImageSuccess(this.message);
|
||||
}
|
||||
|
||||
class DeleteImageFailure extends DeleteImageState {
|
||||
final String errorMessage;
|
||||
|
||||
DeleteImageFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class DeleteImageCubit extends Cubit<DeleteImageState> {
|
||||
final DeleteImageRepository _deleteImageRepository;
|
||||
|
||||
DeleteImageCubit(this._deleteImageRepository) : super(DeleteImageInitial());
|
||||
|
||||
void setDeleteImage({required String imageId}) {
|
||||
emit(DeleteImageInProgress());
|
||||
_deleteImageRepository.setDeleteImage(imageId: imageId).then((value) {
|
||||
emit(DeleteImageSuccess(value["message"]));
|
||||
}).catchError((e) {
|
||||
ApiMessageAndCodeException apiMessageAndCodeException = e;
|
||||
emit(DeleteImageFailure(apiMessageAndCodeException.errorMessage.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
44
news-app/lib/cubits/deleteUserNewsCubit.dart
Normal file
44
news-app/lib/cubits/deleteUserNewsCubit.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../data/repositories/DeleteUserNews/deleteUserNewsRepository.dart';
|
||||
import '../utils/api.dart';
|
||||
|
||||
abstract class DeleteUserNewsState {}
|
||||
|
||||
class DeleteUserNewsInitial extends DeleteUserNewsState {}
|
||||
|
||||
class DeleteUserNewsInProgress extends DeleteUserNewsState {}
|
||||
|
||||
class DeleteUserNewsSuccess extends DeleteUserNewsState {
|
||||
final String message;
|
||||
|
||||
DeleteUserNewsSuccess(this.message);
|
||||
}
|
||||
|
||||
class DeleteUserNewsFailure extends DeleteUserNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
DeleteUserNewsFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class DeleteUserNewsCubit extends Cubit<DeleteUserNewsState> {
|
||||
final DeleteUserNewsRepository _deleteUserNewsRepository;
|
||||
|
||||
DeleteUserNewsCubit(this._deleteUserNewsRepository) : super(DeleteUserNewsInitial());
|
||||
|
||||
void setDeleteUserNews({
|
||||
required String newsId,
|
||||
}) {
|
||||
emit(DeleteUserNewsInProgress());
|
||||
_deleteUserNewsRepository
|
||||
.setDeleteUserNews(
|
||||
newsId: newsId,
|
||||
)
|
||||
.then((value) {
|
||||
emit(DeleteUserNewsSuccess(value["message"]));
|
||||
}).catchError((e) {
|
||||
ApiMessageAndCodeException apiMessageAndCodeException = e;
|
||||
emit(DeleteUserNewsFailure(apiMessageAndCodeException.errorMessage.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
71
news-app/lib/cubits/featureSectionCubit.dart
Normal file
71
news-app/lib/cubits/featureSectionCubit.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/FeatureSectionModel.dart';
|
||||
import 'package:news/data/repositories/FeatureSection/sectionRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class SectionState {}
|
||||
|
||||
class SectionInitial extends SectionState {}
|
||||
|
||||
class SectionFetchInProgress extends SectionState {}
|
||||
|
||||
class SectionFetchSuccess extends SectionState {
|
||||
final List<FeatureSectionModel> section;
|
||||
final int totalCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
SectionFetchSuccess({required this.section, required this.totalCount, required this.hasMore, required this.hasMoreFetchError});
|
||||
}
|
||||
|
||||
class SectionFetchFailure extends SectionState {
|
||||
final String errorMessage;
|
||||
|
||||
SectionFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SectionCubit extends Cubit<SectionState> {
|
||||
final SectionRepository _sectionRepository;
|
||||
|
||||
SectionCubit(this._sectionRepository) : super(SectionInitial());
|
||||
|
||||
void getSection({required String langId, String? latitude, String? longitude}) async {
|
||||
try {
|
||||
emit(SectionFetchInProgress());
|
||||
final result = await _sectionRepository.getSection(langId: langId, latitude: latitude, longitude: longitude, limit: limitOfAPIData.toString(), offset: "0", sectionOffset: "0");
|
||||
(!result[ERROR])
|
||||
? emit(SectionFetchSuccess(
|
||||
section: result['Section'],
|
||||
totalCount: result[TOTAL],
|
||||
hasMore: result['Section'].length < result[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
))
|
||||
: emit(SectionFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(SectionFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreSections() {
|
||||
return (state is SectionFetchSuccess) ? (state as SectionFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreSections({required String langId, String? latitude, String? longitude}) async {
|
||||
if (state is SectionFetchSuccess) {
|
||||
try {
|
||||
final result = await _sectionRepository.getSection(
|
||||
langId: langId, latitude: latitude, longitude: longitude, limit: limitOfAPIData.toString(), offset: "0", sectionOffset: (state as SectionFetchSuccess).section.length.toString());
|
||||
if (!result[ERROR]) {
|
||||
List<FeatureSectionModel> updatedResults = (state as SectionFetchSuccess).section;
|
||||
updatedResults.addAll(result['Section'] as List<FeatureSectionModel>);
|
||||
emit(SectionFetchSuccess(section: updatedResults, totalCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} else {
|
||||
emit(SectionFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(SectionFetchSuccess(
|
||||
section: (state as SectionFetchSuccess).section, hasMoreFetchError: true, totalCount: (state as SectionFetchSuccess).totalCount, hasMore: (state as SectionFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
77
news-app/lib/cubits/generalNewsCubit.dart
Normal file
77
news-app/lib/cubits/generalNewsCubit.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class GeneralNewsState {}
|
||||
|
||||
class GeneralNewsInitial extends GeneralNewsState {}
|
||||
|
||||
class GeneralNewsFetchInProgress extends GeneralNewsState {}
|
||||
|
||||
class GeneralNewsFetchSuccess extends GeneralNewsState {
|
||||
final List<NewsModel> generalNews;
|
||||
final int totalCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
GeneralNewsFetchSuccess({required this.totalCount, required this.hasMoreFetchError, required this.hasMore, required this.generalNews});
|
||||
}
|
||||
|
||||
class GeneralNewsFetchFailure extends GeneralNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
GeneralNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class GeneralNewsCubit extends Cubit<GeneralNewsState> {
|
||||
GeneralNewsCubit() : super(GeneralNewsInitial());
|
||||
|
||||
Future<dynamic> getGeneralNews({required String langId, String? latitude, String? longitude, int? offset, String? newsSlug}) async {
|
||||
emit(GeneralNewsInitial());
|
||||
try {
|
||||
final body = {LANGUAGE_ID: langId, LIMIT: 20};
|
||||
|
||||
if (latitude != null && latitude != "null") body[LATITUDE] = latitude;
|
||||
if (longitude != null && longitude != "null") body[LONGITUDE] = longitude;
|
||||
if (offset != null) body[OFFSET] = offset;
|
||||
if (newsSlug != null && newsSlug != "null" && newsSlug.trim().isNotEmpty) body[SLUG] = newsSlug; // used in case of native link , details screen redirection
|
||||
|
||||
final result = await Api.sendApiRequest(body: body, url: Api.getNewsApi);
|
||||
if (!result[ERROR]) {
|
||||
emit(GeneralNewsFetchSuccess(
|
||||
generalNews: (result[DATA] as List).map((e) => NewsModel.fromJson(e)).toList(), totalCount: result[TOTAL], hasMoreFetchError: false, hasMore: result[DATA].length < result[TOTAL]));
|
||||
} else {
|
||||
emit(GeneralNewsFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw ApiMessageAndCodeException(errorMessage: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreGeneralNews() {
|
||||
return (state is GeneralNewsFetchSuccess) ? (state as GeneralNewsFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreGeneralNews({required String langId, String? latitude, String? longitude}) async {
|
||||
if (state is GeneralNewsFetchSuccess) {
|
||||
try {
|
||||
final result = await getGeneralNews(langId: langId, latitude: latitude, longitude: longitude, offset: (state as GeneralNewsFetchSuccess).generalNews.length);
|
||||
if (!result[ERROR]) {
|
||||
List<NewsModel> updatedResults = (state as GeneralNewsFetchSuccess).generalNews;
|
||||
updatedResults.addAll((result[DATA] as List).map((e) => NewsModel.fromJson(e)).toList());
|
||||
emit(GeneralNewsFetchSuccess(generalNews: updatedResults, totalCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} else {
|
||||
emit(GeneralNewsFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(GeneralNewsFetchSuccess(
|
||||
generalNews: (state as GeneralNewsFetchSuccess).generalNews,
|
||||
hasMoreFetchError: true,
|
||||
totalCount: (state as GeneralNewsFetchSuccess).totalCount,
|
||||
hasMore: (state as GeneralNewsFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/getSurveyAnswerCubit.dart
Normal file
37
news-app/lib/cubits/getSurveyAnswerCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/GetSurveyAnswer/getSurveyAnsRepository.dart';
|
||||
|
||||
abstract class GetSurveyAnsState {}
|
||||
|
||||
class GetSurveyAnsInitial extends GetSurveyAnsState {}
|
||||
|
||||
class GetSurveyAnsFetchInProgress extends GetSurveyAnsState {}
|
||||
|
||||
class GetSurveyAnsFetchSuccess extends GetSurveyAnsState {
|
||||
final List<NewsModel> getSurveyAns;
|
||||
|
||||
GetSurveyAnsFetchSuccess({required this.getSurveyAns});
|
||||
}
|
||||
|
||||
class GetSurveyAnsFetchFailure extends GetSurveyAnsState {
|
||||
final String errorMessage;
|
||||
|
||||
GetSurveyAnsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class GetSurveyAnsCubit extends Cubit<GetSurveyAnsState> {
|
||||
final GetSurveyAnsRepository _getSurveyAnsRepository;
|
||||
|
||||
GetSurveyAnsCubit(this._getSurveyAnsRepository) : super(GetSurveyAnsInitial());
|
||||
|
||||
void getSurveyAns({required String langId}) async {
|
||||
try {
|
||||
emit(GetSurveyAnsFetchInProgress());
|
||||
final result = await _getSurveyAnsRepository.getSurveyAns(langId: langId);
|
||||
emit(GetSurveyAnsFetchSuccess(getSurveyAns: result['GetSurveyAns']));
|
||||
} catch (e) {
|
||||
emit(GetSurveyAnsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
35
news-app/lib/cubits/getUserDataByIdCubit.dart
Normal file
35
news-app/lib/cubits/getUserDataByIdCubit.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/GetUserById/getUserByIdRepository.dart';
|
||||
|
||||
abstract class GetUserByIdState {}
|
||||
|
||||
class GetUserByIdInitial extends GetUserByIdState {}
|
||||
|
||||
class GetUserByIdFetchInProgress extends GetUserByIdState {}
|
||||
|
||||
class GetUserByIdFetchSuccess extends GetUserByIdState {
|
||||
var result;
|
||||
|
||||
GetUserByIdFetchSuccess({required this.result});
|
||||
}
|
||||
|
||||
class GetUserByIdFetchFailure extends GetUserByIdState {
|
||||
final String errorMessage;
|
||||
|
||||
GetUserByIdFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class GetUserByIdCubit extends Cubit<GetUserByIdState> {
|
||||
final GetUserByIdRepository _getUserByIdRepository;
|
||||
|
||||
GetUserByIdCubit(this._getUserByIdRepository) : super(GetUserByIdInitial());
|
||||
|
||||
void getUserById() {
|
||||
emit(GetUserByIdFetchInProgress());
|
||||
_getUserByIdRepository.getUserById().then((value) {
|
||||
emit(GetUserByIdFetchSuccess(result: value));
|
||||
}).catchError((e) {
|
||||
emit(GetUserByIdFetchFailure(e.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
76
news-app/lib/cubits/getUserDraftedNewsCubit.dart
Normal file
76
news-app/lib/cubits/getUserDraftedNewsCubit.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class GetUserDraftedNewsState {}
|
||||
|
||||
class GetUserDraftedNewsInitial extends GetUserDraftedNewsState {}
|
||||
|
||||
class GetUserDraftedNewsFetchInProgress extends GetUserDraftedNewsState {}
|
||||
|
||||
class GetUserDraftedNewsFetchSuccess extends GetUserDraftedNewsState {
|
||||
final List<NewsModel> GetUserDraftedNews;
|
||||
final int totalGetUserDraftedNewsCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
GetUserDraftedNewsFetchSuccess({required this.GetUserDraftedNews, required this.totalGetUserDraftedNewsCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class GetUserDraftedNewsFetchFailure extends GetUserDraftedNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
GetUserDraftedNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class GetUserDraftedNewsCubit extends Cubit<GetUserDraftedNewsState> {
|
||||
GetUserDraftedNewsCubit() : super(GetUserDraftedNewsInitial());
|
||||
|
||||
void getGetUserDraftedNews({int? userId}) async {
|
||||
try {
|
||||
emit(GetUserDraftedNewsFetchInProgress());
|
||||
final apiUrl = "${Api.getDraftNewsApi}/${userId}";
|
||||
final result = await Api.sendApiRequest(body: null, url: apiUrl);
|
||||
(!result[ERROR])
|
||||
? emit(GetUserDraftedNewsFetchSuccess(
|
||||
GetUserDraftedNews: (result[DATA][NEWS][DATA] as List).map((e) => NewsModel.fromJson(e)).toList(),
|
||||
totalGetUserDraftedNewsCount: result[DATA][NEWS][TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: (result[DATA][NEWS][DATA] as List).length < result[DATA][NEWS][TOTAL]))
|
||||
: emit(GetUserDraftedNewsFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(GetUserDraftedNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreGetUserDraftedNews() {
|
||||
return (state is GetUserDraftedNewsFetchSuccess) ? (state as GetUserDraftedNewsFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void deleteNews(int index) {
|
||||
if (state is GetUserDraftedNewsFetchSuccess) {
|
||||
List<NewsModel> newsList = List.from((state as GetUserDraftedNewsFetchSuccess).GetUserDraftedNews)..removeAt(index);
|
||||
|
||||
emit(GetUserDraftedNewsFetchSuccess(
|
||||
GetUserDraftedNews: newsList,
|
||||
hasMore: (state as GetUserDraftedNewsFetchSuccess).hasMore,
|
||||
hasMoreFetchError: false,
|
||||
totalGetUserDraftedNewsCount: (state as GetUserDraftedNewsFetchSuccess).totalGetUserDraftedNewsCount - 1));
|
||||
}
|
||||
}
|
||||
|
||||
void deleteImageId(int index) {
|
||||
if (state is GetUserDraftedNewsFetchSuccess) {
|
||||
List<NewsModel> newsList = (state as GetUserDraftedNewsFetchSuccess).GetUserDraftedNews;
|
||||
|
||||
newsList[index].imageDataList!.removeAt(index);
|
||||
|
||||
emit(GetUserDraftedNewsFetchSuccess(
|
||||
GetUserDraftedNews: newsList,
|
||||
hasMore: (state as GetUserDraftedNewsFetchSuccess).hasMore,
|
||||
hasMoreFetchError: false,
|
||||
totalGetUserDraftedNewsCount: (state as GetUserDraftedNewsFetchSuccess).totalGetUserDraftedNewsCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
87
news-app/lib/cubits/getUserNewsCubit.dart
Normal file
87
news-app/lib/cubits/getUserNewsCubit.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/GetUserNews/getUserNewsRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class GetUserNewsState {}
|
||||
|
||||
class GetUserNewsInitial extends GetUserNewsState {}
|
||||
|
||||
class GetUserNewsFetchInProgress extends GetUserNewsState {}
|
||||
|
||||
class GetUserNewsFetchSuccess extends GetUserNewsState {
|
||||
final List<NewsModel> getUserNews;
|
||||
final int totalGetUserNewsCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
GetUserNewsFetchSuccess({required this.getUserNews, required this.totalGetUserNewsCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class GetUserNewsFetchFailure extends GetUserNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
GetUserNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class GetUserNewsCubit extends Cubit<GetUserNewsState> {
|
||||
final GetUserNewsRepository _getUserNewsRepository;
|
||||
|
||||
GetUserNewsCubit(this._getUserNewsRepository) : super(GetUserNewsInitial());
|
||||
|
||||
void getGetUserNews({String? latitude, String? longitude}) async {
|
||||
try {
|
||||
emit(GetUserNewsFetchInProgress());
|
||||
final result = await _getUserNewsRepository.getGetUserNews(limit: limitOfAPIData.toString(), offset: "0", latitude: latitude, longitude: longitude);
|
||||
(!result[ERROR])
|
||||
? emit(GetUserNewsFetchSuccess(
|
||||
getUserNews: result['GetUserNews'], totalGetUserNewsCount: result[TOTAL], hasMoreFetchError: false, hasMore: (result['GetUserNews'] as List<NewsModel>).length < result[TOTAL]))
|
||||
: emit(GetUserNewsFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(GetUserNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreGetUserNews() {
|
||||
return (state is GetUserNewsFetchSuccess) ? (state as GetUserNewsFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreGetUserNews({String? latitude, String? longitude}) async {
|
||||
if (state is GetUserNewsFetchSuccess) {
|
||||
try {
|
||||
final result = await _getUserNewsRepository.getGetUserNews(
|
||||
limit: limitOfAPIData.toString(), offset: (state as GetUserNewsFetchSuccess).getUserNews.length.toString(), latitude: latitude, longitude: longitude);
|
||||
List<NewsModel> updatedResults = (state as GetUserNewsFetchSuccess).getUserNews;
|
||||
updatedResults.addAll(result['GetUserNews'] as List<NewsModel>);
|
||||
emit(GetUserNewsFetchSuccess(getUserNews: updatedResults, totalGetUserNewsCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(GetUserNewsFetchSuccess(
|
||||
getUserNews: (state as GetUserNewsFetchSuccess).getUserNews,
|
||||
hasMoreFetchError: true,
|
||||
totalGetUserNewsCount: (state as GetUserNewsFetchSuccess).totalGetUserNewsCount,
|
||||
hasMore: (state as GetUserNewsFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void deleteNews(int index) {
|
||||
if (state is GetUserNewsFetchSuccess) {
|
||||
List<NewsModel> newsList = List.from((state as GetUserNewsFetchSuccess).getUserNews)..removeAt(index);
|
||||
|
||||
emit(GetUserNewsFetchSuccess(
|
||||
getUserNews: newsList, hasMore: (state as GetUserNewsFetchSuccess).hasMore, hasMoreFetchError: false, totalGetUserNewsCount: (state as GetUserNewsFetchSuccess).totalGetUserNewsCount - 1));
|
||||
}
|
||||
}
|
||||
|
||||
void deleteImageId(int index) {
|
||||
if (state is GetUserNewsFetchSuccess) {
|
||||
List<NewsModel> newsList = (state as GetUserNewsFetchSuccess).getUserNews;
|
||||
|
||||
newsList[index].imageDataList!.removeAt(index);
|
||||
|
||||
emit(GetUserNewsFetchSuccess(
|
||||
getUserNews: newsList, hasMore: (state as GetUserNewsFetchSuccess).hasMore, hasMoreFetchError: false, totalGetUserNewsCount: (state as GetUserNewsFetchSuccess).totalGetUserNewsCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
47
news-app/lib/cubits/languageCubit.dart
Normal file
47
news-app/lib/cubits/languageCubit.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/appLanguageModel.dart';
|
||||
import 'package:news/data/repositories/language/languageRepository.dart';
|
||||
|
||||
abstract class LanguageState {}
|
||||
|
||||
class LanguageInitial extends LanguageState {}
|
||||
|
||||
class LanguageFetchInProgress extends LanguageState {}
|
||||
|
||||
class LanguageFetchSuccess extends LanguageState {
|
||||
final List<LanguageModel> language;
|
||||
|
||||
LanguageFetchSuccess({required this.language});
|
||||
}
|
||||
|
||||
class LanguageFetchFailure extends LanguageState {
|
||||
final String errorMessage;
|
||||
|
||||
LanguageFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class LanguageCubit extends Cubit<LanguageState> {
|
||||
final LanguageRepository _languageRepository;
|
||||
|
||||
LanguageCubit(this._languageRepository) : super(LanguageInitial());
|
||||
|
||||
Future<List<LanguageModel>> getLanguage() async {
|
||||
try {
|
||||
emit(LanguageFetchInProgress());
|
||||
final result = await _languageRepository.getLanguage();
|
||||
emit(LanguageFetchSuccess(language: result['Language']));
|
||||
return result['Language'];
|
||||
} catch (e) {
|
||||
emit(LanguageFetchFailure(e.toString()));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
List<LanguageModel> langList() {
|
||||
return (state is LanguageFetchSuccess) ? (state as LanguageFetchSuccess).language : [];
|
||||
}
|
||||
|
||||
int getLanguageIndex({required String langName}) {
|
||||
return (state is LanguageFetchSuccess) ? (state as LanguageFetchSuccess).language.indexWhere((element) => element.language == langName) : 0;
|
||||
}
|
||||
}
|
||||
57
news-app/lib/cubits/languageJsonCubit.dart
Normal file
57
news-app/lib/cubits/languageJsonCubit.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/LanguageJson/languageJsonRepository.dart';
|
||||
import 'package:news/utils/appLanguages.dart';
|
||||
|
||||
abstract class LanguageJsonState {}
|
||||
|
||||
class LanguageJsonInitial extends LanguageJsonState {}
|
||||
|
||||
class LanguageJsonFetchInProgress extends LanguageJsonState {}
|
||||
|
||||
class LanguageJsonFetchSuccess extends LanguageJsonState {
|
||||
Map<dynamic, dynamic> languageJson;
|
||||
|
||||
LanguageJsonFetchSuccess({required this.languageJson});
|
||||
}
|
||||
|
||||
class LanguageJsonFetchFailure extends LanguageJsonState {
|
||||
final String errorMessage;
|
||||
|
||||
LanguageJsonFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class LanguageJsonCubit extends Cubit<LanguageJsonState> {
|
||||
final LanguageJsonRepository _languageJsonRepository;
|
||||
|
||||
LanguageJsonCubit(this._languageJsonRepository) : super(LanguageJsonInitial());
|
||||
|
||||
void fetchCurrentLanguageAndLabels(String currentLanguage) async {
|
||||
try {
|
||||
emit(LanguageJsonFetchInProgress());
|
||||
|
||||
await _languageJsonRepository.fetchLanguageLabels(currentLanguage).then((value) {
|
||||
emit(LanguageJsonFetchSuccess(languageJson: value));
|
||||
});
|
||||
} catch (e) {
|
||||
emit(LanguageJsonFetchSuccess(languageJson: appLanguageLabelKeys));
|
||||
}
|
||||
}
|
||||
|
||||
void getLanguageJson({required String lanCode}) async {
|
||||
try {
|
||||
emit(LanguageJsonFetchInProgress());
|
||||
final result = await _languageJsonRepository.getLanguageJson(lanCode: lanCode);
|
||||
emit(LanguageJsonFetchSuccess(languageJson: result));
|
||||
} catch (e) {
|
||||
emit(LanguageJsonFetchSuccess(languageJson: appLanguageLabelKeys));
|
||||
}
|
||||
}
|
||||
|
||||
String getTranslatedLabels(String label) {
|
||||
if (state is LanguageJsonFetchSuccess) {
|
||||
return (state as LanguageJsonFetchSuccess).languageJson[label] ?? appLanguageLabelKeys[label] ?? label;
|
||||
} else {
|
||||
return appLanguageLabelKeys[label] ?? label;
|
||||
}
|
||||
}
|
||||
}
|
||||
39
news-app/lib/cubits/liveStreamCubit.dart
Normal file
39
news-app/lib/cubits/liveStreamCubit.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/LiveStreamingModel.dart';
|
||||
import 'package:news/data/repositories/LiveStream/liveStreamRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class LiveStreamState {}
|
||||
|
||||
class LiveStreamInitial extends LiveStreamState {}
|
||||
|
||||
class LiveStreamFetchInProgress extends LiveStreamState {}
|
||||
|
||||
class LiveStreamFetchSuccess extends LiveStreamState {
|
||||
final List<LiveStreamingModel> liveStream;
|
||||
|
||||
LiveStreamFetchSuccess({required this.liveStream});
|
||||
}
|
||||
|
||||
class LiveStreamFetchFailure extends LiveStreamState {
|
||||
final String errorMessage;
|
||||
|
||||
LiveStreamFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class LiveStreamCubit extends Cubit<LiveStreamState> {
|
||||
final LiveStreamRepository _liveStreamRepository;
|
||||
|
||||
LiveStreamCubit(this._liveStreamRepository) : super(LiveStreamInitial());
|
||||
|
||||
void getLiveStream({required String langId}) async {
|
||||
try {
|
||||
emit(LiveStreamFetchInProgress());
|
||||
final result = await _liveStreamRepository.getLiveStream(langId: langId);
|
||||
|
||||
(!result[ERROR]) ? emit(LiveStreamFetchSuccess(liveStream: result['LiveStream'])) : emit(LiveStreamFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(LiveStreamFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
73
news-app/lib/cubits/locationCityCubit.dart
Normal file
73
news-app/lib/cubits/locationCityCubit.dart
Normal file
@@ -0,0 +1,73 @@
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/locationCityModel.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class LocationCityState {}
|
||||
|
||||
class LocationCityInitial extends LocationCityState {}
|
||||
|
||||
class LocationCityFetchInProgress extends LocationCityState {}
|
||||
|
||||
class LocationCityFetchSuccess extends LocationCityState {
|
||||
final List<LocationCityModel> locationCity;
|
||||
final int totalLocations;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
LocationCityFetchSuccess({required this.locationCity, required this.totalLocations, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class LocationCityFetchFailure extends LocationCityState {
|
||||
final String errorMessage;
|
||||
|
||||
LocationCityFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class LocationCityCubit extends Cubit<LocationCityState> {
|
||||
LocationCityCubit() : super(LocationCityInitial());
|
||||
|
||||
void getLocationCity() async {
|
||||
try {
|
||||
final result = await Api.sendApiRequest(body: {LIMIT: limitOfAPIData, OFFSET: 0}, url: Api.getLocationCityApi);
|
||||
emit(LocationCityFetchSuccess(
|
||||
totalLocations: result[TOTAL],
|
||||
locationCity: (result[DATA] as List).map((e) => LocationCityModel.fromJson(e)).toList(),
|
||||
hasMore: (result[DATA] as List).map((e) => LocationCityModel.fromJson(e)).toList().length < result[TOTAL],
|
||||
hasMoreFetchError: false));
|
||||
} catch (e) {
|
||||
emit(LocationCityFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void getMoreLocationCity() async {
|
||||
if (state is LocationCityFetchSuccess) {
|
||||
try {
|
||||
await Api.sendApiRequest(body: {LIMIT: limitOfAPIData, OFFSET: (state as LocationCityFetchSuccess).locationCity.length.toString()}, url: Api.getLocationCityApi).then((value) {
|
||||
List<LocationCityModel> updatedResults = (state as LocationCityFetchSuccess).locationCity;
|
||||
updatedResults.addAll((value[DATA] as List).map((e) => LocationCityModel.fromJson(e)).toList());
|
||||
emit(LocationCityFetchSuccess(locationCity: updatedResults, totalLocations: value[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < value[TOTAL]));
|
||||
});
|
||||
} catch (e) {
|
||||
emit(LocationCityFetchSuccess(
|
||||
locationCity: (state as LocationCityFetchSuccess).locationCity,
|
||||
totalLocations: (state as LocationCityFetchSuccess).totalLocations,
|
||||
hasMoreFetchError: true,
|
||||
hasMore: (state as LocationCityFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreLocation() {
|
||||
return (state is LocationCityFetchSuccess) ? (state as LocationCityFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
List<LocationCityModel> getLocationCityList() {
|
||||
return (state is LocationCityFetchSuccess) ? (state as LocationCityFetchSuccess).locationCity : [];
|
||||
}
|
||||
|
||||
int getLocationIndex({required String locationName}) {
|
||||
return (state is LocationCityFetchSuccess) ? (state as LocationCityFetchSuccess).locationCity.indexWhere((element) => element.locationName == locationName) : 0;
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/otherPagesCubit.dart
Normal file
37
news-app/lib/cubits/otherPagesCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/OtherPageModel.dart';
|
||||
import 'package:news/data/repositories/OtherPages/otherPagesRepository.dart';
|
||||
|
||||
abstract class OtherPageState {}
|
||||
|
||||
class OtherPageInitial extends OtherPageState {}
|
||||
|
||||
class OtherPageFetchInProgress extends OtherPageState {}
|
||||
|
||||
class OtherPageFetchSuccess extends OtherPageState {
|
||||
final List<OtherPageModel> otherPage;
|
||||
|
||||
OtherPageFetchSuccess({required this.otherPage});
|
||||
}
|
||||
|
||||
class OtherPageFetchFailure extends OtherPageState {
|
||||
final String errorMessage;
|
||||
|
||||
OtherPageFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class OtherPageCubit extends Cubit<OtherPageState> {
|
||||
final OtherPageRepository _otherPageRepository;
|
||||
|
||||
OtherPageCubit(this._otherPageRepository) : super(OtherPageInitial());
|
||||
|
||||
void getOtherPage({required String langId}) async {
|
||||
emit(OtherPageFetchInProgress());
|
||||
try {
|
||||
final result = await _otherPageRepository.getOtherPage(langId: langId);
|
||||
emit(OtherPageFetchSuccess(otherPage: result['OtherPage']));
|
||||
} catch (e) {
|
||||
emit(OtherPageFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
38
news-app/lib/cubits/privacyTermsCubit.dart
Normal file
38
news-app/lib/cubits/privacyTermsCubit.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/OtherPageModel.dart';
|
||||
import 'package:news/data/repositories/OtherPages/otherPagesRepository.dart';
|
||||
|
||||
abstract class PrivacyTermsState {}
|
||||
|
||||
class PrivacyTermsInitial extends PrivacyTermsState {}
|
||||
|
||||
class PrivacyTermsFetchInProgress extends PrivacyTermsState {}
|
||||
|
||||
class PrivacyTermsFetchSuccess extends PrivacyTermsState {
|
||||
final OtherPageModel termsPolicy;
|
||||
final OtherPageModel privacyPolicy;
|
||||
|
||||
PrivacyTermsFetchSuccess({required this.termsPolicy, required this.privacyPolicy});
|
||||
}
|
||||
|
||||
class PrivacyTermsFetchFailure extends PrivacyTermsState {
|
||||
final String errorMessage;
|
||||
|
||||
PrivacyTermsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class PrivacyTermsCubit extends Cubit<PrivacyTermsState> {
|
||||
final OtherPageRepository _otherPageRepository;
|
||||
|
||||
PrivacyTermsCubit(this._otherPageRepository) : super(PrivacyTermsInitial());
|
||||
|
||||
void getPrivacyTerms({required String langId}) async {
|
||||
emit(PrivacyTermsFetchInProgress());
|
||||
try {
|
||||
final Map<String, dynamic> result = await _otherPageRepository.getPrivacyTermsPage(langId: langId);
|
||||
emit(PrivacyTermsFetchSuccess(privacyPolicy: OtherPageModel.fromPrivacyTermsJson(result['privacy_policy']), termsPolicy: OtherPageModel.fromPrivacyTermsJson(result['terms_policy'])));
|
||||
} catch (e) {
|
||||
emit(PrivacyTermsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
77
news-app/lib/cubits/relatedNewsCubit.dart
Normal file
77
news-app/lib/cubits/relatedNewsCubit.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/RelatedNews/relatedNewsRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class RelatedNewsState {}
|
||||
|
||||
class RelatedNewsInitial extends RelatedNewsState {}
|
||||
|
||||
class RelatedNewsFetchInProgress extends RelatedNewsState {}
|
||||
|
||||
class RelatedNewsFetchSuccess extends RelatedNewsState {
|
||||
final List<NewsModel> relatedNews;
|
||||
final int totalRelatedNewsCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
RelatedNewsFetchSuccess({required this.relatedNews, required this.totalRelatedNewsCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class RelatedNewsFetchFailure extends RelatedNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
RelatedNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class RelatedNewsCubit extends Cubit<RelatedNewsState> {
|
||||
final RelatedNewsRepository _relatedNewsRepository;
|
||||
|
||||
RelatedNewsCubit(this._relatedNewsRepository) : super(RelatedNewsInitial());
|
||||
|
||||
void getRelatedNews({required String langId, String? catId, String? subCatId}) async {
|
||||
try {
|
||||
emit(RelatedNewsFetchInProgress());
|
||||
final result = await _relatedNewsRepository.getRelatedNews(perPage: limitOfAPIData.toString(), offset: "0", langId: langId, catId: catId, subCatId: subCatId);
|
||||
emit(RelatedNewsFetchSuccess(
|
||||
relatedNews: result['RelatedNews'], totalRelatedNewsCount: result[TOTAL], hasMoreFetchError: false, hasMore: (result['RelatedNews'] as List<NewsModel>).length < result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(RelatedNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreRelatedNews() {
|
||||
return (state is RelatedNewsFetchSuccess) ? (state as RelatedNewsFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreRelatedNews({required String langId, String? catId, String? subCatId, String? latitude, String? longitude}) async {
|
||||
if (state is RelatedNewsFetchSuccess) {
|
||||
try {
|
||||
final result = await _relatedNewsRepository.getRelatedNews(
|
||||
perPage: limitOfAPIData.toString(),
|
||||
offset: (state as RelatedNewsFetchSuccess).relatedNews.length.toString(),
|
||||
langId: langId,
|
||||
subCatId: subCatId,
|
||||
catId: catId,
|
||||
latitude: latitude,
|
||||
longitude: longitude);
|
||||
List<NewsModel> updatedResults = (state as RelatedNewsFetchSuccess).relatedNews;
|
||||
updatedResults.addAll(result['RelatedNews'] as List<NewsModel>);
|
||||
emit(RelatedNewsFetchSuccess(
|
||||
relatedNews: updatedResults,
|
||||
totalRelatedNewsCount: result[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: updatedResults.length < result[TOTAL],
|
||||
));
|
||||
} catch (e) {
|
||||
emit(RelatedNewsFetchSuccess(
|
||||
relatedNews: (state as RelatedNewsFetchSuccess).relatedNews,
|
||||
hasMoreFetchError: true,
|
||||
totalRelatedNewsCount: (state as RelatedNewsFetchSuccess).totalRelatedNewsCount,
|
||||
hasMore: (state as RelatedNewsFetchSuccess).hasMore,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
71
news-app/lib/cubits/rssFeedCubit.dart
Normal file
71
news-app/lib/cubits/rssFeedCubit.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/RSSFeedModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class RSSFeedState {}
|
||||
|
||||
class RSSFeedInitial extends RSSFeedState {}
|
||||
|
||||
class RSSFeedFetchInProgress extends RSSFeedState {}
|
||||
|
||||
class RSSFeedFetchSuccess extends RSSFeedState {
|
||||
final List<RSSFeedModel> RSSFeed;
|
||||
final int totalRSSFeedCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
RSSFeedFetchSuccess({required this.RSSFeed, required this.totalRSSFeedCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class RSSFeedFetchFailure extends RSSFeedState {
|
||||
final String errorMessage;
|
||||
|
||||
RSSFeedFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class RSSFeedCubit extends Cubit<RSSFeedState> {
|
||||
RSSFeedCubit() : super(RSSFeedInitial());
|
||||
int limit = 10;
|
||||
|
||||
void getRSSFeed({required String langId, String? categoryId, String? subCategoryId}) async {
|
||||
try {
|
||||
emit(RSSFeedFetchInProgress());
|
||||
|
||||
final result = await Api.sendApiRequest(
|
||||
body: {LANGUAGE_ID: langId, if (categoryId != null) CATEGORY_ID: categoryId, if (subCategoryId != null) SUBCAT_ID: subCategoryId, LIMIT: limit, OFFSET: 0}, url: Api.rssFeedApi);
|
||||
|
||||
(!result[ERROR])
|
||||
? emit(RSSFeedFetchSuccess(
|
||||
RSSFeed: (result[DATA] as List).map((e) => RSSFeedModel.fromJson(e)).toList(), totalRSSFeedCount: result[TOTAL], hasMoreFetchError: false, hasMore: result.length < result[TOTAL]))
|
||||
: emit(RSSFeedFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(RSSFeedFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreRSSFeed() {
|
||||
return (state is RSSFeedFetchSuccess) ? (state as RSSFeedFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreRSSFeed({required String langId}) async {
|
||||
if (state is RSSFeedFetchSuccess) {
|
||||
try {
|
||||
final result = await Api.sendApiRequest(body: {LANGUAGE_ID: langId, LIMIT: limit, OFFSET: (state as RSSFeedFetchSuccess).RSSFeed.length}, url: Api.rssFeedApi);
|
||||
if (!result[ERROR]) {
|
||||
List<RSSFeedModel> updatedResults = (state as RSSFeedFetchSuccess).RSSFeed;
|
||||
updatedResults.addAll((result[DATA] as List).map((e) => RSSFeedModel.fromJson(e)).toList());
|
||||
emit(RSSFeedFetchSuccess(RSSFeed: updatedResults, totalRSSFeedCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} else {
|
||||
emit(RSSFeedFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(RSSFeedFetchSuccess(
|
||||
RSSFeed: (state as RSSFeedFetchSuccess).RSSFeed,
|
||||
hasMoreFetchError: true,
|
||||
totalRSSFeedCount: (state as RSSFeedFetchSuccess).totalRSSFeedCount,
|
||||
hasMore: (state as RSSFeedFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
132
news-app/lib/cubits/sectionByIdCubit.dart
Normal file
132
news-app/lib/cubits/sectionByIdCubit.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/BreakingNewsModel.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/SectionById/sectionByIdRepository.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class SectionByIdState {}
|
||||
|
||||
class SectionByIdInitial extends SectionByIdState {}
|
||||
|
||||
class SectionByIdFetchInProgress extends SectionByIdState {}
|
||||
|
||||
class SectionByIdFetchSuccess extends SectionByIdState {
|
||||
final List<NewsModel> newsModel;
|
||||
final List<BreakingNewsModel> breakNewsModel;
|
||||
final int totalCount;
|
||||
final String type;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
SectionByIdFetchSuccess({required this.newsModel, required this.breakNewsModel, required this.totalCount, required this.type, required this.hasMore, required this.hasMoreFetchError});
|
||||
}
|
||||
|
||||
class SectionByIdFetchFailure extends SectionByIdState {
|
||||
final String errorMessage;
|
||||
|
||||
SectionByIdFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SectionByIdCubit extends Cubit<SectionByIdState> {
|
||||
final SectionByIdRepository _sectionByIdRepository;
|
||||
final int limitOfFeaturedSectionData = 10;
|
||||
|
||||
SectionByIdCubit(this._sectionByIdRepository) : super(SectionByIdInitial());
|
||||
|
||||
void getSectionById({required String langId, required String sectionId, String? latitude, String? longitude, String? limit, String? offset}) async {
|
||||
try {
|
||||
emit(SectionByIdFetchInProgress());
|
||||
final result = await _sectionByIdRepository.getSectionById(
|
||||
offset: offset ?? "0", limit: limit ?? limitOfFeaturedSectionData.toString(), langId: langId, sectionId: sectionId, latitude: latitude, longitude: longitude);
|
||||
if (!result[ERROR]) {
|
||||
int totalSections = (result[DATA][0].newsType == "news" || result[DATA][0].newsType == "user_choice")
|
||||
? result[DATA][0].newsTotal!
|
||||
: result[DATA][0].newsType == "breaking_news"
|
||||
? result[DATA][0].breakNewsTotal!
|
||||
: result[DATA][0].videosTotal!;
|
||||
List<NewsModel> newsSection = (result[DATA][0].newsType == "news" || result[DATA][0].newsType == "user_choice")
|
||||
? result[DATA][0].news!
|
||||
: result[DATA][0].videosType == "news"
|
||||
? result[DATA][0].videos!
|
||||
: [];
|
||||
List<BreakingNewsModel> brNewsSection = result[DATA][0].newsType == "breaking_news"
|
||||
? result[DATA][0].breakNews!
|
||||
: result[DATA][0].videosType == "breaking_news"
|
||||
? result[DATA][0].breakVideos!
|
||||
: [];
|
||||
|
||||
emit(SectionByIdFetchSuccess(
|
||||
newsModel: newsSection,
|
||||
breakNewsModel: brNewsSection,
|
||||
totalCount: totalSections,
|
||||
type: result[DATA][0].newsType!,
|
||||
hasMore: ((result[DATA][0].newsType! == NEWS) ? (newsSection.length < totalSections) : (brNewsSection.length < totalSections)),
|
||||
hasMoreFetchError: false));
|
||||
} else {
|
||||
emit(SectionByIdFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!isClosed) emit(SectionByIdFetchFailure(e.toString())); //isClosed checked to resolve Bad state issue of Bloc
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreSections() {
|
||||
return (state is SectionByIdFetchSuccess) ? (state as SectionByIdFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreSectionById({required String langId, required String sectionId, String? latitude, String? longitude, String? limit, String? offset}) async {
|
||||
if (state is SectionByIdFetchSuccess) {
|
||||
try {
|
||||
final result = await _sectionByIdRepository.getSectionById(
|
||||
sectionId: sectionId,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
limit: limit ?? limitOfFeaturedSectionData.toString(),
|
||||
offset: (state as SectionByIdFetchSuccess).newsModel.length.toString(),
|
||||
langId: langId);
|
||||
List<NewsModel> updatedResults = [];
|
||||
List<BreakingNewsModel> updatedBrResults = [];
|
||||
|
||||
if (result[DATA][0].newsType! == NEWS || result[DATA][0].videosType == NEWS) {
|
||||
updatedResults = (state as SectionByIdFetchSuccess).newsModel;
|
||||
List<NewsModel> newValues = (result[DATA][0].newsType == "news" || result[DATA][0].newsType == "user_choice")
|
||||
? result[DATA][0].news ?? []
|
||||
: result[DATA][0].videosType == "news"
|
||||
? result[DATA][0].videos ?? []
|
||||
: [];
|
||||
updatedResults.addAll(newValues);
|
||||
} else {
|
||||
updatedBrResults = (state as SectionByIdFetchSuccess).breakNewsModel;
|
||||
List<BreakingNewsModel> newValues = result[DATA][0].newsType == "breaking_news"
|
||||
? result[DATA][0].breakNews!
|
||||
: result[DATA][0].videosType == "breaking_news"
|
||||
? result[DATA][0].breakVideos!
|
||||
: [];
|
||||
updatedBrResults.addAll(newValues);
|
||||
}
|
||||
|
||||
int totalCount = (result[DATA][0].newsType == "news" || result[DATA][0].newsType == "user_choice")
|
||||
? result[DATA][0].newsTotal!
|
||||
: result[DATA][0].newsType == "breaking_news"
|
||||
? result[DATA][0].breakNewsTotal!
|
||||
: result[DATA][0].videosTotal!;
|
||||
|
||||
emit(SectionByIdFetchSuccess(
|
||||
newsModel: updatedResults,
|
||||
breakNewsModel: updatedBrResults,
|
||||
type: result[DATA][0].newsType!,
|
||||
totalCount: totalCount,
|
||||
hasMoreFetchError: false,
|
||||
hasMore: (result[DATA][0].newsType! == NEWS || result[DATA][0].videosType == NEWS) ? (updatedResults.length < totalCount) : (updatedBrResults.length < totalCount)));
|
||||
} catch (e) {
|
||||
emit(SectionByIdFetchSuccess(
|
||||
type: (state as SectionByIdFetchSuccess).type,
|
||||
breakNewsModel: (state as SectionByIdFetchSuccess).breakNewsModel,
|
||||
newsModel: (state as SectionByIdFetchSuccess).newsModel,
|
||||
hasMoreFetchError: (e.toString() == "No Data Found") ? false : true,
|
||||
totalCount: (state as SectionByIdFetchSuccess).totalCount,
|
||||
hasMore: (state as SectionByIdFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/setNewsViewsCubit.dart
Normal file
37
news-app/lib/cubits/setNewsViewsCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/SetNewsViews/setNewsViewsRepository.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
|
||||
abstract class SetNewsViewsState {}
|
||||
|
||||
class SetNewsViewsInitial extends SetNewsViewsState {}
|
||||
|
||||
class SetNewsViewsInProgress extends SetNewsViewsState {}
|
||||
|
||||
class SetNewsViewsSuccess extends SetNewsViewsState {
|
||||
final String message;
|
||||
|
||||
SetNewsViewsSuccess(this.message);
|
||||
}
|
||||
|
||||
class SetNewsViewsFailure extends SetNewsViewsState {
|
||||
final String errorMessage;
|
||||
|
||||
SetNewsViewsFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SetNewsViewsCubit extends Cubit<SetNewsViewsState> {
|
||||
final SetNewsViewsRepository setNewsViewsRepository;
|
||||
|
||||
SetNewsViewsCubit(this.setNewsViewsRepository) : super(SetNewsViewsInitial());
|
||||
|
||||
void setNewsViews({required String newsId, required bool isBreakingNews}) {
|
||||
emit(SetNewsViewsInProgress());
|
||||
setNewsViewsRepository.setNewsViews(newsId: newsId, isBreakingNews: isBreakingNews).then((value) {
|
||||
emit(SetNewsViewsSuccess(value["message"]));
|
||||
}).catchError((e) {
|
||||
ApiMessageAndCodeException apiMessageAndCodeException = e;
|
||||
emit(SetNewsViewsFailure(apiMessageAndCodeException.errorMessage.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/setSurveyAnswerCubit.dart
Normal file
37
news-app/lib/cubits/setSurveyAnswerCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/SetSurveyAnswer/setSurveyAnsRepository.dart';
|
||||
|
||||
abstract class SetSurveyAnsState {}
|
||||
|
||||
class SetSurveyAnsInitial extends SetSurveyAnsState {}
|
||||
|
||||
class SetSurveyAnsFetchInProgress extends SetSurveyAnsState {}
|
||||
|
||||
class SetSurveyAnsFetchSuccess extends SetSurveyAnsState {
|
||||
var setSurveyAns;
|
||||
|
||||
SetSurveyAnsFetchSuccess({required this.setSurveyAns});
|
||||
}
|
||||
|
||||
class SetSurveyAnsFetchFailure extends SetSurveyAnsState {
|
||||
final String errorMessage;
|
||||
|
||||
SetSurveyAnsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SetSurveyAnsCubit extends Cubit<SetSurveyAnsState> {
|
||||
final SetSurveyAnsRepository _setSurveyAnsRepository;
|
||||
|
||||
SetSurveyAnsCubit(this._setSurveyAnsRepository) : super(SetSurveyAnsInitial());
|
||||
|
||||
void setSurveyAns({required String queId, required String optId}) async {
|
||||
try {
|
||||
emit(SetSurveyAnsFetchInProgress());
|
||||
final result = await _setSurveyAnsRepository.setSurveyAns(queId: queId, optId: optId);
|
||||
|
||||
emit(SetSurveyAnsFetchSuccess(setSurveyAns: result['SetSurveyAns']));
|
||||
} catch (e) {
|
||||
emit(SetSurveyAnsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
41
news-app/lib/cubits/settingCubit.dart
Normal file
41
news-app/lib/cubits/settingCubit.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/SettingsModel.dart';
|
||||
import 'package:news/data/repositories/Settings/settingRepository.dart';
|
||||
|
||||
class SettingsState {
|
||||
final SettingsModel? settingsModel;
|
||||
|
||||
SettingsState({this.settingsModel});
|
||||
}
|
||||
|
||||
class SettingsCubit extends Cubit<SettingsState> {
|
||||
final SettingsRepository _settingsRepository;
|
||||
|
||||
SettingsCubit(this._settingsRepository) : super(SettingsState()) {
|
||||
_getCurrentSettings();
|
||||
}
|
||||
|
||||
void _getCurrentSettings() {
|
||||
emit(SettingsState(settingsModel: SettingsModel.fromJson(_settingsRepository.getCurrentSettings())));
|
||||
}
|
||||
|
||||
SettingsModel getSettings() {
|
||||
return state.settingsModel!;
|
||||
}
|
||||
|
||||
void changeShowIntroSlider(bool value) {
|
||||
_settingsRepository.changeIntroSlider(value);
|
||||
emit(SettingsState(settingsModel: state.settingsModel!.copyWith(showIntroSlider: value)));
|
||||
}
|
||||
|
||||
void changeFcmToken(String value) {
|
||||
_settingsRepository.changeFcmToken(value);
|
||||
emit(SettingsState(settingsModel: state.settingsModel!.copyWith(token: value)));
|
||||
}
|
||||
|
||||
void changeNotification(bool value) {
|
||||
//set HiveBoxKey value for Enabled Notifications
|
||||
_settingsRepository.changeNotification(value);
|
||||
emit(SettingsState(settingsModel: state.settingsModel!.copyWith(notification: value)));
|
||||
}
|
||||
}
|
||||
38
news-app/lib/cubits/slugCheckCubit.dart
Normal file
38
news-app/lib/cubits/slugCheckCubit.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class SlugCheckState {}
|
||||
|
||||
class SlugCheckInitial extends SlugCheckState {}
|
||||
|
||||
class SlugCheckFetchInProgress extends SlugCheckState {}
|
||||
|
||||
class SlugCheckFetchSuccess extends SlugCheckState {
|
||||
String message;
|
||||
SlugCheckFetchSuccess({required this.message});
|
||||
}
|
||||
|
||||
class SlugCheckFetchFailure extends SlugCheckState {
|
||||
final String errorMessage;
|
||||
|
||||
SlugCheckFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SlugCheckCubit extends Cubit<SlugCheckState> {
|
||||
SlugCheckCubit() : super(SlugCheckInitial());
|
||||
|
||||
Future<void> checkSlugAvailability({required String slug, required String langId}) async {
|
||||
try {
|
||||
final result = await Api.sendApiRequest(body: {SLUG: slug, LANGUAGE_ID: langId, LIMIT: limitOfAPIData, OFFSET: 0}, url: Api.slugCheckApi);
|
||||
if (result[ERROR] == true) {
|
||||
emit(SlugCheckFetchFailure(result[MESSAGE].toString()));
|
||||
} else {
|
||||
emit(SlugCheckFetchSuccess(message: result[MESSAGE]));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(SlugCheckFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
49
news-app/lib/cubits/slugNewsCubit.dart
Normal file
49
news-app/lib/cubits/slugNewsCubit.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/utils/api.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class SlugNewsState {}
|
||||
|
||||
class SlugNewsInitial extends SlugNewsState {}
|
||||
|
||||
class SlugNewsFetchInProgress extends SlugNewsState {}
|
||||
|
||||
class SlugNewsFetchSuccess extends SlugNewsState {
|
||||
final List<NewsModel> generalNews;
|
||||
final int totalCount;
|
||||
|
||||
SlugNewsFetchSuccess({required this.totalCount, required this.generalNews});
|
||||
}
|
||||
|
||||
class SlugNewsFetchFailure extends SlugNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
SlugNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SlugNewsCubit extends Cubit<SlugNewsState> {
|
||||
SlugNewsCubit() : super(SlugNewsInitial());
|
||||
|
||||
Future<dynamic> getSlugNews({required String langId, String? latitude, String? longitude, int? offset, String? newsSlug}) async {
|
||||
emit(SlugNewsInitial());
|
||||
try {
|
||||
final body = {LANGUAGE_ID: langId, LIMIT: 20};
|
||||
|
||||
if (latitude != null && latitude != "null") body[LATITUDE] = latitude;
|
||||
if (longitude != null && longitude != "null") body[LONGITUDE] = longitude;
|
||||
if (offset != null) body[OFFSET] = offset;
|
||||
if (newsSlug != null && newsSlug != "null" && newsSlug.trim().isNotEmpty) body[SLUG] = newsSlug; // used in case of native link , details screen redirection
|
||||
|
||||
final result = await Api.sendApiRequest(body: body, url: Api.getNewsApi);
|
||||
if (!result[ERROR]) {
|
||||
emit(SlugNewsFetchSuccess(generalNews: (result[DATA] as List).map((e) => NewsModel.fromJson(e)).toList(), totalCount: result[TOTAL]));
|
||||
} else {
|
||||
emit(SlugNewsFetchFailure(result[MESSAGE]));
|
||||
}
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw ApiMessageAndCodeException(errorMessage: e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
81
news-app/lib/cubits/subCatNewsCubit.dart
Normal file
81
news-app/lib/cubits/subCatNewsCubit.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/SubCatNews/subCatRepository.dart';
|
||||
import 'package:news/utils/ErrorMessageKeys.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class SubCatNewsState {}
|
||||
|
||||
class SubCatNewsInitial extends SubCatNewsState {}
|
||||
|
||||
class SubCatNewsFetchInProgress extends SubCatNewsState {}
|
||||
|
||||
class SubCatNewsFetchSuccess extends SubCatNewsState {
|
||||
final List<NewsModel> subCatNews;
|
||||
final int totalSubCatNewsCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
final bool isFirst;
|
||||
|
||||
SubCatNewsFetchSuccess({required this.subCatNews, required this.totalSubCatNewsCount, required this.hasMoreFetchError, required this.hasMore, required this.isFirst});
|
||||
}
|
||||
|
||||
class SubCatNewsFetchFailure extends SubCatNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
SubCatNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SubCatNewsCubit extends Cubit<SubCatNewsState> {
|
||||
final SubCatNewsRepository _subCatNewsRepository;
|
||||
|
||||
SubCatNewsCubit(this._subCatNewsRepository) : super(SubCatNewsInitial());
|
||||
|
||||
void getSubCatNews({String? catId, String? subCatId, String? latitude, String? longitude, required String langId}) async {
|
||||
try {
|
||||
emit(SubCatNewsFetchInProgress());
|
||||
final result =
|
||||
await _subCatNewsRepository.getSubCatNews(limit: limitOfAPIData.toString(), offset: "0", subCatId: subCatId, langId: langId, catId: catId, latitude: latitude, longitude: longitude);
|
||||
(!result[ERROR])
|
||||
? emit(SubCatNewsFetchSuccess(
|
||||
subCatNews: result['SubCatNews'],
|
||||
totalSubCatNewsCount: result[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: (result['SubCatNews'] as List<NewsModel>).length < result[TOTAL],
|
||||
isFirst: true))
|
||||
: emit(SubCatNewsFetchFailure(ErrorMessageKeys.noDataMessage));
|
||||
} catch (e) {
|
||||
if (!isClosed) emit(SubCatNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreSubCatNews() {
|
||||
return (state is SubCatNewsFetchSuccess) ? (state as SubCatNewsFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreSubCatNews({String? catId, String? subCatId, String? latitude, String? longitude, required String langId}) async {
|
||||
if (state is SubCatNewsFetchSuccess) {
|
||||
try {
|
||||
final result = await _subCatNewsRepository.getSubCatNews(
|
||||
limit: limitOfAPIData.toString(),
|
||||
offset: (state as SubCatNewsFetchSuccess).subCatNews.length.toString(),
|
||||
langId: langId,
|
||||
catId: catId,
|
||||
subCatId: subCatId,
|
||||
latitude: latitude,
|
||||
longitude: longitude);
|
||||
List<NewsModel> updatedResults = (state as SubCatNewsFetchSuccess).subCatNews;
|
||||
updatedResults.addAll(result['SubCatNews'] as List<NewsModel>);
|
||||
emit(SubCatNewsFetchSuccess(subCatNews: updatedResults, totalSubCatNewsCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL], isFirst: false));
|
||||
} catch (e) {
|
||||
emit(SubCatNewsFetchSuccess(
|
||||
subCatNews: (state as SubCatNewsFetchSuccess).subCatNews,
|
||||
hasMoreFetchError: true,
|
||||
totalSubCatNewsCount: (state as SubCatNewsFetchSuccess).totalSubCatNewsCount,
|
||||
hasMore: (state as SubCatNewsFetchSuccess).hasMore,
|
||||
isFirst: false));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
news-app/lib/cubits/subCategoryCubit.dart
Normal file
40
news-app/lib/cubits/subCategoryCubit.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'package:news/data/models/CategoryModel.dart';
|
||||
import 'package:news/data/repositories/SubCategory/subCatRepository.dart';
|
||||
|
||||
abstract class SubCategoryState {}
|
||||
|
||||
class SubCategoryInitial extends SubCategoryState {}
|
||||
|
||||
class SubCategoryFetchInProgress extends SubCategoryState {}
|
||||
|
||||
class SubCategoryFetchSuccess extends SubCategoryState {
|
||||
final List<SubCategoryModel> subCategory;
|
||||
|
||||
SubCategoryFetchSuccess({required this.subCategory});
|
||||
}
|
||||
|
||||
class SubCategoryFetchFailure extends SubCategoryState {
|
||||
final String errorMessage;
|
||||
|
||||
SubCategoryFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SubCategoryCubit extends Cubit<SubCategoryState> {
|
||||
final SubCategoryRepository _subCategoryRepository;
|
||||
|
||||
SubCategoryCubit(this._subCategoryRepository) : super(SubCategoryInitial());
|
||||
|
||||
void getSubCategory({required BuildContext context, required String catId, required String langId}) async {
|
||||
try {
|
||||
emit(SubCategoryFetchInProgress());
|
||||
final result = await _subCategoryRepository.getSubCategory(context: context, catId: catId, langId: langId);
|
||||
|
||||
emit(SubCategoryFetchSuccess(subCategory: result['SubCategory']));
|
||||
} catch (e) {
|
||||
emit(SubCategoryFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
53
news-app/lib/cubits/surveyQuestionCubit.dart
Normal file
53
news-app/lib/cubits/surveyQuestionCubit.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/SurveyQuestion/surveyQueRepository.dart';
|
||||
|
||||
abstract class SurveyQuestionState {}
|
||||
|
||||
class SurveyQuestionInitial extends SurveyQuestionState {}
|
||||
|
||||
class SurveyQuestionFetchInProgress extends SurveyQuestionState {}
|
||||
|
||||
class SurveyQuestionFetchSuccess extends SurveyQuestionState {
|
||||
final List<NewsModel> surveyQuestion;
|
||||
|
||||
SurveyQuestionFetchSuccess({required this.surveyQuestion});
|
||||
}
|
||||
|
||||
class SurveyQuestionFetchFailure extends SurveyQuestionState {
|
||||
final String errorMessage;
|
||||
|
||||
SurveyQuestionFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class SurveyQuestionCubit extends Cubit<SurveyQuestionState> {
|
||||
final SurveyQuestionRepository _surveyQuestionRepository;
|
||||
|
||||
SurveyQuestionCubit(this._surveyQuestionRepository) : super(SurveyQuestionInitial());
|
||||
|
||||
Future<void> getSurveyQuestion({required String langId}) async {
|
||||
try {
|
||||
emit(SurveyQuestionFetchInProgress());
|
||||
await _surveyQuestionRepository.getSurveyQuestion(langId: langId).then((value) {
|
||||
emit(SurveyQuestionFetchSuccess(surveyQuestion: value['SurveyQuestion']));
|
||||
});
|
||||
} catch (e) {
|
||||
emit(SurveyQuestionFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void removeQuestion(String index) {
|
||||
if (state is SurveyQuestionFetchSuccess) {
|
||||
List<NewsModel> queList = List.from((state as SurveyQuestionFetchSuccess).surveyQuestion)..removeWhere((element) => element.id! == index);
|
||||
emit(SurveyQuestionFetchSuccess(surveyQuestion: queList));
|
||||
}
|
||||
}
|
||||
|
||||
List surveyList() {
|
||||
return (state is SurveyQuestionFetchSuccess) ? (state as SurveyQuestionFetchSuccess).surveyQuestion : [];
|
||||
}
|
||||
|
||||
String getSurveyQuestionIndex({required String questionTitle}) {
|
||||
return (state is! SurveyQuestionFetchSuccess) ? "0" : (state as SurveyQuestionFetchSuccess).surveyQuestion.where((element) => element.question == questionTitle).first.id!;
|
||||
}
|
||||
}
|
||||
92
news-app/lib/cubits/tagCubit.dart
Normal file
92
news-app/lib/cubits/tagCubit.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/TagModel.dart';
|
||||
import 'package:news/data/repositories/Tag/tagRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class TagState {}
|
||||
|
||||
class TagInitial extends TagState {}
|
||||
|
||||
class TagFetchInProgress extends TagState {}
|
||||
|
||||
class TagFetchSuccess extends TagState {
|
||||
final List<TagModel> tag;
|
||||
final int total;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
TagFetchSuccess(
|
||||
{required this.tag,
|
||||
required this.total,
|
||||
required this.hasMoreFetchError,
|
||||
required this.hasMore});
|
||||
}
|
||||
|
||||
class TagFetchFailure extends TagState {
|
||||
final String errorMessage;
|
||||
|
||||
TagFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class TagCubit extends Cubit<TagState> {
|
||||
final TagRepository _tagRepository;
|
||||
|
||||
TagCubit(this._tagRepository) : super(TagInitial());
|
||||
|
||||
void getTags({required String langId}) async {
|
||||
try {
|
||||
emit(TagFetchInProgress());
|
||||
final result = await _tagRepository.getTag(
|
||||
langId: langId, limit: limitOfAPIData.toString(), offset: "0");
|
||||
|
||||
emit(TagFetchSuccess(
|
||||
tag: result['Tag'],
|
||||
total: result[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: ((result['Tag'] as List<TagModel>).length < result[TOTAL])));
|
||||
} catch (e) {
|
||||
emit(TagFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Load tags if not already loaded or failed
|
||||
void loadIfFailed({required String langId}) {
|
||||
if (state is TagInitial || state is TagFetchFailure) {
|
||||
getTags(langId: langId);
|
||||
}
|
||||
}
|
||||
|
||||
void getMoreTags({required String langId}) async {
|
||||
if (state is TagFetchSuccess) {
|
||||
try {
|
||||
await _tagRepository
|
||||
.getTag(
|
||||
langId: langId,
|
||||
limit: limitOfAPIData.toString(),
|
||||
offset: (state as TagFetchSuccess).tag.length.toString())
|
||||
.then((value) {
|
||||
List<TagModel> updatedResults = (state as TagFetchSuccess).tag;
|
||||
updatedResults.addAll(value['Tag']);
|
||||
emit(TagFetchSuccess(
|
||||
tag: updatedResults,
|
||||
total: value[TOTAL],
|
||||
hasMoreFetchError: false,
|
||||
hasMore: (updatedResults.length < value[TOTAL])));
|
||||
});
|
||||
} catch (e) {
|
||||
emit(TagFetchSuccess(
|
||||
tag: (state as TagFetchSuccess).tag,
|
||||
total: (state as TagFetchSuccess).total,
|
||||
hasMoreFetchError: true,
|
||||
hasMore: (state as TagFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreTags() {
|
||||
return (state is TagFetchSuccess)
|
||||
? (state as TagFetchSuccess).hasMore
|
||||
: false;
|
||||
}
|
||||
}
|
||||
37
news-app/lib/cubits/tagNewsCubit.dart
Normal file
37
news-app/lib/cubits/tagNewsCubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/TagNews/tagNewsRepository.dart';
|
||||
|
||||
abstract class TagNewsState {}
|
||||
|
||||
class TagNewsInitial extends TagNewsState {}
|
||||
|
||||
class TagNewsFetchInProgress extends TagNewsState {}
|
||||
|
||||
class TagNewsFetchSuccess extends TagNewsState {
|
||||
final List<NewsModel> tagNews;
|
||||
|
||||
TagNewsFetchSuccess({required this.tagNews});
|
||||
}
|
||||
|
||||
class TagNewsFetchFailure extends TagNewsState {
|
||||
final String errorMessage;
|
||||
|
||||
TagNewsFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class TagNewsCubit extends Cubit<TagNewsState> {
|
||||
final TagNewsRepository _tagNewsRepository;
|
||||
|
||||
TagNewsCubit(this._tagNewsRepository) : super(TagNewsInitial());
|
||||
|
||||
void getTagNews({required String tagId, required String langId, String? latitude, String? longitude}) async {
|
||||
try {
|
||||
emit(TagNewsFetchInProgress());
|
||||
final result = await _tagNewsRepository.getTagNews(langId: langId, tagId: tagId, latitude: latitude, longitude: longitude);
|
||||
emit(TagNewsFetchSuccess(tagNews: result['TagNews']));
|
||||
} catch (e) {
|
||||
emit(TagNewsFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
21
news-app/lib/cubits/themeCubit.dart
Normal file
21
news-app/lib/cubits/themeCubit.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/repositories/Settings/settingsLocalDataRepository.dart';
|
||||
|
||||
import 'package:news/ui/styles/appTheme.dart';
|
||||
|
||||
import 'package:news/utils/uiUtils.dart';
|
||||
|
||||
class ThemeState {
|
||||
final AppTheme appTheme;
|
||||
ThemeState(this.appTheme);
|
||||
}
|
||||
|
||||
class ThemeCubit extends Cubit<ThemeState> {
|
||||
SettingsLocalDataRepository settingsRepository;
|
||||
ThemeCubit(this.settingsRepository) : super(ThemeState(UiUtils.getAppThemeFromLabel(settingsRepository.getCurrentTheme())));
|
||||
|
||||
void changeTheme(AppTheme appTheme) {
|
||||
settingsRepository.setCurrentTheme(UiUtils.getThemeLabelFromAppTheme(appTheme));
|
||||
emit(ThemeState(appTheme));
|
||||
}
|
||||
}
|
||||
63
news-app/lib/cubits/updateBottomsheetContentCubit.dart
Normal file
63
news-app/lib/cubits/updateBottomsheetContentCubit.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/CategoryModel.dart';
|
||||
import 'package:news/data/models/TagModel.dart';
|
||||
import 'package:news/data/models/appLanguageModel.dart';
|
||||
import 'package:news/data/models/locationCityModel.dart';
|
||||
|
||||
abstract class BottomSheetEvent {}
|
||||
|
||||
class UpdateBottomSheetContent extends BottomSheetEvent {
|
||||
final List<LocationCityModel> newData;
|
||||
final List<TagModel> newTagsData;
|
||||
final List<TagModel> newLanguagesData;
|
||||
|
||||
UpdateBottomSheetContent(this.newData, this.newTagsData, this.newLanguagesData);
|
||||
}
|
||||
|
||||
// Define the state for your bottom sheet Cubit
|
||||
class BottomSheetState {
|
||||
final List<LocationCityModel> locationData;
|
||||
final List<TagModel> tagsData;
|
||||
final List<LanguageModel> languageData;
|
||||
final List<CategoryModel> categoryData;
|
||||
|
||||
BottomSheetState(this.locationData, this.tagsData, this.languageData, this.categoryData);
|
||||
}
|
||||
|
||||
// Define the Cubit itself
|
||||
class BottomSheetCubit extends Cubit<BottomSheetState> {
|
||||
BottomSheetCubit() : super(BottomSheetState([], [], [], []));
|
||||
// Access the data field within the cubit
|
||||
|
||||
List<LocationCityModel> currentLocationData = [];
|
||||
List<TagModel> currentTagData = [];
|
||||
List<LanguageModel> currentLanguageData = [];
|
||||
List<CategoryModel> currentCategoryData = [];
|
||||
|
||||
getAllLatestContent({required bool isTag, required bool isLocation, required bool isLanguage, required bool isCategory}) {
|
||||
if (!isLocation) currentLocationData = state.locationData;
|
||||
if (!isTag) currentTagData = state.tagsData;
|
||||
if (!isLanguage) currentLanguageData = state.languageData;
|
||||
if (!isCategory) currentCategoryData = state.categoryData;
|
||||
}
|
||||
|
||||
void updateLocationContent(List<LocationCityModel> newData) {
|
||||
getAllLatestContent(isTag: false, isLocation: true, isLanguage: false, isCategory: false);
|
||||
emit(BottomSheetState(newData, currentTagData, currentLanguageData, currentCategoryData));
|
||||
}
|
||||
|
||||
void updateTagsContent(List<TagModel> newTagsData) {
|
||||
getAllLatestContent(isTag: true, isLocation: false, isLanguage: false, isCategory: false);
|
||||
emit(BottomSheetState(currentLocationData, newTagsData, currentLanguageData, currentCategoryData));
|
||||
}
|
||||
|
||||
void updateLanguageContent(List<LanguageModel> newLanguagesData) {
|
||||
getAllLatestContent(isTag: false, isLocation: false, isLanguage: true, isCategory: false);
|
||||
emit(BottomSheetState(currentLocationData, currentTagData, newLanguagesData, currentCategoryData));
|
||||
}
|
||||
|
||||
void updateCategoryContent(List<CategoryModel> newCategoryData) {
|
||||
getAllLatestContent(isTag: false, isLocation: false, isLanguage: false, isCategory: true);
|
||||
emit(BottomSheetState(currentLocationData, currentTagData, currentLanguageData, newCategoryData));
|
||||
}
|
||||
}
|
||||
63
news-app/lib/cubits/videosCubit.dart
Normal file
63
news-app/lib/cubits/videosCubit.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/NewsModel.dart';
|
||||
import 'package:news/data/repositories/Videos/videosRepository.dart';
|
||||
import 'package:news/utils/constant.dart';
|
||||
import 'package:news/utils/strings.dart';
|
||||
|
||||
abstract class VideoState {}
|
||||
|
||||
class VideoInitial extends VideoState {}
|
||||
|
||||
class VideoFetchInProgress extends VideoState {}
|
||||
|
||||
class VideoFetchSuccess extends VideoState {
|
||||
final List<NewsModel> video;
|
||||
final int totalVideoCount;
|
||||
final bool hasMoreFetchError;
|
||||
final bool hasMore;
|
||||
|
||||
VideoFetchSuccess({required this.video, required this.totalVideoCount, required this.hasMoreFetchError, required this.hasMore});
|
||||
}
|
||||
|
||||
class VideoFetchFailure extends VideoState {
|
||||
final String errorMessage;
|
||||
|
||||
VideoFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class VideoCubit extends Cubit<VideoState> {
|
||||
final VideoRepository _videoRepository;
|
||||
|
||||
VideoCubit(this._videoRepository) : super(VideoInitial());
|
||||
|
||||
void getVideo({required String langId, String? latitude, String? longitude}) async {
|
||||
try {
|
||||
emit(VideoFetchInProgress());
|
||||
final result = await _videoRepository.getVideo(limit: limitOfAPIData.toString(), offset: "0", langId: langId, latitude: latitude, longitude: longitude);
|
||||
(!result[ERROR])
|
||||
? emit(VideoFetchSuccess(video: result['Video'], totalVideoCount: result[TOTAL], hasMoreFetchError: false, hasMore: (result['Video'] as List<NewsModel>).length < result[TOTAL]))
|
||||
: emit(VideoFetchFailure(result[MESSAGE]));
|
||||
} catch (e) {
|
||||
emit(VideoFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMoreVideo() {
|
||||
return (state is VideoFetchSuccess) ? (state as VideoFetchSuccess).hasMore : false;
|
||||
}
|
||||
|
||||
void getMoreVideo({required String langId, String? latitude, String? longitude}) async {
|
||||
if (state is VideoFetchSuccess) {
|
||||
try {
|
||||
final result =
|
||||
await _videoRepository.getVideo(langId: langId, limit: limitOfAPIData.toString(), offset: (state as VideoFetchSuccess).video.length.toString(), latitude: latitude, longitude: longitude);
|
||||
List<NewsModel> updatedResults = (state as VideoFetchSuccess).video;
|
||||
updatedResults.addAll(result['Video'] as List<NewsModel>);
|
||||
emit(VideoFetchSuccess(video: updatedResults, totalVideoCount: result[TOTAL], hasMoreFetchError: false, hasMore: updatedResults.length < result[TOTAL]));
|
||||
} catch (e) {
|
||||
emit(VideoFetchSuccess(
|
||||
video: (state as VideoFetchSuccess).video, hasMoreFetchError: true, totalVideoCount: (state as VideoFetchSuccess).totalVideoCount, hasMore: (state as VideoFetchSuccess).hasMore));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
41
news-app/lib/cubits/weatherCubit.dart
Normal file
41
news-app/lib/cubits/weatherCubit.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:news/data/models/WeatherData.dart';
|
||||
import 'package:news/utils/ErrorMessageKeys.dart';
|
||||
|
||||
abstract class WeatherState {}
|
||||
|
||||
class WeatherInitial extends WeatherState {}
|
||||
|
||||
class WeatherFetchInProgress extends WeatherState {}
|
||||
|
||||
class WeatherFetchSuccess extends WeatherState {
|
||||
final WeatherDetails weatherData;
|
||||
|
||||
WeatherFetchSuccess({required this.weatherData});
|
||||
}
|
||||
|
||||
class WeatherFetchFailure extends WeatherState {
|
||||
final String errorMessage;
|
||||
|
||||
WeatherFetchFailure(this.errorMessage);
|
||||
}
|
||||
|
||||
class WeatherCubit extends Cubit<WeatherState> {
|
||||
WeatherCubit() : super(WeatherInitial());
|
||||
|
||||
void getWeatherDetails({required String langId, String? lat, String? lon}) async {
|
||||
try {
|
||||
emit(WeatherFetchInProgress());
|
||||
|
||||
final weatherResponse = await Dio().get('https://api.weatherapi.com/v1/forecast.json?key=d0f2f4dbecc043e78d6123135212408&q=${lat.toString()},${lon.toString()}&days=1&alerts=no&lang=$langId');
|
||||
if (weatherResponse.statusCode == 200) {
|
||||
emit(WeatherFetchSuccess(weatherData: WeatherDetails.fromJson(Map.from(weatherResponse.data))));
|
||||
} else {
|
||||
emit(WeatherFetchFailure(weatherResponse.statusMessage ?? ErrorMessageKeys.defaultErrorMessage));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(WeatherFetchFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user