diff --git a/PasswordManager/App.xaml.cs b/PasswordManager/App.xaml.cs index e9f725ea..bcb1479c 100644 --- a/PasswordManager/App.xaml.cs +++ b/PasswordManager/App.xaml.cs @@ -1,4 +1,5 @@ using PasswordManager.Model.DB; +using PasswordManager.Model.IO; namespace PasswordManager { @@ -6,11 +7,14 @@ public partial class App : Application { public App() { - Task dbInit = PasswordController.Initialize(); + Task[] Inits = { + PasswordController.Initialize(), + AppDirectoryManager.Initialize() + }; InitializeComponent(); - Task.WhenAll(dbInit); + Task.WhenAll(Inits); MainPage = new AppShell(); } } diff --git a/PasswordManager/AppShell.xaml b/PasswordManager/AppShell.xaml index 348368f5..3c35d7c6 100644 --- a/PasswordManager/AppShell.xaml +++ b/PasswordManager/AppShell.xaml @@ -9,6 +9,6 @@ + Route="RecentPage"/> diff --git a/PasswordManager/AppShell.xaml.cs b/PasswordManager/AppShell.xaml.cs index f28d8e96..96e0ffe4 100644 --- a/PasswordManager/AppShell.xaml.cs +++ b/PasswordManager/AppShell.xaml.cs @@ -8,7 +8,8 @@ public AppShell() { InitializeComponent(); - Routing.RegisterRoute(nameof(RecentPage), typeof(RecentPage)); + //Register routes + Routing.RegisterRoute(nameof(AddPage), typeof(AddPage)); } } } \ No newline at end of file diff --git a/PasswordManager/MauiProgram.cs b/PasswordManager/MauiProgram.cs index 912298f3..918a399c 100644 --- a/PasswordManager/MauiProgram.cs +++ b/PasswordManager/MauiProgram.cs @@ -17,9 +17,13 @@ public static MauiApp CreateMauiApp() fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); + //Pass viewmodel into page using Singleton/Transient builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddTransient(); + builder.Services.AddTransient(); + #if DEBUG builder.Logging.AddDebug(); #endif diff --git a/PasswordManager/Model/DB/DBController.cs b/PasswordManager/Model/DB/DBController.cs index b58e7760..ff7961e4 100644 --- a/PasswordManager/Model/DB/DBController.cs +++ b/PasswordManager/Model/DB/DBController.cs @@ -1,79 +1,35 @@ -using System.Data; -using System.Data.SqlServerCe; +using Microsoft.EntityFrameworkCore; using PasswordManager.Model.DB.Schema; +using PasswordManager.Model.IO; +using System.Data; -namespace PasswordManager.Model.DB -{ - public class DBController : IController - { - private static readonly string DBName = PasswordController.Path + @"\PswDB.sdf"; - private static readonly string Com = $"DataSource=\"{DBName}\"; Password=\"scpassw1\""; - private SqlCeEngine _engine; - private SqlCeConnection _connection; - private SqlCeCommand _cmd; - - private async Task OpenCon() - { - _connection = new SqlCeConnection(Com); - await _connection.OpenAsync(); - } - - private async Task CreateCommand(string command) - { - _cmd = new SqlCeCommand(command, _connection); - await _cmd.ExecuteNonQueryAsync(); - SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(_cmd); - DataSet dataSet = new DataSet(); - dataAdapter.Fill(dataSet); - return dataSet; - } +namespace PasswordManager.Model.DB; - public async Task Initialize() - { - File.Delete(DBName); - - if (!File.Exists(DBName)) - { - try - { - _engine = new SqlCeEngine(Com); - _engine.CreateDatabase(); - } - catch { } - - await OpenCon(); - - try - { - await CreateCommand("CREATE TABLE Profiles " + - "(ProfileID int IDENTITY(1,1) PRIMARY KEY," + - "Service nvarchar(4000) NOT NULL," + - "Email nvarchar(4000) NOT NULL," + - "Password nvarchar(4000) NOT NULL," + - "Username nvarchar(4000));"); - _engine.Upgrade(); - } - catch { } - } - else - { - await OpenCon(); - } +public class DBController : DbContext, IController +{ + private static readonly string DBPath = Path.Combine(AppDirectoryManager.AppData, "Psw.db"); + private static readonly string Connection = $"Filename=\"{DBPath}\""; + + private DbSet Profiles { get; set; } - } + public async Task Initialize() + { + await Database.EnsureCreatedAsync(); + } - public async Task Add(Profile profile) - { - await CreateCommand("INSERT INTO Profiles (Service, Email, Password, Username)" + - $"VALUES (\'{profile.Service}\',\'{profile.Email.Adress}\',\'{profile.Password}\',\'{profile.Username}\')"); - } + public async Task Add(Profile profile) + { + await Profiles.AddAsync(profile); + await SaveChangesAsync(); + } - public async Task Select(string condition) - { - DataSet dataSet = await CreateCommand("SELECT * FROM Profiles " + - "WHERE " + condition + ";"); + public IEnumerable Select(Func predicate) + { + return Profiles.Where(predicate); + } - return dataSet; - } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite(Connection); } } diff --git a/PasswordManager/Model/DB/IController.cs b/PasswordManager/Model/DB/IController.cs index 8e2598bc..cc67e396 100644 --- a/PasswordManager/Model/DB/IController.cs +++ b/PasswordManager/Model/DB/IController.cs @@ -1,17 +1,10 @@ using PasswordManager.Model.DB.Schema; -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace PasswordManager.Model.DB +namespace PasswordManager.Model.DB; + +internal interface IController { - internal interface IController - { - public Task Initialize(); - public Task Select(string condition); - public Task Add(Profile profile); - } + public Task Initialize(); + public IEnumerable Select(Func predicate); + public Task Add(Profile profile); } diff --git a/PasswordManager/Model/DB/PasswordController.cs b/PasswordManager/Model/DB/PasswordController.cs index c04942e2..265016ee 100644 --- a/PasswordManager/Model/DB/PasswordController.cs +++ b/PasswordManager/Model/DB/PasswordController.cs @@ -1,61 +1,23 @@ -using System; -using System.IO; -using System.Collections.Generic; -using System.Text; -using System.Data; -using System.Threading.Tasks; -using PasswordManager.Model.DB.Schema; +using PasswordManager.Model.DB.Schema; -namespace PasswordManager.Model.DB -{ - public static class PasswordController - { - private static IController DB = new DBController(); - public static string Path = @"\Passwords"; - - public static async Task Initialize() - { - Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path; - - if (!Directory.Exists(Path)) - { - Directory.CreateDirectory(Path); - } +namespace PasswordManager.Model.DB; - await DB.Initialize(); - } +public static class PasswordController +{ + private static IController DB = new DBController(); - public static async void SavePasswords(Profile[] data) - { - foreach (Profile prof in data) - { - await DB.Add(prof); - } - } + public static async Task Initialize() + { + await DB.Initialize(); + } - public static async Task> SearhProfiles(string keyWord) + public static async void SavePasswords(Profile[] data) + { + foreach (Profile prof in data) { - List profiles = new List(); - - DataSet data = await DB.Select(keyWord); - - DataRowCollection dataRows = data.Tables[0].Rows; - - foreach (DataRow row in dataRows) - { - object[] items = row.ItemArray; - - Profile profile = new Profile(); - - profile.Service = (string)items[1]; - profile.Email = new EMail() { Adress = (string)items[2] }; - profile.Password = (string)items[3]; - profile.Username = (string)items[4]; - - profiles.Add(profile); - } - - return profiles; + await DB.Add(prof); } } + + public static List SearhProfiles(Func predicate) => DB.Select(predicate).ToList(); } diff --git a/PasswordManager/Model/DB/Schema/AProfile.cs b/PasswordManager/Model/DB/Schema/AProfile.cs index 0118bcf5..82bac4b2 100644 --- a/PasswordManager/Model/DB/Schema/AProfile.cs +++ b/PasswordManager/Model/DB/Schema/AProfile.cs @@ -1,15 +1,12 @@ -using System; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; -namespace PasswordManager.Model.DB.Schema +namespace PasswordManager.Model.DB.Schema; + +public abstract class AProfile : IEquatable { - public abstract class AProfile : IEquatable - { - protected string username; - protected string password; - protected EMail eMail; - protected string service; + protected string username; + protected string password; + protected string service; - public abstract bool Equals([AllowNull] Profile other); - } + public abstract bool Equals([AllowNull] Profile other); } \ No newline at end of file diff --git a/PasswordManager/Model/DB/Schema/EMail.cs b/PasswordManager/Model/DB/Schema/EMail.cs index 02c17d7e..3f7a65ea 100644 --- a/PasswordManager/Model/DB/Schema/EMail.cs +++ b/PasswordManager/Model/DB/Schema/EMail.cs @@ -1,62 +1,64 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace PasswordManager.Model.DB.Schema; -namespace PasswordManager.Model.DB.Schema +public class EMail { - public class EMail - { - private const char SplitChar = '@'; + private char SplitChar = '@'; + + public int ID { get; set; } - public string Adress + public string Adress + { + get { - get - { - return _adr + SplitChar + _postfix; - } - set - { - Parse(value); - } + return _adr + SplitChar + _postfix; } + set + { + Parse(value); + } + } - private string _adr; - private string _postfix; + private string _adr; + private string _postfix; - public EMail() { } + public EMail() { } - public EMail(string adress, string postfix) - { - _adr = adress; - _postfix = postfix; - } + public EMail(string adress, string postfix) + { + _adr = adress; + _postfix = postfix; + } - private void Parse(string email) - { - string[] parsed = email.Split(SplitChar); + public EMail(string fullAdress) + { + Adress = fullAdress; + } + + private void Parse(string email) + { + string[] parsed = email.Split(SplitChar); - _adr = parsed[0]; - _postfix = parsed[1]; + _adr = parsed[0]; + _postfix = parsed[1]; - } + } - public static bool operator !=(EMail left, EMail right) => left.Adress != right.Adress; - public static bool operator ==(EMail left, EMail right) => left.Adress == right.Adress; + public static bool operator !=(EMail left, EMail right) => left.Adress != right.Adress; + public static bool operator ==(EMail left, EMail right) => left.Adress == right.Adress; - public override bool Equals(object obj) + public override bool Equals(object obj) + { + try + { + return this == (EMail)obj; + } + catch { - try - { - return this == (EMail)obj; - } - catch - { - return false; - } + return false; } + } - public override int GetHashCode() => base.GetHashCode(); + public override int GetHashCode() => base.GetHashCode(); - public override string ToString() => Adress; - } + public override string ToString() => Adress; } diff --git a/PasswordManager/Model/DB/Schema/Profile.cs b/PasswordManager/Model/DB/Schema/Profile.cs index 737c8ebd..7104a540 100644 --- a/PasswordManager/Model/DB/Schema/Profile.cs +++ b/PasswordManager/Model/DB/Schema/Profile.cs @@ -1,68 +1,64 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using System.Text; -namespace PasswordManager.Model.DB.Schema +namespace PasswordManager.Model.DB.Schema; + +public class Profile : AProfile { - public class Profile : AProfile - { - private const char FieldSplit = ':'; - private const char ProfileSplit = ';'; - private const string NullMessage = "null"; + private const char FieldSplit = ':'; + private const char ProfileSplit = ';'; + private const string NullMessage = "null"; - public string Service { get { return service; } set { service = value; } } - public EMail Email { get { return eMail; } set { eMail = value; } } - public string Password { get { return password; } set { password = value; } } - public string Username { get { return username; } set { username = value; } } + public int ID { get; set; } + public string Service { get { return service; } set { service = value; } } + public string Username { get { return username; } set { username = value; } } + public string Password { get { return password; } set { password = value; } } - public Profile() { } + public Profile() { } - public override string ToString() - { - StringBuilder ret = new StringBuilder(); + public override string ToString() + { + StringBuilder ret = new StringBuilder(); - ret.Append(Service ?? NullMessage).Append(FieldSplit); - ret.Append(Email).Append(FieldSplit); - ret.Append(Password ?? NullMessage).Append(FieldSplit); - ret.Append(Username ?? NullMessage).Append(ProfileSplit); + ret.Append(Service ?? NullMessage).Append(FieldSplit); + ret.Append(Username ?? NullMessage).Append(FieldSplit); + ret.Append(Password ?? NullMessage).Append(ProfileSplit); - return ret.ToString(); - } + return ret.ToString(); + } - public override bool Equals([AllowNull] Profile other) - { - bool[] equals = { Service == other.Service, Email == other.Email, Password == other.Password, Username == other.username }; + public override bool Equals([AllowNull] Profile other) + { + bool[] equals = { Service == other.Service, Password == other.Password, Username == other.username }; - return equals[0] & equals[1] & equals[2] & equals[3]; - } + return equals[0] & equals[1] & equals[2] & equals[3]; + } - public static bool operator !=(Profile left, Profile right) - { - return !left.Equals(right); - } + public static bool operator !=(Profile left, Profile right) + { + return !left.Equals(right); + } + + public static bool operator ==(Profile left, Profile right) + { + return left.Equals(right); + } - public static bool operator ==(Profile left, Profile right) + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) { - return left.Equals(right); + return true; } - public override bool Equals(object obj) + if (ReferenceEquals(obj, null)) { - if (ReferenceEquals(this, obj)) - { - return true; - } - - if (ReferenceEquals(obj, null)) - { - return false; - } - - return Equals(obj as Profile); + return false; } - public override int GetHashCode() => base.GetHashCode(); - + return Equals(obj as Profile); } + + public override int GetHashCode() => base.GetHashCode(); + } diff --git a/PasswordManager/Model/IO/AppDirectoryManager.cs b/PasswordManager/Model/IO/AppDirectoryManager.cs new file mode 100644 index 00000000..eb42e758 --- /dev/null +++ b/PasswordManager/Model/IO/AppDirectoryManager.cs @@ -0,0 +1,22 @@ +namespace PasswordManager.Model.IO; + +public static class AppDirectoryManager +{ + private static string appName = "PasswordManager"; + private static string data = "data"; + + public static string AppData { get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), appName); } } + public static string Data { get { return Path.Combine(AppData, data); } } + + public static async Task Initialize() + { + ensureDirectoruCreated(AppData); + ensureDirectoruCreated(Data); + } + + private static void ensureDirectoruCreated(string path) + { + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + } +} diff --git a/PasswordManager/Model/RecentModel.cs b/PasswordManager/Model/RecentModel.cs index 1af2844e..521b55a5 100644 --- a/PasswordManager/Model/RecentModel.cs +++ b/PasswordManager/Model/RecentModel.cs @@ -1,40 +1,13 @@ using PasswordManager.Model.DB; using PasswordManager.Model.DB.Schema; -using System.Collections.Generic; -using System.Collections.ObjectModel; -namespace PasswordManager.Model +namespace PasswordManager.Model; + +public class RecentModel { - public class RecentModel + public async Task> getProfiles() { - public async Task> getProfiles() - { - PasswordController.SavePasswords(new List(){ new Profile() - { - Service = "steam", - Email = new EMail() - { - Adress = $"dataFormDb@fda.td" - }, - Password = $"pswDb", - Username = "RelM" - } }.ToArray()); - - List profiles = await PasswordController.SearhProfiles("Service LIKE 'steam'") ?? new(); - for (int i = 0; i < 20; i++) - { - profiles.Add(new Profile() - { - Service = "steam", - Email = new EMail() - { - Adress = $"test{i}@fda.td" - }, - Password = $"psw{i}{Random.Shared.Next(0, i + 32)}", - Username = null - }); - } - return profiles; - } + List profiles = PasswordController.SearhProfiles(x => x.Service == "steam") ?? new(); + return profiles; } } diff --git a/PasswordManager/PasswordManager.csproj b/PasswordManager/PasswordManager.csproj index 9b1edc97..78f45d2a 100644 --- a/PasswordManager/PasswordManager.csproj +++ b/PasswordManager/PasswordManager.csproj @@ -7,6 +7,7 @@ Exe PasswordManager + Alpha 0.1.0 true true enable @@ -50,11 +51,15 @@ - + + + + MSBuild:Compile + MSBuild:Compile diff --git a/PasswordManager/PasswordManager.csproj.user b/PasswordManager/PasswordManager.csproj.user index 961af1e5..91458786 100644 --- a/PasswordManager/PasswordManager.csproj.user +++ b/PasswordManager/PasswordManager.csproj.user @@ -2,10 +2,10 @@ False - net7.0-windows10.0.19041.0 - Windows Machine + net7.0-android + Pixel 5 - API 33 (Android 13.0 - API 33) Emulator - pixel_2_r_11_0_-_api_30 + pixel_5_-_api_33 ProjectDebugger @@ -29,6 +29,9 @@ Designer + + Designer + Designer diff --git a/PasswordManager/View/AddPage.xaml b/PasswordManager/View/AddPage.xaml new file mode 100644 index 00000000..2cd85a0c --- /dev/null +++ b/PasswordManager/View/AddPage.xaml @@ -0,0 +1,23 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/PasswordManager/View/AddPage.xaml.cs b/PasswordManager/View/AddPage.xaml.cs new file mode 100644 index 00000000..941f1e94 --- /dev/null +++ b/PasswordManager/View/AddPage.xaml.cs @@ -0,0 +1,12 @@ +using PasswordManager.ViewModel; + +namespace PasswordManager.View; + +public partial class AddPage : ContentPage +{ + public AddPage(AddViewModel vm) + { + InitializeComponent(); + BindingContext = vm; + } +} \ No newline at end of file diff --git a/PasswordManager/View/RecentPage.xaml b/PasswordManager/View/RecentPage.xaml index 0b4ee6c0..cfff54db 100644 --- a/PasswordManager/View/RecentPage.xaml +++ b/PasswordManager/View/RecentPage.xaml @@ -8,7 +8,7 @@ Title="RecentPage"> -