Skip to content

Commit

Permalink
feat: add family providers
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharlock10 committed Mar 5, 2024
1 parent 90b3d78 commit 90e4db2
Show file tree
Hide file tree
Showing 41 changed files with 613 additions and 577 deletions.
9 changes: 6 additions & 3 deletions lib/providers/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class _AuthNotifier extends ChangeNotifier {
final firebaseAuthResponse = await _getFirebaseAuthCredentials();
final idToken = firebaseAuthResponse.idToken;

print("ID TOKEN : $idToken");

if (firebaseAuthResponse.errorCode == 0) {
// user has closed the popup window
return data_classes.SignInProviderResponse(result: false);
Expand Down Expand Up @@ -212,9 +214,8 @@ class _AuthNotifier extends ChangeNotifier {
clearData();
ref.read(providers.champions).clearData();
ref.read(providers.loadout).clearData();
ref.read(providers.matches).clearData();
ref.read(providers.players).clearData();
ref.read(providers.appState).clearData();
ref.read(providers.search).clearData();

// clear values from the database and utilities
utilities.Database.clear();
Expand Down Expand Up @@ -311,7 +312,7 @@ class _AuthNotifier extends ChangeNotifier {
(_) => _.match.matchId == matchId,
);
if (combinedMatch == null) {
final matchDetails = ref.read(providers.matches).matchDetails;
final matchDetails = ref.read(providers.matches(matchId)).matchDetails;
if (matchDetails != null && matchDetails.match.matchId == matchId) {
combinedMatch = data_classes.CombinedMatch(
match: matchDetails.match,
Expand Down Expand Up @@ -433,6 +434,7 @@ class _AuthNotifier extends ChangeNotifier {
try {
googleUser = await GoogleSignIn().signIn();
} catch (error) {
print("Error 1 :: ${error.toString()}");
if (error is PlatformException) {
return _GetFirebaseAuthResponse(
errorCode: 0,
Expand All @@ -457,6 +459,7 @@ class _AuthNotifier extends ChangeNotifier {
try {
googleAuth = await googleUser.authentication;
} catch (error) {
print("Error 2 :: $error");
return _GetFirebaseAuthResponse(
errorCode: 3,
errorMessage: error.toString(),
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export "./loadout.dart";
export "./matches.dart";
export "./players.dart";
export "./queue.dart";
export "./search.dart";
export "./top_matches.dart";
249 changes: 8 additions & 241 deletions lib/providers/matches.dart
Original file line number Diff line number Diff line change
@@ -1,103 +1,22 @@
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:paladinsedge/api/index.dart" as api;
import "package:paladinsedge/data_classes/index.dart" as data_classes;
import "package:paladinsedge/models/index.dart" as models;
import "package:paladinsedge/providers/champions.dart" as champions_provider;
import "package:paladinsedge/utilities/index.dart" as utilities;

class _MatchesNotifier extends ChangeNotifier {
final String matchId;
final ChangeNotifierProviderRef<_MatchesNotifier> ref;

/// Match detail
bool isMatchDetailsLoading = false;
api.MatchDetailsResponse? matchDetails;

/// Player matches
bool isPlayerMatchesLoading = false;
String? combinedMatchesPlayerId;
List<data_classes.CombinedMatch>? combinedMatches;
int? playerStreak;
_MatchesNotifier({
required this.matchId,
required this.ref,
});

/// Common matches
bool isCommonMatchesLoading = false;
String? commonMatchesPlayerId;
List<data_classes.CombinedMatch>? commonMatches;

/// Top matches
bool isTopMatchesLoading = true;
List<models.TopMatch>? topMatches;

/// Matches filter and sorting
String selectedSort = data_classes.MatchSort.defaultSort;
data_classes.SelectedMatchFilter selectedFilter =
data_classes.SelectedMatchFilter();

_MatchesNotifier({required this.ref});

void resetPlayerMatches({bool forceUpdate = false}) {
if (forceUpdate) return;

isPlayerMatchesLoading = true;
combinedMatches = null;
combinedMatchesPlayerId = null;
playerStreak = null;
utilities.postFrameCallback(notifyListeners);
}

/// get the matches for this playerId
Future<void> getPlayerMatches({
required String playerId,
bool forceUpdate = false,
}) async {
final response = await api.MatchRequests.playerMatches(
playerId: playerId,
forceUpdate: forceUpdate,
);
if (!forceUpdate) isPlayerMatchesLoading = false;

if (response == null) return notifyListeners();

// create list of combinedMatches using a temp. map
final Map<String, data_classes.CombinedMatch> tempMatchesMap = {};
for (final match in response.matches) {
tempMatchesMap[match.matchId] = data_classes.CombinedMatch(
match: match,
matchPlayers: [],
);
}
for (final matchPlayer in response.matchPlayers) {
final existingCombinedMatch = tempMatchesMap[matchPlayer.matchId];
if (existingCombinedMatch == null) continue;

tempMatchesMap[matchPlayer.matchId] = existingCombinedMatch.copyWith(
matchPlayers: [...existingCombinedMatch.matchPlayers, matchPlayer],
);
}

combinedMatchesPlayerId = playerId;
combinedMatches = tempMatchesMap.values.toList();

// sort combinedMatches based on the selectedSort
if (combinedMatches != null) {
combinedMatches = data_classes.MatchSort.getSortedMatches(
combinedMatches: combinedMatches!,
sort: selectedSort,
);
}

// get the playerStreak
playerStreak = utilities.getPlayerStreak(
combinedMatches,
combinedMatchesPlayerId,
);

if (!forceUpdate) isPlayerMatchesLoading = false;

notifyListeners();
}

Future<void> getMatchDetails(String matchId) async {
Future<void> getMatchDetails() async {
isMatchDetailsLoading = true;
utilities.postFrameCallback(notifyListeners);

Expand All @@ -109,25 +28,6 @@ class _MatchesNotifier extends ChangeNotifier {
// sort players based on their team
matchDetails?.matchPlayers.sort((a, b) => a.team.compareTo(b.team));

final index = combinedMatches?.indexWhere(
(combinedMatch) =>
combinedMatch.match.matchId == matchDetails?.match.matchId,
);
if (index != null &&
index >= 0 &&
combinedMatches != null &&
matchDetails != null) {
final combinedMatch = combinedMatches!.elementAt(index);
if (combinedMatch.match.isInComplete) {
combinedMatches![index] = data_classes.CombinedMatch(
match: matchDetails!.match,
matchPlayers: matchDetails!.matchPlayers,
hide: combinedMatch.hide,
);
combinedMatches = [...combinedMatches!];
}
}

notifyListeners();
}

Expand All @@ -137,147 +37,14 @@ class _MatchesNotifier extends ChangeNotifier {
utilities.postFrameCallback(notifyListeners);
}

/// Set value of sort and apply sorting
void setSort(String sort) {
if (combinedMatches == null) return;

selectedSort = sort;
combinedMatches = data_classes.MatchSort.getSortedMatches(
combinedMatches: combinedMatches!,
sort: sort,
);

notifyListeners();
}

/// Set value of filter and apply the filter
void setFilterValue(
String? filterName,
data_classes.MatchFilterValue? filterValue,
) {
if (combinedMatches == null || combinedMatchesPlayerId == null) return;

selectedFilter = data_classes.SelectedMatchFilter(
name: filterName,
value: filterValue,
);
final champions = ref.read(champions_provider.champions).champions;

combinedMatches = selectedFilter.isValid
? data_classes.MatchFilter.getFilteredMatches(
combinedMatches: combinedMatches!,
filter: selectedFilter,
champions: champions,
playerId: combinedMatchesPlayerId!,
)
: data_classes.MatchFilter.clearFilters(combinedMatches!);

notifyListeners();
}

void getCommonMatches({
required String userPlayerId,
required String playerId,
}) async {
isCommonMatchesLoading = true;
commonMatchesPlayerId = playerId;
utilities.postFrameCallback(notifyListeners);

final response = await api.MatchRequests.commonMatches(
playerIds: [userPlayerId, playerId],
);
if (response == null) {
isCommonMatchesLoading = false;
commonMatches = null;
notifyListeners();

return;
}

// create list of commonMatches using a temp. map
final Map<String, data_classes.CombinedMatch> tempMatchesMap = {};
for (final match in response.matches) {
tempMatchesMap[match.matchId] = data_classes.CombinedMatch(
match: match,
matchPlayers: [],
);
}
for (final matchPlayer in response.matchPlayers) {
final existingCombinedMatch = tempMatchesMap[matchPlayer.matchId];
if (existingCombinedMatch == null) continue;

tempMatchesMap[matchPlayer.matchId] = existingCombinedMatch.copyWith(
matchPlayers: [...existingCombinedMatch.matchPlayers, matchPlayer],
);
}

isCommonMatchesLoading = false;
commonMatches = tempMatchesMap.values.toList();

// sort commonMatches based on date
if (commonMatches != null) {
commonMatches = data_classes.MatchSort.getSortedMatches(
combinedMatches: commonMatches!,
sort: data_classes.MatchSort.defaultSort,
);
}

notifyListeners();
}

/// Clears all applied filters and sort on combinedMatches
void clearAppliedFiltersAndSort() {
if (combinedMatches == null) return;

combinedMatches = data_classes.MatchFilter.clearFilters(combinedMatches!);
combinedMatches = data_classes.MatchSort.clearSorting(combinedMatches!);
selectedFilter = data_classes.SelectedMatchFilter(
name: selectedFilter.name,
);
selectedSort = data_classes.MatchSort.defaultSort;

utilities.postFrameCallback(notifyListeners);
}

/// Loads the `topMatches` data from local db and syncs it with server
Future<void> loadTopMatches(bool forceUpdate) async {
final savedTopMatches =
forceUpdate ? null : utilities.Database.getTopMatches();

if (savedTopMatches != null) {
isTopMatchesLoading = false;
topMatches = savedTopMatches;

return utilities.postFrameCallback(notifyListeners);
}

final response = await api.MatchRequests.topMatches();
if (response == null) return;

isTopMatchesLoading = false;
topMatches = response.topMatches;
notifyListeners();

// save topMatches locally for future use
// clear topMatches first if forceUpdate
if (forceUpdate) await utilities.Database.topMatchBox?.clear();
topMatches?.forEach(utilities.Database.saveTopMatch);
}

/// Clears all user sensitive data upon logout
void clearData() {
isPlayerMatchesLoading = false;
isMatchDetailsLoading = false;
isCommonMatchesLoading = false;
matchDetails = null;
combinedMatchesPlayerId = null;
combinedMatches = null;
commonMatchesPlayerId = null;
commonMatches = null;
}
}

/// Provider to handle matches
final matches = ChangeNotifierProvider<_MatchesNotifier>(
(ref) => _MatchesNotifier(ref: ref),
final matches = ChangeNotifierProvider.family<_MatchesNotifier, String>(
(ref, matchId) => _MatchesNotifier(ref: ref, matchId: matchId),
);
Loading

0 comments on commit 90e4db2

Please sign in to comment.