diff --git a/MagicBall.xcodeproj/project.pbxproj b/MagicBall.xcodeproj/project.pbxproj index c9a96d7..c9d3f3a 100644 --- a/MagicBall.xcodeproj/project.pbxproj +++ b/MagicBall.xcodeproj/project.pbxproj @@ -15,10 +15,11 @@ 5E4D3FE62790867600A0861A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E4D3FE52790867600A0861A /* Assets.xcassets */; }; 5E4D3FE92790867600A0861A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E4D3FE72790867600A0861A /* LaunchScreen.storyboard */; }; 5E4D3FF12790BC8000A0861A /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4D3FF02790BC7F00A0861A /* SettingsViewController.swift */; }; - 5E4D3FF427916B6C00A0861A /* Answers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4D3FF327916B6C00A0861A /* Answers.swift */; }; + 5E4D3FF427916B6C00A0861A /* MagicBall.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4D3FF327916B6C00A0861A /* MagicBall.swift */; }; 5E4D3FF92791AC8500A0861A /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4D3FF82791AC8500A0861A /* NetworkManager.swift */; }; 5E4D3FFC2792E1DD00A0861A /* MagicBallAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4D3FFB2792E1DD00A0861A /* MagicBallAPI.swift */; }; 5EDC6A0F27BBC06500C1B453 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 5EDC6A0E27BBC06500C1B453 /* README.md */; }; + 5EEF756B27BF9042007A1C99 /* DemoData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EEF756A27BF9042007A1C99 /* DemoData.swift */; }; 5EF855FE2794319800A9DE67 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = 5EF855FD2794319800A9DE67 /* .gitignore */; }; 5EF8560427943B3200A9DE67 /* 002.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5EF8560227943B3200A9DE67 /* 002.jpg */; }; 5EF8560527943B3200A9DE67 /* 001.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5EF8560327943B3200A9DE67 /* 001.jpg */; }; @@ -35,10 +36,11 @@ 5E4D3FE82790867600A0861A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 5E4D3FEA2790867600A0861A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 5E4D3FF02790BC7F00A0861A /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = ""; }; - 5E4D3FF327916B6C00A0861A /* Answers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Answers.swift; sourceTree = ""; }; + 5E4D3FF327916B6C00A0861A /* MagicBall.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicBall.swift; sourceTree = ""; }; 5E4D3FF82791AC8500A0861A /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = ""; }; 5E4D3FFB2792E1DD00A0861A /* MagicBallAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicBallAPI.swift; sourceTree = ""; }; 5EDC6A0E27BBC06500C1B453 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 5EEF756A27BF9042007A1C99 /* DemoData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoData.swift; sourceTree = ""; }; 5EF855FD2794319800A9DE67 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = ""; }; 5EF8560227943B3200A9DE67 /* 002.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 002.jpg; sourceTree = ""; }; 5EF8560327943B3200A9DE67 /* 001.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 001.jpg; sourceTree = ""; }; @@ -57,6 +59,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 5E40ECD927C21AB700E9A56F /* Source */ = { + isa = PBXGroup; + children = ( + 5EEF756A27BF9042007A1C99 /* DemoData.swift */, + ); + path = Source; + sourceTree = ""; + }; 5E4D3FD02790867400A0861A = { isa = PBXGroup; children = ( @@ -83,6 +93,8 @@ 5E4D3FF527916B7400A0861A /* Views */, 5E4D3FFA2791ACD600A0861A /* Controllers */, 5E4D3FF227916B5C00A0861A /* Model */, + 5E4D3FF72791AC5A00A0861A /* Network */, + 5E40ECD927C21AB700E9A56F /* Source */, 5E4D3FF627916BBF00A0861A /* SupportingFiles */, ); path = MagicBall; @@ -91,8 +103,7 @@ 5E4D3FF227916B5C00A0861A /* Model */ = { isa = PBXGroup; children = ( - 5E4D3FF72791AC5A00A0861A /* Network */, - 5E4D3FF327916B6C00A0861A /* Answers.swift */, + 5E4D3FF327916B6C00A0861A /* MagicBall.swift */, ); path = Model; sourceTree = ""; @@ -226,12 +237,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 5EEF756B27BF9042007A1C99 /* DemoData.swift in Sources */, 5E4D3FF92791AC8500A0861A /* NetworkManager.swift in Sources */, 5E4D3FE12790867400A0861A /* HomeViewController.swift in Sources */, 5E4D3FDD2790867400A0861A /* AppDelegate.swift in Sources */, 5E4D3FDF2790867400A0861A /* SceneDelegate.swift in Sources */, 5E4D3FFC2792E1DD00A0861A /* MagicBallAPI.swift in Sources */, - 5E4D3FF427916B6C00A0861A /* Answers.swift in Sources */, + 5E4D3FF427916B6C00A0861A /* MagicBall.swift in Sources */, 5E4D3FF12790BC8000A0861A /* SettingsViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/MagicBall/Controllers/HomeViewController.swift b/MagicBall/Controllers/HomeViewController.swift index 9034aab..3893dd3 100644 --- a/MagicBall/Controllers/HomeViewController.swift +++ b/MagicBall/Controllers/HomeViewController.swift @@ -8,28 +8,24 @@ import UIKit class HomeViewController: UIViewController { - - @IBOutlet weak var answer: UILabel! - - private let networkManager = NetworkManager() - + + // MARK: - IBOutlets + @IBOutlet weak var answerLabel: UILabel! + + // MARK: - Private Properties + private var magicBall = MagicBall() + + // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() self.becomeFirstResponder() - answer.text = "Shake me!" + answerLabel.text = "Shake me!" } - // MARK: - Shake motion + // MARK: - Actions override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { if motion == .motionShake { - networkManager.postRequest { result in - switch result { - case .success(let str): - self.answer.text = str - case .failure(_): - self.answer.text = Answers.demoData[Int.random(in: 0.. Int { - return Answers.demoData.count + return DemoData.answers.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) - cell.textLabel?.text = Answers.demoData[indexPath.row] + cell.textLabel?.text = DemoData.answers[indexPath.row] cell.backgroundColor = .gray.withAlphaComponent(0.2) cell.textLabel?.textColor = .white return cell @@ -71,7 +74,7 @@ extension SettingsViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { - Answers.demoData.remove(at: indexPath.row) + DemoData.answers.remove(at: indexPath.row) self.table.deleteRows(at: [indexPath], with: .automatic) } } diff --git a/MagicBall/Model/Answers.swift b/MagicBall/Model/Answers.swift deleted file mode 100644 index fd20bcd..0000000 --- a/MagicBall/Model/Answers.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// Answers.swift -// 8ball -// -// Created by Sergey Lukaschuk on 14.01.2022. -// - -import Foundation - -struct Answers { - static var demoData = ["Just do it! 🤠", "You rock!🤘", "OMG 😱" , "You are awesome 😎", "WTF 🤬"] -} - diff --git a/MagicBall/Model/MagicBall.swift b/MagicBall/Model/MagicBall.swift new file mode 100644 index 0000000..2a18124 --- /dev/null +++ b/MagicBall/Model/MagicBall.swift @@ -0,0 +1,35 @@ +// +// Answers.swift +// 8ball +// +// Created by Sergey Lukaschuk on 14.01.2022. +// + +import Foundation +import UIKit + +class MagicBall { + + // MARK: - Private Properties + private let networkManager: NetworkManagerProtocol + private let demoData: DemoDataProtocol + + // MARK: - Initializer + init(networkManager: NetworkManagerProtocol = NetworkManager(), demoData: DemoDataProtocol = DemoData()) { + self.networkManager = networkManager + self.demoData = demoData + } + + // MARK: - Public Methods + func getAnswer(for label: UILabel) { + networkManager.postRequest { result in + switch result { + case .success(let answer): + label.text = answer + case .failure(_): + label.text = self.demoData.getRandomAnswer() + } + } + } +} + diff --git a/MagicBall/Model/Network/MagicBallAPI.swift b/MagicBall/Network/MagicBallAPI.swift similarity index 100% rename from MagicBall/Model/Network/MagicBallAPI.swift rename to MagicBall/Network/MagicBallAPI.swift diff --git a/MagicBall/Model/Network/NetworkManager.swift b/MagicBall/Network/NetworkManager.swift similarity index 88% rename from MagicBall/Model/Network/NetworkManager.swift rename to MagicBall/Network/NetworkManager.swift index 1851f4d..2ec06e6 100644 --- a/MagicBall/Model/Network/NetworkManager.swift +++ b/MagicBall/Network/NetworkManager.swift @@ -7,13 +7,18 @@ import Foundation -class NetworkManager { +protocol NetworkManagerProtocol: AnyObject { + func postRequest(completion: @escaping (ObtainResult) -> Void) +} + +class NetworkManager: NetworkManagerProtocol { private let sessionConfiguration = URLSessionConfiguration.default private let session = URLSession.shared private let decoder = JSONDecoder() func postRequest(completion: @escaping (ObtainResult) -> Void ) { + guard let url = URL(string: "https://8ball.delegator.com/magic/JSON/question_string") else { return } session.dataTask(with: url) { [weak self] (data, response, error) in diff --git a/MagicBall/Source/DemoData.swift b/MagicBall/Source/DemoData.swift new file mode 100644 index 0000000..c7809d1 --- /dev/null +++ b/MagicBall/Source/DemoData.swift @@ -0,0 +1,24 @@ +// +// DemoData.swift +// MagicBall +// +// Created by Sergey Lukaschuk on 18.02.2022. +// + +import Foundation + + +protocol DemoDataProtocol: AnyObject { + func getRandomAnswer() -> String +} + +class DemoData: DemoDataProtocol { + + //MARK: - Singleton + static var answers = ["Just do it! 🤠", "You rock!🤘", "OMG 😱" , "You are awesome 😎", "WTF 🤬"] + + // MARK: - Public Methods + func getRandomAnswer() -> String { + return DemoData.answers[Int.random(in: 0.. Bool { // Override point for customization after application launch. - + return true } diff --git a/MagicBall/Views/Base.lproj/Main.storyboard b/MagicBall/Views/Base.lproj/Main.storyboard index e945fff..b81e3b7 100644 --- a/MagicBall/Views/Base.lproj/Main.storyboard +++ b/MagicBall/Views/Base.lproj/Main.storyboard @@ -1,8 +1,7 @@ - - + - + @@ -12,7 +11,7 @@ - + @@ -28,15 +27,15 @@ - + - + - +