captains-log

[TO-FIX] audiojournaling app
git clone git://git.figbert.com/captains-log.git
Log | Files | Refs

commit 312e3876d71309eed8597ce37b3a644a7aae2822
parent abff03fc686dfe9dc8a0882f7bac64c362a8b2a0
Author: therealFIGBERT <figbertwelner@gmail.com>
Date:   Sat, 19 Oct 2019 10:08:46 -0700

Moving major views to their own files, converting stardate to a Date extension

Diffstat:
AHomepage.swift | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ASettings.swift | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
McaptainsLog.xcodeproj/project.pbxproj | 12++++++++++++
McaptainsLog/ContentView.swift | 105+++++--------------------------------------------------------------------------
AcaptainsLog/Extensions.swift | 48++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 224 insertions(+), 99 deletions(-)

diff --git a/Homepage.swift b/Homepage.swift @@ -0,0 +1,85 @@ +// +// Homepage.swift +// captainsLog +// +// Created by Benjamin Welner on 10/18/19. +// Copyright © 2019 FIGBERT Industries. All rights reserved. +// + +import SwiftUI + +struct Homepage: View { + @State private var recording: Bool = false + @State private var timeElapsed: Double = 0.00 + @State private var timer:Timer? + @State private var msg: String = "" + @State private var date = Date() + + var body: some View { + NavigationView { + VStack { + Spacer() + VStack { + Button(action: { + self.recording.toggle() + if (self.recording) { + self.timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { timer in + self.timeElapsed += 0.01 + if (self.timeElapsed <= 0.75) { + self.msg = "Captain's" + } else if (self.timeElapsed <= 1.5) { + self.msg = "log" + } else if (self.timeElapsed <= 2.25) { + self.msg = "stardate" + } else if (self.timeElapsed <= 5) { + self.msg = "\(self.date.stardate())" + } else { + self.msg = "" + } + }) + } else { + self.timer?.invalidate() + } + }) { + Image( + uiImage: UIImage( + systemName: recording ? "mic.fill" : "mic", + withConfiguration: UIImage.SymbolConfiguration( + pointSize: CGFloat.init(100), + weight: .regular, + scale: .large + ) + )! + ) + } + .foregroundColor(Color.primary) + .animation(.none) + Text("\(timeElapsed, specifier: "%.2f") sec") + } + Spacer() + VStack { + Text("\(self.msg)") + .font(.system(size: 24, weight: .regular, design: .rounded)) + Image( + uiImage: UIImage( + systemName: "waveform", + withConfiguration: UIImage.SymbolConfiguration( + pointSize: CGFloat.init(50), + weight: .medium, + scale: .large + ) + )! + ) + .foregroundColor(Color.primary) + } + Spacer() + } + } + } +} + +struct Homepage_Previews: PreviewProvider { + static var previews: some View { + Homepage() + } +} diff --git a/Settings.swift b/Settings.swift @@ -0,0 +1,73 @@ +// +// Settings.swift +// captainsLog +// +// Created by Benjamin Welner on 10/18/19. +// Copyright © 2019 FIGBERT Industries. All rights reserved. +// + +import SwiftUI + +struct Settings: View { + @State private var saveLocally = true + @State private var saveICloud = true + @State private var currentDateFormat = 0 + @State private var currentMessage = 0 + @State private var name = "User" + var dateFormats = [ + "Stardate", + "Gregorian date", + "Jewish date", + "Islamic date", + "Chinese date" + ] + var messages = [ + "Captain’s log, [DATE]", + "A day in the life of [NAME], entry [NUM]", + "[NAME]'s audio journal, day [NUM]", + "Hi, I'm [NAME]", + "[NAME]'s audio journal, for your ears only.", + "Random thoughts, entry [NUM]", + "Here's the tea, sis", + "Rise and grind, gamers" + ] + var body: some View { + NavigationView{ + Form { + Section(header: Text("Save")) { + Toggle(isOn: $saveLocally) { + Text("Save on device") + } + Toggle(isOn: $saveICloud) { + Text("Save to iCloud") + } + } + Section(header: Text("Name")) { + TextField("Name", text: $name) + } + Section(header: Text("Prompt")) { + Picker("Date format", selection: $currentDateFormat) { + ForEach(0 ..< dateFormats.count) { + Text("\(self.dateFormats[$0])") + } + } + Picker("Intro message", selection: $currentMessage) { + ForEach(0 ..< messages.count) { + Text("\(self.messages[$0])") + } + } + } + } + .navigationBarTitle( + Text("Captain's Log Settings"), + displayMode: .inline + ) + } + } +} + +struct Settings_Previews: PreviewProvider { + static var previews: some View { + Settings() + } +} diff --git a/captainsLog.xcodeproj/project.pbxproj b/captainsLog.xcodeproj/project.pbxproj @@ -13,6 +13,9 @@ 3A35D6E623579F7C005B7610 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3A35D6E523579F7C005B7610 /* Assets.xcassets */; }; 3A35D6E923579F7C005B7610 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3A35D6E823579F7C005B7610 /* Preview Assets.xcassets */; }; 3A35D6EC23579F7C005B7610 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3A35D6EA23579F7C005B7610 /* LaunchScreen.storyboard */; }; + 3A76A1E6235AD06C00964901 /* Homepage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76A1E5235AD06C00964901 /* Homepage.swift */; }; + 3A76A1E8235AD08C00964901 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76A1E7235AD08C00964901 /* Settings.swift */; }; + 3A76A1EA235AE6D500964901 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76A1E9235AE6D500964901 /* Extensions.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -24,6 +27,9 @@ 3A35D6E823579F7C005B7610 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; }; 3A35D6EB23579F7C005B7610 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; }; 3A35D6ED23579F7C005B7610 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; + 3A76A1E5235AD06C00964901 /* Homepage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Homepage.swift; sourceTree = SOURCE_ROOT; }; + 3A76A1E7235AD08C00964901 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = SOURCE_ROOT; }; + 3A76A1E9235AE6D500964901 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -59,6 +65,9 @@ 3A35D6DF23579F7B005B7610 /* AppDelegate.swift */, 3A35D6E123579F7B005B7610 /* SceneDelegate.swift */, 3A35D6E323579F7B005B7610 /* ContentView.swift */, + 3A76A1E9235AE6D500964901 /* Extensions.swift */, + 3A76A1E5235AD06C00964901 /* Homepage.swift */, + 3A76A1E7235AD08C00964901 /* Settings.swift */, 3A35D6E523579F7C005B7610 /* Assets.xcassets */, 3A35D6EA23579F7C005B7610 /* LaunchScreen.storyboard */, 3A35D6ED23579F7C005B7610 /* Info.plist */, @@ -149,6 +158,9 @@ 3A35D6E023579F7B005B7610 /* AppDelegate.swift in Sources */, 3A35D6E223579F7B005B7610 /* SceneDelegate.swift in Sources */, 3A35D6E423579F7B005B7610 /* ContentView.swift in Sources */, + 3A76A1E6235AD06C00964901 /* Homepage.swift in Sources */, + 3A76A1E8235AD08C00964901 /* Settings.swift in Sources */, + 3A76A1EA235AE6D500964901 /* Extensions.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/captainsLog/ContentView.swift b/captainsLog/ContentView.swift @@ -8,107 +8,14 @@ import SwiftUI -extension Double { - func withCommas() -> String { - let numberFormatter = NumberFormatter() - numberFormatter.numberStyle = NumberFormatter.Style.decimal - return numberFormatter.string(from: NSNumber(value:self))! - } -} - struct ContentView: View { - @State private var recording = false - @State private var timeElapsed = 0.00 - @State private var timer:Timer? - @State private var msg = "" - var stardate: Double = { () -> Double in - let date = Date() - let calendar = Calendar.current - let year = calendar.component(.year, from: date) - let month = calendar.component(.month, from: date) - let day = calendar.component(.day, from: date) - let leapyear = !(year % 100 == 0) && year % 4 == 0 - let months = [ - 1: 0, - 2: 31, - 3: !leapyear ? 59 : 60, - 4: !leapyear ? 90 : 91, - 5: !leapyear ? 120 : 121, - 6: !leapyear ? 151 : 152, - 7: !leapyear ? 181 : 182, - 8: !leapyear ? 212 : 213, - 9: !leapyear ? 243 : 244, - 10: !leapyear ? 273 : 274, - 11: !leapyear ? 304 : 305, - 12: !leapyear ? 334 : 335 - ] - let n = !leapyear ? 365 : 366 - let b = 2005 - let c = 58000.0 - let monthValue = Double(months[month] ?? 0) - let stageOne = Double(1000*(year-b)) - let stageTwo = (1000.0/Double(n))*(monthValue + Double(day) - 1.0) - return Double(round(100*(c + stageOne + stageTwo))/100) - }() - + @State private var homePage = true var body: some View { - NavigationView { - VStack { - Spacer() - VStack { - Button(action: { - self.recording.toggle() - if (self.recording) { - self.timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { timer in - self.timeElapsed += 0.01 - if (self.timeElapsed <= 0.75) { - self.msg = "Captain's" - } else if (self.timeElapsed <= 1.5) { - self.msg = "log" - } else if (self.timeElapsed <= 2.25) { - self.msg = "stardate" - } else if (self.timeElapsed <= 5) { - self.msg = "\(self.stardate.withCommas())" - } else { - self.msg = "" - } - }) - } else { - self.timer?.invalidate() - } - }) { - Image( - uiImage: UIImage( - systemName: recording ? "mic.fill" : "mic", - withConfiguration: UIImage.SymbolConfiguration( - pointSize: CGFloat.init(100), - weight: .regular, - scale: .large - ) - )! - ) - } - .foregroundColor(Color.primary) - .animation(.none) - Text("\(timeElapsed, specifier: "%.2f") sec") - } - Spacer() - VStack { - Text("\(self.msg)") - .font(.system(size: 24, weight: .regular, design: .rounded)) - Image( - uiImage: UIImage( - systemName: "waveform", - withConfiguration: UIImage.SymbolConfiguration( - pointSize: CGFloat.init(50), - weight: .medium, - scale: .large - ) - )! - ) - .foregroundColor(Color.primary) - } - Spacer() + VStack { + if (homePage) { + Homepage() + } else { + Settings() } } } diff --git a/captainsLog/Extensions.swift b/captainsLog/Extensions.swift @@ -0,0 +1,48 @@ +// +// extensions.swift +// captainsLog +// +// Created by Benjamin Welner on 10/18/19. +// Copyright © 2019 FIGBERT Industries. All rights reserved. +// + +import Foundation + +extension Double { + func withCommas() -> String { + let numberFormatter = NumberFormatter() + numberFormatter.numberStyle = NumberFormatter.Style.decimal + return numberFormatter.string(from: NSNumber(value:self))! + } +} +extension Date { + func stardate() -> Double { + let calendar = Calendar.current + let year = calendar.component(.year, from: self) + let month = calendar.component(.month, from: self) + let day = calendar.component(.day, from: self) + let leapyear = !(year % 100 == 0) && year % 4 == 0 + let months = [ + 1: 0, + 2: 31, + 3: !leapyear ? 59 : 60, + 4: !leapyear ? 90 : 91, + 5: !leapyear ? 120 : 121, + 6: !leapyear ? 151 : 152, + 7: !leapyear ? 181 : 182, + 8: !leapyear ? 212 : 213, + 9: !leapyear ? 243 : 244, + 10: !leapyear ? 273 : 274, + 11: !leapyear ? 304 : 305, + 12: !leapyear ? 334 : 335 + ] + let n = !leapyear ? 365 : 366 + let b = 2005 + let c = 58000.0 + let monthValue = Double(months[month] ?? 0) + let stageOne = Double(1000*(year-b)) + let stageTwo = (1000.0/Double(n))*(monthValue + Double(day) - 1.0) + let stageThree = c + stageOne + stageTwo + return Double(round(100*stageThree)/100) + } +}