Skip to content

Commit

Permalink
fix: search history not loading on login
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharlock10 committed Mar 11, 2024
1 parent f82012c commit 7ee7564
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/providers/champions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ 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/auth.dart" as auth_provider;
import "package:paladinsedge/utilities/index.dart" as utilities;

class _ChampionsNotifier extends ChangeNotifier {
final ChangeNotifierProviderRef<_ChampionsNotifier> ref;

/// loading state for combinedChampions
bool isLoadingCombinedChampions = false;

Expand Down Expand Up @@ -41,6 +44,8 @@ class _ChampionsNotifier extends ChangeNotifier {
// holds the list of favourite champions of the user
Set<int> favouriteChampions = {};

_ChampionsNotifier({required this.ref});

/// Finds and returns the champion from its championId
models.Champion? findChampion(int championId) {
return champions.firstOrNullWhere((_) => _.championId == championId);
Expand All @@ -52,12 +57,14 @@ class _ChampionsNotifier extends ChangeNotifier {
// don't show loading indicator when refreshing
if (!forceUpdate) isLoadingCombinedChampions = true;

final player = ref.read(auth_provider.auth).userPlayer;

final result = await Future.wait([
// champions do not have forceUpdate
// return previous value if forceUpdate
_loadChampions(forceUpdate),
_loadUserPlayerChampions(forceUpdate),
_loadFavouriteChampions(),
player == null ? Future.value(null) : _loadFavouriteChampions(),
]);

// if champions is null, then abort this operation
Expand Down Expand Up @@ -326,4 +333,6 @@ class _ChampionsNotifier extends ChangeNotifier {
}

/// Provider to handle champions
final champions = ChangeNotifierProvider((_) => _ChampionsNotifier());
final champions = ChangeNotifierProvider<_ChampionsNotifier>(
(ref) => _ChampionsNotifier(ref: ref),
);
18 changes: 15 additions & 3 deletions lib/providers/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:paladinsedge/api/index.dart" as api;
import "package:paladinsedge/constants/index.dart" as constants;
import "package:paladinsedge/models/index.dart" as models;
import "package:paladinsedge/providers/auth.dart" as auth_provider;
import "package:paladinsedge/utilities/index.dart" as utilities;

class _SearchNotifier extends ChangeNotifier {
final ChangeNotifierProviderRef<_SearchNotifier> ref;
List<api.LowerSearch> lowerSearchList = [];
List<models.Player> topSearchList = [];
List<models.SearchHistory> searchHistory = [];

_SearchNotifier({required this.ref});

// Loads the `searchHistory` data for the user from local db and
/// syncs it with server for showing in Search screen
void loadSearchHistory() async {
Expand Down Expand Up @@ -53,6 +57,9 @@ class _SearchNotifier extends ChangeNotifier {
required String playerName,
required String playerId,
}) async {
// insert in history, but save it only if the user is not a guest
final isGuest = ref.read(auth_provider.auth).isGuest;

// remove existing searchItem
final index = searchHistory.indexWhere((_) => _.playerId == playerId);
if (index != -1) searchHistory.removeAt(index);
Expand All @@ -66,8 +73,11 @@ class _SearchNotifier extends ChangeNotifier {
searchHistory.insert(0, searchItem);

// remove all entries from searchBox and reinsert entries
await utilities.Database.searchHistoryBox?.clear();
searchHistory.forEach(utilities.Database.saveSearchHistory);
// only works if user is not a guest
if (!isGuest) {
await utilities.Database.searchHistoryBox?.clear();
searchHistory.forEach(utilities.Database.saveSearchHistory);
}
}

Future<api.SearchPlayersResponse?> searchByName({
Expand Down Expand Up @@ -131,4 +141,6 @@ class _SearchNotifier extends ChangeNotifier {
}

/// Provider to handle baseRanks
final search = ChangeNotifierProvider((_) => _SearchNotifier());
final search = ChangeNotifierProvider<_SearchNotifier>(
(ref) => _SearchNotifier(ref: ref),
);

0 comments on commit 7ee7564

Please sign in to comment.