Skip to content

Commit

Permalink
Merge branch 'main' into sidhdhi/handle-scoreboard-with-stream
Browse files Browse the repository at this point in the history
# Conflicts:
#	data/lib/service/ball_score/ball_score_service.dart
#	data/lib/service/match/match_service.dart
#	data/lib/service/team/team_service.dart
  • Loading branch information
cp-sidhdhi-p committed May 9, 2024
2 parents 6b48631 + b0286b0 commit 3f463b5
Show file tree
Hide file tree
Showing 24 changed files with 766 additions and 269 deletions.
51 changes: 32 additions & 19 deletions data/lib/service/ball_score/ball_score_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,48 @@ Stream<List<BallScoreChange>> getBallScoresStreamByInningIds(
controller.add(changes);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
});

return controller.stream;
}

Future<List<BallScoreModel>> getCurrentUserRelatedBalls() async {
Stream<List<BallScoreModel>> getCurrentUserRelatedBalls() {
if (_currentUserId == null) {
return [];
}
try {
final QuerySnapshot<Map<String, dynamic>> snapshot = await _firestore
.collection(FireStoreConst.ballScoresCollection)
.where(Filter.or(
Filter(FireStoreConst.bowlerId, isEqualTo: _currentUserId),
Filter(FireStoreConst.batsmanId, isEqualTo: _currentUserId),
Filter(FireStoreConst.wicketTakerId, isEqualTo: _currentUserId),
Filter(FireStoreConst.playerOutId, isEqualTo: _currentUserId)))
.get();

return snapshot.docs.map((doc) {
final data = doc.data();
return BallScoreModel.fromJson(data).copyWith(id: doc.id);
}).toList();
} catch (error, stack) {
throw AppError.fromError(error, stack);
return Stream.value([]);
}
StreamController<List<BallScoreModel>> controller =
StreamController<List<BallScoreModel>>();
_firestore
.collection(FireStoreConst.ballScoresCollection)
.where(Filter.or(
Filter(FireStoreConst.bowlerId, isEqualTo: _currentUserId),
Filter(FireStoreConst.batsmanId, isEqualTo: _currentUserId),
Filter(FireStoreConst.wicketTakerId, isEqualTo: _currentUserId),
Filter(FireStoreConst.playerOutId, isEqualTo: _currentUserId)))
.snapshots()
.listen((snapshot) {
try {
final ballList = snapshot.docs.map((doc) {
final data = doc.data();
return BallScoreModel.fromJson(data).copyWith(id: doc.id);
}).toList();

controller.add(ballList);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
});

return controller.stream;
}

Future<void> deleteBallAndUpdateTeamDetails({
Expand Down
2 changes: 2 additions & 0 deletions data/lib/service/innings/inning_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ class InningsService {
controller.add(innings);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
});

return controller.stream;
Expand Down
170 changes: 97 additions & 73 deletions data/lib/service/match/match_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,94 +75,112 @@ class MatchService {
}
}

Future<List<MatchModel>> getCurrentUserPlayedMatches() async {
Stream<List<MatchModel>> getCurrentUserPlayedMatches() {
if (_currentUserId == null) {
return [];
return Stream.value([]);
}
try {
QuerySnapshot snapshot =
await _firestore.collection(FireStoreConst.matchesCollection).get();

StreamController<List<MatchModel>> controller =
StreamController<List<MatchModel>>();

_firestore.collection(FireStoreConst.matchesCollection).snapshots().listen(
(QuerySnapshot<Map<String, dynamic>> snapshot) async {
List<MatchModel> matches = [];
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams
.map((e) => e.squad.map((e) => e.id).contains(_currentUserId))
.contains(true) &&
match.match_status == MatchStatus.finish) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
try {
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams
.map((e) => e.squad.map((e) => e.id).contains(_currentUserId))
.contains(true) &&
match.match_status == MatchStatus.finish) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}
controller.add(matches);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
});

return matches;
} catch (error, stack) {
throw AppError.fromError(error, stack);
}
return controller.stream;
}

Future<List<MatchModel>> getCurrentUserRelatedMatches() async {
Stream<List<MatchModel>> getCurrentUserRelatedMatches() {
if (_currentUserId == null) {
return [];
return Stream.value([]);
}
try {
QuerySnapshot snapshot =
await _firestore.collection(FireStoreConst.matchesCollection).get();

StreamController<List<MatchModel>> controller =
StreamController<List<MatchModel>>();

_firestore.collection(FireStoreConst.matchesCollection).snapshots().listen(
(QuerySnapshot<Map<String, dynamic>> snapshot) async {
List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
List<MatchTeamModel> teams = await getTeamsList(match.teams);
if (teams
.map((e) =>
e.team.players?.map((e) => e.id).contains(_currentUserId))
.contains(true) ||
teams
.map((e) => e.team.created_by == _currentUserId)
.contains(true)) {
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
try {
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
List<MatchTeamModel> teams = await getTeamsList(match.teams);
if (teams.any((team) =>
team.team.players
?.map((player) => player.id)
.contains(_currentUserId) ??
false || team.team.created_by == _currentUserId)) {
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}
controller.add(matches);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
});

return matches;
} catch (error, stack) {
throw AppError.fromError(error, stack);
}
return controller.stream;
}

Future<List<MatchModel>> getMatchesByTeamId(String teamId) async {
Expand Down Expand Up @@ -247,9 +265,11 @@ class MatchService {
controller.add(matches);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
});

return controller.stream;
Expand Down Expand Up @@ -309,13 +329,17 @@ class MatchService {
controller.add(matchModel);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
subscription?.cancel();
}
} else {
controller.close();
subscription?.cancel();
}
}, onError: (error, stack) {
controller.addError(AppError.fromError(error, stack));
controller.close();
subscription?.cancel();
});

return controller.stream;
Expand Down
74 changes: 27 additions & 47 deletions data/lib/service/team/team_service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/team/team_model.dart';
import 'package:data/api/user/user_models.dart';
Expand Down Expand Up @@ -76,38 +77,23 @@ class TeamService {
}
}

