Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions app/lib/utils/analytics/intercom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,39 @@ class IntercomManager {
static IntercomManager get instance => _instance;
static final SharedPreferencesUtil _preferences = SharedPreferencesUtil();

bool _initialized = false;

IntercomManager._internal();

Intercom get intercom => Intercom.instance;
bool get _isIntercomEnabled =>
PlatformService.isIntercomSupported && (Env.intercomAppId != null && Env.intercomAppId!.isNotEmpty);
_initialized &&
PlatformService.isIntercomSupported &&
(Env.intercomAppId != null && Env.intercomAppId!.isNotEmpty);
Comment on lines 19 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To improve readability and avoid duplicating logic, consider extracting the condition for whether Intercom is configured into its own private getter. This condition is used here and also in initIntercom.

  bool get _isIntercomConfigured =>
      PlatformService.isIntercomSupported &&
      (Env.intercomAppId != null && Env.intercomAppId!.isNotEmpty);

  bool get _isIntercomEnabled => _initialized && _isIntercomConfigured;

You would then also use _isIntercomConfigured inside initIntercom.


factory IntercomManager() {
return _instance;
}

Future<void> initIntercom() async {
if (Env.intercomAppId == null) return;
return PlatformService.executeIfSupportedAsync(
_isIntercomEnabled,
() => intercom.initialize(
if (Env.intercomAppId == null || Env.intercomAppId!.isEmpty) {
_initialized = true; // Mark as initialized (no-op) so methods degrade gracefully
return;
}
if (!PlatformService.isIntercomSupported) {
_initialized = true; // Mark as initialized on unsupported platforms
return;
}
try {
await intercom.initialize(
Env.intercomAppId!,
iosApiKey: Env.intercomIOSApiKey,
androidApiKey: Env.intercomAndroidApiKey,
),
);
);
_initialized = true;
} catch (e) {
// Initialization failed - _initialized stays false, all methods will no-op
}
}

Future displayChargingArticle(String device) async {
Expand Down