Skip to content

Commit

Permalink
Handle wrong answers
Browse files Browse the repository at this point in the history
Broken because of:
* furkantektas/analog_clock#16
  • Loading branch information
walles committed Jun 30, 2022
1 parent 3f60d51 commit 1fbeb5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Johans Barnklocka II

## TODO
* If the text entry field is wrong when the Go button is pressed, highlight that
somehow. When the user starts editing the field, remove the highlight.
* Handle <https://github.com/furkantektas/analog_clock/issues/16>, maybe draw
our own clock?
* Show "First half of the day" / "Second half of the day" so that we don't have
to guess
* Show "First half of the day" / "Second half of the day"
Expand All @@ -23,3 +23,5 @@
* Add CI running our tests
* If the text entry field is correct when the Go button is pressed, randomize a
new time on the analog clock and clear the field
* If the text entry field is wrong when the Go button is pressed, highlight that
somehow.
20 changes: 14 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MyHomePage extends StatefulWidget {

class _MyHomePageState extends State<MyHomePage> {
DateTime? _timestamp;
String? _errorText;
final _random = Random();

late TextEditingController _timeInputController;
Expand Down Expand Up @@ -101,8 +102,10 @@ class _MyHomePageState extends State<MyHomePage> {
FilteringTextInputFormatter.digitsOnly,
],
keyboardType: TextInputType.number,
decoration: const InputDecoration(
hintText: 'HHMM', labelText: 'Time in digital'),
decoration: InputDecoration(
hintText: 'HHMM',
labelText: 'Time in digital',
errorText: _errorText),
onSubmitted: (String _) {
// FIXME: Only if the input looks like a time!
_handleButtonPress();
Expand All @@ -124,14 +127,19 @@ class _MyHomePageState extends State<MyHomePage> {

_timeInputController.clear();
setState(() {
_errorText = null;
_timestamp = _createRandomTimestamp();
});

// FIXME: Focus the text field
} else {
// FIXME: Somehow highlight this to the user

print('Wrong! ${_getTimestamp()}');
final twoDigits = NumberFormat('00');
final hour = _getTimestamp().hour;
final minute = _getTimestamp().minute;
setState(() {
_errorText =
'Digital time is ${twoDigits.format(hour)}${twoDigits.format(minute)}';
});
}
}
}
Expand All @@ -140,7 +148,7 @@ class _MyHomePageState extends State<MyHomePage> {
/// the timestamp's hours and minutes.
@visibleForTesting
bool isValidRendering(String rendering, DateTime timestamp) {
final NumberFormat twoDigits = NumberFormat('00');
final twoDigits = NumberFormat('00');

if (rendering.length == 3) {
rendering = '0$rendering';
Expand Down

0 comments on commit 1fbeb5d

Please sign in to comment.