141 lines
3.3 KiB
C#
141 lines
3.3 KiB
C#
using System;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Notes.Models;
|
|
|
|
namespace Notes.ViewModels;
|
|
|
|
public class NoteViewModelDesignData : NoteViewModel
|
|
{
|
|
private static IServices services = new Services();
|
|
|
|
public NoteViewModelDesignData(IServices services, AllNotesViewModel allNotesViewModel, Note note) : base(services, allNotesViewModel, note)
|
|
{
|
|
}
|
|
|
|
public NoteViewModelDesignData() : this(services, new AllNotesViewModel(services), new Note())
|
|
{
|
|
Note = new Note()
|
|
{
|
|
Text = "11111",
|
|
Date = DateTime.Now,
|
|
Filename = "dddddd.cccc"
|
|
};
|
|
Text = "11111";
|
|
Title = "dddddd.cccc";
|
|
}
|
|
}
|
|
|
|
public partial class NoteViewModel : ViewModelBase
|
|
{
|
|
private readonly IServices _services;
|
|
|
|
[ObservableProperty] private AllNotesViewModel _allNotesViewModel;
|
|
|
|
[ObservableProperty] private Note _note;
|
|
|
|
[ObservableProperty] private bool _isEditingTitle;
|
|
[ObservableProperty] private bool _isEditingText;
|
|
[ObservableProperty] private string _editButtonText = "编辑";
|
|
[ObservableProperty] private string _title;
|
|
[ObservableProperty] private string _text;
|
|
|
|
|
|
[ObservableProperty] private Note _selectedNote;
|
|
|
|
|
|
public NoteViewModel(IServices services, AllNotesViewModel allNotesViewModel, Note note)
|
|
{
|
|
this._note = note;
|
|
this._allNotesViewModel = allNotesViewModel;
|
|
this._services = services;
|
|
this._title = note.ShortName;
|
|
this._text = note.Text;
|
|
}
|
|
|
|
[Obsolete("Used by designer", true)]
|
|
#pragma warning disable CS8618
|
|
public NoteViewModel()
|
|
#pragma warning restore CS8618
|
|
{
|
|
}
|
|
|
|
partial void OnSelectedNoteChanged(Note? oldValue, Note newValue)
|
|
{
|
|
ReplaceNote(newValue);
|
|
}
|
|
|
|
partial void OnNoteChanged(Note? oldValue, Note note)
|
|
{
|
|
this.Title = note.ShortName;
|
|
this.Text = note.Text;
|
|
}
|
|
|
|
[RelayCommand()]
|
|
public void SaveNote()
|
|
{
|
|
SaveNote(Title, Text);
|
|
}
|
|
|
|
public void SaveNote(string iptTitleText, string iptContentText)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(iptTitleText))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var newName = $"{iptTitleText}{Models.Note.Extension}";
|
|
Models.Note note = Note;
|
|
note.Filename = newName;
|
|
note.Text = iptContentText ?? "";
|
|
note.Date = TimeProvider.System.GetLocalNow().DateTime;
|
|
note.Save();
|
|
Note = note;
|
|
AllNotesViewModel.AddNote(this);
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void Back()
|
|
{
|
|
_services.GetSafeAs<MainViewModel>().Navitation<AllNotesViewModel>();
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OpenNote()
|
|
{
|
|
this._services.GetSafeAs<MainViewModel>().OpenNote(Note);
|
|
}
|
|
|
|
|
|
[RelayCommand]
|
|
public void ChangeTitle()
|
|
{
|
|
IsEditingTitle ^= true;
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void ChangeEditTextState()
|
|
{
|
|
IsEditingText ^= true;
|
|
if (IsEditingText)
|
|
{
|
|
EditButtonText = "完成";
|
|
}
|
|
else
|
|
{
|
|
EditButtonText = "编辑";
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void NewNote()
|
|
{
|
|
AllNotesViewModel.NewNote();
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void ReplaceNote(Note note)
|
|
{
|
|
Note = note;
|
|
}
|
|
} |