Skip to content

Commit 31c7b75

Browse files
committed
Update swift integrated examples to async
1 parent 818ae8f commit 31c7b75

File tree

7 files changed

+189
-179
lines changed

7 files changed

+189
-179
lines changed

templates/swift/example-swiftui/Example.xcodeproj/project.pbxproj

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
1D34C2532702E4A000D1DA8D /* ExampleViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D34C2522702E4A000D1DA8D /* ExampleViewModel.swift */; };
1313
1D34C2552702E4B500D1DA8D /* ExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D34C2542702E4B500D1DA8D /* ExampleView.swift */; };
1414
1D43900826FC8B2500C71E3E /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D43900726FC8B2500C71E3E /* ImagePicker.swift */; };
15-
1DC4093C26BA74470025A85C /* Appwrite in Frameworks */ = {isa = PBXBuildFile; productRef = 1DC4093B26BA74470025A85C /* Appwrite */; };
16-
1DC4093E26BA744D0025A85C /* Appwrite in Frameworks */ = {isa = PBXBuildFile; productRef = 1DC4093D26BA744D0025A85C /* Appwrite */; };
15+
1DBB414827E87CA000ECF86F /* Appwrite in Frameworks */ = {isa = PBXBuildFile; productRef = 1DBB414727E87CA000ECF86F /* Appwrite */; };
16+
1DBB414A27E87CA800ECF86F /* Appwrite in Frameworks */ = {isa = PBXBuildFile; productRef = 1DBB414927E87CA800ECF86F /* Appwrite */; };
1717
2358B1EE270AC9DC0016EFBA /* ExampleViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D34C2522702E4A000D1DA8D /* ExampleViewModel.swift */; };
1818
2358B1EF270AC9E30016EFBA /* ExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D34C2542702E4B500D1DA8D /* ExampleView.swift */; };
1919
2358B1F0270AC9E80016EFBA /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D43900726FC8B2500C71E3E /* ImagePicker.swift */; };
@@ -50,7 +50,7 @@
5050
1D34C2522702E4A000D1DA8D /* ExampleViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleViewModel.swift; sourceTree = "<group>"; };
5151
1D34C2542702E4B500D1DA8D /* ExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleView.swift; sourceTree = "<group>"; };
5252
1D43900726FC8B2500C71E3E /* ImagePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = "<group>"; };
53-
1DC4093A26BA73C50025A85C /* swift-client */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "swift-client"; path = "../../../examples/swift-client"; sourceTree = "<group>"; };
53+
1DBB414627E87BDB00ECF86F /* swift-client */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "swift-client"; path = "../../../examples/swift-client"; sourceTree = "<group>"; };
5454
23DDF5922709A457006EFAFA /* ImagePicker+iOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ImagePicker+iOS.swift"; sourceTree = "<group>"; };
5555
23DDF5942709A46A006EFAFA /* ImagePicker+macOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ImagePicker+macOS.swift"; sourceTree = "<group>"; };
5656
23DDF597270AC2DA006EFAFA /* OSImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSImage.swift; sourceTree = "<group>"; };
@@ -88,21 +88,29 @@
8888
isa = PBXFrameworksBuildPhase;
8989
buildActionMask = 2147483647;
9090
files = (
91-
1DC4093E26BA744D0025A85C /* Appwrite in Frameworks */,
91+
1DBB414A27E87CA800ECF86F /* Appwrite in Frameworks */,
9292
);
9393
runOnlyForDeploymentPostprocessing = 0;
9494
};
9595
4B11596DCF77D21EBB9749E1 /* Frameworks */ = {
9696
isa = PBXFrameworksBuildPhase;
9797
buildActionMask = 2147483647;
9898
files = (
99-
1DC4093C26BA74470025A85C /* Appwrite in Frameworks */,
99+
1DBB414827E87CA000ECF86F /* Appwrite in Frameworks */,
100100
);
101101
runOnlyForDeploymentPostprocessing = 0;
102102
};
103103
/* End PBXFrameworksBuildPhase section */
104104

