Skip to content

Commit

Permalink
Use future wait for optimization (#72)
Browse files Browse the repository at this point in the history
* add divider

* update teams to use streams

* use async map for optimization

* use converter and refactor fetch - refactor create is remained

* update with converter

* use stream_transform

* minor change of spacing and font color

* handle in support case
  • Loading branch information
cp-sidhdhi-p authored Jul 9, 2024
1 parent 3d730ae commit 2bf1835
Show file tree
Hide file tree
Showing 28 changed files with 586 additions and 827 deletions.
18 changes: 9 additions & 9 deletions data/.flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# This is a generated file; do not edit or check into version control.
cloud_firestore=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_firestore-5.0.0/
cloud_firestore_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_firestore_web-4.0.0/
cloud_firestore=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_firestore-5.0.2/
cloud_firestore_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/cloud_firestore_web-4.0.2/
device_info_plus=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/device_info_plus-10.1.0/
firebase_auth=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_auth-5.0.0/
firebase_auth_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_auth_web-5.12.1/
firebase_core=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_core-3.0.0/
firebase_core_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_core_web-2.17.1/
firebase_storage=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_storage-12.0.0/
firebase_storage_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_storage_web-3.9.8/
flutter_timezone=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/flutter_timezone-1.0.8/
firebase_auth=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_auth-5.1.1/
firebase_auth_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_auth_web-5.12.3/
firebase_core=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_core-3.1.1/
firebase_core_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_core_web-2.17.2/
firebase_storage=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_storage-12.1.0/
firebase_storage_web=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/firebase_storage_web-3.9.10/
flutter_timezone=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/flutter_timezone-2.0.0/
package_info_plus=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/package_info_plus-8.0.0/
path_provider_linux=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/
path_provider_windows=/Users/sidhdhi.p/.pub-cache/hosted/pub.dev/path_provider_windows-2.2.1/
Expand Down
2 changes: 1 addition & 1 deletion data/.flutter-plugins-dependencies

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions data/lib/api/ball_score/ball_score_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: non_constant_identifier_names

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/user/user_models.dart';
import 'package:data/extensions/double_extensions.dart';
import 'package:data/extensions/int_extensions.dart';
Expand Down Expand Up @@ -32,6 +33,11 @@ class BallScoreModel with _$BallScoreModel {

factory BallScoreModel.fromJson(Map<String, dynamic> json) =>
_$BallScoreModelFromJson(json);

factory BallScoreModel.fromFireStore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options) =>
BallScoreModel.fromJson(snapshot.data()!);
}

@JsonEnum(valueField: "value")
Expand Down
6 changes: 6 additions & 0 deletions data/lib/api/innings/inning_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: non_constant_identifier_names

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'inning_model.freezed.dart';
Expand All @@ -20,6 +21,11 @@ class InningModel with _$InningModel {

factory InningModel.fromJson(Map<String, dynamic> json) =>
_$InningModelFromJson(json);

factory InningModel.fromFireStore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options) =>
InningModel.fromJson(snapshot.data()!);
}

