notes app

This commit is contained in:
2024-04-12 14:15:22 +09:00
parent 9039c49ad3
commit 7ef194a4cb
13 changed files with 231 additions and 40 deletions

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="NotesNet8.Views.AboutPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:NotesNet8.Models"
Title="AboutPage">
<ContentPage.BindingContext>
<models:About />
</ContentPage.BindingContext>
<VerticalStackLayout Margin="10" Spacing="10">
<HorizontalStackLayout Spacing="10">
<Image HeightRequest="64" SemanticProperties.Description="The dot net bot waving hello!" Source="dotnet_bot.png" />
<Label FontAttributes="Bold" FontSize="22"
Text="{Binding Title}"
VerticalOptions="End" />
<Label FontSize="22"
Text="{Binding Version}"
VerticalOptions="End" />
</HorizontalStackLayout>
<Label Text="{Binding Message}" />
<Button Clicked="LearnMore_Clicked" Text="Learn more..." />
</VerticalStackLayout>
</ContentPage>

View File

@@ -0,0 +1,17 @@
namespace NotesNet8.Views;
public partial class AboutPage : ContentPage
{
public AboutPage()
{
InitializeComponent();
}
private async void LearnMore_Clicked(object sender, EventArgs e)
{
if (BindingContext is Models.About about)
{
await Launcher.Default.OpenAsync(about.MoreInfoUrl);
}
}
}

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="NotesNet8.Views.AllNotesPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:NotesNet8.Models"
Title="AllNotesPage">
<ContentPage.ToolbarItems>
<ToolbarItem Clicked="Add_Clicked"
IconImageSource="{FontImage Glyph='+',
Color=Black,
Size=22}"
Text="Add" />
</ContentPage.ToolbarItems>
<ContentPage.BindingContext>
<models:AllNotes />
</ContentPage.BindingContext>
<CollectionView x:Name="notesCollection"
Margin="20"
ItemsSource="{Binding Notes}"
SelectionChanged="notesCollection_SelectionChanged" SelectionMode="Single">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="10" Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label FontSize="22" Text="{Binding Text}" />
<Label FontSize="14"
Text="{Binding Date}"
TextColor="Silver" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>

View File

@@ -0,0 +1,34 @@
namespace NotesNet8.Views;
public partial class AllNotesPage : ContentPage
{
public AllNotesPage()
{
InitializeComponent();
BindingContext = new Models.AllNotes();
}
protected override void OnAppearing()
{
base.OnAppearing();
((Models.AllNotes)BindingContext).LoadNotes();
}
private async void Add_Clicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync(nameof(NotePage));
}
private async void notesCollection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.Count == 0)
return;
var note = (Models.Note)e.CurrentSelection[0];
await Shell.Current.GoToAsync($"{nameof(NotePage)}?{nameof(NotePage.ItemId)}={note.Filename}");
notesCollection.SelectedItem = null;
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="NotesNet8.Views.NotePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:NotesNet8.Models"
Title="NotePage">
<ContentPage.BindingContext>
<models:Note />
</ContentPage.BindingContext>
<VerticalStackLayout Margin="5" Spacing="10">
<Editor x:Name="TextEditor"
HeightRequest="100" Placeholder="Enter your note"
Text="{Binding Text}"/>
<Grid ColumnDefinitions="*,*" ColumnSpacing="4">
<Button Grid.Column="0"
Clicked="SaveButton_Clicked" Text="Save" />
<Button Grid.Column="1"
Clicked="DeleteButton_Clicked" Text="Delete" />
</Grid>
</VerticalStackLayout>
</ContentPage>

View File

@@ -0,0 +1,50 @@
namespace NotesNet8.Views;
[QueryProperty(nameof(ItemId), nameof(ItemId))]
public partial class NotePage : ContentPage
{
public string ItemId { set { LoadNote(value); } }
public NotePage()
{
InitializeComponent();
string appDataPath = FileSystem.AppDataDirectory;
string randomFileName = $"{Path.GetRandomFileName()}.notes.txt";
LoadNote(Path.Combine(appDataPath, randomFileName));
}
private void LoadNote(string fileName)
{
Models.Note noteModel = new Models.Note();
noteModel.Filename = fileName;
if (File.Exists(fileName))
{
noteModel.Date = File.GetCreationTime(fileName);
noteModel.Text = File.ReadAllText(fileName);
}
BindingContext = noteModel;
}
private async void SaveButton_Clicked(object sender, EventArgs e)
{
if (BindingContext is Models.Note note)
File.WriteAllText(note.Filename, TextEditor.Text);
await Shell.Current.GoToAsync("..");
}
private async void DeleteButton_Clicked(object sender, EventArgs e)
{
if (BindingContext is Models.Note note)
{
if (File.Exists(note.Filename))
File.Delete(note.Filename);
}
await Shell.Current.GoToAsync("..");
}
}