Future<List<TeamModel>> getUserRelatedTeams({
TeamFilterOption option = TeamFilterOption.all,
}) async {
try {
QuerySnapshot mainCollectionSnapshot;

switch (option) {
case TeamFilterOption.all:
mainCollectionSnapshot = await _firestore
.collection(FireStoreConst.teamsCollection)
.where(
Filter.or(
Filter(FireStoreConst.createdBy, isEqualTo: _currentUserId),
Filter(FireStoreConst.players, arrayContains: _currentUserId),
),
)
.get();
case TeamFilterOption.createdByMe:
mainCollectionSnapshot = await _firestore
.collection(FireStoreConst.teamsCollection)
.where(FireStoreConst.createdBy, isEqualTo: _currentUserId)
.get();
case TeamFilterOption.memberMe:
mainCollectionSnapshot = await _firestore
.collection(FireStoreConst.teamsCollection)
.where(FireStoreConst.players, arrayContains: _currentUserId)
.get();
}
Stream<List<TeamModel>> getUserRelatedTeams() {
if (_currentUserId == null) {
return Stream.value([]);
}

return _firestore
.collection(FireStoreConst.teamsCollection)
.where(
Filter.or(
Filter(FireStoreConst.createdBy, isEqualTo: _currentUserId),
Filter(FireStoreConst.players, arrayContains: _currentUserId),
),
)
.snapshots()
.asyncMap((snapshot) async {
List<TeamModel> teams = [];

for (QueryDocumentSnapshot mainDoc in mainCollectionSnapshot.docs) {
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
AddTeamRequestModel teamRequestModel = AddTeamRequestModel.fromJson(
mainDoc.data() as Map<String, dynamic>);

Expand All @@ -116,22 +102,22 @@ class TeamService {
: null;

final team = TeamModel(
name: teamRequestModel.name,
name_lowercase: teamRequestModel.name_lowercase,
id: teamRequestModel.id,
city: teamRequestModel.city,
created_at: teamRequestModel.created_at,
created_by: teamRequestModel.created_by,
profile_img_url: teamRequestModel.profile_img_url,
players: member);
name: teamRequestModel.name,
name_lowercase: teamRequestModel.name_lowercase,
id: teamRequestModel.id,
city: teamRequestModel.city,
created_at: teamRequestModel.created_at,
created_by: teamRequestModel.created_by,
profile_img_url: teamRequestModel.profile_img_url,
players: member,
);

teams.add(team);
}

return teams;
} catch (error, stack) {
}).handleError((error, stack) {
throw AppError.fromError(error, stack);
}
});
}

Future<List<TeamModel>> getUserOwnedTeams() async {
Expand Down Expand Up @@ -264,9 +250,3 @@ class TeamService {
}
}
}

enum TeamFilterOption {
all,
createdByMe,
memberMe;
}
Loading

0 comments on commit 3f463b5

Please sign in to comment.