Skip to content

Commit 76ee42f

Browse files
committed
Create Singleton PubNub Object & Cleanup
Created Singleton class PubNubConnection to be used as a single instance to be used across all screens. Updated base code to use new object & generate UUID.
1 parent 66e2c03 commit 76ee42f

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

getting-started/getting-started.xcodeproj/project.pbxproj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
357AB6D92790E2A200E2839B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 357AB6D72790E2A200E2839B /* LaunchScreen.storyboard */; };
1616
357AB6E22790E8F600E2839B /* PubNubChat in Frameworks */ = {isa = PBXBuildFile; productRef = 357AB6E12790E8F600E2839B /* PubNubChat */; };
1717
357AB6E42790E8F600E2839B /* PubNubChatComponents in Frameworks */ = {isa = PBXBuildFile; productRef = 357AB6E32790E8F600E2839B /* PubNubChatComponents */; };
18+
CD6A16832881EC1200D2139E /* PubNubConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD6A16822881EC1200D2139E /* PubNubConnection.swift */; };
19+
CD6A16862881EFBF00D2139E /* PubNubChat in Frameworks */ = {isa = PBXBuildFile; productRef = CD6A16852881EFBF00D2139E /* PubNubChat */; };
20+
CD6A16882881EFBF00D2139E /* PubNubChatComponents in Frameworks */ = {isa = PBXBuildFile; productRef = CD6A16872881EFBF00D2139E /* PubNubChatComponents */; };
1821
/* End PBXBuildFile section */
1922

2023
/* Begin PBXFileReference section */
@@ -26,6 +29,7 @@
2629
357AB6D52790E2A200E2839B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2730
357AB6D82790E2A200E2839B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2831
357AB6DA2790E2A200E2839B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
32+
CD6A16822881EC1200D2139E /* PubNubConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PubNubConnection.swift; sourceTree = "<group>"; };
2933
/* End PBXFileReference section */
3034

3135
/* Begin PBXFrameworksBuildPhase section */
@@ -34,6 +38,8 @@
3438
buildActionMask = 2147483647;
3539
files = (
3640
357AB6E42790E8F600E2839B /* PubNubChatComponents in Frameworks */,
41+
CD6A16862881EFBF00D2139E /* PubNubChat in Frameworks */,
42+
CD6A16882881EFBF00D2139E /* PubNubChatComponents in Frameworks */,
3743
357AB6E22790E8F600E2839B /* PubNubChat in Frameworks */,
3844
);
3945
runOnlyForDeploymentPostprocessing = 0;
@@ -67,6 +73,7 @@
6773
357AB6D52790E2A200E2839B /* Assets.xcassets */,
6874
357AB6D72790E2A200E2839B /* LaunchScreen.storyboard */,
6975
357AB6DA2790E2A200E2839B /* Info.plist */,
76+
CD6A16822881EC1200D2139E /* PubNubConnection.swift */,
7077
);
7178
path = "getting-started";
7279
sourceTree = "<group>";
@@ -90,6 +97,8 @@
9097
packageProductDependencies = (
9198
357AB6E12790E8F600E2839B /* PubNubChat */,
9299
357AB6E32790E8F600E2839B /* PubNubChatComponents */,
100+
CD6A16852881EFBF00D2139E /* PubNubChat */,
101+
CD6A16872881EFBF00D2139E /* PubNubChatComponents */,
93102
);
94103
productName = "getting-started";
95104
productReference = 357AB6C92790E2A100E2839B /* getting-started.app */;
@@ -121,6 +130,7 @@
121130
mainGroup = 357AB6C02790E2A000E2839B;
122131
packageReferences = (
123132
357AB6E02790E8F600E2839B /* XCRemoteSwiftPackageReference "chat-components-ios" */,
133+
CD6A16842881EFBF00D2139E /* XCRemoteSwiftPackageReference "chat-components-ios" */,
124134
);
125135
productRefGroup = 357AB6CA2790E2A100E2839B /* Products */;
126136
projectDirPath = "";
@@ -150,6 +160,7 @@
150160
buildActionMask = 2147483647;
151161
files = (
152162
357AB6D12790E2A100E2839B /* ViewController.swift in Sources */,
163+
CD6A16832881EC1200D2139E /* PubNubConnection.swift in Sources */,
153164
357AB6CD2790E2A100E2839B /* AppDelegate.swift in Sources */,
154165
357AB6CF2790E2A100E2839B /* SceneDelegate.swift in Sources */,
155166
);
@@ -380,6 +391,14 @@
380391
minimumVersion = 0.0.0;
381392
};
382393
};
394+
CD6A16842881EFBF00D2139E /* XCRemoteSwiftPackageReference "chat-components-ios" */ = {
395+
isa = XCRemoteSwiftPackageReference;
396+
repositoryURL = "https://github.com/pubnub/chat-components-ios";
397+
requirement = {
398+
branch = master;
399+
kind = branch;
400+
};
401+
};
383402
/* End XCRemoteSwiftPackageReference section */
384403

