mirror of
https://github.com/Nioux/AideDeJeu.git
synced 2025-11-02 16:27:04 +00:00
Async du get database path pour permettre création / copie async
This commit is contained in:
parent
7d72713cd4
commit
f1a726e6b1
9 changed files with 96 additions and 124 deletions
|
|
@ -2,6 +2,7 @@
|
|||
using Android.Content.PM;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[assembly: Xamarin.Forms.Dependency(typeof(AideDeJeu.Droid.Version_Android))]
|
||||
namespace AideDeJeu.Droid
|
||||
|
|
@ -27,7 +28,7 @@ namespace AideDeJeu.Droid
|
|||
return info.VersionCode;
|
||||
}
|
||||
|
||||
public string GetDatabasePath(string databaseName)
|
||||
public async Task<string> GetDatabasePathAsync(string databaseName)
|
||||
{
|
||||
//string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
||||
//string dbPath = Path.Combine(path, databaseName);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using AideDeJeu.Tools;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
[assembly: Xamarin.Forms.Dependency(typeof(AideDeJeu.UWP.Version_UWP))]
|
||||
|
|
@ -22,7 +23,7 @@ namespace AideDeJeu.UWP
|
|||
return 0;
|
||||
}
|
||||
|
||||
public string GetDatabasePath(string databaseName)
|
||||
public async Task<string> GetDatabasePathAsync(string databaseName)
|
||||
{
|
||||
var documentsDirectoryPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
|
||||
var path = Path.Combine(documentsDirectoryPath, databaseName);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AideDeJeu.Tools
|
||||
{
|
||||
|
|
@ -8,6 +9,6 @@ namespace AideDeJeu.Tools
|
|||
{
|
||||
string GetVersion();
|
||||
int GetBuild();
|
||||
string GetDatabasePath(string databaseName);
|
||||
Task<string> GetDatabasePathAsync(string databaseName);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ namespace AideDeJeu.ViewModels
|
|||
|
||||
public async Task<IEnumerable<SearchedItem>> DeepSearchAllItemsAsync(string searchText)
|
||||
{
|
||||
using (var context = new StoreViewModel.AideDeJeuContext())
|
||||
using (var context = await StoreViewModel.GetDatabaseContextAsync())
|
||||
{
|
||||
var primary = await context.Items.
|
||||
Where(item => item.Name.Contains(searchText)).
|
||||
|
|
|
|||
|
|
@ -310,8 +310,6 @@ namespace AideDeJeu.ViewModels
|
|||
public class VOSpellFilterViewModel : SpellFilterViewModel
|
||||
{
|
||||
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var levelComparer = new LevelComparer();
|
||||
var classe = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Class).SelectedKey ?? "";
|
||||
|
|
@ -320,7 +318,7 @@ namespace AideDeJeu.ViewModels
|
|||
var ecole = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.School).SelectedKey ?? "";
|
||||
var ritual = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Ritual).SelectedKey ?? "";
|
||||
var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? "";
|
||||
using (var context = new StoreViewModel.AideDeJeuContext())
|
||||
using (var context = await StoreViewModel.GetDatabaseContextAsync())
|
||||
{
|
||||
return context.SpellsVO.Where(spell =>
|
||||
levelComparer.Compare(spell.Level, niveauMin) >= 0 &&
|
||||
|
|
@ -334,7 +332,6 @@ namespace AideDeJeu.ViewModels
|
|||
(Helpers.RemoveDiacritics(spell.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
|
||||
)).OrderBy(spell => spell.Name).ToList();
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
public override List<KeyValuePair<string, string>> Classes { get; } = new List<KeyValuePair<string, string>>()
|
||||
|
|
@ -393,8 +390,6 @@ namespace AideDeJeu.ViewModels
|
|||
public class HDSpellFilterViewModel : SpellFilterViewModel
|
||||
{
|
||||
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var levelComparer = new LevelComparer();
|
||||
var classe = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Class).SelectedKey ?? "";
|
||||
|
|
@ -403,7 +398,7 @@ namespace AideDeJeu.ViewModels
|
|||
var ecole = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.School).SelectedKey ?? "";
|
||||
var ritual = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Ritual).SelectedKey ?? "";
|
||||
var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? "";
|
||||
using (var context = new StoreViewModel.AideDeJeuContext())
|
||||
using (var context = await StoreViewModel.GetDatabaseContextAsync())
|
||||
{
|
||||
return context.SpellsHD.Where(spell =>
|
||||
levelComparer.Compare(spell.Level, niveauMin) >= 0 &&
|
||||
|
|
@ -417,7 +412,6 @@ namespace AideDeJeu.ViewModels
|
|||
(Helpers.RemoveDiacritics(spell.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
|
||||
)).OrderBy(spell => spell.Name).ToList();
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
public override List<KeyValuePair<string, string>> Classes { get; } = new List<KeyValuePair<string, string>>()
|
||||
|
|
@ -655,8 +649,6 @@ namespace AideDeJeu.ViewModels
|
|||
public class VOMonsterFilterViewModel : MonsterFilterViewModel
|
||||
{
|
||||
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var powerComparer = new PowerComparer();
|
||||
|
||||
|
|
@ -667,7 +659,7 @@ namespace AideDeJeu.ViewModels
|
|||
var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? "";
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
using (var context = new StoreViewModel.AideDeJeuContext())
|
||||
using (var context = await StoreViewModel.GetDatabaseContextAsync())
|
||||
{
|
||||
return context.MonstersVO.Where(monster =>
|
||||
monster != null &&
|
||||
|
|
@ -680,9 +672,8 @@ namespace AideDeJeu.ViewModels
|
|||
(Helpers.RemoveDiacritics(monster.Name).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower())) ||
|
||||
(Helpers.RemoveDiacritics(monster.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
|
||||
)
|
||||
).OrderBy(monster => monster.Name).ToListAsync();
|
||||
).OrderBy(monster => monster.Name).ToList();
|
||||
}
|
||||
}, token);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -774,8 +765,6 @@ namespace AideDeJeu.ViewModels
|
|||
public class HDMonsterFilterViewModel : MonsterFilterViewModel
|
||||
{
|
||||
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var powerComparer = new PowerComparer();
|
||||
|
||||
|
|
@ -786,7 +775,7 @@ namespace AideDeJeu.ViewModels
|
|||
var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? "";
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
using (var context = new StoreViewModel.AideDeJeuContext())
|
||||
using (var context = await StoreViewModel.GetDatabaseContextAsync())
|
||||
{
|
||||
return context.MonstersHD.Where(monster =>
|
||||
monster != null &&
|
||||
|
|
@ -799,9 +788,8 @@ namespace AideDeJeu.ViewModels
|
|||
(Helpers.RemoveDiacritics(monster.Name).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower())) ||
|
||||
(Helpers.RemoveDiacritics(monster.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
|
||||
)
|
||||
).OrderBy(monster => monster.Name).ToListAsync();
|
||||
).OrderBy(monster => monster.Name).ToList();
|
||||
}
|
||||
}, token);
|
||||
|
||||
}
|
||||
public override List<KeyValuePair<string, string>> Categories { get; } = new List<KeyValuePair<string, string>>()
|
||||
|
|
@ -914,15 +902,13 @@ namespace AideDeJeu.ViewModels
|
|||
}
|
||||
|
||||
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var priceComparer = new PriceComparer();
|
||||
var type = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Type).SelectedKey ?? "";
|
||||
var minPrice = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MinPrice).SelectedKey ?? "0 pc";
|
||||
var maxPrice = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MaxPrice).SelectedKey ?? "1 000 000 po";
|
||||
|
||||
using (var context = new AideDeJeuContext())
|
||||
using (var context = await GetDatabaseContextAsync())
|
||||
{
|
||||
return context.Equipments.Where(equipment =>
|
||||
equipment.Type.ToLower().Contains(type.ToLower()) &&
|
||||
|
|
@ -933,22 +919,8 @@ namespace AideDeJeu.ViewModels
|
|||
(Helpers.RemoveDiacritics(equipment.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
|
||||
)
|
||||
).OrderBy(eq => eq.Name)
|
||||
.ToListAsync();
|
||||
.ToList();
|
||||
}
|
||||
//return items.Where(item =>
|
||||
//{
|
||||
// var equipment = item as Equipment;
|
||||
// return equipment.Type.ToLower().Contains(type.ToLower()) &&
|
||||
// priceComparer.Compare(equipment.Price, minPrice) >= 0 &&
|
||||
// priceComparer.Compare(equipment.Price, maxPrice) <= 0 &&
|
||||
// (
|
||||
// (Helpers.RemoveDiacritics(equipment.Name).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower())) ||
|
||||
// (Helpers.RemoveDiacritics(equipment.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
|
||||
// );
|
||||
//}).OrderBy(eq => eq.Name)
|
||||
// .AsEnumerable();
|
||||
}, token);
|
||||
|
||||
}
|
||||
|
||||
public abstract List<KeyValuePair<string, string>> Types { get; }
|
||||
|
|
@ -1030,36 +1002,21 @@ namespace AideDeJeu.ViewModels
|
|||
}
|
||||
|
||||
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var priceComparer = new PriceComparer();
|
||||
var type = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Type).SelectedKey ?? "";
|
||||
var rarity = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Rarity).SelectedKey ?? "";
|
||||
var attunement = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Attunement).SelectedKey ?? "";
|
||||
|
||||
using (var context = new AideDeJeuContext())
|
||||
using (var context = await GetDatabaseContextAsync())
|
||||
{
|
||||
return context.MagicItems.Where(magicitem =>
|
||||
MatchContains(magicitem.Type, type) &&
|
||||
MatchContains(magicitem.Rarity, rarity) &&
|
||||
MatchContains(magicitem.Attunement, attunement) &&
|
||||
MatchSearch(magicitem)
|
||||
).OrderBy(eq => eq.Name).ToListAsync();
|
||||
).OrderBy(eq => eq.Name).ToList();
|
||||
}
|
||||
|
||||
//return items.Where(item =>
|
||||
//{
|
||||
// var magicitem = item as MagicItem;
|
||||
// if (magicitem == null) return false;
|
||||
// var matchType = MatchContains(magicitem.Type, type);
|
||||
// var matchRarity = MatchContains(magicitem.Rarity, rarity);
|
||||
// var matchAttunement = MatchContains(magicitem.Attunement, attunement);
|
||||
// var matchSearch = MatchSearch(magicitem);
|
||||
// return matchType && matchRarity && matchAttunement && matchSearch;
|
||||
//}).OrderBy(eq => eq.Name).AsEnumerable();
|
||||
}, token);
|
||||
|
||||
}
|
||||
|
||||
public abstract List<KeyValuePair<string, string>> Types { get; }
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ namespace AideDeJeu.ViewModels
|
|||
{
|
||||
if (Filter != null)
|
||||
{
|
||||
var items = await Filter.FilterItems(await AllItems.GetChildrenAsync(), cancellationToken: cancellationToken);
|
||||
var items = await Task.Run(async() => await Filter.FilterItems(await AllItems.GetChildrenAsync(), cancellationToken: cancellationToken));
|
||||
Items = new Item(items.ToList());
|
||||
Children = items;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -366,6 +366,7 @@ namespace AideDeJeu.ViewModels
|
|||
|
||||
public class AideDeJeuContext : DbContext
|
||||
{
|
||||
public string DbPath { get; set; }
|
||||
public DbSet<Item> Items { get; set; }
|
||||
public DbSet<Equipment> Equipments { get; set; }
|
||||
public DbSet<MagicItem> MagicItems { get; set; }
|
||||
|
|
@ -376,10 +377,14 @@ namespace AideDeJeu.ViewModels
|
|||
public DbSet<SpellVO> SpellsVO { get; set; }
|
||||
public DbSet<MonsterVO> MonstersVO { get; set; }
|
||||
|
||||
public AideDeJeuContext(string dbPath)
|
||||
{
|
||||
this.DbPath = dbPath;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var dbPath = DependencyService.Get<INativeAPI>().GetDatabasePath("database.db");
|
||||
optionsBuilder.UseSqlite($"Data Source='{dbPath}'");
|
||||
optionsBuilder.UseSqlite($"Data Source='{DbPath}'");
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
|
|
@ -394,6 +399,12 @@ namespace AideDeJeu.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
public static async Task<AideDeJeuContext> GetDatabaseContextAsync()
|
||||
{
|
||||
var dbPath = await DependencyService.Get<INativeAPI>().GetDatabasePathAsync("database.db");
|
||||
return new AideDeJeuContext(dbPath);
|
||||
}
|
||||
|
||||
public async Task<Item> GetItemFromDataAsync(string source, string anchor)
|
||||
{
|
||||
var id = $"{source}.md".TrimStart('/');
|
||||
|
|
@ -401,7 +412,7 @@ namespace AideDeJeu.ViewModels
|
|||
{
|
||||
id += $"#{anchor}";
|
||||
}
|
||||
using (var context = new AideDeJeuContext())
|
||||
using (var context = await GetDatabaseContextAsync())
|
||||
{
|
||||
return await context.Items.Where(item => item.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using AideDeJeu.Tools;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[assembly: Xamarin.Forms.Dependency(typeof(AideDeJeu.Cmd.Version_CMD))]
|
||||
namespace AideDeJeu.Cmd
|
||||
|
|
@ -22,7 +23,7 @@ namespace AideDeJeu.Cmd
|
|||
return 0;
|
||||
}
|
||||
|
||||
public string GetDatabasePath(string databaseName)
|
||||
public async Task<string> GetDatabasePathAsync(string databaseName)
|
||||
{
|
||||
return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"..\..\..\..\..\Data\" + databaseName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -300,7 +300,7 @@ namespace AideDeJeuCmd
|
|||
var store = new StoreViewModel();
|
||||
await store.PreloadAllItemsAsync();
|
||||
|
||||
using (var context = new StoreViewModel.AideDeJeuContext())
|
||||
using (var context = await StoreViewModel.GetDatabaseContextAsync())
|
||||
{
|
||||
await context.Database.EnsureDeletedAsync();
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue