Compare commits

..

1 Commits

Author SHA1 Message Date
5cb668aa93 Add class Fournisseur and button for Modif / Delete / Add 2025-01-28 13:50:12 +01:00
14 changed files with 387 additions and 37 deletions

View File

@ -1,4 +1,4 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<?xml version="1.0" encoding="UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
@ -11,4 +11,4 @@
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
</Application>

View File

@ -1,11 +1,20 @@
namespace MauiApp1;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using MauiApp1.Data;
public partial class App : Application
namespace MauiApp1
{
public App()
public partial class App : Application
{
InitializeComponent();
private readonly Database _database;
MainPage = new AppShell();
public App(Database database)
{
InitializeComponent();
_database = database;
MainPage = new AppShell();
}
public static Database Database => Current.Handler.MauiContext.Services.GetService<Database>();
}
}

View File

@ -3,6 +3,7 @@
x:Class="MauiApp1.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:MauiApp1.Views"
xmlns:local="clr-namespace:MauiApp1"
Shell.FlyoutBehavior="Disabled"
Title="MauiApp1">
@ -11,5 +12,14 @@
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />
<TabBar>
<ShellContent Title="Accueil" ContentTemplate="{DataTemplate local:MainPage}" />
<ShellContent Title="Fournisseurs" ContentTemplate="{DataTemplate views:FournisseurPage}" />
</TabBar>
</Shell>
<FlyoutItem Title="Fournisseurs" FlyoutDisplayOptions="AsSingleItem">
<ShellContent Route="fournisseurpage" ContentTemplate="{DataTemplate views:FournisseurPage}" />
</FlyoutItem>
</Shell>

View File

@ -1,9 +1,14 @@
namespace MauiApp1;
using MauiApp1.Views;
namespace MauiApp1;
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
// Enregistrement des routes
Routing.RegisterRoute("fournisseurpage", typeof(FournisseurPage));
}
}

59
MauiApp1/Data/Database.cs Normal file
View File

@ -0,0 +1,59 @@
using SQLite;
using MauiApp1.Model;
namespace MauiApp1.Data
{
public class Database
{
private readonly SQLiteAsyncConnection _database;
public Database(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Fournisseur>().Wait();
}
public async Task<List<Fournisseur>> GetFournisseursAsync()
{
try
{
return await _database.Table<Fournisseur>().ToListAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Erreur lors de la récupération des fournisseurs: {ex.Message}");
throw;
}
}
public async Task<int> SaveFournisseurAsync(Fournisseur fournisseur)
{
try
{
if (fournisseur.Id != 0)
{
return await _database.UpdateAsync(fournisseur);
}
return await _database.InsertAsync(fournisseur);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Erreur lors de la sauvegarde du fournisseur: {ex.Message}");
throw;
}
}
public async Task<int> DeleteFournisseurAsync(Fournisseur fournisseur)
{
try
{
return await _database.DeleteAsync(fournisseur);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Erreur lors de la suppression du fournisseur: {ex.Message}");
throw;
}
}
}
}

View File

@ -7,30 +7,15 @@
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />
<Label
Text="Hello, World!"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
<Button Text="Voir Fournisseurs" Clicked="OnFournisseursClicked"/>
<Label
Text="Welcome to &#10;.NET Multi-platform App UI"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>

View File

@ -1,4 +1,7 @@
namespace MauiApp1;
using MauiApp1.Model;
using MauiApp1.Views;
namespace MauiApp1;
public partial class MainPage : ContentPage
{
@ -9,15 +12,8 @@ public partial class MainPage : ContentPage
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
private async void OnFournisseursClicked(object? sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
await Navigation.PushAsync(new FournisseurPage());
}
}

View File

