Skip to content

Commit

Permalink
fix: minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed May 17, 2024
1 parent 799f169 commit 1ec6331
Show file tree
Hide file tree
Showing 22 changed files with 254 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.17.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.0"),
],
targets: [
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Swift Package Manager](https://img.shields.io/github/v/release/appwrite/sdk-for-apple.svg?color=green&style=flat-square)
![License](https://img.shields.io/github/license/appwrite/sdk-for-apple.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.5.0-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.5.6-blue.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
Expand Down Expand Up @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "5.0.0"),
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "6.0.0"),
],
```

Expand Down
4 changes: 3 additions & 1 deletion Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class Client {
"x-sdk-name": "Apple",
"x-sdk-platform": "client",
"x-sdk-language": "apple",
"x-sdk-version": "5.0.0",
"x-sdk-version": "6.0.0",
"x-appwrite-response-format": "1.5.0"
]

Expand Down Expand Up @@ -577,6 +577,8 @@ extension Client {
return "tvos"
#elseif os(macOS)
return "macos"
#elseif os(visionOS)
return "visionos"
#elseif os(Linux)
return "linux"
#elseif os(Windows)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Appwrite/DeviceInfo/OSDeviceInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

class OSDeviceInfo {

#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
var iOSInfo: IOSDeviceInfo?
#elseif os(watchOS)
var watchOSInfo: WatchOSDeviceInfo?
Expand All @@ -15,7 +15,7 @@ class OSDeviceInfo {
#endif

init() {
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
self.iOSInfo = IOSDeviceInfo()
#elseif os(watchOS)
self.watchOSInfo = WatchOSDeviceInfo()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Appwrite/DeviceInfo/iOS/IOSDeviceInfo.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
import Foundation
import UIKit

Expand Down
12 changes: 8 additions & 4 deletions Sources/Appwrite/DeviceInfo/iOS/UIDevice+ModelName.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS) || os(visionOS)
import Foundation
import UIKit

Expand Down Expand Up @@ -82,7 +82,7 @@ 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 "i386", "x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
case "i386", "x86_64", "arm64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
default: return identifier
#elseif os(tvOS)
case "AppleTV1,1": return "Apple TV (1st generation)"
Expand All @@ -91,8 +91,12 @@ public extension UIDevice {
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
case "i386", "x86_64", "arm64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS"))"
default: return identifier
#elseif os(visionOS)
case "RealityDevice14,1": return "Apple Vision Pro"
case "i386", "x86_64", "arm64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "visionOS"))"
default: return identifier
#endif
}
}
Expand Down
23 changes: 20 additions & 3 deletions Sources/Appwrite/ID.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import Foundation

public class ID {
// Generate an hex ID based on timestamp
// Recreated from https://www.php.net/manual/en/function.uniqid.php
private static func hexTimestamp() -> String {
let now = Date()
let secs = Int(now.timeIntervalSince1970)
let usec = Int((now.timeIntervalSince1970 - Double(secs)) * 1_000_000)
let hexTimestamp = String(format: "%08x%05x", secs, usec)
return hexTimestamp
}

public static func custom(_ id: String) -> String {
return id
}

public static func unique() -> String {
return "unique()"
// Generate a unique ID with padding to have a longer ID
public static func unique(padding: Int = 7) -> String {
let baseId = Self.hexTimestamp()
let randomPadding = (1...padding).map {
_ in String(format: "%x", Int.random(in: 0..<16))
}.joined()
return baseId + randomPadding
}
}
}
10 changes: 7 additions & 3 deletions Sources/Appwrite/Models/RealtimeModels.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Foundation

public class RealtimeSubscription {
public var close: () -> Void
private var close: () async throws -> Void

init(close: @escaping () -> Void) {
init(close: @escaping () async throws-> Void) {
self.close = close
}

public func close() async throws {
try await self.close()
}
}

public class RealtimeCallback {
Expand All @@ -14,7 +18,7 @@ public class RealtimeCallback {

init(
for channels: Set<String>,
and callback: @escaping (RealtimeResponseEvent) -> Void
with callback: @escaping (RealtimeResponseEvent) -> Void
) {
self.channels = channels
self.callback = callback
Expand Down
8 changes: 4 additions & 4 deletions Sources/Appwrite/OAuth/View+OAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
typealias OSApplication = NSApplication
typealias OSViewController = NSViewController
let notificationType = NSApplication.willBecomeActiveNotification
#elseif os(iOS) || os(tvOS)
#elseif os(iOS) || os(tvOS) || os(visionOS)
typealias OSApplication = UIApplication
typealias OSViewController = UIViewController
let notificationType = UIApplication.willEnterForegroundNotification
Expand All @@ -14,7 +14,7 @@ let notificationType = WKApplication.willEnterForegroundNotification

#if canImport(SwiftUI)
import SwiftUI
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *)
extension View {
public func registerOAuthHandler() -> some View {
onOpenURL { url in
Expand All @@ -27,12 +27,12 @@ extension View {
#endif

#if canImport(OSViewController)
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *)
extension OSViewController {
public func registerOAuthHandler() {
#if os(macOS)
typealias OSHostingController = NSHostingController
#elseif os(iOS) || os(tvOS) || os(watchOS)
#elseif os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
typealias OSHostingController = UIHostingController
#endif
self.addChild(OSHostingController(rootView: EmptyView().registerOAuthHandler()))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Appwrite/OAuth/WebAuthComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
/// Used to authenticate with external OAuth2 providers. Launches browser windows and handles
/// suspension until the user completes the process or otherwise returns to the app.
///
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *)
public class WebAuthComponent {

#if canImport(SwiftUI)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Appwrite/PackageInfo/OSPackageInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
class OSPackageInfo {

public static func get() -> PackageInfo {
#if os(iOS) || os(watchOS) || os(tvOS) || os(macOS)
#if os(iOS) || os(watchOS) || os(tvOS) || os(macOS) || os(visionOS)
return PackageInfo.getApplePackage()
#elseif os(Linux)
return PackageInfo.getLinuxPackage()
Expand Down
46 changes: 9 additions & 37 deletions Sources/Appwrite/Services/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ open class Account: Service {
///
/// Add an authenticator app to be used as an MFA factor. Verify the
/// authenticator using the [verify
/// authenticator](/docs/references/cloud/client-web/account#verifyAuthenticator)
/// authenticator](/docs/references/cloud/client-web/account#updateMfaAuthenticator)
/// method.
///
/// @param AppwriteEnums.AuthenticatorType type
Expand Down Expand Up @@ -442,8 +442,8 @@ open class Account: Service {
/// Verify Authenticator
///
/// Verify an authenticator app after adding it using the [add
/// authenticator](/docs/references/cloud/client-web/account#addAuthenticator)
/// method.
/// authenticator](/docs/references/cloud/client-web/account#createMfaAuthenticator)
/// method. add
///
/// @param AppwriteEnums.AuthenticatorType type
/// @param String otp
Expand Down Expand Up @@ -483,8 +483,8 @@ open class Account: Service {
/// Verify Authenticator
///
/// Verify an authenticator app after adding it using the [add
/// authenticator](/docs/references/cloud/client-web/account#addAuthenticator)
/// method.
/// authenticator](/docs/references/cloud/client-web/account#createMfaAuthenticator)
/// method. add
///
/// @param AppwriteEnums.AuthenticatorType type
/// @param String otp
Expand Down Expand Up @@ -512,11 +512,10 @@ open class Account: Service {
/// @throws Exception
/// @return array
///
open func deleteMfaAuthenticator<T>(
open func deleteMfaAuthenticator(
type: AppwriteEnums.AuthenticatorType,
otp: String,
nestedType: T.Type
) async throws -> AppwriteModels.User<T> {
otp: String
) async throws -> Any {
let apiPath: String = "/account/mfa/authenticators/{type}"
.replacingOccurrences(of: "{type}", with: type.rawValue)

Expand All @@ -528,38 +527,11 @@ open class Account: Service {
"content-type": "application/json"
]

let converter: (Any) -> AppwriteModels.User<T> = { response in
return AppwriteModels.User.from(map: response as! [String: Any])
}

return try await client.call(
method: "DELETE",
path: apiPath,
headers: apiHeaders,
params: apiParams,
converter: converter
)
}

///
/// Delete Authenticator
///
/// Delete an authenticator for a user by ID.
///
/// @param AppwriteEnums.AuthenticatorType type
/// @param String otp
/// @throws Exception
/// @return array
///
open func deleteMfaAuthenticator(
type: AppwriteEnums.AuthenticatorType,
otp: String
) async throws -> AppwriteModels.User<[String: AnyCodable]> {
return try await deleteMfaAuthenticator(
type: type,
otp: otp,
nestedType: [String: AnyCodable].self
)
params: apiParams )
}

///
Expand Down
Loading

0 comments on commit 1ec6331

Please sign in to comment.