diff --git a/MauiApp1/App.xaml b/MauiApp1/App.xaml
index 8fb5f38..7201dba 100644
--- a/MauiApp1/App.xaml
+++ b/MauiApp1/App.xaml
@@ -1,4 +1,4 @@
-
+
-
+
\ No newline at end of file
diff --git a/MauiApp1/App.xaml.cs b/MauiApp1/App.xaml.cs
index e2de305..a69f453 100644
--- a/MauiApp1/App.xaml.cs
+++ b/MauiApp1/App.xaml.cs
@@ -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();
}
}
\ No newline at end of file
diff --git a/MauiApp1/AppShell.xaml b/MauiApp1/AppShell.xaml
index 5dd865e..bb5a1be 100644
--- a/MauiApp1/AppShell.xaml
+++ b/MauiApp1/AppShell.xaml
@@ -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" />
+
+
+
+
+
-
+
+
+
+
+
\ No newline at end of file
diff --git a/MauiApp1/AppShell.xaml.cs b/MauiApp1/AppShell.xaml.cs
index 2401c8f..74efde4 100644
--- a/MauiApp1/AppShell.xaml.cs
+++ b/MauiApp1/AppShell.xaml.cs
@@ -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));
}
}
\ No newline at end of file
diff --git a/MauiApp1/Data/Database.cs b/MauiApp1/Data/Database.cs
new file mode 100644
index 0000000..9049348
--- /dev/null
+++ b/MauiApp1/Data/Database.cs
@@ -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().Wait();
+ }
+
+ public async Task> GetFournisseursAsync()
+ {
+ try
+ {
+ return await _database.Table().ToListAsync();
+ }
+ catch (Exception ex)
+ {
+ System.Diagnostics.Debug.WriteLine($"Erreur lors de la récupération des fournisseurs: {ex.Message}");
+ throw;
+ }
+ }
+
+ public async Task 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 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;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/MauiApp1/MainPage.xaml b/MauiApp1/MainPage.xaml
index 4ff41e1..547a173 100644
--- a/MauiApp1/MainPage.xaml
+++ b/MauiApp1/MainPage.xaml
@@ -7,30 +7,15 @@
-
-
+
+
-
-
-
+
diff --git a/MauiApp1/MainPage.xaml.cs b/MauiApp1/MainPage.xaml.cs
index df4cb06..6091be3 100644
--- a/MauiApp1/MainPage.xaml.cs
+++ b/MauiApp1/MainPage.xaml.cs
@@ -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());
}
}
\ No newline at end of file
diff --git a/MauiApp1/MauiApp1.csproj b/MauiApp1/MauiApp1.csproj
index f24a7ba..898a356 100644
--- a/MauiApp1/MauiApp1.csproj
+++ b/MauiApp1/MauiApp1.csproj
@@ -60,6 +60,8 @@
+
+
diff --git a/MauiApp1/MauiProgram.cs b/MauiApp1/MauiProgram.cs
index 984be05..640cb68 100644
--- a/MauiApp1/MauiProgram.cs
+++ b/MauiApp1/MauiProgram.cs
@@ -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(serviceProvider =>
+ new Database(Path.Combine(FileSystem.AppDataDirectory, "FournisseursDB.db3")));
+
+ // Enregistrement des pages
+ builder.Services.AddTransient();
+ builder.Services.AddSingleton();
+
#if DEBUG
builder.Logging.AddDebug();
#endif
diff --git a/MauiApp1/Model/Fournisseur.cs b/MauiApp1/Model/Fournisseur.cs
new file mode 100644
index 0000000..06a531a
--- /dev/null
+++ b/MauiApp1/Model/Fournisseur.cs
@@ -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; }
+ }
+}
\ No newline at end of file
diff --git a/MauiApp1/Services/FournisseurDatabase.cs b/MauiApp1/Services/FournisseurDatabase.cs
new file mode 100644
index 0000000..2af2aab
--- /dev/null
+++ b/MauiApp1/Services/FournisseurDatabase.cs
@@ -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().Wait();
+ }
+
+ // Récupérer tous les fournisseurs
+ public Task> GetFournisseursAsync()
+ {
+ return _database.Table().ToListAsync();
+ }
+
+ // Ajouter ou mettre à jour un fournisseur
+ public Task SaveFournisseurAsync(Fournisseur fournisseur)
+ {
+ if (fournisseur.Id != 0)
+ return _database.UpdateAsync(fournisseur);
+ else
+ return _database.InsertAsync(fournisseur);
+ }
+
+ // Supprimer un fournisseur
+ public Task DeleteFournisseurAsync(Fournisseur fournisseur)
+ {
+ return _database.DeleteAsync(fournisseur);
+ }
+ }
+}
\ No newline at end of file
diff --git a/MauiApp1/ViewModels/FournisseurViewModel.cs b/MauiApp1/ViewModels/FournisseurViewModel.cs
new file mode 100644
index 0000000..29d037a
--- /dev/null
+++ b/MauiApp1/ViewModels/FournisseurViewModel.cs
@@ -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 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();
+ 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();
+ }
+ }
+ }
+}
diff --git a/MauiApp1/Views/FournisseurPage.xaml b/MauiApp1/Views/FournisseurPage.xaml
new file mode 100644
index 0000000..8059bac
--- /dev/null
+++ b/MauiApp1/Views/FournisseurPage.xaml
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/MauiApp1/Views/FournisseurPage.xaml.cs b/MauiApp1/Views/FournisseurPage.xaml.cs
new file mode 100644
index 0000000..37c4cb1
--- /dev/null
+++ b/MauiApp1/Views/FournisseurPage.xaml.cs
@@ -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");
+ }
+ }
+}
\ No newline at end of file