-
-
Save felipe/ad51ed2debfa1c33786834e1c92420e7 to your computer and use it in GitHub Desktop.
A notes app written in >100 lines of swift using SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Listomania | |
// | |
// Created by Jack Newcombe on 05/06/2019. | |
// Copyright © 2019 Jack Newcombe. All rights reserved. | |
// | |
import SwiftUI | |
let dateFormatter = DateFormatter() | |
struct NoteItem: Codable, Hashable, Identifiable, Comparable { | |
let id: Int | |
let text: String | |
private let date = Date() | |
var dateText: String { | |
dateFormatter.dateFormat = "MMM d yyyy, h:mm a" | |
return dateFormatter.string(from: date) | |
} | |
static func <(lhs: NoteItem, rhs: NoteItem) -> Bool { return lhs.id < rhs.id } | |
} | |
struct ContentView : View { | |
@State var items: [NoteItem] = { | |
guard let data = UserDefaults.standard.data(forKey: "notes") else { return [] } | |
if let json = try? JSONDecoder().decode([NoteItem].self, from: data) { | |
return json | |
} | |
return [] | |
}() | |
@State var taskText: String = "" | |
@State var showAlert = false | |
@State var itemToDelete: NoteItem? | |
var inputView: some View { | |
HStack { | |
TextField("Enter text...", text: $taskText) | |
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)) | |
.clipped() | |
Button(action: didTapAddTask, label: { Text("Add") }).padding(8) | |
} | |
} | |
var body: some View { | |
VStack { | |
inputView | |
Divider() | |
List(items) { item in | |
Button(action:{}) { | |
VStack(alignment: .leading) { | |
Text(item.dateText).font(.headline) | |
Text(item.text).lineLimit(nil).multilineTextAlignment(.leading) | |
} | |
} | |
.onLongPressGesture{ | |
self.itemToDelete = item | |
self.showAlert = true | |
} | |
.alert(isPresented: self.$showAlert) | |
{ | |
Alert(title: Text("Hey!"), | |
message: Text("Are you sure you want to delete this item?"), | |
primaryButton: .destructive(Text("Delete"), action: self.deleteNote), | |
secondaryButton: .cancel()) | |
} | |
} | |
} | |
} | |
func didTapAddTask() { | |
var id: Int = 0 | |
let lastItem = items.max() | |
if lastItem != nil { id = lastItem!.id + 1 } | |
items.insert(NoteItem(id: id, text: taskText), at: 0) | |
taskText = "" | |
save() | |
} | |
func deleteNote() { | |
guard let itemToDelete = itemToDelete else { return } | |
items = items.filter { $0 != itemToDelete } | |
save() | |
} | |
func save() { | |
guard let data = try? JSONEncoder().encode(items) else { return } | |
UserDefaults.standard.set(data, forKey: "notes") | |
} | |
} | |
#if DEBUG | |
struct ContentView_Previews : PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment