diff --git a/Package.swift b/Package.swift index ecce0a5..51fc78f 100644 --- a/Package.swift +++ b/Package.swift @@ -23,7 +23,7 @@ let package = Package( dependencies: [ .product(name: "SwiftyJSON", package: "SwiftyJSON"), .product(name: "GRDB", package: "GRDB.swift"), - .product(name: "Reachability", package: "Reachability.swift") + .product(name: "Reachability", package: "Reachability.swift", condition: .when(platforms: [.iOS])) ] ), .testTarget( diff --git a/Sources/com.awareframework.ios.core/AwareSensor.swift b/Sources/com.awareframework.ios.core/AwareSensor.swift index 845556c..dfcbe5b 100644 --- a/Sources/com.awareframework.ios.core/AwareSensor.swift +++ b/Sources/com.awareframework.ios.core/AwareSensor.swift @@ -18,7 +18,7 @@ public protocol ISensorController { func set(label:String) } -open class AwareSensor: NSObject,ISensorController { +open class AwareSensor: NSObject, ISensorController { public let notificationCenter = NotificationCenter.default public var syncState = false diff --git a/Sources/com.awareframework.ios.core/db/core/DbSyncHelper.swift b/Sources/com.awareframework.ios.core/db/core/DbSyncHelper.swift index f30efd4..ef3ba5d 100644 --- a/Sources/com.awareframework.ios.core/db/core/DbSyncHelper.swift +++ b/Sources/com.awareframework.ios.core/db/core/DbSyncHelper.swift @@ -192,7 +192,6 @@ open class DbSyncHelper: NSObject, URLSessionDelegate, URLSessionDataDelegate, U self.urlSession(session, task: task, didCompleteWithError: nil) } } - } ////////// diff --git a/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift b/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift index 93dc2a9..28ca610 100644 --- a/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift +++ b/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift @@ -56,7 +56,7 @@ open class SQLiteEngine: Engine { let instance = self.getSQLiteInstance() try instance?.write{ db in for d in data { - if let result = handler(d) as? PersistableRecord { + if let result = handler(d) as? (any PersistableRecord) { try result.insert(db) } } @@ -69,7 +69,7 @@ open class SQLiteEngine: Engine { } } - open func save(_ data: Array, completion:((Error?)->Void)?){ + open func save(_ data: Array, completion:((Error?)->Void)?){ do { let instance = self.getSQLiteInstance() try instance?.write{ db in @@ -199,6 +199,26 @@ open class SQLiteEngine: Engine { } + + func getAllTableNames(in dbQueue: DatabaseQueue) throws -> [String] { + return try dbQueue.read { db in + try String.fetchAll(db, sql: """ + SELECT name FROM sqlite_master + WHERE type = 'table' AND name NOT LIKE 'sqlite_%' + ORDER BY name + """) + } + } + + func hasTable(_ tableName:String, in dbQueue: DatabaseQueue) throws -> Bool{ + let tables = try getAllTableNames(in: dbQueue) + return tables.contains { element in + if element == tableName { + return true + } + return false + } + } } diff --git a/Sources/com.awareframework.ios.core/manager/DbSyncManager.swift b/Sources/com.awareframework.ios.core/manager/DbSyncManager.swift index 289916c..9822214 100644 --- a/Sources/com.awareframework.ios.core/manager/DbSyncManager.swift +++ b/Sources/com.awareframework.ios.core/manager/DbSyncManager.swift @@ -7,7 +7,10 @@ import Foundation import UIKit + +#if canImport(Reachability) && os(iOS) import Reachability +#endif open class DbSyncManager { @@ -127,20 +130,24 @@ open class DbSyncManager { @objc public func sync(_ force:Bool=false){ if CONFIG.batteryChargingOnly && force == false { - switch UIDevice.current.batteryState { - case .unknown,.unplugged: - return - default: - break - } + #if canImport(Reachability) && os(iOS) + switch UIDevice.current.batteryState { + case .unknown,.unplugged: + return + default: + break + } + #endif } if CONFIG.wifiOnly && force == false { do { - let reachability = try Reachability() - if reachability.connection == .cellular || reachability.connection == .unavailable { - return - } + #if canImport(Reachability) && os(iOS) + let reachability = try Reachability() + if reachability.connection == .cellular || reachability.connection == .unavailable { + return + } + #endif }catch { print(error) return diff --git a/Sources/com.awareframework.ios.core/model/BaseDbModel.swift b/Sources/com.awareframework.ios.core/model/BaseDbModel.swift index fe7a72a..b61d096 100644 --- a/Sources/com.awareframework.ios.core/model/BaseDbModel.swift +++ b/Sources/com.awareframework.ios.core/model/BaseDbModel.swift @@ -22,6 +22,6 @@ public protocol BaseDbModelSQLite: Codable, FetchableRecord, PersistableRecord { func toDictionary() -> Dictionary - static func createTable(queue: DatabaseQueue) + static func createTable(queue: DatabaseQueue) throws static var databaseTableName: String { get } } diff --git a/Sources/com.awareframework.ios.core/util/AwareUtils.swift b/Sources/com.awareframework.ios.core/util/AwareUtils.swift index f66189c..bba746b 100644 --- a/Sources/com.awareframework.ios.core/util/AwareUtils.swift +++ b/Sources/com.awareframework.ios.core/util/AwareUtils.swift @@ -6,7 +6,11 @@ // import Foundation +#if os(iOS) import UIKit +#elseif os(watchOS) +import WatchKit +#endif public class AwareUtils{ @@ -21,7 +25,13 @@ public class AwareUtils{ if let deviceId = UserDefaults.standard.string(forKey: kDeviceIdKey) { return deviceId }else{ + + #if os(watchOS) + let deviceId = WKInterfaceDevice.current().identifierForVendor?.uuidString + #else let deviceId = UIDevice.current.identifierForVendor?.uuidString + #endif + if let did = deviceId { UserDefaults.standard.set(did, forKey: kDeviceIdKey) return did