-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hint view and logic (#345)
* feat: provide hint resource * feat: ask for hint from ui * test: asking for a hint * fix: update method name * feat: ask for hint when submitting text field * feat: add hint responses extension * feat: fetch previous hints * feat: new hints section * feat: add hint section to solving view
- Loading branch information
1 parent
f8fbbf1
commit f62e03b
Showing
25 changed files
with
677 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'hint_response_extension.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:game_domain/game_domain.dart'; | ||
|
||
extension HintResponseExtension on HintResponse { | ||
String get readable { | ||
final random = Random(); | ||
|
||
switch (this) { | ||
case HintResponse.yes: | ||
return yesResponses[random.nextInt(yesResponses.length)]; | ||
case HintResponse.no: | ||
return noResponses[random.nextInt(noResponses.length)]; | ||
case HintResponse.notApplicable: | ||
return notApplicableResponses[ | ||
random.nextInt(notApplicableResponses.length)]; | ||
} | ||
} | ||
} | ||
|
||
@visibleForTesting | ||
const yesResponses = [ | ||
'Yes!', | ||
]; | ||
|
||
@visibleForTesting | ||
const noResponses = [ | ||
'Nope', | ||
]; | ||
|
||
@visibleForTesting | ||
const notApplicableResponses = [ | ||
'Try with a "Yes or No" question', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export 'bloc/hint_bloc.dart'; | ||
export 'extensions/extensions.dart'; | ||
export 'widgets/widgets.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:game_domain/game_domain.dart'; | ||
import 'package:io_crossword/hint/hint.dart'; | ||
import 'package:io_crossword/l10n/l10n.dart'; | ||
import 'package:io_crossword_ui/io_crossword_ui.dart'; | ||
|
||
class HintsSection extends StatelessWidget { | ||
const HintsSection({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final l10n = context.l10n; | ||
|
||
final hintState = context.watch<HintBloc>().state; | ||
final isHintModeActive = hintState.isHintModeActive; | ||
final isThinking = hintState.status == HintStatus.thinking; | ||
final allHints = hintState.hints; | ||
|
||
return Column( | ||
mainAxisSize: MainAxisSize.min, | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: [ | ||
Center( | ||
child: HintText( | ||
text: | ||
isHintModeActive ? l10n.askYesOrNoQuestion : l10n.askGeminiHint, | ||
), | ||
), | ||
const SizedBox(height: 32), | ||
...allHints.mapIndexed( | ||
(i, hint) => HintQuestionResponse( | ||
index: i, | ||
hint: hint, | ||
), | ||
), | ||
if (isThinking) ...[ | ||
const SizedBox(height: 24), | ||
const Center(child: HintLoadingIndicator()), | ||
], | ||
], | ||
); | ||
} | ||
} | ||
|
||
@visibleForTesting | ||
class HintQuestionResponse extends StatelessWidget { | ||
@visibleForTesting | ||
const HintQuestionResponse({ | ||
required this.index, | ||
required this.hint, | ||
super.key, | ||
}); | ||
|
||
final int index; | ||
final Hint hint; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final textTheme = Theme.of(context).textTheme; | ||
final questionNumber = index + 1; | ||
|
||
return Column( | ||
mainAxisSize: MainAxisSize.min, | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Text( | ||
'Q$questionNumber: ${hint.question}', | ||
style: textTheme.bodySmall?.copyWith( | ||
color: IoCrosswordColors.softGray, | ||
), | ||
textAlign: TextAlign.center, | ||
), | ||
const SizedBox(height: 8), | ||
HintText(text: hint.response.readable), | ||
const SizedBox(height: 8), | ||
], | ||
); | ||
} | ||
} | ||
|
||
@visibleForTesting | ||
class HintLoadingIndicator extends StatefulWidget { | ||
@visibleForTesting | ||
const HintLoadingIndicator({super.key}); | ||
|
||
@override | ||
State<HintLoadingIndicator> createState() => _HintLoadingIndicatorState(); | ||
} | ||
|
||
class _HintLoadingIndicatorState extends State<HintLoadingIndicator> | ||
with SingleTickerProviderStateMixin { | ||
late AnimationController _controller; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_controller = AnimationController( | ||
duration: const Duration(seconds: 3), | ||
vsync: this, | ||
)..repeat(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return RotationTransition( | ||
turns: Tween<double>(begin: 0, end: 1).animate(_controller), | ||
child: const GeminiIcon(size: 24), | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller.dispose(); | ||
super.dispose(); | ||
} | ||
} |
Oops, something went wrong.