import 'dart:io'; import 'package:bloc/bloc.dart'; import 'package:tanami_app/core/utils/secure/secure_storage_service.dart'; import '../../../../Api_Helper/base_manager.dart'; import '../../../../features/MainScreens/Settings/domain/repository/settings_api.dart'; import '../../../../features/register/Repository/RegisterApi.dart'; import 'toggle_event.dart'; import 'toggle_state.dart'; class ToggleBloc extends Bloc { final String type; final SharedPreferenceLocalData secureStorageService = SharedPreferenceLocalData(); ToggleBloc(this.type) : super(ToggleInitial()) { on(_onToggleSwitch); _initializeToggleState(); } void _onToggleSwitch(ToggleSwitch event, Emitter emit) async { if (state is ToggleOn) { if (type == "biometric") { emit(ToggleOff()); Map biometricdata = { "code": await secureStorageService.read("temp_token"), "is_2FA_on": false, "deviceId": "test-id", "biometric_type": Platform.isIOS ? "face" : "fingerprint" }; ResponseData response = await RegisterAPIService().BiometricUpdate(biometricdata); if (response.status == ResponseStatus.SUCCESS) { await secureStorageService.write(type, 'off'); await secureStorageService.write("biometric", 'of'); } else { emit(ToggleOn()); await secureStorageService.write(type, 'on'); } } else if (type == "notification") { emit(ToggleOff()); ResponseData response = await SettingsApi().updateNotification(); if (response.status == ResponseStatus.SUCCESS) { await secureStorageService.write(type, 'off'); await secureStorageService.write("notification", 'of'); } else { emit(ToggleOn()); await secureStorageService.write(type, 'on'); } } else { emit(ToggleOff()); await secureStorageService.write(type, 'off'); } } else { if (type == "biometric") { emit(ToggleOn()); Map biometricdata = { "code": await secureStorageService.read("temp_token"), "is_2FA_on": true, "deviceId": "test-id", "biometric_type": Platform.isIOS ? "face" : "fingerprint" }; ResponseData response = await RegisterAPIService().BiometricUpdate(biometricdata); if (response.status == ResponseStatus.SUCCESS) { await secureStorageService.write("biometric", 'on'); await secureStorageService.write(type, 'on'); } else { emit(ToggleOff()); await secureStorageService.write(type, 'off'); } } else if (type == "notification") { emit(ToggleOn()); ResponseData response = await SettingsApi().updateNotification(); if (response.status == ResponseStatus.SUCCESS) { await secureStorageService.write(type, 'on'); await secureStorageService.write("notification", 'on'); } else { emit(ToggleOff()); await secureStorageService.write(type, 'off'); } } else { emit(ToggleOn()); await secureStorageService.write(type, 'on'); } } } void _initializeToggleState() async { final toggleValue = await secureStorageService.read(type); if (toggleValue == 'on') { add(ToggleSwitch()); // Initializing the state by adding an event } } }