mirror of
				https://github.com/Nioux/AideDeJeu.git
				synced 2025-10-30 23:16:09 +00:00 
			
		
		
		
	Modifs
This commit is contained in:
		
							parent
							
								
									b63f39af8c
								
							
						
					
					
						commit
						bfab72f063
					
				
					 3 changed files with 209 additions and 119 deletions
				
			
		
							
								
								
									
										74
									
								
								aidedejeu_flutter/lib/database.dart
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								aidedejeu_flutter/lib/database.dart
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,74 @@ | ||||||
|  | import 'dart:io'; | ||||||
|  | 
 | ||||||
|  | import 'package:aidedejeu_flutter/models/items.dart'; | ||||||
|  | import 'package:flutter/services.dart'; | ||||||
|  | import 'package:path/path.dart'; | ||||||
|  | import 'package:sqflite/sqflite.dart'; | ||||||
|  | 
 | ||||||
|  | Database _database; | ||||||
|  | 
 | ||||||
|  | Future<Database> get database async { | ||||||
|  |   if (_database != null) return _database; | ||||||
|  |   _database = await getDatabaseInstance(); | ||||||
|  |   return _database; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<Database> getDatabaseInstance() async { | ||||||
|  |   var databasesPath = await getDatabasesPath(); | ||||||
|  |   var path = join(databasesPath, "library.db"); | ||||||
|  | 
 | ||||||
|  |   var exists = await databaseExists(path); | ||||||
|  |   if (!exists) { | ||||||
|  |     print("Creating new copy from asset"); | ||||||
|  | 
 | ||||||
|  |     try { | ||||||
|  |       await Directory(dirname(path)).create(recursive: true); | ||||||
|  |     } catch (_) {} | ||||||
|  | 
 | ||||||
|  |     ByteData data = await rootBundle.load(join("assets", "library.db")); | ||||||
|  |     List<int> bytes = | ||||||
|  |     data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); | ||||||
|  | 
 | ||||||
|  |     await File(path).writeAsBytes(bytes, flush: true); | ||||||
|  |   } else { | ||||||
|  |     print("Opening existing database"); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return await openDatabase(path, readOnly: true); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<Item> getItemWithId(String id) async { | ||||||
|  |   print("getItemWithId " + id); | ||||||
|  |   final db = await database; | ||||||
|  |   var response = await db | ||||||
|  |       .query("Items", where: "Id = ? OR RootId = ?", whereArgs: [id, id]); | ||||||
|  |   if (response.isEmpty) { | ||||||
|  |     print("Id not found"); | ||||||
|  |   } | ||||||
|  |   return response.isNotEmpty ? itemFromMap(response.first) : null; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<Item> loadChildrenItems(Item item) async { | ||||||
|  |   print("getChildrenItems " + item.Discriminator); | ||||||
|  |   if (item.Discriminator.endsWith("Items")) { | ||||||
|  |     String discriminator = | ||||||
|  |     item.Discriminator.substring(0, item.Discriminator.length - 1); | ||||||
|  |     String family = ""; | ||||||
|  |     if (item is MonsterItems) { | ||||||
|  |       family = (item as MonsterItems)?.Family ?? ""; | ||||||
|  |     } | ||||||
|  |     final db = await database; | ||||||
|  |     var response = await db | ||||||
|  |         .query("Items", where: "Discriminator = ? AND MonsterItem_Family = ?", | ||||||
|  |         whereArgs: [discriminator, family], | ||||||
|  |         orderBy: "NormalizedName"); | ||||||
|  |     if (response.isEmpty) { | ||||||
|  |       print("Id not found"); | ||||||
|  |     } | ||||||
|  |     item.Children = response.isNotEmpty | ||||||
|  |         ? itemsFromMapList(response) | ||||||
|  |         : null; | ||||||
|  |   } | ||||||
|  |   return item; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | @ -1,76 +1,12 @@ | ||||||
| import 'dart:io'; | import 'package:aidedejeu_flutter/database.dart'; | ||||||
| 
 |  | ||||||
| import 'package:aidedejeu_flutter/models/items.dart'; | import 'package:aidedejeu_flutter/models/items.dart'; | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:flutter/services.dart'; | import 'package:flutter/services.dart'; | ||||||
| import 'package:flutter_markdown/flutter_markdown.dart'; | import 'package:flutter_markdown/flutter_markdown.dart'; | ||||||
| import 'package:flutter_svg/flutter_svg.dart'; | import 'package:flutter_svg/flutter_svg.dart'; | ||||||
| import 'package:path/path.dart'; |  | ||||||
| import 'package:sqflite/sqflite.dart'; |  | ||||||
| 
 | 
 | ||||||
| void main() => runApp(MyApp()); | void main() => runApp(MyApp()); | ||||||
| 
 | 
 | ||||||
| Database _database; |  | ||||||
| 
 |  | ||||||
| Future<Database> get database async { |  | ||||||
|   if (_database != null) return _database; |  | ||||||
|   _database = await getDatabaseInstance(); |  | ||||||
|   return _database; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| Future<Database> getDatabaseInstance() async { |  | ||||||
|   var databasesPath = await getDatabasesPath(); |  | ||||||
|   var path = join(databasesPath, "library.db"); |  | ||||||
| 
 |  | ||||||
|   var exists = await databaseExists(path); |  | ||||||
|   if (!exists) { |  | ||||||
|     print("Creating new copy from asset"); |  | ||||||
| 
 |  | ||||||
|     try { |  | ||||||
|       await Directory(dirname(path)).create(recursive: true); |  | ||||||
|     } catch (_) {} |  | ||||||
| 
 |  | ||||||
|     ByteData data = await rootBundle.load(join("assets", "library.db")); |  | ||||||
|     List<int> bytes = |  | ||||||
|         data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); |  | ||||||
| 
 |  | ||||||
|     await File(path).writeAsBytes(bytes, flush: true); |  | ||||||
|   } else { |  | ||||||
|     print("Opening existing database"); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   return await openDatabase(path, readOnly: true); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| Future<Item> getItemWithId(String id) async { |  | ||||||
|   print("getItemWithId " + id); |  | ||||||
|   final db = await database; |  | ||||||
|   var response = await db |  | ||||||
|       .query("Items", where: "Id = ? OR RootId = ?", whereArgs: [id, id]); |  | ||||||
|   if (response.isEmpty) { |  | ||||||
|     print("Id not found"); |  | ||||||
|   } |  | ||||||
|   return response.isNotEmpty ? itemFromMap(response.first) : null; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| Future<Item> loadChildrenItems(Item item) async { |  | ||||||
|   print("getChildrenItems " + item.Discriminator); |  | ||||||
|   if (item.Discriminator.endsWith("Items")) { |  | ||||||
|     String discriminator = |  | ||||||
|         item.Discriminator.substring(0, item.Discriminator.length - 1); |  | ||||||
|     final db = await database; |  | ||||||
|     var response = await db |  | ||||||
|         .query("Items", where: "Discriminator = ?", whereArgs: [discriminator]); |  | ||||||
|     if (response.isEmpty) { |  | ||||||
|       print("Id not found"); |  | ||||||
|     } |  | ||||||
|     item.Children = response.isNotEmpty |  | ||||||
|         ? itemsFromMapList(response) |  | ||||||
|         : null; |  | ||||||
|   } |  | ||||||
|   return item; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| class MyApp extends StatelessWidget { | class MyApp extends StatelessWidget { | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|  | @ -132,9 +68,6 @@ class _MyHomePageState extends State<MyHomePage> { | ||||||
|         tableColumnWidth: IntrinsicColumnWidth(), |         tableColumnWidth: IntrinsicColumnWidth(), | ||||||
|         tableCellsPadding: EdgeInsets.all(0.2)); |         tableCellsPadding: EdgeInsets.all(0.2)); | ||||||
| 
 | 
 | ||||||
|     /*getItemWithId(this.id) |  | ||||||
|         .then((item) => loadChildrenItems(item).then((items) => setItem(item))) |  | ||||||
|         .catchError((error) => print(error));*/ |  | ||||||
|     loadItem().then((item) => setItem(item)); |     loadItem().then((item) => setItem(item)); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | @ -172,6 +105,7 @@ class _MyHomePageState extends State<MyHomePage> { | ||||||
|   Widget buildChildTile(BuildContext context, Item item) { |   Widget buildChildTile(BuildContext context, Item item) { | ||||||
|     return ListTile( |     return ListTile( | ||||||
|       title: Text(item.Name), |       title: Text(item.Name), | ||||||
|  |       subtitle: Text(item.AliasText ?? ""), | ||||||
|       onTap: () => Navigator.push( |       onTap: () => Navigator.push( | ||||||
|         context, |         context, | ||||||
|         MaterialPageRoute( |         MaterialPageRoute( | ||||||
|  | @ -180,65 +114,86 @@ class _MyHomePageState extends State<MyHomePage> { | ||||||
|       ), |       ), | ||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|   @override | 
 | ||||||
|   Widget build(BuildContext context) { |   Widget buildLibraryPage() { | ||||||
|     int count = 0; |     return Stack( | ||||||
|     if (item != null && item.Children != null) { |  | ||||||
|       count = item.Children.length; |  | ||||||
|     } |  | ||||||
|     final List<int> _listData = List<int>.generate(count + 1, (i) => i); |  | ||||||
|     return Scaffold( |  | ||||||
|       appBar: AppBar( |  | ||||||
|         title: Text(widget.id), |  | ||||||
|       ), |  | ||||||
|       body: //Container( |  | ||||||
|         //child: Column( |  | ||||||
|         Stack( |  | ||||||
|       children: <Widget>[ |       children: <Widget>[ | ||||||
|             //Text(markdown) |  | ||||||
|             item?.Children != null ? |  | ||||||
|         ListView.builder( |         ListView.builder( | ||||||
|               itemCount: item.Children.length + 1, |           itemCount: (item?.Children?.length ?? 0) + 1, | ||||||
|           itemBuilder: (BuildContext context, int index) { |           itemBuilder: (BuildContext context, int index) { | ||||||
|             return index == 0 ? |             return index == 0 ? | ||||||
|             buildMarkdownBody(context) |             buildMarkdownBody(context) | ||||||
|                 : buildChildTile(context, item.Children[index - 1]); |                 : buildChildTile(context, item.Children[index - 1]); | ||||||
|             }) : buildMarkdown(context), //: Text(item.Children[i-1].Name);}).toList() |           }) | ||||||
|       ], |       ], | ||||||
|  |     ); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   Widget buildBookmarksPage() { | ||||||
|  |     return Text("Bookmarks"); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   Widget buildSearchPage() { | ||||||
|  |     return Text("Search"); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   BottomNavigationBarItem buildBottomNavigationBarItem(String title, String assetName) { | ||||||
|  |     return BottomNavigationBarItem( | ||||||
|  |       icon: SvgPicture.asset( | ||||||
|  |         assetName, | ||||||
|  |         height: 30.0, | ||||||
|  |         width: 30.0, | ||||||
|  |         allowDrawingOutsideViewBox: true, | ||||||
|       ), |       ), | ||||||
|       //), |       title: Text(title), | ||||||
|  |       activeIcon: SvgPicture.asset( | ||||||
|  |         assetName, | ||||||
|  |         height: 40.0, | ||||||
|  |         width: 40.0, | ||||||
|  |         allowDrawingOutsideViewBox: true, | ||||||
|  |       ), | ||||||
|  |     ); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   List buildBottomNavigationBarItems() { | ||||||
|  |     return | ||||||
|  |     <BottomNavigationBarItem>[ | ||||||
|  |       buildBottomNavigationBarItem("Bibliothèque", "assets/spell-book.svg"), | ||||||
|  |       buildBottomNavigationBarItem("Favoris", "assets/stars-stack.svg"), | ||||||
|  |       buildBottomNavigationBarItem("Recherche", "assets/crystal-ball.svg"), | ||||||
|  |     ]; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   int indexPage = 0; | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   Widget build(BuildContext context) { | ||||||
|  |     Widget currentPage; | ||||||
|  |     switch(indexPage) { | ||||||
|  |       case 0: | ||||||
|  |         currentPage = buildLibraryPage(); | ||||||
|  |         break; | ||||||
|  |       case 1: | ||||||
|  |         currentPage = buildBookmarksPage(); | ||||||
|  |         break; | ||||||
|  |       case 2: | ||||||
|  |         currentPage = buildSearchPage(); | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  |     return Scaffold( | ||||||
|  |       appBar: AppBar( | ||||||
|  |         title: Text(widget.id), | ||||||
|  |       ), | ||||||
|  |       body: currentPage, | ||||||
|       bottomNavigationBar: BottomNavigationBar( |       bottomNavigationBar: BottomNavigationBar( | ||||||
|         items: <BottomNavigationBarItem>[ |         currentIndex: indexPage, | ||||||
|           BottomNavigationBarItem( |         onTap: (int index) { | ||||||
|             icon: SvgPicture.asset( |           setState(() { | ||||||
|               "assets/spell-book.svg", |             this.indexPage = index; | ||||||
|               height: 40.0, |           } | ||||||
|               width: 40.0, |           ); | ||||||
|               allowDrawingOutsideViewBox: true, |         }, | ||||||
|             ), // Icon(Icons.home), |         items: buildBottomNavigationBarItems(), | ||||||
|             title: Text('Bibliothèque'), |  | ||||||
|           ), |  | ||||||
|           BottomNavigationBarItem( |  | ||||||
|             icon: SvgPicture.asset( |  | ||||||
|               "assets/stars-stack.svg", |  | ||||||
|               height: 40.0, |  | ||||||
|               width: 40.0, |  | ||||||
|               allowDrawingOutsideViewBox: true, |  | ||||||
|             ), // Icon(Icons.business), |  | ||||||
|             title: Text('Favoris'), |  | ||||||
|           ), |  | ||||||
|           BottomNavigationBarItem( |  | ||||||
|             //icon: Icon(Icons.business), |  | ||||||
|             icon: SvgPicture.asset( |  | ||||||
|               "assets/crystal-ball.svg", |  | ||||||
|               height: 40.0, |  | ||||||
|               width: 40.0, |  | ||||||
|               allowDrawingOutsideViewBox: true, |  | ||||||
|             ), |  | ||||||
|             title: Text('Recherche'), |  | ||||||
|             //activeIcon: Icon(Icons.category, color: Color(0xFFEF5123)), |  | ||||||
|           ), |  | ||||||
|         ], |  | ||||||
|       ), |       ), | ||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | @ -21,6 +21,7 @@ class Item { | ||||||
|     this.Id = map["Id"]; |     this.Id = map["Id"]; | ||||||
|     this.Name = map["Name"]; |     this.Name = map["Name"]; | ||||||
|     this.Alias = map["AltName"]; |     this.Alias = map["AltName"]; | ||||||
|  |     this.AliasText = map["AltNameText"]; | ||||||
|     this.Markdown = map["Markdown"]; |     this.Markdown = map["Markdown"]; | ||||||
|     this.Discriminator = map["Discriminator"]; |     this.Discriminator = map["Discriminator"]; | ||||||
|   } |   } | ||||||
|  | @ -38,6 +39,7 @@ class Item { | ||||||
|     "RootId": Id, |     "RootId": Id, | ||||||
|     "Name": Name, |     "Name": Name, | ||||||
|     "AltName": Alias, |     "AltName": Alias, | ||||||
|  |     "AltNameText": AliasText, | ||||||
|     "Markdown": Markdown, |     "Markdown": Markdown, | ||||||
|     "Discriminator": Discriminator, |     "Discriminator": Discriminator, | ||||||
|   }; |   }; | ||||||
|  | @ -101,9 +103,68 @@ class MonsterItem extends Item { | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | class MonsterItems extends Item { | ||||||
|  |   String Family; | ||||||
|  |   String Type; | ||||||
|  |   String Size; | ||||||
|  |   String Alignment; | ||||||
|  |   String Terrain; | ||||||
|  |   String Legendary; | ||||||
|  |   String ArmorClass; | ||||||
|  |   String HitPoints; | ||||||
|  |   String Speed; | ||||||
|  |   String Strength; | ||||||
|  |   String Dexterity; | ||||||
|  |   String Constitution; | ||||||
|  |   String Intelligence; | ||||||
|  |   String Wisdom; | ||||||
|  |   String Charisma; | ||||||
|  |   String SavingThrows; | ||||||
|  |   String Skills; | ||||||
|  |   String DamageVulnerabilities; | ||||||
|  |   String DamageImmunities; | ||||||
|  |   String ConditionImmunities; | ||||||
|  |   String DamageResistances; | ||||||
|  |   String Senses; | ||||||
|  |   String Languages; | ||||||
|  |   String Challenge; | ||||||
|  |   int XP; | ||||||
|  | 
 | ||||||
|  |   MonsterItems.fromMap(Map<String, dynamic> map) | ||||||
|  |       : super.fromMap(map) { | ||||||
|  |     this.Family = map["MonsterItems_Family"]; | ||||||
|  |     this.Type = map["Type"]; | ||||||
|  |     this.Size = map["Size"]; | ||||||
|  |     this.Alignment = map["Alignment"]; | ||||||
|  |     this.Terrain = map["Terrain"]; | ||||||
|  |     this.Legendary = map["Legendary"]; | ||||||
|  |     this.ArmorClass = map["ArmorClass"]; | ||||||
|  |     this.HitPoints = map["HitPoints"]; | ||||||
|  |     this.Speed = map["Speed"]; | ||||||
|  |     this.Strength = map["Strength"]; | ||||||
|  |     this.Dexterity = map["Dexterity"]; | ||||||
|  |     this.Constitution = map["Constitution"]; | ||||||
|  |     this.Intelligence = map["Intelligence"]; | ||||||
|  |     this.Wisdom = map["Wisdom"]; | ||||||
|  |     this.Charisma = map["Charisma"]; | ||||||
|  |     this.SavingThrows = map["SavingThrows"]; | ||||||
|  |     this.Skills = map["Skills"]; | ||||||
|  |     this.DamageVulnerabilities = map["DamageVulnerabilities"]; | ||||||
|  |     this.DamageImmunities = map["DamageImmunities"]; | ||||||
|  |     this.ConditionImmunities = map["ConditionImmunities"]; | ||||||
|  |     this.DamageResistances = map["DamageResistances"]; | ||||||
|  |     this.Senses = map["Senses"]; | ||||||
|  |     this.Languages = map["Languages"]; | ||||||
|  |     this.Challenge = map["Challenge"]; | ||||||
|  |     this.XP = map["XP"]; | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| Item itemFromMap(Map<String, dynamic> map) { | Item itemFromMap(Map<String, dynamic> map) { | ||||||
|   switch(map["Discriminator"]) { |   switch(map["Discriminator"]) { | ||||||
|     case "MonsterItem": return MonsterItem.fromMap(map); |     case "MonsterItem": return MonsterItem.fromMap(map); | ||||||
|  |     case "MonsterItems": return MonsterItems.fromMap(map); | ||||||
|   } |   } | ||||||
|   return Item.fromMap(map); |   return Item.fromMap(map); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Yan Maniez
						Yan Maniez