Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed Feb 6, 2024
1 parent c8c1ce7 commit 6c49dd3
Show file tree
Hide file tree
Showing 18 changed files with 516 additions and 385 deletions.
2 changes: 1 addition & 1 deletion data/.flutter-plugins-dependencies

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions data/lib/extensions/string_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension StringManipulator on String {
String get caseAndSpaceInsensitive {
return trim().replaceAll(RegExp(r'\s+'), "").toLowerCase();
}
}
161 changes: 72 additions & 89 deletions data/lib/service/team/team_service.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
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:data/extensions/string_extensions.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../storage/app_preferences.dart';

final teamServiceProvider = Provider((ref) {
final service = TeamService(FirebaseFirestore.instance);
ref.listen(currentUserPod, (_, next) => service.currentUserId = next?.id);
final service =
TeamService(ref.read(currentUserPod)?.id, FirebaseFirestore.instance);

ref.listen(currentUserPod, (_, next) => service._currentUserId = next?.id);
return service;
});

class TeamService {
String? currentUserId;
String? _currentUserId;
final FirebaseFirestore _firestore;
final String collectionName = 'teams';
final String subCollectionName = 'players';
final String _collectionName = 'teams';
final String _subCollectionName = 'players';

TeamService(this._currentUserId, this._firestore);

TeamService(this._firestore);
Future<String> updateTeam(TeamModel team, List<UserModel> players) async {
DocumentReference teamRef =
_firestore.collection(_collectionName).doc(team.id);
WriteBatch batch = _firestore.batch();

batch.set(teamRef, team.toJson(), SetOptions(merge: true));
String newTeamId = teamRef.id;

for (var player in players) {
DocumentReference playerRef =
teamRef.collection(_subCollectionName).doc(player.id);
batch.set(playerRef, player.toJson(), SetOptions(merge: true));
}

if (team.id == null) {
batch.update(teamRef, {'id': newTeamId});
}
await batch.commit();
return newTeamId;
}

Future<TeamModel> getTeamById(String teamId) async {
CollectionReference teamsCollection = _firestore.collection(collectionName);
CollectionReference teamsCollection =
_firestore.collection(_collectionName);

DocumentSnapshot teamDoc = await teamsCollection.doc(teamId).get();

TeamModel team = TeamModel.fromJson(teamDoc.data() as Map<String, dynamic>);

CollectionReference playersCollection =
teamDoc.reference.collection(subCollectionName);
teamDoc.reference.collection(_subCollectionName);
QuerySnapshot playersSnapshot = await playersCollection.get();

final players = playersSnapshot.docs.map((playerDoc) {
Expand All @@ -37,9 +62,9 @@ class TeamService {
return team.copyWith(players: players);
}

// get Team with Players
Future<List<TeamModel>> getTeamsWithPlayers() async {
CollectionReference teamsCollection = _firestore.collection(collectionName);
Future<List<TeamModel>> getTeams() async {
CollectionReference teamsCollection =
_firestore.collection(_collectionName);

QuerySnapshot mainCollectionSnapshot = await teamsCollection.get();

Expand All @@ -49,129 +74,87 @@ class TeamService {
TeamModel team =
TeamModel.fromJson(mainDoc.data() as Map<String, dynamic>);

CollectionReference playersCollection =
mainDoc.reference.collection(subCollectionName);

QuerySnapshot playersSnapshot = await playersCollection.get();

final players = playersSnapshot.docs.map((playerDoc) {
return UserModel.fromJson(playerDoc.data() as Map<String, dynamic>);
}).toList();

team = team.copyWith(players: players);
teams.add(team);
if (team.created_by == _currentUserId) {
CollectionReference playersCollection =
mainDoc.reference.collection(_subCollectionName);

QuerySnapshot playersSnapshot = await playersCollection.get();

final players = playersSnapshot.docs.map((playerDoc) {
return UserModel.fromJson(playerDoc.data() as Map<String, dynamic>);
}).toList();

team = team.copyWith(players: players);
teams.add(team);
} else {
CollectionReference playersCollection =
mainDoc.reference.collection(_subCollectionName);
QuerySnapshot playersSnapshot = await playersCollection
.where('id', isEqualTo: _currentUserId)
.get();

if (playersSnapshot.docs.isNotEmpty) {
final players = playersSnapshot.docs.map((playerDoc) {
return UserModel.fromJson(playerDoc.data() as Map<String, dynamic>);
}).toList();

team = team.copyWith(players: players);
teams.add(team);
}
}
}

return teams;
}

// update team
Future<void> updateTeam(TeamModel team, List<UserModel> players) async {
DocumentReference teamRef =
_firestore.collection(collectionName).doc(team.id);

WriteBatch batch = _firestore.batch();

batch.set(teamRef, team.toJson(), SetOptions(merge: true));

for (var player in players) {
DocumentReference playerRef =
teamRef.collection(subCollectionName).doc(player.id);
batch.set(playerRef, player.toJson(), SetOptions(merge: true));
}

await batch.commit();
}

// delete team
Future<void> deleteTeam(String teamId) async {
await _firestore.collection(collectionName).doc(teamId).delete();
}

Future<void> deleteTeamWithCollection(String teamId) async {
await _firestore.runTransaction((transaction) async {
CollectionReference teamCollection =
_firestore.collection(collectionName);
_firestore.collection(_collectionName);
DocumentReference teamDocRef = teamCollection.doc(teamId);

// Delete sub-collection
CollectionReference subCollection =
teamDocRef.collection(subCollectionName);
teamDocRef.collection(_subCollectionName);
QuerySnapshot subCollectionSnapshot = await subCollection.get();
for (QueryDocumentSnapshot docSnapshot in subCollectionSnapshot.docs) {
transaction.delete(docSnapshot.reference);
}

// Delete team document
transaction.delete(teamDocRef);
});
}

// add players plural
Future<void> addPlayersToTeam(String teamId, List<UserModel> players) async {
DocumentReference teamRef =
_firestore.collection(collectionName).doc(teamId);
_firestore.collection(_collectionName).doc(teamId);

WriteBatch batch = _firestore.batch();

for (var player in players) {
DocumentReference playerRef =
teamRef.collection(subCollectionName).doc(player.id);
teamRef.collection(_subCollectionName).doc(player.id);
batch.set(playerRef, player.toJson(), SetOptions(merge: true));
}

await batch.commit();
}

// remove players plural
Future<void> removePlayersFromTeam(
String teamId, List<String> playerIds) async {
CollectionReference playersCollection = _firestore
.collection(collectionName)
.collection(_collectionName)
.doc(teamId)
.collection(subCollectionName);
.collection(_subCollectionName);

for (String playerId in playerIds) {
playersCollection.doc(playerId).delete();
}
}

// add player singular
Future<void> addPlayerToTeam(String teamId, UserModel player) async {
CollectionReference playersCollection = _firestore
.collection(collectionName)
.doc(teamId)
.collection(subCollectionName);

await playersCollection.add(player.toJson());
}

// update player singular
Future<void> updatePlayerInTeam(
String teamId, String playerId, UserModel updatedPlayer) async {
CollectionReference playersCollection = _firestore
.collection(collectionName)
.doc(teamId)
.collection(subCollectionName);

await playersCollection.doc(playerId).update(updatedPlayer.toJson());
}

// delete player singular
Future<void> deletePlayerFromTeam(String teamId, String playerId) async {
CollectionReference playersCollection = _firestore
.collection(collectionName)
.doc(teamId)
.collection(subCollectionName);

await playersCollection.doc(playerId).delete();
}

// is Team name available
Future<bool> isTeamNameAvailable(String teamName) async {
QuerySnapshot teamSnap = await _firestore
.collection(collectionName)
.where('name_lowercase', isEqualTo: teamName.toLowerCase())
.collection(_collectionName)
.where('name_lowercase', isEqualTo: teamName.caseAndSpaceInsensitive)
.get();

return teamSnap.docs.isEmpty;
Expand Down
23 changes: 22 additions & 1 deletion khelo/assets/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
"common_sign_out_title": "Sign out",
"common_add_title": "Add",
"common_save_title": "Save",
"common_not_specified_title": "Not Specified",
"common_obscure_phone_number_text": "+{countryCode} ***** ***{lastDigits}",
"@common_obscure_phone_number_text": {
"description": "+{countryCode} ***** ***{lastDigits}",
"placeholders": {
"countryCode": {
"type": "String"
},
"lastDigits": {
"type": "String"
}
}
},

"alert_confirm_default_title": "Are you sure?",
"alert_confirm_default_message": "Are you sure you want to {deleteText}?",
Expand Down Expand Up @@ -85,11 +98,19 @@
"add_team_add_as_member_description_text": "Add me as a team member",
"add_team_enter_team_name_placeholder_text": "Enter team name",
"add_team_location_text": "Location",
"add_team_players_text": "PLAYERS",
"add_team_add_hint_text": "Added user will be shown here, tap on '+' button to add team member.",

"add_team_member_screen_title": "Add team member",
"add_team_member_search_placeholder_text": "Search member with name",
"add_team_member_search_placeholder_text": "Search member name",
"add_team_member_verify_placeholder_text": "Enter the last five digits of the phone number of the selected player.",
"add_team_member_verify_title": "Verify",
"add_team_member_search_hint_text": "Search the person name above to add them in your team.",
"add_team_member_add_text": "ADD",
"add_team_member_added_text": "ADDED",

"team_list_add_members_title": "Add Members",
"team_list_edit_team_title": "Edit Team",

"my_game_teams_tab_title": "Teams",
"my_game_matches_tab_title": "Matches"
Expand Down
2 changes: 1 addition & 1 deletion khelo/lib/domain/extensions/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ extension EmailValidator on String {
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
.hasMatch(this);
}
}
}
2 changes: 1 addition & 1 deletion khelo/lib/ui/flow/profile/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'package:style/text/app_text_style.dart';
class ProfileScreen extends ConsumerWidget {
const ProfileScreen({super.key});

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

0 comments on commit 6c49dd3

Please sign in to comment.