AcaMate_iOS/AcaMate/0. Setup/SwiftUI_Prefix.swift
2025-02-18 01:23:06 +09:00

457 lines
13 KiB
Swift

//
// SwiftUI_Prefix.swift
// PersonalHealthDiary
//
// Created by Sean Kim on 2/20/24.
//
import SwiftUI
// MARK: - ACAMATE
// APPSTORE_URL : https://apps.apple.com/us/app/%EC%95%84%EC%B9%B4%EB%8D%B0%EB%AF%B8%EB%A9%94%EC%9D%B4%ED%8A%B8/id6739448113
#if LOCAL
public let API_URL: String = "http://localhost:5144"
public let WS_URL: String = "ws://localhost:5144"
//public let WS_URL: String = "ws://169.254.53.148:5144"
//#else
#elseif DEV
public let API_URL: String = "https://devacamate.ipstein.myds.me"
//public let WS_URL: String = "ws://devacamate.ipstein.myds.me"
public let WS_URL: String = "ws://192.168.0.71:5144"
//ipconfig getifaddr en0
#else
public let API_URL: String = "https://acamate.ipstein.myds.me"
public let WS_URL: String = "wss://acamate.ipstein.myds.me"
#endif
// MARK: - TYPEALIAS
typealias VOID_TO_VOID = () -> ()
// MARK: - VARIABLE
public var APPSTORE_URL = "https://itunes.apple.com/app/"
public var KEYBOARD_UP_HEIGHT: CGFloat = 46.0
enum Compare: Int{
case bigger = 0
case smaller
case equal
case error
}
// MARK: - FUNCTION
/// print
public func printLog<T>(_ object: T, _ file: String = #file, _ function: String = #function, _ line: Int = #line){
#if DEBUG
let dateString = Date().convertString("yyyy/MM/dd HH:mm:ss:SSS")
Swift.print("""
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____
|* TIME = [\(dateString)] || FILE = [\(file.lastPathComponent)]
| NAME = [\(function)] || LINE = [\(line)]
|>>> PRINT = \(object)
""")
#else
#endif
}
public func getDeviceWidth() -> CGFloat { UIScreen.main.bounds.size.width }
public func getDeviceHeight() -> CGFloat { UIScreen.main.bounds.size.height }
///
public func isIllegalDevice() -> Bool {
func canOpen(path: String) -> Bool {
let file = fopen(path, "r")
guard file != nil else { return false }
fclose(file)
return true
}
guard let cydiaUrlScheme = NSURL(string: "cydia://package/com.example.package") else { return false }
if UIApplication.shared.canOpenURL(cydiaUrlScheme as URL) {
return true
}
#if arch(i386) || arch(x86_64)
return false
#endif
let fileManager = FileManager.default
if fileManager.fileExists(atPath: "/Applications/Cydia.app") ||
fileManager.fileExists(atPath: "/Library/MobileSubstrate/MobileSubstrate.dylib") ||
fileManager.fileExists(atPath: "/bin/bash") ||
fileManager.fileExists(atPath: "/usr/sbin/sshd") ||
fileManager.fileExists(atPath: "/etc/apt") ||
fileManager.fileExists(atPath: "/usr/bin/ssh") ||
fileManager.fileExists(atPath: "/private/var/lib/apt") {
return true
}
if canOpen(path: "/Applications/Cydia.app") ||
canOpen(path: "/Library/MobileSubstrate/MobileSubstrate.dylib") ||
canOpen(path: "/bin/bash") ||
canOpen(path: "/usr/sbin/sshd") ||
canOpen(path: "/etc/apt") ||
canOpen(path: "/usr/bin/ssh") {
return true
}
let path = "/private/" + NSUUID().uuidString
do {
try "anyString".write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
try fileManager.removeItem(atPath: path)
return true
} catch let error { //
printLog("Jail ERROR: \(error))")
return false
}
}
public func fontNameCheck() {
for family: String in UIFont.familyNames {
print(family)
for names : String in UIFont.fontNames(forFamilyName: family){
printLog("\(names)")
}
}
}
/// a b true ( false)
public func isBigger (_ a: Int, _ b: Int) -> Bool {
if a > b { return true } else { return false }
}
/// JSON String Dictionary
public func jsonToDict(_ input: String) -> [String: Any] {
if let jsonData = input.data(using: .utf8){
do {
if let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
return jsonObject
}
} catch let error { //
printLog("JSON ERROR: \(error))")
}
}
return [:]
}
/// JSON Swift .
public func jsonToSwift<T: Decodable>(_ input: String) -> T? {
if let data = input.data(using: .utf8) {
do {
let jsonObject = try JSONDecoder().decode(T.self, from: data)
return jsonObject
} catch let error {
printLog("JSON ERROR: \(error))")
}
}
return nil
}
func versionChange(ver: String) -> [Int] {
return ver.components(separatedBy: ["."]).map {Int($0) ?? 0}
}
/// a b OOO .
func compareVersion(_ a: String, _ b: String) -> Compare {
let aList = versionChange(ver: a)
let bList = versionChange(ver: b)
if aList.count != bList.count { return .error }
else {
for i in 0 ..< aList.count {
if aList[i] > bList[i] { return .bigger }
else if aList[i] < bList[i] { return .smaller }
}
return .equal
}
}
// MARK: - CUSTOM COMPONENTS
// MARK: - EXTENSION
extension String {
///
var lastPathComponent: String {
get {
return (self as NSString).lastPathComponent
}
}
///
func cut(start: Int, end: Int) -> String {
let startIndex = self.index(self.startIndex,offsetBy: start >= 0 ? start : 0)
let endIndex = self.index(self.startIndex,offsetBy: end >= 0 ? end : 0)
let result: String = "\(self[startIndex ..< endIndex])"
return result
}
///
func letter() -> [String] {
return self.map { String($0) }
}
/// ()
func word() -> [String] {
return self.components(separatedBy: " ")
}
/// Date() -> (Bool, Date)
func convertDate(_ dateFormat: String = "yyyyMMdd") -> (Bool, Date) {
let dateFormatter = Date().setDateFormatter(dateFormat)
if let convert = dateFormatter.date(from: self) {
return (true, convert)
} else {
return (false, Date())
}
}
///
///
/// : "[A-Z0-9a-z._%+-]+@[A-Z0-9a-z._%+-]+\\.[A-Za-z]{2,64}"
/// : "[A-Z0-9a-z._%+-]{6,12}"
func checkFilter(_ filter: String) -> Bool {
var result = false
if self == "" { return result }
if (self.range(of: filter, options: .regularExpression) != nil) {
result = true
} else {
result = false
}
return result
}
/// Bool DateFormat
static func makeDateFormat(year: Bool, month: Bool, day: Bool, dayOfWeek: Bool = false, am_pm: Bool = false, hour: Bool = false,_ fullTime: Bool = true, minute: Bool = false, second: Bool = false, mSecond: Bool = false, mSDigit: Int = 1) -> String {
var dateFormat = ""
if year {
dateFormat = dateFormat + "yyyy"
}
if month {
dateFormat = dateFormat + "MM"
}
if day {
dateFormat = dateFormat + "dd"
}
if dayOfWeek {
dateFormat = dateFormat + "EEEEEE"
}
if am_pm {
dateFormat = dateFormat + "a"
}
if fullTime { // 24
if hour {
dateFormat = dateFormat + "HH"
}
} else { // 12
if hour {
dateFormat = dateFormat + "hh"
}
}
if minute {
dateFormat = dateFormat + "mm"
}
if second {
dateFormat = dateFormat + "ss"
}
if mSecond {
for _ in 0 ..< mSDigit {
dateFormat = dateFormat + "S"
}
}
return dateFormat
}
/// String
static func combineDate(year: Int, month: Int, day: Int) -> String {
return "\(year * 10000 + month * 100 + day)"
}
func stringToInt() -> Int {
if let intValue = Int(self) {
return intValue
} else {
return 0
}
}
}
extension Date {
///
///
///
var year: Int {
let dateFormatter = self.setDateFormatter("yyyy")
if let year = Int(dateFormatter.string(from: self)){
return year
} else {
return Calendar.current.component(.year, from: self)
}
}
///
///
///
var month: Int {
let dateFormatter = self.setDateFormatter("MM")
if let month = Int(dateFormatter.string(from: self)) {
return month
} else {
return Calendar.current.component(.month, from: self)
}
}
///
///
///
var day: Int {
let dateFormatter = self.setDateFormatter("dd")
if let day = Int(dateFormatter.string(from: self)) {
return day
} else {
return Calendar.current.component(.day, from: self)
}
}
///
///
/// 0~6
///
/// -1
var dayOfWeek: Int {
let dateFormatter = self.setDateFormatter("EEEEEE")
let convert = dateFormatter.string(from: self)
switch convert {
case "":
return 0
case "":
return 1
case "":
return 2
case "":
return 3
case "":
return 4
case "":
return 5
case "":
return 6
default:
return -1
}
}
func setDateFormatter(_ dateFormat: String) -> DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
dateFormatter.locale = Locale(identifier: "ko_kr")
return dateFormatter
}
/// String
func convertString(_ dateFormat: String = "yyyyMMdd") -> String {
let dateFormatter = self.setDateFormatter(dateFormat)
return dateFormatter.string(from: self)
}
///
///
///
func checkLeapMonth() -> Bool {
if self.year % 4 == 0 {
if self.year % 100 == 0 {
if self.year % 400 == 0 {
return true
}
} else {
return true
}
}
return false
}
///
///
///
func getLastDayOfMonth() -> Int {
switch self.month {
case 1,3,5,7,8,10,12 :
return 31
case 2:
if self.checkLeapMonth() {
return 29
} else {
return 28
}
case 4,6,9,11:
return 30
default:
return -1
}
}
}
extension Encodable {
//
func toStringDict() -> [String:String] {
let mirror = Mirror(reflecting: self)
var result = [String:String]()
for child in mirror.children {
if let key = child.label {
if let value = child.value as? CustomStringConvertible {
result[key] = value.description
} else {
result[key] = "\(child.value)"
}
}
}
return result
}
}
// MARK: - ANNOTATION
///
/// @UserDefault (key: "keyName", defaultValue: "default") var
/// print()
/// - UserDefaults key
/// =
/// - UserDefaults key
@propertyWrapper
struct UserDefault<T> {
private let ud: UserDefaults = .standard
private let key: String
private var defaultValue: T
var wrappedValue: T {
set { ud.set(newValue, forKey: key) }
get { ud.object(forKey: key) as? T ?? defaultValue }
}
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
///: _.removeData()
/// - _
func removeData() {
ud.removeObject(forKey: key)
}
}