diff --git a/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj b/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj index 58462d3e..ef1c05cd 100644 --- a/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj +++ b/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj @@ -94,6 +94,9 @@ + + Assets\database.db + diff --git a/AideDeJeu/AideDeJeu.Android/NativeAPI.cs b/AideDeJeu/AideDeJeu.Android/NativeAPI.cs index dc4c2503..903ab93e 100644 --- a/AideDeJeu/AideDeJeu.Android/NativeAPI.cs +++ b/AideDeJeu/AideDeJeu.Android/NativeAPI.cs @@ -29,33 +29,36 @@ namespace AideDeJeu.Droid public string GetDatabasePath(string databaseName) { - var documentsDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); - var path = Path.Combine(documentsDirectoryPath, databaseName); + string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); + string dbPath = Path.Combine(path, databaseName); + return dbPath; + //var documentsDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); + //var path = Path.Combine(documentsDirectoryPath, databaseName); - // This is where we copy in our pre-created database - if (!File.Exists(path)) - { - using (var inStream = Android.App.Application.Context.Assets.Open(databaseName)) - { - using (var outStream = new FileStream(path, FileMode.Create)) - { - inStream.CopyTo(outStream); - } - } - //using (var binaryReader = new BinaryReader(Android.App.Application.Context.Assets.Open(databaseName))) - //{ - // using (var binaryWriter = new BinaryWriter(new FileStream(path, FileMode.Create))) - // { - // byte[] buffer = new byte[2048]; - // int length = 0; - // while ((length = binaryReader.Read(buffer, 0, buffer.Length)) > 0) - // { - // binaryWriter.Write(buffer, 0, length); - // } - // } - //} - } - return path; + //// This is where we copy in our pre-created database + //if (!File.Exists(path)) + //{ + // using (var inStream = Android.App.Application.Context.Assets.Open(databaseName)) + // { + // using (var outStream = new FileStream(path, FileMode.Create)) + // { + // inStream.CopyTo(outStream); + // } + // } + // //using (var binaryReader = new BinaryReader(Android.App.Application.Context.Assets.Open(databaseName))) + // //{ + // // using (var binaryWriter = new BinaryWriter(new FileStream(path, FileMode.Create))) + // // { + // // byte[] buffer = new byte[2048]; + // // int length = 0; + // // while ((length = binaryReader.Read(buffer, 0, buffer.Length)) > 0) + // // { + // // binaryWriter.Write(buffer, 0, length); + // // } + // // } + // //} + //} + //return path; } } } \ No newline at end of file diff --git a/AideDeJeu/AideDeJeu.GTK/app.config b/AideDeJeu/AideDeJeu.GTK/app.config new file mode 100644 index 00000000..489867c1 --- /dev/null +++ b/AideDeJeu/AideDeJeu.GTK/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj b/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj index bf7f1c98..d2cf0370 100644 --- a/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj +++ b/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj @@ -109,6 +109,9 @@ Designer + + database.db + diff --git a/AideDeJeu/AideDeJeu.iOS/AideDeJeu.iOS.csproj b/AideDeJeu/AideDeJeu.iOS/AideDeJeu.iOS.csproj index 451612dc..baa982a6 100644 --- a/AideDeJeu/AideDeJeu.iOS/AideDeJeu.iOS.csproj +++ b/AideDeJeu/AideDeJeu.iOS/AideDeJeu.iOS.csproj @@ -90,6 +90,9 @@ + + Resources\database.db + diff --git a/AideDeJeu/AideDeJeu.iOS/NativeAPI.cs b/AideDeJeu/AideDeJeu.iOS/NativeAPI.cs index be76b862..059e1122 100644 --- a/AideDeJeu/AideDeJeu.iOS/NativeAPI.cs +++ b/AideDeJeu/AideDeJeu.iOS/NativeAPI.cs @@ -24,8 +24,9 @@ namespace AideDeJeu.Droid public string GetDatabasePath(string databaseName) { - var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", databaseName); - return databasePath; + return NSBundle.MainBundle.PathForResource("database", "db"); + //var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", databaseName); + //return databasePath; } } } \ No newline at end of file diff --git a/AideDeJeu/AideDeJeu/ViewModels/StoreViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/StoreViewModel.cs index 60d81e2e..4adeba75 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/StoreViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/StoreViewModel.cs @@ -4,6 +4,7 @@ using Markdig; using Markdig.Parsers; using Markdig.Syntax; using Markdig.Syntax.Inlines; +using SQLite; using System; using System.Collections.Generic; using System.Linq; @@ -11,6 +12,7 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; +using Xamarin.Forms; namespace AideDeJeu.ViewModels { @@ -360,8 +362,60 @@ namespace AideDeJeu.ViewModels } } + + + + public class TodoItemDatabase + { + readonly SQLiteAsyncConnection database; + + public TodoItemDatabase(string dbPath) + { + database = new SQLiteAsyncConnection(dbPath); + database.CreateTableAsync().Wait(); + } + + public Task> GetItemsAsync() + { + return database.Table().ToListAsync(); + } + + public Task> GetItemsNotDoneAsync() + { + return database.QueryAsync("SELECT * FROM [Item]"); + } + + public Task GetItemAsync(string id) + { + return database.Table().Where(i => i.Id == id).FirstOrDefaultAsync(); + } + + public Task SaveItemAsync(Item item) + { + if (!string.IsNullOrEmpty(item.Id)) + { + return database.UpdateAsync(item); + } + else + { + return database.InsertAsync(item); + } + } + + public Task DeleteItemAsync(Item item) + { + return database.DeleteAsync(item); + } + } public async Task GetItemFromDataAsync(string source, string anchor) { + SQLiteAsyncConnection database; + var dbPath = DependencyService.Get().GetDatabasePath("database.db"); + database = new SQLiteAsyncConnection(dbPath, SQLiteOpenFlags.ReadOnly); + return null; + var it = await database.Table().ToListAsync(); + + var id = $"{source}.md#{anchor}"; //await Task.Delay(3000); if (!_AllItems.ContainsKey(id) && !_AllItems.ContainsKey(source)) diff --git a/AideDeJeu/AideDeJeuCmd/AideDeJeuCmd.csproj b/AideDeJeu/AideDeJeuCmd/AideDeJeuCmd.csproj index d1ef690a..28b0ad6a 100644 --- a/AideDeJeu/AideDeJeuCmd/AideDeJeuCmd.csproj +++ b/AideDeJeu/AideDeJeuCmd/AideDeJeuCmd.csproj @@ -6,6 +6,14 @@ latest + + + + + + + + diff --git a/AideDeJeu/AideDeJeuCmd/MockXamarin.cs b/AideDeJeu/AideDeJeuCmd/MockXamarin.cs new file mode 100644 index 00000000..7ae173cb --- /dev/null +++ b/AideDeJeu/AideDeJeuCmd/MockXamarin.cs @@ -0,0 +1,204 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Xamarin.Forms; +using Xamarin.Forms.Internals; + +namespace Tests.Xamarin.Forms.Mocks +{ + public static class MockForms + { + public static void Init() + { + Device.Info = new MockDeviceInfo(); + Device.PlatformServices = new MockPlatformServices(); + + DependencyService.Register(); + DependencyService.Register(); + } + + internal class MockPlatformServices : IPlatformServices + { + Action _invokeOnMainThread; + Action _openUriAction; + Func> _getStreamAsync; + + public MockPlatformServices(Action invokeOnMainThread = null, Action openUriAction = null, Func> getStreamAsync = null) + { + _invokeOnMainThread = invokeOnMainThread; + _openUriAction = openUriAction; + _getStreamAsync = getStreamAsync; + } + + public string GetMD5Hash(string input) + { + throw new NotImplementedException(); + } + static int hex(int v) + { + if (v < 10) + return '0' + v; + return 'a' + v - 10; + } + + public double GetNamedSize(NamedSize size, Type targetElement, bool useOldSizes) + { + switch (size) + { + case NamedSize.Default: + return 10; + case NamedSize.Micro: + return 4; + case NamedSize.Small: + return 8; + case NamedSize.Medium: + return 12; + case NamedSize.Large: + return 16; + default: + throw new ArgumentOutOfRangeException("size"); + } + } + + public void OpenUriAction(Uri uri) + { + if (_openUriAction != null) + _openUriAction(uri); + else + throw new NotImplementedException(); + } + + public bool IsInvokeRequired + { + get { return false; } + } + + public string RuntimePlatform { get; set; } + + public void BeginInvokeOnMainThread(Action action) + { + if (_invokeOnMainThread == null) + action(); + else + _invokeOnMainThread(action); + } + + public Ticker CreateTicker() + { + return new MockTicker(); + } + + public void StartTimer(TimeSpan interval, Func callback) + { + Timer timer = null; + TimerCallback onTimeout = o => BeginInvokeOnMainThread(() => { + if (callback()) + return; + + timer.Dispose(); + }); + timer = new Timer(onTimeout, null, interval, interval); + } + + public Task GetStreamAsync(Uri uri, CancellationToken cancellationToken) + { + if (_getStreamAsync == null) + throw new NotImplementedException(); + return _getStreamAsync(uri, cancellationToken); + } + + public Assembly[] GetAssemblies() + { + return new Assembly[0]; + } + + public IIsolatedStorageFile GetUserStoreForApplication() + { + throw new NotImplementedException(); + } + + public void QuitApplication() + { + //throw new NotImplementedException(); + } + } + + internal class MockDeserializer : IDeserializer + { + public Task> DeserializePropertiesAsync() + { + return Task.FromResult>(new Dictionary()); + } + + public Task SerializePropertiesAsync(IDictionary properties) + { + return Task.FromResult(false); + } + } + + internal class MockResourcesProvider : ISystemResourcesProvider + { + public IResourceDictionary GetSystemResources() + { + var dictionary = new ResourceDictionary(); + Style style; + style = new Style(typeof(Label)); + dictionary[Device.Styles.BodyStyleKey] = style; + + style = new Style(typeof(Label)); + style.Setters.Add(Label.FontSizeProperty, 50); + dictionary[Device.Styles.TitleStyleKey] = style; + + style = new Style(typeof(Label)); + style.Setters.Add(Label.FontSizeProperty, 40); + dictionary[Device.Styles.SubtitleStyleKey] = style; + + style = new Style(typeof(Label)); + style.Setters.Add(Label.FontSizeProperty, 30); + dictionary[Device.Styles.CaptionStyleKey] = style; + + style = new Style(typeof(Label)); + style.Setters.Add(Label.FontSizeProperty, 20); + dictionary[Device.Styles.ListItemTextStyleKey] = style; + + style = new Style(typeof(Label)); + style.Setters.Add(Label.FontSizeProperty, 10); + dictionary[Device.Styles.ListItemDetailTextStyleKey] = style; + + return dictionary; + } + } + + internal class MockTicker : Ticker + { + bool _enabled; + + protected override void EnableTimer() + { + _enabled = true; + + while (_enabled) + { + SendSignals(16); + } + } + + protected override void DisableTimer() + { + _enabled = false; + } + } + + internal class MockDeviceInfo : DeviceInfo + { + public override Size PixelScreenSize => throw new NotImplementedException(); + + public override Size ScaledScreenSize => throw new NotImplementedException(); + + public override double ScalingFactor => throw new NotImplementedException(); + } + } +} diff --git a/AideDeJeu/AideDeJeuCmd/NativeAPI.cs b/AideDeJeu/AideDeJeuCmd/NativeAPI.cs new file mode 100644 index 00000000..7bb93fc8 --- /dev/null +++ b/AideDeJeu/AideDeJeuCmd/NativeAPI.cs @@ -0,0 +1,30 @@ +using AideDeJeu.Tools; +using System.IO; +using System.Reflection; + +[assembly: Xamarin.Forms.Dependency(typeof(AideDeJeu.Cmd.Version_CMD))] +namespace AideDeJeu.Cmd +{ + public class Version_CMD : INativeAPI + { + public string GetVersion() + { + //Package package = Package.Current; + //PackageId packageId = package.Id; + //PackageVersion version = packageId.Version; + + //return string.Format("{0}.{1}", version.Major, version.Minor); + return ""; + } + + public int GetBuild() + { + return 0; + } + + public string GetDatabasePath(string databaseName) + { + return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), databaseName); + } + } +} \ No newline at end of file diff --git a/AideDeJeu/AideDeJeuCmd/Program.cs b/AideDeJeu/AideDeJeuCmd/Program.cs index 1f08aae3..15dd1287 100644 --- a/AideDeJeu/AideDeJeuCmd/Program.cs +++ b/AideDeJeu/AideDeJeuCmd/Program.cs @@ -290,6 +290,11 @@ namespace AideDeJeuCmd static async Task Main(string[] args) { + Tests.Xamarin.Forms.Mocks.MockForms.Init(); + DependencyService.Register(); + var store = new StoreViewModel(); + await store.GetItemFromDataAsync("test", "truc"); + return; await ReorderSpellsAsync(); return; string dataDir = @"..\..\..\..\..\Data\"; diff --git a/AideDeJeu/AideDeJeuUnitTest/AideDeJeuUnitTest.csproj b/AideDeJeu/AideDeJeuUnitTest/AideDeJeuUnitTest.csproj index 4cb9b66b..afd2b4df 100644 --- a/AideDeJeu/AideDeJeuUnitTest/AideDeJeuUnitTest.csproj +++ b/AideDeJeu/AideDeJeuUnitTest/AideDeJeuUnitTest.csproj @@ -10,6 +10,7 @@ + diff --git a/AideDeJeu/AideDeJeuWeb/AideDeJeuWeb.csproj b/AideDeJeu/AideDeJeuWeb/AideDeJeuWeb.csproj index 62c48716..c48d81eb 100644 --- a/AideDeJeu/AideDeJeuWeb/AideDeJeuWeb.csproj +++ b/AideDeJeu/AideDeJeuWeb/AideDeJeuWeb.csproj @@ -10,6 +10,7 @@ + diff --git a/Data/database.db b/Data/database.db new file mode 100644 index 00000000..9731b320 Binary files /dev/null and b/Data/database.db differ