Skip to content

Commit

Permalink
chore: hook sample app with flutter source (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 authored Nov 6, 2024
1 parent 0ed2485 commit 9eb5f45
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-sample-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
run: |
cp ".env.example" ".env"
sd 'SITE_ID=.*' "SITE_ID=${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_SITE_ID', matrix.sample-app)] }}" ".env"
sd 'API_KEY=.*' "API_KEY=${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_API_KEY', matrix.sample-app)] }}" ".env"
sd 'CDP_API_KEY=.*' "CDP_API_KEY=${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_CDP_API_KEY', matrix.sample-app)] }}" ".env"
- name: Setup workspace credentials in iOS environment files
run: |
Expand Down
2 changes: 1 addition & 1 deletion apps/amiapp_flutter/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SITE_ID=siteid
API_KEY=apikey
CDP_API_KEY=cdpapikey
18 changes: 9 additions & 9 deletions apps/amiapp_flutter/lib/src/customer_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ class CustomerIOSDK extends ChangeNotifier {
}

final InAppConfig? inAppConfig;
if (_sdkConfig?.siteId != null) {
inAppConfig = InAppConfig(siteId: _sdkConfig!.siteId);
if (_sdkConfig?.migrationSiteId != null) {
inAppConfig = InAppConfig(siteId: _sdkConfig!.migrationSiteId ?? '');
} else {
inAppConfig = null;
}
return CustomerIO.initialize(
config: CustomerIOConfig(
cdpApiKey: '${_sdkConfig?.siteId}:${_sdkConfig?.apiKey}',
migrationSiteId: _sdkConfig?.siteId,
cdpApiKey: '${_sdkConfig?.cdnHost}:${_sdkConfig?.cdpApiKey}',
migrationSiteId: _sdkConfig?.migrationSiteId,
region: Region.us,
logLevel: logLevel,
autoTrackDeviceAttributes:
_sdkConfig?.deviceAttributesTrackingEnabled,
apiHost: _sdkConfig?.trackingUrl,
cdnHost: _sdkConfig?.trackingUrl,
flushAt: _sdkConfig?.backgroundQueueMinNumOfTasks,
flushInterval: _sdkConfig?.backgroundQueueSecondsDelay?.toInt(),
_sdkConfig?.autoTrackDeviceAttributes,
apiHost: _sdkConfig?.apiHost,
cdnHost: _sdkConfig?.cdnHost,
flushAt: _sdkConfig?.flushAt,
flushInterval: _sdkConfig?.flushInterval?.toInt(),
inAppConfig: inAppConfig,
),
);
Expand Down
161 changes: 95 additions & 66 deletions apps/amiapp_flutter/lib/src/data/config.dart
Original file line number Diff line number Diff line change
@@ -1,58 +1,87 @@
import 'package:customer_io/config/in_app_config.dart';
import 'package:customer_io/config/push_config.dart';
import 'package:customer_io/customer_io_enums.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CustomerIOSDKConfig {
String siteId;
String apiKey;
String? trackingUrl;
double? backgroundQueueSecondsDelay;
int? backgroundQueueMinNumOfTasks;
bool screenTrackingEnabled;
bool deviceAttributesTrackingEnabled;
bool debugModeEnabled;
final String cdpApiKey;
final String? migrationSiteId;
final Region? region;
final bool? debugModeEnabled;
final bool? screenTrackingEnabled;
final bool? autoTrackDeviceAttributes;
final String? apiHost;
final String? cdnHost;
final int? flushAt;
final int? flushInterval;
final InAppConfig? inAppConfig;
final PushConfig pushConfig;

CustomerIOSDKConfig({
required this.siteId,
required this.apiKey,
this.trackingUrl = "https://track-sdk.customer.io/",
this.backgroundQueueSecondsDelay = 30.0,
this.backgroundQueueMinNumOfTasks = 10,
this.screenTrackingEnabled = true,
this.deviceAttributesTrackingEnabled = true,
this.debugModeEnabled = true,
});
required this.cdpApiKey,
this.migrationSiteId,
this.region,
this.debugModeEnabled,
this.screenTrackingEnabled,
this.autoTrackDeviceAttributes,
this.apiHost,
this.cdnHost,
this.flushAt,
this.flushInterval,
this.inAppConfig,
PushConfig? pushConfig,
}) : pushConfig = pushConfig ?? PushConfig();

factory CustomerIOSDKConfig.fromEnv() => CustomerIOSDKConfig(
siteId: dotenv.env[_PreferencesKey.siteId]!,
apiKey: dotenv.env[_PreferencesKey.apiKey]!);
factory CustomerIOSDKConfig.fromEnv() =>
CustomerIOSDKConfig(
cdpApiKey: dotenv.env[_PreferencesKey.cdpApiKey]!,
migrationSiteId: dotenv.env[_PreferencesKey.migrationSiteId],
);

factory CustomerIOSDKConfig.fromPrefs(SharedPreferences prefs) {
final siteId = prefs.getString(_PreferencesKey.siteId);
final apiKey = prefs.getString(_PreferencesKey.apiKey);
final cdpApiKey = prefs.getString(_PreferencesKey.cdpApiKey);

if (siteId == null) {
throw ArgumentError('siteId cannot be null');
} else if (apiKey == null) {
throw ArgumentError('apiKey cannot be null');
if (cdpApiKey == null) {
throw ArgumentError('cdpApiKey cannot be null');
}

return CustomerIOSDKConfig(
siteId: siteId,
apiKey: apiKey,
trackingUrl: prefs.getString(_PreferencesKey.trackingUrl),
backgroundQueueSecondsDelay:
prefs.getDouble(_PreferencesKey.backgroundQueueSecondsDelay),
backgroundQueueMinNumOfTasks:
prefs.getInt(_PreferencesKey.backgroundQueueMinNumOfTasks),
screenTrackingEnabled:
prefs.getBool(_PreferencesKey.screenTrackingEnabled) != false,
deviceAttributesTrackingEnabled:
prefs.getBool(_PreferencesKey.deviceAttributesTrackingEnabled) !=
false,
debugModeEnabled:
prefs.getBool(_PreferencesKey.debugModeEnabled) != false,
cdpApiKey: cdpApiKey,
migrationSiteId: prefs.getString(_PreferencesKey.migrationSiteId),
region: prefs.getString(_PreferencesKey.region) != null
? Region.values.firstWhere(
(e) => e.name == prefs.getString(_PreferencesKey.region))
: null,
debugModeEnabled: prefs.getBool(_PreferencesKey.debugModeEnabled) !=
false,
screenTrackingEnabled: prefs.getBool(
_PreferencesKey.screenTrackingEnabled) != false,
autoTrackDeviceAttributes:
prefs.getBool(_PreferencesKey.autoTrackDeviceAttributes),
apiHost: prefs.getString(_PreferencesKey.apiHost),
cdnHost: prefs.getString(_PreferencesKey.cdnHost),
flushAt: prefs.getInt(_PreferencesKey.flushAt),
flushInterval: prefs.getInt(_PreferencesKey.flushInterval),
);
}

Map<String, dynamic> toMap() {
return {
'cdpApiKey': cdpApiKey,
'migrationSiteId': migrationSiteId,
'region': region?.name,
'logLevel': debugModeEnabled,
'screenTrackingEnabled': screenTrackingEnabled,
'autoTrackDeviceAttributes': autoTrackDeviceAttributes,
'apiHost': apiHost,
'cdnHost': cdnHost,
'flushAt': flushAt,
'flushInterval': flushInterval,
'inAppConfig': inAppConfig?.toMap(),
'pushConfig': pushConfig.toMap(),
};
}
}

extension ConfigurationPreferencesExtensions on SharedPreferences {
Expand All @@ -66,50 +95,50 @@ extension ConfigurationPreferencesExtensions on SharedPreferences {
return value != null ? setInt(key, value) : remove(key);
}

Future<bool> setOrRemoveDouble(String key, double? value) {
return value != null ? setDouble(key, value) : remove(key);
}

Future<bool> setOrRemoveBool(String key, bool? value) {
return value != null ? setBool(key, value) : remove(key);
}

Future<bool> saveSDKConfigState(CustomerIOSDKConfig config) async {
bool result = true;
result = result &&
await setOrRemoveString(_PreferencesKey.siteId, config.siteId);
result = result &&
await setOrRemoveString(_PreferencesKey.apiKey, config.apiKey);
await setOrRemoveString(_PreferencesKey.cdpApiKey, config.cdpApiKey);
result = result &&
await setOrRemoveString(
_PreferencesKey.trackingUrl, config.trackingUrl);
result = result &&
await setOrRemoveDouble(_PreferencesKey.backgroundQueueSecondsDelay,
config.backgroundQueueSecondsDelay);
_PreferencesKey.migrationSiteId, config.migrationSiteId);
result = result &&
await setOrRemoveInt(_PreferencesKey.backgroundQueueMinNumOfTasks,
config.backgroundQueueMinNumOfTasks);
await setOrRemoveString(_PreferencesKey.region, config.region?.name);
result = result &&
await setOrRemoveBool(_PreferencesKey.screenTrackingEnabled,
config.screenTrackingEnabled);
await setOrRemoveBool(
_PreferencesKey.debugModeEnabled, config.debugModeEnabled);
result = result &&
await setOrRemoveBool(_PreferencesKey.deviceAttributesTrackingEnabled,
config.deviceAttributesTrackingEnabled);
await setOrRemoveBool(_PreferencesKey.autoTrackDeviceAttributes,
config.autoTrackDeviceAttributes);
result = result &&
await setOrRemoveBool(
_PreferencesKey.debugModeEnabled, config.debugModeEnabled);
_PreferencesKey.screenTrackingEnabled, config.screenTrackingEnabled);
result = result &&
await setOrRemoveString(_PreferencesKey.apiHost, config.apiHost);
result = result &&
await setOrRemoveString(_PreferencesKey.cdnHost, config.cdnHost);
result =
result && await setOrRemoveInt(_PreferencesKey.flushAt, config.flushAt);
result = result &&
await setOrRemoveInt(
_PreferencesKey.flushInterval, config.flushInterval);
return result;
}
}

class _PreferencesKey {
static const siteId = 'SITE_ID';
static const apiKey = 'API_KEY';
static const trackingUrl = 'TRACKING_URL';
static const backgroundQueueSecondsDelay = 'BACKGROUND_QUEUE_SECONDS_DELAY';
static const backgroundQueueMinNumOfTasks =
'BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS';
static const screenTrackingEnabled = 'TRACK_SCREENS';
static const deviceAttributesTrackingEnabled = 'TRACK_DEVICE_ATTRIBUTES';
static const cdpApiKey = 'CDP_API_KEY';
static const migrationSiteId = 'SITE_ID';
static const region = 'REGION';
static const debugModeEnabled = 'DEBUG_MODE';
static const screenTrackingEnabled = 'SCREEN_TRACKING';
static const autoTrackDeviceAttributes = 'AUTO_TRACK_DEVICE_ATTRIBUTES';
static const apiHost = 'API_HOST';
static const cdnHost = 'CDN_HOST';
static const flushAt = 'FLUSH_AT';
static const flushInterval = 'FLUSH_INTERVAL';
}
60 changes: 30 additions & 30 deletions apps/amiapp_flutter/lib/src/screens/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ class _SettingsScreenState extends State<SettingsScreen> {

final cioConfig = widget._customerIOSDK.sdkConfig;
_deviceTokenValueController = TextEditingController();
_trackingURLValueController =
TextEditingController(text: cioConfig?.trackingUrl);
// _trackingURLValueController =
// TextEditingController(text: cioConfig?.trackingUrl);
_siteIDValueController = TextEditingController(
text: widget.siteIdInitialValue ?? cioConfig?.siteId);
text: widget.siteIdInitialValue ?? cioConfig?.migrationSiteId);
_apiKeyValueController = TextEditingController(
text: widget.apiKeyInitialValue ?? cioConfig?.apiKey);
_bqSecondsDelayValueController = TextEditingController(
text: cioConfig?.backgroundQueueSecondsDelay?.toTrimmedString());
_bqMinNumberOfTasksValueController = TextEditingController(
text: cioConfig?.backgroundQueueMinNumOfTasks?.toString());
_featureTrackScreens = cioConfig?.screenTrackingEnabled ?? true;
_featureTrackDeviceAttributes =
cioConfig?.deviceAttributesTrackingEnabled ?? true;
text: widget.apiKeyInitialValue ?? cioConfig?.cdpApiKey);
// _bqSecondsDelayValueController = TextEditingController(
// text: cioConfig?.backgroundQueueSecondsDelay?.toTrimmedString());
// _bqMinNumberOfTasksValueController = TextEditingController(
// text: cioConfig?.backgroundQueueMinNumOfTasks?.toString());
// _featureTrackScreens = cioConfig?.screenTrackingEnabled ?? true;
// _featureTrackDeviceAttributes =
// cioConfig?.deviceAttributesTrackingEnabled ?? true;
_featureDebugMode = cioConfig?.debugModeEnabled ?? true;

