mirror of
				https://github.com/Nioux/AideDeJeu.git
				synced 2025-10-31 07:26:09 +00:00 
			
		
		
		
	BloC
This commit is contained in:
		
							parent
							
								
									b758deab1a
								
							
						
					
					
						commit
						765d65afd4
					
				
					 6 changed files with 60 additions and 224 deletions
				
			
		|  | @ -4,9 +4,19 @@ import 'package:bloc/bloc.dart'; | ||||||
| import 'package:equatable/equatable.dart'; | import 'package:equatable/equatable.dart'; | ||||||
| import 'package:flutter/cupertino.dart'; | import 'package:flutter/cupertino.dart'; | ||||||
| 
 | 
 | ||||||
| class PlayerCharacterState { | class PlayerCharacterState extends Equatable { | ||||||
|   final BuildContext context; |   final BuildContext context; | ||||||
| 
 | 
 | ||||||
|  |   final RaceItem race; | ||||||
|  |   final SubRaceItem subRace; | ||||||
|  |   final List<RaceItem> races; | ||||||
|  |   final List<SubRaceItem> subRaces; | ||||||
|  | 
 | ||||||
|  |   final BackgroundItem background; | ||||||
|  |   final SubBackgroundItem subBackground; | ||||||
|  |   final List<BackgroundItem> backgrounds; | ||||||
|  |   final List<SubBackgroundItem> subBackgrounds; | ||||||
|  | 
 | ||||||
|   PlayerCharacterState({ |   PlayerCharacterState({ | ||||||
|     this.context, |     this.context, | ||||||
|     this.race, |     this.race, | ||||||
|  | @ -17,7 +27,7 @@ class PlayerCharacterState { | ||||||
|     this.backgrounds, |     this.backgrounds, | ||||||
|     this.subBackground, |     this.subBackground, | ||||||
|     this.subBackgrounds, |     this.subBackgrounds, | ||||||
|   }) {} |   }); | ||||||
| 
 | 
 | ||||||
|   PlayerCharacterState copyWith({ |   PlayerCharacterState copyWith({ | ||||||
|     BuildContext context, |     BuildContext context, | ||||||
|  | @ -43,15 +53,29 @@ class PlayerCharacterState { | ||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   RaceItem race; |   PlayerCharacterState copyWithClean({ | ||||||
|   SubRaceItem subRace; |     BuildContext context, | ||||||
|   List<RaceItem> races; |     RaceItem race, | ||||||
|   List<SubRaceItem> subRaces; |     List<RaceItem> races, | ||||||
| 
 |     SubRaceItem subRace, | ||||||
|   BackgroundItem background; |     List<SubRaceItem> subRaces, | ||||||
|   SubBackgroundItem subBackground; |     BackgroundItem background, | ||||||
|   List<BackgroundItem> backgrounds; |     List<BackgroundItem> backgrounds, | ||||||
|   List<SubBackgroundItem> subBackgrounds; |     SubBackgroundItem subBackground, | ||||||
|  |     List<SubBackgroundItem> subBackgrounds, | ||||||
|  |   }) { | ||||||
|  |     return PlayerCharacterState( | ||||||
|  |       context: context ?? this.context, | ||||||
|  |       race: race ?? this.race, | ||||||
|  |       races: races ?? this.races, | ||||||
|  |       subRace: race != null ? null : subRace ?? this.subRace, | ||||||
|  |       subRaces: race != null ? subRaces : subRaces ?? this.subRaces, | ||||||
|  |       background: background ?? this.background, | ||||||
|  |       backgrounds: backgrounds ?? this.backgrounds, | ||||||
|  |       subBackground: background != null ? null : subBackground ?? this.subBackground, | ||||||
|  |       subBackgrounds: background != null ? subBackgrounds : subBackgrounds ?? this.subBackgrounds, | ||||||
|  |     ); | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   List<Object> get props => [ |   List<Object> get props => [ | ||||||
|  | @ -64,97 +88,25 @@ class PlayerCharacterState { | ||||||
|         backgrounds, |         backgrounds, | ||||||
|         subBackgrounds |         subBackgrounds | ||||||
|       ]; |       ]; | ||||||
| 
 |  | ||||||
|   Future<void> initRaces() async { |  | ||||||
|     print("initRaces"); |  | ||||||
|     var races = await loadRaces(context); |  | ||||||
|     this.races = races.toList(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> initSubRaces(RaceItem race) async { |  | ||||||
|     print("initSubRaces"); |  | ||||||
|     var subRaces = await loadSubRaces(context, race); |  | ||||||
|     this.subRaces = subRaces; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> initBackgrounds() async { |  | ||||||
|     print("initBackgrounds"); |  | ||||||
|     var backgrounds = await loadBackgrounds(context); |  | ||||||
|     this.backgrounds = backgrounds; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> initSubBackgrounds(BackgroundItem background) async { |  | ||||||
|     print("initSubBackgrounds"); |  | ||||||
|     var subBackgrounds = await loadSubBackgrounds(context, background); |  | ||||||
|     this.subBackgrounds = subBackgrounds; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   // setters |  | ||||||
| 
 |  | ||||||
|   Future<void> setRace(RaceItem race) async { |  | ||||||
|     this.race = race; |  | ||||||
|     this.subRace = null; |  | ||||||
|     this.subRaces = null; |  | ||||||
|     await initSubRaces(race); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> setSubRace(SubRaceItem subRace) async { |  | ||||||
|     this.subRace = subRace; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> setBackground(BackgroundItem background) async { |  | ||||||
|     this.background = background; |  | ||||||
|     this.subBackground = null; |  | ||||||
|     this.subBackgrounds = null; |  | ||||||
|     await initSubBackgrounds(background); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> setSubBackground(SubBackgroundItem subBackground) async { |  | ||||||
|     this.subBackground = subBackground; |  | ||||||
|   } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| abstract class PlayerCharacterEvent extends Equatable {} | abstract class PlayerCharacterEvent extends Equatable {} | ||||||
| 
 | 
 | ||||||
| /* | class RaceEvent extends SetItemEvent<RaceItem> { | ||||||
| class RacesEvent extends PlayerCharacterEvent { |   RaceEvent(RaceItem item) : super(item); | ||||||
|   List<RaceItem> races; |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   List<Object> get props => [races]; |  | ||||||
| } |  | ||||||
| */ |  | ||||||
| class RaceEvent extends PlayerCharacterEvent { |  | ||||||
|   RaceItem race; |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   List<Object> get props => [race]; |  | ||||||
| 
 |  | ||||||
|   RaceEvent(RaceItem race) { |  | ||||||
|     this.race = race; |  | ||||||
|   } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class SubRaceEvent extends PlayerCharacterEvent { | class SubRaceEvent extends SetItemEvent<SubRaceItem> { | ||||||
|   SubRaceItem subRace; |   SubRaceEvent(SubRaceItem item) : super(item); | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   List<Object> get props => [subRace]; |  | ||||||
| 
 |  | ||||||
|   SubRaceEvent(SubRaceItem subRace) { |  | ||||||
|     this.subRace = subRace; |  | ||||||
|   } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class SetItemEvent<T> extends PlayerCharacterEvent { | class SetItemEvent<T> extends PlayerCharacterEvent { | ||||||
|   T item; |   final T item; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   List<Object> get props => [item]; |   List<Object> get props => [item]; | ||||||
| 
 | 
 | ||||||
|   SetItemEvent(T item) { |   SetItemEvent(T item) : this.item = item; | ||||||
|     this.item = item; |  | ||||||
|   } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class BackgroundEvent extends SetItemEvent<BackgroundItem> { | class BackgroundEvent extends SetItemEvent<BackgroundItem> { | ||||||
|  | @ -170,7 +122,6 @@ class LoadEvent extends PlayerCharacterEvent { | ||||||
|   List<Object> get props => []; |   List<Object> get props => []; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| //enum PlayerCharacterEvent { setRace, setSubRace, } |  | ||||||
| class PlayerCharacterBloc | class PlayerCharacterBloc | ||||||
|     extends Bloc<PlayerCharacterEvent, PlayerCharacterState> { |     extends Bloc<PlayerCharacterEvent, PlayerCharacterState> { | ||||||
|   BuildContext context; |   BuildContext context; | ||||||
|  | @ -194,35 +145,33 @@ class PlayerCharacterBloc | ||||||
|     } else if (event is SubBackgroundEvent) { |     } else if (event is SubBackgroundEvent) { | ||||||
|       yield* _mapSubBackgroundEventToState(event); |       yield* _mapSubBackgroundEventToState(event); | ||||||
|     } else if (event is LoadEvent) { |     } else if (event is LoadEvent) { | ||||||
|       final currentState = state.copyWith(); // state; |       yield* _mapLoadEventToState(event); | ||||||
|       await currentState.initRaces(); |  | ||||||
|       await currentState.initBackgrounds(); |  | ||||||
|       yield currentState; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|   } |   } | ||||||
|   Stream<PlayerCharacterState> _mapRaceEventToState( |   Stream<PlayerCharacterState> _mapRaceEventToState( | ||||||
|       RaceEvent event) async* { |       RaceEvent event) async* { | ||||||
|     final currentState = state.copyWith(); |     var subRaces = await loadSubRaces(context, event.item); | ||||||
|     await currentState.setRace(event.race); |     yield state.copyWithClean(race: event.item, subRaces: subRaces); | ||||||
|     yield currentState; |  | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|   Stream<PlayerCharacterState> _mapSubRaceEventToState( |   Stream<PlayerCharacterState> _mapSubRaceEventToState( | ||||||
|       SubRaceEvent event) async* { |       SubRaceEvent event) async* { | ||||||
|     final currentState = state.copyWith(); |     yield state.copyWith(subRace: event.item); | ||||||
|     await currentState.setSubRace(event.subRace); |  | ||||||
|     yield currentState; |  | ||||||
|   } |   } | ||||||
|   Stream<PlayerCharacterState> _mapBackgroundEventToState( |   Stream<PlayerCharacterState> _mapBackgroundEventToState( | ||||||
|       BackgroundEvent event) async* { |       BackgroundEvent event) async* { | ||||||
|     final currentState = state.copyWith(); |     var subBackgrounds = await loadSubBackgrounds(context, event.item); | ||||||
|     await currentState.setBackground(event.item); |     yield state.copyWithClean(background: event.item,subBackgrounds: subBackgrounds); | ||||||
|     yield currentState; |  | ||||||
|   } |   } | ||||||
|   Stream<PlayerCharacterState> _mapSubBackgroundEventToState( |   Stream<PlayerCharacterState> _mapSubBackgroundEventToState( | ||||||
|       SubBackgroundEvent event) async* { |       SubBackgroundEvent event) async* { | ||||||
|     final currentState = state.copyWith(); |     yield state.copyWith(subBackground: event.item); | ||||||
|     await currentState.setSubBackground(event.item); |   } | ||||||
|     yield currentState; |   Stream<PlayerCharacterState> _mapLoadEventToState( | ||||||
|  |       LoadEvent event) async* { | ||||||
|  |     var races = await loadRaces(context); | ||||||
|  |     var backgrounds = await loadBackgrounds(context); | ||||||
|  |     yield state.copyWith(races: races, backgrounds: backgrounds); // state; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -120,7 +120,7 @@ Future<List<SubRaceItem>> loadSubRaces(BuildContext context, RaceItem race) asyn | ||||||
|   return null; |   return null; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Future<List<T>> loadTypedItems<T extends Item>(BuildContext context, {String itemType, Item item = null}) async { | Future<List<T>> loadTypedItems<T extends Item>(BuildContext context, {String itemType, Item item}) async { | ||||||
|   final db = await database; |   final db = await database; | ||||||
|   var response = await db.query( |   var response = await db.query( | ||||||
|       "Items", |       "Items", | ||||||
|  |  | ||||||
|  | @ -119,7 +119,7 @@ class SpellItem extends Item { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class Items extends Item { | class Items extends Item { | ||||||
|   Items(Map<String, dynamic> map) : super(map) {} |   Items(Map<String, dynamic> map) : super(map); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| abstract class FilteredItems extends Items { | abstract class FilteredItems extends Items { | ||||||
|  |  | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| import 'package:aidedejeu_flutter/models/items.dart'; |  | ||||||
| import 'package:aidedejeu_flutter/theme.dart'; | import 'package:aidedejeu_flutter/theme.dart'; | ||||||
| import 'package:flutter/cupertino.dart'; | import 'package:flutter/cupertino.dart'; | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
|  |  | ||||||
|  | @ -23,7 +23,7 @@ class _LibraryPageState extends State<LibraryPage> { | ||||||
|     setState(() { |     setState(() { | ||||||
|       this.item = item; |       this.item = item; | ||||||
|       if (item is FilteredItems) { |       if (item is FilteredItems) { | ||||||
|         this.filters = (item as FilteredItems).toFilterList(); |         this.filters = item.toFilterList(); | ||||||
|       } else { |       } else { | ||||||
|         this.filters = null; |         this.filters = null; | ||||||
|       } |       } | ||||||
|  | @ -35,9 +35,9 @@ class _LibraryPageState extends State<LibraryPage> { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   String markdown = ""; |   String markdown = ""; | ||||||
|   Item item = null; |   Item item; | ||||||
|   MarkdownStyleSheet styleSheet; |   MarkdownStyleSheet styleSheet; | ||||||
|   List<Filter> filters = null; |   List<Filter> filters; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   void initState() { |   void initState() { | ||||||
|  |  | ||||||
|  | @ -1,127 +1,15 @@ | ||||||
| import 'package:aidedejeu_flutter/blocs/player_character/player_character_bloc.dart'; | import 'package:aidedejeu_flutter/blocs/player_character/player_character_bloc.dart'; | ||||||
| import 'package:aidedejeu_flutter/database.dart'; |  | ||||||
| import 'package:aidedejeu_flutter/localization.dart'; | import 'package:aidedejeu_flutter/localization.dart'; | ||||||
| import 'package:aidedejeu_flutter/models/items.dart'; | import 'package:aidedejeu_flutter/models/items.dart'; | ||||||
| import 'package:aidedejeu_flutter/theme.dart'; |  | ||||||
| import 'package:aidedejeu_flutter/widgets/library.dart'; | import 'package:aidedejeu_flutter/widgets/library.dart'; | ||||||
| import 'package:bloc/bloc.dart'; |  | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
| import 'package:flutter_markdown/flutter_markdown.dart'; | import 'package:flutter_markdown/flutter_markdown.dart'; | ||||||
| import 'package:equatable/equatable.dart'; |  | ||||||
| 
 | 
 | ||||||
| class PCEditorPage extends StatelessWidget { | class PCEditorPage extends StatelessWidget { | ||||||
|   // StatefulWidget { |  | ||||||
| /*  PCEditorPage({Key key}) : super(key: key); |  | ||||||
| 
 | 
 | ||||||
|   @override |  | ||||||
|   State<StatefulWidget> createState() => _PCEditorPageState(); |  | ||||||
| }*/ |  | ||||||
| /* |  | ||||||
| class PCEditorViewModel { |  | ||||||
|   RaceItem _race; |  | ||||||
|   SubRaceItem _subRace; |  | ||||||
|   List<RaceItem> _races; |  | ||||||
|   List<SubRaceItem> _subRaces; |  | ||||||
| 
 |  | ||||||
|   BackgroundItem _background; |  | ||||||
|   SubBackgroundItem _subBackground; |  | ||||||
|   List<BackgroundItem> _backgrounds; |  | ||||||
|   List<SubBackgroundItem> _subBackgrounds; |  | ||||||
| 
 |  | ||||||
|   _PCEditorPageState state; |  | ||||||
|   BuildContext context; |  | ||||||
|   PCEditorViewModel(_PCEditorPageState state, BuildContext context) { |  | ||||||
|     this.state = state; |  | ||||||
|     this.context = context; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _initRaces() async { |  | ||||||
|     var races = await loadRaces(context); |  | ||||||
|     state.setState(() { |  | ||||||
|       _races = races.toList(); |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _initSubRaces(RaceItem race) async { |  | ||||||
|     var subRaces = await loadSubRaces(context, race); |  | ||||||
|     state.setState(() { |  | ||||||
|       _subRaces = subRaces; |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _initBackgrounds() async { |  | ||||||
|     var backgrounds = await loadBackgrounds(context); |  | ||||||
|     state.setState(() { |  | ||||||
|       _backgrounds = backgrounds; |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _initSubBackgrounds(BackgroundItem background) async { |  | ||||||
|     var subBackgrounds = await loadSubBackgrounds(context, background); |  | ||||||
|     state.setState(() { |  | ||||||
|       _subBackgrounds = subBackgrounds; |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   // setters |  | ||||||
| 
 |  | ||||||
|   void _setRace(RaceItem race) { |  | ||||||
|     state.setState(() { |  | ||||||
|       this._race = race; |  | ||||||
|       this._subRace = null; |  | ||||||
|       this._subRaces = null; |  | ||||||
|     }); |  | ||||||
|     _initSubRaces(race); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _setSubRace(SubRaceItem subRace) { |  | ||||||
|     state.setState(() { |  | ||||||
|       this._subRace = subRace; |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _setBackground(BackgroundItem background) { |  | ||||||
|     state.setState(() { |  | ||||||
|       this._background = background; |  | ||||||
|       this._subBackground = null; |  | ||||||
|       this._subBackgrounds = null; |  | ||||||
|     }); |  | ||||||
|     _initSubBackgrounds(background); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void _setSubBackground(SubBackgroundItem subBackground) { |  | ||||||
|     state.setState(() { |  | ||||||
|       this._subBackground = subBackground; |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| //class _PCEditorPageState extends State<PCEditorPage> { |  | ||||||
|   MarkdownStyleSheet styleSheet; |   MarkdownStyleSheet styleSheet; | ||||||
| 
 | 
 | ||||||
|   //PCEditorViewModel vm; |  | ||||||
|   // inits |  | ||||||
| /* |  | ||||||
|   @override |  | ||||||
|   void initState() { |  | ||||||
|     super.initState(); |  | ||||||
|     //vm = PCEditorViewModel(this, context); |  | ||||||
|     //vm._initRaces(); |  | ||||||
|     //vm._initBackgrounds(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   @protected |  | ||||||
|   @mustCallSuper |  | ||||||
|   void didChangeDependencies() { |  | ||||||
|     super.didChangeDependencies(); |  | ||||||
|     styleSheet = mainMarkdownStyleSheet(context); |  | ||||||
|   } |  | ||||||
| */ |  | ||||||
|   // widgets generics |   // widgets generics | ||||||
| 
 | 
 | ||||||
|   Widget _buildMarkdown( |   Widget _buildMarkdown( | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Yan Maniez
						Yan Maniez