105105
/* Begin PBXGroup section */
106+
1DBB414527E87BDB00ECF86F /* Packages */ = {
107+
isa = PBXGroup;
108+
children = (
109+
1DBB414627E87BDB00ECF86F /* swift-client */,
110+
);
111+
name = Packages;
112+
sourceTree = "<group>";
113+
};
106114
1DC4093526BA6E7A0025A85C /* Frameworks */ = {
107115
isa = PBXGroup;
108116
children = (
@@ -121,7 +129,7 @@
121129
4B1152396B94D0F596CEBB52 = {
122130
isa = PBXGroup;
123131
children = (
124-
1DC4093A26BA73C50025A85C /* swift-client */,
132+
1DBB414527E87BDB00ECF86F /* Packages */,
125133
4B115B49441350FF784C7745 /* Products */,
126134
4B11566FD1E1FA1ABD88E438 /* Shared */,
127135
4B115E925CD74030DFF6498C /* iOS */,
@@ -211,7 +219,7 @@
211219
);
212220
name = "test (iOS)";
213221
packageProductDependencies = (
214-
1DC4093B26BA74470025A85C /* Appwrite */,
222+
1DBB414727E87CA000ECF86F /* Appwrite */,
215223
);
216224
productName = "test (iOS)";
217225
productReference = 4B115DA4916DAA4E8F13734F /* test.app */;
@@ -231,7 +239,7 @@
231239
);
232240
name = "test (macOS)";
233241
packageProductDependencies = (
234-
1DC4093D26BA744D0025A85C /* Appwrite */,
242+
1DBB414927E87CA800ECF86F /* Appwrite */,
235243
);
236244
productName = "test (macOS)";
237245
productReference = 4B11505C9899942E695B59FF /* test.app */;
@@ -464,7 +472,7 @@
464472
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
465473
ENABLE_PREVIEWS = YES;
466474
INFOPLIST_FILE = iOS/Info.plist;
467-
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
475+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
468476
LD_RUNPATH_SEARCH_PATHS = (
469477
"$(inherited)",
470478
"@executable_path/Frameworks",
@@ -502,7 +510,7 @@
502510
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
503511
ENABLE_PREVIEWS = YES;
504512
INFOPLIST_FILE = iOS/Info.plist;
505-
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
513+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
506514
LD_RUNPATH_SEARCH_PATHS = (
507515
"$(inherited)",
508516
"@executable_path/Frameworks",
@@ -715,11 +723,11 @@
715723
/* End XCConfigurationList section */
716724

717725
/* Begin XCSwiftPackageProductDependency section */
718-
1DC4093B26BA74470025A85C /* Appwrite */ = {
726+
1DBB414727E87CA000ECF86F /* Appwrite */ = {
719727
isa = XCSwiftPackageProductDependency;
720728
productName = Appwrite;
721729
};
722-
1DC4093D26BA744D0025A85C /* Appwrite */ = {
730+
1DBB414927E87CA800ECF86F /* Appwrite */ = {
723731
isa = XCSwiftPackageProductDependency;
724732
productName = Appwrite;
725733
};

templates/swift/example-swiftui/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/swift/example-swiftui/Shared/ExampleView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ struct ExampleView: View {
2121
.padding()
2222

2323
Button("Login") {
24-
viewModel.login()
24+
Task { await viewModel.login() }
2525
}
2626

2727
Button("Login with Facebook") {
28-
viewModel.loginWithFacebook()
28+
Task { await viewModel.loginWithFacebook() }
2929
}
3030

3131
Button("Register") {
32-
viewModel.register()
32+
Task { await viewModel.register() }
3333
}
3434

3535
Button("Download image") {
36-
viewModel.download()
36+
Task { await viewModel.download() }
3737
}
3838

3939
Button("Upload image") {
@@ -55,7 +55,7 @@ struct ExampleView: View {
5555
#endif
5656
}
5757
.onChange(of: imageToUpload) { img in
58-
viewModel.upload(image: img)
58+
Task { await viewModel.upload(image: img) }
5959
}
6060
.registerOAuthHandler()
6161
}

templates/swift/example-swiftui/Shared/ExampleViewModel.swift

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Appwrite
44
import NIO
55

66
let host = "https://localhost/v1"
7-
let projectId = "60f6a0d6e2a52"
7+
let projectId = "test"
88

99
extension ExampleView {
1010

@@ -22,65 +22,65 @@ extension ExampleView {
2222

2323
@Published public var username: String = "[email protected]"
2424
@Published public var password: String = "password"
25-
@Published public var fileId: String = "60f7a0178c3e5"
26-
@Published public var collectionId: String = "6155742223662"
25+
@Published public var userId: String = "unique()"
26+
@Published public var bucketId: String = "test"
27+
@Published public var fileId: String = "test"
28+
@Published public var collectionId: String = "test"
2729
@Published public var isShowPhotoLibrary = false
2830
@Published public var response: String = ""
2931

30-
func register() {
31-
account.create(email: username, password: password) { result in
32-
DispatchQueue.main.async {
33-
switch result {
34-
case .failure(let error):
35-
self.response = error.message
36-
case .success(let response):
37-
self.response = response.email
38-
}
39-
}
32+
func register() async {
33+
do {
34+
let response = try await account.create(
35+
userId: userId,
36+
email: username,
37+
password: password
38+
)
39+
self.userId = response.id
40+
self.response = String(describing: response.toMap())
41+
} catch {
42+
self.response = String(describing: error)
4043
}
41-
42-
4344
}
4445

45-
func login() {
46-
account.createSession(email: username, password: password) { result in
47-
DispatchQueue.main.async {
48-
switch result {
49-
case .failure(let error):
50-
self.response = error.message
51-
case .success(let response):
52-
self.response = response.userId
53-
}
54-
}
46+
func login() async {
47+
do {
48+
let response = try await account.createSession(
49+
email: username,
50+
password: password
51+
)
52+
self.response = String(describing: response.toMap())
53+
} catch {
54+
self.response = String(describing: error)
5555
}
5656
}
5757

58-
func loginWithFacebook() {
59-
account.createOAuth2Session(
60-
provider: "facebook",
61-
success: "\(host)/auth/oauth2/success",
62-
failure: "\(host)/auth/oauth2/failure"
63-
) { result in
64-
switch result {
65-
case .failure: self.response = "false"
66-
case .success(let response): self.response = response.description
67-
}
58+
func loginWithFacebook() async {
59+
do {
60+
let response = try await account.createOAuth2Session(
61+
provider: "facebook",
62+
success: "\(host)/auth/oauth2/success",
63+
failure: "\(host)/auth/oauth2/failure"
64+
)
65+
self.response = String(describing: response)
66+
} catch {
67+
self.response = String(describing: error)
6868
}
6969
}
7070

71-
func download() {
72-
storage.getFileDownload(fileId: fileId) { result in
73-
DispatchQueue.main.async {
74-
switch result {
75-
case .failure(let error): self.response = error.message
76-
case .success(let response):
77-
self.downloadedImage = Image(data: Data(buffer: response))
78-
}
79-
}
71+
func download() async {
72+
do {
73+
let response = try await storage.getFileDownload(
74+
bucketId: bucketId,
75+
fileId: fileId
76+
)
77+
self.downloadedImage = Image(data: Data(buffer: response))
78+
} catch {
79+
self.response = String(describing: error)
8080
}
8181
}
8282

83-
func upload(image: OSImage) {
83+
func upload(image: OSImage) async {
8484
let imageBuffer = ByteBufferAllocator()
8585
.buffer(data: image.data)
8686

@@ -95,22 +95,22 @@ extension ExampleView {
9595
buffer: imageBuffer
9696
)
9797

98-
storage.createFile(file: file) { result in
99-
DispatchQueue.main.async {
100-
switch result {
101-
case .failure(let error):
102-
self.response = error.message
103-
case .success(let response):
104-
self.response = response.name
105-
}
106-
}
98+
do {
99+
let response = try await storage.createFile(
100+
bucketId: bucketId,
101+
fileId: fileId,
102+
file: file
103+
)
104+
self.response = String(describing: response.toMap())
105+
} catch {
106+
self.response = String(describing: error)
107107
}
108108
}
109109

110110
func subscribe() {
111-
_ = realtime.subscribe(channels: ["collections.\(collectionId).documents"]) { response in
111+
_ = realtime.subscribe(channels: ["collections.\(collectionId).documents"]) { event in
112112
DispatchQueue.main.async {
113-
self.response = String(describing: response.payload!)
113+
self.response = String(describing: event.payload!)
114114
}
115115
}
116116
}

templates/swift/example-uikit/UIKitExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)