Skip to content

Commit c4d4db3

Browse files
Merge pull request #13 from DevCrew-io/feat/package-singleton
Feat/package singleton
2 parents df4283f + 6a029e8 commit c4d4db3

13 files changed

+144
-101
lines changed

CHANGELOG.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## [1.0.2] - 06-23-2023
910

10-
## 1.0.0 06-20-2023
11+
### Changed
12+
- Implement singleton pattern in package.
13+
- Improve errror messages.
1114

1215
### Added
16+
- Add example project with MVVM.
1317

18+
## [1.0.1] - 06-22-2023
19+
20+
### Fixed
21+
- Support lower swift / xcode versions.
22+
23+
## [1.0.0] - 06-20-2023
24+
25+
### Added
1426
- Chat.
1527
- Completions.
1628
- Text to Images.

Example/Example/App/AppDelegate.swift

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,32 @@
66
//
77

88
import UIKit
9+
import ChatGPTAPIManager
910

1011
@main
1112
class AppDelegate: UIResponder, UIApplicationDelegate {
12-
13-
14-
13+
1514
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
16-
// Override point for customization after application launch.
15+
16+
ChatGPTAPIManager.shared.apiKey = "YOUR_API_KEY"
17+
1718
return true
1819
}
19-
20+
2021
// MARK: UISceneSession Lifecycle
21-
22+
2223
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
2324
// Called when a new scene session is being created.
2425
// Use this method to select a configuration to create the new scene with.
2526
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
2627
}
27-
28+
2829
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
2930
// Called when the user discards a scene session.
3031
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
3132
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
3233
}
33-
34-
34+
35+
3536
}
3637

Example/Example/App/SceneDelegate.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import UIKit
1010
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
1111

1212
var window: UIWindow?
13-
14-
13+
1514
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
1615
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
1716
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.

Example/Example/ViewModles/ChatViewModel.swift

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ protocol ChatViewModelProtocols {
1414
var onFailure:(()-> Void)? { get set }
1515
var reloadTableView:(()-> Void)? { get set }
1616
var chatMessages: [ChatMessage] { get set }
17-
1817
}
1918

