diff --git a/WunderLINQ.xcodeproj/project.pbxproj b/WunderLINQ.xcodeproj/project.pbxproj index 04294c7..c394f48 100644 --- a/WunderLINQ.xcodeproj/project.pbxproj +++ b/WunderLINQ.xcodeproj/project.pbxproj @@ -110,6 +110,9 @@ B98CA75D252E6C6B00530075 /* UIStoryboard+extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98CA75C252E6C6B00530075 /* UIStoryboard+extensions.swift */; }; B98CA762252E6F5500530075 /* String+Localizable.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98CA761252E6F5500530075 /* String+Localizable.swift */; }; D80365FE2B377FCD00F48637 /* SoundManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D80365FD2B377FCD00F48637 /* SoundManager.swift */; }; + D81CBFAE2C8119580029C5AB /* Entitlements.plist in Resources */ = {isa = PBXBuildFile; fileRef = D81CBFAD2C8119580029C5AB /* Entitlements.plist */; }; + D826545A2C82768000049F9E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D82654582C82768000049F9E /* SceneDelegate.swift */; }; + D826545B2C82768000049F9E /* CarPlaySceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D82654592C82768000049F9E /* CarPlaySceneDelegate.swift */; }; D8585C53293EBD550025D0AB /* Main_iPad.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D8585C51293EBD540025D0AB /* Main_iPad.storyboard */; }; D87629122A7EDEB90021E7A7 /* standard-dashboard-portrait.svg in Resources */ = {isa = PBXBuildFile; fileRef = D87629112A7EDEB90021E7A7 /* standard-dashboard-portrait.svg */; }; D880B6022C6BFED900B4252B /* wav in Resources */ = {isa = PBXBuildFile; fileRef = D880B6012C6BFED900B4252B /* wav */; }; @@ -272,6 +275,9 @@ B98CA75C252E6C6B00530075 /* UIStoryboard+extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIStoryboard+extensions.swift"; sourceTree = ""; }; B98CA761252E6F5500530075 /* String+Localizable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Localizable.swift"; sourceTree = ""; }; D80365FD2B377FCD00F48637 /* SoundManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundManager.swift; sourceTree = ""; }; + D81CBFAD2C8119580029C5AB /* Entitlements.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Entitlements.plist; sourceTree = ""; }; + D82654582C82768000049F9E /* SceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; + D82654592C82768000049F9E /* CarPlaySceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CarPlaySceneDelegate.swift; sourceTree = ""; }; D8585C52293EBD540025D0AB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main_iPad.storyboard; sourceTree = ""; }; D87629112A7EDEB90021E7A7 /* standard-dashboard-portrait.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "standard-dashboard-portrait.svg"; sourceTree = ""; }; D880B6012C6BFED900B4252B /* wav */ = {isa = PBXFileReference; lastKnownFileType = folder; path = wav; sourceTree = ""; }; @@ -411,6 +417,7 @@ 7EA4784C1F40B154000CEDDC /* WunderLINQ */ = { isa = PBXGroup; children = ( + D82654572C82758F00049F9E /* CarPlay */, 7E00516528EA150B00BC3CC1 /* TaskList */, 7EBC4E2328E133B800B1998B /* comms */, 7EB80AB82790FF8800D3A051 /* hardware */, @@ -462,6 +469,7 @@ 7EA478561F40B154000CEDDC /* LaunchScreen.storyboard */, 7E4D76E521DDAEC100825175 /* InfoPlist.strings */, 7EA478591F40B154000CEDDC /* Info.plist */, + D81CBFAD2C8119580029C5AB /* Entitlements.plist */, ); path = WunderLINQ; sourceTree = ""; @@ -566,6 +574,15 @@ path = External; sourceTree = ""; }; + D82654572C82758F00049F9E /* CarPlay */ = { + isa = PBXGroup; + children = ( + D82654592C82768000049F9E /* CarPlaySceneDelegate.swift */, + D82654582C82768000049F9E /* SceneDelegate.swift */, + ); + path = CarPlay; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -623,7 +640,6 @@ 7EA478491F40B154000CEDDC = { CreatedOnToolsVersion = 8.3.3; LastSwiftMigration = 1020; - ProvisioningStyle = Automatic; SystemCapabilities = { com.apple.BackgroundModes = { enabled = 1; @@ -632,9 +648,7 @@ }; 7EA4785D1F40B154000CEDDC = { CreatedOnToolsVersion = 8.3.3; - DevelopmentTeam = L5697YQ5RS; LastSwiftMigration = 1020; - ProvisioningStyle = Automatic; TestTargetID = 7EA478491F40B154000CEDDC; }; }; @@ -683,6 +697,7 @@ D87629122A7EDEB90021E7A7 /* standard-dashboard-portrait.svg in Resources */, 7EA478581F40B154000CEDDC /* LaunchScreen.storyboard in Resources */, 7EA478551F40B154000CEDDC /* Assets.xcassets in Resources */, + D81CBFAE2C8119580029C5AB /* Entitlements.plist in Resources */, 7EB5EDE4252136F800B61160 /* Main.storyboard in Resources */, 7ED2013620D0D0030066BCE6 /* Default-568h@2x.png in Resources */, 7E755C13260A787100B29507 /* standard-dashboard.svg in Resources */, @@ -750,6 +765,7 @@ buildActionMask = 2147483647; files = ( 7EA478711F40F273000CEDDC /* Device.swift in Sources */, + D826545B2C82768000049F9E /* CarPlaySceneDelegate.swift in Sources */, 7E80BD3D211CFF7300B8CA0D /* LocalisableButton.swift in Sources */, 7E5E0F78212289140048B9A2 /* AppUtility.swift in Sources */, 7EA478771F412BAC000CEDDC /* CLLocation+Extension.swift in Sources */, @@ -796,6 +812,7 @@ 7EBFD48E210ADD0000FC9665 /* String+Extensions.swift in Sources */, 7E9346B528E0A67F009E13C0 /* UIViewController+Extensions.swift in Sources */, 7EAF646A22FE261D00B11550 /* WeatherMapViewController.swift in Sources */, + D826545A2C82768000049F9E /* SceneDelegate.swift in Sources */, 7EA478811F44F57C000CEDDC /* MusicViewController.swift in Sources */, 7EF07772258FF762009A6BAA /* HWSettingsActionViewController.swift in Sources */, 7E620A0D26278E4900F6E23B /* AppSettingsViewController.swift in Sources */, @@ -950,6 +967,7 @@ CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_ENTITLEMENTS = WunderLINQ/Entitlements.plist; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = dwarf; @@ -1012,6 +1030,7 @@ CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_ENTITLEMENTS = WunderLINQ/Entitlements.plist; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; @@ -1043,7 +1062,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ANALYZER_LOCALIZABILITY_EMPTY_CONTEXT = YES; CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; - CODE_SIGN_IDENTITY = "iPhone Developer"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 5; DEVELOPMENT_TEAM = 3J48UMN5W2; @@ -1073,7 +1092,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ANALYZER_LOCALIZABILITY_EMPTY_CONTEXT = YES; CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; - CODE_SIGN_IDENTITY = "iPhone Developer"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 5; DEVELOPMENT_TEAM = 3J48UMN5W2; @@ -1101,7 +1120,9 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; - DEVELOPMENT_TEAM = L5697YQ5RS; + CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = WunderLINQTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -1110,6 +1131,7 @@ ); PRODUCT_BUNDLE_IDENTIFIER = com.blackboxembedded.WunderLINQTests; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WunderLINQ.app/WunderLINQ"; }; @@ -1120,7 +1142,9 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; - DEVELOPMENT_TEAM = L5697YQ5RS; + CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = WunderLINQTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -1129,6 +1153,7 @@ ); PRODUCT_BUNDLE_IDENTIFIER = com.blackboxembedded.WunderLINQTests; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WunderLINQ.app/WunderLINQ"; }; diff --git a/WunderLINQ/Base.lproj/Localizable.strings b/WunderLINQ/Base.lproj/Localizable.strings index 6de36aa..3550a32 100644 --- a/WunderLINQ/Base.lproj/Localizable.strings +++ b/WunderLINQ/Base.lproj/Localizable.strings @@ -383,6 +383,9 @@ "dash_altitude_label" = "ELEV"; //VolumeViewController "systemvolume_title" = "System Volume"; +//CarPlay +"carplay_ios14_warning_text" = "iOS 14 Required"; +"carplay_ios14_warning_detail" = "CarPlay Support Requires iOS 14+"; //Keyboard HID "hid_0x00_label" = "Not Set"; "hid_keyboard_label" = "Standard"; diff --git a/WunderLINQ/CarPlay/CarPlaySceneDelegate.swift b/WunderLINQ/CarPlay/CarPlaySceneDelegate.swift new file mode 100644 index 0000000..8770a5e --- /dev/null +++ b/WunderLINQ/CarPlay/CarPlaySceneDelegate.swift @@ -0,0 +1,141 @@ +/* +WunderLINQ Client Application +Copyright (C) 2020 Keith Conger, Black Box Embedded, LLC + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +import UIKit +import CarPlay + +class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate, CPListTemplateDelegate { + var interfaceController: CPInterfaceController? + var refreshTimer: Timer? + + func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, + didConnect interfaceController: CPInterfaceController) { + + self.interfaceController = interfaceController + + updateGridTemplate() + + // Start the timer to refresh the grid template every 10 seconds + startRefreshTimer() + } + + @objc func updateGridTemplate() { + + if #available(iOS 14.0, *) { + let dataPoint1 = UserDefaults.standard.integer(forKey: "grid_one_preference") + let gridButton1 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint1))], + image: MotorcycleData.getIcon(dataPoint: dataPoint1)) { button in + } + let dataPoint2 = UserDefaults.standard.integer(forKey: "grid_two_preference") + let gridButton2 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint2))], + image: MotorcycleData.getIcon(dataPoint: dataPoint2)) { button in + } + let dataPoint3 = UserDefaults.standard.integer(forKey: "grid_three_preference") + let gridButton3 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint3))], + image: MotorcycleData.getIcon(dataPoint: dataPoint3)) { button in + } + let dataPoint4 = UserDefaults.standard.integer(forKey: "grid_four_preference") + let gridButton4 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint4))], + image: MotorcycleData.getIcon(dataPoint: dataPoint4)) { button in + } + let dataPoint5 = UserDefaults.standard.integer(forKey: "grid_five_preference") + let gridButton5 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint5))], + image: MotorcycleData.getIcon(dataPoint: dataPoint5)) { button in + } + let dataPoint6 = UserDefaults.standard.integer(forKey: "grid_six_preference") + let gridButton6 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint6))], + image: MotorcycleData.getIcon(dataPoint: dataPoint6)) { button in + } + let dataPoint7 = UserDefaults.standard.integer(forKey: "grid_seven_preference") + let gridButton7 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint7))], + image: MotorcycleData.getIcon(dataPoint: dataPoint7)) { button in + } + let dataPoint8 = UserDefaults.standard.integer(forKey: "grid_eight_preference") + let gridButton8 = CPGridButton(titleVariants: [padString(MotorcycleData.getValue(dataPoint: dataPoint8))], + image: MotorcycleData.getIcon(dataPoint: dataPoint8)) { button in + } + let gridTemplate = CPGridTemplate(title: NSLocalizedString("product", comment: ""), gridButtons: [gridButton1,gridButton2,gridButton3,gridButton4,gridButton5,gridButton6,gridButton7,gridButton8]) + + interfaceController?.setRootTemplate(gridTemplate, + animated: true, + completion: nil) + } else { + // Fallback for iOS 13 + interfaceController?.setRootTemplate(self.listTemplate(), + animated: true) + } + } + + func startRefreshTimer() { + refreshTimer?.invalidate() // Invalidate any existing timer + refreshTimer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(updateGridTemplate), userInfo: nil, repeats: true) + } + + func listTemplate() -> CPListTemplate { + let item = CPListItem(text: NSLocalizedString("carplay_ios14_warning_text", comment: ""), detailText: NSLocalizedString("carplay_ios14_warning_detail", comment: "")) + + if #available(iOS 14.0, *) { + item.handler = { item, completion in + NSLog("Item selected") + completion() + } + } + + let section = CPListSection(items: [item]) + let listTemplate = CPListTemplate(title: NSLocalizedString("product", comment: ""), sections: [section]) + + if #available(iOS 14.0, *) { + // Do nothing, handler is already set + } else { + listTemplate.delegate = self + } + + return listTemplate + } + + func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didDisconnectInterfaceController interfaceController: CPInterfaceController) { + self.interfaceController = nil + refreshTimer?.invalidate() // Invalidate the timer when disconnected + } + + // Delegate method for CPListTemplateDelegate (iOS 13) + func listTemplate(_ listTemplate: CPListTemplate, didSelect item: CPListItem, completionHandler: @escaping () -> Void) { + NSLog("Item selected") + completionHandler() + } + + func padString(_ input: String) -> String { + let minimumLength = 10 + + // If the string is already of sufficient length, return it as is + if input.count >= minimumLength { + return input + } + + // Calculate the number of spaces needed + let totalPadding = minimumLength - input.count + let leadingPadding = totalPadding / 2 + let trailingPadding = totalPadding - leadingPadding + + // Create the padded string + let paddedString = String(repeating: " ", count: leadingPadding) + input + String(repeating: " ", count: trailingPadding) + + return paddedString + } + +} diff --git a/WunderLINQ/CarPlay/SceneDelegate.swift b/WunderLINQ/CarPlay/SceneDelegate.swift new file mode 100644 index 0000000..ad10ad8 --- /dev/null +++ b/WunderLINQ/CarPlay/SceneDelegate.swift @@ -0,0 +1,25 @@ +/* +WunderLINQ Client Application +Copyright (C) 2020 Keith Conger, Black Box Embedded, LLC + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +import UIKit + +class SceneDelegate: UIResponder, UIWindowSceneDelegate { + + var window: UIWindow? + +} diff --git a/WunderLINQ/Entitlements.plist b/WunderLINQ/Entitlements.plist new file mode 100644 index 0000000..6a80566 --- /dev/null +++ b/WunderLINQ/Entitlements.plist @@ -0,0 +1,15 @@ + + + + + +com.apple.developer.carplay-driving-task + + + diff --git a/WunderLINQ/Info.plist b/WunderLINQ/Info.plist index 6dd18e9..fc890d7 100644 --- a/WunderLINQ/Info.plist +++ b/WunderLINQ/Info.plist @@ -2,6 +2,34 @@ + UIApplicationSceneManifest + + UISceneConfigurations + + UIWindowSceneSessionRoleApplication + + + UISceneStoryboardFile + Main + UISceneConfigurationName + Default Configuration + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).SceneDelegate + + + CPTemplateApplicationSceneSessionRoleApplication + + + UISceneClassName + CPTemplateApplicationScene + UISceneConfigurationName + CarPlay Configuration + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).CarPlaySceneDelegate + + + + CFBundleAllowMixedLocalizations CFBundleDevelopmentRegion @@ -109,8 +137,8 @@ $(PRODUCT_NAME) needs access to save photos taken to your photo library. NSPhotoLibraryUsageDescription $(PRODUCT_NAME) needs access to save photos taken to your photo library. - NSMotionUsageDescription - $(PRODUCT_NAME) needs access to the barometric pressure sensor data. + NSMotionUsageDescription + $(PRODUCT_NAME) needs access to the barometric pressure sensor data. UIAppFonts agencyfb.ttf diff --git a/WunderLINQ/MainCollectionViewController.swift b/WunderLINQ/MainCollectionViewController.swift index ee0ea58..c517a8a 100644 --- a/WunderLINQ/MainCollectionViewController.swift +++ b/WunderLINQ/MainCollectionViewController.swift @@ -878,9 +878,9 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource } } - let label = getLabel(cell: indexPath.row + 1) + let label = MotorcycleData.getLabel(dataPoint: getCellDataPoint(cell: indexPath.row + 1)) cell.setLabel(label: label) - let icon = getIcon(cell: indexPath.row + 1) + let icon = MotorcycleData.getIcon(dataPoint: getCellDataPoint(cell: indexPath.row + 1)) cell.setIcon(icon: icon) return cell @@ -997,266 +997,6 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource return cellDataPoint } - func getLabel(cell: Int) -> String { - var label:String = "" - let cellDataPoint = getCellDataPoint(cell: cell) - var temperatureUnit = "C" - var heightUnit = "m" - var distanceUnit = "km" - var distanceTimeUnit = "kmh" - var consumptionUnit = "L/100" - var pressureUnit = "psi" - // Pressure Unit - switch UserDefaults.standard.integer(forKey: "pressure_unit_preference"){ - case 0: - pressureUnit = "bar" - case 1: - pressureUnit = "kPa" - case 2: - pressureUnit = "kg-f" - case 3: - pressureUnit = "psi" - default: - NSLog("MainCollectionViewController: Unknown pressure unit setting") - } - if UserDefaults.standard.integer(forKey: "temperature_unit_preference") == 1 { - temperatureUnit = "F" - } - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - distanceUnit = "mi" - distanceTimeUnit = "mph" - heightUnit = "ft" - } - switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ - case 0: - consumptionUnit = "L/100" - case 1: - consumptionUnit = "mpg" - case 2: - consumptionUnit = "mpg" - case 3: - consumptionUnit = "km/L" - default: - NSLog("MainCollectionViewController: Unknown consumption unit setting") - } - - switch (cellDataPoint){ - case 0: - // Gear - label = NSLocalizedString("gear_header", comment: "") - case 1: - // Engine Temperature - label = NSLocalizedString("enginetemp_header", comment: "") + " (" + temperatureUnit + ")" - case 2: - // Ambient Temperature - label = NSLocalizedString("ambienttemp_header", comment: "") + " (" + temperatureUnit + ")" - case 3: - // Front Tire Pressure - label = NSLocalizedString("frontpressure_header", comment: "") + " (" + pressureUnit + ")" - case 4: - // Rear Tire Pressure - label = NSLocalizedString("rearpressure_header", comment: "") + " (" + pressureUnit + ")" - case 5: - // Odometer - label = NSLocalizedString("odometer_header", comment: "") + " (" + distanceUnit + ")" - case 6: - // Voltage - label = NSLocalizedString("voltage_header", comment: "") + " (V)" - case 7: - // Trottle - label = NSLocalizedString("throttle_header", comment: "") + " (%)" - case 8: - // Front Brakes - label = NSLocalizedString("frontbrakes_header", comment: "") - case 9: - // Rear Brakes - label = NSLocalizedString("rearbrakes_header", comment: "") - case 10: - // Ambient Light - label = NSLocalizedString("ambientlight_header", comment: "") - case 11: - // Trip 1 - label = NSLocalizedString("tripone_header", comment: "") + " (" + distanceUnit + ")" - case 12: - // Trip 2 - label = NSLocalizedString("triptwo_header", comment: "") + " (" + distanceUnit + ")" - case 13: - // Trip Auto - label = NSLocalizedString("tripauto_header", comment: "") + " (" + distanceUnit + ")" - case 14: - // Speed - label = NSLocalizedString("speed_header", comment: "") + " (" + distanceTimeUnit + ")" - case 15: - //Average Speed - label = NSLocalizedString("avgspeed_header", comment: "") + " (" + distanceTimeUnit + ")" - case 16: - //Current Consumption - label = NSLocalizedString("cconsumption_header", comment: "") + " (" + consumptionUnit + ")" - case 17: - //Fuel Economy One - label = NSLocalizedString("fueleconomyone_header", comment: "") + " (" + consumptionUnit + ")" - case 18: - //Fuel Economy Two - label = NSLocalizedString("fueleconomytwo_header", comment: "") + " (" + consumptionUnit + ")" - case 19: - //Fuel Range - label = NSLocalizedString("fuelrange_header", comment: "") + " (" + distanceUnit + ")" - case 20: - //Shifts - label = NSLocalizedString("shifts_header", comment: "") - case 21: - //Lean Angle - label = NSLocalizedString("leanangle_header", comment: "") - case 22: - //g-force - label = NSLocalizedString("gforce_header", comment: "") - case 23: - //bearing - label = NSLocalizedString("bearing_header", comment: "") - case 24: - //time - label = NSLocalizedString("time_header", comment: "") - case 25: - //barometric pressure - label = NSLocalizedString("barometric_header", comment: "") + " (mBar)" - case 26: - //GPS Speed - label = NSLocalizedString("gpsspeed_header", comment: "") + " (" + distanceTimeUnit + ")" - case 27: - //altitude - label = NSLocalizedString("altitude_header", comment: "") + " (" + heightUnit + ")" - case 28: - //Sunrise/Sunset - label = NSLocalizedString("sunrisesunset_header", comment: "") - case 29: - //RPM - label = NSLocalizedString("rpm_header", comment: "") - case 30: - //Lean Angle - label = NSLocalizedString("leanangle_bike_header", comment: "") - case 31: - //Rear Wheel Speed - label = NSLocalizedString("rearwheel_speed_header", comment: "") - case 32: - //Device Battery - label = NSLocalizedString("local_battery_header", comment: "") - default: - NSLog("MainCollectionViewController: Unknown : \(cellDataPoint)") - } - - return label - } - - func getIcon(cell: Int) -> UIImage { - var icon:UIImage = (UIImage(named: "Cog")?.withRenderingMode(.alwaysTemplate))! - let cellDataPoint = getCellDataPoint(cell: cell) - switch (cellDataPoint){ - case 0: - // Gear - icon = (UIImage(named: "Cog")?.withRenderingMode(.alwaysTemplate))! - case 1: - // Engine Temperature - icon = (UIImage(named: "Engine-Temp")?.withRenderingMode(.alwaysTemplate))! - case 2: - // Ambient Temperature - icon = (UIImage(named: "Thermometer")?.withRenderingMode(.alwaysTemplate))! - case 3: - // Front Tire Pressure - icon = (UIImage(named: "Tire")?.withRenderingMode(.alwaysTemplate))! - case 4: - // Rear Tire Pressure - icon = (UIImage(named: "Tire")?.withRenderingMode(.alwaysTemplate))! - case 5: - // Odometer - icon = (UIImage(named: "Odometer")?.withRenderingMode(.alwaysTemplate))! - case 6: - // Voltage - icon = (UIImage(named: "Battery")?.withRenderingMode(.alwaysTemplate))! - case 7: - // Trottle - icon = (UIImage(named: "Signature")?.withRenderingMode(.alwaysTemplate))! - case 8: - // Front Brakes - icon = (UIImage(named: "Brakes")?.withRenderingMode(.alwaysTemplate))! - case 9: - // Rear Brakes - icon = (UIImage(named: "Brakes")?.withRenderingMode(.alwaysTemplate))! - case 10: - // Ambient Light - icon = (UIImage(named: "Light-bulb")?.withRenderingMode(.alwaysTemplate))! - case 11: - // Trip 1 - icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! - case 12: - // Trip 2 - icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! - case 13: - // Trip Auto - icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! - case 14: - // Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - case 15: - //Average Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - case 16: - //Current Consumption - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - case 17: - //Fuel Economy One - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - case 18: - //Fuel Economy Two - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - case 19: - //Fuel Range - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - case 20: - //Shifts - icon = (UIImage(named: "Arrows-alt")?.withRenderingMode(.alwaysTemplate))! - case 21: - //Lean Angle - icon = (UIImage(named: "Angle")?.withRenderingMode(.alwaysTemplate))! - case 22: - //g-force - icon = (UIImage(named: "Accelerometer")?.withRenderingMode(.alwaysTemplate))! - case 23: - //bearing - icon = (UIImage(named: "Compass")?.withRenderingMode(.alwaysTemplate))! - case 24: - //time - icon = (UIImage(named: "Clock")?.withRenderingMode(.alwaysTemplate))! - case 25: - //barometric pressure - icon = (UIImage(named: "Barometer")?.withRenderingMode(.alwaysTemplate))! - case 26: - //GPS Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - case 27: - //altitude - icon = (UIImage(named: "Mountain")?.withRenderingMode(.alwaysTemplate))! - case 28: - //Sunrise/Sunset - icon = (UIImage(named: "Sun")?.withRenderingMode(.alwaysTemplate))! - case 29: - //RPM - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - case 30: - //Lean Angle Bike - icon = (UIImage(named: "Angle")?.withRenderingMode(.alwaysTemplate))! - case 31: - //Rear Wheel Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - case 32: - //Device Battery - icon = (UIImage(named: "Battery-Empty")?.withRenderingMode(.alwaysTemplate))! - default: - NSLog("MainCollectionViewController: Unknown : \(cellDataPoint)") - } - - return icon - } - // MARK: - Updating UI func updateMessageDisplay() { // Update Buttons @@ -1271,594 +1011,17 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource // Update main display let cellCount = UserDefaults.standard.integer(forKey: "GRIDCOUNT") - switch (cellCount){ - case 15: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - let cell3 = UserDefaults.standard.integer(forKey: "grid_three_preference") - let cell4 = UserDefaults.standard.integer(forKey: "grid_four_preference") - let cell5 = UserDefaults.standard.integer(forKey: "grid_five_preference") - let cell6 = UserDefaults.standard.integer(forKey: "grid_six_preference") - let cell7 = UserDefaults.standard.integer(forKey: "grid_seven_preference") - let cell8 = UserDefaults.standard.integer(forKey: "grid_eight_preference") - let cell9 = UserDefaults.standard.integer(forKey: "grid_nine_preference") - let cell10 = UserDefaults.standard.integer(forKey: "grid_ten_preference") - let cell11 = UserDefaults.standard.integer(forKey: "grid_eleven_preference") - let cell12 = UserDefaults.standard.integer(forKey: "grid_twelve_preference") - let cell13 = UserDefaults.standard.integer(forKey: "grid_thirteen_preference") - let cell14 = UserDefaults.standard.integer(forKey: "grid_fourteen_preference") - let cell15 = UserDefaults.standard.integer(forKey: "grid_fifteen_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - setCellText(3, dataPoint: cell3) - setCellText(4, dataPoint: cell4) - setCellText(5, dataPoint: cell5) - setCellText(6, dataPoint: cell6) - setCellText(7, dataPoint: cell7) - setCellText(8, dataPoint: cell8) - setCellText(9, dataPoint: cell9) - setCellText(10, dataPoint: cell10) - setCellText(11, dataPoint: cell11) - setCellText(12, dataPoint: cell12) - setCellText(13, dataPoint: cell13) - setCellText(14, dataPoint: cell14) - setCellText(15, dataPoint: cell15) - case 12: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - let cell3 = UserDefaults.standard.integer(forKey: "grid_three_preference") - let cell4 = UserDefaults.standard.integer(forKey: "grid_four_preference") - let cell5 = UserDefaults.standard.integer(forKey: "grid_five_preference") - let cell6 = UserDefaults.standard.integer(forKey: "grid_six_preference") - let cell7 = UserDefaults.standard.integer(forKey: "grid_seven_preference") - let cell8 = UserDefaults.standard.integer(forKey: "grid_eight_preference") - let cell9 = UserDefaults.standard.integer(forKey: "grid_nine_preference") - let cell10 = UserDefaults.standard.integer(forKey: "grid_ten_preference") - let cell11 = UserDefaults.standard.integer(forKey: "grid_eleven_preference") - let cell12 = UserDefaults.standard.integer(forKey: "grid_twelve_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - setCellText(3, dataPoint: cell3) - setCellText(4, dataPoint: cell4) - setCellText(5, dataPoint: cell5) - setCellText(6, dataPoint: cell6) - setCellText(7, dataPoint: cell7) - setCellText(8, dataPoint: cell8) - setCellText(9, dataPoint: cell9) - setCellText(10, dataPoint: cell10) - setCellText(11, dataPoint: cell11) - setCellText(12, dataPoint: cell12) - case 10: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - let cell3 = UserDefaults.standard.integer(forKey: "grid_three_preference") - let cell4 = UserDefaults.standard.integer(forKey: "grid_four_preference") - let cell5 = UserDefaults.standard.integer(forKey: "grid_five_preference") - let cell6 = UserDefaults.standard.integer(forKey: "grid_six_preference") - let cell7 = UserDefaults.standard.integer(forKey: "grid_seven_preference") - let cell8 = UserDefaults.standard.integer(forKey: "grid_eight_preference") - let cell9 = UserDefaults.standard.integer(forKey: "grid_nine_preference") - let cell10 = UserDefaults.standard.integer(forKey: "grid_ten_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - setCellText(3, dataPoint: cell3) - setCellText(4, dataPoint: cell4) - setCellText(5, dataPoint: cell5) - setCellText(6, dataPoint: cell6) - setCellText(7, dataPoint: cell7) - setCellText(8, dataPoint: cell8) - setCellText(9, dataPoint: cell9) - setCellText(10, dataPoint: cell10) - case 8: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - let cell3 = UserDefaults.standard.integer(forKey: "grid_three_preference") - let cell4 = UserDefaults.standard.integer(forKey: "grid_four_preference") - let cell5 = UserDefaults.standard.integer(forKey: "grid_five_preference") - let cell6 = UserDefaults.standard.integer(forKey: "grid_six_preference") - let cell7 = UserDefaults.standard.integer(forKey: "grid_seven_preference") - let cell8 = UserDefaults.standard.integer(forKey: "grid_eight_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - setCellText(3, dataPoint: cell3) - setCellText(4, dataPoint: cell4) - setCellText(5, dataPoint: cell5) - setCellText(6, dataPoint: cell6) - setCellText(7, dataPoint: cell7) - setCellText(8, dataPoint: cell8) - case 4: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - let cell3 = UserDefaults.standard.integer(forKey: "grid_three_preference") - let cell4 = UserDefaults.standard.integer(forKey: "grid_four_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - setCellText(3, dataPoint: cell3) - setCellText(4, dataPoint: cell4) - case 2: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - case 1: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - setCellText(1, dataPoint: cell1) - default: - let cell1 = UserDefaults.standard.integer(forKey: "grid_one_preference") - let cell2 = UserDefaults.standard.integer(forKey: "grid_two_preference") - let cell3 = UserDefaults.standard.integer(forKey: "grid_three_preference") - let cell4 = UserDefaults.standard.integer(forKey: "grid_four_preference") - let cell5 = UserDefaults.standard.integer(forKey: "grid_five_preference") - let cell6 = UserDefaults.standard.integer(forKey: "grid_six_preference") - let cell7 = UserDefaults.standard.integer(forKey: "grid_seven_preference") - let cell8 = UserDefaults.standard.integer(forKey: "grid_eight_preference") - let cell9 = UserDefaults.standard.integer(forKey: "grid_nine_preference") - let cell10 = UserDefaults.standard.integer(forKey: "grid_ten_preference") - let cell11 = UserDefaults.standard.integer(forKey: "grid_eleven_preference") - let cell12 = UserDefaults.standard.integer(forKey: "grid_twelve_preference") - let cell13 = UserDefaults.standard.integer(forKey: "grid_thirteen_preference") - let cell14 = UserDefaults.standard.integer(forKey: "grid_fourteen_preference") - let cell15 = UserDefaults.standard.integer(forKey: "grid_fifteen_preference") - setCellText(1, dataPoint: cell1) - setCellText(2, dataPoint: cell2) - setCellText(3, dataPoint: cell3) - setCellText(4, dataPoint: cell4) - setCellText(5, dataPoint: cell5) - setCellText(6, dataPoint: cell6) - setCellText(7, dataPoint: cell7) - setCellText(8, dataPoint: cell8) - setCellText(9, dataPoint: cell9) - setCellText(10, dataPoint: cell10) - setCellText(11, dataPoint: cell11) - setCellText(12, dataPoint: cell12) - setCellText(13, dataPoint: cell13) - setCellText(14, dataPoint: cell14) - setCellText(15, dataPoint: cell15) - } - } - - func setCellText(_ cellNumber: Int, dataPoint: Int){ - - let label = getLabel(cell: cellNumber) - var value:String = NSLocalizedString("blank_field", comment: "") - var icon: UIImage = getIcon(cell: cellNumber) - - switch (dataPoint){ - case 0: - // Gear - icon = (UIImage(named: "Cog")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.gear != nil { - value = motorcycleData.getgear() - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 1: - // Engine Temperature - icon = (UIImage(named: "Engine-Temp")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.engineTemperature != nil { - var engineTemp:Double = motorcycleData.engineTemperature! - if (engineTemp >= 104.0){ - icon = icon.imageWithColor(color1: UIColor.red) - } - if (UserDefaults.standard.integer(forKey: "temperature_unit_preference") == 1 ){ - engineTemp = Utility.celciusToFahrenheit(engineTemp) - } - value = "\(engineTemp.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 2: - // Ambient Temperature - icon = (UIImage(named: "Thermometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.ambientTemperature != nil { - var ambientTemp:Double = motorcycleData.ambientTemperature! - if(ambientTemp <= 0){ - icon = (UIImage(named: "Snowflake")?.withRenderingMode(.alwaysTemplate))! - icon = icon.imageWithColor(color1: UIColor.blue) - } - if UserDefaults.standard.integer(forKey: "temperature_unit_preference") == 1 { - ambientTemp = Utility.celciusToFahrenheit(ambientTemp) - } - value = "\(ambientTemp.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 3: - // Front Tire Pressure - icon = (UIImage(named: "Tire")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.frontTirePressure != nil { - var frontPressure:Double = motorcycleData.frontTirePressure! - switch UserDefaults.standard.integer(forKey: "pressure_unit_preference"){ - case 1: - frontPressure = Utility.barTokPa(frontPressure) - case 2: - frontPressure = Utility.barTokgf(frontPressure) - case 3: - frontPressure = Utility.barToPsi(frontPressure) - default: - break - } - value = "\(frontPressure.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - if(faults.getFrontTirePressureCriticalActive()){ - icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! - icon = icon.imageWithColor(color1: UIColor.red) - } else if(faults.getRearTirePressureWarningActive()){ - icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! - icon = icon.imageWithColor(color1: UIColor.yellow) - } - case 4: - // Rear Tire Pressure - icon = (UIImage(named: "Tire")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.rearTirePressure != nil { - var rearPressure:Double = motorcycleData.rearTirePressure! - switch UserDefaults.standard.integer(forKey: "pressure_unit_preference"){ - case 1: - rearPressure = Utility.barTokPa(rearPressure) - case 2: - rearPressure = Utility.barTokgf(rearPressure) - case 3: - rearPressure = Utility.barToPsi(rearPressure) - default: - break - } - value = "\(rearPressure.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - if(faults.getRearTirePressureCriticalActive()){ - icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! - icon = icon.imageWithColor(color1: UIColor.red) - } else if(faults.getRearTirePressureWarningActive()){ - icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! - icon = icon.imageWithColor(color1: UIColor.yellow) - } - case 5: - // Odometer - icon = (UIImage(named: "Odometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.odometer != nil { - var odometer:Double = motorcycleData.odometer! - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - odometer = Double(Utility.kmToMiles(Double(odometer))) - } - value = "\(Int(odometer))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 6: - // Voltage - icon = (UIImage(named: "Battery")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.voltage != nil { - value = "\(motorcycleData.voltage!)" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 7: - // Trottle - icon = (UIImage(named: "Signature")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.throttlePosition != nil { - value = "\(Int(round(motorcycleData.throttlePosition!)))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 8: - // Front Brakes - icon = (UIImage(named: "Brakes")?.withRenderingMode(.alwaysTemplate))! - if ((motorcycleData.frontBrake != nil) && motorcycleData.frontBrake != 0) { - value = "\(motorcycleData.frontBrake!)" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 9: - // Rear Brakes - icon = (UIImage(named: "Brakes")?.withRenderingMode(.alwaysTemplate))! - if ((motorcycleData.rearBrake != nil) && motorcycleData.rearBrake != 0) { - value = "\(motorcycleData.rearBrake!)" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 10: - // Ambient Light - icon = (UIImage(named: "Light-bulb")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.ambientLight != nil { - value = "\(motorcycleData.ambientLight!)" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 11: - // Trip 1 - icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.tripOne != nil { - var tripOne:Double = motorcycleData.tripOne! - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - tripOne = Double(Utility.kmToMiles(Double(tripOne))) - } - value = "\(tripOne.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 12: - // Trip 2 - icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.tripTwo != nil { - var tripTwo:Double = motorcycleData.gettripTwo() - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - tripTwo = Double(Utility.kmToMiles(Double(tripTwo))) - } - value = "\(tripTwo.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 13: - // Trip Auto - icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.tripAuto != nil { - var tripAuto:Double = motorcycleData.gettripAuto() - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - tripAuto = Double(Utility.kmToMiles(Double(tripAuto))) - } - value = "\(tripAuto.rounded(toPlaces: 1))" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 14: - // Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.speed != nil { - let speedValue = motorcycleData.speed! - value = "\(Int(speedValue))" - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - value = "\(Int(round(Utility.kmToMiles(speedValue))))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 15: - //Average Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.averageSpeed != nil { - let avgSpeedValue:Double = motorcycleData.averageSpeed! - value = "\(Int(avgSpeedValue))" - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - value = "\(Int(round(Utility.kmToMiles(avgSpeedValue))))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 16: - //Current Consumption - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.currentConsumption != nil { - let currentConsumptionValue:Double = motorcycleData.currentConsumption! - value = "\(currentConsumptionValue.rounded(toPlaces: 1))" - switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ - case 1: - value = "\(Utility.l100ToMpg(currentConsumptionValue).rounded(toPlaces: 1))" - case 2: - value = "\(Utility.l100ToMpgi(currentConsumptionValue).rounded(toPlaces: 1))" - case 3: - value = "\(Utility.l100Tokml(currentConsumptionValue).rounded(toPlaces: 1))" - default: - value = "\(currentConsumptionValue.rounded(toPlaces: 1))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 17: - //Fuel Economy One - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.fuelEconomyOne != nil { - let fuelEconomyOneValue:Double = motorcycleData.fuelEconomyOne! - value = "\(fuelEconomyOneValue.rounded(toPlaces: 1))" - switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ - case 1: - value = "\(Utility.l100ToMpg(fuelEconomyOneValue).rounded(toPlaces: 1))" - case 2: - value = "\(Utility.l100ToMpgi(fuelEconomyOneValue).rounded(toPlaces: 1))" - case 3: - value = "\(Utility.l100Tokml(fuelEconomyOneValue).rounded(toPlaces: 1))" - default: - value = "\(fuelEconomyOneValue.rounded(toPlaces: 1))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 18: - //Fuel Economy Two - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.fuelEconomyTwo != nil { - let fuelEconomyTwoValue:Double = motorcycleData.fuelEconomyTwo! - value = "\(fuelEconomyTwoValue.rounded(toPlaces: 1))" - switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ - case 1: - value = "\(Utility.l100ToMpg(fuelEconomyTwoValue).rounded(toPlaces: 1))" - case 2: - value = "\(Utility.l100ToMpgi(fuelEconomyTwoValue).rounded(toPlaces: 1))" - case 3: - value = "\(Utility.l100Tokml(fuelEconomyTwoValue).rounded(toPlaces: 1))" - default: - value = "\(fuelEconomyTwoValue.rounded(toPlaces: 1))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 19: - //Fuel Range - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.fuelRange != nil { - let fuelRangeValue:Double = motorcycleData.fuelRange! - value = "\(Int(fuelRangeValue))" - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - value = "\(Int(round(Utility.kmToMiles(fuelRangeValue))))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 20: - //Shifts - icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.shifts != nil { - value = "\(motorcycleData.shifts!)" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 21: - //Lean Angle - icon = (UIImage(named: "Angle")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.leanAngle != nil { - value = "\(Int(round(motorcycleData.leanAngle!)))" - } - case 22: - //g-force - icon = (UIImage(named: "Accelerometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.gForce != nil { - value = "\(motorcycleData.gForce!.rounded(toPlaces: 1))" - } - case 23: - //Bearing - icon = (UIImage(named: "Compass")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.bearing != nil { - value = "\(motorcycleData.bearing!)" - if UserDefaults.standard.integer(forKey: "bearing_unit_preference") != 0 { - let bearing = motorcycleData.bearing! - var cardinal = "-"; - if bearing > 331 || bearing <= 28 { - cardinal = NSLocalizedString("north", comment: "") - } else if bearing > 28 && bearing <= 73 { - cardinal = NSLocalizedString("north_east", comment: "") - } else if bearing > 73 && bearing <= 118 { - cardinal = NSLocalizedString("east", comment: "") - } else if bearing > 118 && bearing <= 163 { - cardinal = NSLocalizedString("south_east", comment: "") - } else if bearing > 163 && bearing <= 208 { - cardinal = NSLocalizedString("south", comment: "") - } else if bearing > 208 && bearing <= 253 { - cardinal = NSLocalizedString("south_west", comment: "") - } else if bearing > 253 && bearing <= 298 { - cardinal = NSLocalizedString("west", comment: "") - } else if bearing > 298 && bearing <= 331 { - cardinal = NSLocalizedString("north_west", comment: "") - } else { - cardinal = "-" - } - value = cardinal - } - } - case 24: - //Time - icon = (UIImage(named: "Clock")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.time != nil { - let formatter = DateFormatter() - formatter.dateFormat = "h:mm a" - if UserDefaults.standard.integer(forKey: "time_format_preference") > 0 { - formatter.dateFormat = "HH:mm" - } - value = ("\(formatter.string(from: motorcycleData.time!))") - } - case 25: - //Barometric Pressure - icon = (UIImage(named: "Barometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.barometricPressure != nil { - value = "\(Int(round(motorcycleData.barometricPressure!)))" - } - case 26: - //GPS speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.location != nil { - var gpsSpeed:String = "0" - if motorcycleData.location!.speed >= 0{ - gpsSpeed = "\(motorcycleData.location!.speed * 3.6)" - let gpsSpeedValue:Double = motorcycleData.location!.speed * 3.6 - gpsSpeed = "\(Int(round(gpsSpeedValue)))" - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - gpsSpeed = "\(Int(round(Utility.kmToMiles(gpsSpeedValue))))" - } - value = gpsSpeed - } - } - case 27: - //Altitude - icon = (UIImage(named: "Mountain")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.location != nil { - var altitude:String = "\(Int(round(motorcycleData.location!.altitude)))" - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - altitude = "\(Int(round(Utility.mtoFeet(motorcycleData.location!.altitude))))" - } - value = altitude - } - case 28: - //Sunrise/Sunset - icon = (UIImage(named: "Sun")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.location != nil { - let today = Date() - let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)! - let solar = Solar(for: yesterday, coordinate: motorcycleData.location!.coordinate) - let sunrise = solar?.sunrise - let sunset = solar?.sunset - - if(today > sunset!){ - icon = (UIImage(named: "Moon")?.withRenderingMode(.alwaysTemplate))! - } - let formatter = DateFormatter() - formatter.dateFormat = "h:mm a" - if UserDefaults.standard.integer(forKey: "time_format_preference") > 0 { - formatter.dateFormat = "HH:mm" - } - value = ("\(formatter.string(from: sunrise!))/\(formatter.string(from: sunset!))") - } - case 29: - //RPM - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - if ((motorcycleData.rpm != nil) && motorcycleData.rpm != 0) { - value = "\(motorcycleData.rpm!)" - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 30: - //Lean Angle Bike - icon = (UIImage(named: "Angle")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.leanAngleBike != nil { - value = "\(Int(round(motorcycleData.leanAngleBike!)))" - } - case 31: - // Rear Wheel Speed - icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.rearSpeed != nil { - let speedValue = motorcycleData.rearSpeed! - value = "\(Int(speedValue))" - if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { - value = "\(Int(round(Utility.kmToMiles(speedValue))))" - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - case 32: - // Device Battery - icon = (UIImage(named: "Battery-Empty")?.withRenderingMode(.alwaysTemplate))! - if motorcycleData.localBattery != nil { - let batteryPct = motorcycleData.localBattery! - value = "\(Int(batteryPct))" - - if(batteryPct > 95){ - icon = (UIImage(named: "Battery-Full")?.withRenderingMode(.alwaysTemplate))! - } else if(batteryPct > 75 && batteryPct < 95){ - icon = (UIImage(named: "Battery-Three-Quarters")?.withRenderingMode(.alwaysTemplate))! - } else if(batteryPct > 50 && batteryPct < 75){ - icon = (UIImage(named: "Battery-Half")?.withRenderingMode(.alwaysTemplate))! - } else if(batteryPct > 25 && batteryPct < 50){ - icon = (UIImage(named: "Battery-Quarter")?.withRenderingMode(.alwaysTemplate))! - } else if(batteryPct > 0 && batteryPct < 25){ - icon = (UIImage(named: "Battery-Empty")?.withRenderingMode(.alwaysTemplate))! - icon = icon.imageWithColor(color1: UIColor.red) - } - } else { - value = NSLocalizedString("blank_field", comment: "") - } - default: - NSLog("MainCollectionViewController: Unknown : \(dataPoint)") + for i in 1...cellCount { + setCell(i) } + + } + + func setCell(_ cellNumber: Int){ + let label = MotorcycleData.getLabel(dataPoint: getCellDataPoint(cell: cellNumber)) + var value:String = MotorcycleData.getValue(dataPoint: getCellDataPoint(cell: cellNumber)) + var icon: UIImage = MotorcycleData.getIcon(dataPoint: getCellDataPoint(cell: cellNumber)) + if let cell = self.collectionView.cellForItem(at: IndexPath(row: cellNumber - 1, section: 0) as IndexPath) as? MainCollectionViewCell{ cell.setLabel(label: label) cell.setValue(value: value) @@ -1871,14 +1034,16 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource var dataArray = [UInt8](repeating: 0, count: dataLength) (data as NSData).getBytes(&dataArray, length: dataLength * MemoryLayout.size) - var messageHexString = "" - for i in 0 ..< dataLength { - messageHexString += String(format: "%02X", dataArray[i]) - if i < dataLength - 1 { - messageHexString += "," + if UserDefaults.standard.bool(forKey: "debug_logging_preference") { + var messageHexString = "" + for i in 0 ..< dataLength { + messageHexString += String(format: "%02X", dataArray[i]) + if i < dataLength - 1 { + messageHexString += "," + } } + NSLog("MainCollectionViewController: Command Response Received: \(messageHexString)") } - NSLog("MainCollectionViewController: Command Response Received: \(messageHexString)") switch (dataArray[0]){ case 0x57: diff --git a/WunderLINQ/hardware/WLQ/MotorcycleData.swift b/WunderLINQ/hardware/WLQ/MotorcycleData.swift index df8090c..8cec68b 100644 --- a/WunderLINQ/hardware/WLQ/MotorcycleData.swift +++ b/WunderLINQ/hardware/WLQ/MotorcycleData.swift @@ -384,4 +384,718 @@ class MotorcycleData { self.rearSpeed = nil self.localBattery = nil } + + public let DATA_GEAR = 0 + public let DATA_ENGINE_TEMP = 1 + public let DATA_AIR_TEMP = 2 + public let DATA_FRONT_RDC = 3 + public let DATA_REAR_RDC = 4 + public let DATA_ODOMETER = 5 + public let DATA_VOLTAGE = 6 + public let DATA_THROTTLE = 7 + public let DATA_FRONT_BRAKE = 8 + public let DATA_REAR_BRAKE = 9 + public let DATA_AMBIENT_LIGHT = 10 + public let DATA_TRIP_ONE = 11 + public let DATA_TRIP_TWO = 12 + public let DATA_TRIP_AUTO = 13 + public let DATA_SPEED = 14 + public let DATA_AVG_SPEED = 15 + public let DATA_CURRENT_CONSUMPTION = 16 + public let DATA_ECONOMY_ONE = 17 + public let DATA_ECONOMY_TWO = 18 + public let DATA_RANGE = 19 + public let DATA_SHIFTS = 20 + public let DATA_LEAN_DEVICE = 21 + public let DATA_GFORCE_DEVICE = 22 + public let DATA_BEARING_DEVICE = 23 + public let DATA_TIME_DEVICE = 24 + public let DATA_BAROMETRIC_DEVICE = 25 + public let DATA_SPEED_DEVICE = 26 + public let DATA_ALTITUDE_DEVICE = 27 + public let DATA_SUN_DEVICE = 28 + public let DATA_RPM = 29 + public let DATA_LEAN = 30 + public let DATA_REAR_SPEED = 31 + public let DATA_BATTERY_DEVICE = 32 + + class func getLabel(dataPoint: Int) -> String { + var label:String = "" + var temperatureUnit = "C" + var heightUnit = "m" + var distanceUnit = "km" + var distanceTimeUnit = "kmh" + var consumptionUnit = "L/100" + var pressureUnit = "psi" + // Pressure Unit + switch UserDefaults.standard.integer(forKey: "pressure_unit_preference"){ + case 0: + pressureUnit = "bar" + case 1: + pressureUnit = "kPa" + case 2: + pressureUnit = "kg-f" + case 3: + pressureUnit = "psi" + default: + NSLog("MainCollectionViewController: Unknown pressure unit setting") + } + if UserDefaults.standard.integer(forKey: "temperature_unit_preference") == 1 { + temperatureUnit = "F" + } + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + distanceUnit = "mi" + distanceTimeUnit = "mph" + heightUnit = "ft" + } + switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ + case 0: + consumptionUnit = "L/100" + case 1: + consumptionUnit = "mpg" + case 2: + consumptionUnit = "mpg" + case 3: + consumptionUnit = "km/L" + default: + NSLog("MainCollectionViewController: Unknown consumption unit setting") + } + + switch (dataPoint){ + case MotorcycleData.shared.DATA_GEAR: + // Gear + label = NSLocalizedString("gear_header", comment: "") + case MotorcycleData.shared.DATA_ENGINE_TEMP: + // Engine Temperature + label = NSLocalizedString("enginetemp_header", comment: "") + " (" + temperatureUnit + ")" + case MotorcycleData.shared.DATA_AIR_TEMP: + // Ambient Temperature + label = NSLocalizedString("ambienttemp_header", comment: "") + " (" + temperatureUnit + ")" + case MotorcycleData.shared.DATA_FRONT_RDC: + // Front Tire Pressure + label = NSLocalizedString("frontpressure_header", comment: "") + " (" + pressureUnit + ")" + case MotorcycleData.shared.DATA_REAR_RDC: + // Rear Tire Pressure + label = NSLocalizedString("rearpressure_header", comment: "") + " (" + pressureUnit + ")" + case MotorcycleData.shared.DATA_ODOMETER: + // Odometer + label = NSLocalizedString("odometer_header", comment: "") + " (" + distanceUnit + ")" + case MotorcycleData.shared.DATA_VOLTAGE: + // Voltage + label = NSLocalizedString("voltage_header", comment: "") + " (V)" + case MotorcycleData.shared.DATA_THROTTLE: + // Trottle + label = NSLocalizedString("throttle_header", comment: "") + " (%)" + case MotorcycleData.shared.DATA_FRONT_BRAKE: + // Front Brakes + label = NSLocalizedString("frontbrakes_header", comment: "") + case MotorcycleData.shared.DATA_REAR_BRAKE: + // Rear Brakes + label = NSLocalizedString("rearbrakes_header", comment: "") + case MotorcycleData.shared.DATA_AMBIENT_LIGHT: + // Ambient Light + label = NSLocalizedString("ambientlight_header", comment: "") + case MotorcycleData.shared.DATA_TRIP_ONE: + // Trip 1 + label = NSLocalizedString("tripone_header", comment: "") + " (" + distanceUnit + ")" + case MotorcycleData.shared.DATA_TRIP_TWO: + // Trip 2 + label = NSLocalizedString("triptwo_header", comment: "") + " (" + distanceUnit + ")" + case MotorcycleData.shared.DATA_TRIP_AUTO: + // Trip Auto + label = NSLocalizedString("tripauto_header", comment: "") + " (" + distanceUnit + ")" + case MotorcycleData.shared.DATA_SPEED: + // Speed + label = NSLocalizedString("speed_header", comment: "") + " (" + distanceTimeUnit + ")" + case MotorcycleData.shared.DATA_AVG_SPEED: + //Average Speed + label = NSLocalizedString("avgspeed_header", comment: "") + " (" + distanceTimeUnit + ")" + case MotorcycleData.shared.DATA_CURRENT_CONSUMPTION: + //Current Consumption + label = NSLocalizedString("cconsumption_header", comment: "") + " (" + consumptionUnit + ")" + case MotorcycleData.shared.DATA_ECONOMY_ONE: + //Fuel Economy One + label = NSLocalizedString("fueleconomyone_header", comment: "") + " (" + consumptionUnit + ")" + case MotorcycleData.shared.DATA_ECONOMY_TWO: + //Fuel Economy Two + label = NSLocalizedString("fueleconomytwo_header", comment: "") + " (" + consumptionUnit + ")" + case MotorcycleData.shared.DATA_RANGE: + //Fuel Range + label = NSLocalizedString("fuelrange_header", comment: "") + " (" + distanceUnit + ")" + case MotorcycleData.shared.DATA_SHIFTS: + //Shifts + label = NSLocalizedString("shifts_header", comment: "") + case MotorcycleData.shared.DATA_LEAN_DEVICE: + //Lean Angle + label = NSLocalizedString("leanangle_header", comment: "") + case MotorcycleData.shared.DATA_GFORCE_DEVICE: + //g-force + label = NSLocalizedString("gforce_header", comment: "") + case MotorcycleData.shared.DATA_BEARING_DEVICE: + //bearing + label = NSLocalizedString("bearing_header", comment: "") + case MotorcycleData.shared.DATA_TIME_DEVICE: + //time + label = NSLocalizedString("time_header", comment: "") + case MotorcycleData.shared.DATA_BAROMETRIC_DEVICE: + //barometric pressure + label = NSLocalizedString("barometric_header", comment: "") + " (mBar)" + case MotorcycleData.shared.DATA_SPEED_DEVICE: + //GPS Speed + label = NSLocalizedString("gpsspeed_header", comment: "") + " (" + distanceTimeUnit + ")" + case MotorcycleData.shared.DATA_ALTITUDE_DEVICE: + //altitude + label = NSLocalizedString("altitude_header", comment: "") + " (" + heightUnit + ")" + case MotorcycleData.shared.DATA_SUN_DEVICE: + //Sunrise/Sunset + label = NSLocalizedString("sunrisesunset_header", comment: "") + case MotorcycleData.shared.DATA_RPM: + //RPM + label = NSLocalizedString("rpm_header", comment: "") + case MotorcycleData.shared.DATA_LEAN: + //Lean Angle + label = NSLocalizedString("leanangle_bike_header", comment: "") + case MotorcycleData.shared.DATA_REAR_SPEED: + //Rear Wheel Speed + label = NSLocalizedString("rearwheel_speed_header", comment: "") + case MotorcycleData.shared.DATA_BATTERY_DEVICE: + //Device Battery + label = NSLocalizedString("local_battery_header", comment: "") + default: + NSLog("MotorcycleData: Unknown : \(dataPoint)") + } + + return label + } + + class func getIcon(dataPoint: Int) -> UIImage { + var icon:UIImage = (UIImage(named: "Cog")?.withRenderingMode(.alwaysTemplate))! + switch (dataPoint){ + case MotorcycleData.shared.DATA_GEAR: + // Gear + icon = (UIImage(named: "Cog")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_ENGINE_TEMP: + // Engine Temperature + icon = (UIImage(named: "Engine-Temp")?.withRenderingMode(.alwaysTemplate))! + if (MotorcycleData.shared.engineTemperature != nil) { + var engineTemp:Double = MotorcycleData.shared.engineTemperature! + if (engineTemp >= 104.0){ + icon = icon.imageWithColor(color1: UIColor.red) + } + } + case MotorcycleData.shared.DATA_AIR_TEMP: + // Ambient Temperature + icon = (UIImage(named: "Thermometer")?.withRenderingMode(.alwaysTemplate))! + if (MotorcycleData.shared.ambientTemperature != nil) { + var ambientTemp:Double = MotorcycleData.shared.ambientTemperature! + if(ambientTemp <= 0){ + icon = (UIImage(named: "Snowflake")?.withRenderingMode(.alwaysTemplate))! + icon = icon.imageWithColor(color1: UIColor.blue) + } + } + case MotorcycleData.shared.DATA_FRONT_RDC: + // Front Tire Pressure + icon = (UIImage(named: "Tire")?.withRenderingMode(.alwaysTemplate))! + if(Faults.shared.getFrontTirePressureCriticalActive()){ + icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! + icon = icon.imageWithColor(color1: UIColor.red) + } else if(Faults.shared.getRearTirePressureWarningActive()){ + icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! + icon = icon.imageWithColor(color1: UIColor.yellow) + } + case MotorcycleData.shared.DATA_REAR_RDC: + // Rear Tire Pressure + icon = (UIImage(named: "Tire")?.withRenderingMode(.alwaysTemplate))! + if(Faults.shared.getRearTirePressureCriticalActive()){ + icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! + icon = icon.imageWithColor(color1: UIColor.red) + } else if(Faults.shared.getRearTirePressureWarningActive()){ + icon = (UIImage(named: "Tire-Alert")?.withRenderingMode(.alwaysTemplate))! + icon = icon.imageWithColor(color1: UIColor.yellow) + } + case MotorcycleData.shared.DATA_ODOMETER: + // Odometer + icon = (UIImage(named: "Odometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_VOLTAGE: + // Voltage + icon = (UIImage(named: "Battery")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_THROTTLE: + // Trottle + icon = (UIImage(named: "Signature")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_FRONT_BRAKE: + // Front Brakes + icon = (UIImage(named: "Brakes")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_REAR_BRAKE: + // Rear Brakes + icon = (UIImage(named: "Brakes")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_AMBIENT_LIGHT: + // Ambient Light + icon = (UIImage(named: "Light-bulb")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_TRIP_ONE: + // Trip 1 + icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_TRIP_TWO: + // Trip 2 + icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_TRIP_AUTO: + // Trip Auto + icon = (UIImage(named: "Suitcase")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_SPEED: + // Speed + icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_AVG_SPEED: + //Average Speed + icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_CURRENT_CONSUMPTION: + //Current Consumption + icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_ECONOMY_ONE: + //Fuel Economy One + icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_ECONOMY_TWO: + //Fuel Economy Two + icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_RANGE: + //Fuel Range + icon = (UIImage(named: "Gas-pump")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_SHIFTS: + //Shifts + icon = (UIImage(named: "Arrows-alt")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_LEAN_DEVICE: + //Lean Angle Device + icon = (UIImage(named: "Angle")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_GFORCE_DEVICE: + //g-force + icon = (UIImage(named: "Accelerometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_BEARING_DEVICE: + //bearing + icon = (UIImage(named: "Compass")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_TIME_DEVICE: + //time + icon = (UIImage(named: "Clock")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_BAROMETRIC_DEVICE: + //barometric pressure + icon = (UIImage(named: "Barometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_SPEED_DEVICE: + //GPS Speed + icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_ALTITUDE_DEVICE: + //altitude + icon = (UIImage(named: "Mountain")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_SUN_DEVICE: + //Sunrise/Sunset + icon = (UIImage(named: "Sun")?.withRenderingMode(.alwaysTemplate))! + if (MotorcycleData.shared.location != nil) { + let today = Date() + let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)! + let solar = Solar(for: yesterday, coordinate: MotorcycleData.shared.location!.coordinate) + let sunset = solar?.sunset + if(today > sunset!){ + icon = (UIImage(named: "Moon")?.withRenderingMode(.alwaysTemplate))! + } + } + case MotorcycleData.shared.DATA_RPM: + //RPM + icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_LEAN: + //Lean Angle Bike + icon = (UIImage(named: "Angle")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_REAR_SPEED: + //Rear Wheel Speed + icon = (UIImage(named: "Tachometer")?.withRenderingMode(.alwaysTemplate))! + case MotorcycleData.shared.DATA_BATTERY_DEVICE: + //Device Battery + icon = (UIImage(named: "Battery-Empty")?.withRenderingMode(.alwaysTemplate))! + if (MotorcycleData.shared.localBattery != nil) { + let batteryPct = MotorcycleData.shared.localBattery! + if(batteryPct > 95){ + icon = (UIImage(named: "Battery-Full")?.withRenderingMode(.alwaysTemplate))! + } else if(batteryPct > 75 && batteryPct < 95){ + icon = (UIImage(named: "Battery-Three-Quarters")?.withRenderingMode(.alwaysTemplate))! + } else if(batteryPct > 50 && batteryPct < 75){ + icon = (UIImage(named: "Battery-Half")?.withRenderingMode(.alwaysTemplate))! + } else if(batteryPct > 25 && batteryPct < 50){ + icon = (UIImage(named: "Battery-Quarter")?.withRenderingMode(.alwaysTemplate))! + } else if(batteryPct > 0 && batteryPct < 25){ + icon = (UIImage(named: "Battery-Empty")?.withRenderingMode(.alwaysTemplate))! + icon = icon.imageWithColor(color1: UIColor.red) + } + } + default: + NSLog("MotorcycleData: Unknown : \(dataPoint)") + } + + return icon + } + + class func getValue(dataPoint: Int) -> String { + var value:String = NSLocalizedString("blank_field", comment: "") + switch (dataPoint){ + case MotorcycleData.shared.DATA_GEAR: + // Gear + if MotorcycleData.shared.gear != nil { + value = MotorcycleData.shared.getgear() + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_ENGINE_TEMP: + // Engine Temperature + if MotorcycleData.shared.engineTemperature != nil { + var engineTemp:Double = MotorcycleData.shared.engineTemperature! + if (UserDefaults.standard.integer(forKey: "temperature_unit_preference") == 1 ){ + engineTemp = Utility.celciusToFahrenheit(engineTemp) + } + value = "\(engineTemp.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_AIR_TEMP: + // Ambient Temperature + if MotorcycleData.shared.ambientTemperature != nil { + var ambientTemp:Double = MotorcycleData.shared.ambientTemperature! + if UserDefaults.standard.integer(forKey: "temperature_unit_preference") == 1 { + ambientTemp = Utility.celciusToFahrenheit(ambientTemp) + } + value = "\(ambientTemp.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_FRONT_RDC: + // Front Tire Pressure + if MotorcycleData.shared.frontTirePressure != nil { + var frontPressure:Double = MotorcycleData.shared.frontTirePressure! + switch UserDefaults.standard.integer(forKey: "pressure_unit_preference"){ + case 1: + frontPressure = Utility.barTokPa(frontPressure) + case 2: + frontPressure = Utility.barTokgf(frontPressure) + case 3: + frontPressure = Utility.barToPsi(frontPressure) + default: + break + } + value = "\(frontPressure.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_REAR_RDC: + // Rear Tire Pressure + if MotorcycleData.shared.rearTirePressure != nil { + var rearPressure:Double = MotorcycleData.shared.rearTirePressure! + switch UserDefaults.standard.integer(forKey: "pressure_unit_preference"){ + case 1: + rearPressure = Utility.barTokPa(rearPressure) + case 2: + rearPressure = Utility.barTokgf(rearPressure) + case 3: + rearPressure = Utility.barToPsi(rearPressure) + default: + break + } + value = "\(rearPressure.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_ODOMETER: + // Odometer + if MotorcycleData.shared.odometer != nil { + var odometer:Double = MotorcycleData.shared.odometer! + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + odometer = Double(Utility.kmToMiles(Double(odometer))) + } + value = "\(Int(odometer))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_VOLTAGE: + // Voltage + if MotorcycleData.shared.voltage != nil { + value = "\(MotorcycleData.shared.voltage!)" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_THROTTLE: + // Trottle + if MotorcycleData.shared.throttlePosition != nil { + value = "\(Int(round(MotorcycleData.shared.throttlePosition!)))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_FRONT_BRAKE: + // Front Brakes + if ((MotorcycleData.shared.frontBrake != nil) && MotorcycleData.shared.frontBrake != 0) { + value = "\(MotorcycleData.shared.frontBrake!)" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_REAR_BRAKE: + // Rear Brakes + if ((MotorcycleData.shared.rearBrake != nil) && MotorcycleData.shared.rearBrake != 0) { + value = "\(MotorcycleData.shared.rearBrake!)" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_AMBIENT_LIGHT: + // Ambient Light + if MotorcycleData.shared.ambientLight != nil { + value = "\(MotorcycleData.shared.ambientLight!)" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_TRIP_ONE: + // Trip 1 + if MotorcycleData.shared.tripOne != nil { + var tripOne:Double = MotorcycleData.shared.tripOne! + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + tripOne = Double(Utility.kmToMiles(Double(tripOne))) + } + value = "\(tripOne.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_TRIP_TWO: + // Trip 2 + if MotorcycleData.shared.tripTwo != nil { + var tripTwo:Double = MotorcycleData.shared.gettripTwo() + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + tripTwo = Double(Utility.kmToMiles(Double(tripTwo))) + } + value = "\(tripTwo.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_TRIP_AUTO: + // Trip Auto + if MotorcycleData.shared.tripAuto != nil { + var tripAuto:Double = MotorcycleData.shared.gettripAuto() + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + tripAuto = Double(Utility.kmToMiles(Double(tripAuto))) + } + value = "\(tripAuto.rounded(toPlaces: 1))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_SPEED: + // Speed + if MotorcycleData.shared.speed != nil { + let speedValue = MotorcycleData.shared.speed! + value = "\(Int(speedValue))" + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + value = "\(Int(round(Utility.kmToMiles(speedValue))))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_AVG_SPEED: + //Average Speed + if MotorcycleData.shared.averageSpeed != nil { + let avgSpeedValue:Double = MotorcycleData.shared.averageSpeed! + value = "\(Int(avgSpeedValue))" + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + value = "\(Int(round(Utility.kmToMiles(avgSpeedValue))))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_CURRENT_CONSUMPTION: + //Current Consumption + if MotorcycleData.shared.currentConsumption != nil { + let currentConsumptionValue:Double = MotorcycleData.shared.currentConsumption! + value = "\(currentConsumptionValue.rounded(toPlaces: 1))" + switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ + case 1: + value = "\(Utility.l100ToMpg(currentConsumptionValue).rounded(toPlaces: 1))" + case 2: + value = "\(Utility.l100ToMpgi(currentConsumptionValue).rounded(toPlaces: 1))" + case 3: + value = "\(Utility.l100Tokml(currentConsumptionValue).rounded(toPlaces: 1))" + default: + value = "\(currentConsumptionValue.rounded(toPlaces: 1))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_ECONOMY_ONE: + //Fuel Economy One + if MotorcycleData.shared.fuelEconomyOne != nil { + let fuelEconomyOneValue:Double = MotorcycleData.shared.fuelEconomyOne! + value = "\(fuelEconomyOneValue.rounded(toPlaces: 1))" + switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ + case 1: + value = "\(Utility.l100ToMpg(fuelEconomyOneValue).rounded(toPlaces: 1))" + case 2: + value = "\(Utility.l100ToMpgi(fuelEconomyOneValue).rounded(toPlaces: 1))" + case 3: + value = "\(Utility.l100Tokml(fuelEconomyOneValue).rounded(toPlaces: 1))" + default: + value = "\(fuelEconomyOneValue.rounded(toPlaces: 1))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_ECONOMY_TWO: + //Fuel Economy Two + if MotorcycleData.shared.fuelEconomyTwo != nil { + let fuelEconomyTwoValue:Double = MotorcycleData.shared.fuelEconomyTwo! + value = "\(fuelEconomyTwoValue.rounded(toPlaces: 1))" + switch UserDefaults.standard.integer(forKey: "consumption_unit_preference"){ + case 1: + value = "\(Utility.l100ToMpg(fuelEconomyTwoValue).rounded(toPlaces: 1))" + case 2: + value = "\(Utility.l100ToMpgi(fuelEconomyTwoValue).rounded(toPlaces: 1))" + case 3: + value = "\(Utility.l100Tokml(fuelEconomyTwoValue).rounded(toPlaces: 1))" + default: + value = "\(fuelEconomyTwoValue.rounded(toPlaces: 1))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_RANGE: + //Fuel Range + if MotorcycleData.shared.fuelRange != nil { + let fuelRangeValue:Double = MotorcycleData.shared.fuelRange! + value = "\(Int(fuelRangeValue))" + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + value = "\(Int(round(Utility.kmToMiles(fuelRangeValue))))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_SHIFTS: + //Shifts + if MotorcycleData.shared.shifts != nil { + value = "\(MotorcycleData.shared.shifts!)" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_LEAN_DEVICE: + //Lean Angle + if MotorcycleData.shared.leanAngle != nil { + value = "\(Int(round(MotorcycleData.shared.leanAngle!)))" + } + case MotorcycleData.shared.DATA_GFORCE_DEVICE: + //g-force + if MotorcycleData.shared.gForce != nil { + value = "\(MotorcycleData.shared.gForce!.rounded(toPlaces: 1))" + } + case MotorcycleData.shared.DATA_BEARING_DEVICE: + //Bearing + if MotorcycleData.shared.bearing != nil { + value = "\(MotorcycleData.shared.bearing!)" + if UserDefaults.standard.integer(forKey: "bearing_unit_preference") != 0 { + let bearing = MotorcycleData.shared.bearing! + var cardinal = "-"; + if bearing > 331 || bearing <= 28 { + cardinal = NSLocalizedString("north", comment: "") + } else if bearing > 28 && bearing <= 73 { + cardinal = NSLocalizedString("north_east", comment: "") + } else if bearing > 73 && bearing <= 118 { + cardinal = NSLocalizedString("east", comment: "") + } else if bearing > 118 && bearing <= 163 { + cardinal = NSLocalizedString("south_east", comment: "") + } else if bearing > 163 && bearing <= 208 { + cardinal = NSLocalizedString("south", comment: "") + } else if bearing > 208 && bearing <= 253 { + cardinal = NSLocalizedString("south_west", comment: "") + } else if bearing > 253 && bearing <= 298 { + cardinal = NSLocalizedString("west", comment: "") + } else if bearing > 298 && bearing <= 331 { + cardinal = NSLocalizedString("north_west", comment: "") + } else { + cardinal = "-" + } + value = cardinal + } + } + case MotorcycleData.shared.DATA_TIME_DEVICE: + //Time + if MotorcycleData.shared.time != nil { + let formatter = DateFormatter() + formatter.dateFormat = "h:mm a" + if UserDefaults.standard.integer(forKey: "time_format_preference") > 0 { + formatter.dateFormat = "HH:mm" + } + value = ("\(formatter.string(from: MotorcycleData.shared.time!))") + } + case MotorcycleData.shared.DATA_BAROMETRIC_DEVICE: + //Barometric Pressure + if MotorcycleData.shared.barometricPressure != nil { + value = "\(Int(round(MotorcycleData.shared.barometricPressure!)))" + } + case MotorcycleData.shared.DATA_SPEED_DEVICE: + //GPS speed + if MotorcycleData.shared.location != nil { + var gpsSpeed:String = "0" + if MotorcycleData.shared.location!.speed >= 0{ + gpsSpeed = "\(MotorcycleData.shared.location!.speed * 3.6)" + let gpsSpeedValue:Double = MotorcycleData.shared.location!.speed * 3.6 + gpsSpeed = "\(Int(round(gpsSpeedValue)))" + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + gpsSpeed = "\(Int(round(Utility.kmToMiles(gpsSpeedValue))))" + } + value = gpsSpeed + } + } + case MotorcycleData.shared.DATA_ALTITUDE_DEVICE: + //Altitude + if MotorcycleData.shared.location != nil { + var altitude:String = "\(Int(round(MotorcycleData.shared.location!.altitude)))" + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + altitude = "\(Int(round(Utility.mtoFeet(MotorcycleData.shared.location!.altitude))))" + } + value = altitude + } + case MotorcycleData.shared.DATA_SUN_DEVICE: + //Sunrise/Sunset + if MotorcycleData.shared.location != nil { + let today = Date() + let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)! + let solar = Solar(for: yesterday, coordinate: MotorcycleData.shared.location!.coordinate) + let sunrise = solar?.sunrise + let sunset = solar?.sunset + let formatter = DateFormatter() + formatter.dateFormat = "h:mm a" + if UserDefaults.standard.integer(forKey: "time_format_preference") > 0 { + formatter.dateFormat = "HH:mm" + } + value = ("\(formatter.string(from: sunrise!))/\(formatter.string(from: sunset!))") + } + case MotorcycleData.shared.DATA_RPM: + //RPM + if ((MotorcycleData.shared.rpm != nil) && MotorcycleData.shared.rpm != 0) { + value = "\(MotorcycleData.shared.rpm!)" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_LEAN: + //Lean Angle Bike + if MotorcycleData.shared.leanAngleBike != nil { + value = "\(Int(round(MotorcycleData.shared.leanAngleBike!)))" + } + case MotorcycleData.shared.DATA_REAR_SPEED: + // Rear Wheel Speed + if MotorcycleData.shared.rearSpeed != nil { + let speedValue = MotorcycleData.shared.rearSpeed! + value = "\(Int(speedValue))" + if UserDefaults.standard.integer(forKey: "distance_unit_preference") == 1 { + value = "\(Int(round(Utility.kmToMiles(speedValue))))" + } + } else { + value = NSLocalizedString("blank_field", comment: "") + } + case MotorcycleData.shared.DATA_BATTERY_DEVICE: + // Device Battery + if MotorcycleData.shared.localBattery != nil { + let batteryPct = MotorcycleData.shared.localBattery! + value = "\(Int(batteryPct))" + } else { + value = NSLocalizedString("blank_field", comment: "") + } + default: + NSLog("MotorcycleData: Unknown : \(dataPoint)") + } + + return value + } }