feat(notes): add notes feature
This commit is contained in:
385
WorterBuch/NotesListView.swift
Normal file
385
WorterBuch/NotesListView.swift
Normal file
@@ -0,0 +1,385 @@
|
||||
//
|
||||
// NotesListView.swift
|
||||
// WorterBuch
|
||||
//
|
||||
// Created by Oliver Hnát on 06.12.2025.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import PencilKit
|
||||
import CoreData
|
||||
|
||||
struct NotesListView: View {
|
||||
@Environment(\.managedObjectContext) private var viewContext
|
||||
@FetchRequest(
|
||||
sortDescriptors: [NSSortDescriptor(keyPath: \Note.timestamp, ascending: false)],
|
||||
animation: .default
|
||||
)
|
||||
private var notes: FetchedResults<Note>
|
||||
|
||||
@State private var selectedNote: Note?
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
// Left sidebar - list of notes
|
||||
List(selection: $selectedNote) {
|
||||
ForEach(notes, id: \.id) { note in
|
||||
NoteRowView(note: note)
|
||||
.tag(note)
|
||||
.onTapGesture {
|
||||
// Save current note before switching
|
||||
saveContext()
|
||||
// Small delay to ensure save completes
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
|
||||
selectedNote = note
|
||||
}
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteNotes)
|
||||
}
|
||||
.listStyle(.sidebar)
|
||||
.navigationTitle("Notes")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: addNote) {
|
||||
Image(systemName: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Right panel - note editor or placeholder
|
||||
if let note = selectedNote {
|
||||
NoteEditorContentView(note: note, onAddNote: addNote)
|
||||
.id(note.id) // Force recreation when switching notes
|
||||
.navigationBarHidden(true)
|
||||
} else {
|
||||
VStack(spacing: 20) {
|
||||
HStack {
|
||||
Spacer()
|
||||
Button(action: addNote) {
|
||||
Image(systemName: "plus")
|
||||
.font(.title2)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Image(systemName: "note.text")
|
||||
.font(.system(size: 60))
|
||||
.foregroundColor(.secondary)
|
||||
Text("Select a note or create a new one")
|
||||
.font(.title3)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(Color(.systemGroupedBackground))
|
||||
.navigationBarHidden(true)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Select first note if none selected
|
||||
if selectedNote == nil && !notes.isEmpty {
|
||||
selectedNote = notes.first
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func addNote() {
|
||||
// Save current note first
|
||||
saveContext()
|
||||
|
||||
// Create new note on main thread
|
||||
DispatchQueue.main.async {
|
||||
withAnimation {
|
||||
let newNote = Note.create(in: viewContext)
|
||||
do {
|
||||
try viewContext.save()
|
||||
selectedNote = newNote
|
||||
} catch {
|
||||
print("Error creating note: \(error)")
|
||||
viewContext.rollback()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteNotes(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
let notesToDelete = offsets.map { notes[$0] }
|
||||
|
||||
// Clear selection if we're deleting the selected note
|
||||
if let selected = selectedNote, notesToDelete.contains(where: { $0.id == selected.id }) {
|
||||
selectedNote = nil
|
||||
}
|
||||
|
||||
// Delete the notes
|
||||
notesToDelete.forEach { note in
|
||||
viewContext.delete(note)
|
||||
}
|
||||
|
||||
saveContext()
|
||||
|
||||
// After deletion, if no notes are left, ensure selection is nil
|
||||
// Otherwise, select the first remaining note if nothing is selected
|
||||
DispatchQueue.main.async {
|
||||
if notes.isEmpty {
|
||||
selectedNote = nil
|
||||
} else if selectedNote == nil && !notes.isEmpty {
|
||||
selectedNote = notes.first
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func saveContext() {
|
||||
do {
|
||||
try viewContext.save()
|
||||
} catch {
|
||||
let nsError = error as NSError
|
||||
print("Error saving context: \(nsError), \(nsError.userInfo)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NoteRowView: View {
|
||||
@ObservedObject var note: Note
|
||||
|
||||
private var timeAgo: String {
|
||||
guard let timestamp = note.timestamp else { return "" }
|
||||
let formatter = RelativeDateTimeFormatter()
|
||||
formatter.unitsStyle = .full
|
||||
return formatter.localizedString(for: timestamp, relativeTo: Date())
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(note.title)
|
||||
.font(.body)
|
||||
.lineLimit(2)
|
||||
|
||||
if note.timestamp != nil {
|
||||
Text(timeAgo)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
}
|
||||
|
||||
struct NoteEditorContentView: View {
|
||||
@ObservedObject var note: Note
|
||||
@Environment(\.managedObjectContext) private var viewContext
|
||||
let onAddNote: () -> Void
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
// Title/header area
|
||||
HStack {
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(note.title)
|
||||
.font(.headline)
|
||||
.fontWeight(.semibold)
|
||||
|
||||
if let timestamp = note.timestamp {
|
||||
Text(timestamp, style: .date)
|
||||
.font(.caption2)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Button(action: onAddNote) {
|
||||
Image(systemName: "plus")
|
||||
.font(.body)
|
||||
}
|
||||
.padding(.trailing, 8)
|
||||
|
||||
Button(action: {
|
||||
note.pkDrawing = PKDrawing()
|
||||
note.text = ""
|
||||
saveContext()
|
||||
}) {
|
||||
Label("Clear", systemImage: "trash")
|
||||
.foregroundColor(.red)
|
||||
.font(.caption)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 8)
|
||||
.background(Color(.systemBackground))
|
||||
|
||||
Divider()
|
||||
|
||||
// Note editor content
|
||||
NoteEditorContentOnly(
|
||||
drawing: bindingForDrawing(),
|
||||
text: bindingForText()
|
||||
)
|
||||
}
|
||||
.background(Color(.systemGroupedBackground))
|
||||
}
|
||||
|
||||
private func bindingForDrawing() -> Binding<PKDrawing> {
|
||||
Binding(
|
||||
get: { note.pkDrawing },
|
||||
set: { newValue in
|
||||
note.pkDrawing = newValue
|
||||
saveContext()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private func bindingForText() -> Binding<String> {
|
||||
Binding(
|
||||
get: { note.text ?? "" },
|
||||
set: { newValue in
|
||||
note.text = newValue
|
||||
saveContext()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private func saveContext() {
|
||||
do {
|
||||
try viewContext.save()
|
||||
} catch {
|
||||
let nsError = error as NSError
|
||||
print("Error saving context: \(nsError), \(nsError.userInfo)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NoteEditorContentOnly: View {
|
||||
@Binding var drawing: PKDrawing
|
||||
@Binding var text: String
|
||||
|
||||
@State private var isRecognizing = false
|
||||
@State private var showTranscription = false
|
||||
@State private var viewAppeared = false
|
||||
@State private var recognitionWorkItem: DispatchWorkItem?
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
// Handwriting canvas - scrollable
|
||||
if viewAppeared {
|
||||
ScrollableCanvasView(
|
||||
drawing: $drawing,
|
||||
onDrawingChanged: { newDrawing in
|
||||
recognizeHandwriting(newDrawing)
|
||||
},
|
||||
isEditable: true
|
||||
)
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(maxHeight: .infinity)
|
||||
.background(Color(.systemGray6))
|
||||
.cornerRadius(12)
|
||||
.padding()
|
||||
} else {
|
||||
// Placeholder while loading
|
||||
Rectangle()
|
||||
.fill(Color(.systemGray6))
|
||||
.frame(maxHeight: .infinity)
|
||||
.cornerRadius(12)
|
||||
.padding()
|
||||
.onAppear {
|
||||
// Show canvas after a brief delay to ensure window is ready
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
|
||||
viewAppeared = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Transcribed text section - collapsible
|
||||
if showTranscription {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack {
|
||||
Text("Transcribed Text")
|
||||
.font(.headline)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
if isRecognizing {
|
||||
ProgressView()
|
||||
.scaleEffect(0.8)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Button(action: {
|
||||
withAnimation {
|
||||
showTranscription = false
|
||||
}
|
||||
}) {
|
||||
Image(systemName: "chevron.down")
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
TextEditor(text: $text)
|
||||
.font(.body)
|
||||
.frame(minHeight: 150)
|
||||
.padding(8)
|
||||
.background(Color(.systemGray6))
|
||||
.cornerRadius(8)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.stroke(Color.gray.opacity(0.3), lineWidth: 1)
|
||||
)
|
||||
}
|
||||
.padding()
|
||||
.transition(.move(edge: .bottom).combined(with: .opacity))
|
||||
}
|
||||
|
||||
// Toggle transcription button
|
||||
if !showTranscription {
|
||||
Button(action: {
|
||||
withAnimation {
|
||||
showTranscription = true
|
||||
}
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "text.bubble")
|
||||
Text("Show Transcription")
|
||||
}
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.blue)
|
||||
.padding(.vertical, 8)
|
||||
}
|
||||
.padding(.bottom)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func recognizeHandwriting(_ drawing: PKDrawing) {
|
||||
guard !drawing.bounds.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
// Cancel any pending recognition
|
||||
recognitionWorkItem?.cancel()
|
||||
|
||||
// Create new work item with debounce
|
||||
let workItem = DispatchWorkItem { [drawing] in
|
||||
Task { @MainActor in
|
||||
isRecognizing = true
|
||||
|
||||
if let recognizedText = await HandwritingRecognizer.recognizeTextAsync(from: drawing) {
|
||||
text = recognizedText
|
||||
isRecognizing = false
|
||||
} else {
|
||||
isRecognizing = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
recognitionWorkItem = workItem
|
||||
|
||||
// Execute after 800ms delay (adjust as needed)
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: workItem)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user