Add Contact Update

main
unknown 6 months ago
parent e77ebc692c
commit 47f3cf95c7
  1. 44
      Helpers/DatabaseHelper.cs
  2. 4
      Models/EmergencyContact.cs
  3. BIN
      Resources/Images/unnamed2.png
  4. 47
      Views/AddContactPage.xaml
  5. 39
      Views/AddContactPage.xaml.cs
  6. 2
      Views/DashboardPage.xaml
  7. 2
      Views/DashboardPage.xaml.cs
  8. 12
      Views/EmergencyContactsPage.xaml
  9. 15
      Views/EmergencyContactsPage.xaml.cs

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SQLite;
namespace Justice.Helpers
{
public class DatabaseHelper
{
private readonly SQLiteAsyncConnection _database;
public DatabaseHelper(string databaseName = "Appdatabase.db")
{
var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), databaseName);
_database = new SQLiteAsyncConnection(dbPath);
}
public async Task InitializeAsync<T>() where T: new()
{
await _database.CreateTableAsync<T>();
}
public async Task<int> InsertAsync<T>(T item) where T : new()
{
return await _database.InsertAsync(item);
}
public async Task<List<T>>GetAllAsync<T>()where T : new()
{
return await _database.Table<T>().ToListAsync();
}
public async Task<List<T>> GetAsync<T>(Expression<Func<T, bool>> predicate) where T : new()
{
return await _database.Table<T>().Where(predicate).ToListAsync();
}
public async Task<int> UpdateAsync<T>(T item) where T : new()
{
return await _database.UpdateAsync(item);
}
public async Task<int> DeleteAsync<T>(T item) where T : new()
{
return await _database.DeleteAsync(item);
}
}
}

@ -3,14 +3,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Justice.Models;
namespace Justice.Models
{
public class Contact
public class EmergencyContact
{
public int Id { get; set; }
public string Name { get; set; }
public string MobileNumber { get; set; }
public string Group { get; set; }
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

@ -0,0 +1,47 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Justice.Views.AddContactPage"
Title="Add Emergency Contact">
<StackLayout Padding="20">
<Label Text="Name" FontSize="Medium" />
<Entry x:Name="NameEntry" Placeholder="Enter name" />
<Label Text="Mobile Number" FontSize="Medium" />
<Entry x:Name="MobileNumberEntry" Placeholder="Enter mobile number" Keyboard="Telephone" />
<Label Text="Group" FontSize="Medium" />
<Picker x:Name="GroupPicker">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Parents</x:String>
<x:String>Friends</x:String>
<x:String>Colleagues</x:String>
<x:String>Others</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
<Button Text="Save Contact" Clicked="OnSaveContactClicked" />
<ScrollView>
<StackLayout>
<Grid RowSpacing="10" ColumnSpacing="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" HeightRequest="100" Margin="1">
</Border>
<Border Grid.Column="1" HeightRequest="100">
</Border>
<Border Grid.Column="2" HeightRequest="100">
</Border>
</Grid>
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage>

@ -0,0 +1,39 @@
using Justice.Models;
using Justice.Views;
using Justice.Helpers;
namespace Justice.Views;
public partial class AddContactPage : ContentPage
{
private readonly DatabaseHelper _databaseHelper;
public AddContactPage()
{
InitializeComponent();
_databaseHelper = new DatabaseHelper();
}
private async void OnSaveContactClicked(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(NameEntry.Text) || string.IsNullOrWhiteSpace(MobileNumberEntry.Text) || GroupPicker.SelectedItem == null)
{
await DisplayAlert("Error", "All Fields Required", "OK");
}
var contact = new EmergencyContact
{
Name = NameEntry.Text,
MobileNumber = MobileNumberEntry.Text,
Group = GroupPicker.SelectedItem.ToString()
};
await _databaseHelper.InsertAsync(contact);
await DisplayAlert("Success", "Contact Saved Successfully", "OK");
await Navigation.PushAsync(new EmergencyContactsPage());
}
catch (Exception ex)
{
await DisplayAlert("Success", $"{ex}", "OK");
}
}
}

@ -25,7 +25,7 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<VerticalStackLayout Grid.Column="0" VerticalOptions="Center">
<Label Text="Welcome" FontSize="20" TextColor="LightGray" FontAttributes="Bold" Padding="10"/>
<Label Text="Welcome" FontSize="20" TextColor="LightGray" FontAttributes="Bold" Padding="10" AnchorY="10"/>
</VerticalStackLayout>
<Border Grid.Column="1" StrokeThickness="1" Stroke="Gray" HeightRequest="60" WidthRequest="60" Margin="8">
<Border.StrokeShape>

@ -51,6 +51,6 @@ public partial class DashboardPage : ContentPage
private void OnAddContactClicked(object sender, EventArgs e)
{
DisplayAlert("Add contact button clicked", "Add contact", "OK");
Navigation.PushAsync(new AddContactPage());
}
}

@ -0,0 +1,12 @@
<?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="Justice.Views.EmergencyContactsPage"
Title="EmergencyContactsPage">
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>

@ -0,0 +1,15 @@
using Justice.Helpers;
namespace Justice.Views;
public partial class EmergencyContactsPage : ContentPage
{
private readonly DatabaseHelper _databaseHelper;
public EmergencyContactsPage()
{
InitializeComponent();
_databaseHelper = new DatabaseHelper();
}
}
Loading…
Cancel
Save