Skip to content

Commit

Permalink
Merge pull request #39 from schickling/cleanup
Browse files Browse the repository at this point in the history
Resolved Initialization Issue
  • Loading branch information
codytwinton authored Apr 8, 2018
2 parents 724cb38 + 9e1c03d commit 6cd4337
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.1

- **Fixed**: Resolved bug with initialization via. device identifier

## 1.1

- **Add**: Support for iPad Pro 10.5
Expand Down
2 changes: 1 addition & 1 deletion Device.swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Device.swift"
s.version = "1.1"
s.version = "1.1.1"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = "Super-lightweight library to detect used device"
s.homepage = "https://github.com/schickling/Device.swift"
Expand Down
6 changes: 5 additions & 1 deletion Device.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0930;
TargetAttributes = {
8FBDE8941B5CFA1B00C78486 = {
CreatedOnToolsVersion = 7.0;
Expand Down Expand Up @@ -233,12 +233,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -290,12 +292,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
8 changes: 3 additions & 5 deletions Device.xcodeproj/xcshareddata/xcschemes/Device.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,9 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down Expand Up @@ -57,7 +56,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
2 changes: 1 addition & 1 deletion Device/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
68 changes: 35 additions & 33 deletions Device/UIDeviceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
//
//

// MARK: Imports

import Foundation
import UIKit

// MARK: -

public extension UIDevice {

/// Returns the `DeviceType` of the device in use
public var deviceType: DeviceType {
return DeviceType.current
}
}

/// Enum representing the different types of iOS devices available
public enum DeviceType: String, EnumProtocol {
public enum DeviceType: String, CaseIterable {
case iPhone2G

case iPhone3G
Expand Down Expand Up @@ -75,9 +69,9 @@ public enum DeviceType: String, EnumProtocol {
case simulator
case notAvailable

// MARK: - Constants
// MARK: Constants

/// Returns the current device type
/// The current device type
public static var current: DeviceType {

var systemInfo = utsname()
Expand All @@ -98,7 +92,7 @@ public enum DeviceType: String, EnumProtocol {

// MARK: Variables

/// Returns the display name of the device type
/// The display name of the device type
public var displayName: String {

switch self {
Expand Down Expand Up @@ -144,6 +138,7 @@ public enum DeviceType: String, EnumProtocol {
}
}

/// The identifiers associated with each device type
internal var identifiers: [String] {

switch self {
Expand Down Expand Up @@ -192,15 +187,14 @@ public enum DeviceType: String, EnumProtocol {
}
}

// MARK: - Inits
// MARK: Inits

/** Creates a device type
- parameter identifier: The identifier of the device
- returns: The device type based on the provided identifier
*/
/// Creates a device type
///
/// - Parameter identifier: The identifier of the device
internal init(identifier: String) {

for device in DeviceType.all {
for device in DeviceType.allCases {
for deviceId in device.identifiers {
guard identifier == deviceId else { continue }
self = device
Expand All @@ -212,32 +206,40 @@ public enum DeviceType: String, EnumProtocol {
}
}

// MARK: -

// MARK: - EnumProtocol
public extension UIDevice {

internal protocol EnumProtocol: Hashable {
/// -returns: All Enum Values
static var all: [Self] { get }
/// The `DeviceType` of the device in use
public var deviceType: DeviceType {
return DeviceType.current
}
}

#if swift(>=4.2)
#else

// MARK: -

// MARK: - Extensions
internal protocol CaseIterable {
associatedtype AllCases: Collection where AllCases.Element == Self
static var allCases: AllCases { get }
}

internal extension EnumProtocol {

static var all: [Self] {
typealias Type = Self
let cases = AnySequence { () -> AnyIterator<Type> in
internal extension CaseIterable where Self: Hashable {
static var allCases: [Self] {
return [Self](AnySequence { () -> AnyIterator<Self> in
var raw = 0
return AnyIterator {
let current: Self = withUnsafePointer(to: &raw) { $0.withMemoryRebound(to: Type.self, capacity: 1) { $0.pointee } }
guard current.hashValue == raw else { return nil }
let current = withUnsafeBytes(of: &raw) { $0.load(as: Self.self) }
guard current.hashValue == raw else {
return nil
}
raw += 1
return current
}
}

return Array(cases)
})
}
}

#endif
6 changes: 4 additions & 2 deletions DeviceTests/DeviceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import XCTest

fileprivate extension DeviceType {

/// The test identifers to test against
var testIdentifiers: [String] {

switch self {
Expand Down Expand Up @@ -125,10 +126,11 @@ class DeviceTests: XCTestCase {

let emptyDevice = DeviceType(identifier: "")
XCTAssertEqual(emptyDevice, DeviceType.notAvailable)

XCTAssertEqual(DeviceType.allCases.count, 39)

for type in DeviceType.all {
for type in DeviceType.allCases {
XCTAssertFalse(type.displayName.isEmpty)

XCTAssert(type.identifiers == type.testIdentifiers)
}
}
Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var deviceType: DeviceType

Add the following to your `Cartfile` and follow [these instructions](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application)

```
```bash
github "schickling/Device.swift"
```

Expand All @@ -36,7 +36,6 @@ use_frameworks!
pod 'Device.swift'
```


## Usage

```swift
Expand All @@ -54,16 +53,12 @@ default: print("Check other available cases of DeviceType")

Also, check out our [Documentation][docs-url]



[carthage-image]: https://img.shields.io/badge/Carthage-compatible-4BC51D.svg
[docs-image]: https://img.shields.io/cocoapods/metrics/doc-percent/Device.swift.svg
[lang-image]: https://img.shields.io/badge/swift-3.0-orange.svg
[lang-image]: https://img.shields.io/badge/swift-4.1-orange.svg
[license-image]: https://img.shields.io/github/license/schickling/Device.swift.svg
[platform-image]: https://img.shields.io/cocoapods/p/Device.swift.svg
[pod-version-image]: https://img.shields.io/cocoapods/v/Device.swift.svg


[carthage-url]: https://github.com/Carthage/Carthage
[cocoapod-url]: http://cocoapods.org/pods/Device.swift
[docs-url]: http://cocoadocs.org/docsets/Device.swift
Expand Down

0 comments on commit 6cd4337

Please sign in to comment.