Skip to content

Commit

Permalink
added favorites and watchlist buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkumpe committed Jan 25, 2021
1 parent 97984e6 commit b52be6c
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 28 deletions.
6 changes: 3 additions & 3 deletions KKid.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,15 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = KKid/KKid.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2.1.0.2;
CURRENT_PROJECT_VERSION = 2.1.0.3;
DEVELOPMENT_TEAM = 2T42Z3DM34;
INFOPLIST_FILE = KKid/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.1.0;
MARKETING_VERSION = 2.1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.kumpeapps.ios.KKid;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -858,7 +858,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = KKid/KKid.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2.1.0.2;
CURRENT_PROJECT_VERSION = 2.1.0.3;
DEVELOPMENT_TEAM = 2T42Z3DM34;
INFOPLIST_FILE = KKid/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
Expand Down
100 changes: 100 additions & 0 deletions KKid/Controller/MovieDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class MovieDetailViewController: UIViewController, YTSwiftyPlayerDelegate {
@IBOutlet weak var imageBackground: UIButton!
@IBOutlet weak var buttonMovieRating: UIButton!
@IBOutlet weak var buttonTmdb: UIButton!
@IBOutlet weak var buttonFavorite: UIButton!
@IBOutlet weak var buttonWatchList: UIButton!

// MARK: images
@IBOutlet weak var imageMovieRating: UIImageView!
Expand Down Expand Up @@ -66,6 +68,8 @@ class MovieDetailViewController: UIViewController, YTSwiftyPlayerDelegate {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
getMovieRating()
getFavorites()
getWatchlist()
}

// MARK: viewWillDisappear
Expand Down Expand Up @@ -149,4 +153,100 @@ class MovieDetailViewController: UIViewController, YTSwiftyPlayerDelegate {
}
}

@IBAction func pressedFavorite(_ sender: Any) {
guard let user = LoggedInUser.selectedUser else {
return
}
guard let sessionId = user.tmdbKey, sessionId != "" else {
ShowAlert.banner(title: "TMDb Account Not Linked", message: "This feature requires a TMDb account to be linked to this user via edit profile.")
return
}
selectedMovie.favorite = !selectedMovie.favorite!
updateFavoriteButton(selectedMovie.favorite!)
TMDb_Client.postFavorite(sessionId: sessionId, mediaType: "movie", mediaId: selectedMovie.id!, favorite: selectedMovie.favorite!) { (success) in
if success {
Logger.log(.success, "marked as favorite \(self.selectedMovie.favorite!)")
} else {
Logger.log(.error, "mark as favorite error")
}
}
}

@IBAction func pressedWatchList(_ sender: Any) {
guard let user = LoggedInUser.selectedUser else {
return
}
guard let sessionId = user.tmdbKey, sessionId != "" else {
ShowAlert.banner(title: "TMDb Account Not Linked", message: "This feature requires a TMDb account to be linked to this user via edit profile.")
return
}
selectedMovie.watchList = !selectedMovie.watchList!
updateWatchlistButton(selectedMovie.watchList!)
TMDb_Client.postWatchlist(sessionId: sessionId, mediaType: "movie", mediaId: selectedMovie.id!, watchlist: selectedMovie.watchList!) { (success) in
if success {
Logger.log(.success, "marked as watchList \(self.selectedMovie.watchList!)")
} else {
Logger.log(.error, "mark as watchList error")
}
}
}

// MARK: updateFavoriteButton
func updateFavoriteButton(_ favorite: Bool) {
if favorite {
buttonFavorite.alpha = 1
} else {
buttonFavorite.alpha = 0.5
}
}

// MARK: updateWatchlistButton
func updateWatchlistButton(_ watchlist: Bool) {
if watchlist {
buttonWatchList.alpha = 1
} else {
buttonWatchList.alpha = 0.5
}
}

