Skip to content

Commit

Permalink
Merge pull request #32 from canopas/Mayank/refactor-observers
Browse files Browse the repository at this point in the history
Refactor observers
  • Loading branch information
cp-mayank authored Jun 4, 2024
2 parents 549dfc4 + b09fa2a commit 2dd4c21
Show file tree
Hide file tree
Showing 16 changed files with 705 additions and 708 deletions.
80 changes: 40 additions & 40 deletions khelo/lib/ui/flow/matches/add_match/add_match_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,6 @@ class AddMatchScreen extends ConsumerStatefulWidget {
class _AddMatchScreenState extends ConsumerState<AddMatchScreen> {
late AddMatchViewNotifier notifier;

void _observeActionError(BuildContext context, WidgetRef ref) {
ref.listen(addMatchViewStateProvider.select((value) => value.actionError),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
});
}

void _observePushTossDetailScreen(
BuildContext context,
WidgetRef ref,
String? matchId,
) {
ref.listen(
addMatchViewStateProvider.select((value) => value.pushTossDetailScreen),
(previous, next) {
if (next == true) {
AppRoute.addTossDetail(
matchId: ref.read(addMatchViewStateProvider.notifier).matchId ??
"INVALID ID")
.pushReplacement(context);
} else if (next == false) {
AppRoute.scoreBoard(
matchId: ref.read(addMatchViewStateProvider.notifier).matchId ??
"INVALID ID")
.pushReplacement(context);
}
});
}

void _observePop(BuildContext context, WidgetRef ref) {
ref.listen(addMatchViewStateProvider.select((value) => value.pop),
(previous, next) {
if (next != null && next) {
context.pop();
}
});
}

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -472,4 +432,44 @@ class _AddMatchScreenState extends ConsumerState<AddMatchScreen> {
},
);
}

void _observeActionError(BuildContext context, WidgetRef ref) {
ref.listen(addMatchViewStateProvider.select((value) => value.actionError),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
});
}

void _observePushTossDetailScreen(
BuildContext context,
WidgetRef ref,
String? matchId,
) {
ref.listen(
addMatchViewStateProvider.select((value) => value.pushTossDetailScreen),
(previous, next) {
if (next == true) {
AppRoute.addTossDetail(
matchId: ref.read(addMatchViewStateProvider.notifier).matchId ??
"INVALID ID")
.pushReplacement(context);
} else if (next == false) {
AppRoute.scoreBoard(
matchId: ref.read(addMatchViewStateProvider.notifier).matchId ??
"INVALID ID")
.pushReplacement(context);
}
});
}

