diff --git a/.DS_Store b/.DS_Store index 20d2851..22c1b8b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/TapeDeck.xcodeproj/project.pbxproj b/TapeDeck.xcodeproj/project.pbxproj index a6906e1..c9ccedb 100644 --- a/TapeDeck.xcodeproj/project.pbxproj +++ b/TapeDeck.xcodeproj/project.pbxproj @@ -29,6 +29,8 @@ BDE56CC325186B0C00A47239 /* hssceneExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = BDE56CB825186B0500A47239 /* hssceneExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; BDE56CCB25186C4100A47239 /* FinkHeavy.ttf in Resources */ = {isa = PBXBuildFile; fileRef = BDB53FCC250765A1004E9788 /* FinkHeavy.ttf */; }; BDE8138F2507422100371570 /* ThirdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDE8138E2507422100371570 /* ThirdViewController.swift */; }; + BDF7489D2616C0D900877F21 /* SwiftyJSON in Frameworks */ = {isa = PBXBuildFile; productRef = BDF7489C2616C0D900877F21 /* SwiftyJSON */; }; + BDF748A42616C11500877F21 /* Zip in Frameworks */ = {isa = PBXBuildFile; productRef = BDF748A32616C11500877F21 /* Zip */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -106,7 +108,9 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + BDF7489D2616C0D900877F21 /* SwiftyJSON in Frameworks */, BD7D35DB2510111700A61B6B /* SwiftyGif in Frameworks */, + BDF748A42616C11500877F21 /* Zip in Frameworks */, BD7A5E39251034D7003E992C /* StoreKit.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -256,6 +260,8 @@ name = grandfatherrock; packageProductDependencies = ( BD7D35DA2510111700A61B6B /* SwiftyGif */, + BDF7489C2616C0D900877F21 /* SwiftyJSON */, + BDF748A32616C11500877F21 /* Zip */, ); productName = grandfatherrock; productReference = BDA060F62447F3E6009C72CE /* Nook Music.app */; @@ -351,6 +357,8 @@ mainGroup = BDA060ED2447F3E5009C72CE; packageReferences = ( BD7D35D92510111700A61B6B /* XCRemoteSwiftPackageReference "SwiftyGif" */, + BDF7489B2616C0D900877F21 /* XCRemoteSwiftPackageReference "SwiftyJSON" */, + BDF748A22616C11500877F21 /* XCRemoteSwiftPackageReference "Zip" */, ); productRefGroup = BDA060F72447F3E6009C72CE /* Products */; projectDirPath = ""; @@ -605,7 +613,7 @@ CODE_SIGN_ENTITLEMENTS = grandfatherrock/grandfatherrock.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 2; DEVELOPMENT_TEAM = PRBH2T6668; INFOPLIST_FILE = grandfatherrock/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -613,7 +621,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.2.1; PRODUCT_BUNDLE_IDENTIFIER = dev.mpg13.grandfatherrock; PRODUCT_NAME = "Nook Music"; SUPPORTS_MACCATALYST = NO; @@ -631,7 +639,7 @@ CODE_SIGN_ENTITLEMENTS = grandfatherrock/grandfatherrock.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 2; DEVELOPMENT_TEAM = PRBH2T6668; INFOPLIST_FILE = grandfatherrock/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -639,7 +647,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.2.1; PRODUCT_BUNDLE_IDENTIFIER = dev.mpg13.grandfatherrock; PRODUCT_NAME = "Nook Music"; SUPPORTS_MACCATALYST = NO; @@ -741,7 +749,7 @@ CODE_SIGN_ENTITLEMENTS = hssceneExtension.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 2; DEVELOPMENT_TEAM = PRBH2T6668; INFOPLIST_FILE = hsscene/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 14.0; @@ -750,7 +758,7 @@ "@executable_path/Frameworks", "@executable_path/../../Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.2.1; PRODUCT_BUNDLE_IDENTIFIER = dev.mpg13.grandfatherrock.hsscene; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -769,7 +777,7 @@ CODE_SIGN_ENTITLEMENTS = hssceneExtension.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 2; DEVELOPMENT_TEAM = PRBH2T6668; INFOPLIST_FILE = hsscene/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 14.0; @@ -778,7 +786,7 @@ "@executable_path/Frameworks", "@executable_path/../../Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.2.1; PRODUCT_BUNDLE_IDENTIFIER = dev.mpg13.grandfatherrock.hsscene; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -848,6 +856,22 @@ minimumVersion = 5.3.0; }; }; + BDF7489B2616C0D900877F21 /* XCRemoteSwiftPackageReference "SwiftyJSON" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/SwiftyJSON/SwiftyJSON.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 5.0.1; + }; + }; + BDF748A22616C11500877F21 /* XCRemoteSwiftPackageReference "Zip" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/marmelroy/Zip.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 2.1.1; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -856,6 +880,16 @@ package = BD7D35D92510111700A61B6B /* XCRemoteSwiftPackageReference "SwiftyGif" */; productName = SwiftyGif; }; + BDF7489C2616C0D900877F21 /* SwiftyJSON */ = { + isa = XCSwiftPackageProductDependency; + package = BDF7489B2616C0D900877F21 /* XCRemoteSwiftPackageReference "SwiftyJSON" */; + productName = SwiftyJSON; + }; + BDF748A32616C11500877F21 /* Zip */ = { + isa = XCSwiftPackageProductDependency; + package = BDF748A22616C11500877F21 /* XCRemoteSwiftPackageReference "Zip" */; + productName = Zip; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = BDA060EE2447F3E5009C72CE /* Project object */; diff --git a/TapeDeck.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/TapeDeck.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 858addb..1dfd496 100644 --- a/TapeDeck.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/TapeDeck.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -9,6 +9,24 @@ "revision": "0cd770feed5807d9fd4fa8df5ad74b56b814b0c8", "version": "5.4.0" } + }, + { + "package": "SwiftyJSON", + "repositoryURL": "https://github.com/SwiftyJSON/SwiftyJSON.git", + "state": { + "branch": null, + "revision": "b3dcd7dbd0d488e1a7077cb33b00f2083e382f07", + "version": "5.0.1" + } + }, + { + "package": "Zip", + "repositoryURL": "https://github.com/marmelroy/Zip.git", + "state": { + "branch": null, + "revision": "bd19d974e8a38cc8d3a88c90c8a107386c3b8ccf", + "version": "2.1.1" + } } ] }, diff --git a/TapeDeck.xcodeproj/project.xcworkspace/xcuserdata/micahgomez.xcuserdatad/UserInterfaceState.xcuserstate b/TapeDeck.xcodeproj/project.xcworkspace/xcuserdata/micahgomez.xcuserdatad/UserInterfaceState.xcuserstate index 56d461c..ad41f6f 100644 Binary files a/TapeDeck.xcodeproj/project.xcworkspace/xcuserdata/micahgomez.xcuserdatad/UserInterfaceState.xcuserstate and b/TapeDeck.xcodeproj/project.xcworkspace/xcuserdata/micahgomez.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TapeDeck.xcodeproj/xcuserdata/micahgomez.xcuserdatad/xcschemes/xcschememanagement.plist b/TapeDeck.xcodeproj/xcuserdata/micahgomez.xcuserdatad/xcschemes/xcschememanagement.plist index 2c66688..2ad49ff 100644 --- a/TapeDeck.xcodeproj/xcuserdata/micahgomez.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/TapeDeck.xcodeproj/xcuserdata/micahgomez.xcuserdatad/xcschemes/xcschememanagement.plist @@ -4,15 +4,36 @@ SchemeUserState + Playground (Playground) 1.xcscheme + + isShown + + orderHint + 3 + + Playground (Playground) 2.xcscheme + + isShown + + orderHint + 4 + + Playground (Playground).xcscheme + + isShown + + orderHint + 2 + grandfatherrock.xcscheme_^#shared#^_ orderHint - 1 + 0 hssceneExtension.xcscheme_^#shared#^_ orderHint - 0 + 1 hswidgetExtension.xcscheme_^#shared#^_ diff --git a/grandfatherrock/.DS_Store b/grandfatherrock/.DS_Store index aa6d45c..692d802 100644 Binary files a/grandfatherrock/.DS_Store and b/grandfatherrock/.DS_Store differ diff --git a/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/Contents.json b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/Contents.json new file mode 100644 index 0000000..7d2bff1 --- /dev/null +++ b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/Contents.json @@ -0,0 +1,23 @@ +{ + "images" : [ + { + "filename" : "coollogo_com-13092940.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "coollogo_com-13092941.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "coollogo_com-13092942.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092940.png b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092940.png new file mode 100644 index 0000000..fbf874b Binary files /dev/null and b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092940.png differ diff --git a/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092941.png b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092941.png new file mode 100644 index 0000000..fbf874b Binary files /dev/null and b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092941.png differ diff --git a/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092942.png b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092942.png new file mode 100644 index 0000000..fbf874b Binary files /dev/null and b/grandfatherrock/Assets.xcassets/Micah Gomez.imageset/coollogo_com-13092942.png differ diff --git a/grandfatherrock/Base.lproj/LaunchScreen.storyboard b/grandfatherrock/Base.lproj/LaunchScreen.storyboard index e2ca5f6..1977b90 100644 --- a/grandfatherrock/Base.lproj/LaunchScreen.storyboard +++ b/grandfatherrock/Base.lproj/LaunchScreen.storyboard @@ -1,9 +1,10 @@ - - + + - + + @@ -14,32 +15,43 @@ - + - - + + - - + + + - - + + + + + + + - + - + diff --git a/grandfatherrock/Base.lproj/Main.storyboard b/grandfatherrock/Base.lproj/Main.storyboard index 7f4a26d..cd35214 100644 --- a/grandfatherrock/Base.lproj/Main.storyboard +++ b/grandfatherrock/Base.lproj/Main.storyboard @@ -1,9 +1,10 @@ - + - + + @@ -117,7 +118,7 @@ - + @@ -220,6 +221,7 @@ + @@ -230,12 +232,13 @@ - + + @@ -405,38 +408,141 @@ - - + + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + - + - + + - + - + - diff --git a/grandfatherrock/FirstViewController.swift b/grandfatherrock/FirstViewController.swift index e7bb93d..862c8b9 100644 --- a/grandfatherrock/FirstViewController.swift +++ b/grandfatherrock/FirstViewController.swift @@ -49,190 +49,134 @@ extension UserDefaults: ObjectSavable { } } +var audioPlayer = AVAudioPlayer() +var musicPlayer = MPMusicPlayerController.applicationMusicPlayer + public class musicHandler{ @objc class func updateMusic(){ + print("updating music") + print(GlobalVars.musicStarted) + timer.invalidate() + //audioPlayer.stop() + //musicPlayer.stop() + //print("updating song") GlobalVars.hour = Calendar.current.component(.hour, from: Date()) //song = GlobalVars.titleCode + hourPadding + String(GlobalVars.hour) - ////print(song) - let musicPlayer = MPMusicPlayerController.applicationMusicPlayer + //print(song) + let documentsUrl:URL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!) as URL + var mediaPredicate = MPMediaPredicate() do{ - var mediaPredicate = MPMediaPredicate() - switch GlobalVars.selectedMusicList { - case 0: - if(GlobalVars.musicSelectionID0[GlobalVars.hour] != 0){ - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID0[GlobalVars.hour], forProperty: "persistentID") - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) - } - //print("Song Found!") - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID0[searchHour], forProperty: "persistentID") + print(GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.hour]) + print(GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.hour]) + + if(GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.hour] != 0){ + mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.hour], forProperty: "persistentID") + GlobalVars.currentMusicFormat = true + print("ID is not zero") + } else if(GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.hour] != ""){ + do { + let destinationFileUrl = documentsUrl.appendingPathComponent(GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.hour]) + audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath:destinationFileUrl.path)) + GlobalVars.currentMusicFormat = false + print("URL is not blank") + } catch { + print(error.localizedDescription) } - case 1: - if(GlobalVars.musicSelectionID1[GlobalVars.hour] != 0){ - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID1[GlobalVars.hour], forProperty: "persistentID") - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID1[searchHour] - - //print(searchID) + } else { + print("ID is zero, URL is blank, scanning for previous songs") + var searchID:UInt64 = 0 + var searchURL:String = "" + GlobalVars.searchHour = GlobalVars.hour + while (searchID == 0 && searchURL == "") { + if(GlobalVars.searchHour > 0){ + GlobalVars.searchHour -= 1 + } else { + GlobalVars.searchHour = 23 } - //print("Song Found!") - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID1[searchHour], forProperty: "persistentID") - } - case 2: - if(GlobalVars.musicSelectionID2[GlobalVars.hour] != 0){ - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID2[GlobalVars.hour], forProperty: "persistentID") - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID2[searchHour] - - //print(searchID) + if(GlobalVars.searchHour == GlobalVars.hour){ + //print("no songs found!") + break } - //print("Song Found!") - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID2[searchHour], forProperty: "persistentID") - } - case 3: - if(GlobalVars.musicSelectionID3[GlobalVars.hour] != 0){ - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID3[GlobalVars.hour], forProperty: "persistentID") - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.searchHour] + searchURL = GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.searchHour] + + print("searchID \(searchID)") + print("searchURL \(searchURL)") + + if (searchID != 0){ + mediaPredicate = MPMediaPropertyPredicate(value: searchID, forProperty: "persistentID") + GlobalVars.currentMusicFormat = true + print("ID is not zero") + } else if (searchURL != ""){ + do { + let destinationFileUrl = documentsUrl.appendingPathComponent(searchURL) + audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath:destinationFileUrl.path)) + GlobalVars.currentMusicFormat = false + print("URL is not blank") + } catch { + print(error.localizedDescription) } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID3[searchHour] - - //print(searchID) } - //print("Song Found!") - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID3[searchHour], forProperty: "persistentID") - } - case 4: - if(GlobalVars.musicSelectionID4[GlobalVars.hour] != 0){ - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID4[GlobalVars.hour], forProperty: "persistentID") - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID4[searchHour] - - //print(searchID) - } - //print("Song Found!") - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID4[searchHour], forProperty: "persistentID") - } - default: - if(GlobalVars.musicSelectionID0[GlobalVars.hour] != 0){ - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID0[GlobalVars.hour], forProperty: "persistentID") - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) - } - //print("Song Found!") - mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID0[searchHour], forProperty: "persistentID") } + print("Format: \(GlobalVars.currentMusicFormat)") } - //let mediaPredicate = MPMediaPropertyPredicate(value: GlobalVars.musicSelectionID1[GlobalVars.hour], forProperty: "persistentID") - - let mediaQuery = MPMediaQuery() - - mediaQuery.addFilterPredicate(mediaPredicate) - - - //let mediaItemCollection: MPMediaItemCollection = MPMediaItemCollection() - - musicPlayer.setQueue(with: mediaQuery) - musicPlayer.repeatMode = .one //print(mediaQuery) //musicPlayer.play() + let mediaQuery = MPMediaQuery() + + mediaQuery.addFilterPredicate(mediaPredicate) + + + //let mediaItemCollection: MPMediaItemCollection = MPMediaItemCollection() + + musicPlayer.setQueue(with: mediaQuery) + musicPlayer.repeatMode = .one } + if(GlobalVars.currentMusicFormat) { + + if(GlobalVars.musicStarted){ + + musicPlayer.play() + } + } else { + audioPlayer.prepareToPlay() + audioPlayer.numberOfLoops = -1 + + let audioSession = AVAudioSession.sharedInstance() + do{ + //try audioSession.setCategory(.playback, mode: .default) + try audioSession.setActive(true) + try audioSession.setCategory(.playback, mode: .default, options: + .init(rawValue: 0)) + } catch { + print(error.localizedDescription) + } + if(GlobalVars.musicStarted){ + audioPlayer.play() + let mpic = MPNowPlayingInfoCenter.default() + mpic.nowPlayingInfo = [ + MPMediaItemPropertyTitle:"This Is a Test", + MPMediaItemPropertyArtist:"Matt Neuburg" + ] + } + } - - if(MPMusicPlayerController.applicationMusicPlayer.playbackState == MPMusicPlaybackState.playing){ + /*if(musicPlayer.playbackState == MPMusicPlaybackState.playing){ musicPlayer.play() - } + }*/ + print("making timer") if(Calendar.current.component(.minute, from: Date()) == 0){ date = Date().addingTimeInterval(3600) } else{ @@ -247,24 +191,24 @@ public class musicHandler{ timer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(updateMusic), userInfo: nil, repeats: false) RunLoop.main.add(timer, forMode: RunLoop.Mode.common) + print("done updating music") } } struct GlobalVars { - static var musicSelectionID0:[UInt64] = [UInt64]() - static var musicSelection0:[String] = [String]() - static var musicSelectionID1:[UInt64] = [UInt64]() - static var musicSelection1:[String] = [String]() - static var musicSelectionID2:[UInt64] = [UInt64]() - static var musicSelection2:[String] = [String]() - static var musicSelectionID3:[UInt64] = [UInt64]() - static var musicSelection3:[String] = [String]() - static var musicSelectionID4:[UInt64] = [UInt64]() - static var musicSelection4:[String] = [String]() - static var selectedMusicList:Int = 0 + static var musicSelectionID:[[UInt64]] = [[UInt64]](repeating: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], count: 5) + static var musicSelection:[[String]] = [[String]](repeating: ["","","","","","","","","","","","","","","","","","","","","","","",""], count: 5) + static var musicFileURL:[[String]] = [[String]](repeating: ["","","","","","","","","","","","","","","","","","","","","","","",""], count: 5) + static var musicFormatType:[[Bool]] = [[Bool]](repeating: [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false], count: 5) + + static var selectedCell:Int? = nil + + static var selectedMusicList:Int = UserDefaults.standard.integer(forKey: "savedSelectedMusicList") static var musicStarted = false + static var currentMusicFormat:Bool = false static var titleName = UserDefaults.standard.string(forKey: "selectedTitle") static var hour = Calendar.current.component(.hour, from: Date()) + static var searchHour:Int = 0 } var i = 0 @@ -280,7 +224,6 @@ var prevVol = 0.0 class FirstViewController: UIViewController{ let defaults = UserDefaults.standard - let musicPlayer = MPMusicPlayerController.applicationMusicPlayer //@IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var timeLabel: UILabel! @@ -292,153 +235,66 @@ class FirstViewController: UIViewController{ //dateLabel.text = DateFormatter.localizedString(from: Date(), dateStyle: .long, timeStyle: .none) timeLabel.text = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short) var nowPlayingString:String = "Now Playing: " - if (MPMusicPlayerController.applicationMusicPlayer.playbackState == MPMusicPlaybackState.playing) { - switch GlobalVars.selectedMusicList { - case 0: - if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) + if (musicPlayer.playbackState == MPMusicPlaybackState.playing) { + print("musicPlayer is playing") + if GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + GlobalVars.searchHour = GlobalVars.hour + while (searchID == 0) { + if(GlobalVars.searchHour > 0){ + GlobalVars.searchHour -= 1 + } else { + GlobalVars.searchHour = 23 } - nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) - } - case 1: - if GlobalVars.musicSelectionID1[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection1[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID1[searchHour] - - //print(searchID) + if(GlobalVars.searchHour == GlobalVars.hour){ + //print("no songs found!") + break } - nowPlayingString.append(GlobalVars.musicSelection1[searchHour]) - } - case 2: - if GlobalVars.musicSelectionID2[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection2[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID2[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection2[searchHour]) - } - case 3: - if GlobalVars.musicSelectionID3[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection3[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID3[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection3[searchHour]) - } - case 4: - if GlobalVars.musicSelectionID4[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection4[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID4[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection4[searchHour]) - } - default: - if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.searchHour] + + //print(searchID) } + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.searchHour]) } + songLabel.text = nowPlayingString - } else { + } + else if (audioPlayer.isPlaying) { + print("audioplayer is playing") + if GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.hour] != "" { + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.hour]) + } else { + var searchURL:String = "" + GlobalVars.searchHour = GlobalVars.hour + while (searchURL == "") { + if(GlobalVars.searchHour > 0){ + GlobalVars.searchHour -= 1 + } else { + GlobalVars.searchHour = 23 + } + if(GlobalVars.searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchURL = GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.searchHour]) + print(nowPlayingString) + songLabel.text = nowPlayingString + } + } + else { + print("neither is playing") songLabel.text = "" } - if (MPMusicPlayerController.applicationMusicPlayer.playbackState == MPMusicPlaybackState.playing) { + if (GlobalVars.musicStarted) { GlobalVars.musicStarted = true controlButton.accessibilityLabel = "Pause Button" controlButton.setBackgroundImage(UIImage(systemName: "pause.circle"), for: UIControl.State.normal) @@ -454,144 +310,72 @@ class FirstViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. + print("list \(GlobalVars.selectedMusicList)") + + UIApplication.shared.beginReceivingRemoteControlEvents() + let commandCenter = MPRemoteCommandCenter.shared() + commandCenter.pauseCommand.isEnabled = true + commandCenter.playCommand.isEnabled = true + commandCenter.nextTrackCommand.isEnabled = false + commandCenter.previousTrackCommand.isEnabled = false + + commandCenter.playCommand.addTarget { [unowned self] event in + if !audioPlayer.isPlaying { + //musicHandler.updateMusic() + audioPlayer.play() + GlobalVars.musicStarted = true + print("music play") + return .success + } + return .commandFailed + } + + // Add handler for Pause Command + commandCenter.pauseCommand.addTarget { [unowned self] event in + if audioPlayer.isPlaying { + audioPlayer.pause() + GlobalVars.musicStarted = false + print("music pause") + return .success + } + return .commandFailed + } musicPlayer.stop() + audioPlayer.stop() GlobalVars.musicStarted = false if(!defaults.bool(forKey: "didRun")){ - defaults.set("New Horizons", forKey: "selectedTitle") defaults.set(0, forKey: "titleNo") //defaults.set(true, forKey: "didRun") - GlobalVars.musicSelectionID0 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - GlobalVars.musicSelection0 = ["","","","","","","","","","","","","","","","","","","","","","","",""] - GlobalVars.musicSelectionID1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - GlobalVars.musicSelection1 = ["","","","","","","","","","","","","","","","","","","","","","","",""] - GlobalVars.musicSelectionID2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - GlobalVars.musicSelection2 = ["","","","","","","","","","","","","","","","","","","","","","","",""] - GlobalVars.musicSelectionID3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - GlobalVars.musicSelection3 = ["","","","","","","","","","","","","","","","","","","","","","","",""] - GlobalVars.musicSelectionID4 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - GlobalVars.musicSelection4 = ["","","","","","","","","","","","","","","","","","","","","","","",""] - do { - try defaults.setObject(GlobalVars.musicSelection0, forKey: "savedMusicSelection0") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID0, forKey: "savedMusicSelectionID0") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection1, forKey: "savedMusicSelection1") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID1, forKey: "savedMusicSelectionID1") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection2, forKey: "savedMusicSelection2") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID2, forKey: "savedMusicSelectionID2") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection3, forKey: "savedMusicSelection3") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID3, forKey: "savedMusicSelectionID3") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection4, forKey: "savedMusicSelection4") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID4, forKey: "savedMusicSelectionID4") - } catch { - //print(error.localizedDescription) + for n in 0...4 { + print(n) + GlobalVars.musicSelectionID[n] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + GlobalVars.musicSelection[n] = ["","","","","","","","","","","","","","","","","","","","","","","",""] + GlobalVars.musicFormatType[n] = [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false] + GlobalVars.musicFileURL[n] = ["","","","","","","","","","","","","","","","","","","","","","","",""] } + do {try defaults.setObject(GlobalVars.musicSelection, forKey: "savedMusicSelection")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicSelectionID, forKey: "savedMusicSelectionID")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicFileURL, forKey: "savedMusicFileURL")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicFormatType, forKey: "savedMusicFileURL")} catch {/*print(error.localizedDescription)*/} //print("First App run") - } else { - do { - GlobalVars.musicSelection0 = try defaults.getObject(forKey: "savedMusicSelection0", castTo: [String].self) - //print(GlobalVars.musicSelection0) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelectionID0 = try defaults.getObject(forKey: "savedMusicSelectionID0", castTo: [UInt64].self) - //print(GlobalVars.musicSelectionID0) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelection1 = try defaults.getObject(forKey: "savedMusicSelection1", castTo: [String].self) - //print(GlobalVars.musicSelection1) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelectionID1 = try defaults.getObject(forKey: "savedMusicSelectionID1", castTo: [UInt64].self) - //print(GlobalVars.musicSelectionID1) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelection2 = try defaults.getObject(forKey: "savedMusicSelection2", castTo: [String].self) - //print(GlobalVars.musicSelection2) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelectionID2 = try defaults.getObject(forKey: "savedMusicSelectionID2", castTo: [UInt64].self) - //print(GlobalVars.musicSelectionID2) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelection3 = try defaults.getObject(forKey: "savedMusicSelection3", castTo: [String].self) - //print(GlobalVars.musicSelection3) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelectionID3 = try defaults.getObject(forKey: "savedMusicSelectionID3", castTo: [UInt64].self) - //print(GlobalVars.musicSelectionID3) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelection4 = try defaults.getObject(forKey: "savedMusicSelection4", castTo: [String].self) - //print(GlobalVars.musicSelection4) - } catch { - //print(error.localizedDescription) - } - do { - GlobalVars.musicSelectionID4 = try defaults.getObject(forKey: "savedMusicSelectionID4", castTo: [UInt64].self) - //print(GlobalVars.musicSelectionID4) - } catch { - //print(error.localizedDescription) - } + } else { - do { - GlobalVars.selectedMusicList = try defaults.getObject(forKey: "savedSelectedMusicList", castTo: Int.self) - //print(GlobalVars.selectedMusicList) - } catch { - //print(error.localizedDescription) - } + if(!defaults.bool(forKey: "didUpdate")){ + for n in 0...4 { + print(n) + GlobalVars.musicFormatType[n] = [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false] + GlobalVars.musicFileURL[n] = ["","","","","","","","","","","","","","","","","","","","","","","",""] + } + defaults.set(true, forKey: "didUpdate") + print("appjustupdated") + } + do {GlobalVars.musicSelection = try defaults.getObject(forKey: "savedMusicSelection", castTo: [[String]].self)} catch {/*print(error.localizedDescription)*/} + do {GlobalVars.musicSelectionID = try defaults.getObject(forKey: "savedMusicSelectionID", castTo: [[UInt64]].self)} catch {/*print(error.localizedDescription)*/} + do {GlobalVars.musicFileURL = try defaults.getObject(forKey: "savedMusicFileURL", castTo: [[String]].self)} catch {/*print(error.localizedDescription)*/} + do {GlobalVars.musicFormatType = try defaults.getObject(forKey: "savedMusicFileType", castTo: [[Bool]].self)} catch {/*print(error.localizedDescription)*/} //print("App Ran Before") } @@ -662,7 +446,6 @@ class FirstViewController: UIViewController{ @IBOutlet var gradientView: UIView! override func viewDidAppear(_ animated: Bool) { - //print("fuck!") let prevHour = GlobalVars.hour GlobalVars.hour = Calendar.current.component(.hour, from: Date()) @@ -703,7 +486,7 @@ class FirstViewController: UIViewController{ } - if (MPMusicPlayerController.applicationMusicPlayer.playbackState == MPMusicPlaybackState.playing) { + if (GlobalVars.musicStarted) { GlobalVars.musicStarted = true controlButton.accessibilityLabel = "Pause Button" controlButton.setBackgroundImage(UIImage(systemName: "pause.circle"), for: UIControl.State.normal) @@ -716,7 +499,21 @@ class FirstViewController: UIViewController{ } var foundSong = false - switch GlobalVars.selectedMusicList { + + + print("woah! \(GlobalVars.selectedMusicList)") + for i in GlobalVars.musicSelectionID[GlobalVars.selectedMusicList] { + if (i != 0){ + foundSong = true + } + } + for i in GlobalVars.musicFileURL[GlobalVars.selectedMusicList] { + if (i != ""){ + foundSong = true + } + } + + /*switch GlobalVars.selectedMusicList { case 0: for i in GlobalVars.musicSelectionID0 { if (i != 0){ @@ -753,7 +550,7 @@ class FirstViewController: UIViewController{ foundSong = true } } - } + }*/ if !foundSong && defaults.bool(forKey: "didRun") { let errorAlert = UIAlertController(title: "Song Error", message: "No songs found in this track. Please visit the 'Select Music' tab to set a song and get started!", preferredStyle: .alert) @@ -793,146 +590,29 @@ class FirstViewController: UIViewController{ self.view.layoutIfNeeded() var nowPlayingString:String = "Now Playing: " - if (MPMusicPlayerController.applicationMusicPlayer.playbackState == MPMusicPlaybackState.playing) { - switch GlobalVars.selectedMusicList { - case 0: - if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) + if (musicPlayer.playbackState == MPMusicPlaybackState.playing) { + + if GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 } - nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) - } - case 1: - if GlobalVars.musicSelectionID1[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection1[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID1[searchHour] - - //print(searchID) + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break } - nowPlayingString.append(GlobalVars.musicSelection1[searchHour]) - } - case 2: - if GlobalVars.musicSelectionID2[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection2[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID2[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection2[searchHour]) - } - case 3: - if GlobalVars.musicSelectionID3[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection3[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID3[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection3[searchHour]) - } - case 4: - if GlobalVars.musicSelectionID4[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection4[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID4[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection4[searchHour]) - } - default: - if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][searchHour] + + //print(searchID) } + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][searchHour]) } songLabel.text = nowPlayingString } else { @@ -942,6 +622,7 @@ class FirstViewController: UIViewController{ override func viewWillAppear(_ animated: Bool) { + print(AVAudioPlayer().isPlaying) // create the gradient layer let gradient = CAGradientLayer() gradient.frame = self.view.bounds @@ -1013,56 +694,10 @@ class FirstViewController: UIViewController{ override func viewWillDisappear(_ animated: Bool) { gradientView.layer.sublayers?.forEach { $0.removeFromSuperlayer() } - do { - try defaults.setObject(GlobalVars.musicSelection0, forKey: "savedMusicSelection0") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID0, forKey: "savedMusicSelectionID0") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection1, forKey: "savedMusicSelection1") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID1, forKey: "savedMusicSelectionID1") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection2, forKey: "savedMusicSelection2") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID2, forKey: "savedMusicSelectionID2") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection3, forKey: "savedMusicSelection3") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID3, forKey: "savedMusicSelectionID3") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection4, forKey: "savedMusicSelection4") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID4, forKey: "savedMusicSelectionID4") - } catch { - //print(error.localizedDescription) - } + do {try defaults.setObject(GlobalVars.musicSelection, forKey: "savedMusicSelection")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicSelectionID, forKey: "savedMusicSelectionID")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicFileURL, forKey: "savedMusicFileURL")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicFormatType, forKey: "savedMusicFileType")} catch {/*print(error.localizedDescription)*/} } @IBOutlet weak var controlButton: UIButton! @@ -1071,171 +706,226 @@ class FirstViewController: UIViewController{ @IBAction func play(_ sender: Any) { + + musicHandler.updateMusic() if(!GlobalVars.musicStarted){ GlobalVars.musicStarted = true - musicHandler.updateMusic() - let musicPlayer = MPMusicPlayerController.applicationMusicPlayer - musicPlayer.play() - GlobalVars.musicStarted = true + if GlobalVars.currentMusicFormat { + musicPlayer.play() + } else { + audioPlayer.play() + } + controlButton.accessibilityLabel = "Pause Button" controlButton.setBackgroundImage(UIImage(systemName: "pause.circle"), for: UIControl.State.normal) UIView.animate(withDuration: 0.4,delay:0,usingSpringWithDamping: 0.5,initialSpringVelocity: 0,animations: {self.PlayButtonSize.constant = 130;self.view.layoutIfNeeded()}, completion: nil) - } - else{ - let musicPlayer = MPMusicPlayerController.applicationMusicPlayer - musicPlayer.pause() + print("Music started: \(GlobalVars.musicStarted)") + } else{ + GlobalVars.musicStarted = false + musicPlayer.pause() + audioPlayer.pause() + controlButton.accessibilityLabel = "Play Button" controlButton.setBackgroundImage(UIImage(systemName: "play.circle"), for: UIControl.State.normal) UIView.animate(withDuration: 0.4, delay: 0,usingSpringWithDamping: 0.5,initialSpringVelocity: 0,animations: {self.PlayButtonSize.constant = 90;self.view.layoutIfNeeded()}, completion: nil) - + print("Music started: \(GlobalVars.musicStarted)") } var nowPlayingString:String = "Now Playing: " - if (MPMusicPlayerController.applicationMusicPlayer.playbackState == MPMusicPlaybackState.playing) { - switch GlobalVars.selectedMusicList { - case 0: - if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) - } - case 1: - if GlobalVars.musicSelectionID1[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection1[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID1[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection1[searchHour]) - } - case 2: - if GlobalVars.musicSelectionID2[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection2[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID2[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection2[searchHour]) - } - case 3: - if GlobalVars.musicSelectionID3[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection3[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID3[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection3[searchHour]) - } - case 4: - if GlobalVars.musicSelectionID4[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection4[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID4[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection4[searchHour]) - } - default: - if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { - nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) - } else { - var searchID:UInt64 = 0 - var searchHour:Int = GlobalVars.hour - while (searchID == 0) { - if(searchHour > 0){ - searchHour -= 1 - } else { - searchHour = 23 - } - if(searchHour == GlobalVars.hour){ - //print("no songs found!") - break - } - //print("searching ", searchHour) - searchID = GlobalVars.musicSelectionID0[searchHour] - - //print(searchID) - } - nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) - } - } - songLabel.text = nowPlayingString + if (musicPlayer.playbackState == MPMusicPlaybackState.playing) { + + if GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.hour]) } else { + var searchID:UInt64 = 0 + GlobalVars.searchHour = GlobalVars.hour + while (searchID == 0) { + if(GlobalVars.searchHour > 0){ + GlobalVars.searchHour -= 1 + } else { + GlobalVars.searchHour = 23 + } + if(GlobalVars.searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.searchHour]) + } + + /*switch GlobalVars.selectedMusicList { + case 0: + if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 + } + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID0[searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) + } + case 1: + if GlobalVars.musicSelectionID1[GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection1[GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 + } + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID1[searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection1[searchHour]) + } + case 2: + if GlobalVars.musicSelectionID2[GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection2[GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 + } + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID2[searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection2[searchHour]) + } + case 3: + if GlobalVars.musicSelectionID3[GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection3[GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 + } + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID3[searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection3[searchHour]) + } + case 4: + if GlobalVars.musicSelectionID4[GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection4[GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 + } + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID4[searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection4[searchHour]) + } + default: + if GlobalVars.musicSelectionID0[GlobalVars.hour] != 0 { + nowPlayingString.append(GlobalVars.musicSelection0[GlobalVars.hour]) + } else { + var searchID:UInt64 = 0 + var searchHour:Int = GlobalVars.hour + while (searchID == 0) { + if(searchHour > 0){ + searchHour -= 1 + } else { + searchHour = 23 + } + if(searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchID = GlobalVars.musicSelectionID0[searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection0[searchHour]) + } + }*/ + songLabel.text = nowPlayingString + } + else if (audioPlayer.isPlaying) { + if GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.hour] != "" { + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.hour]) + } else { + var searchURL:String = "" + GlobalVars.searchHour = GlobalVars.hour + while (searchURL == "") { + if(GlobalVars.searchHour > 0){ + GlobalVars.searchHour -= 1 + } else { + GlobalVars.searchHour = 23 + } + if(GlobalVars.searchHour == GlobalVars.hour){ + //print("no songs found!") + break + } + //print("searching ", searchHour) + searchURL = GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.searchHour] + + //print(searchID) + } + nowPlayingString.append(GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.searchHour]) + } + } + else { songLabel.text = "" } } diff --git a/grandfatherrock/FourthViewController.swift b/grandfatherrock/FourthViewController.swift index 7d43f8b..d027f13 100644 --- a/grandfatherrock/FourthViewController.swift +++ b/grandfatherrock/FourthViewController.swift @@ -7,39 +7,194 @@ // import UIKit -import AudioToolbox +import Zip +import SwiftyJSON +import MediaPlayer +class fileDataCell : UITableViewCell { + + @IBOutlet weak var filetypeLabel: UILabel! + @IBOutlet weak var filenameLabel : UILabel! + +} -class FourthViewController: UIViewController, UIPickerViewDelegate{ +extension FileManager { + func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL]? { + let documentsURL = urls(for: directory, in: .userDomainMask)[0] + let fileURLs = try? contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: skipsHiddenFiles ? .skipsHiddenFiles : [] ) + + return fileURLs + } +} + +class FourthViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MPMediaPickerControllerDelegate { + var refreshControl = UIRefreshControl() + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + var resourcesContent = FileManager.default.urls(for: .documentDirectory) ?? [] + resourcesContent.sort { + $0.lastPathComponent < $1.lastPathComponent + } + + print(FileManager.default.urls(for: .documentDirectory) ?? "none") + + return resourcesContent.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "filecell", for: indexPath) as! fileDataCell + + var resourcesContent = FileManager.default.urls(for: .documentDirectory) ?? [] + resourcesContent.sort { + $0.lastPathComponent < $1.lastPathComponent + } + + cell.filenameLabel?.text = resourcesContent[indexPath.row].lastPathComponent + cell.filetypeLabel?.text = resourcesContent[indexPath.row].pathExtension + cell.selectionStyle = .none + return cell + } + + func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool + { + return true + } + + func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){ + var resourcesContent = FileManager.default.urls(for: .documentDirectory) ?? [] + resourcesContent.sort { + $0.lastPathComponent < $1.lastPathComponent + } + + let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true) + let filePathToDelete = dirs[0].appendingFormat("/" + resourcesContent[indexPath.row].lastPathComponent) + let fileManager = FileManager.default + + if editingStyle == .delete + { + do { + + //Check if file exists + if fileManager.fileExists(atPath: filePathToDelete) { + //Delete file + try fileManager.removeItem(atPath: filePathToDelete) + } else { + print("File does not exist: \(resourcesContent[indexPath.row].absoluteString)") + } + + } + catch let error as NSError { + print("An error took place: \(error)") + } + filesTable.reloadData() + } + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ + var resourcesContent = FileManager.default.urls(for: .documentDirectory) ?? [] + resourcesContent.sort { + $0.lastPathComponent < $1.lastPathComponent + } + print(resourcesContent[indexPath.row].lastPathComponent) + print(resourcesContent[indexPath.row]) + GlobalVars.musicSelection[GlobalVars.selectedMusicList][GlobalVars.selectedCell!] = resourcesContent[indexPath.row].lastPathComponent + GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][GlobalVars.selectedCell!] = 0 + GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.selectedCell!] = resourcesContent[indexPath.row].lastPathComponent + //print("PATH FROM IS \(resourcesContent[indexPath.row].absoluteString)") + print("PATH SAVED IS \(GlobalVars.musicFileURL[GlobalVars.selectedMusicList][GlobalVars.selectedCell!])") + GlobalVars.musicFormatType[GlobalVars.selectedMusicList][GlobalVars.selectedCell!] = false + GlobalVars.selectedCell = nil + self.dismiss(animated: true, completion: nil) + } + + let defaults = UserDefaults.standard + + @IBOutlet var filesTable: UITableView! @IBOutlet var gradientView: UIView! - let defaults = UserDefaults.standard + @IBOutlet var selectListControl: UISegmentedControl! + + @IBAction func selectListControlChanged(_ sender: Any) { + + MPMusicPlayerController.applicationMusicPlayer.stop() + + GlobalVars.selectedMusicList = selectListControl.selectedSegmentIndex + //print(GlobalVars.selectedMusicList) + filesTable.reloadData() + /*var noSongs = false + switch GlobalVars.selectedMusicList { + case 0: + for i in GlobalVars.musicSelectionID0 { + if (i == 0){ + noSongs = true + } + } + case 1: + for i in GlobalVars.musicSelectionID1 { + if (i == 0){ + noSongs = true + } + } + case 2: + for i in GlobalVars.musicSelectionID2 { + if (i == 0){ + noSongs = true + } + } + case 3: + for i in GlobalVars.musicSelectionID3 { + if (i == 0){ + noSongs = true + } + } + case 4: + for i in GlobalVars.musicSelectionID4 { + if (i == 0){ + noSongs = true + } + } + default: + for i in GlobalVars.musicSelectionID0 { + if (i == 0){ + noSongs = true + } + } + } + + if noSongs { + MPMusicPlayerController.applicationMusicPlayer.pause() + //print("pausing") + }*/ + + musicHandler.updateMusic() + //print("updating") + } + + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. - /*let message = "\n\n\n\n\n\n" - let alert = UIAlertController(title: "Please Select Note", message: message, preferredStyle: UIAlertController.Style.actionSheet) - alert.isModalInPopover = true - - let pickerFrame = UIPickerView(frame: CGRect(x: 5, y: 20, width: 400 - 20, height: 140)) // CGRectMake(left, top, width, height) - left and top are like margins - pickerFrame.tag = 555 - //set the pickers datasource and delegate - pickerFrame.delegate = self - - //Add the picker to the alert controller - alert.view.addSubview(pickerFrame) - let okAction = UIAlertAction(title: "OK", style: .default, handler: { - (alert: UIAlertAction!) -> Void in - //Perform Action - }) - alert.addAction(okAction) - let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil) - alert.addAction(cancelAction) - self.parent!.present(alert, animated: true, completion: { }) - */ + filesTable.delegate = self + filesTable.dataSource = self + + filesTable.alwaysBounceVertical = true + let refresher = UIRefreshControl() + refresher.addTarget(self, action: #selector(refreshStream), for: .valueChanged) + + refreshControl = refresher + filesTable.addSubview(refreshControl) + } + + @objc func refreshStream() { + + print("refresh") + filesTable?.reloadData() + + refreshControl.endRefreshing() + } override func viewWillAppear(_ animated: Bool) { @@ -69,45 +224,228 @@ class FourthViewController: UIViewController, UIPickerViewDelegate{ } gradient.locations = [0.00, 1.00] - + + /*let gradientChangeAnimation = CABasicAnimation(keyPath: "colors") + gradientChangeAnimation.duration = 5.0 + if((GlobalVars.hour >= 8) && (GlobalVars.hour <= 19)){ //Daytime + //print("day") + gradientChangeAnimation.toValue = [UIColor(red: 0.00, green: 0.31, blue: 0.59, alpha: 1).cgColor, UIColor(red: 0.07, green: 0.45, blue: 0.87, alpha: 1).cgColor] + } else if((GlobalVars.hour >= 20) && (GlobalVars.hour <= 21)){//Sunset + //print("sunset") + gradientChangeAnimation.toValue = [UIColor(red: 0.00, green: 0.31, blue: 0.59, alpha: 1).cgColor, UIColor(red: 1.00, green: 0.61, blue: 0.43, alpha: 1).cgColor] + } else if((GlobalVars.hour >= 22) || (GlobalVars.hour <= 5)){//Night + //print("night") + gradientChangeAnimation.toValue = [ + UIColor(red: 0.68, green: 0.67, blue: 1.0, alpha: 1).cgColor, + UIColor(red: 1.00, green: 0.00, blue: 0.09, alpha: 1).cgColor + ] + } else if((GlobalVars.hour >= 6) && (GlobalVars.hour <= 7)){//Sunrise + //print("sunrise") + gradientChangeAnimation.toValue = [UIColor(red: 0.02, green: 0.26, blue: 0.49, alpha: 1).cgColor, UIColor(red: 0.89, green: 0.68, blue: 0.60, alpha: 1).cgColor] + } + gradientChangeAnimation.autoreverses = true + gradientChangeAnimation.fillMode = CAMediaTimingFillMode.forwards + gradientChangeAnimation.isRemovedOnCompletion = false + gradient.add(gradientChangeAnimation, forKey: "colorChange") + */ + // add the gradient to the view gradientView.layer.addSublayer(gradient) - /// Create a sequence - var sequence : MusicSequence? = nil - var musicSequenceStatus = NewMusicSequence(&sequence) - var track : MusicTrack? = nil - /// Create a music track containg a sequence and a music track - var musicTrack = MusicSequenceNewTrack(sequence!, &track) - var time = MusicTimeStamp(1.0) - - - // The notes of the song - var notes: [UInt8] = [50,47,48,50/*,128*/,40,48,/*128,*/47,43,45,47/*,128*/,38,45] - - for index:Int in 0...11 { - var note = MIDINoteMessage(channel: 0, - note: notes[index], - velocity: 64, - releaseVelocity: 0, - duration: 1.0) - guard let track = track else {fatalError()} - musicTrack = MusicTrackNewMIDINoteEvent(track, time, ¬e) - time += 1 - } - - - // Creating a player - var musicNotePlayer : MusicPlayer? = nil - var player = NewMusicPlayer(&musicNotePlayer) - - player = MusicPlayerSetSequence(musicNotePlayer!, sequence) - player = MusicPlayerStart(musicNotePlayer!) + /*switch GlobalVars.selectedMusicList { + case 0: + //print(GlobalVars.musicSelectionID0) + print(GlobalVars.musicSelection0) + case 1: + //print(GlobalVars.musicSelectionID1) + print(GlobalVars.musicSelection1) + case 2: + //print(GlobalVars.musicSelectionID2) + print(GlobalVars.musicSelection2) + case 3: + //print(GlobalVars.musicSelectionID3) + print(GlobalVars.musicSelection3) + case 4: + //print(GlobalVars.musicSelectionID4) + print(GlobalVars.musicSelection4) + default: + //print(GlobalVars.musicSelectionID0) + print(GlobalVars.musicSelection0) + }*/ } override func viewDidDisappear(_ animated: Bool) { gradientView.layer.sublayers?.forEach { $0.removeFromSuperlayer() } } + + @IBAction func importPopup(_ sender: Any) { + + //1. Create the alert controller. + let alert = UIAlertController(title: "Download Music", message: "Enter a link to an MP3, ZIP, or properly formatted JSON file \n If your files aren't visible after entering the link, try pulling down to refresh", preferredStyle: .alert) + + //2. Add the text field. You can configure it however you need. + alert.addTextField { (textField) in + textField.text = "https://downloads.khinsider.com" + } + + // 3. Grab the value from the text field, and print it when the user clicks OK. + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {_ in + let textField = alert.textFields![0] // Force unwrapping because we know it exists. + print("Text field: \(textField.text)") + + if (!textField.text!.isEmpty) { + let fileURL = URL(string: textField.text!) + let sessionConfig = URLSessionConfiguration.default + let session = URLSession(configuration: sessionConfig) + + let request = URLRequest(url:fileURL!) + + + if ((fileURL?.pathExtension == "mp3") || (fileURL?.pathExtension == "zip")) { + let documentsUrl:URL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!) as URL + let destinationFileUrl = documentsUrl.appendingPathComponent(fileURL?.lastPathComponent ?? "fileerr") + + self.downloadSong(request: request, destination: destinationFileUrl, runFrom: "Default") + + } else if (fileURL?.pathExtension == "json") { + + print("file is json") + + let documentsUrl:URL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!) as URL + + let task = session.dataTask(with: request) { (data, response, error) in + if error == nil { + do{ + let jsonData = try JSON(data: data!) + print(jsonData) + //print(jsonURL) + for (index,subJson):(String, JSON) in jsonData { + let hour:Int = subJson["hour"].intValue + print(hour) + let url:URL = subJson["url"].url! + //let url:URL = URL(fileURLWithPath: urlAsString) + let urlRequest = URLRequest(url:url) + print(url) + + var DLHourPadding:String = "" + + if(hour < 10){ + DLHourPadding = "0" + } + + let destinationFileUrl = documentsUrl.appendingPathComponent((fileURL!.deletingPathExtension().lastPathComponent) + " - " + DLHourPadding + String(hour) + " - " + url.lastPathComponent) + print(destinationFileUrl) + self.downloadSong(request: urlRequest, destination: destinationFileUrl, runFrom: "JSON") + + GlobalVars.musicFormatType[GlobalVars.selectedMusicList][hour] = false + GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][hour] = 0 + GlobalVars.musicSelection[GlobalVars.selectedMusicList][hour] = destinationFileUrl.lastPathComponent + GlobalVars.musicFileURL[GlobalVars.selectedMusicList][hour] = destinationFileUrl.lastPathComponent + } + + } + catch { + print(error) + } + } + } + task.resume() + + + } else { + let unsupportedAlert = UIAlertController(title: "", message: "Please make sure your url links to an mp3, zip, or json file", preferredStyle: UIAlertController.Style.alert) + unsupportedAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil)) + self.present(unsupportedAlert, animated: true, completion: nil) + } + + } + + })) + + // 4. Present the alert. + self.present(alert, animated: true, completion: nil) + + } + @IBAction func close(_ sender: Any) { + dismiss(animated: true) + } + + func downloadSong(request:URLRequest,destination:URL,runFrom:String) -> Void { + let sessionConfig = URLSessionConfiguration.default + let session = URLSession(configuration: sessionConfig) + let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in + if let tempLocalUrl = tempLocalUrl, error == nil { + + do { + try FileManager.default.copyItem(at: tempLocalUrl, to: destination) + } catch (let writeError) { + print("Error creating a file \(destination) : \(writeError)") + DispatchQueue.main.async { + let writeErrAlert = UIAlertController(title: "Error Saving File '\(destination.lastPathComponent)'", message: "Check to make sure another file with the same name doesn't already exist", preferredStyle: UIAlertController.Style.alert) + writeErrAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil)) + self.present(writeErrAlert, animated: true, completion: nil) + self.filesTable.reloadData() + } + } + // Success + if (runFrom == "Default") { + if let statusCode = (response as? HTTPURLResponse)?.statusCode { + print("Successfully downloaded. Status code: \(statusCode)") + DispatchQueue.main.async { + let dlAlert = UIAlertController(title: "Successfully Downloaded", message: "\(destination.lastPathComponent)", preferredStyle: UIAlertController.Style.alert) + dlAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil)) + self.present(dlAlert, animated: true, completion: nil) + self.filesTable.reloadData() + } + } + } + } else { + print("Error took place while downloading a file. Error description: %@", error?.localizedDescription as Any); + DispatchQueue.main.async { + let networkErrAlert = UIAlertController(title: "Error Downloading File '\(destination.lastPathComponent)'", message: "Check to make sure your internet connection is stable and that the URL is correct", preferredStyle: UIAlertController.Style.alert) + networkErrAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil)) + self.present(networkErrAlert, animated: true, completion: nil) + self.filesTable.reloadData() + } + } + + if destination.pathExtension == "zip" { + print("it's a zip!") + + if runFrom == "Default" { + let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true) + let filePathToDelete = dirs[0].appendingFormat("/" + destination.lastPathComponent) + + do { + //let unzipDirectory = try Zip.quickUnzipFile(destination) // Unzip + let unzipDirectory = try Zip.unzipFile(destination, destination: URL(fileURLWithPath: dirs[0]), overwrite: false, password: nil) + } + catch { + print("Something went wrong: \(error)") + } + + do { + let fileManager = FileManager.default + + // Check if file exists + if fileManager.fileExists(atPath: filePathToDelete) { + // Delete file + try fileManager.removeItem(atPath: filePathToDelete) + } else { + print("File does not exist") + } + } + catch let error as NSError { + print("An error took place: \(error)") + } + } else if runFrom == "JSON" { + print("Cannot download zip in json!") + } + + } + } + task.resume() + } } diff --git a/grandfatherrock/Info.plist b/grandfatherrock/Info.plist index 561899e..793ea51 100644 --- a/grandfatherrock/Info.plist +++ b/grandfatherrock/Info.plist @@ -24,6 +24,8 @@ public.app-category.music LSRequiresIPhoneOS + LSSupportsOpeningDocumentsInPlace + NSAppleMusicUsageDescription To select songs for each hour UIAppFonts @@ -53,6 +55,8 @@ audio + UIFileSharingEnabled + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/grandfatherrock/SecondViewController.swift b/grandfatherrock/SecondViewController.swift index b4593b0..1028380 100644 --- a/grandfatherrock/SecondViewController.swift +++ b/grandfatherrock/SecondViewController.swift @@ -81,7 +81,16 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa cell.accessibilityLabel = cell.hourLabel.text cell.accessibilityLabel?.append(". ") - switch GlobalVars.selectedMusicList { + cell.songLabel?.text = GlobalVars.musicSelection[GlobalVars.selectedMusicList][indexPath.row] + if ((GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][indexPath.row] == 0) && (GlobalVars.musicFileURL[GlobalVars.selectedMusicList][indexPath.row] == "")){ + cell.accessoryType = .detailButton + cell.accessibilityLabel?.append("No song set") + } else { + cell.accessoryType = .none + cell.accessibilityLabel?.append(cell.songLabel.text!) + } + + /*switch GlobalVars.selectedMusicList { case 0: cell.songLabel?.text = GlobalVars.musicSelection0[indexPath.row] if (GlobalVars.musicSelectionID0[indexPath.row] == 0){ @@ -131,7 +140,7 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa cell.songLabel?.text = "uh oh!" cell.accessoryType = .detailButton cell.accessibilityLabel?.append("No song set") - } + }*/ cell.accessibilityHint?.append("Press to set a song for this hour") @@ -146,7 +155,13 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){ if editingStyle == .delete { - switch GlobalVars.selectedMusicList { + + GlobalVars.musicSelection[GlobalVars.selectedMusicList][indexPath.row] = "" + GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][indexPath.row] = 0 + GlobalVars.musicFileURL[GlobalVars.selectedMusicList][indexPath.row] = "" + GlobalVars.musicFormatType[GlobalVars.selectedMusicList][indexPath.row] = true + + /*switch GlobalVars.selectedMusicList { case 0: GlobalVars.musicSelection0[indexPath.row] = "" GlobalVars.musicSelectionID0[indexPath.row] = 0 @@ -165,7 +180,7 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa default: GlobalVars.musicSelection0[indexPath.row] = "" GlobalVars.musicSelectionID0[indexPath.row] = 0 - } + }*/ hoursTable.reloadData() @@ -177,15 +192,31 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ - //let mediaPicker = MPMediaPickerController(mediaTypes: MPMediaType.music) - tableHour = indexPath.row - //print(tableHour) - present(mediaPicker, animated: true, completion: nil) - /*if (selectedSong != "") { - GlobalVars.musicSelection[indexPath.row] = selectedSong - selectedSong = "" + let alert = UIAlertController(title: "Where do you want to select music from?", message: "", preferredStyle: .actionSheet) + alert.popoverPresentationController?.sourceView = self.view + alert.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 2.0, width: 1.0, height: 1.0) + if (UIDevice.current.userInterfaceIdiom == .pad){ + alert.popoverPresentationController!.permittedArrowDirections = [] } - self.hoursTable.reloadData()*/ + //shareMenu.popoverPresentationController.sourceView = self.view + //shareMenu.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0) + + alert.addAction(UIAlertAction(title: "From Music App", style: .default, handler: {_ in + self.tableHour = indexPath.row + self.present(self.mediaPicker, animated: true, completion: nil) + })) + alert.addAction(UIAlertAction(title: "From File", style: .default, handler: {_ in + let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "FourthViewController") as! UIViewController + GlobalVars.selectedCell = indexPath.row + NotificationVC.modalPresentationStyle = .fullScreen + self.present(NotificationVC, animated: true, completion: nil) + + })) + alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {_ in + + })) + + self.present(alert, animated: true) } @@ -223,9 +254,18 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa //print("selected song") selectedSong = String(mediaItemCollection.items[0].title ?? "NOTHING") //print(selectedSong) + /* + GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][tableHour] = mediaItemCollection.items[0].persistentID + GlobalVars.musicSelection[GlobalVars.selectedMusicList][tableHour] = selectedSong + */ + GlobalVars.musicSelection[GlobalVars.selectedMusicList][tableHour] = selectedSong + GlobalVars.musicSelectionID[GlobalVars.selectedMusicList][tableHour] = mediaItemCollection.items[0].persistentID + GlobalVars.musicFileURL[GlobalVars.selectedMusicList][tableHour] = "" + GlobalVars.musicFormatType[GlobalVars.selectedMusicList][tableHour] = true + //print(GlobalVars.selectedMusicList) - switch GlobalVars.selectedMusicList { + /*switch GlobalVars.selectedMusicList { case 0: GlobalVars.musicSelectionID0[tableHour] = mediaItemCollection.items[0].persistentID GlobalVars.musicSelection0[tableHour] = selectedSong @@ -244,7 +284,7 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa default: GlobalVars.musicSelectionID0[tableHour] = mediaItemCollection.items[0].persistentID GlobalVars.musicSelection0[tableHour] = selectedSong - } + }*/ //GlobalVars.musicSelectionID1[tableHour] = mediaItemCollection.items[0].persistentID //GlobalVars.musicSelection1[tableHour] = selectedSong @@ -271,8 +311,10 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa @IBAction func selectListControlChanged(_ sender: Any) { MPMusicPlayerController.applicationMusicPlayer.stop() + audioPlayer.stop() GlobalVars.selectedMusicList = selectListControl.selectedSegmentIndex + defaults.set(selectListControl.selectedSegmentIndex, forKey: "savedSelectedMusicList") //print(GlobalVars.selectedMusicList) hoursTable.reloadData() /*var noSongs = false @@ -345,11 +387,12 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa hoursTable.dataSource = self selectListControl.selectedSegmentIndex = GlobalVars.selectedMusicList - } override func viewWillAppear(_ animated: Bool) { + hoursTable.reloadData() + let prevHour = GlobalVars.hour GlobalVars.hour = Calendar.current.component(.hour, from: Date()) if GlobalVars.hour != prevHour { @@ -402,85 +445,13 @@ class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDa // add the gradient to the view gradientView.layer.addSublayer(gradient) - switch GlobalVars.selectedMusicList { - case 0: - //print(GlobalVars.musicSelectionID0) - print(GlobalVars.musicSelection0) - case 1: - //print(GlobalVars.musicSelectionID1) - print(GlobalVars.musicSelection1) - case 2: - //print(GlobalVars.musicSelectionID2) - print(GlobalVars.musicSelection2) - case 3: - //print(GlobalVars.musicSelectionID3) - print(GlobalVars.musicSelection3) - case 4: - //print(GlobalVars.musicSelectionID4) - print(GlobalVars.musicSelection4) - default: - //print(GlobalVars.musicSelectionID0) - print(GlobalVars.musicSelection0) - } } override func viewWillDisappear(_ animated: Bool) { - do { - try defaults.setObject(GlobalVars.musicSelection0, forKey: "savedMusicSelection0") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID0, forKey: "savedMusicSelectionID0") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection1, forKey: "savedMusicSelection1") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID1, forKey: "savedMusicSelectionID1") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection2, forKey: "savedMusicSelection2") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID2, forKey: "savedMusicSelectionID2") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection3, forKey: "savedMusicSelection3") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID3, forKey: "savedMusicSelectionID3") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelection4, forKey: "savedMusicSelection4") - } catch { - //print(error.localizedDescription) - } - do { - try defaults.setObject(GlobalVars.musicSelectionID4, forKey: "savedMusicSelectionID4") - } catch { - //print(error.localizedDescription) - } - - do { - try defaults.setObject(GlobalVars.selectedMusicList, forKey: "savedSelectedMusicList") - } catch { - //print(error.localizedDescription) - } + do {try defaults.setObject(GlobalVars.musicSelection, forKey: "savedMusicSelection")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicSelectionID, forKey: "savedMusicSelectionID")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicFileURL, forKey: "savedMusicFileURL")} catch {/*print(error.localizedDescription)*/} + do {try defaults.setObject(GlobalVars.musicFormatType, forKey: "savedMusicFileType")} catch {/*print(error.localizedDescription)*/} } override func viewDidDisappear(_ animated: Bool) {