Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Extension for UIDevice #102 #104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions PiwikTracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1F3CA58C1E09A30600121FDC /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F3CA58B1E09A30600121FDC /* Queue.swift */; };
1FCA6D451DBE0B2F0033F01C /* PiwikTracker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FCA6D3B1DBE0B2F0033F01C /* PiwikTracker.framework */; };
1FCA6D4C1DBE0B2F0033F01C /* PiwikTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FCA6D3E1DBE0B2F0033F01C /* PiwikTracker.h */; settings = {ATTRIBUTES = (Public, ); }; };
2DE767EF1E0890CA00CA3CD7 /* Device.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DE767EE1E0890CA00CA3CD7 /* Device.swift */; };
82A551CEC50ABB2EAD50FC4E /* Pods_PiwikTrackerTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 68F0C25DEF98C13D32CC46DD /* Pods_PiwikTrackerTests.framework */; };
/* End PBXBuildFile section */

Expand All @@ -42,6 +43,7 @@
1FCA6D3F1DBE0B2F0033F01C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1FCA6D441DBE0B2F0033F01C /* PiwikTrackerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PiwikTrackerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1FCA6D4B1DBE0B2F0033F01C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2DE767EE1E0890CA00CA3CD7 /* Device.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Device.swift; sourceTree = "<group>"; };
68F0C25DEF98C13D32CC46DD /* Pods_PiwikTrackerTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PiwikTrackerTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8F688B0F18F298B5E61552AC /* Pods-PiwikTrackerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PiwikTrackerTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-PiwikTrackerTests/Pods-PiwikTrackerTests.release.xcconfig"; sourceTree = "<group>"; };
FD284F50B998823EAFB0CEE9 /* Pods-PiwikTrackerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PiwikTrackerTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PiwikTrackerTests/Pods-PiwikTrackerTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,6 +107,7 @@
1F1949F11E17A91100458199 /* MemoryQueue.swift */,
1FCA6D3E1DBE0B2F0033F01C /* PiwikTracker.h */,
1FCA6D3F1DBE0B2F0033F01C /* Info.plist */,
2DE767EE1E0890CA00CA3CD7 /* Device.swift */,
);
path = PiwikTracker;
sourceTree = "<group>";
Expand Down Expand Up @@ -299,6 +302,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2DE767EF1E0890CA00CA3CD7 /* Device.swift in Sources */,
1F092C161E225E0200394B30 /* Event.swift in Sources */,
1F092C141E224C3E00394B30 /* PiwikUserDefaults.swift in Sources */,
1F1949F21E17A91100458199 /* MemoryQueue.swift in Sources */,
Expand Down
154 changes: 154 additions & 0 deletions PiwikTracker/Device.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import UIKit