void _observePop(BuildContext context, WidgetRef ref) {
ref.listen(addMatchViewStateProvider.select((value) => value.pop),
(previous, next) {
if (next != null && next) {
context.pop();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ class MatchOfficialSelectionView extends StatelessWidget {
],
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,4 @@ class OverDetailView extends StatelessWidget {
),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class _AddMatchOfficialsScreenState

@override
Widget build(BuildContext context) {
notifier = ref.watch(addMatchOfficialsStateProvider.notifier);
final state = ref.watch(addMatchOfficialsStateProvider);

return AppPage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class SearchUserBottomSheet extends ConsumerWidget {

return state.searchedUsers.isEmpty
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Center(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Center(
child: Text(
context.l10n.search_user_empty_text,
textAlign: TextAlign.center,
Expand All @@ -75,7 +75,7 @@ class SearchUserBottomSheet extends ConsumerWidget {
),
),
),
)
)
: ListView.separated(
separatorBuilder: (context, index) {
return Divider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class _PowerPlayScreenState extends ConsumerState<PowerPlayScreen> {
int maxOversInRow = _calculateMaxOversInRow(context);
double approxCellWidth = _calculateCellWidth(context, maxOversInRow);

notifier = ref.watch(powerPlayStateProvider.notifier);
final state = ref.watch(powerPlayStateProvider);

return AppPage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class _SelectSquadScreenState extends ConsumerState<SelectSquadScreen> {

@override
Widget build(BuildContext context) {
notifier = ref.watch(selectSquadStateProvider.notifier);
final state = ref.watch(selectSquadStateProvider);

return AppPage(
Expand All @@ -76,18 +75,16 @@ class _SelectSquadScreenState extends ConsumerState<SelectSquadScreen> {
BlendMode.srcIn),
)),
],
body: Builder(
builder: (context) {
return ListView(
padding: context.mediaQueryPadding +
const EdgeInsets.symmetric(horizontal: 16),
children: [
_playingSquadList(context, notifier, state),
_teamMemberList(context, notifier, state)
],
);
}
),
body: Builder(builder: (context) {
return ListView(
padding: context.mediaQueryPadding +
const EdgeInsets.symmetric(horizontal: 16),
children: [
_playingSquadList(context, notifier, state),
_teamMemberList(context, notifier, state)
],
);
}),
);
}

Expand Down
34 changes: 17 additions & 17 deletions khelo/lib/ui/flow/profile/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ import 'package:style/text/app_text_style.dart';
class ProfileScreen extends ConsumerWidget {
const ProfileScreen({super.key});

void _observeActionError(BuildContext context, WidgetRef ref) {
ref.listen(profileStateProvider.select((value) => value.actionError),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
});
}

void _observeUserSession(BuildContext context, WidgetRef ref) {
ref.listen(hasUserSession, (previous, next) {
if (!next) {
AppRoute.intro.go(context);
}
});
}

@override
Widget build(BuildContext context, WidgetRef ref) {
final notifier = ref.watch(profileStateProvider.notifier);
Expand Down Expand Up @@ -164,4 +147,21 @@ class ProfileScreen extends ConsumerWidget {
return const SizedBox();
}
}

void _observeActionError(BuildContext context, WidgetRef ref) {
ref.listen(profileStateProvider.select((value) => value.actionError),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
});
}

void _observeUserSession(BuildContext context, WidgetRef ref) {
ref.listen(hasUserSession, (previous, next) {
if (!next) {
AppRoute.intro.go(context);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,6 @@ class _AddTossDetailScreenState extends ConsumerState<AddTossDetailScreen> {
runPostFrame(() => notifier.setData(widget.matchId));
}

void _observeActionError(BuildContext context, WidgetRef ref) {
ref.listen(addTossDetailStateProvider.select((value) => value.actionError),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
});
}

void _observePushScoreBoard() {
ref.listen(
addTossDetailStateProvider.select((value) => value.pushScoreBoard),
(previous, next) {
if (next != null && next) {
AppRoute.scoreBoard(matchId: notifier.matchId!)
.pushReplacement(context);
}
});
}

@override
Widget build(BuildContext context) {
final state = ref.watch(addTossDetailStateProvider);
Expand Down Expand Up @@ -267,4 +247,24 @@ class _AddTossDetailScreenState extends ConsumerState<AddTossDetailScreen> {
return Assets.images.bowler;
}
}

void _observeActionError(BuildContext context, WidgetRef ref) {
ref.listen(addTossDetailStateProvider.select((value) => value.actionError),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
});
}

void _observePushScoreBoard() {
ref.listen(
addTossDetailStateProvider.select((value) => value.pushScoreBoard),
(previous, next) {
if (next != null && next) {
AppRoute.scoreBoard(matchId: notifier.matchId!)
.pushReplacement(context);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ class _SelectPlayerSheetState extends ConsumerState<SelectPlayerSheet> {
final state = ref.watch(scoreBoardStateProvider);
final notifier = ref.watch(scoreBoardStateProvider.notifier);

final showCheckBox = widget.playerSelectionType !=
PlayerSelectionType.bowler
? widget.batsManList.any((element) => element.status == PlayerStatus.injured)
: false;
final showCheckBox =
widget.playerSelectionType != PlayerSelectionType.bowler
? widget.batsManList
.any((element) => element.status == PlayerStatus.injured)
: false;

final injuredPlayerRemained =
widget.playerSelectionType != PlayerSelectionType.bowler
Expand Down
Loading

0 comments on commit 2dd4c21

Please sign in to comment.