Last active
December 30, 2020 05:25
-
-
Save notoroid/40663ec56c55fa296ea4b36eea8dbfc5 to your computer and use it in GitHub Desktop.
SFSymbolsSample for SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import DeviceGuru | |
enum DeviceStyleIcon: String { | |
case phoneHome = "phh" // iPhone and iPod touch devices with homebutton | |
case phone = "ph" // iPhone | |
case padHome = "pah" // iPad witn homebutton | |
case pad = "pa" // iPad | |
case none = "n" // none (AppleWatch, appleTV, mac) | |
} | |
extension DeviceGuru { | |
func styleIcon() -> DeviceStyleIcon { | |
let notHaveHomeButton = self.notHaveHomeButton() | |
let platform = self.platform() | |
let deviceIconType: DeviceStyleIcon | |
switch platform { | |
case .iPhone, .iPodTouch: | |
deviceIconType = notHaveHomeButton ? .phone : .phoneHome | |
case .iPad: | |
deviceIconType = notHaveHomeButton ? .pad : .padHome | |
default: | |
deviceIconType = .none | |
} | |
return deviceIconType | |
} | |
func notHaveHomeButton() -> Bool { | |
let hardwareString = self.hardwareString() | |
let includePrefixCollection = ["iPhone10", "iPhone11", "iPhone12", "iPhone13", "iPad8", "iPad13",] | |
let excludePrefixCollection = ["iPhone12,8","iPhone10,1","iPhone10,2","iPhone10,4","iPhone10,5",] | |
let notHaveHomeButton = includePrefixCollection.contains(where: { (includePrefix) -> Bool in hardwareString.hasPrefix(includePrefix) }) && !excludePrefixCollection.contains { (excludePrefix) -> Bool in hardwareString.hasPrefix(excludePrefix) } | |
return notHaveHomeButton | |
} | |
} | |
extension DeviceStyleIcon { | |
func imageIcon() -> Image { | |
switch self { | |
case .phoneHome: | |
return Image(systemName: "iphone.homebutton") | |
case .phone: | |
return Image(systemName: "iphone") | |
case .padHome: | |
return Image(systemName: "ipad.homebutton") | |
case .pad: | |
return Image(systemName: "ipad") | |
default: | |
return Image(systemName: "laptopcomputer") | |
} | |
} | |
} | |
struct ContentView: View { | |
func imageIcon() -> Image { | |
DeviceGuru().styleIcon().imageIcon() | |
} | |
func deviceDescription() -> Text { | |
Text(DeviceGuru().hardwareDescription() ?? "") | |
} | |
var body: some View { | |
GeometryReader{ geometry in | |
HStack { | |
Spacer() | |
VStack { | |
Spacer() | |
self.imageIcon() | |
.font(.system(size: min(geometry.size.width, geometry.size.height) * 0.2, weight: .thin)) | |
.foregroundColor(.black) | |
.padding(min(geometry.size.width, geometry.size.height) * 0.05) | |
.background( | |
Circle() | |
.foregroundColor( | |
.white | |
) | |
) | |
self.deviceDescription() | |
.foregroundColor(.black) | |
Spacer() | |
} | |
Spacer() | |
} | |
} | |
.background(Color(red: 0.8, green: 0.8, blue: 0.8)) | |
.ignoresSafeArea() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment