From 2911bc359b312012859fe76beb498a990a863905 Mon Sep 17 00:00:00 2001 From: Yan Maniez Date: Wed, 19 Feb 2020 15:50:44 +0100 Subject: [PATCH] Plein de tests --- .gitignore | 1 + aidedejeu_flutter/lib/main.dart | 117 ++++++++++++++++++------ aidedejeu_flutter/lib/models/items.dart | 115 +++++++++++++++++++++++ 3 files changed, 204 insertions(+), 29 deletions(-) create mode 100644 aidedejeu_flutter/lib/models/items.dart diff --git a/.gitignore b/.gitignore index 18fc1faf..add0d743 100644 --- a/.gitignore +++ b/.gitignore @@ -264,3 +264,4 @@ __pycache__/ /AideDeJeu/AideDeJeu.Android/zipalign.bat /Ignore /AideDeJeu/AideDeJeuWeb/Properties/PublishProfiles/AideDeJeuWeb - Web Deploy.pubxml +*.lock diff --git a/aidedejeu_flutter/lib/main.dart b/aidedejeu_flutter/lib/main.dart index 9009edc6..5bb3c922 100644 --- a/aidedejeu_flutter/lib/main.dart +++ b/aidedejeu_flutter/lib/main.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:aidedejeu_flutter/models/items.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_markdown/flutter_markdown.dart'; @@ -9,23 +10,6 @@ import 'package:sqflite/sqflite.dart'; void main() => runApp(MyApp()); -class Item { - String Id; - String Markdown; - - Item({this.Id, this.Markdown}); - - factory Item.fromMap(Map json) => new Item( - Id: json["Id"], - Markdown: json["Markdown"], - ); - - Map toMap() => { - "Id": Id, - "Markdown": Markdown, - }; -} - Database _database; Future get database async { @@ -48,7 +32,7 @@ Future getDatabaseInstance() async { ByteData data = await rootBundle.load(join("assets", "library.db")); List bytes = - data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); + data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); await File(path).writeAsBytes(bytes, flush: true); } else { @@ -66,7 +50,25 @@ Future getItemWithId(String id) async { if (response.isEmpty) { print("Id not found"); } - return response.isNotEmpty ? Item.fromMap(response.first) : null; + return response.isNotEmpty ? itemFromMap(response.first) : null; +} + +Future 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 { @@ -96,15 +98,18 @@ class _MyHomePageState extends State { final String id; - void setMarkdown(String md) { + void setItem(Item item) { setState(() { - markdown = md.replaceAllMapped(RegExp(r''), (match) { + this.item = item; + this.markdown = + item.Markdown.replaceAllMapped(RegExp(r''), (match) { return ''; }); }); } String markdown = ""; + Item item = null; MarkdownStyleSheet styleSheet; @override @@ -125,31 +130,85 @@ class _MyHomePageState extends State { //styleSheet = MarkdownStyleSheet.fromTheme(theme); styleSheet = MarkdownStyleSheet( + //p: TextStyle(fontSize: 14.0, fontFamily: 'LinuxLibertine'), + tableColumnWidth: IntrinsicColumnWidth(), tableCellsPadding: EdgeInsets.all(0.2)); getItemWithId(this.id) - .then((item) => setMarkdown(item.Markdown)) + .then((item) => loadChildrenItems(item).then((items) => setItem(item))) .catchError((error) => print(error)); } - final Widget svg = SvgPicture.asset("assets/crystal-ball.svg", + final Widget svg = SvgPicture.asset( + "assets/crystal-ball.svg", height: 20.0, width: 20.0, allowDrawingOutsideViewBox: true, ); + + 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) + ), + ), + ); + } @override Widget build(BuildContext context) { - + int count = 0; + if (item != null && item.Children != null) { + count = item.Children.length; + } + final List _listData = List.generate(count + 1, (i) => i); return Scaffold( appBar: AppBar( title: Text(widget.id), ), - body: Markdown( - data: markdown, - styleSheet: styleSheet, - onTapLink: (link) => Navigator.push(context, - MaterialPageRoute(builder: (context) => MyHomePage(id: link)))), + body: //Container( + //child: Column( + Stack( + children: [ + //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() + ], + ), + //), bottomNavigationBar: BottomNavigationBar( items: const [ BottomNavigationBarItem( diff --git a/aidedejeu_flutter/lib/models/items.dart b/aidedejeu_flutter/lib/models/items.dart new file mode 100644 index 00000000..f297989a --- /dev/null +++ b/aidedejeu_flutter/lib/models/items.dart @@ -0,0 +1,115 @@ +class Item { + String Id; + String RootId; + String ParentLink; + String Name; + String NormalizedName; + String ParentName; + int NameLevel; + String Alias; + String AliasText; + String NormalizedAlias; + String Source; + String Markdown; + String FullText; + String Discriminator; + List Children; + + Item({this.Id, this.Name, this.Alias, this.Markdown, this.Discriminator}); + + Item.fromMap(Map map) { + this.Id = map["Id"]; + this.Name = map["Name"]; + this.Alias = map["AltName"]; + this.Markdown = map["Markdown"]; + this.Discriminator = map["Discriminator"]; + } + + /*factory Item.fromMap(Map json) => new Item( + Id: json["Id"], + Name: json["Name"], + Alias: json["AltName"], + Markdown: json["Markdown"], + Discriminator: json["Discriminator"], + );*/ + + Map toMap() => { + "Id": Id, + "RootId": Id, + "Name": Name, + "AltName": Alias, + "Markdown": Markdown, + "Discriminator": Discriminator, + }; +} + +class MonsterItem 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; + + MonsterItem.fromMap(Map map) + : super.fromMap(map) { + this.Family = map["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 map) { + switch(map["Discriminator"]) { + case "MonsterItem": return MonsterItem.fromMap(map); + } + return Item.fromMap(map); +} + +List itemsFromMapList(List> mapList) { + return List.generate(mapList.length, (i) { + return itemFromMap(mapList[i]); + }); +} \ No newline at end of file