From 1dfc04f79fd2dd71d241f2c503b954d8cd0a7cf7 Mon Sep 17 00:00:00 2001 From: Nick Thompson Date: Sat, 12 May 2018 19:48:28 -0400 Subject: [PATCH] Added settings/about page and favorite teams --- LazyMan-iOS.xcodeproj/project.pbxproj | 48 +- LazyMan-iOS/AppDelegate.swift | 2 + .../GameList/Base.lproj/GameList.storyboard | 14 + .../GameList/GameListViewController.swift | 13 +- LazyMan-iOS/GameList/GameTableViewCell.swift | 9 + .../GameView/Base.lproj/Game.storyboard | 2 + LazyMan-iOS/GameView/GamePresenter.swift | 6 +- LazyMan-iOS/GameView/GameViewController.swift | 14 +- LazyMan-iOS/Managers/SettingsManager.swift | 149 ++++++ LazyMan-iOS/Managers/UpdateManager.swift | 63 +++ LazyMan-iOS/Models/Game.swift | 17 +- .../Base.lproj/LaunchScreen.storyboard | 2 +- .../Base.lproj/LaunchScreen.storyboardbak | 61 --- LazyMan-iOS/Resources/Info.plist | 8 +- .../Settings/Base.lproj/Settings.storyboard | 446 ++++++++++++++++++ .../SettingsOptionsViewController.swift | 81 ++++ .../Settings/SettingsViewController.swift | 151 ++++++ 17 files changed, 1013 insertions(+), 73 deletions(-) create mode 100644 LazyMan-iOS/Managers/SettingsManager.swift create mode 100644 LazyMan-iOS/Managers/UpdateManager.swift delete mode 100644 LazyMan-iOS/Resources/Base.lproj/LaunchScreen.storyboardbak create mode 100644 LazyMan-iOS/Settings/Base.lproj/Settings.storyboard create mode 100644 LazyMan-iOS/Settings/SettingsOptionsViewController.swift create mode 100644 LazyMan-iOS/Settings/SettingsViewController.swift diff --git a/LazyMan-iOS.xcodeproj/project.pbxproj b/LazyMan-iOS.xcodeproj/project.pbxproj index 4e11289..88275ed 100644 --- a/LazyMan-iOS.xcodeproj/project.pbxproj +++ b/LazyMan-iOS.xcodeproj/project.pbxproj @@ -35,8 +35,13 @@ 01D0F3C3205DBA8B008174EC /* FeedPlaylist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D0F3C2205DBA8B008174EC /* FeedPlaylist.swift */; }; 01DC7B952092530A003C311A /* GameListTableBackgroundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DC7B942092530A003C311A /* GameListTableBackgroundView.swift */; }; 01DC7B9720925325003C311A /* GameListTableBackgroundView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 01DC7B9620925325003C311A /* GameListTableBackgroundView.xib */; }; + 01DC7B9F2092BE6B003C311A /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DC7B9E2092BE6B003C311A /* SettingsViewController.swift */; }; + 01DC7BA020940B23003C311A /* Settings.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 01DC7BA220940B23003C311A /* Settings.storyboard */; }; + 01DC7BA4209414AC003C311A /* SettingsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DC7BA3209414AC003C311A /* SettingsManager.swift */; }; 01E593CA204B03F800CE338A /* GameManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E593C9204B03F800CE338A /* GameManager.swift */; }; 01E593CE204B07E400CE338A /* Game.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E593CD204B07E400CE338A /* Game.swift */; }; + 01E9A71120A75DA600AB231B /* UpdateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E9A71020A75DA600AB231B /* UpdateManager.swift */; }; + 01E9A71320A772B100AB231B /* SettingsOptionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E9A71220A772B100AB231B /* SettingsOptionsViewController.swift */; }; 01EDB67E208A8DD700E74B52 /* String+replace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01EDB67D208A8DD700E74B52 /* String+replace.swift */; }; 2DBB500360049A35282D0F72 /* Pods_LazyMan_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6692BA5DC5F8089A7F24FDEC /* Pods_LazyMan_iOS.framework */; }; /* End PBXBuildFile section */ @@ -72,8 +77,13 @@ 01D0F3CA2061CFB9008174EC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Game.storyboard; sourceTree = ""; }; 01DC7B942092530A003C311A /* GameListTableBackgroundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameListTableBackgroundView.swift; sourceTree = ""; }; 01DC7B9620925325003C311A /* GameListTableBackgroundView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GameListTableBackgroundView.xib; sourceTree = ""; }; + 01DC7B9E2092BE6B003C311A /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = ""; }; + 01DC7BA120940B23003C311A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Settings.storyboard; sourceTree = ""; }; + 01DC7BA3209414AC003C311A /* SettingsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsManager.swift; sourceTree = ""; }; 01E593C9204B03F800CE338A /* GameManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameManager.swift; sourceTree = ""; }; 01E593CD204B07E400CE338A /* Game.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Game.swift; sourceTree = ""; }; + 01E9A71020A75DA600AB231B /* UpdateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateManager.swift; sourceTree = ""; }; + 01E9A71220A772B100AB231B /* SettingsOptionsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsOptionsViewController.swift; sourceTree = ""; }; 01EDB67D208A8DD700E74B52 /* String+replace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+replace.swift"; sourceTree = ""; }; 6692BA5DC5F8089A7F24FDEC /* Pods_LazyMan_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LazyMan_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; }; AB4E0BF11AC215B0A921B72A /* Pods-LazyMan-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LazyMan-iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-LazyMan-iOS/Pods-LazyMan-iOS.release.xcconfig"; sourceTree = ""; }; @@ -113,6 +123,7 @@ 012CB8AD203A3E7C00E76740 /* LazyMan-iOS */ = { isa = PBXGroup; children = ( + 01DC7B9B2092BB99003C311A /* Settings */, 01DC7B99209293F1003C311A /* Managers */, 01DC7B98209293A8003C311A /* Models */, 019387BB2081396400D1CDBC /* Example Schedules */, @@ -201,10 +212,22 @@ children = ( 015F345D2050CFC40005453C /* TeamManager.swift */, 01E593C9204B03F800CE338A /* GameManager.swift */, + 01DC7BA3209414AC003C311A /* SettingsManager.swift */, + 01E9A71020A75DA600AB231B /* UpdateManager.swift */, ); path = Managers; sourceTree = ""; }; + 01DC7B9B2092BB99003C311A /* Settings */ = { + isa = PBXGroup; + children = ( + 01DC7BA220940B23003C311A /* Settings.storyboard */, + 01DC7B9E2092BE6B003C311A /* SettingsViewController.swift */, + 01E9A71220A772B100AB231B /* SettingsOptionsViewController.swift */, + ); + path = Settings; + sourceTree = ""; + }; 1A68C0BF12E806DE05FE9B6A /* Pods */ = { isa = PBXGroup; children = ( @@ -232,7 +255,7 @@ 1A27598C587C0F45B8858311 /* [CP] Check Pods Manifest.lock */, 012CB8A7203A3E7C00E76740 /* Sources */, 012CB8A8203A3E7C00E76740 /* Frameworks */, - 01DC7B9A2092A4EA003C311A /* ShellScript */, + 01DC7B9A2092A4EA003C311A /* Build Number and Storyboard Labels */, 012CB8A9203A3E7C00E76740 /* Resources */, 426B6D90171E64CC4320375D /* [CP] Embed Pods Frameworks */, 217844BAFF81752828390607 /* [CP] Copy Pods Resources */, @@ -259,6 +282,11 @@ 012CB8AA203A3E7C00E76740 = { CreatedOnToolsVersion = 9.2; ProvisioningStyle = Automatic; + SystemCapabilities = { + com.apple.BackgroundModes = { + enabled = 1; + }; + }; }; }; }; @@ -294,24 +322,26 @@ 010DB56720423DF300DFB4A0 /* Logos.xcassets in Resources */, 01344C862061D0EC0040BDE7 /* Game.storyboard in Resources */, 0179AEC22076DB5D00539C6D /* nhlschedule2018-04-05.json in Resources */, + 01DC7BA020940B23003C311A /* Settings.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ - 01DC7B9A2092A4EA003C311A /* ShellScript */ = { + 01DC7B9A2092A4EA003C311A /* Build Number and Storyboard Labels */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); + name = "Build Number and Storyboard Labels"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "# ON/OFF Script Toggle (script ON with #, script OFF without #)\n#exit 0\n\n# Increment Build Number Bool (Increment ON with true, increment OFF with false)\nshouldIncrement=true\n\n# App vesion / Build version constants\nversionNumber=$(/usr/libexec/PlistBuddy -c \"Print CFBundleShortVersionString\" \"$INFOPLIST_FILE\")\nbuildNumber=$(/usr/libexec/PlistBuddy -c \"Print CFBundleVersion\" \"$INFOPLIST_FILE\")\n\n# Increment build number\nif [ \"$shouldIncrement\" = true ]; then\nbuildNumber=$(($buildNumber + 1))\n/usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"$INFOPLIST_FILE\"\nfi\n\n# Output version & build numbers into a label on LaunchScreen.storyboard\nsed -i bak -e \"/userLabel=\\\"APP_VERSION\\\"/s/text=\\\"[^\\\"]*\\\"/text=\\\"Version: $versionNumber – Build: $buildNumber\\\"/\" \"$PROJECT_DIR/$PROJECT_NAME/Resources/Base.lproj/LaunchScreen.storyboard\""; + shellScript = "# ON/OFF Script Toggle (script ON with #, script OFF without #)\n#exit 0\n\n# Increment Build Number Bool (Increment ON with true, increment OFF with false)\nshouldIncrement=true\n\n# App vesion / Build version constants\nversionNumber=$(/usr/libexec/PlistBuddy -c \"Print CFBundleShortVersionString\" \"$INFOPLIST_FILE\")\nbuildNumber=$(/usr/libexec/PlistBuddy -c \"Print CFBundleVersion\" \"$INFOPLIST_FILE\")\n\n# Increment build number\nif [ \"$shouldIncrement\" = true ]; then\nbuildNumber=$(($buildNumber + 1))\n/usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"$INFOPLIST_FILE\"\nfi\n\n# Output version & build numbers into a label on LaunchScreen.storyboard\nsed -i bak -e \"/userLabel=\\\"APP_VERSION\\\"/s/text=\\\"[^\\\"]*\\\"/text=\\\"Version: $versionNumber – Build: $buildNumber\\\"/\" \"$PROJECT_DIR/$PROJECT_NAME/Resources/Base.lproj/LaunchScreen.storyboard\"\nsed -i bak -e \"/userLabel=\\\"APP_VERSION\\\"/s/text=\\\"[^\\\"]*\\\"/text=\\\"Version: $versionNumber – Build: $buildNumber\\\"/\" \"$PROJECT_DIR/$PROJECT_NAME/Settings/Base.lproj/Settings.storyboard\""; }; 1A27598C587C0F45B8858311 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; @@ -390,12 +420,16 @@ 015F34622055F1550005453C /* GameSettingsOptionsViewController.swift in Sources */, 013959EA207ADF3B00E37E9C /* JSONLoader.swift in Sources */, 01EDB67E208A8DD700E74B52 /* String+replace.swift in Sources */, + 01DC7B9F2092BE6B003C311A /* SettingsViewController.swift in Sources */, 01794C6C2072FFD5008FFFEE /* GameOptionSelector.swift in Sources */, 01344C8B2061D32E0040BDE7 /* GamePresenter.swift in Sources */, 01D0F3C3205DBA8B008174EC /* FeedPlaylist.swift in Sources */, 012CB8AF203A3E7C00E76740 /* AppDelegate.swift in Sources */, + 01DC7BA4209414AC003C311A /* SettingsManager.swift in Sources */, 010E4584203A6964001EF46C /* GameListViewController.swift in Sources */, + 01E9A71320A772B100AB231B /* SettingsOptionsViewController.swift in Sources */, 015F345E2050CFC40005453C /* TeamManager.swift in Sources */, + 01E9A71120A75DA600AB231B /* UpdateManager.swift in Sources */, 01434A742040D3FF0094E221 /* GameTableViewCell.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -427,6 +461,14 @@ name = Game.storyboard; sourceTree = ""; }; + 01DC7BA220940B23003C311A /* Settings.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 01DC7BA120940B23003C311A /* Base */, + ); + name = Settings.storyboard; + sourceTree = ""; + }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ diff --git a/LazyMan-iOS/AppDelegate.swift b/LazyMan-iOS/AppDelegate.swift index 8510dda..71cd064 100644 --- a/LazyMan-iOS/AppDelegate.swift +++ b/LazyMan-iOS/AppDelegate.swift @@ -22,6 +22,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele let selectedView = UIView() selectedView.backgroundColor = UIColor.darkGray UITableViewCell.appearance().selectedBackgroundView = selectedView + UITableViewCell.appearance().backgroundColor = .black + // Configures split view controller if let svc = self.window?.rootViewController as? UISplitViewController diff --git a/LazyMan-iOS/GameList/Base.lproj/GameList.storyboard b/LazyMan-iOS/GameList/Base.lproj/GameList.storyboard index d9e8670..5f5b36b 100644 --- a/LazyMan-iOS/GameList/Base.lproj/GameList.storyboard +++ b/LazyMan-iOS/GameList/Base.lproj/GameList.storyboard @@ -127,6 +127,11 @@ + + + + + @@ -142,6 +147,14 @@ + + + + + + + + @@ -320,5 +333,6 @@ + diff --git a/LazyMan-iOS/GameList/GameListViewController.swift b/LazyMan-iOS/GameList/GameListViewController.swift index 2c50298..9c8fac2 100644 --- a/LazyMan-iOS/GameList/GameListViewController.swift +++ b/LazyMan-iOS/GameList/GameListViewController.swift @@ -82,6 +82,8 @@ class GameListViewController: UIViewController, GameListViewControllerType override func loadView() { super.loadView() + self.league = SettingsManager.shared.defaultLeague + self.leagueControl.selectedSegmentIndex = SettingsManager.shared.defaultLeague == .NHL ? 0 : 1 self.splitViewController?.delegate = self self.calendar.scope = .week self.weekFormatter.dateFormat = "EEEE MMMM d, yyyy" @@ -89,6 +91,15 @@ class GameListViewController: UIViewController, GameListViewControllerType NotificationCenter.default.addObserver(self, selector: #selector(updateToday), name: .UIApplicationWillEnterForeground, object: nil) } + override func viewDidLoad() + { + super.viewDidLoad() + if SettingsManager.shared.versionUpdates + { + UpdateManager.checkUpdate(completion: self.showMessage) + } + } + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) @@ -114,7 +125,7 @@ class GameListViewController: UIViewController, GameListViewControllerType // MARK: - Private - private func showError(message: String) + private func showMessage(message: String) { let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert) let okAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil) diff --git a/LazyMan-iOS/GameList/GameTableViewCell.swift b/LazyMan-iOS/GameList/GameTableViewCell.swift index 3d370c1..48ab6bf 100644 --- a/LazyMan-iOS/GameList/GameTableViewCell.swift +++ b/LazyMan-iOS/GameList/GameTableViewCell.swift @@ -25,6 +25,15 @@ class GameTableViewCell: UITableViewCell self.awayTeamLabel.text = self.game?.awayTeam.fullName self.homeTeamLabel.text = self.game?.homeTeam.fullName self.timeLabel.text = self.game?.getGameState() + + if game?.hasFavoriteTeam() ?? false + { + self.backgroundColor = UIColor(red: 0.0, green: 0.07, blue: 0.14, alpha: 1.0) + } + else + { + self.backgroundColor = .black + } } } } diff --git a/LazyMan-iOS/GameView/Base.lproj/Game.storyboard b/LazyMan-iOS/GameView/Base.lproj/Game.storyboard index c8269f7..21f9ffa 100644 --- a/LazyMan-iOS/GameView/Base.lproj/Game.storyboard +++ b/LazyMan-iOS/GameView/Base.lproj/Game.storyboard @@ -96,6 +96,7 @@ + @@ -218,6 +219,7 @@ + diff --git a/LazyMan-iOS/GameView/GamePresenter.swift b/LazyMan-iOS/GameView/GamePresenter.swift index 2f47976..74fb9c8 100644 --- a/LazyMan-iOS/GameView/GamePresenter.swift +++ b/LazyMan-iOS/GameView/GamePresenter.swift @@ -53,7 +53,7 @@ class GamePresenter: NSObject, GamePresenterType func viewDidLoad() { - self.cdnSelector.select(index: 0) + self.cdnSelector.select(index: SettingsManager.shared.defaultCDN == .Akamai ? 0 : 1) if self.feedSelector.count > 0 { self.feedSelector.select(index: 0) } } @@ -110,7 +110,7 @@ class GamePresenter: NSObject, GamePresenterType self.qualitySelector = GameOptionSelector(objects: feedPlaylists) self.qualitySelector?.onSelection = self.qualitySelected - if feedPlaylists.count > 0 { self.qualitySelector?.select(index: 0) } + if feedPlaylists.count > SettingsManager.shared.defaultQuality { self.qualitySelector?.select(index: SettingsManager.shared.defaultQuality) } }) { (error) in self.gameView?.showError(message: error) @@ -129,7 +129,7 @@ class GamePresenter: NSObject, GamePresenterType self.qualitySelector = GameOptionSelector(objects: feedPlaylists) self.qualitySelector?.onSelection = self.qualitySelected - if feedPlaylists.count > 1 { self.qualitySelector?.select(index: 1) } + if feedPlaylists.count > SettingsManager.shared.defaultQuality { self.qualitySelector?.select(index: SettingsManager.shared.defaultQuality) } }) { (error) in self.gameView?.showError(message: error) diff --git a/LazyMan-iOS/GameView/GameViewController.swift b/LazyMan-iOS/GameView/GameViewController.swift index f507a31..b5751dc 100644 --- a/LazyMan-iOS/GameView/GameViewController.swift +++ b/LazyMan-iOS/GameView/GameViewController.swift @@ -36,6 +36,7 @@ class GameViewController: UIViewController, GameViewControllerType, AVPlayerView self.navigation.title = gameTitle } } + private var hidden = false // MARK: - IBActions @@ -65,6 +66,12 @@ class GameViewController: UIViewController, GameViewControllerType, AVPlayerView self.presenter.viewWillAppear() } + override func viewDidAppear(_ animated: Bool) + { + super.viewDidAppear(animated) + self.hidden = false + } + // MARK: - Navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) @@ -87,7 +94,11 @@ class GameViewController: UIViewController, GameViewControllerType, AVPlayerView let playerItem = AVPlayerItem(asset: asset) let player = AVPlayer(playerItem: playerItem) self.playerVC?.player = player - player.play() + if !self.hidden + { + try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) + player.play() + } } func showError(message: String) @@ -109,5 +120,6 @@ class GameViewController: UIViewController, GameViewControllerType, AVPlayerView @objc private func pause() { self.playerVC?.player?.pause() + self.hidden = true } } diff --git a/LazyMan-iOS/Managers/SettingsManager.swift b/LazyMan-iOS/Managers/SettingsManager.swift new file mode 100644 index 0000000..a42c101 --- /dev/null +++ b/LazyMan-iOS/Managers/SettingsManager.swift @@ -0,0 +1,149 @@ +// +// SettingsManager.swift +// LazyMan-iOS +// +// Created by Nick Thompson on 4/27/18. +// Copyright © 2018 Nick Thompson. All rights reserved. +// + +import UIKit + +class SettingsManager +{ + static let shared = SettingsManager() + + // MARK: - Persisted Properties + + private let defaultLeagueKey = "defaultLeague" + var defaultLeague: League { + get { + if let value = UserDefaults.standard.string(forKey: defaultLeagueKey), + let league = League(rawValue: value) + { + return league + } + else + { + return .NHL + } + } + set { + UserDefaults.standard.set(newValue.rawValue, forKey: defaultLeagueKey) + } + } + + private let defaultQualityKey = "defaultQuality" + var defaultQuality: Int { + get { + return UserDefaults.standard.integer(forKey: defaultQualityKey) + } + set { + UserDefaults.standard.set(newValue, forKey: defaultQualityKey) + } + } + + private let defaultCDNKey = "defaultCDN" + var defaultCDN: CDN { + get { + if let value = UserDefaults.standard.string(forKey: defaultCDNKey), + let cdn = CDN(rawValue: value) + { + return cdn + } + else + { + return .Akamai + } + } + set { + UserDefaults.standard.set(newValue.rawValue, forKey: defaultCDNKey) + } + } + + private let favoriteNHLTeamKey = "favoriteNHLTeam" + var favoriteNHLTeam: Team? { + get { + if let value = UserDefaults.standard.string(forKey: favoriteNHLTeamKey), + let team = TeamManager.nhlTeams[value] + { + return team + } + else + { + return nil + } + } + set { + if let newValue = newValue, newValue.league == .NHL + { + UserDefaults.standard.set(newValue.shortName, forKey: favoriteNHLTeamKey) + } + else + { + UserDefaults.standard.removeObject(forKey: favoriteNHLTeamKey) + } + } + } + + private let favoriteMLBTeamKey = "favoriteMLBTeam" + var favoriteMLBTeam: Team? { + get { + if let value = UserDefaults.standard.string(forKey: favoriteMLBTeamKey), + let team = TeamManager.mlbTeams[value] + { + return team + } + else + { + return nil + } + } + set { + if let newValue = newValue, newValue.league == .MLB + { + UserDefaults.standard.set(newValue.shortName, forKey: favoriteMLBTeamKey) + } + else + { + UserDefaults.standard.removeObject(forKey: favoriteMLBTeamKey) + } + } + } + + private let hostWarningsKey = "hostWarnings" + var hostWarnings: Bool { + get { + return UserDefaults.standard.bool(forKey: hostWarningsKey) + } + set { + UserDefaults.standard.set(newValue, forKey: hostWarningsKey) + } + } + + private let versionUpdatesKey = "versionUpdates" + var versionUpdates: Bool { + get { + if UserDefaults.standard.object(forKey: versionUpdatesKey) != nil + { + return UserDefaults.standard.bool(forKey: versionUpdatesKey) + } + else + { + return true + } + } + set { + UserDefaults.standard.set(newValue, forKey: versionUpdatesKey) + } + } + + private let betaUpdatesKey = "betaUpdates" + var betaUpdates: Bool { + get { + return UserDefaults.standard.bool(forKey: betaUpdatesKey) + } + set { + UserDefaults.standard.set(newValue, forKey: betaUpdatesKey) + } + } +} diff --git a/LazyMan-iOS/Managers/UpdateManager.swift b/LazyMan-iOS/Managers/UpdateManager.swift new file mode 100644 index 0000000..e08389e --- /dev/null +++ b/LazyMan-iOS/Managers/UpdateManager.swift @@ -0,0 +1,63 @@ +// +// UpdateManager.swift +// LazyMan-iOS +// +// Created by Nick Thompson on 5/12/18. +// Copyright © 2018 Nick Thompson. All rights reserved. +// + +import UIKit +import SwiftyJSON + +class UpdateManager +{ + // MARK: - Static private properties + + static private let githubReleaseURL = URL(string: "https://api.github.com/repos/inickt/LazyMan-iOS/releases")! + + /** + Checks for a current version update. + */ + static func checkUpdate(completion: @escaping ((String) -> ()), userPressed: Bool = false) + { + let config = URLSessionConfiguration.default + config.requestCachePolicy = .reloadIgnoringLocalCacheData + config.urlCache = nil + let session = URLSession(configuration: config) + + session.dataTask(with: githubReleaseURL) { (data, response, error) in + guard let data = data else + { + return + } + + guard let json = try? JSON(data: data).arrayValue else { return } + + guard let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else { return } + + for release in json + { + if release["prerelease"].boolValue && !SettingsManager.shared.betaUpdates { continue } + + guard let releaseVersion = release["tag_name"].string else { continue } + + let releaseVersionNumber = String(releaseVersion.suffix(releaseVersion.count - 1)).replacingOccurrences(of: "-beta", with: "") + + if currentVersion.compare(releaseVersionNumber, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending + { + DispatchQueue.main.async { + completion("\(release["prerelease"].boolValue ? "Beta version " : "Version ")\(releaseVersionNumber) is now avalible. You have \(currentVersion).") + } + return + } + } + + if userPressed + { + DispatchQueue.main.async { + completion("You are on the latest version.") + } + } + }.resume() + } +} diff --git a/LazyMan-iOS/Models/Game.swift b/LazyMan-iOS/Models/Game.swift index 9ac459a..b74698d 100644 --- a/LazyMan-iOS/Models/Game.swift +++ b/LazyMan-iOS/Models/Game.swift @@ -95,7 +95,11 @@ class Game static func sort(game1: Game, game2: Game) -> Bool { - if (game1.gameState == .live && game2.gameState == .live) || (game1.gameState == .preview && game2.gameState == .preview) + if game1.hasFavoriteTeam() || game2.hasFavoriteTeam() + { + return game1.hasFavoriteTeam() + } + else if (game1.gameState == .live && game2.gameState == .live) || (game1.gameState == .preview && game2.gameState == .preview) { return game1.startTime <= game2.startTime } @@ -104,4 +108,15 @@ class Game return game1.gameState.rawValue <= game2.gameState.rawValue } } + + func hasFavoriteTeam() -> Bool + { + switch self.awayTeam.league + { + case .NHL: + return self.awayTeam.shortName == SettingsManager.shared.favoriteNHLTeam?.shortName || self.homeTeam.shortName == SettingsManager.shared.favoriteNHLTeam?.shortName + case .MLB: + return self.awayTeam.shortName == SettingsManager.shared.favoriteMLBTeam?.shortName || self.homeTeam.shortName == SettingsManager.shared.favoriteMLBTeam?.shortName + } + } } diff --git a/LazyMan-iOS/Resources/Base.lproj/LaunchScreen.storyboard b/LazyMan-iOS/Resources/Base.lproj/LaunchScreen.storyboard index c5dbeda..0c0c070 100644 --- a/LazyMan-iOS/Resources/Base.lproj/LaunchScreen.storyboard +++ b/LazyMan-iOS/Resources/Base.lproj/LaunchScreen.storyboard @@ -19,7 +19,7 @@ -