diff --git a/khelo/lib/ui/flow/home/home_screen.dart b/khelo/lib/ui/flow/home/home_screen.dart index 864855e0..3d08f62f 100644 --- a/khelo/lib/ui/flow/home/home_screen.dart +++ b/khelo/lib/ui/flow/home/home_screen.dart @@ -15,13 +15,40 @@ import 'package:style/indicator/progress_indicator.dart'; import 'package:style/page_views/expandable_page_view.dart'; import 'package:style/text/app_text_style.dart'; -class HomeScreen extends ConsumerWidget { +class HomeScreen extends ConsumerStatefulWidget { const HomeScreen({super.key}); @override - Widget build(BuildContext context, WidgetRef ref) { + ConsumerState createState() => _HomeScreenState(); +} + +class _HomeScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { + late HomeViewNotifier notifier; + + @override + bool get wantKeepAlive => true; + + @override + void initState() { + WidgetsBinding.instance.addObserver(this); + super.initState(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + notifier.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + + @override + Widget build(BuildContext context) { + super.build(context); final state = ref.watch(homeViewStateProvider); - final notifier = ref.watch(homeViewStateProvider.notifier); + notifier = ref.watch(homeViewStateProvider.notifier); return AppPage( title: context.l10n.home_screen_title, @@ -63,8 +90,7 @@ class HomeScreen extends ConsumerWidget { return ExpandablePageView( itemCount: state.matches.length, itemBuilder: (context, index) { - return _matchCell( - context, state.matches[index]); + return _matchCell(context, state.matches[index]); }, ); } diff --git a/khelo/lib/ui/flow/home/home_view_model.dart b/khelo/lib/ui/flow/home/home_view_model.dart index 41be2cd0..b6317847 100644 --- a/khelo/lib/ui/flow/home/home_view_model.dart +++ b/khelo/lib/ui/flow/home/home_view_model.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'package:data/api/match/match_model.dart'; import 'package:data/service/match/match_service.dart'; import 'package:flutter/cupertino.dart'; @@ -7,12 +8,13 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'home_view_model.freezed.dart'; final homeViewStateProvider = - StateNotifierProvider.autoDispose( + StateNotifierProvider( (ref) => HomeViewNotifier(ref.read(matchServiceProvider)), ); class HomeViewNotifier extends StateNotifier { final MatchService _matchService; + late StreamSubscription _streamSubscription; HomeViewNotifier(this._matchService) : super(const HomeViewState()) { loadMatches(); @@ -21,7 +23,7 @@ class HomeViewNotifier extends StateNotifier { void loadMatches() async { state = state.copyWith(loading: state.matches.isEmpty); - _matchService.getRunningMatches().listen( + _streamSubscription = _matchService.getRunningMatches().listen( (matches) { state = state.copyWith(matches: matches, loading: false, error: null); }, @@ -31,6 +33,16 @@ class HomeViewNotifier extends StateNotifier { }, ); } + + _cancelStreamSubscription() async { + await _streamSubscription.cancel(); + } + + @override + void dispose() { + _cancelStreamSubscription(); + super.dispose(); + } } @freezed diff --git a/khelo/lib/ui/flow/main/main_screen.dart b/khelo/lib/ui/flow/main/main_screen.dart index 999ea464..12fdcbfd 100644 --- a/khelo/lib/ui/flow/main/main_screen.dart +++ b/khelo/lib/ui/flow/main/main_screen.dart @@ -18,7 +18,8 @@ class MainScreen extends ConsumerStatefulWidget { ConsumerState createState() => _MainScreenState(); } -class _MainScreenState extends ConsumerState { +class _MainScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { static final List _widgets = [ const HomeScreen(), const MyGameTabScreen(), @@ -29,8 +30,28 @@ class _MainScreenState extends ConsumerState { final _materialPageController = PageController(); final _cupertinoTabController = CupertinoTabController(); + @override + bool get wantKeepAlive => true; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addObserver(this); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + _materialPageController.dispose(); + _cupertinoTabController.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + @override Widget build(BuildContext context) { + super.build(context); if (Platform.isIOS) { return _cupertinoTabs(context); } diff --git a/khelo/lib/ui/flow/matches/match_list_screen.dart b/khelo/lib/ui/flow/matches/match_list_screen.dart index 7e7d22b0..077c081f 100644 --- a/khelo/lib/ui/flow/matches/match_list_screen.dart +++ b/khelo/lib/ui/flow/matches/match_list_screen.dart @@ -17,13 +17,40 @@ import 'package:style/text/app_text_style.dart'; import 'match_list_view_model.dart'; -class MatchListScreen extends ConsumerWidget { +class MatchListScreen extends ConsumerStatefulWidget { const MatchListScreen({super.key}); @override - Widget build(BuildContext context, WidgetRef ref) { + ConsumerState createState() => _MatchListScreenState(); +} + +class _MatchListScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { + late MatchListViewNotifier notifier; + + @override + bool get wantKeepAlive => true; + + @override + void initState() { + WidgetsBinding.instance.addObserver(this); + super.initState(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + notifier.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + + @override + Widget build(BuildContext context) { + super.build(context); final state = ref.watch(matchListStateProvider); - final notifier = ref.watch(matchListStateProvider.notifier); + notifier = ref.watch(matchListStateProvider.notifier); return Column( children: [ @@ -58,9 +85,11 @@ class MatchListScreen extends ConsumerWidget { ); } - Widget _matchList(BuildContext context, - MatchListViewNotifier notifier, - MatchListViewState state,) { + Widget _matchList( + BuildContext context, + MatchListViewNotifier notifier, + MatchListViewState state, + ) { if (state.loading) { return const Expanded( child: Center( @@ -73,7 +102,8 @@ class MatchListScreen extends ConsumerWidget { error: state.error, onRetryTap: () { notifier.cancelStreamSubscription(); - notifier.loadMatches();}, + notifier.loadMatches(); + }, ); } @@ -104,9 +134,11 @@ class MatchListScreen extends ConsumerWidget { } } - Widget _matchCell(BuildContext context, - MatchModel match, - String? currentUserId,) { + Widget _matchCell( + BuildContext context, + MatchModel match, + String? currentUserId, + ) { return OnTapScale( onTap: () { AppRoute.matchDetailTab(matchId: match.id ?? "").push(context); @@ -131,9 +163,11 @@ class MatchListScreen extends ConsumerWidget { ); } - Widget _matchOtherDetail(BuildContext context, - MatchModel match, - String? currentUserId,) { + Widget _matchOtherDetail( + BuildContext context, + MatchModel match, + String? currentUserId, + ) { return Row( children: [ Expanded( @@ -163,9 +197,11 @@ class MatchListScreen extends ConsumerWidget { ); } - Widget _matchEditOrResumeActionButton(BuildContext context, - MatchModel match, - String? currentUserId,) { + Widget _matchEditOrResumeActionButton( + BuildContext context, + MatchModel match, + String? currentUserId, + ) { if (match.match_status != MatchStatus.finish && match.created_by == currentUserId) { return OnTapScale( @@ -207,8 +243,8 @@ class MatchListScreen extends ConsumerWidget { wicket: match.teams.last.wicket ?? 0, totalOvers: match.number_of_over, isRunning: - match.current_playing_team_id == match.teams.first.team.id && - match.match_status == MatchStatus.running, + match.current_playing_team_id == match.teams.first.team.id && + match.match_status == MatchStatus.running, ), _teamNameView( context, @@ -216,8 +252,8 @@ class MatchListScreen extends ConsumerWidget { wicket: match.teams.first.wicket ?? 0, totalOvers: match.number_of_over, isRunning: - match.current_playing_team_id == match.teams.last.team.id && - match.match_status == MatchStatus.running, + match.current_playing_team_id == match.teams.last.team.id && + match.match_status == MatchStatus.running, ), ], ), @@ -226,7 +262,8 @@ class MatchListScreen extends ConsumerWidget { ); } - Widget _teamNameView(BuildContext context, { + Widget _teamNameView( + BuildContext context, { required MatchTeamModel team, required int wicket, required int totalOvers, @@ -240,22 +277,22 @@ class MatchListScreen extends ConsumerWidget { : context.colorScheme.textPrimary), children: isRunning ? [ - TextSpan( - text: " - ", - style: AppTextStyle.subtitle1 - .copyWith(color: context.colorScheme.textSecondary), - ), - TextSpan( - text: "${team.run}/$wicket", - style: AppTextStyle.header4 - .copyWith(color: context.colorScheme.textPrimary), - ), - TextSpan( - text: " (${team.over ?? 0}/$totalOvers)", - style: AppTextStyle.body2 - .copyWith(color: context.colorScheme.textSecondary), - ), - ] + TextSpan( + text: " - ", + style: AppTextStyle.subtitle1 + .copyWith(color: context.colorScheme.textSecondary), + ), + TextSpan( + text: "${team.run}/$wicket", + style: AppTextStyle.header4 + .copyWith(color: context.colorScheme.textPrimary), + ), + TextSpan( + text: " (${team.over ?? 0}/$totalOvers)", + style: AppTextStyle.body2 + .copyWith(color: context.colorScheme.textSecondary), + ), + ] : List.empty())); } diff --git a/khelo/lib/ui/flow/matches/match_list_view_model.dart b/khelo/lib/ui/flow/matches/match_list_view_model.dart index 304f4af1..4fe022d0 100644 --- a/khelo/lib/ui/flow/matches/match_list_view_model.dart +++ b/khelo/lib/ui/flow/matches/match_list_view_model.dart @@ -8,8 +8,8 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'match_list_view_model.freezed.dart'; -final matchListStateProvider = StateNotifierProvider.autoDispose< - MatchListViewNotifier, MatchListViewState>((ref) { +final matchListStateProvider = + StateNotifierProvider((ref) { final notifier = MatchListViewNotifier( ref.read(matchServiceProvider), ref.read(currentUserPod)?.id, @@ -27,7 +27,9 @@ class MatchListViewNotifier extends StateNotifier { MatchListViewNotifier(this._matchService, String? userId) : super(MatchListViewState( currentUserId: userId, - )); + )) { + loadMatches(); + } void setUserId(String? userId) { state = state.copyWith(currentUserId: userId); @@ -41,13 +43,11 @@ class MatchListViewNotifier extends StateNotifier { state = state.copyWith(matches: matches, loading: false, error: null); }, onError: (e) { state = state.copyWith(loading: false, error: e); - debugPrint( - "MatchListViewNotifier: error while load matches -> $e"); + debugPrint("MatchListViewNotifier: error while load matches -> $e"); }); } catch (e) { state = state.copyWith(loading: false, error: e); - debugPrint( - "MatchListViewNotifier: error while load matches -> $e"); + debugPrint("MatchListViewNotifier: error while load matches -> $e"); } } diff --git a/khelo/lib/ui/flow/my_game/my_game_tab_screen.dart b/khelo/lib/ui/flow/my_game/my_game_tab_screen.dart index b3377db9..12da1eda 100644 --- a/khelo/lib/ui/flow/my_game/my_game_tab_screen.dart +++ b/khelo/lib/ui/flow/my_game/my_game_tab_screen.dart @@ -3,9 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:khelo/components/app_page.dart'; import 'package:khelo/domain/extensions/context_extensions.dart'; -import 'package:khelo/domain/extensions/widget_extension.dart'; import 'package:khelo/ui/flow/matches/match_list_screen.dart'; -import 'package:khelo/ui/flow/matches/match_list_view_model.dart'; import 'package:khelo/ui/flow/my_game/my_game_tab_view_model.dart'; import 'package:khelo/ui/flow/team/team_list_screen.dart'; import 'package:khelo/ui/flow/team/team_list_view_model.dart'; @@ -20,7 +18,8 @@ class MyGameTabScreen extends ConsumerStatefulWidget { ConsumerState createState() => _MyGameTabScreenState(); } -class _MyGameTabScreenState extends ConsumerState { +class _MyGameTabScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { final List _tabs = [ const MatchListScreen(), const TeamListScreen(), @@ -28,34 +27,36 @@ class _MyGameTabScreenState extends ConsumerState { late PageController _controller; - late MatchListViewNotifier _matchListNotifier; - late TeamListViewNotifier _teamListNotifier; - int get _selectedTab => _controller.hasClients ? _controller.page?.round() ?? 0 : _controller.initialPage; + @override + bool get wantKeepAlive => true; + @override void initState() { super.initState(); - _matchListNotifier = ref.read(matchListStateProvider.notifier); - _teamListNotifier = ref.read(teamListViewStateProvider.notifier); + WidgetsBinding.instance.addObserver(this); _controller = PageController( initialPage: ref.read(myGameTabViewStateProvider).selectedTab, ); + } - runPostFrame(() { - _matchListNotifier.loadMatches(); - _teamListNotifier.loadTeamList(); - }); + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + _controller.dispose(); + WidgetsBinding.instance.removeObserver(this); + } } @override Widget build(BuildContext context) { + super.build(context); final notifier = ref.watch(myGameTabViewStateProvider.notifier); - _matchListNotifier = ref.watch(matchListStateProvider.notifier); - _teamListNotifier = ref.watch(teamListViewStateProvider.notifier); return AppPage( body: Builder( @@ -143,4 +144,4 @@ class _MyGameTabScreenState extends ConsumerState { ), ); } -} +} \ No newline at end of file diff --git a/khelo/lib/ui/flow/stats/my_stats_tab_screen.dart b/khelo/lib/ui/flow/stats/my_stats_tab_screen.dart index 81519810..4347eb69 100644 --- a/khelo/lib/ui/flow/stats/my_stats_tab_screen.dart +++ b/khelo/lib/ui/flow/stats/my_stats_tab_screen.dart @@ -2,12 +2,9 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:khelo/components/app_page.dart'; import 'package:khelo/domain/extensions/context_extensions.dart'; -import 'package:khelo/domain/extensions/widget_extension.dart'; import 'package:khelo/ui/flow/stats/my_stats_tab_view_model.dart'; import 'package:khelo/ui/flow/stats/user_match/user_match_list_screen.dart'; -import 'package:khelo/ui/flow/stats/user_match/user_match_list_view_model.dart'; import 'package:khelo/ui/flow/stats/user_stat/user_stat_screen.dart'; -import 'package:khelo/ui/flow/stats/user_stat/user_stat_view_model.dart'; import 'package:style/animations/on_tap_scale.dart'; import 'package:style/extensions/context_extensions.dart'; import 'package:style/text/app_text_style.dart'; @@ -19,7 +16,8 @@ class MyStatsTabScreen extends ConsumerStatefulWidget { ConsumerState createState() => _MyStatsTabScreenState(); } -class _MyStatsTabScreenState extends ConsumerState { +class _MyStatsTabScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { final List _tabs = [ const UserMatchListScreen(), const UserStatScreen(), @@ -27,34 +25,36 @@ class _MyStatsTabScreenState extends ConsumerState { late PageController _controller; - late UserMatchListViewNotifier _userMatchListNotifier; - late UserStatViewNotifier _userStatNotifier; - int get _selectedTab => _controller.hasClients ? _controller.page?.round() ?? 0 : _controller.initialPage; + @override + bool get wantKeepAlive => true; + @override void initState() { super.initState(); - _userMatchListNotifier = ref.read(userMatchListStateProvider.notifier); - _userStatNotifier = ref.read(userStatViewStateProvider.notifier); - - runPostFrame(() { - _userMatchListNotifier.loadUserMatches(); - _userStatNotifier.getUserRelatedBalls(); - }); + WidgetsBinding.instance.addObserver(this); _controller = PageController( initialPage: ref.read(myStatsTabStateProvider).selectedTab, ); } + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + _controller.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + @override Widget build(BuildContext context) { + super.build(context); final notifier = ref.watch(myStatsTabStateProvider.notifier); - _userMatchListNotifier = ref.watch(userMatchListStateProvider.notifier); - _userStatNotifier = ref.watch(userStatViewStateProvider.notifier); return AppPage( title: context.l10n.my_stat_screen_title, @@ -133,4 +133,4 @@ class _MyStatsTabScreenState extends ConsumerState { ), ); } -} +} \ No newline at end of file diff --git a/khelo/lib/ui/flow/stats/user_match/user_match_list_screen.dart b/khelo/lib/ui/flow/stats/user_match/user_match_list_screen.dart index 19d4af6c..6cc4e586 100644 --- a/khelo/lib/ui/flow/stats/user_match/user_match_list_screen.dart +++ b/khelo/lib/ui/flow/stats/user_match/user_match_list_screen.dart @@ -16,13 +16,41 @@ import 'package:style/extensions/context_extensions.dart'; import 'package:style/indicator/progress_indicator.dart'; import 'package:style/text/app_text_style.dart'; -class UserMatchListScreen extends ConsumerWidget { +class UserMatchListScreen extends ConsumerStatefulWidget { const UserMatchListScreen({super.key}); @override - Widget build(BuildContext context, WidgetRef ref) { + ConsumerState createState() => _UserMatchListScreenState(); +} + +class _UserMatchListScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { + late UserMatchListViewNotifier notifier; + + @override + bool get wantKeepAlive => true; + + @override + void initState() { + WidgetsBinding.instance.addObserver(this); + super.initState(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + notifier.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + + @override + Widget build(BuildContext context) { + super.build(context); + final state = ref.watch(userMatchListStateProvider); - final notifier = ref.watch(userMatchListStateProvider.notifier); + notifier = ref.watch(userMatchListStateProvider.notifier); if (state.loading) { return const AppProgressIndicator(); diff --git a/khelo/lib/ui/flow/stats/user_match/user_match_list_view_model.dart b/khelo/lib/ui/flow/stats/user_match/user_match_list_view_model.dart index 3c295b6e..114276d7 100644 --- a/khelo/lib/ui/flow/stats/user_match/user_match_list_view_model.dart +++ b/khelo/lib/ui/flow/stats/user_match/user_match_list_view_model.dart @@ -7,7 +7,7 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'user_match_list_view_model.freezed.dart'; -final userMatchListStateProvider = StateNotifierProvider.autoDispose< +final userMatchListStateProvider = StateNotifierProvider< UserMatchListViewNotifier, UserMatchListState>((ref) { return UserMatchListViewNotifier( ref.read(matchServiceProvider), @@ -19,7 +19,9 @@ class UserMatchListViewNotifier extends StateNotifier { late StreamSubscription _matchesStreamSubscription; UserMatchListViewNotifier(this._matchService) - : super(const UserMatchListState()); + : super(const UserMatchListState()){ + loadUserMatches(); + } Future loadUserMatches() async { state = state.copyWith(loading: true); diff --git a/khelo/lib/ui/flow/stats/user_stat/user_stat_screen.dart b/khelo/lib/ui/flow/stats/user_stat/user_stat_screen.dart index 9c5ff199..b5cde099 100644 --- a/khelo/lib/ui/flow/stats/user_stat/user_stat_screen.dart +++ b/khelo/lib/ui/flow/stats/user_stat/user_stat_screen.dart @@ -8,13 +8,40 @@ import 'package:style/extensions/context_extensions.dart'; import 'package:style/indicator/progress_indicator.dart'; import 'package:style/text/app_text_style.dart'; -class UserStatScreen extends ConsumerWidget { +class UserStatScreen extends ConsumerStatefulWidget { const UserStatScreen({super.key}); @override - Widget build(BuildContext context, WidgetRef ref) { + ConsumerState createState() => _UserStatScreenState(); +} + +class _UserStatScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { + late UserStatViewNotifier notifier; + + @override + bool get wantKeepAlive => true; + + @override + void initState() { + WidgetsBinding.instance.addObserver(this); + super.initState(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + notifier.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + + @override + Widget build(BuildContext context) { + super.build(context); final state = ref.watch(userStatViewStateProvider); - final notifier = ref.watch(userStatViewStateProvider.notifier); + notifier = ref.watch(userStatViewStateProvider.notifier); if (state.loading) { return const AppProgressIndicator(); diff --git a/khelo/lib/ui/flow/stats/user_stat/user_stat_view_model.dart b/khelo/lib/ui/flow/stats/user_stat/user_stat_view_model.dart index b600e7b6..c4ca46f1 100644 --- a/khelo/lib/ui/flow/stats/user_stat/user_stat_view_model.dart +++ b/khelo/lib/ui/flow/stats/user_stat/user_stat_view_model.dart @@ -9,8 +9,7 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'user_stat_view_model.freezed.dart'; final userStatViewStateProvider = - StateNotifierProvider.autoDispose( - (ref) { + StateNotifierProvider((ref) { final notifier = UserStatViewNotifier( ref.read(ballScoreServiceProvider), ref.read(currentUserPod)?.id, @@ -26,7 +25,9 @@ class UserStatViewNotifier extends StateNotifier { late StreamSubscription _ballScoreStreamSubscription; UserStatViewNotifier(this._ballScoreService, String? userId) - : super(UserStatViewState(currentUserId: userId)); + : super(UserStatViewState(currentUserId: userId)) { + getUserRelatedBalls(); + } void setUserId(String? userId) { state = state.copyWith(currentUserId: userId); @@ -37,7 +38,8 @@ class UserStatViewNotifier extends StateNotifier { try { _ballScoreStreamSubscription = _ballScoreService.getCurrentUserRelatedBalls().listen((ballScores) { - state = state.copyWith(ballList: ballScores, loading: false, error: null); + state = + state.copyWith(ballList: ballScores, loading: false, error: null); }, onError: (e) { state = state.copyWith(error: e, loading: false); debugPrint( diff --git a/khelo/lib/ui/flow/team/team_list_screen.dart b/khelo/lib/ui/flow/team/team_list_screen.dart index 792c17e8..09bd4e56 100644 --- a/khelo/lib/ui/flow/team/team_list_screen.dart +++ b/khelo/lib/ui/flow/team/team_list_screen.dart @@ -13,9 +13,35 @@ import 'package:style/extensions/context_extensions.dart'; import 'package:style/indicator/progress_indicator.dart'; import 'package:style/text/app_text_style.dart'; -class TeamListScreen extends ConsumerWidget { +class TeamListScreen extends ConsumerStatefulWidget { const TeamListScreen({super.key}); + @override + ConsumerState createState() => _TeamListScreenState(); +} + +class _TeamListScreenState extends ConsumerState + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { + late TeamListViewNotifier notifier; + + @override + bool get wantKeepAlive => true; + + @override + void initState() { + WidgetsBinding.instance.addObserver(this); + super.initState(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.detached) { + // deallocate resources + notifier.dispose(); + WidgetsBinding.instance.removeObserver(this); + } + } + void _observeShowFilterOptionSheet( BuildContext context, WidgetRef ref, @@ -28,8 +54,9 @@ class TeamListScreen extends ConsumerWidget { } @override - Widget build(BuildContext context, WidgetRef ref) { - final notifier = ref.watch(teamListViewStateProvider.notifier); + Widget build(BuildContext context) { + super.build(context); + notifier = ref.watch(teamListViewStateProvider.notifier); final state = ref.watch(teamListViewStateProvider); _observeShowFilterOptionSheet(context, ref); @@ -135,7 +162,6 @@ class TeamListScreen extends ConsumerWidget { } else if (value == context.l10n.team_list_edit_team_title) { await AppRoute.addTeam(team: team).push(context); } - notifier.loadTeamList(); }, ) ] @@ -182,7 +208,6 @@ class TeamListScreen extends ConsumerWidget { backgroundColor: context.colorScheme.primary, onTap: () async { await AppRoute.addTeam().push(context); - notifier.loadTeamList(); }, icon: Icon( Icons.add_rounded, diff --git a/khelo/lib/ui/flow/team/team_list_view_model.dart b/khelo/lib/ui/flow/team/team_list_view_model.dart index 86896ff9..950f0780 100644 --- a/khelo/lib/ui/flow/team/team_list_view_model.dart +++ b/khelo/lib/ui/flow/team/team_list_view_model.dart @@ -10,7 +10,7 @@ import 'package:khelo/domain/extensions/context_extensions.dart'; part 'team_list_view_model.freezed.dart'; final teamListViewStateProvider = - StateNotifierProvider.autoDispose( + StateNotifierProvider( (ref) { final notifier = TeamListViewNotifier( ref.read(teamServiceProvider), @@ -27,7 +27,9 @@ class TeamListViewNotifier extends StateNotifier { late StreamSubscription _teamsStreamSubscription; TeamListViewNotifier(this._teamService, String? userId) - : super(TeamListViewState(currentUserId: userId)); + : super(TeamListViewState(currentUserId: userId)) { + loadTeamList(); + } void setUserId(String? userId) { state = state.copyWith(currentUserId: userId);