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

task 12008+09+10: Search Movie Screen: Create UI + Handle Event + Fetch API #8

Open
wants to merge 1 commit 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
117 changes: 97 additions & 20 deletions MovieApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions MovieApp/Assembler/Assembler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ protocol Assembler: class,
GettingStartedAssembler,
MovieDetailAssembler,
MainAssembler,
SearchMovieAssembler,
AppAssembler {
}

Expand Down
1 change: 0 additions & 1 deletion MovieApp/Data/Source/Remote/API/Request/BaseRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

import Foundation
import Alamofire

class BaseRequest: NSObject {
Expand Down
3 changes: 0 additions & 3 deletions MovieApp/Data/Source/Remote/API/Request/CastListRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

import ObjectMapper
import Alamofire

final class CastListRequest: BaseRequest {
required init(movieId: Int) {
super.init(url: URLs.movieApi + "/\(movieId)/credits", requestType: .get, body: [:])
Expand Down
21 changes: 21 additions & 0 deletions MovieApp/Data/Source/Remote/API/Request/DiscoverMovieRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DiscoverMovieRequest.swift
// MovieApp
//
// Created by Phan Dinh Van on 5/23/19.
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

final class DiscoverMovieRequest: BaseRequest {
required init(genreIdList: [Int], page: Int) {
var genreIdStringList = ""
for (index, id) in genreIdList.enumerated() {
genreIdStringList += index == 0 ? "\(id)" : ",\(id)"
}
let body: [String: Any] = [
"with_genres": genreIdStringList,
"page": page
]
super.init(url: URLs.movieDiscoverApi, requestType: .get, body: body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

import ObjectMapper
import Alamofire

final class GenreListRequest: BaseRequest {
required init() {
super.init(url: URLs.genreListApi, requestType: .get, body: [:])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

import ObjectMapper
import Alamofire

final class MovieDetailRequest: BaseRequest {
required init(movieId: Int) {
super.init(url: URLs.movieApi + "/\(movieId)", requestType: .get, body: [:])
Expand Down
22 changes: 22 additions & 0 deletions MovieApp/Data/Source/Remote/API/Request/MovieResultResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// MovieResultResponse.swift
// MovieApp
//
// Created by Phan Dinh Van on 5/23/19.
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

import ObjectMapper

final class MovieResultResponse: Mappable {
var totalPages = 0
var movies = [Movie]()

required init(map: Map) {
}

func mapping(map: Map) {
totalPages <- map["total_pages"]
movies <- map["results"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

final class PopularMovieRequest: BaseRequest {
required init(page: Int) {
let body: [String: Any] = [
"page": page,
]
let body: [String: Any] = ["page": page]
super.init(url: URLs.popularMovieApi, requestType: .get, body: body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import ObjectMapper

final class PopularMovieResponse:Mappable {
final class PopularMovieResponse: Mappable {
var movies = [Movie]()

required init(map: Map) {
Expand Down
17 changes: 17 additions & 0 deletions MovieApp/Data/Source/Remote/API/Request/SearchMovieRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// SearchMovieRequest.swift
// MovieApp
//
// Created by Phan Dinh Van on 5/23/19.
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

final class SearchMovieRequest: BaseRequest {
required init(query: String, page: Int) {
let body: [String: Any] = [
"query": query,
"page": page
]
super.init(url: URLs.searchMovieApi, requestType: .get, body: body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

final class UpcommingMovieRequest: BaseRequest {
required init(page: Int) {
let body: [String: Any] = [
"page": page,
]
let body: [String: Any] = ["page": page]
super.init(url: URLs.upcomingMovieApi, requestType: .get, body: body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

import ObjectMapper
import Alamofire

final class VideoListRequest: BaseRequest {
required init(movieId: Int) {
super.init(url: URLs.movieApi + "/\(movieId)/videos", requestType: .get, body: [:])
Expand Down
2 changes: 1 addition & 1 deletion MovieApp/Extension/UIColor+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension UIColor {
)
}

static let gradientGettingStartedBackground = [UIColor(rgb: 0x6C6C6C).cgColor, UIColor(rgb: 0x000000).cgColor]
static let gradientBackground = [UIColor(rgb: 0x6C6C6C).cgColor, UIColor(rgb: 0x000000).cgColor]
static let gettingStartedButton = UIColor(rgb: 0x49525D)

public class var lightColor: UIColor {
Expand Down
15 changes: 15 additions & 0 deletions MovieApp/Extension/UISearchBar+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// UISearchBar+.swift
// MovieApp
//
// Created by Phan Dinh Van on 5/24/19.
// Copyright © 2019 nguyen.nam.khanh. All rights reserved.
//

extension UISearchBar {
public func setSerchTextcolor(color: UIColor) {
let clrChange = subviews.flatMap { $0.subviews }
guard let sc = (clrChange.first { $0 is UITextField }) as? UITextField else { return }
sc.textColor = color
}
}
16 changes: 16 additions & 0 deletions MovieApp/Repositories/MovieRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ protocol MovieRepository {
func getMovieDetail(input: MovieDetailRequest) -> Observable<MovieDetail>
func getCastList(input: CastListRequest) -> Observable<[Cast]>
func getVideoList(input: VideoListRequest) -> Observable<[Video]>
func searchMovie(input: SearchMovieRequest) -> Observable<([Movie], Int)>
func discoverMovie(input: DiscoverMovieRequest) -> Observable<([Movie], Int)>
}

final class MovieRepositoryImpl: MovieRepository {
Expand Down Expand Up @@ -51,4 +53,18 @@ final class MovieRepositoryImpl: MovieRepository {
return response.videos
})
}

func searchMovie(input: SearchMovieRequest) -> Observable<([Movie], Int)> {
return api.request(input: input)
.map({ (response: MovieResultResponse) -> ([Movie], Int) in
return (response.movies, response.totalPages)
})
}

func discoverMovie(input: DiscoverMovieRequest) -> Observable<([Movie], Int)> {
return api.request(input: input)
.map({ (response: MovieResultResponse) -> ([Movie], Int) in
return (response.movies, response.totalPages)
})
}
}
6 changes: 3 additions & 3 deletions MovieApp/Repositories/RealmRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ protocol RealmRepository {
final class RealmRepositoryImpl: RealmRepository {
private let database = Database.share

func getObjects<T>(fileName: String, objType: T.Type) -> Observable<Results<T>> where T : Object {
func getObjects<T: Object>(fileName: String, objType: T.Type) -> Observable<Results<T>> {
return database.getObjects(fileName: fileName, objType: objType)
}

func deleteObject<T>(fileName: String, object: T) -> Observable<Void> where T : Object {
func deleteObject<T: Object>(fileName: String, object: T) -> Observable<Void> {
return database.deleteObject(fileName: fileName, object: object)
}

func deleteAll(fileName: String) -> Observable<Void> {
return database.deleteAll(fileName: fileName)
}

func saveObject<T>(fileName: String, object: T) -> Observable<Void> where T : Object {
func saveObject<T: Object>(fileName: String, object: T) -> Observable<Void> {
return database.saveObject(fileName: fileName, object: object)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

final class GettingStartedGenreCell: UICollectionViewCell, NibReusable {
final class GenreCell: UICollectionViewCell, NibReusable {
@IBOutlet private weak var genreTitleLabel: UILabel!

override func awakeFromNib() {
Expand All @@ -18,6 +18,13 @@ final class GettingStartedGenreCell: UICollectionViewCell, NibReusable {
}
}

override func prepareForReuse() {
super.prepareForReuse()
self.do {
$0.backgroundColor = .clear
}
}

override func draw(_ rect: CGRect) {
super.draw(rect)
self.do {
Expand All @@ -32,6 +39,12 @@ final class GettingStartedGenreCell: UICollectionViewCell, NibReusable {
genreTitleLabel.text = viewModel.title
}

func updateCell(isSelected: Bool) {
self.do {
$0.backgroundColor = isSelected ? .gray : .clear
}
}

func toggleState() {
self.do {
$0.backgroundColor = $0.backgroundColor == .clear ? .gray : .clear
Expand Down
56 changes: 56 additions & 0 deletions MovieApp/Scenes/GettingStarted/Cell/GenreCell.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="GenreCell" customModule="MovieApp" customModuleProvider="target"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="GenreCell" id="2BO-BV-A1Q" customClass="GenreCell" customModule="MovieApp" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="81" height="64"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO">
<rect key="frame" x="0.0" y="0.0" width="81" height="64"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ebx-eu-kAA">
<rect key="frame" x="0.0" y="0.0" width="81" height="64"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MRL-jo-7gO">
<rect key="frame" x="10" y="21.5" width="61" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="MRL-jo-7gO" firstAttribute="centerY" secondItem="ebx-eu-kAA" secondAttribute="centerY" id="2ak-FA-ng2"/>
<constraint firstItem="MRL-jo-7gO" firstAttribute="centerX" secondItem="ebx-eu-kAA" secondAttribute="centerX" id="DH2-7S-y9R"/>
<constraint firstAttribute="trailing" secondItem="MRL-jo-7gO" secondAttribute="trailing" priority="500" constant="10" id="GVz-5R-7Wm"/>
<constraint firstItem="MRL-jo-7gO" firstAttribute="leading" secondItem="ebx-eu-kAA" secondAttribute="leading" priority="500" constant="10" id="sOx-fw-0pT"/>
</constraints>
</view>
</subviews>
</view>
<constraints>
<constraint firstItem="ebx-eu-kAA" firstAttribute="leading" secondItem="2BO-BV-A1Q" secondAttribute="leading" id="EOn-wu-Wgq"/>
<constraint firstAttribute="trailing" secondItem="ebx-eu-kAA" secondAttribute="trailing" id="mbv-D8-nUP"/>
<constraint firstItem="ebx-eu-kAA" firstAttribute="top" secondItem="2BO-BV-A1Q" secondAttribute="top" id="o8D-xI-Rhy"/>
<constraint firstAttribute="bottom" secondItem="ebx-eu-kAA" secondAttribute="bottom" id="yC8-Vw-gRR"/>
</constraints>
<viewLayoutGuide key="safeArea" id="kOp-1r-Gcs"/>
<size key="customSize" width="81" height="64"/>
<connections>
<outlet property="genreTitleLabel" destination="MRL-jo-7gO" id="pAY-eL-0L5"/>
</connections>
<point key="canvasLocation" x="-93.478260869565219" y="-61.607142857142854"/>
</collectionViewCell>
</objects>
</document>
3 changes: 3 additions & 0 deletions MovieApp/Scenes/GettingStarted/Cell/GenreItemViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ struct GenreItemViewModel {
var title: String {
return genre.name
}
var selected: Bool {
return genre.selected
}

init(genre: Genre) {
self.genre = genre
Expand Down
42 changes: 0 additions & 42 deletions MovieApp/Scenes/GettingStarted/Cell/GettingStartedGenreCell.xib

This file was deleted.

Loading