Skip to content

Commit

Permalink
Merge pull request #7 from Cootz/dev
Browse files Browse the repository at this point in the history
Dev merge for version update
  • Loading branch information
Cootz authored Feb 3, 2023
2 parents de1e03f + 56a7fcc commit 0de1b3a
Show file tree
Hide file tree
Showing 29 changed files with 352 additions and 284 deletions.
8 changes: 6 additions & 2 deletions PasswordManager/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using PasswordManager.Model.DB;
using PasswordManager.Model.IO;

namespace PasswordManager
{
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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion PasswordManager/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:RecentPage}"
Route="RecentPage" />
Route="RecentPage"/>

</Shell>
3 changes: 2 additions & 1 deletion PasswordManager/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public AppShell()
{
InitializeComponent();

Routing.RegisterRoute(nameof(RecentPage), typeof(RecentPage));
//Register routes
Routing.RegisterRoute(nameof(AddPage), typeof(AddPage));
}
}
}
4 changes: 4 additions & 0 deletions PasswordManager/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ public static MauiApp CreateMauiApp()
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

//Pass viewmodel into page using Singleton/Transient
builder.Services.AddSingleton<RecentPage>();
builder.Services.AddSingleton<RecentViewModel>();

builder.Services.AddTransient<AddPage>();
builder.Services.AddTransient<AddViewModel>();

#if DEBUG
builder.Logging.AddDebug();
#endif
Expand Down
96 changes: 26 additions & 70 deletions PasswordManager/Model/DB/DBController.cs
Original file line number Diff line number Diff line change
@@ -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<DataSet> 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<Profile> 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<DataSet> Select(string condition)
{
DataSet dataSet = await CreateCommand("SELECT * FROM Profiles " +
"WHERE " + condition + ";");
public IEnumerable<Profile> Select(Func<Profile, bool> predicate)
{
return Profiles.Where(predicate);
}

return dataSet;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(Connection);
}
}
19 changes: 6 additions & 13 deletions PasswordManager/Model/DB/IController.cs
Original file line number Diff line number Diff line change
@@ -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<DataSet> Select(string condition);
public Task Add(Profile profile);
}
public Task Initialize();
public IEnumerable<Profile> Select(Func<Profile, bool> predicate);
public Task Add(Profile profile);
}
68 changes: 15 additions & 53 deletions PasswordManager/Model/DB/PasswordController.cs
Original file line number Diff line number Diff line change
@@ -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<List<Profile>> SearhProfiles(string keyWord)
public static async void SavePasswords(Profile[] data)
{
foreach (Profile prof in data)
{
List<Profile> profiles = new List<Profile>();

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<Profile> SearhProfiles(Func<Profile, bool> predicate) => DB.Select(predicate).ToList();
}
19 changes: 8 additions & 11 deletions PasswordManager/Model/DB/Schema/AProfile.cs
Original file line number Diff line number Diff line change
@@ -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<Profile>
{
public abstract class AProfile : IEquatable<Profile>
{
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);
}
90 changes: 46 additions & 44 deletions PasswordManager/Model/DB/Schema/EMail.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 0de1b3a

Please sign in to comment.