Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions MagicBall.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -35,10 +36,11 @@
5E4D3FE82790867600A0861A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
5E4D3FEA2790867600A0861A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5E4D3FF02790BC7F00A0861A /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
5E4D3FF327916B6C00A0861A /* Answers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Answers.swift; sourceTree = "<group>"; };
5E4D3FF327916B6C00A0861A /* MagicBall.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicBall.swift; sourceTree = "<group>"; };
5E4D3FF82791AC8500A0861A /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
5E4D3FFB2792E1DD00A0861A /* MagicBallAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicBallAPI.swift; sourceTree = "<group>"; };
5EDC6A0E27BBC06500C1B453 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
5EEF756A27BF9042007A1C99 /* DemoData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoData.swift; sourceTree = "<group>"; };
5EF855FD2794319800A9DE67 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
5EF8560227943B3200A9DE67 /* 002.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 002.jpg; sourceTree = "<group>"; };
5EF8560327943B3200A9DE67 /* 001.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 001.jpg; sourceTree = "<group>"; };
Expand All @@ -57,6 +59,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
5E40ECD927C21AB700E9A56F /* Source */ = {
isa = PBXGroup;
children = (
5EEF756A27BF9042007A1C99 /* DemoData.swift */,
);
path = Source;
sourceTree = "<group>";
};
5E4D3FD02790867400A0861A = {
isa = PBXGroup;
children = (
Expand All @@ -83,6 +93,8 @@
5E4D3FF527916B7400A0861A /* Views */,
5E4D3FFA2791ACD600A0861A /* Controllers */,
5E4D3FF227916B5C00A0861A /* Model */,
5E4D3FF72791AC5A00A0861A /* Network */,
5E40ECD927C21AB700E9A56F /* Source */,
5E4D3FF627916BBF00A0861A /* SupportingFiles */,
);
path = MagicBall;
Expand All @@ -91,8 +103,7 @@
5E4D3FF227916B5C00A0861A /* Model */ = {
isa = PBXGroup;
children = (
5E4D3FF72791AC5A00A0861A /* Network */,
5E4D3FF327916B6C00A0861A /* Answers.swift */,
5E4D3FF327916B6C00A0861A /* MagicBall.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 11 additions & 15 deletions MagicBall/Controllers/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<Answers.demoData.count)]
}
}
magicBall.getAnswer(for: answerLabel)
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions MagicBall/Controllers/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import Foundation
import UIKit

class SettingsViewController: UIViewController {


// MARK: - Private Properties
private let table: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()


// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
Expand All @@ -32,7 +34,8 @@ class SettingsViewController: UIViewController {
super.viewDidLayoutSubviews()
table.frame = view.bounds
}


// MARK: - Public Methods
@objc private func addTap() {
let alert = UIAlertController(
title: "Demo List Answers",
Expand All @@ -47,7 +50,7 @@ class SettingsViewController: UIViewController {
guard let text = field.text, !text.isEmpty else { fatalError() }

DispatchQueue.main.async {
Answers.demoData.append(text)
DemoData.answers.append(text)
self?.table.reloadData()
}
}))
Expand All @@ -58,20 +61,20 @@ class SettingsViewController: UIViewController {
// MARK: - TableViewDataSource
extension SettingsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> 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
}

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)
}
}
Expand Down
13 changes: 0 additions & 13 deletions MagicBall/Model/Answers.swift

This file was deleted.

35 changes: 35 additions & 0 deletions MagicBall/Model/MagicBall.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions MagicBall/Source/DemoData.swift
Original file line number Diff line number Diff line change
@@ -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..<DemoData.answers.count)]
}
}
2 changes: 1 addition & 1 deletion MagicBall/SupportingFiles/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

return true
}

Expand Down
15 changes: 7 additions & 8 deletions MagicBall/Views/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19455" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="wZc-xy-7xF">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="wZc-xy-7xF">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19454"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -12,7 +11,7 @@
<objects>
<viewController id="k3K-Tr-8SL" customClass="SettingsViewController" customModule="_ball" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Wej-54-NUe">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="CMm-Ro-ayq"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand All @@ -28,15 +27,15 @@
<objects>
<viewController id="6g3-aV-6sN" customClass="HomeViewController" customModule="_ball" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="9Nh-3H-SYA">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" alpha="0.5" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="magic_ background" translatesAutoresizingMaskIntoConstraints="NO" id="NfC-Db-7hu">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WE2-SY-vwy">
<rect key="frame" x="171.5" y="430" width="71" height="36"/>
<rect key="frame" x="264.5" y="282" width="71" height="36"/>
<fontDescription key="fontDescription" type="system" pointSize="30"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
Expand All @@ -55,7 +54,7 @@
</view>
<tabBarItem key="tabBarItem" title="Home" image="house.fill" catalog="system" id="Q0t-W3-bL6"/>
<connections>
<outlet property="answer" destination="WE2-SY-vwy" id="cDJ-dd-6sG"/>
<outlet property="answerLabel" destination="WE2-SY-vwy" id="cDJ-dd-6sG"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="bdb-4J-alt" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
Expand Down