@ -60,6 +60,8 @@
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0"/>
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.11-pre20241216174303" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,6 @@
using Microsoft.Extensions.Logging;
using MauiApp1.Views;
using MauiApp1.Data;
namespace MauiApp1;
@ -15,6 +17,14 @@ public static class MauiProgram
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
// Enregistrement des services
builder.Services.AddSingleton<Database>(serviceProvider =>
new Database(Path.Combine(FileSystem.AppDataDirectory, "FournisseursDB.db3")));
// Enregistrement des pages
builder.Services.AddTransient<FournisseurPage>();
builder.Services.AddSingleton<App>();
#if DEBUG
builder.Logging.AddDebug();
#endif

View File

@ -0,0 +1,19 @@
using SQLite;
namespace MauiApp1.Model
{
public class Fournisseur
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[NotNull]
public string Name { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Address { get; set; }
}
}

View File

@ -0,0 +1,39 @@
using SQLite;
using System.Collections.Generic;
using System.Threading.Tasks;
using MauiApp1.Model;
namespace MauiApp1.Services
{
public class FournisseurDatabase
{
private readonly SQLiteAsyncConnection _database;
public FournisseurDatabase(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Fournisseur>().Wait();
}
// Récupérer tous les fournisseurs
public Task<List<Fournisseur>> GetFournisseursAsync()
{
return _database.Table<Fournisseur>().ToListAsync();
}
// Ajouter ou mettre à jour un fournisseur
public Task<int> SaveFournisseurAsync(Fournisseur fournisseur)
{
if (fournisseur.Id != 0)
return _database.UpdateAsync(fournisseur);
else
return _database.InsertAsync(fournisseur);
}
// Supprimer un fournisseur
public Task<int> DeleteFournisseurAsync(Fournisseur fournisseur)
{
return _database.DeleteAsync(fournisseur);
}
}
}

View File

@ -0,0 +1,75 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using MauiApp1.Model;
using MauiApp1.Services;
using System.Threading.Tasks;
namespace MauiApp1.ViewModels
{
public class FournisseurViewModel : BindableObject
{
private readonly FournisseurDatabase _database;
public ObservableCollection<Fournisseur> Fournisseurs { get; set; }
private Fournisseur _selectedFournisseur;
public Fournisseur SelectedFournisseur
{
get => _selectedFournisseur;
set
{
_selectedFournisseur = value;
OnPropertyChanged();
}
}
public ICommand AjouterFournisseurCommand { get; }
public ICommand ModifierFournisseurCommand { get; }
public ICommand SupprimerFournisseurCommand { get; }
public FournisseurViewModel(FournisseurDatabase database)
{
_database = database;
Fournisseurs = new ObservableCollection<Fournisseur>();
LoadFournisseurs();
AjouterFournisseurCommand = new Command(async () => await AjouterFournisseur());
ModifierFournisseurCommand = new Command(async () => await ModifierFournisseur());
SupprimerFournisseurCommand = new Command(async () => await SupprimerFournisseur());
}
private async void LoadFournisseurs()
{
var fournisseurs = await _database.GetFournisseursAsync();
Fournisseurs.Clear();
foreach (var fournisseur in fournisseurs)
{
Fournisseurs.Add(fournisseur);
}
}
private async Task AjouterFournisseur()
{
var nouveauFournisseur = new Fournisseur { Name = "Nouveau", Email = "new@mail.com", Telephone = "0000000000", Address = "Ville" };
await _database.SaveFournisseurAsync(nouveauFournisseur);
LoadFournisseurs();
}
private async Task ModifierFournisseur()
{
if (SelectedFournisseur != null)
{
await _database.SaveFournisseurAsync(SelectedFournisseur);
LoadFournisseurs();
}
}
private async Task SupprimerFournisseur()
{
if (SelectedFournisseur != null)
{
await _database.DeleteFournisseurAsync(SelectedFournisseur);
LoadFournisseurs();
}
}
}
}

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.Views.FournisseurPage"
Title="Fournisseurs">
<Grid RowDefinitions="Auto,*">
<!-- Bouton Ajouter -->
<Button Grid.Row="0"
Text="Ajouter un fournisseur"
Clicked="OnAddFournisseurClicked"
Margin="10"/>
<!-- Liste des fournisseurs -->
<CollectionView Grid.Row="1"
x:Name="FournisseursList"
ItemsSource="{Binding ItemsSource}" x:FieldModifier="public">
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Modifier"
BackgroundColor="Orange"
Clicked="OnEditFournisseur"/>
<SwipeItem Text="Supprimer"
BackgroundColor="Red"
Clicked="OnDeleteFournisseur"/>
</SwipeItems>
</SwipeView.RightItems>
<Grid Padding="10" RowDefinitions="Auto,Auto,Auto,Auto">
<Label Grid.Row="0"
Text="{Binding Name}"
FontAttributes="Bold"/>
<Label Grid.Row="1"
Text="{Binding Email}"/>
<Label Grid.Row="2"
Text="{Binding Telephone}"/>
<Label Grid.Row="3"
Text="{Binding Address}"/>
</Grid>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</ContentPage>