internal struct Device {

/// Creates an returns a new device object representing the current device
static func makeCurrentDevice() -> Device {
Copy link
Member

Choose a reason for hiding this comment

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

Not overly happy with the word make here but it's not a huge issue.

let platform = currentPlatform()
let humanReadablePlatformName = humanReadablePlatformNameForCurrentDevice()
let os = osVersionForCurrentDevice()
let screenSize = screenSizeForCurrentDevice()
let nativeScreenSize = nativeScreenSizeForCurrentDevice()
return Device(platform: platform,
humanReadablePlatformName: humanReadablePlatformName,
osVersion: os,
screenSize: screenSize,
nativeScreenSize: nativeScreenSize)
}

/// The platform name of the device i.e. "iPhone1,1" or "iPad3,6"
let platform: String

/// A human readable version of the platform name i.e. "iPhone 6 Plus" or "iPad Air 2 (WiFi)"
/// Will be nil if no human readable string was found.
let humanReadablePlatformName: String?

/// The version number of the OS as String i.e. "1.2" or "9.4"
let osVersion: String

// The screen size in points
let screenSize: CGSize

// The screen size in pixels
let nativeScreenSize: CGSize

/// The platform name of the current device i.e. "iPhone1,1" or "iPad3,6"
private static func currentPlatform() -> String {
var size = 0
sysctlbyname("hw.machine", nil, &size, nil, 0)
var machine = [CChar](repeating: 0, count: Int(size))
sysctlbyname("hw.machine", &machine, &size, nil, 0)
return String(cString: machine)
}

/// Returns a human readable version of the current platform name i.e. "iPhone 6 Plus" or "iPad Air 2 (WiFi)"
/// Will return nil, if no human readable string could be found for the current platform
private static func humanReadablePlatformNameForCurrentDevice() -> String? {
let platform = currentPlatform()
switch platform {

// iPhone
case "iPhone1,1": return "iPhone 1G"
case "iPhone1,2": return "iPhone 3G"
case "iPhone2,1": return "iPhone 3GS"
case "iPhone3,1": return "iPhone 4"
case "iPhone3,2": return "iPhone 4 (Revision A)"
case "iPhone3,3": return "Verizon iPhone 4"
case "iPhone4,1": return "iPhone 4S"
case "iPhone5,1": return "iPhone 5 (GSM)"
case "iPhone5,2": return "iPhone 5 (GSM+CDMA)"
case "iPhone5,3": return "iPhone 5c (GSM)"
case "iPhone5,4": return "iPhone 5c (Global)"
case "iPhone6,1": return "iPhone 5s (GSM)"
case "iPhone6,2": return "iPhone 5s (Global)"
case "iPhone7,1": return "iPhone 6 Plus"
case "iPhone7,2": return "iPhone 6"
case "iPhone8,1": return "iPhone 6s"
case "iPhone8,2": return "iPhone 6s Plus"
case "iPhone8,4": return "iPhone SE"
case "iPhone9,1": return "iPhone 7 (GSM+CDMA)"
case "iPhone9,2": return "iPhone 7 Plus (GSM+CDMA)"
case "iPhone9,3": return "iPhone 7 (Global)"
case "iPhone9,4": return "iPhone 7 Plus (Global)"

// iPod
case "iPod1,1": return "iPod Touch 1G"
case "iPod2,1": return "iPod Touch 2G"
case "iPod3,1": return "iPod Touch 3G"
case "iPod4,1": return "iPod Touch 4G"
case "iPod5,1": return "iPod Touch 5G"
case "iPod7,1": return "iPod Touch 6G"

// iPad
case "iPad1,1": return "iPad 1"
case "iPad2,1": return "iPad 2 (WiFi)"
case "iPad2,2": return "iPad 2 (GSM)"
case "iPad2,3": return "iPad 2 (CDMA)"
case "iPad2,4": return "iPad 2 (WiFi)"
case "iPad2,5": return "iPad Mini 1 (WiFi)"
case "iPad2,6": return "iPad Mini 1 (GSM)"
case "iPad2,7": return "iPad Mini 1 (GSM+CDMA)"
case "iPad3,1": return "iPad 3 (WiFi)"
case "iPad3,2": return "iPad 3 (GSM+CDMA)"
case "iPad3,3": return "iPad 3 (GSM)"
case "iPad3,4": return "iPad 4 (WiFi)"
case "iPad3,5": return "iPad 4 (GSM)"
case "iPad3,6": return "iPad 4 (GSM+CDMA)"
case "iPad4,1": return "iPad Air 1 (WiFi)"
case "iPad4,2": return "iPad Air 1 (Cellular)"
case "iPad4,3": return "iPad Air"
case "iPad4,4": return "iPad Mini 2 (WiFi)"
case "iPad4,5": return "iPad Mini 2 (Cellular)"
case "iPad4,6": return "iPad Mini 2 (Rev)"
case "iPad4,7": return "iPad Mini 3 (WiFi)"
case "iPad4,8": return "iPad Mini 3 (A1600)"
case "iPad4,9": return "iPad Mini 3 (A1601)"
case "iPad5,1": return "iPad Mini 4 (WiFi)"
case "iPad5,2": return "iPad Mini 4 (Cellular)"
case "iPad5,3": return "iPad Air 2 (WiFi)"
case "iPad5,4": return "iPad Air 2 (Cellular)"
case "iPad6,3": return "iPad Pro 9.7 (WiFi)"
case "iPad6,4": return "iPad Pro 9.7 (Cellular)"
case "iPad6,7": return "iPad Pro 12.9 (WiFi)"
case "iPad6,8": return "iPad Pro 12.9 (Cellular)"

// Apple Watch
case "Watch1,1": return "Apple Watch 38mm"
case "Watch1,2": return "Apple Watch 42mm"
case "Watch2,3": return "Apple Watch 38mm (Series 2)"
case "Watch2,4": return "Apple Watch 42mm (Series 2)"
case "Watch2,6": return "Apple Watch 38mm (Series 1)"
case "Watch2,7": return "Apple Watch 42mm (Series 1)"

// Apple TV
case "AppleTV2,1": return "Apple TV 2G"
case "AppleTV3,1": return "Apple TV 3G"
case "AppleTV3,2": return "Apple TV 3G (2013)"
case "AppleTV5,3": return "Apple TV 4G"

case "i386": return "Simulator"
case "x86_64": return "Simulator"

default: return nil
}
}

/// Reaturns the version number of the current OS as String i.e. "1.2" or "9.4"
private static func osVersionForCurrentDevice() -> String {
return UIDevice.current.systemVersion
}

// Returns the screen size in points
private static func screenSizeForCurrentDevice() -> CGSize {
let bounds = UIScreen.main.bounds
return bounds.size
}

// Returns the screen size in pixels
private static func nativeScreenSizeForCurrentDevice() -> CGSize {
let bounds = UIScreen.main.nativeBounds
return bounds.size
}
Copy link
Member

Choose a reason for hiding this comment

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

Those static methods with a current prefixed to irritate me a bit. A possible alternative would be a static method on Device that returns a new Device populated with values for the current device. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a factory method would be great.
Even so, I think it is good practice to write small functions. The methods above does exactly what they promise :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed the method names a bit to remove irritation ;)

Copy link
Member

Choose a reason for hiding this comment

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

I like that!

}