From 646e9ed20c4af9f22459f9ba47b0a425fb050556 Mon Sep 17 00:00:00 2001 From: Tushar Jain Date: Sat, 24 Feb 2024 21:28:22 +0530 Subject: [PATCH] fix(playermatches): does not recalc. playerStreak after fetching resolves #437 --- lib/providers/matches.dart | 8 +++++ lib/screens/home/home.dart | 1 + lib/screens/main/main.dart | 2 +- lib/screens/player_detail/player_detail.dart | 32 ++----------------- ...player_detail_header_expandable_panel.dart | 2 +- lib/utilities/match.dart | 32 +++++++++++++++++++ lib/widgets/text_chip.dart | 6 ++-- 7 files changed, 48 insertions(+), 35 deletions(-) diff --git a/lib/providers/matches.dart b/lib/providers/matches.dart index c79e61f..932b443 100644 --- a/lib/providers/matches.dart +++ b/lib/providers/matches.dart @@ -17,6 +17,7 @@ class _MatchesNotifier extends ChangeNotifier { bool isPlayerMatchesLoading = false; String? combinedMatchesPlayerId; List? combinedMatches; + int? playerStreak; /// Common matches bool isCommonMatchesLoading = false; @@ -40,6 +41,7 @@ class _MatchesNotifier extends ChangeNotifier { isPlayerMatchesLoading = true; combinedMatches = null; combinedMatchesPlayerId = null; + playerStreak = null; utilities.postFrameCallback(notifyListeners); } @@ -84,6 +86,12 @@ class _MatchesNotifier extends ChangeNotifier { ); } + // get the playerStreak + playerStreak = utilities.getPlayerStreak( + combinedMatches, + combinedMatchesPlayerId, + ); + if (!forceUpdate) isPlayerMatchesLoading = false; notifyListeners(); diff --git a/lib/screens/home/home.dart b/lib/screens/home/home.dart index a7caad3..dd4197c 100644 --- a/lib/screens/home/home.dart +++ b/lib/screens/home/home.dart @@ -23,6 +23,7 @@ class Home extends HookConsumerWidget { providers.auth.select((_) => _.user?.favouriteFriends), ); final isGuest = ref.watch(providers.auth.select((_) => _.isGuest)); + // State final isRefreshing = useState(false); diff --git a/lib/screens/main/main.dart b/lib/screens/main/main.dart index 452e611..72153de 100644 --- a/lib/screens/main/main.dart +++ b/lib/screens/main/main.dart @@ -55,7 +55,7 @@ class Main extends HookConsumerWidget { return Scaffold( drawer: const AppDrawer(), drawerEnableOpenDragGesture: constants.isMobile, - drawerEdgeDragWidth: MediaQuery.of(context).size.width / 2, + drawerEdgeDragWidth: MediaQuery.of(context).size.width / 3, onDrawerChanged: onDrawerChanged, body: MainPagesStack(selectedIndex: bottomTabIndex), bottomNavigationBar: MainBottomTabs( diff --git a/lib/screens/player_detail/player_detail.dart b/lib/screens/player_detail/player_detail.dart index 42e6369..e27f6d8 100644 --- a/lib/screens/player_detail/player_detail.dart +++ b/lib/screens/player_detail/player_detail.dart @@ -9,7 +9,6 @@ import "package:paladinsedge/screens/index.dart" as screens; import "package:paladinsedge/screens/player_detail/player_detail_header.dart"; import "package:paladinsedge/screens/player_detail/player_detail_matches.dart"; import "package:paladinsedge/screens/player_detail/player_detail_menu.dart"; -import "package:paladinsedge/utilities/index.dart" as utilities; import "package:paladinsedge/widgets/index.dart" as widgets; class PlayerDetail extends HookConsumerWidget { @@ -51,8 +50,8 @@ class PlayerDetail extends HookConsumerWidget { final playerInferred = ref.watch( providers.players.select((_) => _.playerInferred), ); - final combinedMatches = ref.watch( - providers.matches.select((_) => _.combinedMatches), + final playerStreak = ref.watch( + providers.matches.select((_) => _.playerStreak), ); // Variables @@ -65,33 +64,6 @@ class PlayerDetail extends HookConsumerWidget { // State final isRefreshing = useState(false); - // Hooks - final playerStreak = useMemoized( - () { - if (combinedMatches == null) return null; - if (combinedMatches.length < 5) return null; - if (combinedMatchesPlayerId == null) return null; - - final playerId = combinedMatchesPlayerId; - final firstCombinedMatch = combinedMatches.first; - final isFirstWin = utilities.didPlayerWin(firstCombinedMatch, playerId); - int streak = 1; - - for (final combinedMatch in combinedMatches.skip(1)) { - final isWin = utilities.didPlayerWin(combinedMatch, playerId); - if (isFirstWin == isWin) { - streak++; - } else { - break; - } - } - if (streak < 3) return null; - - return isFirstWin ? streak : -streak; - }, - [combinedMatches, combinedMatchesPlayerId], - ); - // Methods final getPlayerDetails = useCallback( ({bool forceUpdate = false}) async { diff --git a/lib/screens/player_detail/player_detail_header_expandable_panel.dart b/lib/screens/player_detail/player_detail_header_expandable_panel.dart index 8e1cbf7..d849968 100644 --- a/lib/screens/player_detail/player_detail_header_expandable_panel.dart +++ b/lib/screens/player_detail/player_detail_header_expandable_panel.dart @@ -49,7 +49,7 @@ class _RecentMatchesBar extends HookConsumerWidget { } for (final combinedMatch in filteredCombinedMatches) { - if (combinedMatch.match.queue.contains("Training")) continue; + if (utilities.isTrainingMatch(combinedMatch.match)) continue; final matchPlayer = utilities.getMatchPlayerFromPlayerId( combinedMatch.matchPlayers, diff --git a/lib/utilities/match.dart b/lib/utilities/match.dart index dc195ce..4ed8b88 100644 --- a/lib/utilities/match.dart +++ b/lib/utilities/match.dart @@ -2,6 +2,10 @@ import "package:dartx/dartx.dart"; import "package:paladinsedge/data_classes/index.dart" as data_classes; import "package:paladinsedge/models/index.dart" as models; +bool isTrainingMatch(models.Match match) { + return match.queue.contains("Training"); +} + models.MatchPlayer? getMatchPlayerFromPlayerId( List matchPlayers, String? playerId, @@ -21,3 +25,31 @@ bool didPlayerWin( return matchPlayer.team == combinedMatch.match.winningTeam; } + +int? getPlayerStreak( + List? combinedMatches, + String? combinedMatchesPlayerId, +) { + if (combinedMatches == null) return null; + if (combinedMatches.length < 5) return null; + if (combinedMatchesPlayerId == null) return null; + + final playerId = combinedMatchesPlayerId; + final firstCombinedMatch = combinedMatches.first; + final isFirstWin = didPlayerWin(firstCombinedMatch, playerId); + int streak = 1; + + for (final combinedMatch in combinedMatches.skip(1)) { + if (isTrainingMatch(combinedMatch.match)) continue; + + final isWin = didPlayerWin(combinedMatch, playerId); + if (isFirstWin == isWin) { + streak++; + } else { + break; + } + } + if (streak < 3) return null; + + return isFirstWin ? streak : -streak; +} diff --git a/lib/widgets/text_chip.dart b/lib/widgets/text_chip.dart index 576e4cf..d2c9c95 100644 --- a/lib/widgets/text_chip.dart +++ b/lib/widgets/text_chip.dart @@ -21,12 +21,12 @@ class TextChip extends StatelessWidget { this.width, this.height, this.onTap, - this.textSize = 10, + this.textSize = 8, this.color = Colors.grey, this.spacing = 0, this.trailingIcon, - this.iconSize = 12, - this.trailingIconSize = 12, + this.iconSize = 10, + this.trailingIconSize = 10, this.highlightText, Key? key, }) : super(key: key);