Changing. Deleting
This commit is contained in:
parent
5ced7ad535
commit
3262e555f2
22 changed files with 223 additions and 53 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
55
MauiApp1/Helpers/ChangeTransactionFile.cs
Normal file
55
MauiApp1/Helpers/ChangeTransactionFile.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -57,6 +57,13 @@
|
|||
</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">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</MauiXaml>
|
||||
|
|
|
@ -1,32 +1,65 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MauiApp1.Model
|
||||
{
|
||||
public class Transaction
|
||||
public class Transaction : INotifyPropertyChanged
|
||||
{
|
||||
private string category;
|
||||
private float price;
|
||||
private DateTime date;
|
||||
|
||||
[JsonConstructor]
|
||||
public Transaction(string category, float price)
|
||||
{
|
||||
this.category = category;
|
||||
this.price = price;
|
||||
date = DateTime.Now;
|
||||
}
|
||||
|
||||
[JsonProperty]
|
||||
public DateTime Date
|
||||
{
|
||||
get { return date; }
|
||||
set { date = value; }
|
||||
}
|
||||
|
||||
[JsonProperty]
|
||||
public string Category
|
||||
{
|
||||
get { return category; }
|
||||
set { category = value; }
|
||||
set
|
||||
{
|
||||
if (value != category)
|
||||
{
|
||||
category = value;
|
||||
OnPropertyChanged(nameof(Category));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonProperty]
|
||||
public float 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,13 +7,16 @@
|
|||
<Label Text="Категория"
|
||||
FontSize="Medium"/>
|
||||
<Entry Placeholder="Введите данные"
|
||||
Margin="0, 10"/>
|
||||
Margin="0, 10"
|
||||
Text="{Binding CategoryEntry}"/>
|
||||
|
||||
<Label Text="Стоимость"
|
||||
FontSize="Medium"/>
|
||||
<Entry Placeholder="Введите данные"
|
||||
Margin="0, 10"/>
|
||||
Margin="0, 10"
|
||||
Text="{Binding PriceEntry}"/>
|
||||
|
||||
<Button Text="Добавить"/>
|
||||
<Button Text="Добавить"
|
||||
Command="{Binding AddTransactionCommand}"/>
|
||||
</VerticalStackLayout>
|
||||
</ContentPage>
|
|
@ -1,9 +1,22 @@
|
|||
using MauiApp1.ViewModel;
|
||||
using System.Collections.ObjectModel;
|
||||
using MauiApp1.Model;
|
||||
|
||||
namespace MauiApp1.View;
|
||||
|
||||
public partial class AddTransactionView : ContentPage
|
||||
{
|
||||
public AddTransactionView()
|
||||
public AddTransactionView(ObservableCollection<Transaction> transactions)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
BindingContext = new AddTransactionViewModel(transactions);
|
||||
}
|
||||
|
||||
public AddTransactionView(ObservableCollection<Transaction> transactions, Transaction transaction)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
BindingContext = new AddTransactionViewModel(transactions, transaction);
|
||||
}
|
||||
}
|
|
@ -2,19 +2,19 @@
|
|||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="MauiApp1.View.TransactionView"
|
||||
Title="Transactions">
|
||||
Title="Transactions"
|
||||
x:Name="TransactionPage">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Button x:Name="btnAddTransaction"
|
||||
Text="Add Transaction"
|
||||
<Button Text="Добавить"
|
||||
HorizontalOptions="Center"
|
||||
VerticalOptions="Center"
|
||||
Margin="20, 10"
|
||||
Command="{Binding AddTransactionCommand}"/>
|
||||
Command="{Binding AddCommand}"/>
|
||||
|
||||
<CollectionView ItemsSource="{Binding Transactions}" Grid.Row="1" >
|
||||
<CollectionView.ItemTemplate>
|
||||
|
@ -22,27 +22,37 @@
|
|||
<SwipeView>
|
||||
<SwipeView.RightItems>
|
||||
<SwipeItems>
|
||||
<SwipeItemView>
|
||||
<SwipeItemView Command="{Binding Path=BindingContext.EditCommand,
|
||||
Source={x:Reference Name=TransactionPage}}" CommandParameter="{Binding .}">
|
||||
<Frame BackgroundColor="LightGreen"
|
||||
Margin="5">
|
||||
<Label Text="Изменить"/>
|
||||
<Label Text="✏️"
|
||||
FontSize="Large"
|
||||
VerticalTextAlignment="Center"
|
||||
HorizontalTextAlignment="Center"/>
|
||||
</Frame>
|
||||
</SwipeItemView>
|
||||
|
||||
<SwipeItemView>
|
||||
<Frame BackgroundColor="LightCoral"
|
||||
Margin="5">
|
||||
<Label Text="Удалить"/>
|
||||
<SwipeItemView Command="{Binding Path=BindingContext.DeleteCommand,
|
||||
Source={x:Reference Name=TransactionPage}}" CommandParameter="{Binding .}">
|
||||
<Frame BackgroundColor="LightCoral" Margin="5">
|
||||
<Label Text="🗑️"
|
||||
FontSize="Large"
|
||||
VerticalTextAlignment="Center"
|
||||
HorizontalTextAlignment="Center"/>
|
||||
</Frame>
|
||||
</SwipeItemView>
|
||||
</SwipeItems>
|
||||
</SwipeView.RightItems>
|
||||
|
||||
<Frame CornerRadius="20" Margin="10" BackgroundColor="{AppThemeBinding Light={StaticResource FrameLight}, Dark={StaticResource FrameDark}}">
|
||||
<StackLayout Orientation="Vertical">
|
||||
<StackLayout Orientation="Horizontal">
|
||||
<Label Text="{Binding Category}" FontSize="Medium" HorizontalOptions="StartAndExpand"/>
|
||||
<Label Text="{Binding Price}" FontSize="Medium"/>
|
||||
</StackLayout>
|
||||
<Label Text="{Binding Date}" FontSize="Micro"/>
|
||||
</StackLayout>
|
||||
</Frame>
|
||||
</SwipeView>
|
||||
</DataTemplate>
|
||||
|
|
|
@ -7,6 +7,6 @@ public partial class TransactionView : ContentPage
|
|||
{
|
||||
InitializeComponent();
|
||||
|
||||
BindingContext = new TransactionViewModel();
|
||||
BindingContext = new TransactionViewModel(Navigation);
|
||||
}
|
||||
}
|
|
@ -1,14 +1,47 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using MauiApp1.Model;
|
||||
using MauiApp1.Helpers;
|
||||
|
||||
namespace MauiApp1.ViewModel
|
||||
{
|
||||
public class AddTransactionViewModel
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,31 +1,47 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using MauiApp1.Model;
|
||||
using MauiApp1.View;
|
||||
using MauiApp1.Helpers;
|
||||
|
||||
namespace MauiApp1.ViewModel
|
||||
{
|
||||
public class TransactionViewModel
|
||||
{
|
||||
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;
|
||||
|
||||
AddCommand = new Command(async () => await AddTransaction());
|
||||
DeleteCommand = new Command<Transaction>(DeleteTransaction);
|
||||
EditCommand = new Command<Transaction>(async t => await EditTransaction(t));
|
||||
|
||||
Transactions = ChangeTransactionFile.LoadData(Transactions);
|
||||
}
|
||||
|
||||
private void DeleteTransaction(Transaction transaction)
|
||||
{
|
||||
new("Транспорт", 79.00f),
|
||||
new("Еда", 2000),
|
||||
new("Кино", 300),
|
||||
new("Транспорт", 79.00f),
|
||||
new("Транспорт", 79.00f),
|
||||
new("Транспорт", 7632.00f),
|
||||
new("Транспорт", 79.00f),
|
||||
new("Транспорт", 79.00f)
|
||||
};
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue