From a155df8e85532b2fcee430a3a1ccc5d94892c3a6 Mon Sep 17 00:00:00 2001 From: Joel DeSante Date: Fri, 3 Feb 2023 18:49:11 -0500 Subject: [PATCH 1/4] Update tabbar icons --- moodmoji/ContentView.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moodmoji/ContentView.swift b/moodmoji/ContentView.swift index e280b7e..6fbe00c 100644 --- a/moodmoji/ContentView.swift +++ b/moodmoji/ContentView.swift @@ -13,12 +13,12 @@ struct ContentView: View { TabView { EntriesView() .tabItem { - Image(systemName: "1.circle.fill") + Image(systemName: "book.fill") Text("Entries") } LogView() .tabItem { - Image(systemName: "2.square.fill") + Image(systemName: "pencil.line") Text("Log") } } From 4b0df90fe873b442dad6cbc13dce90fef3bf1fea Mon Sep 17 00:00:00 2001 From: Joel DeSante Date: Fri, 3 Feb 2023 20:08:26 -0500 Subject: [PATCH 2/4] Improve code quality and seperation of concerns --- moodmoji.xcodeproj/project.pbxproj | 62 +++++++++++-- moodmoji/ContentView.swift | 2 +- moodmoji/EntriesHandler.swift | 90 ------------------- moodmoji/Timeline.swift | 46 ++++++++++ moodmoji/log/Log.swift | 27 ++++++ moodmoji/log/LogFileSystem.swift | 58 ++++++++++++ moodmoji/log/LogModel.swift | 13 +++ moodmoji/{ => views/log}/LogView.swift | 0 .../timeline/TimelineView.swift} | 4 +- 9 files changed, 200 insertions(+), 102 deletions(-) delete mode 100644 moodmoji/EntriesHandler.swift create mode 100644 moodmoji/Timeline.swift create mode 100644 moodmoji/log/Log.swift create mode 100644 moodmoji/log/LogFileSystem.swift create mode 100644 moodmoji/log/LogModel.swift rename moodmoji/{ => views/log}/LogView.swift (100%) rename moodmoji/{EntriesView.swift => views/timeline/TimelineView.swift} (97%) diff --git a/moodmoji.xcodeproj/project.pbxproj b/moodmoji.xcodeproj/project.pbxproj index 34f0097..15ef7d3 100644 --- a/moodmoji.xcodeproj/project.pbxproj +++ b/moodmoji.xcodeproj/project.pbxproj @@ -7,10 +7,13 @@ objects = { /* Begin PBXBuildFile section */ + 9E0E6B5B298DDA6500AC89B1 /* LogModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E0E6B5A298DDA6500AC89B1 /* LogModel.swift */; }; + 9E0E6B5E298DDBEA00AC89B1 /* LogFileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E0E6B5D298DDBEA00AC89B1 /* LogFileSystem.swift */; }; + 9E0E6B60298DDEBB00AC89B1 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E0E6B5F298DDEBB00AC89B1 /* Log.swift */; }; 9EABF78F29875A3400557EFE /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF78E29875A3400557EFE /* Colors.swift */; }; 9EABF791298763B300557EFE /* LogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF790298763B200557EFE /* LogView.swift */; }; - 9EABF793298766FA00557EFE /* EntriesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF792298766FA00557EFE /* EntriesView.swift */; }; - 9EABF79729876B4D00557EFE /* EntriesHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF79629876B4D00557EFE /* EntriesHandler.swift */; }; + 9EABF793298766FA00557EFE /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF792298766FA00557EFE /* TimelineView.swift */; }; + 9EABF79729876B4D00557EFE /* Timeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF79629876B4D00557EFE /* Timeline.swift */; }; 9EABF79929878F1800557EFE /* Entry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF79829878F1800557EFE /* Entry.swift */; }; 9EABF79B2987B12000557EFE /* Email.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF79A2987B12000557EFE /* Email.swift */; }; 9EABF79D2987B23D00557EFE /* MailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EABF79C2987B23D00557EFE /* MailView.swift */; }; @@ -24,10 +27,13 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 9E0E6B5A298DDA6500AC89B1 /* LogModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogModel.swift; sourceTree = ""; }; + 9E0E6B5D298DDBEA00AC89B1 /* LogFileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogFileSystem.swift; sourceTree = ""; }; + 9E0E6B5F298DDEBB00AC89B1 /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = ""; }; 9EABF78E29875A3400557EFE /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = ""; }; 9EABF790298763B200557EFE /* LogView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogView.swift; sourceTree = ""; }; - 9EABF792298766FA00557EFE /* EntriesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntriesView.swift; sourceTree = ""; }; - 9EABF79629876B4D00557EFE /* EntriesHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntriesHandler.swift; sourceTree = ""; }; + 9EABF792298766FA00557EFE /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = ""; }; + 9EABF79629876B4D00557EFE /* Timeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline.swift; sourceTree = ""; }; 9EABF79829878F1800557EFE /* Entry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Entry.swift; sourceTree = ""; }; 9EABF79A2987B12000557EFE /* Email.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Email.swift; sourceTree = ""; }; 9EABF79C2987B23D00557EFE /* MailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MailView.swift; sourceTree = ""; }; @@ -52,6 +58,41 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 9E0E6B56298DD67800AC89B1 /* views */ = { + isa = PBXGroup; + children = ( + 9E0E6B59298DD6A600AC89B1 /* log */, + 9E0E6B57298DD68000AC89B1 /* timeline */, + ); + path = views; + sourceTree = ""; + }; + 9E0E6B57298DD68000AC89B1 /* timeline */ = { + isa = PBXGroup; + children = ( + 9EABF792298766FA00557EFE /* TimelineView.swift */, + ); + path = timeline; + sourceTree = ""; + }; + 9E0E6B59298DD6A600AC89B1 /* log */ = { + isa = PBXGroup; + children = ( + 9EABF790298763B200557EFE /* LogView.swift */, + ); + path = log; + sourceTree = ""; + }; + 9E0E6B5C298DDBA500AC89B1 /* log */ = { + isa = PBXGroup; + children = ( + 9E0E6B5A298DDA6500AC89B1 /* LogModel.swift */, + 9E0E6B5D298DDBEA00AC89B1 /* LogFileSystem.swift */, + 9E0E6B5F298DDEBB00AC89B1 /* Log.swift */, + ); + path = log; + sourceTree = ""; + }; 9EB165D4298721CD004D4E49 = { isa = PBXGroup; children = ( @@ -71,15 +112,15 @@ 9EB165DF298721CD004D4E49 /* moodmoji */ = { isa = PBXGroup; children = ( + 9E0E6B5C298DDBA500AC89B1 /* log */, + 9E0E6B56298DD67800AC89B1 /* views */, 9EB165E0298721CD004D4E49 /* moodmojiApp.swift */, 9EABF78E29875A3400557EFE /* Colors.swift */, 9EB165E2298721CD004D4E49 /* ContentView.swift */, - 9EABF792298766FA00557EFE /* EntriesView.swift */, 9EABF79C2987B23D00557EFE /* MailView.swift */, 9EABF79829878F1800557EFE /* Entry.swift */, - 9EABF79629876B4D00557EFE /* EntriesHandler.swift */, + 9EABF79629876B4D00557EFE /* Timeline.swift */, 9EABF79A2987B12000557EFE /* Email.swift */, - 9EABF790298763B200557EFE /* LogView.swift */, 9EB165E4298721D0004D4E49 /* Assets.xcassets */, 9EB165E6298721D0004D4E49 /* Preview Content */, 9EB165F629872A05004D4E49 /* MoodGrid.swift */, @@ -167,11 +208,14 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9E0E6B5B298DDA6500AC89B1 /* LogModel.swift in Sources */, 9EABF79929878F1800557EFE /* Entry.swift in Sources */, + 9E0E6B60298DDEBB00AC89B1 /* Log.swift in Sources */, 9EB165F32987275C004D4E49 /* Mood.swift in Sources */, - 9EABF79729876B4D00557EFE /* EntriesHandler.swift in Sources */, - 9EABF793298766FA00557EFE /* EntriesView.swift in Sources */, + 9EABF79729876B4D00557EFE /* Timeline.swift in Sources */, + 9EABF793298766FA00557EFE /* TimelineView.swift in Sources */, 9EABF79B2987B12000557EFE /* Email.swift in Sources */, + 9E0E6B5E298DDBEA00AC89B1 /* LogFileSystem.swift in Sources */, 9EABF78F29875A3400557EFE /* Colors.swift in Sources */, 9EB165F729872A05004D4E49 /* MoodGrid.swift in Sources */, 9EABF79D2987B23D00557EFE /* MailView.swift in Sources */, diff --git a/moodmoji/ContentView.swift b/moodmoji/ContentView.swift index 6fbe00c..f82a8a0 100644 --- a/moodmoji/ContentView.swift +++ b/moodmoji/ContentView.swift @@ -11,7 +11,7 @@ struct ContentView: View { var body: some View { TabView { - EntriesView() + TimelineView() .tabItem { Image(systemName: "book.fill") Text("Entries") diff --git a/moodmoji/EntriesHandler.swift b/moodmoji/EntriesHandler.swift deleted file mode 100644 index 62b5b49..0000000 --- a/moodmoji/EntriesHandler.swift +++ /dev/null @@ -1,90 +0,0 @@ -// -// Entries.swift -// moodmoji -// -// Created by Joel DeSante on 1/29/23. -// - -import Foundation - -struct EntryLog: Identifiable { - let id: UUID; - let emotions: String; - let notes: String; - let date: Date; - - init(id: UUID = UUID(), emotions: String, notes: String, date: Date) { - self.id = id; - self.emotions = emotions; - self.notes = notes; - self.date = date; - } -} - -class EntriesHandler { - - private static func getDocumentsDirectory() -> URL { - let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) - return paths[0] - } - - static func getEntries() -> [EntryLog] { - - var entries: [EntryLog] = []; - - let directoryContents = try! FileManager.default.contentsOfDirectory( - at: getDocumentsDirectory(), - includingPropertiesForKeys: nil - ) - - let sortedEntries = directoryContents.sorted { - Double($0.lastPathComponent)! > Double($1.lastPathComponent)! - } - - for url in sortedEntries { - let contents = try! String(contentsOf: url); - let contentsPart = contents.split(whereSeparator: \.isNewline); - let date = Date(timeIntervalSince1970: Double(url.lastPathComponent)!) - - entries.append( - EntryLog( - emotions: String(contentsPart.first!), - notes: String(contentsPart.last!), - date: date - ) - ); - } - - return entries; - } - - static func getEntriesAsText() -> String { - let entries = getEntries(); - var text = ""; - for entry in entries { - text.append("\(entry.date.formatted())\n") - text.append("\(entry.emotions)\n") - text.append("\(entry.notes)\n") - text.append("---\n\n") - } - - return text; - } - - static func createEntry(date: Date, emotions: Set, notes: String) { - - let logName = "\(String(date.timeIntervalSince1970))" - let emotions = emotions.joined(separator: ", "); - let content = "Moods: \(emotions)\nLog: \(notes)"; - let filename = getDocumentsDirectory().appendingPathComponent(logName, conformingTo: .text); - - do { - try content.write(to: filename, atomically: true, encoding: String.Encoding.utf8) - print("Created Log") - } catch { - // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding - print("Failed to write to file system") - } - - } -} diff --git a/moodmoji/Timeline.swift b/moodmoji/Timeline.swift new file mode 100644 index 0000000..18190c0 --- /dev/null +++ b/moodmoji/Timeline.swift @@ -0,0 +1,46 @@ +// +// Entries.swift +// moodmoji +// +// Created by Joel DeSante on 1/29/23. +// + +import Foundation + +class Timeline { + + private let logModel: LogModel; + private var logs: [Log]; + + init() { + self.logModel = LogFileSystem(); + self.logs = []; + self.loadLogs(); + } + + func createLog(log: Log) { + logModel.save(log: log); + logs.append(log); + self.logs = self.sortLogsByDate(logs: self.logs); + } + + func loadLogs() { + self.logs = self.sortLogsByDate(logs: self.logModel.loadAllLogs()); + } + + func getLogsAsText() -> String { + var text = ""; + for log in logs { + text.append("\(log.getString())---\n\n") + } + return text; + } + + private func sortLogsByDate(logs: [Log]) -> [Log] { + let sortedEntries = logs.sorted { + $0.date > $1.date + } + + return sortedEntries; + } +} diff --git a/moodmoji/log/Log.swift b/moodmoji/log/Log.swift new file mode 100644 index 0000000..fa1d070 --- /dev/null +++ b/moodmoji/log/Log.swift @@ -0,0 +1,27 @@ +// +// Log.swift +// moodmoji +// +// Created by Joel DeSante on 2/3/23. +// + +import Foundation + +struct Log: Identifiable { + let id: UUID; + let emotions: String; + let notes: String; + let date: Date; + + init(id: UUID, emotions: String, notes: String, date: Date) { + self.id = id + self.emotions = emotions + self.notes = notes + self.date = date + } + + func getString() { + let output = "\(self.date.formatted())\n\(self.emotions)\n\(self.notes)\n"; + return + } +} diff --git a/moodmoji/log/LogFileSystem.swift b/moodmoji/log/LogFileSystem.swift new file mode 100644 index 0000000..6d2e4e6 --- /dev/null +++ b/moodmoji/log/LogFileSystem.swift @@ -0,0 +1,58 @@ +// +// FilePersistable.swift +// moodmoji +// +// Created by Joel DeSante on 2/3/23. +// + +import Foundation + +class LogFileSystem: LogModel { + + func save(log: Log) { + + let logName = "\(String(log.date.timeIntervalSince1970))" + let content = "Moods: \(log.emotions)\nLog: \(log.notes)"; + + let filename = getDocumentsDirectory().appendingPathComponent(logName, conformingTo: .text); + + do { + try content.write(to: filename, atomically: true, encoding: String.Encoding.utf8) + print("Created Log") + } catch { + // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding + print("Failed to write to file system") + } + } + + func loadAllLogs() -> [Log] { + + var logs: [Log] = []; + let directoryContents = try! FileManager.default.contentsOfDirectory( + at: getDocumentsDirectory(), + includingPropertiesForKeys: nil + ) + + for url in directoryContents { + let contents = try! String(contentsOf: url); + let contentsPart = contents.split(whereSeparator: \.isNewline); + let date = Date(timeIntervalSince1970: Double(url.lastPathComponent)!) + + logs.append( + Log( + id: UUID(), + emotions: String(contentsPart.first!), + notes: String(contentsPart.last!), + date: date + ) + ); + } + + return logs; + } + + private func getDocumentsDirectory() -> URL { + let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + return paths[0] + } +} diff --git a/moodmoji/log/LogModel.swift b/moodmoji/log/LogModel.swift new file mode 100644 index 0000000..0a94e27 --- /dev/null +++ b/moodmoji/log/LogModel.swift @@ -0,0 +1,13 @@ +// +// Persistable.swift +// moodmoji +// +// Created by Joel DeSante on 2/3/23. +// + +import Foundation + +protocol LogModel { + func save(log: Log) -> Void; + func loadAllLogs() -> [Log]; +} diff --git a/moodmoji/LogView.swift b/moodmoji/views/log/LogView.swift similarity index 100% rename from moodmoji/LogView.swift rename to moodmoji/views/log/LogView.swift diff --git a/moodmoji/EntriesView.swift b/moodmoji/views/timeline/TimelineView.swift similarity index 97% rename from moodmoji/EntriesView.swift rename to moodmoji/views/timeline/TimelineView.swift index 8f78057..33d1996 100644 --- a/moodmoji/EntriesView.swift +++ b/moodmoji/views/timeline/TimelineView.swift @@ -7,7 +7,7 @@ import SwiftUI -struct EntriesView: View { +struct TimelineView: View { @State var entries = EntriesHandler.getEntries(); @State private var mailData = ComposeMailData( @@ -57,6 +57,6 @@ struct EntriesView: View { struct EntriesView_Previews: PreviewProvider { static var previews: some View { - EntriesView() + TimelineView() } } From a9723322166d064bfb14e975ee5bb0527d72e919 Mon Sep 17 00:00:00 2001 From: Joel DeSante Date: Fri, 3 Feb 2023 20:39:27 -0500 Subject: [PATCH 3/4] Linked up timeline as observable object --- moodmoji/ContentView.swift | 6 ++- moodmoji/Timeline.swift | 4 +- moodmoji/views/log/LogView.swift | 14 +++++- moodmoji/views/timeline/TimelineView.swift | 50 +++++----------------- 4 files changed, 29 insertions(+), 45 deletions(-) diff --git a/moodmoji/ContentView.swift b/moodmoji/ContentView.swift index f82a8a0..8f7bd90 100644 --- a/moodmoji/ContentView.swift +++ b/moodmoji/ContentView.swift @@ -9,14 +9,16 @@ import SwiftUI struct ContentView: View { + @StateObject var timeline: Timeline = Timeline(); + var body: some View { TabView { - TimelineView() + TimelineView(timeline: timeline) .tabItem { Image(systemName: "book.fill") Text("Entries") } - LogView() + LogView(timeline: timeline) .tabItem { Image(systemName: "pencil.line") Text("Log") diff --git a/moodmoji/Timeline.swift b/moodmoji/Timeline.swift index 18190c0..58f3aeb 100644 --- a/moodmoji/Timeline.swift +++ b/moodmoji/Timeline.swift @@ -7,10 +7,10 @@ import Foundation -class Timeline { +class Timeline: ObservableObject { private let logModel: LogModel; - private var logs: [Log]; + @Published var logs: [Log]; init() { self.logModel = LogFileSystem(); diff --git a/moodmoji/views/log/LogView.swift b/moodmoji/views/log/LogView.swift index ffb5985..9e3cecf 100644 --- a/moodmoji/views/log/LogView.swift +++ b/moodmoji/views/log/LogView.swift @@ -9,6 +9,7 @@ import SwiftUI struct LogView: View { + @ObservedObject var timeline: Timeline; @State private var additionalNotes: String = ""; @State private var selectedEmotions = Set(); @State private var currentDate = Date() @@ -38,8 +39,17 @@ struct LogView: View { DatePicker("", selection: $currentDate, displayedComponents: [.date, .hourAndMinute]) .labelsHidden() Button("Log") { - EntriesHandler.createEntry(date: currentDate, emotions: selectedEmotions, notes: additionalNotes); + let log = Log( + id: UUID(), + emotions: selectedEmotions.joined(separator: ", "), + notes: additionalNotes, + date: currentDate + ) + + timeline.createLog(log: log); + + // Reset selectedEmotions = Set(); additionalNotes = ""; currentDate = Date() @@ -55,6 +65,6 @@ struct LogView: View { struct LogView_Previews: PreviewProvider { static var previews: some View { - LogView() + LogView(timeline: Timeline()) } } diff --git a/moodmoji/views/timeline/TimelineView.swift b/moodmoji/views/timeline/TimelineView.swift index 33d1996..b2d6459 100644 --- a/moodmoji/views/timeline/TimelineView.swift +++ b/moodmoji/views/timeline/TimelineView.swift @@ -9,54 +9,26 @@ import SwiftUI struct TimelineView: View { - @State var entries = EntriesHandler.getEntries(); - @State private var mailData = ComposeMailData( - subject: "Mood Log", - recipients: [""], - message: EntriesHandler.getEntriesAsText(), - attachments: [AttachmentData(data: EntriesHandler.getEntriesAsText().data(using: .utf8)!, - mimeType: "text/plain", - fileName: "text.txt") - ]); - @State private var showMailView = false - + @ObservedObject var timeline: Timeline; + var body: some View { ScrollView { VStack { - HStack { - Button(action: { - showMailView.toggle() - }) { - Text("Send mail") - }.disabled(!MailView.canSendMail) - .sheet(isPresented: $showMailView) { - MailView(data: $mailData) { result in - print(result) - } - } - Button("Reload") { - entries = EntriesHandler.getEntries(); - mailData = ComposeMailData( - subject: "Mood Log", - recipients: [""], - message: EntriesHandler.getEntriesAsText(), - attachments: [AttachmentData(data: EntriesHandler.getEntriesAsText().data(using: .utf8)!, - mimeType: "text/plain", - fileName: "text.txt") - ]); - } - } - Divider() - ForEach(entries) { entry in - Entry(emotions: entry.emotions, notes: entry.notes, date: entry.date) + Text("Logs") + .font(.title) + ForEach(timeline.logs) { log in + Entry(emotions: log.emotions, notes: log.notes, date: log.date) } } } } } -struct EntriesView_Previews: PreviewProvider { +struct TimelineView_Previews: PreviewProvider { + + @StateObject var timeline: Timeline = Timeline(); + static var previews: some View { - TimelineView() + TimelineView(timeline: Timeline()) } } From aab8ba54a2dd003619ae181b94d516d7faa298fa Mon Sep 17 00:00:00 2001 From: Joel DeSante Date: Fri, 3 Feb 2023 21:01:24 -0500 Subject: [PATCH 4/4] Fixed bugs. Readded email sender. --- moodmoji/ContentView.swift | 2 +- moodmoji/log/Log.swift | 4 +-- moodmoji/views/timeline/TimelineView.swift | 41 ++++++++++++++++++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/moodmoji/ContentView.swift b/moodmoji/ContentView.swift index 8f7bd90..ae17ba0 100644 --- a/moodmoji/ContentView.swift +++ b/moodmoji/ContentView.swift @@ -20,7 +20,7 @@ struct ContentView: View { } LogView(timeline: timeline) .tabItem { - Image(systemName: "pencil.line") + Image(systemName: "pencil.circle.fill") Text("Log") } } diff --git a/moodmoji/log/Log.swift b/moodmoji/log/Log.swift index fa1d070..abcfee2 100644 --- a/moodmoji/log/Log.swift +++ b/moodmoji/log/Log.swift @@ -20,8 +20,8 @@ struct Log: Identifiable { self.date = date } - func getString() { + func getString() -> String { let output = "\(self.date.formatted())\n\(self.emotions)\n\(self.notes)\n"; - return + return output; } } diff --git a/moodmoji/views/timeline/TimelineView.swift b/moodmoji/views/timeline/TimelineView.swift index b2d6459..5f5fd4d 100644 --- a/moodmoji/views/timeline/TimelineView.swift +++ b/moodmoji/views/timeline/TimelineView.swift @@ -10,10 +10,48 @@ import SwiftUI struct TimelineView: View { @ObservedObject var timeline: Timeline; + @State private var showMailView = false; + @State private var mailData = ComposeMailData( + subject: "Mood Log", + recipients: [""], + message: "", + attachments: [ + AttachmentData( + data: "".data(using: .utf8)!, + mimeType: "text/plain", + fileName: "log.txt" + ) + ] + ); var body: some View { ScrollView { VStack { + HStack { + Button("Send Email") { + mailData = ComposeMailData( + subject: "Mood Log", + recipients: [""], + message: timeline.getLogsAsText(), + attachments: [ + AttachmentData( + data: timeline.getLogsAsText().data(using: .utf8)!, + mimeType: "text/plain", + fileName: "log.txt" + ) + ] + ); + showMailView.toggle(); + } + .disabled(!MailView.canSendMail) + .sheet(isPresented: $showMailView) { + MailView(data: $mailData) { result in + print(result) + } + } + + } + Divider() Text("Logs") .font(.title) ForEach(timeline.logs) { log in @@ -25,9 +63,6 @@ struct TimelineView: View { } struct TimelineView_Previews: PreviewProvider { - - @StateObject var timeline: Timeline = Timeline(); - static var previews: some View { TimelineView(timeline: Timeline()) }