| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  | import 'dart:io'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  | import 'package:aidedejeu_flutter/models/items.dart'; | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  | import 'package:flutter/services.dart'; | 
					
						
							|  |  |  | import 'package:flutter_markdown/flutter_markdown.dart'; | 
					
						
							|  |  |  | import 'package:flutter_svg/flutter_svg.dart'; | 
					
						
							|  |  |  | import 'package:path/path.dart'; | 
					
						
							|  |  |  | import 'package:sqflite/sqflite.dart'; | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void main() => runApp(MyApp()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  | 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 = | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |         data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |   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; | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | class MyApp extends StatelessWidget { | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     return MaterialApp( | 
					
						
							|  |  |  |       title: 'Flutter Demo', | 
					
						
							|  |  |  |       theme: ThemeData( | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |         primarySwatch: Colors.red, | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |       ), | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |       home: MyHomePage(id: 'index.md'), | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MyHomePage extends StatefulWidget { | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   MyHomePage({Key key, @required this.id}) : super(key: key); | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   final String id; | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   _MyHomePageState createState() => _MyHomePageState(id: this.id); | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _MyHomePageState extends State<MyHomePage> { | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   _MyHomePageState({@required this.id}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final String id; | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |   void setItem(Item item) { | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |     setState(() { | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |       this.item = item; | 
					
						
							|  |  |  |       this.markdown = | 
					
						
							|  |  |  |           item.Markdown.replaceAllMapped(RegExp(r'<!--.*?-->'), (match) { | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |         return ''; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   String markdown = ""; | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |   Item item = null; | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   MarkdownStyleSheet styleSheet; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   void initState() { | 
					
						
							|  |  |  |     super.initState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ThemeData theme = ThemeData( | 
					
						
							|  |  |  |       brightness: Brightness.light, | 
					
						
							|  |  |  |       primaryColor: Colors.lightBlue[800], | 
					
						
							|  |  |  |       accentColor: Colors.cyan[600], | 
					
						
							|  |  |  |       fontFamily: 'LinuxLibertine', | 
					
						
							|  |  |  |       textTheme: TextTheme( | 
					
						
							|  |  |  |         headline: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold), | 
					
						
							|  |  |  |         title: TextStyle(fontSize: 18.0, fontStyle: FontStyle.italic), | 
					
						
							|  |  |  |         body1: TextStyle(fontSize: 14.0, fontFamily: 'Hind'), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 16:00:01 +01:00
										 |  |  |     styleSheet = MarkdownStyleSheet.fromTheme(theme).copyWith( | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |         tableColumnWidth: IntrinsicColumnWidth(), | 
					
						
							|  |  |  |         tableCellsPadding: EdgeInsets.all(0.2)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 16:00:01 +01:00
										 |  |  |     /*getItemWithId(this.id) | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |         .then((item) => loadChildrenItems(item).then((items) => setItem(item))) | 
					
						
							| 
									
										
										
										
											2020-02-19 16:00:01 +01:00
										 |  |  |         .catchError((error) => print(error));*/ | 
					
						
							| 
									
										
										
										
											2020-02-19 16:47:28 +01:00
										 |  |  |     loadItem().then((item) => setItem(item)); | 
					
						
							| 
									
										
										
										
											2020-02-19 16:00:01 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 16:47:28 +01:00
										 |  |  |   Future<Item> loadItem() async { | 
					
						
							| 
									
										
										
										
											2020-02-19 16:00:01 +01:00
										 |  |  |     var item = await getItemWithId(this.id); | 
					
						
							|  |  |  |     var items = await loadChildrenItems(item); | 
					
						
							| 
									
										
										
										
											2020-02-19 16:47:28 +01:00
										 |  |  |     //setItem(item);
 | 
					
						
							|  |  |  |     return item; | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 15:16:10 +01:00
										 |  |  |   final Widget svgLibrary = SvgPicture.asset( | 
					
						
							|  |  |  |     "assets/spell-book.svg", | 
					
						
							|  |  |  |     height: 20.0, | 
					
						
							|  |  |  |     width: 20.0, | 
					
						
							|  |  |  |     allowDrawingOutsideViewBox: true, | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final Widget svgBookmarks = SvgPicture.asset( | 
					
						
							|  |  |  |     "assets/stars-stack.svg", | 
					
						
							|  |  |  |     height: 20.0, | 
					
						
							|  |  |  |     width: 20.0, | 
					
						
							|  |  |  |     allowDrawingOutsideViewBox: true, | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final Widget svgSearch = SvgPicture.asset( | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |     "assets/crystal-ball.svg", | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |     height: 20.0, | 
					
						
							|  |  |  |     width: 20.0, | 
					
						
							|  |  |  |     allowDrawingOutsideViewBox: true, | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Widget buildMarkdown(BuildContext context) { | 
					
						
							|  |  |  |     return Markdown( | 
					
						
							|  |  |  |       data: markdown, | 
					
						
							|  |  |  |       styleSheet: styleSheet, | 
					
						
							|  |  |  |       onTapLink: (link) => Navigator.push( | 
					
						
							|  |  |  |         context, | 
					
						
							|  |  |  |         MaterialPageRoute( | 
					
						
							|  |  |  |             builder: (context) => MyHomePage(id: link) | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   Widget buildMarkdownBody(BuildContext context) { | 
					
						
							|  |  |  |     return MarkdownBody( | 
					
						
							|  |  |  |       data: markdown, | 
					
						
							|  |  |  |       styleSheet: styleSheet, | 
					
						
							|  |  |  |       onTapLink: (link) => Navigator.push( | 
					
						
							|  |  |  |         context, | 
					
						
							|  |  |  |         MaterialPageRoute( | 
					
						
							|  |  |  |             builder: (context) => MyHomePage(id: link) | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   Widget buildChildTile(BuildContext context, Item item) { | 
					
						
							|  |  |  |     return ListTile( | 
					
						
							|  |  |  |       title: Text(item.Name), | 
					
						
							|  |  |  |       onTap: () => Navigator.push( | 
					
						
							|  |  |  |         context, | 
					
						
							|  |  |  |         MaterialPageRoute( | 
					
						
							|  |  |  |             builder: (context) => MyHomePage(id: item.Id) | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |     int count = 0; | 
					
						
							|  |  |  |     if (item != null && item.Children != null) { | 
					
						
							|  |  |  |       count = item.Children.length; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     final List<int> _listData = List<int>.generate(count + 1, (i) => i); | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |     return Scaffold( | 
					
						
							|  |  |  |       appBar: AppBar( | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |         title: Text(widget.id), | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |       ), | 
					
						
							| 
									
										
										
										
											2020-02-19 15:50:44 +01:00
										 |  |  |       body: //Container(
 | 
					
						
							|  |  |  |         //child: Column(
 | 
					
						
							|  |  |  |         Stack( | 
					
						
							|  |  |  |           children: <Widget>[ | 
					
						
							|  |  |  |             //Text(markdown)
 | 
					
						
							|  |  |  |             item?.Children != null ? | 
					
						
							|  |  |  |             ListView.builder( | 
					
						
							|  |  |  |               itemCount: item.Children.length + 1, | 
					
						
							|  |  |  |                 itemBuilder: (BuildContext context, int index) { | 
					
						
							|  |  |  |                 return index == 0 ? | 
					
						
							|  |  |  |                 buildMarkdownBody(context) | 
					
						
							|  |  |  |                 : buildChildTile(context, item.Children[index - 1]); | 
					
						
							|  |  |  |             }) : buildMarkdown(context), //: Text(item.Children[i-1].Name);}).toList()
 | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       //),
 | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |       bottomNavigationBar: BottomNavigationBar( | 
					
						
							| 
									
										
										
										
											2020-02-20 15:16:10 +01:00
										 |  |  |         items: <BottomNavigationBarItem>[ | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |           BottomNavigationBarItem( | 
					
						
							| 
									
										
										
										
											2020-02-20 15:16:10 +01:00
										 |  |  |             icon: svgLibrary, // Icon(Icons.home),
 | 
					
						
							|  |  |  |             title: Text('Bibliothèque'), | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |           ), | 
					
						
							|  |  |  |           BottomNavigationBarItem( | 
					
						
							| 
									
										
										
										
											2020-02-20 15:16:10 +01:00
										 |  |  |             icon: svgBookmarks, // Icon(Icons.business),
 | 
					
						
							|  |  |  |             title: Text('Favoris'), | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |           ), | 
					
						
							|  |  |  |           BottomNavigationBarItem( | 
					
						
							| 
									
										
										
										
											2020-02-20 15:16:10 +01:00
										 |  |  |             //icon: Icon(Icons.business),
 | 
					
						
							|  |  |  |             icon: svgSearch, | 
					
						
							|  |  |  |             title: Text('Recherche'), | 
					
						
							| 
									
										
										
										
											2020-02-14 10:37:58 +01:00
										 |  |  |             //activeIcon: Icon(Icons.category, color: Color(0xFFEF5123)),
 | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2020-02-14 01:36:31 +01:00
										 |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |