Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
feat: transition mixin doc and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiMiguel committed Nov 24, 2023
1 parent 8f0d8ed commit eaab2ea
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions lib/home/widgets/question_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class _QuestionViewState extends State<QuestionView>

@override
void initializeTransitionController() {
super.initializeTransitionController();

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
Expand Down
2 changes: 2 additions & 0 deletions lib/home/widgets/thinking_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class _ThinkingViewState extends State<ThinkingView>

@override
void initializeTransitionController() {
super.initializeTransitionController();

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
Expand Down
30 changes: 17 additions & 13 deletions lib/home/widgets/transition_screen_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ import 'package:dash_ai_search/home/home.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

/// Mixin to handle transitions between screens.
mixin TransitionScreenMixin<T extends StatefulWidget> on State<T> {
/// The [AnimationController] for the enter transition.
@protected
late AnimationController enterTransitionController;

/// The [AnimationController] for the exit transition.
@protected
late AnimationController exitTransitionController;

/// The [Status]es that trigger the forward enter transition.
@protected
List<Status> forwardEnterStatuses = [];

/// The [Status]es that trigger the forward exit transition.
@protected
List<Status> forwardExitStatuses = [];

/// The [Status]es that trigger the back enter transition.
@protected
List<Status> backEnterStatuses = [];

/// The [Status]es that trigger the back exit transition.
@protected
List<Status> backExitStatuses = [];

/// Initialize the [AnimationController]s.
@protected
void initializeTransitionController() {}

Expand All @@ -30,20 +38,20 @@ mixin TransitionScreenMixin<T extends StatefulWidget> on State<T> {

initializeTransitionController();

enterAnimation();
_enterAnimation();

context.read<HomeBloc>().stream.listen((state) {
if (forwardEnterStatuses.contains(state.status)) {
enterAnimation();
_enterAnimation();
}
if (forwardExitStatuses.contains(state.status)) {
exitAnimation();
_exitAnimation();
}
if (backEnterStatuses.contains(state.status)) {
popEnterAnimation();
_popEnterAnimation();
}
if (backExitStatuses.contains(state.status)) {
popExitAnimation();
_popExitAnimation();
}
});
}
Expand All @@ -55,23 +63,19 @@ mixin TransitionScreenMixin<T extends StatefulWidget> on State<T> {
super.dispose();
}

@protected
void enterAnimation() {
void _enterAnimation() {
enterTransitionController.forward();
}

@protected
void exitAnimation() {
void _exitAnimation() {
exitTransitionController.forward();
}

@protected
void popEnterAnimation() {
void _popEnterAnimation() {
exitTransitionController.reverse();
}

@protected
void popExitAnimation() {
void _popExitAnimation() {
enterTransitionController.reverse();
}
}
2 changes: 2 additions & 0 deletions lib/home/widgets/welcome_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class _WelcomeViewState extends State<WelcomeView>

@override
void initializeTransitionController() {
super.initializeTransitionController();

enterTransitionController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
Expand Down
4 changes: 3 additions & 1 deletion test/home/widgets/transition_screen_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void main() {
});

testWidgets(
'calls exitTransitionController forward '
'calls exitAnimationController forward '
'when status is in forwardExitStatuses', (tester) async {
final streamController = StreamController<HomeState>();
whenListen(
Expand Down Expand Up @@ -180,6 +180,8 @@ void main() {

streamController
.add(const HomeState(status: Status.askQuestionToThinking));

await tester.pumpAndSettle();
verify(() => enterAnimationController.reverse()).called(1);
});
});
Expand Down
1 change: 1 addition & 0 deletions test/home/widgets/welcome_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void main() {
),
);

await tester.pumpAndSettle();
await tester.tap(find.byType(PrimaryCTA));

verify(() => homeBloc.add(const FromWelcomeToQuestion())).called(1);
Expand Down

0 comments on commit eaab2ea

Please sign in to comment.