fix(settings): Issue-256

This commit is contained in:
mso7nueh
2025-09-11 20:11:37 +03:00
parent b840c936bf
commit 74b5c6732d
4 changed files with 73 additions and 17 deletions

View File

@@ -137,7 +137,7 @@ Future<void> init() async {
sl.registerFactory<DietsCubit>(() => DietsCubit(feedRepository: sl(), dietsRepository: sl()));
sl.registerFactory<CommentsCubit>(() => CommentsCubit(feedRepository: sl(), commentsRepository: sl()));
sl.registerFactory<PlansCubit>(() => PlansCubit(feedRepository: sl(), planRepository: sl()));
sl.registerFactory<SettingsCubit>(() => SettingsCubit(profileRepository: sl(), settingsRepository: sl(), consultantRepository: sl()));
sl.registerFactory<SettingsCubit>(() => SettingsCubit(profileRepository: sl(), settingsRepository: sl(), consultantRepository: sl(), consultationsRepository: sl()));
sl.registerFactory<RegisterConsultantCubit>(() => RegisterConsultantCubit(consultantRepository: sl()));
sl.registerFactory<DiaryCubit>(
() => DiaryCubit(

View File

@@ -1,5 +1,7 @@
import 'package:eatvio_app/features/consultant/become_consultant/models/consultation_request_model.dart';
import 'package:eatvio_app/features/consultant/become_consultant/repositories/consultant_repository.dart';
import 'package:eatvio_app/features/consultations/repositories/consultations_repository.dart';
import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:eatvio_app/features/settings/models/profile_settings_model.dart';
@@ -11,16 +13,20 @@ class SettingsCubit extends Cubit<SettingsState> {
final ProfileRepository profileRepository;
final SettingsRepository settingsRepository;
final ConsultantRepository consultantRepository;
final ConsultationsRepository consultationsRepository;
SettingsCubit({
required this.profileRepository,
required this.settingsRepository,
required this.profileRepository,
required this.settingsRepository,
required this.consultantRepository,
required this.consultationsRepository,
}) : super(const SettingsState.loading());
Future<void> init() async {
final profile = await profileRepository.getMyProfile();
late final ConsultationRequestModel? consultantInfo;
late final bool hasActiveConsultation;
try {
if (profile.phoneConfirmationState.contains('confirmed')) {
consultantInfo = await consultantRepository.getMyRequestStatus(profile.slug);
@@ -30,6 +36,13 @@ class SettingsCubit extends Cubit<SettingsState> {
} catch (e) {
consultantInfo = null;
}
try {
hasActiveConsultation = await _checkActiveConsultation();
} catch (e) {
hasActiveConsultation = false;
}
try {
emit(
SettingsState.loaded(
@@ -37,6 +50,7 @@ class SettingsCubit extends Cubit<SettingsState> {
profileDetails: await profileRepository.getMyProfileDetails(),
profile: profile,
consultationRequestModel: consultantInfo,
hasActiveConsultation: hasActiveConsultation,
),
);
} catch (e) {
@@ -44,13 +58,9 @@ class SettingsCubit extends Cubit<SettingsState> {
}
}
Future<void> updateProfileDetails({
required ProfileDetailsModel profileDetailsModel,
}) async {
Future<void> updateProfileDetails({required ProfileDetailsModel profileDetailsModel}) async {
try {
await settingsRepository.updateProfileDetails(
profileDetailsModel: profileDetailsModel,
);
await settingsRepository.updateProfileDetails(profileDetailsModel: profileDetailsModel);
} catch (e) {
rethrow;
} finally {
@@ -58,13 +68,9 @@ class SettingsCubit extends Cubit<SettingsState> {
}
}
Future<void> updateProfileSettings({
required ProfileSettingsModel profileSettingsModel,
}) async {
Future<void> updateProfileSettings({required ProfileSettingsModel profileSettingsModel}) async {
try {
await settingsRepository.updateProfileSettings(
profileSettingsModel: profileSettingsModel,
);
await settingsRepository.updateProfileSettings(profileSettingsModel: profileSettingsModel);
} catch (e) {
rethrow;
} finally {
@@ -80,4 +86,17 @@ class SettingsCubit extends Cubit<SettingsState> {
return false;
}
}
Future<bool> _checkActiveConsultation() async {
try {
final userPurchases = await consultationsRepository.getUserPurchases(null, null);
final activeStates = ChosenPlanStateEnum.getActiveStates();
return userPurchases.any((purchase) =>
activeStates.contains(purchase.chosenPlanState)
);
} catch (e) {
return false;
}
}
}

View File

@@ -1,5 +1,6 @@
import 'package:eatvio_app/features/consultant/become_consultant/models/consultation_request_model.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/profile/models/profile_details_model.dart';
import 'package:eatvio_app/features/settings/models/profile_settings_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -16,6 +17,7 @@ abstract class SettingsState with _$SettingsState {
required ProfileDetailsModel profileDetails,
required ProfileModel profile,
ConsultationRequestModel? consultationRequestModel,
bool hasActiveConsultation,
}) = SettingsStateLoaded;
const factory SettingsState.error({

View File

@@ -32,6 +32,24 @@ class _SettingsScreenState extends State<SettingsScreen> {
late bool bedTimeSwitchState;
late bool moodSwitchState;
void _showConsultationWarning() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Предупреждение'),
content: const Text('Во время консультации этот параметр нельзя отключить'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Понятно'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return BlocProvider<SettingsCubit>(
@@ -158,16 +176,24 @@ class _SettingsScreenState extends State<SettingsScreen> {
_tile(
padding: const EdgeInsets.symmetric(horizontal: 16),
onTap: () {
if (state.hasActiveConsultation && !moodSwitchState) {
_showConsultationWarning();
return;
}
setState(() {
moodSwitchState = !moodSwitchState;
});
settingsCubit.updateProfileSettings(profileSettingsModel: state.profileSettings.copyWith(isMoodFillable: moodSwitchState));
},
title: 'Указывать “настроение” в заполнение дневника',
title: 'Указывать "настроение" в заполнение дневника',
trailing: CupertinoSwitch(
value: moodSwitchState,
activeTrackColor: appTheme.blueA400,
onChanged: (newValue) {
if (state.hasActiveConsultation && !newValue) {
_showConsultationWarning();
return;
}
setState(() {
moodSwitchState = newValue;
});
@@ -179,16 +205,25 @@ class _SettingsScreenState extends State<SettingsScreen> {
_tile(
padding: const EdgeInsets.symmetric(horizontal: 16),
onTap: () {
if (state.hasActiveConsultation && !bedTimeSwitchState) {
_showConsultationWarning();
return;
}
setState(() {
bedTimeSwitchState = !bedTimeSwitchState;
});
settingsCubit.updateProfileSettings(profileSettingsModel: state.profileSettings.copyWith(isBedtimeFillable: bedTimeSwitchState));
},
title: 'Указывать “отход ко сну” в заполнение дневника',
title: 'Указывать "отход ко сну" в заполнение дневника',
trailing: CupertinoSwitch(
value: bedTimeSwitchState,
activeTrackColor: appTheme.blueA400,
onChanged: (newValue) {
if (state.hasActiveConsultation && !newValue) {
_showConsultationWarning();
return;
}
setState(() {
bedTimeSwitchState = newValue;
});