-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy patharticle_page.dart
264 lines (235 loc) · 8.12 KB
/
article_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import 'package:app_ui/app_ui.dart';
import 'package:article_repository/article_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_news_example/ads/ads.dart';
import 'package:flutter_news_example/app/app.dart';
import 'package:flutter_news_example/article/article.dart';
import 'package:flutter_news_example/l10n/l10n.dart';
import 'package:flutter_news_example/subscriptions/subscriptions.dart';
import 'package:go_router/go_router.dart';
import 'package:news_blocks_ui/news_blocks_ui.dart';
import 'package:share_launcher/share_launcher.dart';
/// The supported behaviors for interstitial ad.
enum InterstitialAdBehavior {
/// Displays the ad when opening the article.
onOpen,
/// Displays the ad when closing the article.
onClose,
}
class ArticlePage extends StatelessWidget {
const ArticlePage({
required this.id,
required this.isVideoArticle,
required this.interstitialAdBehavior,
super.key,
});
static const routeName = 'article';
static const routePath = 'article/:id';
static Widget routeBuilder(
BuildContext context,
GoRouterState state,
) {
final id = state.pathParameters['id'];
final isVideoArticle = bool.tryParse(
state.uri.queryParameters['isVideoArticle'] ?? 'false',
) ??
false;
final interstitialAdBehavior =
state.uri.queryParameters['interstitialAdBehavior'] != null
? InterstitialAdBehavior.values.firstWhere(
(e) =>
e.toString() ==
'InterstitialAdBehavior.'
// ignore: lines_longer_than_80_chars
'${state.uri.queryParameters['interstitialAdBehavior']}',
)
: null;
if (id == null) {
throw Exception('Missing required "id" parameter');
}
return ArticlePage(
id: id,
isVideoArticle: isVideoArticle,
interstitialAdBehavior:
interstitialAdBehavior ?? InterstitialAdBehavior.onOpen,
);
}
/// The id of the requested article.
final String id;
/// Whether the requested article is a video article.
final bool isVideoArticle;
/// Indicates when the interstitial ad will be displayed.
/// Default to [InterstitialAdBehavior.onOpen]
final InterstitialAdBehavior interstitialAdBehavior;
@override
Widget build(BuildContext context) {
return BlocProvider<ArticleBloc>(
create: (_) => ArticleBloc(
articleId: id,
shareLauncher: const ShareLauncher(),
articleRepository: context.read<ArticleRepository>(),
)..add(const ArticleRequested()),
child: ArticleView(
isVideoArticle: isVideoArticle,
interstitialAdBehavior: interstitialAdBehavior,
),
);
}
}
class ArticleView extends StatelessWidget {
const ArticleView({
required this.isVideoArticle,
required this.interstitialAdBehavior,
super.key,
});
final bool isVideoArticle;
final InterstitialAdBehavior interstitialAdBehavior;
@override
Widget build(BuildContext context) {
final backgroundColor =
isVideoArticle ? AppColors.darkBackground : AppColors.white;
final foregroundColor =
isVideoArticle ? AppColors.white : AppColors.highEmphasisSurface;
final uri = context.select((ArticleBloc bloc) => bloc.state.uri);
final isSubscriber =
context.select<AppBloc, bool>((bloc) => bloc.state.isUserSubscribed);
return PopScope(
onPopInvokedWithResult: (_, __) => _onPop(context),
child: HasToShowInterstitialAdListener(
interstitialAdBehavior: interstitialAdBehavior,
child: HasReachedArticleLimitListener(
child: HasWatchedRewardedAdListener(
child: Scaffold(
backgroundColor: backgroundColor,
appBar: AppBar(
systemOverlayStyle: SystemUiOverlayStyle(
statusBarIconBrightness:
isVideoArticle ? Brightness.light : Brightness.dark,
statusBarBrightness:
isVideoArticle ? Brightness.dark : Brightness.light,
),
leading: isVideoArticle
? AppBackButton.light(
onPressed: Navigator.of(context).pop,
)
: AppBackButton(
onPressed: Navigator.of(context).pop,
),
actions: [
if (uri != null && uri.toString().isNotEmpty)
Padding(
key: const Key('articlePage_shareButton'),
padding: const EdgeInsets.only(right: AppSpacing.lg),
child: ShareButton(
shareText: context.l10n.shareText,
color: foregroundColor,
onPressed: () => context
.read<ArticleBloc>()
.add(ShareRequested(uri: uri)),
),
),
if (!isSubscriber) const ArticleSubscribeButton(),
],
),
body: ArticleThemeOverride(
isVideoArticle: isVideoArticle,
child: const ArticleContent(),
),
),
),
),
),
);
}
void _onPop(BuildContext context) {
final state = context.read<ArticleBloc>().state;
if (state.showInterstitialAd &&
interstitialAdBehavior == InterstitialAdBehavior.onClose) {
context
.read<FullScreenAdsBloc>()
.add(const ShowInterstitialAdRequested());
}
}
}
@visibleForTesting
class ArticleSubscribeButton extends StatelessWidget {
const ArticleSubscribeButton({super.key});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: AppSpacing.lg),
child: Align(
alignment: Alignment.centerRight,
child: AppButton.smallRedWine(
onPressed: () => showPurchaseSubscriptionDialog(context: context),
child: Text(context.l10n.subscribeButtonText),
),
),
);
}
}
@visibleForTesting
class HasReachedArticleLimitListener extends StatelessWidget {
const HasReachedArticleLimitListener({super.key, this.child});
final Widget? child;
@override
Widget build(BuildContext context) {
return BlocListener<ArticleBloc, ArticleState>(
listener: (context, state) {
if (!state.hasReachedArticleViewsLimit) {
context.read<ArticleBloc>().add(const ArticleRequested());
}
},
listenWhen: (previous, current) =>
previous.hasReachedArticleViewsLimit !=
current.hasReachedArticleViewsLimit,
child: child,
);
}
}
@visibleForTesting
class HasWatchedRewardedAdListener extends StatelessWidget {
const HasWatchedRewardedAdListener({super.key, this.child});
final Widget? child;
@override
Widget build(BuildContext context) {
return BlocListener<FullScreenAdsBloc, FullScreenAdsState>(
listener: (context, state) {
if (state.earnedReward != null) {
context.read<ArticleBloc>().add(const ArticleRewardedAdWatched());
}
},
listenWhen: (previous, current) =>
previous.earnedReward != current.earnedReward,
child: child,
);
}
}
@visibleForTesting
class HasToShowInterstitialAdListener extends StatelessWidget {
const HasToShowInterstitialAdListener({
required this.child,
required this.interstitialAdBehavior,
super.key,
});
final Widget child;
final InterstitialAdBehavior interstitialAdBehavior;
@override
Widget build(BuildContext context) {
return BlocListener<ArticleBloc, ArticleState>(
listener: (context, state) {
if (state.showInterstitialAd &&
interstitialAdBehavior == InterstitialAdBehavior.onOpen) {
context
.read<FullScreenAdsBloc>()
.add(const ShowInterstitialAdRequested());
}
},
listenWhen: (previous, current) =>
previous.showInterstitialAd != current.showInterstitialAd,
child: child,
);
}
}