385404
/* Begin XCSwiftPackageProductDependency section */
@@ -393,6 +412,16 @@
393412
package = 357AB6E02790E8F600E2839B /* XCRemoteSwiftPackageReference "chat-components-ios" */;
394413
productName = PubNubChatComponents;
395414
};
415+
CD6A16852881EFBF00D2139E /* PubNubChat */ = {
416+
isa = XCSwiftPackageProductDependency;
417+
package = CD6A16842881EFBF00D2139E /* XCRemoteSwiftPackageReference "chat-components-ios" */;
418+
productName = PubNubChat;
419+
};
420+
CD6A16872881EFBF00D2139E /* PubNubChatComponents */ = {
421+
isa = XCSwiftPackageProductDependency;
422+
package = CD6A16842881EFBF00D2139E /* XCRemoteSwiftPackageReference "chat-components-ios" */;
423+
productName = PubNubChatComponents;
424+
};
396425
/* End XCSwiftPackageProductDependency section */
397426
};
398427
rootObject = 357AB6C12790E2A000E2839B /* Project object */;

getting-started/getting-started.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// PubNubObj.swift
3+
// getting-started
4+
//
5+
// Used to provide a globally accessible, shared instance of a class.
6+
// Useful for adding more screens to application.
7+
// Created by Oliver Carson on 7/14/22.
8+
//
9+
10+
import Foundation
11+
import PubNub
12+
13+
final class PubNubConnection {
14+
private static let PUBNUB_PUBLISH_KEY = "pub-c-7ae1c68f-8009-470a-83d6-b14848591f3a"
15+
private static let PUBNUB_SUBSCRIBE_KEY = "sub-c-a7cb2610-ad18-4a4a-a5db-188271558045"
16+
static let shared : PubNub = PubNub(
17+
configuration: PubNubConfiguration(
18+
publishKey: PUBNUB_PUBLISH_KEY,
19+
subscribeKey: PUBNUB_SUBSCRIBE_KEY,
20+
uuid: String(NSUUID().uuidString.prefix(6)) // UUID will be first 6 characters
21+
))
22+
private init() {}
23+
}

getting-started/getting-started/SceneDelegate.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,8 @@ import PubNub
1111
import PubNubChat
1212
import PubNubChatComponents
1313

14-
let PUBNUB_PUBLISH_KEY = "myPublishKey"
15-
let PUBNUB_SUBSCRIBE_KEY = "myPublishKey"
16-
1714
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
18-
19-
// Creates PubNub configuration
20-
lazy var pubnubConfiguration = {
21-
return PubNubConfiguration(
22-
publishKey: PUBNUB_PUBLISH_KEY,
23-
subscribeKey: PUBNUB_SUBSCRIBE_KEY,
24-
uuid: "myFirstUser"
25-
)
26-
}()
27-
15+
var pubNub = PubNubConnection.shared
2816
var window: UIWindow?
2917

3018
var chatProvider: PubNubChatProvider?
@@ -42,7 +30,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
4230
if chatProvider == nil {
4331
// Creates a new ChatProvider
4432
let provider = PubNubChatProvider(
45-
pubnubProvider: PubNub(configuration: pubnubConfiguration)
33+
pubnubProvider: pubNub
4634
)
4735

4836
// Preloads dummy data
@@ -77,14 +65,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
7765
// Creates a user object with uuid
7866
let user = PubNubChatUser(
7967
id: chatProvider.pubnubConfig.uuid,
80-
name: "myFirstUser",
68+
name: chatProvider.pubnubConfig.uuid,
8169
avatarURL: URL(string: "https://picsum.photos/seed/\(chatProvider.pubnubConfig.uuid)/200")
8270
)
8371

8472
// Creates a channel object
8573
let channel = PubNubChatChannel(
8674
id: defaultChannelId,
87-
name: "Default",
75+
name: defaultChannelId,
8876
type: "direct",
8977
avatarURL: URL(string: "https://picsum.photos/seed/\(defaultChannelId)/200")
9078
)
@@ -99,4 +87,3 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
9987
chatProvider.dataProvider.load(members: [membership])
10088
}
10189
}
102-

0 commit comments

Comments
 (0)