Feature: Notes creation

This commit is contained in:
2025-09-25 19:36:21 +02:00
parent 7b84754bd6
commit 209e08754b
5 changed files with 123 additions and 14 deletions
+15
View File
@@ -0,0 +1,15 @@
//
// Note.swift
// Peered
//
// Created by Oskar Chybowski on 25/09/2025.
//
import Foundation
struct Note: Identifiable {
var id: URL { path }
let name: String
let path: URL
}
+51
View File
@@ -0,0 +1,51 @@
//
// NotesStorage.swift
// Peered
//
// Created by Oskar Chybowski on 25/09/2025.
//
import Foundation
struct NotesStorage {
let storageProvider: FileManager = FileManager.default
func loadNotes() -> [Note] {
let files = try! storageProvider.contentsOfDirectory(atPath: URL.documentsDirectory.path)
var notes = [Note]()
for file in files.compactMap(URL.init) {
let name = file.lastPathComponent
let note = Note(
name: name,
path: file
)
notes.append(note)
}
return notes
}
func createNote(name: String) {
let currentNotes = loadNotes()
var proposedName = name
var index: Int? = nil
while currentNotes.contains(where: { $0.name == proposedName }) {
if let _index = index {
index = _index + 1
} else {
index = 1
}
}
proposedName = if let index {
"\(proposedName) \(index)"
} else {
proposedName
}
let pathToWrite = URL.documentsDirectory.appendingPathComponent(proposedName).appendingPathExtension(for: .text)
try! String().write(to: pathToWrite, atomically: true, encoding: .utf8)
}
}