| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  | 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); | 
					
						
							| 
									
										
										
										
											2020-02-22 02:43:08 +01:00
										 |  |  |   exists = false; | 
					
						
							| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  |   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; | 
					
						
							| 
									
										
										
										
											2020-02-22 00:59:48 +01:00
										 |  |  |   var response = await db.query( | 
					
						
							|  |  |  |       "Items", | 
					
						
							|  |  |  |       where: "Id = ? OR RootId = ?", | 
					
						
							|  |  |  |       whereArgs: [id, id] | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  |   if (response.isEmpty) { | 
					
						
							|  |  |  |     print("Id not found"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return response.isNotEmpty ? itemFromMap(response.first) : null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<Item> loadChildrenItems(Item item) async { | 
					
						
							| 
									
										
										
										
											2020-02-22 02:43:08 +01:00
										 |  |  |   print("getChildrenItems " + (item?.ItemType ?? "")); | 
					
						
							| 
									
										
										
										
											2020-02-21 21:28:14 +01:00
										 |  |  |   if (item.ItemType.endsWith("Items")) { | 
					
						
							|  |  |  |     String itemType = | 
					
						
							|  |  |  |     item.ItemType.substring(0, item.ItemType.length - 1); | 
					
						
							| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  |     String family = ""; | 
					
						
							| 
									
										
										
										
											2020-02-22 00:59:48 +01:00
										 |  |  |     if (item is FilteredItems) { | 
					
						
							|  |  |  |       family = (item as FilteredItems)?.Family ?? ""; | 
					
						
							| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     final db = await database; | 
					
						
							| 
									
										
										
										
											2020-02-22 00:59:48 +01:00
										 |  |  |     var response = await db.query( | 
					
						
							|  |  |  |         "Items", | 
					
						
							|  |  |  |         where: "ItemType = ? AND Family = ?", | 
					
						
							| 
									
										
										
										
											2020-02-21 21:28:14 +01:00
										 |  |  |         whereArgs: [itemType, family], | 
					
						
							| 
									
										
										
										
											2020-02-22 00:59:48 +01:00
										 |  |  |         orderBy: "NormalizedName" | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  |     if (response.isEmpty) { | 
					
						
							| 
									
										
										
										
											2020-02-22 00:59:48 +01:00
										 |  |  |       print("Children not found"); | 
					
						
							| 
									
										
										
										
											2020-02-21 14:49:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     item.Children = response.isNotEmpty | 
					
						
							|  |  |  |         ? itemsFromMapList(response) | 
					
						
							|  |  |  |         : null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return item; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |