aboutsummaryrefslogtreecommitdiff
path: root/LibreEdit
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2024-01-13 20:43:58 -0600
committerChristian Cleberg <hello@cleberg.net>2024-01-13 20:43:58 -0600
commit9e2c7e09543cf5288303e59125e897f8731b8a1a (patch)
treefbca7d8ece927ff7ca4ec901f47cb2a9aaddd446 /LibreEdit
parent541737c71ec220d276f0be7bf1092fb879f1b231 (diff)
downloadlibre-edit-9e2c7e09543cf5288303e59125e897f8731b8a1a.tar.gz
libre-edit-9e2c7e09543cf5288303e59125e897f8731b8a1a.tar.bz2
libre-edit-9e2c7e09543cf5288303e59125e897f8731b8a1a.zip
initial commit
Diffstat (limited to 'LibreEdit')
-rw-r--r--LibreEdit/ContentView.swift111
1 files changed, 105 insertions, 6 deletions
diff --git a/LibreEdit/ContentView.swift b/LibreEdit/ContentView.swift
index 3d5fcf6..87def10 100644
--- a/LibreEdit/ContentView.swift
+++ b/LibreEdit/ContentView.swift
@@ -7,15 +7,114 @@
import SwiftUI
+func getDatetime() -> String {
+ let dateFormatter = DateFormatter()
+ let enUSPosixLocale = Locale(identifier: "en_US_POSIX")
+ dateFormatter.locale = enUSPosixLocale
+ dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
+ dateFormatter.calendar = Calendar(identifier: .gregorian)
+
+ let iso8601String = dateFormatter.string(from: Date())
+
+ return iso8601String
+}
+
+// Ensure that `Supports Document Browser` = YES in the `ios-edit.xcodeproj` file,
+// so that users can browse saved files in the Files app
+func getDocumentsDirectory() -> URL {
+ let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
+ let documentsDirectory = paths[0]
+ return documentsDirectory
+}
+
struct ContentView: View {
+ enum FocusedField {
+ case content
+ }
+
+ @State private var content = ""
+ @State private var wordCount: Int = 0
+ @State private var showingPopover = false
+ @FocusState private var focusedField: FocusedField?
+
var body: some View {
- VStack {
- Image(systemName: "globe")
- .imageScale(.large)
- .foregroundStyle(.tint)
- Text("Hello, world!")
+ NavigationView {
+ VStack(alignment: .leading) {
+ Text("\(wordCount) words")
+ .font(.headline)
+ .foregroundColor(.secondary)
+ .padding(.trailing)
+ TextEditor(text: $content)
+ .onChange(of: content) {
+ let words = content.split { $0 == " " || $0.isNewline }
+ self.wordCount = words.count
+ }
+ .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
+ .focused($focusedField, equals: .content)
+ }
+ .onAppear {
+ focusedField = .content
+ }
+ .padding()
+ .navigationTitle("Libre Edit")
+ .toolbar {
+ ToolbarItemGroup(placement: .primaryAction) {
+ Button(action: {
+ do {
+ let data = Data(self.content.utf8)
+ let url = URL.documentsDirectory.appending(path: (getDatetime() + ".txt"))
+ try data.write(to: url, options: [.atomic, .completeFileProtection])
+
+ // TODO: Alerts currently aren't working/aren't visible
+ Text("File saved successfully!")
+ .foregroundColor(.white)
+ .bold()
+ .font(.footnote)
+ .frame(width: 140, height: 30)
+ .background(Color.indigo.cornerRadius(7))
+ print("Saved content to file: \(url)")
+ } catch {
+ // TODO: Alerts currently aren't working/aren't visible
+ Text("Failed to save file.")
+ .foregroundColor(.white)
+ .bold()
+ .font(.footnote)
+ .frame(width: 140, height: 30)
+ .background(Color.red.cornerRadius(7))
+ print("Error saving content to file: \(error)")
+ }
+ }) {
+ Text("Save")
+ }
+ // TODO: Confirm (possibly save) & then clear TextEditor
+ Button(action: {
+ print("TODO: This will create a new note.")
+ }) {
+ Text("New")
+ }
+ }
+ ToolbarItemGroup(placement: .secondaryAction) {
+ Button {
+ showingPopover = true
+ } label: {
+ Text("App Info")
+ }.popover(isPresented: $showingPopover) {
+ VStack(alignment: .leading) {
+ Text("App Info")
+ .font(.largeTitle)
+ Text("")
+ Text("Libre Edit is a free and open source text editor for iOS built by [Christian Cleberg](https://cleberg.net).")
+ Text("")
+ Text("Visit the [GitHub Repository](https://github.com/ccleberg/ios-edit) to view the source code.")
+ Text("")
+ Text("This project was developed under the [GNU GPL v3 license](https://github.com/ccleberg/ios-edit/LICENSE).")
+ Spacer()
+ }
+ .padding()
+ }
+ }
+ }
}
- .padding()
}
}