Changing. Deleting

This commit is contained in:
Mootfrost777 2022-07-01 13:10:10 +03:00
parent 5ced7ad535
commit 3262e555f2
22 changed files with 223 additions and 53 deletions

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,55 @@
using MauiApp1.Model;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
namespace MauiApp1.Helpers
{
public class ChangeTransactionFile
{
static string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
public static ObservableCollection<Transaction> LoadData(ObservableCollection<Transaction> transactions)
{
ObservableCollection<Transaction> data;
string fileName = "Transactions.json";
string path = Path.Combine(folder, fileName);
if (!File.Exists(path))
{
File.Create(path).Close();
data = new ObservableCollection<Transaction>();
}
else
{
string json = "";
using (StreamReader sr = new StreamReader(path))
{
json = sr.ReadToEnd();
}
data = JsonConvert.DeserializeObject<ObservableCollection<Transaction>>(json);
if (data == null)
{
foreach (Transaction transaction in data)
{
transactions.Add(transaction);
}
}
}
return data;
}
public static void SaveData(ObservableCollection<Transaction> transactions)
{
string fileName = "Transactions.json";
string path = Path.Combine(folder, fileName);
string json = JsonConvert.SerializeObject(transactions);
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(json);
}
}
}
}

View file

@ -57,6 +57,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup>
<MauiXaml Update="View\AddTransactionView.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="View\CategoryView.xaml"> <MauiXaml Update="View\CategoryView.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</MauiXaml> </MauiXaml>

View file

@ -1,32 +1,65 @@
using System; using System.ComponentModel;
using System.Collections.Generic; using Newtonsoft.Json;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp1.Model namespace MauiApp1.Model
{ {
public class Transaction public class Transaction : INotifyPropertyChanged
{ {
private string category; private string category;
private float price; private float price;
private DateTime date;
[JsonConstructor]
public Transaction(string category, float price) public Transaction(string category, float price)
{ {
this.category = category; this.category = category;
this.price = price; this.price = price;
date = DateTime.Now;
}
[JsonProperty]
public DateTime Date
{
get { return date; }
set { date = value; }
} }
[JsonProperty]
public string Category public string Category
{ {
get { return category; } get { return category; }
set { category = value; } set
{
if (value != category)
{
category = value;
OnPropertyChanged(nameof(Category));
}
}
} }
[JsonProperty]
public float Price public float Price
{ {
get { return price; } get { return price; }
set { price = value; } set
{
if (value != price)
{
price = value;
OnPropertyChanged(nameof(Price));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
if (property != null)
{
PropertyChanged(this, new(property));
}
} }
} }
} }

View file

@ -7,13 +7,16 @@
<Label Text="Категория" <Label Text="Категория"
FontSize="Medium"/> FontSize="Medium"/>
<Entry Placeholder="Введите данные" <Entry Placeholder="Введите данные"
Margin="0, 10"/> Margin="0, 10"
Text="{Binding CategoryEntry}"/>
<Label Text="Стоимость" <Label Text="Стоимость"
FontSize="Medium"/> FontSize="Medium"/>
<Entry Placeholder="Введите данные" <Entry Placeholder="Введите данные"
Margin="0, 10"/> Margin="0, 10"
Text="{Binding PriceEntry}"/>
<Button Text="Добавить"/> <Button Text="Добавить"
Command="{Binding AddTransactionCommand}"/>
</VerticalStackLayout> </VerticalStackLayout>
</ContentPage> </ContentPage>

View file

@ -1,9 +1,22 @@
using MauiApp1.ViewModel;
using System.Collections.ObjectModel;
using MauiApp1.Model;
namespace MauiApp1.View; namespace MauiApp1.View;
public partial class AddTransactionView : ContentPage public partial class AddTransactionView : ContentPage
{ {
public AddTransactionView() public AddTransactionView(ObservableCollection<Transaction> transactions)
{ {
InitializeComponent(); InitializeComponent();
BindingContext = new AddTransactionViewModel(transactions);
} }
public AddTransactionView(ObservableCollection<Transaction> transactions, Transaction transaction)
{
InitializeComponent();
BindingContext = new AddTransactionViewModel(transactions, transaction);
}
} }

View file

@ -2,19 +2,19 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.View.TransactionView" x:Class="MauiApp1.View.TransactionView"
Title="Transactions"> Title="Transactions"
x:Name="TransactionPage">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition/> <RowDefinition/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Button x:Name="btnAddTransaction" <Button Text="Добавить"
Text="Add Transaction"
HorizontalOptions="Center" HorizontalOptions="Center"
VerticalOptions="Center" VerticalOptions="Center"
Margin="20, 10" Margin="20, 10"
Command="{Binding AddTransactionCommand}"/> Command="{Binding AddCommand}"/>
<CollectionView ItemsSource="{Binding Transactions}" Grid.Row="1" > <CollectionView ItemsSource="{Binding Transactions}" Grid.Row="1" >
<CollectionView.ItemTemplate> <CollectionView.ItemTemplate>
@ -22,27 +22,37 @@
<SwipeView> <SwipeView>
<SwipeView.RightItems> <SwipeView.RightItems>
<SwipeItems> <SwipeItems>
<SwipeItemView> <SwipeItemView Command="{Binding Path=BindingContext.EditCommand,
Source={x:Reference Name=TransactionPage}}" CommandParameter="{Binding .}">
<Frame BackgroundColor="LightGreen" <Frame BackgroundColor="LightGreen"
Margin="5"> Margin="5">
<Label Text="Изменить"/> <Label Text="✏️"
FontSize="Large"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"/>
</Frame> </Frame>
</SwipeItemView> </SwipeItemView>
<SwipeItemView> <SwipeItemView Command="{Binding Path=BindingContext.DeleteCommand,
<Frame BackgroundColor="LightCoral" Source={x:Reference Name=TransactionPage}}" CommandParameter="{Binding .}">
Margin="5"> <Frame BackgroundColor="LightCoral" Margin="5">
<Label Text="Удалить"/> <Label Text="🗑️"
FontSize="Large"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"/>
</Frame> </Frame>
</SwipeItemView> </SwipeItemView>
</SwipeItems> </SwipeItems>
</SwipeView.RightItems> </SwipeView.RightItems>
<Frame CornerRadius="20" Margin="10" BackgroundColor="{AppThemeBinding Light={StaticResource FrameLight}, Dark={StaticResource FrameDark}}"> <Frame CornerRadius="20" Margin="10" BackgroundColor="{AppThemeBinding Light={StaticResource FrameLight}, Dark={StaticResource FrameDark}}">
<StackLayout Orientation="Horizontal"> <StackLayout Orientation="Vertical">
<Label Text="{Binding Category}" FontSize="Medium" HorizontalOptions="StartAndExpand"/> <StackLayout Orientation="Horizontal">
<Label Text="{Binding Price}" FontSize="Medium" /> <Label Text="{Binding Category}" FontSize="Medium" HorizontalOptions="StartAndExpand"/>
</StackLayout> <Label Text="{Binding Price}" FontSize="Medium"/>
</StackLayout>
<Label Text="{Binding Date}" FontSize="Micro"/>
</StackLayout>
</Frame> </Frame>
</SwipeView> </SwipeView>
</DataTemplate> </DataTemplate>

