Files

25 lines
749 B
C#

using System.Collections.ObjectModel;
using System.Text.Json;
using NoteApp.Model;
namespace NoteApp.Service;
public class FileService {
private readonly string _fileName;
public FileService(){
_fileName = Path.Combine(FileSystem.AppDataDirectory, "notes.json");
}
public async Task<ObservableCollection<Note>> LoadNotes(){
if(!File.Exists(_fileName))
return [];
var json = await File.ReadAllTextAsync(_fileName);
return JsonSerializer.Deserialize<ObservableCollection<Note>>(json) ?? [];
}
public async Task SaveNotes(ObservableCollection<Note> notes){
var json = JsonSerializer.Serialize(notes);
await File.WriteAllTextAsync(_fileName, json);
}
}