Skip to content

Commit

Permalink
improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrutskikh committed Apr 20, 2021
1 parent eb5e809 commit af35dc7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 112 deletions.
235 changes: 142 additions & 93 deletions example/lib/title_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class _TitleScreenState extends State<TitleScreen> {
late StreamSubscription<bool> _onLowerPower;

var _deviceId = virtualDeviceToken;
var _isSdkEnabled = false;
var _isAllRequiredPermissionsGranted = false;
var _isTracking = false;

@override
void initState() {
Expand All @@ -37,6 +40,11 @@ class _TitleScreenState extends State<TitleScreen> {

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
_isSdkEnabled = await _trackingApi.isSdkEnabled() ?? false;
_isAllRequiredPermissionsGranted =
await _trackingApi.isAllRequiredPermissionsAndSensorsGranted() ?? false;
_isTracking = await _trackingApi.isTracking() ?? false;

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
Expand All @@ -53,100 +61,83 @@ class _TitleScreenState extends State<TitleScreen> {
appBar: AppBar(
title: const Text('TelematicsSDK_demo'),
),
body: Center(
child: ListView(
shrinkWrap: true,
padding: const EdgeInsets.symmetric(horizontal: 16),
children: [
TextFormField(
initialValue: _deviceId,
decoration: const InputDecoration(
hintText: 'virtual device token',
labelText: 'virtual device token',
body: Stack(children: [
ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(16),
children: [
Text('SDK status: ${_isSdkEnabled ? 'Enable' : 'Disable'}'),
Text(
'Permissions: ${_isAllRequiredPermissionsGranted ? 'Granted' : 'Not granted'}',
),
Text('Tracking: ${_isTracking ? 'Started' : 'Not stated'}'),
]),
Center(
child: ListView(
shrinkWrap: true,
padding: const EdgeInsets.symmetric(horizontal: 16),
children: [
TextFormField(
initialValue: _deviceId,
decoration: const InputDecoration(
hintText: 'virtual device token',
labelText: 'virtual device token',
),
keyboardType: TextInputType.text,
textInputAction: TextInputAction.done,
maxLengthEnforcement: MaxLengthEnforcement.none,
onChanged: (value) {
setState(() {
_deviceId = value;
});
},
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(FocusNode());
},
),
ElevatedButton(
onPressed: !_isSdkEnabled ? _onEnableSDK : null,
child: const Text('Enable SDK'),
),
ElevatedButton(
onPressed: _isSdkEnabled ? _onDisableSDK : null,
child: const Text('Disable SDK'),
),
_sizedBoxSpace,
ElevatedButton(
onPressed: () async {
if (!_isAllRequiredPermissionsGranted) {
_trackingApi.showPermissionWizard(
enableAggressivePermissionsWizard: false,
enableAggressivePermissionsWizardPage: true,
);
} else {
_showSnackBar('All permissions are already granted');
}
},
child: const Text('Start Permission Wizard'),
),
_sizedBoxSpace,
ElevatedButton(
onPressed: !_isTracking ? _onStartTracking : null,
child: const Text('Start tracking manually'),
),
ElevatedButton(
onPressed: _isTracking ? _onStopTracking : null,
child: const Text('Stop tracking manually'),
),
keyboardType: TextInputType.text,
textInputAction: TextInputAction.done,
maxLengthEnforcement: MaxLengthEnforcement.none,
onChanged: (value) {
setState(() {
_deviceId = value;
});
},
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(FocusNode());
},
),
ElevatedButton(
onPressed: () async {
if (_deviceId.isEmpty) {
_showSnackBar('virtual device token is empty');
} else if (!(await _trackingApi
.isAllRequiredPermissionsAndSensorsGranted() ??
false)) {
_showSnackBar('Please grant all required permissions');
} else {
await _trackingApi.setDeviceID(deviceId: _deviceId);
await _trackingApi.setEnableSdk(enable: true);
}
},
child: const Text('Enable SDK'),
),
ElevatedButton(
onPressed: () async {
await _trackingApi.setEnableSdk(enable: false);
},
child: const Text('Disable SDK'),
),
_sizedBoxSpace,
ElevatedButton(
onPressed: () async {
if (!(await _trackingApi
.isAllRequiredPermissionsAndSensorsGranted() ??
false)) {
_trackingApi.showPermissionWizard(
enableAggressivePermissionsWizard: false,
enableAggressivePermissionsWizardPage: true,
);
} else {
_showSnackBar('All permissions are already granted');
}
},
child: const Text('Start Permission Wizard'),
),
_sizedBoxSpace,
ElevatedButton(
onPressed: () async {
if (!(await _trackingApi.isSdkEnabled() ?? false)) {
_showSnackBar('Enable SDK first');
} else if (await _trackingApi.isTracking() ?? false) {
_showSnackBar('Stop current track first');
} else {
_trackingApi.startTracking();
}
},
child: const Text('Start tracking manually'),
),
ElevatedButton(
onPressed: () async {
if (await _trackingApi.isTracking() ?? false) {
_trackingApi.stopTracking();
} else {
_showSnackBar('Start tracking first');
}
},
child: const Text('Stop tracking manually'),
),
// _sizedBoxSpace,
// ElevatedButton(
// onPressed: () async {
// if (!(await _trackingApi.isSdkEnabled() ?? false)) {
// _showSnackBar('Enable SDK first');
// } else {}
// },
// child: const Text('Dashboard'),
// ),
],
)),
// _sizedBoxSpace,
// ElevatedButton(
// onPressed: () async {
// if (!(await _trackingApi.isSdkEnabled() ?? false)) {
// _showSnackBar('Enable SDK first');
// } else {}
// },
// child: const Text('Dashboard'),
// ),
],
)),
]),
);
}