View File

@ -0,0 +1,93 @@
using MauiApp1.Model;
namespace MauiApp1.Views;
public partial class FournisseurPage : ContentPage
{
public FournisseurPage()
{
InitializeComponent();
LoadFournisseurs();
}
private async Task LoadFournisseurs()
{
try
{
var fournisseurs = await App.Database.GetFournisseursAsync();
FournisseursList.ItemsSource = fournisseurs;
}
catch (Exception ex)
{
await DisplayAlert("Erreur", "Impossible de charger les fournisseurs: " + ex.Message, "OK");
}
}
private async void OnAddFournisseurClicked(object sender, EventArgs e)
{
try
{
var fournisseur = new Fournisseur
{
Name = await DisplayPromptAsync("Nouveau fournisseur", "Nom:"),
Email = await DisplayPromptAsync("Email", "Email:"),
Telephone = await DisplayPromptAsync("Téléphone", "Numéro:"),
Address = await DisplayPromptAsync("Adresse", "Adresse:")
};
if (string.IsNullOrWhiteSpace(fournisseur.Name)) return;
await App.Database.SaveFournisseurAsync(fournisseur);
await LoadFournisseurs();
}
catch (Exception ex)
{
await DisplayAlert("Erreur", "Impossible d'ajouter le fournisseur: " + ex.Message, "OK");
}
}
private async void OnEditFournisseur(object sender, EventArgs e)
{
try
{
var swipeItem = sender as SwipeItem;
var fournisseur = swipeItem?.BindingContext as Fournisseur;
if (fournisseur == null) return;
fournisseur.Name = await DisplayPromptAsync("Modifier", "Nom:", initialValue: fournisseur.Name);
fournisseur.Email = await DisplayPromptAsync("Modifier", "Email:", initialValue: fournisseur.Email);
fournisseur.Telephone = await DisplayPromptAsync("Modifier", "Téléphone:", initialValue: fournisseur.Telephone);
fournisseur.Address = await DisplayPromptAsync("Modifier", "Adresse:", initialValue: fournisseur.Address);
await App.Database.SaveFournisseurAsync(fournisseur);
await LoadFournisseurs();
}
catch (Exception ex)
{
await DisplayAlert("Erreur", "Impossible de modifier le fournisseur: " + ex.Message, "OK");
}
}
private async void OnDeleteFournisseur(object sender, EventArgs e)
{
try
{
var swipeItem = sender as SwipeItem;
var fournisseur = swipeItem?.BindingContext as Fournisseur;
if (fournisseur == null) return;
bool answer = await DisplayAlert("Confirmation",
"Voulez-vous vraiment supprimer ce fournisseur ?", "Oui", "Non");
if (answer)
{
await App.Database.DeleteFournisseurAsync(fournisseur);
await LoadFournisseurs();
}
}
catch (Exception ex)
{
await DisplayAlert("Erreur", "Impossible de supprimer le fournisseur: " + ex.Message, "OK");
}
}
}