diff --git a/example/lib/main.dart b/example/lib/main.dart index 1d1374b..1b96b95 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter_carplay/constants/constants.dart'; import 'package:flutter_carplay/flutter_carplay.dart'; void main() { @@ -454,7 +455,7 @@ class _MyAppState extends State { Center( child: Text( 'Carplay Status: ' + - CPEnumUtils.stringFromEnum(connectionStatus), + connectionStatus.stringValue(), ), ), Row( diff --git a/lib/carplay_worker.dart b/lib/carplay_worker.dart index aa570d3..001be23 100644 --- a/lib/carplay_worker.dart +++ b/lib/carplay_worker.dart @@ -1,11 +1,11 @@ import 'dart:async'; import 'package:flutter_carplay/constants/constants.dart'; import 'package:flutter_carplay/controllers/carplay_controller.dart'; -import 'package:flutter_carplay/flutter_carplay.dart'; -import 'package:flutter_carplay/helpers/enum_utils.dart'; +import 'package:flutter_carplay/models/action_sheet/action_sheet_template.dart'; import 'package:flutter_carplay/models/alert/alert_template.dart'; import 'package:flutter_carplay/models/grid/grid_template.dart'; import 'package:flutter_carplay/models/information/information_template.dart'; +import 'package:flutter_carplay/models/list/list_template.dart'; import 'package:flutter_carplay/models/poi/poi_template.dart'; import 'package:flutter_carplay/models/tabbar/tabbar_template.dart'; import 'package:flutter_carplay/constants/private_constants.dart'; @@ -24,15 +24,13 @@ import 'package:flutter_carplay/constants/private_constants.dart'; /// - [Learn more about MFi Program](https://mfi.apple.com) class FlutterCarplay { /// A main Flutter CarPlay Controller to manage the system. - static final FlutterCarPlayController _carPlayController = - FlutterCarPlayController(); + static final FlutterCarPlayController _carPlayController = FlutterCarPlayController(); /// CarPlay main bridge as a listener from CarPlay and native side. late final StreamSubscription? _eventBroadcast; /// Current CarPlay and mobile app connection status. - static String _connectionStatus = - CPEnumUtils.stringFromEnum(CPConnectionStatusTypes.unknown.toString()); + static String _connectionStatus = CPConnectionStatusTypes.unknown.stringValue(); /// A listener function, which will be triggered when CarPlay connection changes /// and will be transmitted to the main code, allowing the user to access @@ -41,49 +39,33 @@ class FlutterCarplay { /// Creates an [FlutterCarplay] and starts the connection. FlutterCarplay() { - _eventBroadcast = _carPlayController.eventChannel - .receiveBroadcastStream() - .listen((event) { - final FCPChannelTypes receivedChannelType = CPEnumUtils.enumFromString( - FCPChannelTypes.values, - event["type"], - ); + _eventBroadcast = _carPlayController.eventChannel.receiveBroadcastStream().listen((event) { + final FCPChannelTypes receivedChannelType = FCPChannelTypesUtil.parseValue(event["type"]); switch (receivedChannelType) { case FCPChannelTypes.onCarplayConnectionChange: - final CPConnectionStatusTypes connectionStatus = - CPEnumUtils.enumFromString( - CPConnectionStatusTypes.values, - event["data"]["status"], - ); - _connectionStatus = - CPEnumUtils.stringFromEnum(connectionStatus.toString()); + final CPConnectionStatusTypes connectionStatus = CPConnectionStatusTypesUtil.parseValue(event["data"]["status"]); + _connectionStatus = connectionStatus.stringValue(); if (_onCarplayConnectionChange != null) { _onCarplayConnectionChange!(connectionStatus); } break; case FCPChannelTypes.onFCPListItemSelected: - _carPlayController - .processFCPListItemSelectedChannel(event["data"]["elementId"]); + _carPlayController.processFCPListItemSelectedChannel(event["data"]["elementId"]); break; case FCPChannelTypes.onFCPAlertActionPressed: - _carPlayController - .processFCPAlertActionPressed(event["data"]["elementId"]); + _carPlayController.processFCPAlertActionPressed(event["data"]["elementId"]); break; case FCPChannelTypes.onPresentStateChanged: - _carPlayController - .processFCPAlertTemplateCompleted(event["data"]["completed"]); + _carPlayController.processFCPAlertTemplateCompleted(event["data"]["completed"]); break; case FCPChannelTypes.onGridButtonPressed: - _carPlayController - .processFCPGridButtonPressed(event["data"]["elementId"]); + _carPlayController.processFCPGridButtonPressed(event["data"]["elementId"]); break; case FCPChannelTypes.onBarButtonPressed: - _carPlayController - .processFCPBarButtonPressed(event["data"]["elementId"]); + _carPlayController.processFCPBarButtonPressed(event["data"]["elementId"]); break; case FCPChannelTypes.onTextButtonPressed: - _carPlayController - .processFCPTextButtonPressed(event["data"]["elementId"]); + _carPlayController.processFCPTextButtonPressed(event["data"]["elementId"]); break; default: break; @@ -152,8 +134,7 @@ class FlutterCarplay { rootTemplate.runtimeType == CPListTemplate || rootTemplate.runtimeType == CPInformationTemplate || rootTemplate.runtimeType == CPPointOfInterestTemplate) { - _carPlayController.methodChannel - .invokeMethod('setRootTemplate', { + _carPlayController.methodChannel.invokeMethod('setRootTemplate', { 'rootTemplate': rootTemplate.toJson(), 'animated': animated, 'runtimeType': "F" + rootTemplate.runtimeType.toString(), @@ -187,13 +168,11 @@ class FlutterCarplay { required CPAlertTemplate template, bool animated = true, }) { - _carPlayController.methodChannel.invokeMethod( - CPEnumUtils.stringFromEnum(FCPChannelTypes.setAlert.toString()), - { - 'rootTemplate': template.toJson(), - 'animated': animated, - 'onPresent': template.onPresent != null ? true : false, - }).then((value) { + _carPlayController.methodChannel.invokeMethod(FCPChannelTypes.setAlert.stringValue(), { + 'rootTemplate': template.toJson(), + 'animated': animated, + 'onPresent': template.onPresent != null ? true : false, + }).then((value) { if (value) { FlutterCarPlayController.currentPresentTemplate = template; } @@ -210,12 +189,10 @@ class FlutterCarplay { required CPActionSheetTemplate template, bool animated = true, }) { - _carPlayController.methodChannel.invokeMethod( - CPEnumUtils.stringFromEnum(FCPChannelTypes.setActionSheet.toString()), - { - 'rootTemplate': template.toJson(), - 'animated': animated, - }).then((value) { + _carPlayController.methodChannel.invokeMethod(FCPChannelTypes.setActionSheet.stringValue(), { + 'rootTemplate': template.toJson(), + 'animated': animated, + }).then((value) { if (value) { FlutterCarPlayController.currentPresentTemplate = template; } @@ -240,9 +217,7 @@ class FlutterCarplay { /// Removes all of the templates from the navigation hierarchy except the root template. /// If animated is true, CarPlay animates the presentation of the template. static Future popToRoot({bool animated = true}) async { - FlutterCarPlayController.templateHistory = [ - FlutterCarPlayController.currentRootTemplate - ]; + FlutterCarPlayController.templateHistory = [FlutterCarPlayController.currentRootTemplate]; return await _carPlayController.reactToNativeModule( FCPChannelTypes.popToRootTemplate, animated, @@ -272,10 +247,8 @@ class FlutterCarplay { if (template.runtimeType == CPGridTemplate || template.runtimeType == CPListTemplate || template.runtimeType == CPInformationTemplate || - template.runtimeType == CPPointOfInterestTemplate - ) { - bool isCompleted = await _carPlayController - .reactToNativeModule(FCPChannelTypes.pushTemplate, { + template.runtimeType == CPPointOfInterestTemplate) { + bool isCompleted = await _carPlayController.reactToNativeModule(FCPChannelTypes.pushTemplate, { "template": template.toJson(), "animated": animated, "runtimeType": "F" + template.runtimeType.toString(), diff --git a/lib/constants/constants.dart b/lib/constants/constants.dart index f86d0ae..96fa87d 100644 --- a/lib/constants/constants.dart +++ b/lib/constants/constants.dart @@ -4,3 +4,36 @@ enum CPConnectionStatusTypes { disconnected, unknown, } + +class CPConnectionStatusTypesUtil { + CPConnectionStatusTypesUtil._(); + static CPConnectionStatusTypes parseValue(String value) { + switch (value) { + case 'background': + return CPConnectionStatusTypes.background; + case 'connected': + return CPConnectionStatusTypes.connected; + case 'disconnected': + return CPConnectionStatusTypes.disconnected; + case 'unknown': + return CPConnectionStatusTypes.unknown; + default: + return CPConnectionStatusTypes.unknown; + } + } +} + +extension CPConnectionStatusTypeExtension on CPConnectionStatusTypes { + String stringValue() { + switch (this) { + case CPConnectionStatusTypes.background: + return 'background'; + case CPConnectionStatusTypes.connected: + return 'connected'; + case CPConnectionStatusTypes.disconnected: + return 'disconnected'; + case CPConnectionStatusTypes.unknown: + return 'unknown'; + } + } +} diff --git a/lib/constants/private_constants.dart b/lib/constants/private_constants.dart index 63cb643..dd5af84 100644 --- a/lib/constants/private_constants.dart +++ b/lib/constants/private_constants.dart @@ -18,3 +18,93 @@ enum FCPChannelTypes { onTextButtonPressed, popToRootTemplate, } + +class FCPChannelTypesUtil { + FCPChannelTypesUtil._(); + + static FCPChannelTypes parseValue(String value) { + switch (value) { + case 'onCarplayConnectionChange': + return FCPChannelTypes.onCarplayConnectionChange; + case 'setRootTemplate': + return FCPChannelTypes.setRootTemplate; + case 'forceUpdateRootTemplate': + return FCPChannelTypes.forceUpdateRootTemplate; + case 'updateListItem': + return FCPChannelTypes.updateListItem; + case 'onFCPListItemSelected': + return FCPChannelTypes.onFCPListItemSelected; + case 'onFCPListItemSelectedComplete': + return FCPChannelTypes.onFCPListItemSelectedComplete; + case 'onFCPAlertActionPressed': + return FCPChannelTypes.onFCPAlertActionPressed; + case 'setAlert': + return FCPChannelTypes.setAlert; + case 'onPresentStateChanged': + return FCPChannelTypes.onPresentStateChanged; + case 'popTemplate': + return FCPChannelTypes.popTemplate; + case 'closePresent': + return FCPChannelTypes.closePresent; + case 'pushTemplate': + return FCPChannelTypes.pushTemplate; + case 'showNowPlaying': + return FCPChannelTypes.showNowPlaying; + case 'onGridButtonPressed': + return FCPChannelTypes.onGridButtonPressed; + case 'setActionSheet': + return FCPChannelTypes.setActionSheet; + case 'onBarButtonPressed': + return FCPChannelTypes.onBarButtonPressed; + case 'onTextButtonPressed': + return FCPChannelTypes.onTextButtonPressed; + case 'popToRootTemplate': + return FCPChannelTypes.popToRootTemplate; + default: + throw ArgumentError('$value not supported'); + } + } +} + +extension FCPChannelTypesExtension on FCPChannelTypes { + String stringValue() { + switch (this) { + case FCPChannelTypes.onCarplayConnectionChange: + return 'onCarplayConnectionChange'; + case FCPChannelTypes.setRootTemplate: + return 'setRootTemplate'; + case FCPChannelTypes.forceUpdateRootTemplate: + return 'forceUpdateRootTemplate'; + case FCPChannelTypes.updateListItem: + return 'updateListItem'; + case FCPChannelTypes.onFCPListItemSelected: + return 'onFCPListItemSelected'; + case FCPChannelTypes.onFCPListItemSelectedComplete: + return 'onFCPListItemSelectedComplete'; + case FCPChannelTypes.onFCPAlertActionPressed: + return 'onFCPAlertActionPressed'; + case FCPChannelTypes.setAlert: + return 'setAlert'; + case FCPChannelTypes.onPresentStateChanged: + return 'onPresentStateChanged'; + case FCPChannelTypes.popTemplate: + return 'popTemplate'; + case FCPChannelTypes.closePresent: + return 'closePresent'; + case FCPChannelTypes.pushTemplate: + return 'pushTemplate'; + case FCPChannelTypes.showNowPlaying: + return 'showNowPlaying'; + case FCPChannelTypes.onGridButtonPressed: + return 'onGridButtonPressed'; + case FCPChannelTypes.setActionSheet: + return 'setActionSheet'; + case FCPChannelTypes.onBarButtonPressed: + return 'onBarButtonPressed'; + case FCPChannelTypes.onTextButtonPressed: + return 'onTextButtonPressed'; + case FCPChannelTypes.popToRootTemplate: + return 'popToRootTemplate'; + } + } +} diff --git a/lib/controllers/carplay_controller.dart b/lib/controllers/carplay_controller.dart index 5f538fe..a5c2f6a 100644 --- a/lib/controllers/carplay_controller.dart +++ b/lib/controllers/carplay_controller.dart @@ -1,12 +1,11 @@ import 'package:flutter/services.dart'; -import 'package:flutter_carplay/flutter_carplay.dart'; import 'package:flutter_carplay/helpers/carplay_helper.dart'; -import 'package:flutter_carplay/helpers/enum_utils.dart'; import 'package:flutter_carplay/models/alert/alert_action.dart'; import 'package:flutter_carplay/models/button/bar_button.dart'; import 'package:flutter_carplay/models/button/text_button.dart'; import 'package:flutter_carplay/models/grid/grid_button.dart'; import 'package:flutter_carplay/models/grid/grid_template.dart'; +import 'package:flutter_carplay/models/list/list_item.dart'; import 'package:flutter_carplay/models/list/list_template.dart'; import 'package:flutter_carplay/models/information/information_template.dart'; import 'package:flutter_carplay/models/poi/poi.dart'; @@ -18,10 +17,8 @@ import 'package:flutter_carplay/constants/private_constants.dart'; /// system with the Apple CarPlay and native functions. class FlutterCarPlayController { static final FlutterCarplayHelper _carplayHelper = FlutterCarplayHelper(); - static final MethodChannel _methodChannel = - MethodChannel(_carplayHelper.makeFCPChannelId()); - static final EventChannel _eventChannel = - EventChannel(_carplayHelper.makeFCPChannelId(event: "/event")); + static final MethodChannel _methodChannel = MethodChannel(_carplayHelper.makeFCPChannelId()); + static final EventChannel _eventChannel = EventChannel(_carplayHelper.makeFCPChannelId(event: "/event")); /// [CPTabBarTemplate], [CPGridTemplate], [CPListTemplate], [CPIInformationTemplate], [CPPointOfInterestTemplate] in a List static List templateHistory = []; @@ -41,14 +38,12 @@ class FlutterCarPlayController { } Future reactToNativeModule(FCPChannelTypes type, dynamic data) async { - final value = await _methodChannel.invokeMethod( - CPEnumUtils.stringFromEnum(type.toString()), data); + final value = await _methodChannel.invokeMethod(type.stringValue(), data); return value; } static void updateCPListItem(CPListItem updatedListItem) { - _methodChannel.invokeMethod('updateListItem', - {...updatedListItem.toJson()}).then((value) { + _methodChannel.invokeMethod('updateListItem', {...updatedListItem.toJson()}).then((value) { if (value) { l1: for (var h in templateHistory) { @@ -58,10 +53,7 @@ class FlutterCarPlayController { for (var s in t.sections) { for (var i in s.items) { if (i.uniqueId == updatedListItem.uniqueId) { - currentRootTemplate! - .templates[currentRootTemplate!.templates.indexOf(t)] - .sections[t.sections.indexOf(s)] - .items[s.items.indexOf(i)] = updatedListItem; + currentRootTemplate!.templates[currentRootTemplate!.templates.indexOf(t)].sections[t.sections.indexOf(s)].items[s.items.indexOf(i)] = updatedListItem; break l1; } } @@ -72,9 +64,7 @@ class FlutterCarPlayController { for (var s in (h as CPListTemplate).sections) { for (var i in s.items) { if (i.uniqueId == updatedListItem.uniqueId) { - currentRootTemplate! - .sections[currentRootTemplate!.sections.indexOf(s)] - .items[s.items.indexOf(i)] = updatedListItem; + currentRootTemplate!.sections[currentRootTemplate!.sections.indexOf(s)].items[s.items.indexOf(i)] = updatedListItem; break l1; } } @@ -116,8 +106,7 @@ class FlutterCarPlayController { } void processFCPAlertActionPressed(String elementId) { - CPAlertAction selectedAlertAction = currentPresentTemplate!.actions - .firstWhere((e) => e.uniqueId == elementId); + CPAlertAction selectedAlertAction = currentPresentTemplate!.actions.firstWhere((e) => e.uniqueId == elementId); selectedAlertAction.onPress(); } @@ -160,20 +149,16 @@ class FlutterCarPlayController { for (var t in templateHistory) { if (t.runtimeType.toString() == "CPPointOfInterestTemplate") { for (CPPointOfInterest p in t.poi) { - if (p.primaryButton != null && - p.primaryButton!.uniqueId == elementId) { + if (p.primaryButton != null && p.primaryButton!.uniqueId == elementId) { p.primaryButton!.onPress(); break l1; } - if (p.secondaryButton != null && - p.secondaryButton!.uniqueId == elementId) { + if (p.secondaryButton != null && p.secondaryButton!.uniqueId == elementId) { p.secondaryButton!.onPress(); break l1; } } - } - else - { + } else { if (t.runtimeType.toString() == "CPInformationTemplate") { l2: for (CPTextButton b in t.actions) { @@ -186,6 +171,4 @@ class FlutterCarPlayController { } } } - } - - +} diff --git a/lib/flutter_carplay.dart b/lib/flutter_carplay.dart index 3a2b579..aedb91b 100644 --- a/lib/flutter_carplay.dart +++ b/lib/flutter_carplay.dart @@ -1,4 +1,3 @@ -export "package:flutter_carplay/helpers/enum_utils.dart"; export 'package:flutter_carplay/constants/constants.dart' show CPConnectionStatusTypes; export "package:flutter_carplay/models/action_sheet/action_sheet_template.dart" diff --git a/lib/helpers/enum_utils.dart b/lib/helpers/enum_utils.dart deleted file mode 100644 index b13373e..0000000 --- a/lib/helpers/enum_utils.dart +++ /dev/null @@ -1,17 +0,0 @@ -class CPEnumUtils { - static T enumFromString(Iterable values, String string) { - return values.firstWhere( - (f) => - f - .toString() - .substring(f.toString().indexOf('.') + 1) - .toString() - .toUpperCase() == - string.substring(string.indexOf('.') + 1).toString().toUpperCase(), - ); - } - - static String stringFromEnum(dynamic value) { - return value.toString().split('.').last; - } -} diff --git a/lib/models/alert/alert_action.dart b/lib/models/alert/alert_action.dart index a5c941e..b82d649 100644 --- a/lib/models/alert/alert_action.dart +++ b/lib/models/alert/alert_action.dart @@ -1,4 +1,3 @@ -import 'package:flutter_carplay/helpers/enum_utils.dart'; import 'package:flutter_carplay/models/alert/alert_constants.dart'; import 'package:uuid/uuid.dart'; @@ -27,7 +26,7 @@ class CPAlertAction { Map toJson() => { "_elementId": _elementId, "title": title, - "style": CPEnumUtils.stringFromEnum(style.toString()), + "style": style.stringValue(), }; String get uniqueId { diff --git a/lib/models/alert/alert_constants.dart b/lib/models/alert/alert_constants.dart index 193c059..9f3c42e 100644 --- a/lib/models/alert/alert_constants.dart +++ b/lib/models/alert/alert_constants.dart @@ -3,3 +3,35 @@ enum CPAlertActionStyles { cancel, destructive, } + +class CPAlertActionStylesUtil { + CPAlertActionStylesUtil._(); + + static CPAlertActionStyles parseValue(String value) { + switch (value) { + case 'normal': + return CPAlertActionStyles.normal; + case 'cancel': + return CPAlertActionStyles.cancel; + case 'destructive': + return CPAlertActionStyles.destructive; + default: + throw ArgumentError('$value not supported'); + } + } +} + +extension CPAlertActionStylesExtension on CPAlertActionStyles { + String stringValue() { + switch (this) { + case CPAlertActionStyles.normal: + return 'normal'; + case CPAlertActionStyles.cancel: + return 'cancel'; + case CPAlertActionStyles.destructive: + return 'destructive'; + default: + throw ArgumentError('$this not supported'); + } + } +} diff --git a/lib/models/button/alert_constants.dart b/lib/models/button/alert_constants.dart index 0bf0025..8ada04f 100644 --- a/lib/models/button/alert_constants.dart +++ b/lib/models/button/alert_constants.dart @@ -2,3 +2,29 @@ enum CPBarButtonStyles { none, rounded, } + +class CPBarButtonStylesUtil { + CPBarButtonStylesUtil._(); + + static CPBarButtonStyles parseValue(String value) { + switch (value) { + case 'rounded': + return CPBarButtonStyles.rounded; + case 'none': + default: + return CPBarButtonStyles.none; + } + } +} + +extension CPBarButtonStylesExtension on CPBarButtonStyles { + String stringValue() { + switch (this) { + case CPBarButtonStyles.rounded: + return 'rounded'; + case CPBarButtonStyles.none: + default: + return 'none'; + } + } +} diff --git a/lib/models/button/bar_button.dart b/lib/models/button/bar_button.dart index 61b8421..043829d 100644 --- a/lib/models/button/bar_button.dart +++ b/lib/models/button/bar_button.dart @@ -1,4 +1,3 @@ -import 'package:flutter_carplay/helpers/enum_utils.dart'; import 'package:flutter_carplay/models/button/alert_constants.dart'; import 'package:uuid/uuid.dart'; @@ -27,7 +26,7 @@ class CPBarButton { Map toJson() => { "_elementId": _elementId, "title": title, - "style": CPEnumUtils.stringFromEnum(style.toString()), + "style": style.stringValue(), }; String get uniqueId { diff --git a/lib/models/button/text_button.dart b/lib/models/button/text_button.dart index 82d5051..4fb0600 100644 --- a/lib/models/button/text_button.dart +++ b/lib/models/button/text_button.dart @@ -1,14 +1,42 @@ -import 'package:flutter_carplay/helpers/enum_utils.dart'; import 'package:uuid/uuid.dart'; - enum CPTextButtonStyles { normal, cancel, confirm, +} + +class CPTextButtonStylesUtil { + CPTextButtonStylesUtil._(); + static CPTextButtonStyles parseValue(String value) { + switch (value) { + case 'normal': + return CPTextButtonStyles.normal; + case 'cancel': + return CPTextButtonStyles.cancel; + case 'confirm': + return CPTextButtonStyles.confirm; + default: + throw ArgumentError('$value is not supported'); + } + } } +extension CPTextButtonStylesExtension on CPTextButtonStyles { + String stringValue() { + switch (this) { + case CPTextButtonStyles.normal: + return 'normal'; + case CPTextButtonStyles.cancel: + return 'cancel'; + case CPTextButtonStyles.confirm: + return 'confirm'; + default: + throw ArgumentError('$this is not supported'); + } + } +} /// A button object for placement in a point of interest or information template. class CPTextButton { @@ -33,10 +61,10 @@ class CPTextButton { }); Map toJson() => { - "_elementId": _elementId, - "title": title, - "style": CPEnumUtils.stringFromEnum(style.toString()), - }; + "_elementId": _elementId, + "title": title, + "style": style.stringValue(), + }; String get uniqueId { return _elementId; diff --git a/lib/models/information/information_constants.dart b/lib/models/information/information_constants.dart index ccc26c0..116b1d3 100644 --- a/lib/models/information/information_constants.dart +++ b/lib/models/information/information_constants.dart @@ -2,3 +2,31 @@ enum CPInformationTemplateLayout { leading, twoColumn, } + +class CPInformationTemplateLayoutUtil { + CPInformationTemplateLayoutUtil._(); + + static CPInformationTemplateLayout parseValue(String value) { + switch (value) { + case 'leading': + return CPInformationTemplateLayout.leading; + case 'twoColumn': + return CPInformationTemplateLayout.twoColumn; + default: + throw ArgumentError('$value is not supproted'); + } + } +} + +extension CPListItemPlayingIndicatorLocationsExtension on CPInformationTemplateLayout? { + String stringValue() { + switch (this) { + case CPInformationTemplateLayout.leading: + return 'leading'; + case CPInformationTemplateLayout.twoColumn: + return 'twoColumn'; + default: + throw ArgumentError('$this is not supported'); + } + } +} \ No newline at end of file diff --git a/lib/models/information/information_template.dart b/lib/models/information/information_template.dart index bf7246d..fcf057e 100644 --- a/lib/models/information/information_template.dart +++ b/lib/models/information/information_template.dart @@ -1,5 +1,4 @@ import 'package:flutter_carplay/models/button/text_button.dart'; -import 'package:flutter_carplay/helpers/enum_utils.dart'; import 'information_constants.dart'; import 'information_item.dart'; @@ -34,7 +33,7 @@ class CPInformationTemplate { Map toJson() => { "_elementId": _elementId, - "layout": CPEnumUtils.stringFromEnum(layout.toString()), + "layout": layout.stringValue(), "title": title, "actions": actions.map((e) => e.toJson()).toList(), "informationItems": informationItems.map((e) => e.toJson()).toList(), diff --git a/lib/models/list/list_constants.dart b/lib/models/list/list_constants.dart index e983219..2a86b24 100644 --- a/lib/models/list/list_constants.dart +++ b/lib/models/list/list_constants.dart @@ -3,8 +3,66 @@ enum CPListItemPlayingIndicatorLocations { leading, } +class CPListItemPlayingIndicatorLocationsUtil { + CPListItemPlayingIndicatorLocationsUtil._(); + + static CPListItemPlayingIndicatorLocations parseValue(String value) { + switch (value) { + case 'trailing': + return CPListItemPlayingIndicatorLocations.trailing; + case 'leading': + return CPListItemPlayingIndicatorLocations.leading; + default: + throw ArgumentError('$value is not supproted'); + } + } +} + +extension CPListItemPlayingIndicatorLocationsExtension on CPListItemPlayingIndicatorLocations? { + String stringValue() { + switch (this) { + case CPListItemPlayingIndicatorLocations.trailing: + return 'trailing'; + case CPListItemPlayingIndicatorLocations.leading: + return 'leading'; + default: + throw ArgumentError('$this is not supported'); + } + } +} + enum CPListItemAccessoryTypes { none, cloud, disclosureIndicator, } + +class CPListItemAccessoryTypesUtil { + CPListItemAccessoryTypesUtil._(); + + static CPListItemAccessoryTypes parseValue(String value) { + switch (value) { + case 'cloud': + return CPListItemAccessoryTypes.cloud; + case 'disclosureIndicator': + return CPListItemAccessoryTypes.disclosureIndicator; + case 'none': + default: + return CPListItemAccessoryTypes.none; + } + } +} + +extension CPListItemAccessoryTypesExtension on CPListItemAccessoryTypes? { + String stringValue() { + switch (this) { + case CPListItemAccessoryTypes.cloud: + return 'cloud'; + case CPListItemAccessoryTypes.disclosureIndicator: + return 'disclosureIndicator'; + case CPListItemAccessoryTypes.none: + default: + return 'none'; + } + } +} diff --git a/lib/models/list/list_item.dart b/lib/models/list/list_item.dart index e68ca68..aac7776 100644 --- a/lib/models/list/list_item.dart +++ b/lib/models/list/list_item.dart @@ -1,5 +1,4 @@ import 'package:flutter_carplay/controllers/carplay_controller.dart'; -import 'package:flutter_carplay/helpers/enum_utils.dart'; import 'package:flutter_carplay/models/list/list_constants.dart'; import 'package:uuid/uuid.dart'; @@ -59,8 +58,8 @@ class CPListItem { "playbackProgress": playbackProgress, "isPlaying": isPlaying, "playingIndicatorLocation": - CPEnumUtils.stringFromEnum(playingIndicatorLocation.toString()), - "accessoryType": CPEnumUtils.stringFromEnum(accessoryType.toString()), + playingIndicatorLocation.stringValue(), + "accessoryType": accessoryType.stringValue(), }; /// Updating the list item's primary text. diff --git a/test/helpers/enum_utils_test.dart b/test/helpers/enum_utils_test.dart deleted file mode 100644 index 7cd577c..0000000 --- a/test/helpers/enum_utils_test.dart +++ /dev/null @@ -1,30 +0,0 @@ -import 'package:flutter_carplay/flutter_carplay.dart'; -import 'package:flutter_test/flutter_test.dart'; - -void main() { - group('CPEnumUtils', () { - test('convert string into any type of enum', () { - final cpListItemAccessoryType = - CPEnumUtils.enumFromString(CPListItemAccessoryTypes.values, 'cloud'); - - expect(cpListItemAccessoryType, CPListItemAccessoryTypes.cloud); - - final cpListItemPlayingIndicatorLocation = CPEnumUtils.enumFromString( - CPListItemPlayingIndicatorLocations.values, 'trailing'); - - expect(cpListItemPlayingIndicatorLocation, - CPListItemPlayingIndicatorLocations.trailing); - }); - - test('convert dynamic type into string after the `.`', () { - final cpAlertActionStylesString = CPEnumUtils.stringFromEnum(CPAlertActionStyles.normal.toString()); - - expect(cpAlertActionStylesString, 'normal'); - - final fcpChannelTypesString = - CPEnumUtils.stringFromEnum('car.setAlert'); - - expect(fcpChannelTypesString, 'setAlert'); - }); - }); -}