mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-30 14:12:31 +02:00
Add projects
This commit is contained in:
15
Examples/UINote/SwiftUINote/Models/Note.swift
Executable file
15
Examples/UINote/SwiftUINote/Models/Note.swift
Executable file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Note.swift
|
||||
// SwiftUINote
|
||||
//
|
||||
// Created by chanju Jeon on 05/06/2019.
|
||||
// Copyright © 2019 we'd. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct Note: Hashable, Codable, Identifiable {
|
||||
let id = UUID()
|
||||
var text: String
|
||||
var date = Date()
|
||||
}
|
||||
38
Examples/UINote/SwiftUINote/Models/NoteData.swift
Executable file
38
Examples/UINote/SwiftUINote/Models/NoteData.swift
Executable file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// NoteData.swift
|
||||
// SwiftUINote
|
||||
//
|
||||
// Created by chanju Jeon on 05/06/2019.
|
||||
// Copyright © 2019 we'd. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
class NoteData {
|
||||
|
||||
static let shared = NoteData()
|
||||
|
||||
var notes: [Note] = [
|
||||
Note(text: "New Note"),
|
||||
Note(text: "Another Note")
|
||||
]
|
||||
|
||||
private init() { load() }
|
||||
|
||||
static func dateToString(date: Date) -> String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .medium
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
func save() {
|
||||
UserDefaults.standard.set(try? PropertyListEncoder().encode(notes), forKey: "notes")
|
||||
debugPrint("save called")
|
||||
}
|
||||
|
||||
func load() {
|
||||
if let data = UserDefaults.standard.object(forKey: "notes") as? Data {
|
||||
self.notes = try! PropertyListDecoder().decode([Note].self, from: data)
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Examples/UINote/SwiftUINote/Models/UserData.swift
Executable file
21
Examples/UINote/SwiftUINote/Models/UserData.swift
Executable file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// UserData.swift
|
||||
// SwiftUINote
|
||||
//
|
||||
// Created by chanju Jeon on 05/06/2019.
|
||||
// Copyright © 2019 we'd. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
final class UserData: BindableObject {
|
||||
let didChange = PassthroughSubject<UserData, Never>()
|
||||
|
||||
var notes = NoteData.shared.notes {
|
||||
didSet {
|
||||
didChange.send(self)
|
||||
NoteData.shared.notes = notes
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user