super.initState();
Expand All @@ -76,15 +76,15 @@ class _SettingsScreenState extends State<SettingsScreen> {
}

final newConfig = CustomerIOSDKConfig(
siteId: _siteIDValueController.text.trim(),
apiKey: _apiKeyValueController.text.trim(),
trackingUrl: _trackingURLValueController.text.trim(),
backgroundQueueSecondsDelay:
_bqSecondsDelayValueController.text.trim().toDoubleOrNull(),
backgroundQueueMinNumOfTasks:
_bqMinNumberOfTasksValueController.text.trim().toIntOrNull(),
migrationSiteId: _siteIDValueController.text.trim(),
cdpApiKey: _apiKeyValueController.text.trim(),
// trackingUrl: _trackingURLValueController.text.trim(),
// backgroundQueueSecondsDelay:
// _bqSecondsDelayValueController.text.trim().toDoubleOrNull(),
// backgroundQueueMinNumOfTasks:
// _bqMinNumberOfTasksValueController.text.trim().toIntOrNull(),
screenTrackingEnabled: _featureTrackScreens,
deviceAttributesTrackingEnabled: _featureTrackDeviceAttributes,
// deviceAttributesTrackingEnabled: _featureTrackDeviceAttributes,
debugModeEnabled: _featureDebugMode,
);
widget._customerIOSDK.saveConfigToPreferences(newConfig).then((success) {
Expand All @@ -109,17 +109,17 @@ class _SettingsScreenState extends State<SettingsScreen> {
}

setState(() {
_siteIDValueController.text = defaultConfig.siteId;
_apiKeyValueController.text = defaultConfig.apiKey;
_trackingURLValueController.text = defaultConfig.trackingUrl ?? '';
_bqSecondsDelayValueController.text =
defaultConfig.backgroundQueueSecondsDelay?.toTrimmedString() ?? '';
_bqMinNumberOfTasksValueController.text =
defaultConfig.backgroundQueueMinNumOfTasks?.toString() ?? '';
_featureTrackScreens = defaultConfig.screenTrackingEnabled;
_featureTrackDeviceAttributes =
defaultConfig.deviceAttributesTrackingEnabled;
_featureDebugMode = defaultConfig.debugModeEnabled;
_siteIDValueController.text = defaultConfig.migrationSiteId ?? '';
_apiKeyValueController.text = defaultConfig.cdpApiKey;
// _trackingURLValueController.text = defaultConfig.trackingUrl ?? '';
// _bqSecondsDelayValueController.text =
// defaultConfig.backgroundQueueSecondsDelay?.toTrimmedString() ?? '';
// _bqMinNumberOfTasksValueController.text =
// defaultConfig.backgroundQueueMinNumOfTasks?.toString() ?? '';
// _featureTrackScreens = defaultConfig.screenTrackingEnabled;
// _featureTrackDeviceAttributes =
// defaultConfig.deviceAttributesTrackingEnabled;
_featureDebugMode = defaultConfig.debugModeEnabled ?? true;
_saveSettings(context);
});
}
Expand Down

0 comments on commit 9eb5f45

Please sign in to comment.