From 1b6c6873826240454cbc6516e1148be6044ccada Mon Sep 17 00:00:00 2001 From: adityaoberai Date: Sun, 22 Jan 2023 03:17:06 +0530 Subject: [PATCH] Add db logic to save SOS phone number --- SOS/App.xaml.cs | 18 +++++--- SOS/Data/Constants.cs | 15 +++++++ SOS/Data/SettingsRepository.cs | 81 ++++++++++++++++++++++++++++++++++ SOS/MainPage.xaml.cs | 21 ++++++++- SOS/MauiProgram.cs | 8 +++- SOS/Models/DbResponse.cs | 9 ++++ SOS/Models/SettingsData.cs | 13 ++++++ SOS/SOS.csproj | 10 +++++ SOS/SettingsPage.xaml | 7 ++- SOS/SettingsPage.xaml.cs | 45 ++++++++++++++++++- 10 files changed, 215 insertions(+), 12 deletions(-) create mode 100644 SOS/Data/Constants.cs create mode 100644 SOS/Data/SettingsRepository.cs create mode 100644 SOS/Models/DbResponse.cs create mode 100644 SOS/Models/SettingsData.cs diff --git a/SOS/App.xaml.cs b/SOS/App.xaml.cs index 41c1ae2..0890257 100644 --- a/SOS/App.xaml.cs +++ b/SOS/App.xaml.cs @@ -1,11 +1,17 @@ -namespace SOS; +using SOS.Data; + +namespace SOS; public partial class App : Application { - public App() - { - InitializeComponent(); + public static SettingsRepository SettingsRepo { get; private set; } + + public App(SettingsRepository repo) + { + InitializeComponent(); + + MainPage = new AppShell(); - MainPage = new AppShell(); - } + SettingsRepo = repo; + } } diff --git a/SOS/Data/Constants.cs b/SOS/Data/Constants.cs new file mode 100644 index 0000000..20204c9 --- /dev/null +++ b/SOS/Data/Constants.cs @@ -0,0 +1,15 @@ +namespace SOS.Data +{ + public static class Constants + { + public const string DatabaseFilename = "SOSSettings.db3"; + + public const SQLite.SQLiteOpenFlags Flags = + SQLite.SQLiteOpenFlags.ReadWrite | + SQLite.SQLiteOpenFlags.Create | + SQLite.SQLiteOpenFlags.SharedCache; + + public static string DatabasePath => + Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename); + } +} diff --git a/SOS/Data/SettingsRepository.cs b/SOS/Data/SettingsRepository.cs new file mode 100644 index 0000000..024d700 --- /dev/null +++ b/SOS/Data/SettingsRepository.cs @@ -0,0 +1,81 @@ +using SOS.Models; +using SQLite; + +namespace SOS.Data +{ + public class SettingsRepository + { + private SQLiteAsyncConnection conn; + + public async Task Init() + { + if (conn != null) + { + return; + } + + conn = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags); + await conn.CreateTableAsync(); + } + + public async Task SaveNumber(string phoneNumber) + { + try + { + await Init(); + + var settingsList = await conn.Table().ToListAsync(); + var settings = settingsList.FirstOrDefault(); + if (settings is null) + { + settings = new SettingsData { PhoneNumber = phoneNumber }; + await conn.InsertAsync(settings); + } + else + { + settings.PhoneNumber = phoneNumber; + await conn.UpdateAsync(settings); + } + return new DbResponse() + { + Status = true, + StatusMessage = "Number saved sucessfully", + SettingsData = settings + }; + } + catch (Exception ex) + { + return new DbResponse() + { + Status = false, + StatusMessage = ex.Message, + SettingsData = null + }; + } + } + + public async Task IsNumberSavedAsync() + { + await Init(); + var settingsList = await conn.Table().ToListAsync(); + var settings = settingsList.FirstOrDefault(); + + if (settings is null) + { + return new DbResponse() + { + Status = false, + StatusMessage = "SOS number missing", + SettingsData = null + }; + } + + return new DbResponse() + { + Status = true, + StatusMessage = "SOS number exists", + SettingsData = settings + }; + } + } +} diff --git a/SOS/MainPage.xaml.cs b/SOS/MainPage.xaml.cs index 353cb30..fe91ab1 100644 --- a/SOS/MainPage.xaml.cs +++ b/SOS/MainPage.xaml.cs @@ -5,12 +5,31 @@ public partial class MainPage : ContentPage public MainPage() { InitializeComponent(); + + Init(); } + private async void Init() + { + var settingsDbResponse = await App.SettingsRepo.IsNumberSavedAsync(); + if (settingsDbResponse.Status == false) + { + await DisplayAlert("Alert", "Please add SOS number", "Ok"); + } + } + private async void SOSButtonClicked(object sender, EventArgs e) { + var settingsDbResponse = await App.SettingsRepo.IsNumberSavedAsync(); + if (settingsDbResponse.Status == false) + { + await DisplayAlert("Alert", "SOS number missing", "Ok"); + return; + } + + var settings = settingsDbResponse.SettingsData; SOSButton.BackgroundColor = Colors.Crimson; - await DisplayAlert("Alert", "Button Works", "Ok"); + await DisplayAlert("Alert", $"Saved number: {settings.PhoneNumber}", "Ok"); SOSButton.BackgroundColor = Colors.Red; } } diff --git a/SOS/MauiProgram.cs b/SOS/MauiProgram.cs index a4dc27e..261624f 100644 --- a/SOS/MauiProgram.cs +++ b/SOS/MauiProgram.cs @@ -1,4 +1,6 @@ -namespace SOS; +using SOS.Data; + +namespace SOS; public static class MauiProgram { @@ -13,6 +15,8 @@ public static MauiApp CreateMauiApp() fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); - return builder.Build(); + builder.Services.AddSingleton(); + + return builder.Build(); } } diff --git a/SOS/Models/DbResponse.cs b/SOS/Models/DbResponse.cs new file mode 100644 index 0000000..d1a3e7f --- /dev/null +++ b/SOS/Models/DbResponse.cs @@ -0,0 +1,9 @@ +namespace SOS.Models +{ + public class DbResponse + { + public bool Status { get; set; } + public string StatusMessage { get; set; } + public SettingsData SettingsData { get; set; } + } +} diff --git a/SOS/Models/SettingsData.cs b/SOS/Models/SettingsData.cs new file mode 100644 index 0000000..2b508f9 --- /dev/null +++ b/SOS/Models/SettingsData.cs @@ -0,0 +1,13 @@ +using SQLite; + +namespace SOS.Models +{ + [Table("settings")] + public class SettingsData + { + [PrimaryKey, AutoIncrement, Column("_id")] + public int Id { get; set; } + [MaxLength(14), Unique] + public string PhoneNumber { get; set; } + } +} diff --git a/SOS/SOS.csproj b/SOS/SOS.csproj index 313909a..d8996ea 100644 --- a/SOS/SOS.csproj +++ b/SOS/SOS.csproj @@ -47,10 +47,20 @@ + + + + + + + + MSBuild:Compile + + diff --git a/SOS/SettingsPage.xaml b/SOS/SettingsPage.xaml index 109941d..905197a 100644 --- a/SOS/SettingsPage.xaml +++ b/SOS/SettingsPage.xaml @@ -22,6 +22,11 @@ VerticalOptions="Center" HorizontalOptions="FillAndExpand" Clicked="SaveSettingsButtonClicked" /> - + +