-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add loading, error dialog, and sudoku page routing
- Loading branch information
1 parent
a7fa4d0
commit 6802a95
Showing
25 changed files
with
1,221 additions
and
161 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,67 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:sudoku/api/api.dart'; | ||
import 'package:sudoku/models/models.dart'; | ||
|
||
part 'home_event.dart'; | ||
part 'home_state.dart'; | ||
|
||
class HomeBloc extends Bloc<HomeEvent, HomeState> { | ||
HomeBloc({ | ||
required SudokuAPI apiClient, | ||
}) : _apiClient = apiClient, | ||
super(const HomeState()) { | ||
on<SudokuCreationRequested>(_onSudokuCreationRequested); | ||
} | ||
|
||
final SudokuAPI _apiClient; | ||
|
||
FutureOr<void> _onSudokuCreationRequested( | ||
SudokuCreationRequested event, | ||
Emitter<HomeState> emit, | ||
) async { | ||
emit( | ||
state.copyWith( | ||
sudoku: () => null, | ||
difficulty: () => event.difficulty, | ||
sudokuCreationStatus: () => SudokuCreationStatus.inProgress, | ||
sudokuCreationError: () => null, | ||
), | ||
); | ||
|
||
try { | ||
final sudoku = await _apiClient.createSudoku( | ||
difficulty: event.difficulty, | ||
); | ||
emit( | ||
state.copyWith( | ||
sudoku: () => sudoku, | ||
sudokuCreationStatus: () => SudokuCreationStatus.completed, | ||
), | ||
); | ||
} on SudokuInvalidRawDataException catch (_) { | ||
emit( | ||
state.copyWith( | ||
sudokuCreationStatus: () => SudokuCreationStatus.failed, | ||
sudokuCreationError: () => SudokuCreationErrorType.invalidRawData, | ||
), | ||
); | ||
} on SudokuAPIClientException catch (_) { | ||
emit( | ||
state.copyWith( | ||
sudokuCreationStatus: () => SudokuCreationStatus.failed, | ||
sudokuCreationError: () => SudokuCreationErrorType.apiClient, | ||
), | ||
); | ||
} catch (_) { | ||
emit( | ||
state.copyWith( | ||
sudokuCreationStatus: () => SudokuCreationStatus.failed, | ||
sudokuCreationError: () => SudokuCreationErrorType.unexpected, | ||
), | ||
); | ||
} | ||
} | ||
} |
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,14 @@ | ||
part of 'home_bloc.dart'; | ||
|
||
sealed class HomeEvent extends Equatable { | ||
const HomeEvent(); | ||
} | ||
|
||
final class SudokuCreationRequested extends HomeEvent { | ||
const SudokuCreationRequested(this.difficulty); | ||
|
||
final Difficulty difficulty; | ||
|
||
@override | ||
List<Object?> get props => [difficulty]; | ||
} |
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,46 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first | ||
part of 'home_bloc.dart'; | ||
|
||
enum SudokuCreationStatus { initial, inProgress, completed, failed } | ||
|
||
enum SudokuCreationErrorType { unexpected, invalidRawData, apiClient } | ||
|
||
class HomeState extends Equatable { | ||
const HomeState({ | ||
this.sudoku, | ||
this.difficulty, | ||
this.sudokuCreationStatus = SudokuCreationStatus.initial, | ||
this.sudokuCreationError, | ||
}); | ||
|
||
final Sudoku? sudoku; | ||
final Difficulty? difficulty; | ||
final SudokuCreationStatus sudokuCreationStatus; | ||
final SudokuCreationErrorType? sudokuCreationError; | ||
|
||
@override | ||
List<Object?> get props => [ | ||
sudoku, | ||
difficulty, | ||
sudokuCreationStatus, | ||
sudokuCreationError, | ||
]; | ||
|
||
HomeState copyWith({ | ||
Sudoku? Function()? sudoku, | ||
Difficulty? Function()? difficulty, | ||
SudokuCreationStatus Function()? sudokuCreationStatus, | ||
SudokuCreationErrorType? Function()? sudokuCreationError, | ||
}) { | ||
return HomeState( | ||
sudoku: sudoku != null ? sudoku() : this.sudoku, | ||
difficulty: difficulty != null ? difficulty() : this.difficulty, | ||
sudokuCreationStatus: sudokuCreationStatus != null | ||
? sudokuCreationStatus() | ||
: this.sudokuCreationStatus, | ||
sudokuCreationError: sudokuCreationError != null | ||
? sudokuCreationError() | ||
: this.sudokuCreationError, | ||
); | ||
} | ||
} |
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 +1,2 @@ | ||
export 'bloc/home_bloc.dart'; | ||
export 'view/home_page.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,14 @@ | ||
/// Defines the sudoku difficulty level | ||
enum Difficulty { | ||
/// Easy level | ||
easy, | ||
|
||
/// Medium level | ||
medium, | ||
|
||
/// Difficult level | ||
difficult, | ||
|
||
/// Expert level | ||
expert | ||
} |
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,4 +1,5 @@ | ||
export 'block.dart'; | ||
export 'difficulty.dart'; | ||
export 'position.dart'; | ||
export 'sudoku.dart'; | ||
export 'ticker.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
Oops, something went wrong.