Skip to content

Commit

Permalink
resource de-allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed May 6, 2024
1 parent db910a7 commit 75c2c94
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 103 deletions.
36 changes: 31 additions & 5 deletions khelo/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<HomeScreen>
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,
Expand Down Expand Up @@ -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]);
},
);
}
Expand Down
16 changes: 14 additions & 2 deletions khelo/lib/ui/flow/home/home_view_model.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,12 +8,13 @@ import 'package:freezed_annotation/freezed_annotation.dart';
part 'home_view_model.freezed.dart';

final homeViewStateProvider =
StateNotifierProvider.autoDispose<HomeViewNotifier, HomeViewState>(
StateNotifierProvider<HomeViewNotifier, HomeViewState>(
(ref) => HomeViewNotifier(ref.read(matchServiceProvider)),
);

class HomeViewNotifier extends StateNotifier<HomeViewState> {
final MatchService _matchService;
late StreamSubscription _streamSubscription;

HomeViewNotifier(this._matchService) : super(const HomeViewState()) {
loadMatches();
Expand All @@ -21,7 +23,7 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
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);
},
Expand All @@ -31,6 +33,16 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
},
);
}

_cancelStreamSubscription() async {
await _streamSubscription.cancel();
}

@override
void dispose() {
_cancelStreamSubscription();
super.dispose();
}
}

@freezed
Expand Down
23 changes: 22 additions & 1 deletion khelo/lib/ui/flow/main/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class MainScreen extends ConsumerStatefulWidget {
ConsumerState<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends ConsumerState<MainScreen> {
class _MainScreenState extends ConsumerState<MainScreen>
with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {
static final List<Widget> _widgets = <Widget>[
const HomeScreen(),
const MyGameTabScreen(),
Expand All @@ -29,8 +30,28 @@ class _MainScreenState extends ConsumerState<MainScreen> {
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);
}
Expand Down
111 changes: 74 additions & 37 deletions khelo/lib/ui/flow/matches/match_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MatchListScreen>
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: [
Expand Down Expand Up @@ -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(
Expand All @@ -73,7 +102,8 @@ class MatchListScreen extends ConsumerWidget {
error: state.error,
onRetryTap: () {
notifier.cancelStreamSubscription();
notifier.loadMatches();},
notifier.loadMatches();
},
);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -207,17 +243,17 @@ 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,
team: match.teams.last,
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,
),
],
),
Expand All @@ -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,
Expand All @@ -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()));
}

Expand Down
14 changes: 7 additions & 7 deletions khelo/lib/ui/flow/matches/match_list_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MatchListViewNotifier, MatchListViewState>((ref) {
final notifier = MatchListViewNotifier(
ref.read(matchServiceProvider),
ref.read(currentUserPod)?.id,
Expand All @@ -27,7 +27,9 @@ class MatchListViewNotifier extends StateNotifier<MatchListViewState> {
MatchListViewNotifier(this._matchService, String? userId)
: super(MatchListViewState(
currentUserId: userId,
));
)) {
loadMatches();
}

void setUserId(String? userId) {
state = state.copyWith(currentUserId: userId);
Expand All @@ -41,13 +43,11 @@ class MatchListViewNotifier extends StateNotifier<MatchListViewState> {
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");
}
}

Expand Down
Loading

0 comments on commit 75c2c94

Please sign in to comment.