-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add db logic to save SOS phone number
- Loading branch information
1 parent
032ee73
commit 1b6c687
Showing
10 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SettingsData>(); | ||
} | ||
|
||
public async Task<DbResponse> SaveNumber(string phoneNumber) | ||
{ | ||
try | ||
{ | ||
await Init(); | ||
|
||
var settingsList = await conn.Table<SettingsData>().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<DbResponse> IsNumberSavedAsync() | ||
{ | ||
await Init(); | ||
var settingsList = await conn.Table<SettingsData>().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 | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,55 @@ | ||
using SOS.Models; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace SOS; | ||
|
||
public partial class SettingsPage : ContentPage | ||
{ | ||
public SettingsData settings; | ||
|
||
public SettingsPage() | ||
{ | ||
InitializeComponent(); | ||
|
||
Task.Run(async () => await SetNumberIfExists()); | ||
} | ||
|
||
private async Task SetNumberIfExists() | ||
{ | ||
var settingsDbResponse = await App.SettingsRepo.IsNumberSavedAsync(); | ||
settings = settingsDbResponse.SettingsData; | ||
|
||
if(settingsDbResponse.Status is true) | ||
{ | ||
SavedNumberLabel.Text = $"Saved Number: {settings.PhoneNumber}"; | ||
} | ||
|
||
else | ||
{ | ||
SavedNumberLabel.Text = settingsDbResponse.StatusMessage; | ||
settings = new SettingsData(); | ||
} | ||
} | ||
|
||
private async void SaveSettingsButtonClicked(object sender, EventArgs e) | ||
{ | ||
await DisplayAlert("Alert", "Button Works", "Ok"); | ||
{ | ||
string phoneNumber = PhoneNumber.Text; | ||
|
||
Regex validatePhoneNumberRegex = new Regex("^\\+?[1-9][0-9]{7,14}$"); | ||
|
||
if(String.IsNullOrEmpty(phoneNumber) || validatePhoneNumberRegex.IsMatch(phoneNumber) is not true) | ||
{ | ||
await DisplayAlert("Alert", "Enter phone number in proper format", "Ok"); | ||
return; | ||
} | ||
|
||
var settingsDbResponse = await App.SettingsRepo.SaveNumber(phoneNumber); | ||
|
||
if(settingsDbResponse.Status is false) | ||
{ | ||
await DisplayAlert("Error", settingsDbResponse.StatusMessage, "Ok"); | ||
} | ||
|
||
await SetNumberIfExists(); | ||
} | ||
} |