Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote test #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions MovieSearch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# MovieSearch

Lets you search for movies

## Overview

Let you search for movies by name.

## Topics

### Technology Choise

1. Used Swift UI instead of UI Kit just to showcase I can code in Swift UI as well.
2. Used User defaults to presist search history, as the information is not huge.

634 changes: 634 additions & 0 deletions MovieSearch.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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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>SchemeUserState</key>
<dict>
<key>MovieSearch.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
74 changes: 74 additions & 0 deletions MovieSearch/APIManager/APIManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// APIManager.swift
// MovieSearch
//
// Created by Ananth Jk on 19/01/2022.
//

import Foundation

enum APIError: Error {
case networkError
case parsingError
}

final class APIManager {

static let shared = APIManager()

let apiKey = "a303f5f9"
let host = "https://www.omdbapi.com/"

private let urlSession: URLSession

init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

func execute<Value: Decodable>(_ request: Request<Value>, completion: @escaping (Result<Value, APIError>) -> Void) {
urlSession.dataTask(with: urlRequest(for: request)) { responseData, response, _ in
if let data = responseData {
let response: Value
do {
response = try JSONDecoder().decode(Value.self, from: data)
} catch(let err) {
print(err)
completion(.failure(.parsingError))
return
}

completion(.success(response))
} else {
completion(.failure(.networkError))
}
}.resume()
}

private func urlRequest<Value>(for request: Request<Value>) -> URLRequest {
let url = URL(host, apiKey, request)
var result = URLRequest(url: url)
result.httpMethod = request.method.rawValue
return result
}

}

extension URL {
func url(with queryItems: [URLQueryItem]) -> URL {
var components = URLComponents(url: self, resolvingAgainstBaseURL: true)!
components.queryItems = (components.queryItems ?? []) + queryItems
return components.url!
}

init<Value>(_ host: String, _ apiKey: String, _ request: Request<Value>) {

let queryItems = [ ("apikey", apiKey), (request.parameter?.rawValue ?? "s", request.parameterValue) ]
.map { name, value in URLQueryItem(name: name, value: "\(value ?? "")") }

let url = URL(string: host)!
.appendingPathComponent(request.path ?? "")
.url(with: queryItems)

self.init(string: url.absoluteString)!
}
}
33 changes: 33 additions & 0 deletions MovieSearch/APIManager/Request.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Request.swift
// MovieSearch
//
// Created by Ananth Jk on 19/01/2022.
//

import Foundation

enum Method: String {
case get = "GET"
}
enum Parameters: String {
case search = "s"
case id = "i"
case title = "t"
}

struct Request<Value> {

var method: Method
var parameterValue: String?
var path: String?
var parameter: Parameters?

init(method: Method = .get, parameter: Parameters = .search, parameterValue: String?, path: String?) {
self.method = method
self.path = path
self.parameter = parameter
self.parameterValue = parameterValue
}

}
11 changes: 11 additions & 0 deletions MovieSearch/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
98 changes: 98 additions & 0 deletions MovieSearch/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions MovieSearch/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
63 changes: 63 additions & 0 deletions MovieSearch/Model/Movie.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Movie.swift
// MovieSearch
//
// Created by Ananth Jk on 19/01/2022.
//

import Foundation
import UIKit

struct Movie: Decodable, Identifiable {
var id: String {
imdbID
}
let title: String
let year: String
var imdbID: String
let plot: String?
let imdbRating: String?
let genre: String?
let type: String?
let poster: String

enum CodingKeys: String, CodingKey {
case imdbID
case imdbRating
case title = "Title"
case year = "Year"
case plot = "Plot"
case genre = "Genre"
case poster = "Poster"
case type = "Type"
}
}
extension Movie {
static func getMovieDetails(for id: String) -> Request<Movie> {
Request(method: .get, parameter: .id, parameterValue: id, path: nil)
}
static func getImage(for movie: Movie, completion: @escaping (_ image: UIImage) -> Void) {
let imageURL = URL(string: movie.poster)
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL!)
DispatchQueue.main.async {
guard let data = data else { return }
completion(UIImage(data: data) ?? UIImage(systemName: "photo")!)
}
}
}
}

struct Search: Decodable {
var movies: [Movie]

enum CodingKeys: String, CodingKey {
case movies = "Search"
}
}

extension Search {
static func search(name: String) -> Request<Search> {
Request(parameterValue: name, path: nil)
}
}
Loading