View file

@ -7,6 +7,6 @@ public partial class TransactionView : ContentPage
{ {
InitializeComponent(); InitializeComponent();
BindingContext = new TransactionViewModel(); BindingContext = new TransactionViewModel(Navigation);
} }
} }

View file

@ -1,14 +1,47 @@
using System; using System.Collections.ObjectModel;
using System.Collections.Generic; using System.Windows.Input;
using System.Linq; using MauiApp1.Model;
using System.Text; using MauiApp1.Helpers;
using System.Threading.Tasks;
namespace MauiApp1.ViewModel namespace MauiApp1.ViewModel
{ {
public class AddTransactionViewModel public class AddTransactionViewModel
{ {
public string CategoryEntry { get; set; } public string CategoryEntry { get; set; }
public float PriceEntery { get; set; } public float PriceEntry { get; set; }
public ICommand AddTransactionCommand { get; set; }
private ObservableCollection<Transaction> transactions;
private Transaction transaction;
public AddTransactionViewModel(ObservableCollection<Transaction> transactions)
{
this.transactions = transactions;
AddTransactionCommand = new Command(AddTransaction);
}
public AddTransactionViewModel(ObservableCollection<Transaction> transactions, Transaction transaction)
{
this.transactions = transactions;
this.transaction = transaction;
AddTransactionCommand = new Command(EditTransaction);
PriceEntry = transaction.Price;
CategoryEntry = transaction.Category;
}
public void AddTransaction()
{
transactions.Insert(0, new Transaction(CategoryEntry, PriceEntry));
ChangeTransactionFile.SaveData(transactions);
}
public void EditTransaction()
{
transaction.Price = PriceEntry;
transaction.Category = CategoryEntry;
ChangeTransactionFile.SaveData(transactions);
}
} }
} }

View file

@ -1,31 +1,47 @@
using System; using System.Collections.ObjectModel;
using System.Collections.Generic; using System.Windows.Input;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using MauiApp1.Model; using MauiApp1.Model;
using MauiApp1.View;
using MauiApp1.Helpers;
namespace MauiApp1.ViewModel namespace MauiApp1.ViewModel
{ {
public class TransactionViewModel public class TransactionViewModel
{ {
public ObservableCollection<Transaction> Transactions { get; set; } public ObservableCollection<Transaction> Transactions { get; set; }
= new();
public TransactionViewModel() public ICommand AddCommand { get; set; }
public ICommand DeleteCommand { get; set; }
public ICommand EditCommand { get; set; }
private INavigation Navigation;
public TransactionViewModel(INavigation navigation)
{ {
Transactions = new ObservableCollection<Transaction>() Navigation = navigation;
{
new("Транспорт", 79.00f), AddCommand = new Command(async () => await AddTransaction());
new("Еда", 2000), DeleteCommand = new Command<Transaction>(DeleteTransaction);
new("Кино", 300), EditCommand = new Command<Transaction>(async t => await EditTransaction(t));
new("Транспорт", 79.00f),
new("Транспорт", 79.00f), Transactions = ChangeTransactionFile.LoadData(Transactions);
new("Транспорт", 7632.00f), }
new("Транспорт", 79.00f),
new("Транспорт", 79.00f) private void DeleteTransaction(Transaction transaction)
}; {
Transactions.Remove(transaction);
ChangeTransactionFile.SaveData(Transactions);
}
private async Task AddTransaction()
{
await Navigation.PushAsync(new AddTransactionView(Transactions));
}
private async Task EditTransaction(Transaction transac)
{
await Navigation.PushAsync(new AddTransactionView(Transactions, transac));
} }
} }
} }