// MARK: getFavorites
func getFavorites() {
guard let sessionID = LoggedInUser.selectedUser!.tmdbKey, sessionID != "" else {
return
}
let movieID = selectedMovie.id
selectedMovie.favorite = false
TMDb_Client.getFavoriteMovies(sessionId: sessionID) { (success, response) in
if success {
guard let movies = response?.results else {
return
}
for movie in movies where movie.id == movieID {
self.selectedMovie.favorite = true
self.updateFavoriteButton(true)
}
}
}
}

// MARK: getWatchlist
func getWatchlist() {
guard let sessionID = LoggedInUser.selectedUser!.tmdbKey, sessionID != "" else {
return
}
let movieID = selectedMovie.id
selectedMovie.watchList = false
TMDb_Client.getMovieWatchlist(sessionId: sessionID) { (success, response) in
if success {
guard let movies = response?.results else {
return
}
for movie in movies where movie.id == movieID {
self.selectedMovie.watchList = true
self.updateWatchlistButton(true)
}
}
}
}

}
11 changes: 10 additions & 1 deletion KKid/Controller/MovieSearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class MovieSearchViewController: UIViewController, UICollectionViewDelegate, UIC
}
}

// MARK: viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.currentPage = 0
self.totalPages = 0
self.movies = []
collectionView.reloadData()
performFetchMore { }
}

// MARK: viewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Expand All @@ -55,7 +65,6 @@ class MovieSearchViewController: UIViewController, UICollectionViewDelegate, UIC
super.viewWillAppear(animated)
#if targetEnvironment(simulator)
searchBar.text = "Stargate"
performFetchMore { }
#endif
}

Expand Down
29 changes: 29 additions & 0 deletions KKid/Model/TMDb Client/TMDb_Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import Foundation
import KumpeHelpers
import ContentRestrictionsKit
import Alamofire
import Alamofire_SwiftyJSON

class TMDb_Client: KumpeAPIClient {

Expand Down Expand Up @@ -240,4 +242,31 @@ class TMDb_Client: KumpeAPIClient {
}
}
}

// MARK: postFavorite
class func postFavorite(sessionId: String, mediaType: String, mediaId: Int, favorite: Bool, completion: @escaping (Bool) -> Void) {
let url = "\(TMDb_Constants.postFavorite)?api_key=\(TMDb_Constants.apiKey)&session_id=\(sessionId)"
let parameters = [
"media_type":"\(mediaType)",
"media_id":"\(mediaId)",
"favorite":favorite
] as [String : Any]
TMDb_Client.apiPost(apiUrl: url, parameters: parameters, postToBody: true) { (success, _) in
completion(success)
}
}

// MARK: postWatchlist
class func postWatchlist(sessionId: String, mediaType: String, mediaId: Int, watchlist: Bool, completion: @escaping (Bool) -> Void) {
let url = "\(TMDb_Constants.postWatchList)?api_key=\(TMDb_Constants.apiKey)&session_id=\(sessionId)"
let parameters = [
"media_type":"\(mediaType)",
"media_id":"\(mediaId)",
"watchlist":watchlist
] as [String : Any]
TMDb_Client.apiPost(apiUrl: url, parameters: parameters, postToBody: true) { (success, _) in
completion(success)
}
}

}
2 changes: 2 additions & 0 deletions KKid/Model/TMDb Client/TMDb_Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public struct TMDb_Constants {
static let searchUrl = "\(baseUrl)/search"
static let favoriteMoviesUrl = "\(baseUrl)/account/1/favorite/movies"
static let watchlistMoviesUrl = "\(baseUrl)/account/1/watchlist/movies"
static let postFavorite = "\(baseUrl)/account/1/favorite"
static let postWatchList = "\(baseUrl)/account/1/watchlist"
static let searchMoviesUrl = "\(searchUrl)/movie"
static let imageBaseUrl = "https://image.tmdb.org/t/p"
static let trailerBaseURL = "https://www.youtube.com/watch?v="
Expand Down
10 changes: 9 additions & 1 deletion KKid/Storyboards/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
<nil key="highlightedColor"/>
</label>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="myx-O7-kWp" customClass="BadgeSwift" customModule="BadgeSwift">
<rect key="frame" x="64" y="0.0" width="24.5" height="24.5"/>
<rect key="frame" x="65.5" y="0.0" width="23" height="21"/>
<accessibility key="accessibilityConfiguration" label="0"/>
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="23" id="BDO-Ij-O96"/>
Expand Down Expand Up @@ -1025,10 +1025,16 @@
<constraint firstAttribute="height" constant="43" id="5pI-3R-vmA"/>
</constraints>
<state key="normal" image="icons8-test_passed"/>
<connections>
<action selector="pressedWatchList:" destination="zkE-tG-dD6" eventType="touchUpInside" id="fxf-Qv-HDR"/>
</connections>
</button>
<button opaque="NO" alpha="0.5" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="N5T-wY-g2B" userLabel="ButtonFavorite">
<rect key="frame" x="322" y="40.5" width="38" height="43"/>
<state key="normal" image="icons8-heart_outline"/>
<connections>
<action selector="pressedFavorite:" destination="zkE-tG-dD6" eventType="touchUpInside" id="L89-cK-rYj"/>
</connections>
</button>
</subviews>
<constraints>
Expand Down Expand Up @@ -1096,8 +1102,10 @@
</barButtonItem>
</navigationItem>
<connections>
<outlet property="buttonFavorite" destination="N5T-wY-g2B" id="PX0-bE-YBw"/>
<outlet property="buttonMovieRating" destination="pqM-zT-uJB" id="i88-tr-rxI"/>
<outlet property="buttonTmdb" destination="eGt-ei-RUz" id="T4T-yV-ikU"/>
<outlet property="buttonWatchList" destination="WZA-3h-XFF" id="0np-0d-f1O"/>
<outlet property="imageBackground" destination="h7p-fZ-Tas" id="Ab1-zr-36x"/>
<outlet property="imageMovieRating" destination="Kgm-AV-PT5" id="Gg6-9C-RSe"/>
<outlet property="labelOverview" destination="TCS-S6-J2l" id="AZd-os-Yen"/>
Expand Down
46 changes: 23 additions & 23 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,55 @@ PODS:
- Google-Mobile-Ads-SDK (7.69.0):
- GoogleAppMeasurement (~> 7.0)
- GoogleUserMessagingPlatform (~> 1.1)
- GoogleAppMeasurement (7.3.0):
- GoogleAppMeasurement (7.4.0):
- GoogleUtilities/AppDelegateSwizzler (~> 7.0)
- GoogleUtilities/MethodSwizzler (~> 7.0)
- GoogleUtilities/Network (~> 7.0)
- "GoogleUtilities/NSData+zlib (~> 7.0)"
- nanopb (~> 2.30906.0)
- nanopb (~> 2.30907.0)
- GoogleUserMessagingPlatform (1.4.0)
- GoogleUtilities/AppDelegateSwizzler (7.1.1):
- GoogleUtilities/AppDelegateSwizzler (7.2.0):
- GoogleUtilities/Environment
- GoogleUtilities/Logger
- GoogleUtilities/Network
- GoogleUtilities/Environment (7.1.1):
- GoogleUtilities/Environment (7.2.0):
- PromisesObjC (~> 1.2)
- GoogleUtilities/Logger (7.1.1):
- GoogleUtilities/Logger (7.2.0):
- GoogleUtilities/Environment
- GoogleUtilities/MethodSwizzler (7.1.1):
- GoogleUtilities/MethodSwizzler (7.2.0):
- GoogleUtilities/Logger
- GoogleUtilities/Network (7.1.1):
- GoogleUtilities/Network (7.2.0):
- GoogleUtilities/Logger
- "GoogleUtilities/NSData+zlib"
- GoogleUtilities/Reachability
- "GoogleUtilities/NSData+zlib (7.1.1)"
- GoogleUtilities/Reachability (7.1.1):
- "GoogleUtilities/NSData+zlib (7.2.0)"
- GoogleUtilities/Reachability (7.2.0):
- GoogleUtilities/Logger
- Haptico (1.1.1)
- JKRefresher (1.3)
- Keys (1.0.1)
- Kingfisher (6.0.1)
- KumpeHelpers (1.1.10):
- KumpeHelpers (1.1.12):
- Alamofire-SwiftyJSON
- CollectionViewCenteredFlowLayout
- DeviceKit
- Haptico
- ReachabilitySwift
- SwiftMessages
- Sync
- nanopb (2.30906.0):
- nanopb/decode (= 2.30906.0)
- nanopb/encode (= 2.30906.0)
- nanopb/decode (2.30906.0)
- nanopb/encode (2.30906.0)
- nanopb (2.30907.0):
- nanopb/decode (= 2.30907.0)
- nanopb/encode (= 2.30907.0)
- nanopb/decode (2.30907.0)
- nanopb/encode (2.30907.0)
- PrivacyKit (1.0.1)
- PromisesObjC (1.2.12)
- ReachabilitySwift (5.0.0)
- Smile (2.1.1)
- Snowflake (0.5.0)
- SwiftMessages (8.0.5):
- SwiftMessages/App (= 8.0.5)
- SwiftMessages/App (8.0.5)
- SwiftMessages (9.0.0):
- SwiftMessages/App (= 9.0.0)
- SwiftMessages/App (9.0.0)
- SwiftyFORM (1.8.4)
- SwiftyJSON (4.0.0)
- Sync (6.5.0)
Expand Down Expand Up @@ -142,21 +142,21 @@ SPEC CHECKSUMS:
ContentRestrictionsKit: db88b231efbc00ee45f3eefae04ba7b4219fe433
DeviceKit: cadb5ff25d45fe186778bf2d0fd2ea4750542189
Google-Mobile-Ads-SDK: 2f288748a42920d1c744946a460896a95b0e9110
GoogleAppMeasurement: 8d3c0aeede16ab7764144b5a4ca8e1d4323841b7
GoogleAppMeasurement: 688d7f00e2894d9e13823ed9a028b13b993bc277
GoogleUserMessagingPlatform: b168e8c46cd8f92aa3e34b584c4ca78a411ce367
GoogleUtilities: 3dc4ff0d5e4840e2fa8eef0889620e8c33d4218c
GoogleUtilities: d866834472f1324d080496bc67ab3ce5d0d46027
Haptico: 3ab938425737e2b90939d41e1b23538cdbe1cf44
JKRefresher: a88df1a1be91a93de78b05d18b357677b1bea62e
Keys: a576f4c9c1c641ca913a959a9c62ed3f215a8de9
Kingfisher: adde87a4f74f6a3845395769354efff593581740
KumpeHelpers: 8aa2f5ba43f7fd726b412f66d0dab226717f8b77
nanopb: 1bf24dd71191072e120b83dd02d08f3da0d65e53
KumpeHelpers: 13fbf88c78e9bddd25e347900604e17196f52e9d
nanopb: 59221d7f958fb711001e6a449489542d92ae113e
PrivacyKit: 2e77ab49c5bfe0e6cedfd423a5ad4c7744942d54
PromisesObjC: 3113f7f76903778cf4a0586bd1ab89329a0b7b97
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
Smile: 50023dc068fb11984df713916970b89e6b705d68
Snowflake: fbaa96fd607f00c06ccdbd4484bc9b2d67646beb
SwiftMessages: 539fd54a766bf08ec667e90aa141c44b8b8aaa55
SwiftMessages: 55274ca3a239062398603008ed81924cfd1fe798
SwiftyFORM: ce2dc9afb5b4488715112108a114c6d26e0b2836
SwiftyJSON: 070dabdcb1beb81b247c65ffa3a79dbbfb3b48aa
Sync: 86bb8aa0ea2f955ea5ac97b7b73129f86591c39e
Expand Down

0 comments on commit b52be6c

Please sign in to comment.