Skip to content

Commit

Permalink
use string const for firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed May 9, 2024
1 parent 4b1ec6e commit 38ec05d
Show file tree
Hide file tree
Showing 16 changed files with 636 additions and 543 deletions.
42 changes: 42 additions & 0 deletions data/lib/utils/constant/firestore_constant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class FireStoreConst {
// collection
static const String matchesCollection = "matches";
static const String teamsCollection = "teams";
static const String inningsCollection = "innings";
static const String ballScoresCollection = "ball_scores";
static const String usersCollection = "users";
static const String userProfileImagesFolder = "UserProfileImages";
static const String teamProfileImagesFolder = "TeamProfileImages";

// matches field const
static const String id = "id";
static const String teams = "teams";
static const String teamId = "team_id";
static const String run = "run";
static const String over = "over";
static const String wicket = "wicket";
static const String squad = "squad";
static const String currentPlayingTeamId = "current_playing_team_id";
static const String matchStatus = "match_status";
static const String tossWinnerId = "toss_winner_id";
static const String tossDecision = "toss_decision";

// innings field const
static const String matchId = "match_id";
static const String inningsStatus = "innings_status";
static const String overs = "overs";
static const String totalRuns = "total_runs";
static const String totalWickets = "total_wickets";

// ball score field const
static const String inningId = "inning_id";
static const String bowlerId = "bowler_id";
static const String batsmanId = "batsman_id";
static const String wicketTakerId = "wicket_taker_id";
static const String playerOutId = "player_out_id";

// teams field const
static const String players = "players";
static const String createdBy = "created_by";
static const String nameLowercase = "name_lowercase";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ extension MatchModelString on MatchModel {
final secondTeam =
teams.firstWhere((element) => element.team.id != firstTeam.team.id);

if ((firstTeam.run ?? 0) > (secondTeam.run ?? 0)) {
if (firstTeam.run > secondTeam.run) {
// first batting team won
final teamName = firstTeam.team.name;

final runDifference = (firstTeam.run ?? 0) - (secondTeam.run ?? 0);
final runDifference = firstTeam.run - secondTeam.run;

return (
teamName: teamName,
difference: runDifference,
wonByText: context.l10n.common_runs_dot_title,
);
} else if ((firstTeam.run ?? 0) == (secondTeam.run ?? 0)) {
} else if (firstTeam.run == secondTeam.run) {
return (teamName: "", difference: 0, wonByText: "");
} else {
// second batting team won
final teamName = secondTeam.team.name;

final wicketDifference =
secondTeam.squad.length - (firstTeam.wicket ?? 0);
secondTeam.squad.length - firstTeam.wicket;

return (
teamName: teamName,
Expand Down
8 changes: 4 additions & 4 deletions khelo/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class HomeScreen extends ConsumerWidget {
_teamScore(
context,
match.teams.first,
match.teams.elementAt(1).wicket ?? 0,
match.teams.elementAt(1).wicket,
match.current_playing_team_id == match.teams.first.team.id,
),
const SizedBox(
Expand All @@ -97,7 +97,7 @@ class HomeScreen extends ConsumerWidget {
_teamScore(
context,
match.teams.elementAt(1),
match.teams.first.wicket ?? 0,
match.teams.first.wicket,
match.current_playing_team_id ==
match.teams.elementAt(1).team.id),
const SizedBox(
Expand Down Expand Up @@ -134,12 +134,12 @@ class HomeScreen extends ConsumerWidget {
: context.colorScheme.textPrimary)),
),
Text.rich(TextSpan(
text: "${team.run ?? 0}-$wicket",
text: "${team.run}-$wicket",
style: AppTextStyle.header3
.copyWith(color: context.colorScheme.textPrimary),
children: [
TextSpan(
text: " ${team.over ?? 0}",
text: " ${team.over}",
style: AppTextStyle.subtitle2
.copyWith(color: context.colorScheme.textSecondary),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class FinalScoreView extends ConsumerWidget {
for (final team in state.match!.teams) {
final wicketCount = state.match!.teams
.firstWhere((element) => element.team.id != team.team.id)
.wicket ??
0;
.wicket;

children.add(_teamScore(context, team, wicketCount));
children.add(const SizedBox(height: 8));
Expand Down Expand Up @@ -62,12 +61,12 @@ class FinalScoreView extends ConsumerWidget {
.copyWith(color: context.colorScheme.textPrimary)),
const Spacer(),
Text.rich(TextSpan(
text: "${team.run ?? 0}-$wicket",
text: "${team.run}-$wicket",
style: AppTextStyle.header3
.copyWith(color: context.colorScheme.textPrimary),
children: [
TextSpan(
text: " ${team.over ?? 0}",
text: " ${team.over}",
style: AppTextStyle.subtitle2
.copyWith(color: context.colorScheme.textSecondary),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class MatchDetailInfoView extends ConsumerWidget {
}

return ListView(
padding:
context.mediaQueryPadding + const EdgeInsets.symmetric(horizontal: 8),
padding: context.mediaQueryPadding,
children: [
_sectionTitle(context, context.l10n.match_detail_match_info_tab_title),
_matchTitleView(context, state.match!),
Expand All @@ -51,6 +50,7 @@ class MatchDetailInfoView extends ConsumerWidget {
for (final team in state.match?.teams ?? []) ...[
_teamPlayerView(context, team: team)
],
const SizedBox(height: 16),
_sectionTitle(context, context.l10n.match_info_venue_title),
_dataRowView(context,
title: context.l10n.match_info_ground_title,
Expand All @@ -65,8 +65,7 @@ class MatchDetailInfoView extends ConsumerWidget {
Widget _sectionTitle(BuildContext context, String title) {
return Container(
color: context.colorScheme.containerNormalOnSurface,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
margin: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text(
title,
style: AppTextStyle.subtitle1
Expand All @@ -81,7 +80,7 @@ class MatchDetailInfoView extends ConsumerWidget {
String? subtitle,
}) {
return Padding(
padding: const EdgeInsets.only(left: 8, right: 8, top: 4),
padding: const EdgeInsets.only(left: 16, right: 16, top: 6),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ class MatchDetailScorecardView extends ConsumerWidget {
.copyWith(color: context.colorScheme.textPrimary, fontSize: 20),
),
Text(
"${team.run} - $wicket ${context.l10n.match_commentary_trailing_over_short_text(team.over ?? 0)}",
"${team.run} - $wicket ${context.l10n.match_commentary_trailing_over_short_text(team.over)}",
style: AppTextStyle.subtitle1
.copyWith(color: context.colorScheme.textPrimary, fontSize: 20),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/ball_score/ball_score_model.dart';
import 'package:data/api/innings/inning_model.dart';
import 'package:data/api/match/match_model.dart';
Expand Down Expand Up @@ -96,7 +97,7 @@ class MatchDetailTabViewNotifier extends StateNotifier<MatchDetailTabState> {
state = state.copyWith(
firstInning: firstInning, secondInning: secondInning, error: null);
if (!state.ballScoreQueryListenerSet) {
_loadBallScores();
_loadBallScoresV1();
}
},
onError: (e, stack) {
Expand All @@ -107,7 +108,7 @@ class MatchDetailTabViewNotifier extends StateNotifier<MatchDetailTabState> {
);
}

void _loadBallScores() {
void _loadBallScores() {
if (state.firstInning == null || state.secondInning == null) {
return;
}
Expand All @@ -119,7 +120,15 @@ class MatchDetailTabViewNotifier extends StateNotifier<MatchDetailTabState> {
state.secondInning?.id ?? "INVALID ID"
]).listen(
(scores) {
final sortedList = scores;
List<BallScoreModel> sortedList = state.ballScores.toList();
for (final score in scores) {
if (score.type == DocumentChangeType.added) {
sortedList.add(score.ballScore);
} else if (score.type == DocumentChangeType.removed) {
sortedList
.removeWhere((element) => element.id == score.ballScore.id);
}
}
sortedList.sort(
(a, b) => a.time.compareTo(b.time),
);
Expand Down
6 changes: 3 additions & 3 deletions khelo/lib/ui/flow/matches/match_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class MatchListScreen extends ConsumerWidget {
_teamNameView(
context,
team: match.teams.first,
wicket: match.teams.last.wicket ?? 0,
wicket: match.teams.last.wicket,
totalOvers: match.number_of_over,
isRunning:
match.current_playing_team_id == match.teams.first.team.id &&
Expand All @@ -219,7 +219,7 @@ class MatchListScreen extends ConsumerWidget {
_teamNameView(
context,
team: match.teams.last,
wicket: match.teams.first.wicket ?? 0,
wicket: match.teams.first.wicket,
totalOvers: match.number_of_over,
isRunning:
match.current_playing_team_id == match.teams.last.team.id &&
Expand Down Expand Up @@ -258,7 +258,7 @@ class MatchListScreen extends ConsumerWidget {
.copyWith(color: context.colorScheme.textPrimary),
),
TextSpan(
text: " (${team.over ?? 0}/$totalOvers)",
text: " (${team.over}/$totalOvers)",
style: AppTextStyle.body2
.copyWith(color: context.colorScheme.textSecondary),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,27 @@ class AddTossDetailViewNotifier extends StateNotifier<AddTossDetailState> {
}

Future<void> onNextButtonTap() async {
final currentPlayingTeamId = state.tossWinnerDecision == TossDecision.bat
? state.tossWinnerTeamId
: state.match!.teams
.where((element) => element.team.id != state.tossWinnerTeamId)
.firstOrNull
?.team
.id;
if (state.match?.id == null ||
state.tossWinnerTeamId == null ||
state.tossWinnerDecision == null) {
state.tossWinnerDecision == null ||
currentPlayingTeamId == null) {
return;
}
state =
state.copyWith(isTossDetailUpdateInProgress: true, actionError: null);
try {
await _matchService.updateTossDetails(
state.match!.id!,
state.tossWinnerTeamId!,
state.tossWinnerDecision!,
);
state.match!.id!,
state.tossWinnerTeamId!,
state.tossWinnerDecision!,
currentPlayingTeamId);
state = state.copyWith(
isTossDetailUpdateInProgress: false, pushScoreBoard: true);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ class InningCompleteDialog extends ConsumerWidget {
_subTitleText(
context,
title: "${context.l10n.score_board_runs_title}:",
subTitle: state.totalRuns.toString(),
subTitle: (state.currentInning?.total_runs ?? "").toString(),
),
const SizedBox(
width: 16,
),
_subTitleText(
context,
title: "${context.l10n.score_board_wickets_text}:",
subTitle: state.wicketCount.toString(),
subTitle: (state.otherInning?.total_wickets ?? "").toString(),
)
],
),
Expand All @@ -108,7 +108,7 @@ class InningCompleteDialog extends ConsumerWidget {
}

String _getOverCount(ScoreBoardViewState state) {
return state.currentInning?.overs?.toString() ??
return state.currentInning?.overs.toString() ??
"${state.overCount - 1}.${state.ballCount}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ class OverCompleteDialog extends ConsumerWidget {
.copyWith(color: context.colorScheme.textPrimary, fontSize: 22),
children: [
TextSpan(
text: " ${state.totalRuns}/${state.wicketCount}",
text:
" ${state.currentInning!.total_runs}/${state.otherInning!.total_wickets}",
style: AppTextStyle.subtitle2
.copyWith(color: context.colorScheme.textSecondary),
),
Expand Down
14 changes: 8 additions & 6 deletions khelo/lib/ui/flow/score_board/components/score_display_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ class ScoreDisplayView extends ConsumerWidget {
Widget _teamName(
BuildContext context, ScoreBoardViewState state, bool batting) {
final String? battingTeam = state.match?.teams
.firstWhere(
(element) => element.team.id == state.currentInning?.team_id)
.team
.where((element) => element.team.id == state.currentInning?.team_id)
.firstOrNull
?.team
.name;
final String? bowlingTeam = state.match?.teams
.firstWhere((element) => element.team.id == state.otherInning?.team_id)
.team
.where((element) => element.team.id == state.otherInning?.team_id)
.firstOrNull
?.team
.name;

return Text(
Expand Down Expand Up @@ -74,7 +75,8 @@ class ScoreDisplayView extends ConsumerWidget {
TextSpan(
children: [
TextSpan(
text: '${state.totalRuns}/${state.wicketCount}',
text:
'${state.currentInning?.total_runs ?? 0}/${state.otherInning?.total_wickets ?? 0}',
style: AppTextStyle.subtitle1.copyWith(
color: context.colorScheme.textPrimary, fontSize: 39),
),
Expand Down
10 changes: 6 additions & 4 deletions khelo/lib/ui/flow/score_board/score_board_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ class _ScoreBoardScreenState extends ConsumerState<ScoreBoardScreen> {
if (state.error != null) {
return ErrorScreen(
error: state.error,
onRetryTap: notifier.getMatchById,
onRetryTap: () {
notifier.cancelStreamSubscription();
notifier.loadMatch();
},
);
}

Expand Down Expand Up @@ -476,14 +479,13 @@ class _ScoreBoardScreenState extends ConsumerState<ScoreBoardScreen> {
extra: extra,
playerOutId: outBatsMan?.id,
wicketTakerId: wicketTakerId,
wicketType: type,
switchStrike: extra != null ? extra % 2 == 0 : false);
wicketType: type);
}

Future<void> _showStrikerSelectionDialog(BuildContext context) async {
final striker = await StrikerSelectionDialog.show<UserModel>(context);
if (striker != null && context.mounted) {
notifier.setOrSwitchStriker(batsMan: striker);
notifier.setOrSwitchStriker(batsManId: striker.id);
}
}

Expand Down
Loading

0 comments on commit 38ec05d

Please sign in to comment.