Expand All @@ -158,16 +149,74 @@ class _TitleScreenState extends State<TitleScreen> {
super.dispose();
}

Future<void> _onEnableSDK() async {
if (_deviceId.isEmpty) {
_showSnackBar('virtual device token is empty');
} else if (!_isAllRequiredPermissionsGranted) {
_showSnackBar('Please grant all required permissions');
} else {
await _trackingApi.setDeviceID(deviceId: _deviceId);
await _trackingApi.setEnableSdk(enable: true);

setState(() {
_isSdkEnabled = true;
});
}
}

Future<void> _onDisableSDK() async {
await _trackingApi.setEnableSdk(enable: false);
await _trackingApi.clearDeviceID();

setState(() {
_isSdkEnabled = false;
});
}

void _onPermissionWizardResult(PermissionWizardResult result) {
const _wizardResultMapping = {
PermissionWizardResult.allGranted: 'All permissions was granted',
PermissionWizardResult.notAllGranted: 'All permissions was not granted',
PermissionWizardResult.canceled: 'Wizard cancelled',
};

if (result == PermissionWizardResult.allGranted ||
result == PermissionWizardResult.notAllGranted) {
setState(() {
_isAllRequiredPermissionsGranted =
result == PermissionWizardResult.allGranted;
});
}

_showSnackBar(_wizardResultMapping[result] ?? '');
}

Future<void> _onStartTracking() async {
if (!(await _trackingApi.isSdkEnabled() ?? false)) {
_showSnackBar('Enable SDK first');
} else if (await _trackingApi.isTracking() ?? false) {
_showSnackBar('Stop current track first');
} else {
_trackingApi.startTracking();
}

setState(() {
_isTracking = true;
});
}

Future<void> _onStopTracking() async {
if (await _trackingApi.isTracking() ?? false) {
_trackingApi.stopTracking();
} else {
_showSnackBar('Start tracking first');
}

setState(() {
_isTracking = false;
});
}

void _onLowPowerResult(bool isLowPower) {
if (isLowPower) {
_showSnackBar(
Expand Down
19 changes: 0 additions & 19 deletions test/telematics_sdk_test.dart

This file was deleted.

0 comments on commit af35dc7

Please sign in to comment.