import 'dart:async';
import 'package:app_settings/app_settings.dart';
import 'package:comwell_key_app/base/base_cubit.dart';
import 'package:comwell_key_app/domain/models/app_error.dart';
import 'package:comwell_key_app/domain/repositories/bluetooth_repository.dart';
import 'package:comwell_key_app/utils/secure_storage.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part "../../../../.generated/presentation/screens/onboarding/bluetooth/bluetooth_permission_cubit.freezed.dart";
class BluetoothPermissionCubit extends BaseCubit<BluetoothPermissionState> {
BluetoothPermissionCubit(this._bluetoothRepository, this._comwellPreferences, {required this.isOnboarding})
: super(const BluetoothPermissionState()) {
init();
}
final bool isOnboarding;
final ComwellPreferences _comwellPreferences;
final BluetoothRepository _bluetoothRepository;
int _clickCount = 0;
void init() async {
await _comwellPreferences.setOnboardingHasSeenBluetooth();
}
Future<void> onEnabledPressed() async {
await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
}
Future<bool> onRequestPermissionClicked() async {
_clickCount++;
final shouldRedirectToSettings = _clickCount >= 3;
if (shouldRedirectToSettings) {
await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
} else {
await _bluetoothRepository.request();
}
final isGranted = await _bluetoothRepository.isPermissionGranted();
return isGranted;
}
}
@freezed
abstract class BluetoothPermissionState with _$BluetoothPermissionState {
const factory BluetoothPermissionState({
@Default(false) bool isLoading,
@Default(AppError.none) AppError error,
}) = _BluetoothPermissionState;
}