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
82 changes: 82 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
971 changes: 971 additions & 0 deletions Drink-O-Matic/Drink-O-Matic.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions Drink-O-Matic/Drink-O-Matic.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
45 changes: 45 additions & 0 deletions Drink-O-Matic/Drink-O-Matic/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// APIClient.swift
// Drink-O-Matic
//
// Created by Ramiro Coll Doñetz on 01/08/2019.
// Copyright © 2019 Ramiro Coll Doñetz. All rights reserved.
//

import Alamofire

class APIClient {
@discardableResult
private static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:@escaping (AFResult<T>)->Void) -> DataRequest {
return AF.request(route)
.responseDecodable (decoder: decoder){ (response: DataResponse<T>) in
completion(response.result)
}
}

// static func login(email: String, password: String, completion:@escaping (Result<User>)->Void) {
// performRequest(route: APIRouter.login(email: email, password: password), completion: completion)
// }

static func getDrinks(completion:@escaping (AFResult<DrinkList>)->Void) {
let jsonDecoder = JSONDecoder()
performRequest(route: APIRouter.drinks(filter: K.APIDrinksFilter.filterByCocktailGlass), decoder: jsonDecoder, completion: completion)
}

static func getDrinkDetails(drinkId: String, completion:@escaping (AFResult<DrinkDetails>)->Void) {

AF.request(APIRouter.drink(id: drinkId)).responseJSON { (response) in
switch response.result {
case .success(let value):
if let responseDict = value as? [String: Any] {
let drinkDetail = DrinkDetails.initFromDictionary(dict: responseDict)
completion(.success(drinkDetail))
}
case .failure(let error):
// error handling
completion(.failure(error))
}
}
}
}

99 changes: 99 additions & 0 deletions Drink-O-Matic/Drink-O-Matic/APIClient/APIRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// APIRouter.swift
// Drink-O-Matic
//
// Created by Ramiro Coll Doñetz on 01/08/2019.
// Copyright © 2019 Ramiro Coll Doñetz. All rights reserved.
//

import Alamofire

enum APIRouter: URLRequestConvertible {

case drinks(filter: [String:String])
case drink(id: String)

// MARK: - HTTPMethod
private var method: HTTPMethod {
switch self {
// case .login:
// return .post
case .drinks, .drink:
return .get
}
}

// MARK: - Path
private var path: String {
switch self {
// case .login:
// return "/login"
case .drinks:
return "/filter.php"
case .drink:
return "/lookup.php"
}
}

// MARK: - Parameters
private var queryParameters: Parameters? {
switch self {
case .drinks(let filter):
return filter
case .drink(let id):
return [K.APIParameterKey.drinkId: id]
}
}

// MARK: - Body Parameters
private var bodyParameters: Parameters? {
switch self {
// case .login(let email, let password):
// return [K.APIParameterKey.email: email, K.APIParameterKey.password: password]
default:
return nil
}
}

// MARK: - URLRequestConvertible
func asURLRequest() throws -> URLRequest {
//create url and query params
let baseUrl = try K.ProductionServer.baseURL.asURL()
var urlComponents = URLComponents.init(string: baseUrl.appendingPathComponent(path).absoluteString)
if let parameters = queryParameters {
urlComponents?.setQueryItems(with: parameters)
}

//protective code
guard let requestUrl = urlComponents?.url else {
throw AFError.invalidURL(url: baseUrl.appendingPathComponent(path))
}

//here we start to work with the URLRequest object
var urlRequest = URLRequest(url: requestUrl)

// HTTP Method
urlRequest.httpMethod = method.rawValue

// Common Headers
urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeaderField.acceptType.rawValue)
urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeaderField.contentType.rawValue)

// Parameters
if let parameters = bodyParameters {
do {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
} catch {
throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
}
}

return urlRequest
}
}

extension URLComponents {
mutating func setQueryItems(with parameters: Parameters) {
self.queryItems = parameters.map { URLQueryItem(name: $0.key, value: $0.value as? String) }
}
}
46 changes: 46 additions & 0 deletions Drink-O-Matic/Drink-O-Matic/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AppDelegate.swift
// Drink-O-Matic
//
// Created by Ramiro Coll Doñetz on 01/08/2019.
// Copyright © 2019 Ramiro Coll Doñetz. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


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

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

34 changes: 34 additions & 0 deletions Drink-O-Matic/Drink-O-Matic/Models/Drink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Drink.swift
// Drink-O-Matic
//
// Created by Ramiro Coll Doñetz on 01/08/2019.
// Copyright © 2019 Ramiro Coll Doñetz. All rights reserved.
//

import Foundation
import UIKit

struct DrinkList: Codable {
let drinks: [Drink]
}

struct Drink: Codable {
let name: String
let thumbUrl: String
let id: String
}

extension Drink {
//Customizing Key Names to match the naming style
enum CodingKeys: String, CodingKey {
case name = "strDrink"
case thumbUrl = "strDrinkThumb"
case id = "idDrink"
}

func highlightedImage() -> Drink {
// let scaledImage = image.resize(toWidth: image.size.width * GlobalConstants.cardHighlightedFactor)
return Drink.init(name: name, thumbUrl: thumbUrl, id: id)
}
}
32 changes: 32 additions & 0 deletions Drink-O-Matic/Drink-O-Matic/Models/DrinkDetails.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// DrinkDetails.swift
// Drink-O-Matic
//
// Created by Ramiro Coll Doñetz on 01/08/2019.
// Copyright © 2019 Ramiro Coll Doñetz. All rights reserved.
//

import UIKit

class DrinkDetails {
var instructions: String?
var ingredients = [String]()

static func initFromDictionary(dict: [String: Any]) -> DrinkDetails {
let drinkDetail = DrinkDetails()

if let drinkArray = dict["drinks"] as? [Any], let drinkDict = drinkArray.first as? [String: Any] {
//parse instructions
drinkDetail.instructions = drinkDict["strInstructions"] as? String

//parse ingredients
var ingredientCount = 1
while let ingredient = drinkDict["strIngredient\(ingredientCount)"] as? String, !ingredient.isEmpty{
drinkDetail.ingredients.append(ingredient)
ingredientCount += 1
}
}

return drinkDetail
}
}
Loading