Files
Tanami_App/lib/shared/components/bloc/toggle/toggle_bloc.dart

104 lines
3.4 KiB
Dart
Raw Normal View History

2024-07-25 19:19:25 +05:30
import 'dart:io';
2024-06-06 11:05:45 +05:30
import 'package:bloc/bloc.dart';
2024-07-03 18:46:18 +05:30
import 'package:tanami_app/core/utils/secure/secure_storage_service.dart';
2024-06-06 11:05:45 +05:30
2024-07-25 19:19:25 +05:30
import '../../../../Api_Helper/base_manager.dart';
import '../../../../features/MainScreens/Settings/domain/repository/settings_api.dart';
import '../../../../features/register/Repository/RegisterApi.dart';
2024-06-06 11:05:45 +05:30
import 'toggle_event.dart';
import 'toggle_state.dart';
class ToggleBloc extends Bloc<ToggleEvent, ToggleState> {
2024-07-03 18:46:18 +05:30
final String type;
2024-07-26 16:54:11 +05:30
final SharedPreferenceLocalData secureStorageService =
SharedPreferenceLocalData();
2024-07-03 18:46:18 +05:30
ToggleBloc(this.type) : super(ToggleInitial()) {
2024-06-06 12:30:53 +05:30
on<ToggleSwitch>(_onToggleSwitch);
2024-07-03 18:46:18 +05:30
_initializeToggleState();
2024-06-06 12:30:53 +05:30
}
2024-06-06 11:05:45 +05:30
2024-07-03 18:46:18 +05:30
void _onToggleSwitch(ToggleSwitch event, Emitter<ToggleState> emit) async {
2024-06-06 12:30:53 +05:30
if (state is ToggleOn) {
2024-07-25 19:19:25 +05:30
if (type == "biometric") {
emit(ToggleOff());
Map<String, dynamic> biometricdata = {
"code": await secureStorageService.read("temp_token"),
2024-07-30 11:29:02 +05:30
"is_2FA_on": false,
2024-07-26 17:41:05 +05:30
"deviceId": "test-id",
2024-07-25 19:19:25 +05:30
"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');
}
2024-06-06 12:30:53 +05:30
} else {
2024-07-25 19:19:25 +05:30
if (type == "biometric") {
emit(ToggleOn());
Map<String, dynamic> biometricdata = {
"code": await secureStorageService.read("temp_token"),
"is_2FA_on": true,
2024-07-26 17:41:05 +05:30
"deviceId": "test-id",
2024-07-25 19:19:25 +05:30
"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');
}
2024-07-03 18:46:18 +05:30
}
}
void _initializeToggleState() async {
final toggleValue = await secureStorageService.read(type);
if (toggleValue == 'on') {
add(ToggleSwitch()); // Initializing the state by adding an event
2024-06-06 11:05:45 +05:30
}
}
}