import 'package:flutter/material.dart'; import 'package:hydrated_bloc/hydrated_bloc.dart'; import '../../settings.dart'; import '../../utils/api.dart'; import '../../utils/constant.dart'; import '../helper/custom_exception.dart'; abstract class ProfileSettingState {} //String? profileSettingData = ''; class ProfileSettingInitial extends ProfileSettingState {} class ProfileSettingFetchProgress extends ProfileSettingState {} class ProfileSettingFetchSuccess extends ProfileSettingState { String data; ProfileSettingFetchSuccess({required this.data}); Map toMap() { return { 'data': data, }; } factory ProfileSettingFetchSuccess.fromMap(Map map) { return ProfileSettingFetchSuccess( data: map['data'] as String, ); } } class ProfileSettingFetchFailure extends ProfileSettingState { final String errmsg; ProfileSettingFetchFailure(this.errmsg); } class ProfileSettingCubit extends Cubit with HydratedMixin { ProfileSettingCubit() : super(ProfileSettingInitial()); void fetchProfileSetting(BuildContext context, String title, {bool? forceRefresh}) async { if (forceRefresh != true) { if (state is ProfileSettingFetchSuccess) { await Future.delayed( const Duration(seconds: AppSettings.hiddenAPIProcessDelay)); } else { emit(ProfileSettingFetchProgress()); } } else { emit(ProfileSettingFetchProgress()); } if (forceRefresh == true) { fetchProfileSettingFromDb(context, title).then((value) { emit(ProfileSettingFetchSuccess(data: value ?? "")); }).catchError((e, stack) { emit(ProfileSettingFetchFailure(stack.toString())); }); } else { if (state is! ProfileSettingFetchSuccess) { fetchProfileSettingFromDb(context, title).then((value) { emit(ProfileSettingFetchSuccess(data: value ?? "")); }).catchError((e, stack) { emit(ProfileSettingFetchFailure(stack.toString())); }); } else { emit( ProfileSettingFetchSuccess( data: (state as ProfileSettingFetchSuccess).data), ); } } } Future fetchProfileSettingFromDb( BuildContext context, String title) async { try { String? profileSettingData; Map body = { Api.type: title, }; var response = await Api.post( url: Api.apiGetSystemSettings, parameter: body, useAuthToken: false); if (!response[Api.error]) { if (title == Api.currencySymbol) { // Constant.currencySymbol = getdata['data'].toString(); } else if (title == Api.maintenanceMode) { Constant.maintenanceMode = response['data'].toString(); } else { Map data = (response['data']); if (title == Api.termsAndConditions) { profileSettingData = data['terms_conditions']; // .where((element) => element['type'] == "terms_conditions") // .first['data']; } if (title == Api.privacyPolicy) { profileSettingData = data['privacy_policy']; // .where((element) => element['type'] == "privacy_policy") // .first['data']; } if (title == Api.aboutApp) { profileSettingData = data['about_us']; // .where((element) => element['type'] == "about_us") // .first['data']; } } } else { throw CustomException(response[Api.message]); } return profileSettingData; } catch (e) { rethrow; } } @override ProfileSettingState? fromJson(Map json) { try { if (json['cubit_state'] == "ProfileSettingFetchSuccess") { ProfileSettingFetchSuccess profileSettingFetchSuccess = ProfileSettingFetchSuccess.fromMap(json); return profileSettingFetchSuccess; } } catch (e) {} return null; } @override Map? toJson(ProfileSettingState state) { try { if (state is ProfileSettingFetchSuccess) { Map mapped = state.toMap(); mapped['cubit_state'] = "ProfileSettingFetchSuccess"; return mapped; } } catch (e) {} return null; } }