2019
class ChatViewModel: ChatViewModelProtocols {
2120

22-
var onSuccess:(()-> Void)? = nil
23-
var onFailure:(()-> Void)? = nil
24-
var reloadTableView:(()-> Void)? = nil
25-
var chatMessages: [ChatMessage] = []
26-
27-
let chatGPTAPI = ChatGPTAPIManager(apiKey: "xxxxxx")
28-
21+
var onSuccess:(()-> Void)? = nil
22+
var onFailure:(()-> Void)? = nil
23+
var reloadTableView:(()-> Void)? = nil
24+
var chatMessages: [ChatMessage] = []
2925

3026
func sendMessage(message: String ) {
3127

@@ -36,7 +32,7 @@ class ChatViewModel: ChatViewModelProtocols {
3632

3733
EZLoadingActivity.show("Loading...", disableUI: true)
3834

39-
chatGPTAPI.sendChatRequest(prompt: message,model: .gptThreePointFiveTurbo,endPoint: .chat) { result in
35+
ChatGPTAPIManager.shared.sendChatRequest(prompt: message,model: .gptThreePointFiveTurbo,endPoint: .chat) { result in
4036
switch result {
4137
case .success(let response):
4238
print("API response: \(response)")
@@ -57,4 +53,5 @@ class ChatViewModel: ChatViewModelProtocols {
5753
}
5854
}
5955
}
56+
6057
}

Example/Example/ViewModles/ImageViewModel.swift

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ import ChatGPTAPIManager
1010

1111
class ImageGenerationViewModel {
1212

13-
var onSuccess:(()-> Void)? = nil
14-
var onFailure:(()-> Void)? = nil
15-
var imageURLString: String?
16-
17-
let chatGPTAPI = ChatGPTAPIManager(apiKey: "xxxxx")
18-
13+
var onSuccess:(()-> Void)? = nil
14+
var onFailure:(()-> Void)? = nil
15+
var imageURLString: String?
1916

2017
func sendImageRequest(_ text: String ) {
2118

2219
EZLoadingActivity.show("Loading...", disableUI: true)
2320

24-
chatGPTAPI.generateImage(prompt: text,model: .gptThreePointFiveTurbo, imageSize: .fiveTwelve,endPoint: .generateImage) { result in
21+
ChatGPTAPIManager.shared.generateImage(prompt: text,model: .gptThreePointFiveTurbo, imageSize: .fiveTwelve,endPoint: .generateImage) { result in
2522
switch result {
2623
case .success(let response):
2724
print("API response: \(response)")
@@ -41,4 +38,5 @@ class ImageGenerationViewModel {
4138
}
4239
}
4340
}
41+
4442
}

Example/Example/ViewModles/TextGenerationViewModel.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import ChatGPTAPIManager
1010

1111
class TextGenerationViewModel: ChatViewModelProtocols {
1212

13-
var onSuccess:(()-> Void)? = nil
14-
var onFailure:(()-> Void)? = nil
15-
var reloadTableView:(()-> Void)? = nil
16-
var chatMessages: [ChatMessage] = []
13+
var onSuccess:(()-> Void)? = nil
14+
var onFailure:(()-> Void)? = nil
15+
var reloadTableView:(()-> Void)? = nil
16+
var chatMessages: [ChatMessage] = []
17+
1718

18-
let chatGPTAPI = ChatGPTAPIManager(apiKey: "xxxx")
1919
var rows:Int {
2020
return self.chatMessages.count
2121
}
@@ -29,7 +29,7 @@ class TextGenerationViewModel: ChatViewModelProtocols {
2929

3030
EZLoadingActivity.show("Loading...", disableUI: true)
3131

32-
chatGPTAPI.sendTextRequest(prompt: message,model: .textDavinci003,endPoint: .completion) { result in
32+
ChatGPTAPIManager.shared.sendTextRequest(prompt: message,model: .textDavinci003,endPoint: .completion) { result in
3333
switch result {
3434
case .success(let response):
3535
print("API response: \(response)")

Example/Example/Views/Cells/AssistantCell.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import UIKit
99

1010
class AssistantCell: UITableViewCell {
11+
1112
@IBOutlet private weak var messageLabel: UILabel!
13+
1214
override func awakeFromNib() {
1315
super.awakeFromNib()
1416
// Initialization code
@@ -17,10 +19,10 @@ class AssistantCell: UITableViewCell {
1719

1820
override func setSelected(_ selected: Bool, animated: Bool) {
1921
super.setSelected(selected, animated: animated)
20-
21-
// Configure the view for the selected state
2222
}
23+
2324
func configure(with message: ChatMessage) {
2425
messageLabel.text = message.content
2526
}
27+
2628
}

Example/Example/Views/Cells/UserCell.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import UIKit
99

1010
class UserCell: UITableViewCell {
11+
1112
@IBOutlet private weak var messageLabel: UILabel!
13+
1214
override func awakeFromNib() {
1315
super.awakeFromNib()
1416
// Initialization code
@@ -17,10 +19,10 @@ class UserCell: UITableViewCell {
1719

1820
override func setSelected(_ selected: Bool, animated: Bool) {
1921
super.setSelected(selected, animated: animated)
20-
21-
// Configure the view for the selected state
2222
}
23+
2324
func configure(with message: ChatMessage) {
2425
messageLabel.text = message.content
2526
}
27+
2628
}

Example/Example/Views/ChatAndText/ChatViewController.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class ChatViewController: UIViewController {
1616
@IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!
1717

1818
// MARK: - Variables
19-
2019
var vm: ChatViewModelProtocols!
2120

2221
// MARK: - ViewController Life Cycle
@@ -42,7 +41,6 @@ class ChatViewController: UIViewController {
4241
}
4342

4443
// MARK: - Keyboard
45-
4644
@objc func keyboardWillShow(_ notification: Notification) {
4745
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
4846
let keyboardHeight = keyboardSize.height
@@ -81,8 +79,8 @@ class ChatViewController: UIViewController {
8179
sendMessageToChatGPT(message)
8280
}
8381
}
84-
// MARK: - NetworkCall
8582

83+
// MARK: - NetworkCall
8684
func sendMessageToChatGPT(_ message: String) {
8785

8886
vm.reloadTableView = {
@@ -105,7 +103,9 @@ class ChatViewController: UIViewController {
105103
}
106104
}
107105
}
106+
108107
}
108+
109109
// MARK: - UITableViewDataSource
110110
extension ChatViewController: UITableViewDataSource {
111111
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
@@ -131,6 +131,7 @@ extension ChatViewController: UITableViewDataSource {
131131

132132
}
133133
}
134+
134135
// MARK: - UITableViewDelegate
135136
extension ChatViewController: UITableViewDelegate {
136137
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
@@ -141,6 +142,7 @@ extension ChatViewController: UITableViewDelegate {
141142
return labelHeight + 20 // Add padding
142143
}
143144
}
145+
144146
// MARK: - UITextFieldDelegate
145147
extension ChatViewController: UITextFieldDelegate {
146148
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
@@ -152,8 +154,6 @@ extension ChatViewController: UITextFieldDelegate {
152154
}
153155
}
154156

155-
156-
157157
extension String {
158158
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
159159
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)

Example/Example/Views/TextToImage/ImageWithTextViewController.swift

+5-13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
import UIKit
99
import ChatGPTAPIManager
1010
class ImageWithTextViewController: UIViewController {
11-
11+
1212
@IBOutlet weak var imageView: UIImageView!
1313
@IBOutlet weak var textField: UITextField!
1414
let vm = ImageGenerationViewModel()
1515

1616
override func viewDidLoad() {
1717
super.viewDidLoad()
18-
18+
1919
// Do any additional setup after loading the view.
2020
}
21+
2122
// MARK: - IBAction
2223
@IBAction func sendImageRequestMessage(_ sender: UIButton) {
2324
if let textInstruction = textField.text, !textInstruction.isEmpty {
@@ -26,6 +27,7 @@ class ImageWithTextViewController: UIViewController {
2627
generateImageWithText(textInstruction)
2728
}
2829
}
30+
2931
func generateImageWithText(_ text: String) {
3032
vm.sendImageRequest(text)
3133
vm.onSuccess = {
@@ -48,18 +50,8 @@ class ImageWithTextViewController: UIViewController {
4850
vm.onFailure = {
4951
EZLoadingActivity.hide(false,animated: true)
5052
}
51-
53+
5254
}
5355

54-
/*
55-
// MARK: - Navigation
56-
57-
// In a storyboard-based application, you will often want to do a little preparation before navigation
58-
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
59-
// Get the new view controller using segue.destination.
60-
// Pass the selected object to the new view controller.
61-
}
62-
*/
63-
6456
}
6557

Example/Example/Views/ViewController.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
import UIKit
99

1010
class ViewController: UIViewController {
11-
11+
1212
override func viewDidLoad() {
1313
super.viewDidLoad()
1414
// Do any additional setup after loading the view.
1515
}
16-
// MARK: - IBAction
16+
17+
// MARK: - IBAction
1718
@IBAction func chatVC(_ sender: UIButton) {
1819
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
1920
let vm = ChatViewModel()
2021
vc.vm = vm
2122
self.navigationController?.pushViewController(vc, animated: true)
2223
}
24+
2325
@IBAction func textVC(_ sender: UIButton) {
2426
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
2527
let vm = TextGenerationViewModel()
2628
vc.vm = vm
2729
self.navigationController?.pushViewController(vc, animated: true)
2830
}
31+
2932
@IBAction func txtToImageVC(_ sender: UIButton) {
3033
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ImageWithTextViewController") as! ImageWithTextViewController
3134
self.navigationController?.pushViewController(vc, animated: true)
3235
}
36+
3337
}
3438

0 commit comments

Comments
 (0)