@JsonEnum(valueField: "value")
Expand Down
12 changes: 12 additions & 0 deletions data/lib/api/match/match_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: non_constant_identifier_names

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/team/team_model.dart';
import 'package:data/api/user/user_models.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
Expand Down Expand Up @@ -137,6 +138,8 @@ class MatchPlayer with _$MatchPlayer {

@freezed
class AddEditMatchRequest with _$AddEditMatchRequest {
// ignore: invalid_annotation_target
@JsonSerializable(anyMap: true, explicitToJson: true)
const factory AddEditMatchRequest({
String? id,
required List<AddMatchTeamRequest> teams,
Expand Down Expand Up @@ -167,10 +170,17 @@ class AddEditMatchRequest with _$AddEditMatchRequest {

factory AddEditMatchRequest.fromJson(Map<String, dynamic> json) =>
_$AddEditMatchRequestFromJson(json);

factory AddEditMatchRequest.fromFireStore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options) =>
AddEditMatchRequest.fromJson(snapshot.data()!);
}

@freezed
class AddMatchTeamRequest with _$AddMatchTeamRequest {
// ignore: invalid_annotation_target
@JsonSerializable(anyMap: true, explicitToJson: true)
const factory AddMatchTeamRequest({
required String team_id,
String? captain_id,
Expand All @@ -187,6 +197,8 @@ class AddMatchTeamRequest with _$AddMatchTeamRequest {

@freezed
class MatchPlayerRequest with _$MatchPlayerRequest {
// ignore: invalid_annotation_target
@JsonSerializable(anyMap: true, explicitToJson: true)
const factory MatchPlayerRequest({
required String id,
required PlayerStatus status,
Expand Down
9 changes: 6 additions & 3 deletions data/lib/api/match/match_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,8 @@ class __$$AddEditMatchRequestImplCopyWithImpl<$Res>
}

/// @nodoc
@JsonSerializable()
@JsonSerializable(anyMap: true, explicitToJson: true)
class _$AddEditMatchRequestImpl implements _AddEditMatchRequest {
const _$AddEditMatchRequestImpl(
{this.id,
Expand Down Expand Up @@ -2170,7 +2171,8 @@ class __$$AddMatchTeamRequestImplCopyWithImpl<$Res>
}

/// @nodoc
@JsonSerializable()
@JsonSerializable(anyMap: true, explicitToJson: true)
class _$AddMatchTeamRequestImpl implements _AddMatchTeamRequest {
const _$AddMatchTeamRequestImpl(
{required this.team_id,
Expand Down Expand Up @@ -2386,7 +2388,8 @@ class __$$MatchPlayerRequestImplCopyWithImpl<$Res>
}

/// @nodoc
@JsonSerializable()
@JsonSerializable(anyMap: true, explicitToJson: true)
class _$MatchPlayerRequestImpl implements _MatchPlayerRequest {
const _$MatchPlayerRequestImpl(
{required this.id, required this.status, this.index});
Expand Down
20 changes: 9 additions & 11 deletions data/lib/api/match/match_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions data/lib/api/support/support_models.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'support_models.freezed.dart';
Expand All @@ -17,6 +18,11 @@ class AddSupportCaseRequest with _$AddSupportCaseRequest {

factory AddSupportCaseRequest.fromJson(Map<String, dynamic> json) =>
_$AddSupportCaseRequestFromJson(json);

factory AddSupportCaseRequest.fromFireStore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options) =>
AddSupportCaseRequest.fromJson(snapshot.data()!);
}

enum AttachmentUploadStatus {
Expand Down
6 changes: 6 additions & 0 deletions data/lib/api/team/team_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: non_constant_identifier_names

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:data/api/user/user_models.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

Expand Down Expand Up @@ -39,4 +40,9 @@ class AddTeamRequestModel with _$AddTeamRequestModel {

factory AddTeamRequestModel.fromJson(Map<String, dynamic> json) =>
_$AddTeamRequestModelFromJson(json);

factory AddTeamRequestModel.fromFireStore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options) =>
AddTeamRequestModel.fromJson(snapshot.data()!);
}
6 changes: 6 additions & 0 deletions data/lib/api/user/user_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'dart:convert';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'user_models.freezed.dart';
Expand Down Expand Up @@ -32,6 +33,11 @@ class UserModel with _$UserModel {
factory UserModel.fromJson(Map<String, dynamic> json) =>
_$UserModelFromJson(json);

factory UserModel.fromFireStore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options) =>
UserModel.fromJson(snapshot.data()!);

String toJsonString() => jsonEncode(toJson());

String get nameInitial => name?[0].toUpperCase() ?? '?';
Expand Down
2 changes: 1 addition & 1 deletion data/lib/errors/app_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AppError implements Exception {
} else if (error is FirebaseException) {
return _handleFirebaseError(error);
} else if (error is TypeError) {
return SomethingWentWrongError(stackTrace: error.stackTrace);
return SomethingWentWrongError(message: error.toString(), stackTrace: error.stackTrace);
} else if (error is LargeAttachmentUploadError) {
return const LargeAttachmentUploadError();
} else {
Expand Down
74 changes: 19 additions & 55 deletions data/lib/service/ball_score/ball_score_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ class BallScoreService {
final MatchService _matchService;
final InningsService _inningsService;
String? _currentUserId;
final CollectionReference<BallScoreModel> _ballScoreCollection;

BallScoreService(this._firestore, this._matchService, this._inningsService,
this._currentUserId);
this._currentUserId)
: _ballScoreCollection = _firestore
.collection(FireStoreConst.ballScoresCollection)
.withConverter(
fromFirestore: BallScoreModel.fromFireStore,
toFirestore: (BallScoreModel score, _) => score.toJson());

Future<void> addBallScoreAndUpdateTeamDetails({
required BallScoreModel score,
Expand All @@ -44,8 +50,7 @@ class BallScoreService {
MatchPlayerRequest? updatedPlayer,
}) async {
try {
DocumentReference scoreRef =
_firestore.collection(FireStoreConst.ballScoresCollection).doc();
final scoreRef = _ballScoreCollection.doc();
await _firestore.runTransaction(maxAttempts: 1, (transaction) async {
final overCount =
double.parse("${score.over_number - 1}.${score.ball_number}");
Expand All @@ -72,7 +77,7 @@ class BallScoreService {
// add ball
transaction.set(
scoreRef,
score.copyWith(id: scoreRef.id).toJson(),
score.copyWith(id: scoreRef.id),
SetOptions(merge: true),
);
}).catchError((error, stack) {
Expand All @@ -85,68 +90,29 @@ class BallScoreService {

Stream<List<BallScoreChange>> getBallScoresStreamByInningIds(
List<String> inningIds) {
StreamController<List<BallScoreChange>> controller =
StreamController<List<BallScoreChange>>();

_firestore
.collection(FireStoreConst.ballScoresCollection)
return _ballScoreCollection
.where(FireStoreConst.inningId, whereIn: inningIds)
.snapshots()
.listen((QuerySnapshot<Map<String, dynamic>> snapshot) {
try {
List<BallScoreChange> changes = [];
for (var doc in snapshot.docChanges) {
final data = doc.doc.data();
if (data != null) {
changes
.add(BallScoreChange(doc.type, BallScoreModel.fromJson(data)));
}
}
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;
.map((event) => event.docChanges
.map((score) => BallScoreChange(score.type, score.doc.data()!))
.toList())
.handleError((error, stack) => throw AppError.fromError(error, stack));
}

Stream<List<BallScoreModel>> getCurrentUserRelatedBalls() {
if (_currentUserId == null) {
return Stream.value([]);
}
StreamController<List<BallScoreModel>> controller =
StreamController<List<BallScoreModel>>();
_firestore
.collection(FireStoreConst.ballScoresCollection)

return _ballScoreCollection
.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;
.map((event) => event.docs.map((score) => score.data()).toList())
.handleError((error, stack) => throw AppError.fromError(error, stack));
}

Future<void> deleteBallAndUpdateTeamDetails({
Expand Down Expand Up @@ -185,9 +151,7 @@ class BallScoreService {
runs: totalBowlingTeamRuns);

// delete ball
final docRef = _firestore
.collection(FireStoreConst.ballScoresCollection)
.doc(ballId);
final docRef = _ballScoreCollection.doc(ballId);
transaction.delete(docRef);
});
} catch (error, stack) {
Expand Down
Loading

0 comments on commit 2bf1835

Please sign in to comment.