Skip to content

Commit

Permalink
refactor: update bugsee manager state variables
Browse files Browse the repository at this point in the history
  • Loading branch information
koukibadr committed Nov 18, 2024
1 parent a2a88db commit 7e3e5f1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
64 changes: 39 additions & 25 deletions src/app/lib/business/bugsee/bugsee_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ abstract interface class BugseeManager {
required BugseeRepository bugseeRepository,
}) = _BugseeManager;

bool get isRequireRestart;

bool get bugseeIsEnabled;
bool get captureVideoIsEnabled;
bool get isValidConfiguration;
/// indicate if the app require a restart to reactivate the bugsee configurations
///
/// `true` only if `isConfigurationValid == true` and bugsee is turned on
bool get isRestartRequired;

/// indicate if bugsee is enabled or not
/// by default bugsee is enabled if `isConfigurationValid == true`.
bool get isBugseeEnabled;

/// indicate whether video capturing is enabled or not.
/// enabled by default if `isBugseeEnabled == true`.
///
/// cannot be true if `isBugseeEnabled == false`.
bool get isVideoCaptureEnabled;

/// indicate if bugsee configuration is valid
/// config is valid if app in release mode and the provided token is valid
/// following the [bugseeTokenFormat] regex.
bool get isConfigurationValid;

/// initialize bugsee with given token
/// bugsee is not available in debug mode
Expand Down Expand Up @@ -66,16 +80,16 @@ final class _BugseeManager implements BugseeManager {
});

@override
bool isRequireRestart = false;
bool isRestartRequired = false;

@override
bool bugseeIsEnabled = false;
bool isBugseeEnabled = false;

@override
late bool captureVideoIsEnabled = false;
late bool isVideoCaptureEnabled = false;

@override
bool isValidConfiguration = true;
bool isConfigurationValid = true;

late bool _isBugSeeInitialized;
BugseeLaunchOptions? launchOptions;
Expand All @@ -91,14 +105,14 @@ final class _BugseeManager implements BugseeManager {
_isBugSeeInitialized = false;

if (kDebugMode) {
isValidConfiguration = false;
isConfigurationValid = false;
logger.i("BUGSEE: deactivated in debug mode");
return;
}

if (bugseeToken == null ||
!RegExp(bugseeTokenFormat).hasMatch(bugseeToken)) {
isValidConfiguration = false;
isConfigurationValid = false;
logger.i(
"BUGSEE: token is null or invalid, bugsee won't be initialized",
);
Expand All @@ -109,8 +123,8 @@ final class _BugseeManager implements BugseeManager {
await launchBugseeLogger(bugseeToken);
}

bugseeIsEnabled = _isBugSeeInitialized;
captureVideoIsEnabled = _isBugSeeInitialized &&
isBugseeEnabled = _isBugSeeInitialized;
isVideoCaptureEnabled = _isBugSeeInitialized &&
(bugseeConfigurationData.isVideoCaptureEnabled ?? true);
}

Expand Down Expand Up @@ -144,7 +158,7 @@ final class _BugseeManager implements BugseeManager {
required Exception exception,
StackTrace? stackTrace,
}) async {
if (bugseeIsEnabled) {
if (isBugseeEnabled) {
await Bugsee.logException(exception, stackTrace);
}
}
Expand All @@ -154,30 +168,30 @@ final class _BugseeManager implements BugseeManager {
required Exception exception,
StackTrace? stackTrace,
}) async {
if (bugseeIsEnabled) {
if (isBugseeEnabled) {
await Bugsee.logUnhandledException(exception);
}
}

@override
Future<void> setIsBugseeEnabled(bool isBugseeEnabled) async {
if (isValidConfiguration) {
Future<void> setIsBugseeEnabled(bool value) async {
if (isConfigurationValid) {
isBugseeEnabled = value;
await bugseeRepository.setIsBugseeEnabled(isBugseeEnabled);

isRequireRestart = _isBugSeeInitialized && isBugseeEnabled;
bugseeIsEnabled = isBugseeEnabled;
captureVideoIsEnabled = bugseeIsEnabled;
isRestartRequired = _isBugSeeInitialized && isBugseeEnabled;
isVideoCaptureEnabled = isBugseeEnabled;

if (!isRequireRestart) {
if (!isRestartRequired) {
await Bugsee.stop();
}
}
}

@override
Future<void> setIsVideoCaptureEnabled(bool isVideoCaptureEnabled) async {
if (bugseeIsEnabled) {
captureVideoIsEnabled = isVideoCaptureEnabled;
Future<void> setIsVideoCaptureEnabled(bool value) async {
if (isBugseeEnabled) {
isVideoCaptureEnabled = value;
await bugseeRepository.setIsVideoCaptureEnabled(isVideoCaptureEnabled);
if (!isVideoCaptureEnabled) {
await Bugsee.pause();
Expand All @@ -189,7 +203,7 @@ final class _BugseeManager implements BugseeManager {

@override
Future<void> showCaptureLogReport() async {
if (bugseeIsEnabled) {
if (isBugseeEnabled) {
await Bugsee.showReportDialog();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class _BugseeConfigurationWidgetState extends State<BugseeConfigurationWidget> {
@override
void initState() {
super.initState();
isConfigEnabled = bugseeManager.bugseeIsEnabled;
isCaptureVideoEnabled = bugseeManager.captureVideoIsEnabled;
requireRestart = bugseeManager.isRequireRestart;
isConfigEnabled = bugseeManager.isBugseeEnabled;
isCaptureVideoEnabled = bugseeManager.isVideoCaptureEnabled;
requireRestart = bugseeManager.isRestartRequired;
}

@override
Expand All @@ -34,7 +34,7 @@ class _BugseeConfigurationWidgetState extends State<BugseeConfigurationWidget> {
children: [
Column(
children: [
if (!bugseeManager.isValidConfiguration)
if (!bugseeManager.isConfigurationValid)
Container(
color: const Color.fromARGB(170, 255, 0, 0),
child: const Text(
Expand Down Expand Up @@ -68,9 +68,9 @@ class _BugseeConfigurationWidgetState extends State<BugseeConfigurationWidget> {
onChanged: (value) async {
await bugseeManager.setIsBugseeEnabled(value);
setState(() {
isConfigEnabled = bugseeManager.bugseeIsEnabled;
isCaptureVideoEnabled = bugseeManager.captureVideoIsEnabled;
requireRestart = bugseeManager.isRequireRestart;
isConfigEnabled = bugseeManager.isBugseeEnabled;
isCaptureVideoEnabled = bugseeManager.isVideoCaptureEnabled;
requireRestart = bugseeManager.isRestartRequired;
});
},
),
Expand All @@ -80,7 +80,7 @@ class _BugseeConfigurationWidgetState extends State<BugseeConfigurationWidget> {
onChanged: (value) async {
await bugseeManager.setIsVideoCaptureEnabled(value);
setState(() {
isCaptureVideoEnabled = bugseeManager.captureVideoIsEnabled;
isCaptureVideoEnabled = bugseeManager.isVideoCaptureEnabled;
});
},
),
Expand Down

0 comments on commit 7e3e5f1

Please sign in to comment.