Skip to content

Commit

Permalink
Merge pull request #612 from appwrite/fix-tvos-watchos
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar authored Feb 15, 2023
2 parents a5decbf + cc82a02 commit 93ef96d
Show file tree
Hide file tree
Showing 24 changed files with 1,171 additions and 189 deletions.
25 changes: 25 additions & 0 deletions src/SDK/Language/Apple.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ public function getFiles(): array
'destination' => '/example-swiftui/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist',
'template' => '/swift/example-swiftui/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist',
],
[
'scope' => 'default',
'destination' => '/example-uikit/UIKitExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved',
'template' => '/swift/example-uikit/UIKitExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved',
],
[
'scope' => 'default',
'destination' => '/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (iOS).xcscheme',
'template' => '/swift/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (iOS).xcscheme',
],
[
'scope' => 'default',
'destination' => '/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (macOS).xcscheme',
'template' => '/swift/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (macOS).xcscheme',
],
[
'scope' => 'default',
'destination' => '/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (tvOS).xcscheme',
'template' => '/swift/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (tvOS).xcscheme',
],
[
'scope' => 'default',
'destination' => '/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (watchOS).xcscheme',
'template' => '/swift/example-swiftui/Example.xcodeproj/xcshareddata/xcschemes/test (watchOS).xcscheme',
],
[
'scope' => 'default',
'destination' => '/example-swiftui/Example.xcodeproj/project.pbxproj',
Expand Down
22 changes: 16 additions & 6 deletions src/SDK/Language/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ public function getFiles(): array
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/iOS/iOSDeviceInfo.swift',
'template' => 'swift/Sources/DeviceInfo/iOS/iOSDeviceInfo.swift',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/iOS/IOSDeviceInfo.swift',
'template' => 'swift/Sources/DeviceInfo/iOS/IOSDeviceInfo.swift',
],
[
'scope' => 'default',
Expand All @@ -221,13 +221,23 @@ public function getFiles(): array
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/MacOS/MacOSDeviceInfo.swift',
'template' => 'swift/Sources/DeviceInfo/MacOS/MacOSDeviceInfo.swift',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/macOS/MacOSDeviceInfo.swift',
'template' => 'swift/Sources/DeviceInfo/macOS/MacOSDeviceInfo.swift',
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/MacOS/CwlSysCtl.swift',
'template' => 'swift/Sources/DeviceInfo/MacOS/CwlSysCtl.swift',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/watchOS/WatchOSDeviceInfo.swift',
'template' => 'swift/Sources/DeviceInfo/watchOS/WatchOSDeviceInfo.swift',
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/watchOS/WKInterfaceDevice+ModelName.swift',
'template' => 'swift/Sources/DeviceInfo/watchOS/WKInterfaceDevice+ModelName.swift',
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/DeviceInfo/macOS/CwlSysCtl.swift',
'template' => 'swift/Sources/DeviceInfo/macOS/CwlSysCtl.swift',
],
[
'scope' => 'default',
Expand Down
2 changes: 1 addition & 1 deletion templates/swift/Package.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
platforms: [
.iOS("15.0"),
.macOS("11.0"),
.watchOS("6.0"),
.watchOS("7.0"),
.tvOS("13.0"),
],
products: [
Expand Down
6 changes: 3 additions & 3 deletions templates/swift/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@ extension Client {
device = "\(info!.modelIdentifier) iOS/\(info!.systemVersion)"
#elseif os(watchOS)
let info = deviceInfo.watchOSInfo
device = "\(info!.systemInfo.machine) watchOS/\(info!.systemVersion)"
device = "\(info!.modelIdentifier) watchOS/\(info!.systemVersion)"
#elseif os(tvOS)
let info = deviceInfo.tvOSInfo
device = "\(info!.systemInfo.machine) tvOS/\(info!.systemVersion)"
let info = deviceInfo.iOSInfo
device = "\(info!.modelIdentifier) tvOS/\(info!.systemVersion)"
#elseif os(macOS)
let info = deviceInfo.macOSInfo
device = "(Macintosh; \(info!.model))"
Expand Down
73 changes: 22 additions & 51 deletions templates/swift/Sources/DeviceInfo/Linux/LinuxDeviceInfo.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if os(Linux)
import Foundation

class LinuxDeviceInfo : DeviceInfo {
class LinuxDeviceInfo {

let name: String
let version: String
Expand All @@ -14,53 +14,24 @@ class LinuxDeviceInfo : DeviceInfo {
let variant: String
let variantId: String
let machineId: String

internal init(
name: String,
version: String,
id: String,
idLike: [String],
versionCodename: String,
versionId: String,
prettyName: String,
buildId: String,
variant: String,
variantId: String,
machineId: String
) {
self.name = name
self.version = version
self.id = id
self.idLike = idLike
self.versionCodename = versionCodename
self.versionId = versionId
self.prettyName = prettyName
self.buildId = buildId
self.variant = variant
self.variantId = variantId
self.machineId = machineId
}

public static func get() -> LinuxDeviceInfo {
let os = getOsRelease()
let lsb = getLsbRelease()
let machineId = getMachineId()

return LinuxDeviceInfo(
name: os["NAME"] ?? "Linux",
version: os["VERSION"] ?? lsb["LSB_VERSION"] ?? "",
id: os["ID"] ?? lsb["DISTRIB_ID"] ?? "linux",
idLike: os["ID_LIKE"]?.split(separator: " ").map { String($0) } ?? [],
versionCodename: os["VERSION_CODENAME"] ?? lsb["DISTRIB_CODENAME"] ?? "",
versionId: os["VERSION_ID"] ?? lsb["DISTRIB_RELEASE"] ?? "",
prettyName: os["PRETTY_NAME"] ?? lsb["DISTRIB_DESCRIPTION"] ?? "Linux",
buildId: os["BUILD_ID"] ?? "",
variant: os["VARIANT"] ?? "",
variantId: os["VARIANT_ID"] ?? "",
machineId: machineId
)

public init() {
let os = LinuxDeviceInfo.getOsRelease()
let lsb = LinuxDeviceInfo.getLsbRelease()

name = os["NAME"] ?? "Linux"
version = os["VERSION"] ?? lsb["LSB_VERSION"] ?? ""
id = os["ID"] ?? lsb["DISTRIB_ID"] ?? "linux"
idLike = os["ID_LIKE"]?.split(separator: " ").map { String($0) } ?? []
versionCodename = os["VERSION_CODENAME"] ?? lsb["DISTRIB_CODENAME"] ?? ""
versionId = os["VERSION_ID"] ?? lsb["DISTRIB_RELEASE"] ?? ""
prettyName = os["PRETTY_NAME"] ?? lsb["DISTRIB_DESCRIPTION"] ?? "Linux"
buildId = os["BUILD_ID"] ?? ""
variant = os["VARIANT"] ?? ""
variantId = os["VARIANT_ID"] ?? ""
machineId = LinuxDeviceInfo.getMachineId()
}

private static func getOsRelease() -> [String: String] {
return tryReadKeyValues(path: "/etc/os-release")
}
Expand All @@ -82,19 +53,19 @@ class LinuxDeviceInfo : DeviceInfo {
let url = URL(fileURLWithPath: path)
let string = try! String(contentsOf: url, encoding: .utf8)
let lines = string.components(separatedBy: .newlines)

var dict = [String: String]()
for line in lines {
let splits = line.split(separator: "=")
if splits.count > 1 {
let key = String(splits[0])
let value = String(splits[1])

dict[key] = value
}
}

return dict
}
}
#endif
#endif
17 changes: 0 additions & 17 deletions templates/swift/Sources/DeviceInfo/MacOS/MacOSDeviceInfo.swift

This file was deleted.

20 changes: 11 additions & 9 deletions templates/swift/Sources/DeviceInfo/OSDeviceInfo.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Foundation

protocol DeviceInfo {}

class OSDeviceInfo {

#if os(iOS) || os(watchOS) || os(tvOS)
var iOSInfo: iOSDeviceInfo?
#if os(iOS) || os(tvOS)
var iOSInfo: IOSDeviceInfo?
#elseif os(watchOS)
var watchOSInfo: WatchOSDeviceInfo?
#elseif os(macOS)
var macOSInfo: MacOSDeviceInfo?
#elseif os(Linux)
Expand All @@ -15,14 +15,16 @@ class OSDeviceInfo {
#endif

init() {
#if os(iOS) || os(watchOS) || os(tvOS)
self.iOSInfo = iOSDeviceInfo.get()
#if os(iOS) || os(tvOS)
self.iOSInfo = IOSDeviceInfo()
#elseif os(watchOS)
self.watchOSInfo = WatchOSDeviceInfo()
#elseif os(macOS)
self.macOSInfo = MacOSDeviceInfo.get()
self.macOSInfo = MacOSDeviceInfo()
#elseif os(Linux)
self.linuxInfo = LinuxDeviceInfo.get()
self.linuxInfo = LinuxDeviceInfo()
#elseif os(Windows)
self.windowsInfo = LinuxDeviceInfo.get()
self.windowsInfo = LinuxDeviceInfo()
#endif
}
}
Expand Down
29 changes: 7 additions & 22 deletions templates/swift/Sources/DeviceInfo/Windows/WindowsDeviceInfo.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
#if os(Windows)
import Foundation

class WindowsDeviceInfo : DeviceInfo {
class WindowsDeviceInfo {

let numberOfCores: String
let computerName: String
let systemMemoryInMegabytes: UInt64

public init(
numberOfCores: String,
computerName: String,
systemMemoryInMegabytes: UInt64
) {
self.numberOfCores = numberOfCores
self.computerName = computerName
self.systemMemoryInMegabytes = systemMemoryInMegabytes
}

public static func get() -> WindowsDeviceInfo {
let memory = ProcessInfo.processInfo.physicalMemory / 1000 / 1000 // Bytes to MB

return WindowsDeviceInfo(
numberOfCores: ProcessInfo.processInfo.processorCount.description,
computerName: Host.current().localizedName ?? "",
systemMemoryInMegabytes: memory
)
public init() {
numberOfCores = ProcessInfo.processInfo.processorCount.description
computerName = Host.current().localizedName ?? ""
systemMemoryInMegabytes = ProcessInfo.processInfo.physicalMemory / 1000 / 1000 // Bytes to MB
}
}
#endif

#endif
27 changes: 27 additions & 0 deletions templates/swift/Sources/DeviceInfo/iOS/IOSDeviceInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if os(iOS) || os(tvOS)
import Foundation
import UIKit

class IOSDeviceInfo {

let name: String
let systemName: String
let systemVersion: String
let model: String
let localizedModel: String
let identifierForVendor: String
let modelIdentifier: String

public init() {
let device = UIDevice.current

name = device.name
systemName = device.systemName
systemVersion = device.systemVersion
model = device.model
localizedModel = device.localizedModel
identifierForVendor = device.identifierForVendor?.uuidString ?? ""
modelIdentifier = UIDevice.modelName
}
}
#endif
25 changes: 14 additions & 11 deletions templates/swift/Sources/DeviceInfo/iOS/UIDevice+ModelName.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if os(iOS) || os(tvOS) || os(watchOS)
#if os(iOS) || os(tvOS)
import Foundation
import UIKit

Expand All @@ -14,8 +14,8 @@ public extension UIDevice {
}

func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity
#if os(iOS)
switch identifier {
#if os(iOS)
case "iPod5,1": return "iPod touch (5th generation)"
case "iPod7,1": return "iPod touch (6th generation)"
case "iPod9,1": return "iPod touch (7th generation)"
Expand Down Expand Up @@ -49,6 +49,11 @@ public extension UIDevice {
case "iPhone14,5": return "iPhone 13"
case "iPhone14,2": return "iPhone 13 Pro"
case "iPhone14,3": return "iPhone 13 Pro Max"
case "iPhone14,6": return "iPhone SE (3rd generation)"
case "iPhone14,7": return "iPhone 14"
case "iPhone14,8": return "iPhone 14 Plus"
case "iPhone15,2": return "iPhone 14 Pro"
case "iPhone15,3": return "iPhone 14 Pro Max"
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return "iPad 2"
case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad (3rd generation)"
case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad (4th generation)"
Expand Down Expand Up @@ -77,21 +82,19 @@ public extension UIDevice {
case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8": return "iPad Pro (12.9-inch) (3rd generation)"
case "iPad8,11", "iPad8,12": return "iPad Pro (12.9-inch) (4th generation)"
case "iPad13,8", "iPad13,9", "iPad13,10", "iPad13,11":return "iPad Pro (12.9-inch) (5th generation)"
case "AppleTV5,3": return "Apple TV"
case "AppleTV6,2": return "Apple TV 4K"
case "AudioAccessory1,1": return "HomePod"
case "AudioAccessory5,1": return "HomePod mini"
case "i386", "x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
default: return identifier
}
#elseif os(tvOS)
switch identifier {
case "AppleTV5,3": return "Apple TV 4"
case "AppleTV6,2": return "Apple TV 4K"
case "AppleTV1,1": return "Apple TV (1st generation)"
case "AppleTV2,1": return "Apple TV (2nd generation)"
case "AppleTV3,1", "AppleTV3,2": return "Apple TV (3rd generation)"
case "AppleTV5,3": return "Apple TV (4th generation)"
case "AppleTV6,2": return "Apple TV 4K (1st generation)"
case "AppleTV11,1": return "Apple TV 4K (2nd generation)"
case "i386", "x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS"))"
default: return identifier
}
#endif
}
}

return mapToDevice(identifier: identifier)
Expand Down
Loading

0 comments on commit 93ef96d

Please sign in to comment.