Skip to content

Commit 79bcdd5

Browse files
authored
Merge pull request #101 from flutter-news-app-full-source-code/fix-demo-env-login-with-email
Fix demo env login with email
2 parents 5df7288 + d839252 commit 79bcdd5

File tree

11 files changed

+86
-19
lines changed

11 files changed

+86
-19
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
2222
required DataRepository<User> userRepository,
2323
required local_config.AppEnvironment environment,
2424
this.demoDataMigrationService,
25+
this.initialUser,
2526
}) : _authenticationRepository = authenticationRepository,
2627
_userAppSettingsRepository = userAppSettingsRepository,
2728
_appConfigRepository = appConfigRepository,
@@ -54,6 +55,13 @@ class AppBloc extends Bloc<AppEvent, AppState> {
5455
selectedBottomNavigationIndex: 0,
5556
remoteConfig: null,
5657
environment: environment,
58+
// Initialize status and user based on initialUser
59+
status: initialUser != null
60+
? (initialUser.appRole == AppUserRole.standardUser
61+
? AppStatus.authenticated
62+
: AppStatus.anonymous)
63+
: AppStatus.unauthenticated,
64+
user: initialUser,
5765
),
5866
) {
5967
on<AppUserChanged>(_onAppUserChanged);
@@ -79,6 +87,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
7987
final DataRepository<User> _userRepository;
8088
final local_config.AppEnvironment _environment;
8189
final DemoDataMigrationService? demoDataMigrationService;
90+
final User? initialUser;
8291
late final StreamSubscription<User?> _userSubscription;
8392

8493
/// Handles user changes and loads initial settings once user is available.

lib/app/view/app.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class App extends StatelessWidget {
3333
required AppEnvironment environment,
3434
required AdService adService,
3535
this.demoDataMigrationService,
36+
this.initialUser,
3637
super.key,
3738
}) : _authenticationRepository = authenticationRepository,
3839
_headlinesRepository = headlinesRepository,
@@ -61,6 +62,7 @@ class App extends StatelessWidget {
6162
final AppEnvironment _environment;
6263
final AdService _adService;
6364
final DemoDataMigrationService? demoDataMigrationService;
65+
final User? initialUser;
6466

6567
@override
6668
Widget build(BuildContext context) {
@@ -89,6 +91,7 @@ class App extends StatelessWidget {
8991
userRepository: context.read<DataRepository<User>>(),
9092
environment: _environment,
9193
demoDataMigrationService: demoDataMigrationService,
94+
initialUser: initialUser,
9295
),
9396
),
9497
BlocProvider(

lib/authentication/view/request_code_page.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import 'dart:async';
55

66
import 'package:flutter/material.dart';
77
import 'package:flutter_bloc/flutter_bloc.dart';
8+
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
9+
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/config.dart';
810
import 'package:flutter_news_app_mobile_client_full_source_code/authentication/bloc/authentication_bloc.dart';
911
import 'package:flutter_news_app_mobile_client_full_source_code/l10n/l10n.dart';
1012
import 'package:flutter_news_app_mobile_client_full_source_code/router/routes.dart';
@@ -130,6 +132,33 @@ class _RequestCodeView extends StatelessWidget {
130132
),
131133
textAlign: TextAlign.center,
132134
),
135+
136+
// NOT NEEDED; any email is accepted in demo mode
137+
//
138+
//Display demo email suggestion if in demo environment
139+
// BlocSelector<AppBloc, AppState, AppEnvironment?>(
140+
// selector: (state) => state.environment,
141+
// builder: (context, environment) {
142+
// if (environment == AppEnvironment.demo) {
143+
// return Column(
144+
// children: [
145+
// const SizedBox(height: AppSpacing.md),
146+
// Text(
147+
// l10n.demoEmailSuggestionMessage(
148+
// 'admin@mail.com',
149+
// ),
150+
// style: textTheme.bodyMedium?.copyWith(
151+
// color: colorScheme.secondary,
152+
// fontWeight: FontWeight.bold,
153+
// ),
154+
// textAlign: TextAlign.center,
155+
// ),
156+
// ],
157+
// );
158+
// }
159+
// return const SizedBox.shrink();
160+
// },
161+
// ),
133162
const SizedBox(height: AppSpacing.xxl),
134163
_EmailLinkForm(isLoading: isLoading),
135164
],

lib/bootstrap.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ Future<Widget> bootstrap(
7373
);
7474
}
7575

76+
// Fetch the initial user from the authentication repository.
77+
// This ensures the AppBloc starts with an accurate authentication status.
78+
final initialUser = await authenticationRepository.getCurrentUser();
79+
7680
// Conditional data client instantiation based on environment
7781
DataClient<Headline> headlinesClient;
7882
DataClient<Topic> topicsClient;
@@ -289,5 +293,6 @@ Future<Widget> bootstrap(
289293
environment: environment,
290294
demoDataMigrationService: demoDataMigrationService,
291295
adService: adService,
296+
initialUser: initialUser,
292297
);
293298
}

lib/l10n/app_localizations.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,12 @@ abstract class AppLocalizations {
15501550
/// **'Demo Mode: Use code {code}'**
15511551
String demoVerificationCodeMessage(String code);
15521552

1553+
/// Message shown in demo mode to suggest an email for sign-in
1554+
///
1555+
/// In en, this message translates to:
1556+
/// **'Demo Mode: Use email {email}'**
1557+
String demoEmailSuggestionMessage(String email);
1558+
15531559
/// Label for Headline content type
15541560
///
15551561
/// In en, this message translates to:

lib/l10n/app_localizations_ar.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,11 @@ class AppLocalizationsAr extends AppLocalizations {
804804
return 'وضع العرض التوضيحي: استخدم الرمز $code';
805805
}
806806

807+
@override
808+
String demoEmailSuggestionMessage(String email) {
809+
return 'وضع العرض التوضيحي: استخدم البريد الإلكتروني $email';
810+
}
811+
807812
@override
808813
String get contentTypeHeadline => 'العناوين الرئيسية';
809814

lib/l10n/app_localizations_en.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,11 @@ class AppLocalizationsEn extends AppLocalizations {
807807
return 'Demo Mode: Use code $code';
808808
}
809809

810+
@override
811+
String demoEmailSuggestionMessage(String email) {
812+
return 'Demo Mode: Use email $email';
813+
}
814+
810815
@override
811816
String get contentTypeHeadline => 'Headlines';
812817

lib/l10n/arb/app_ar.arb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,16 @@
10461046
}
10471047
}
10481048
},
1049+
"demoEmailSuggestionMessage": "وضع العرض التوضيحي: استخدم البريد الإلكتروني {email}",
1050+
"@demoEmailSuggestionMessage": {
1051+
"description": "رسالة تظهر في وضع العرض التوضيحي لاقتراح بريد إلكتروني لتسجيل الدخول",
1052+
"placeholders": {
1053+
"email": {
1054+
"type": "String",
1055+
"example": "demo@example.com"
1056+
}
1057+
}
1058+
},
10491059
"contentTypeHeadline": "العناوين الرئيسية",
10501060
"@contentTypeHeadline": {
10511061
"description": "Label for Headline content type"

lib/l10n/arb/app_en.arb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@
602602
"@headlinesFeedFilterNoSourcesMatch": {
603603
"description": "Message shown when no sources match the selected filters"
604604
},
605-
606605
"searchModelTypeHeadline": "Headlines",
607606
"@searchModelTypeHeadline": {
608607
"description": "Dropdown display name for Headline search type"
@@ -639,7 +638,6 @@
639638
"@searchHintTextCountry": {
640639
"description": "Hint text for searching countries"
641640
},
642-
643641
"searchPageInitialHeadline": "Start Your Search",
644642
"@searchPageInitialHeadline": {
645643
"description": "Generic headline for the initial state of the search page"
@@ -1038,7 +1036,7 @@
10381036
"@followedCategoriesEmptySubheadline": {
10391037
"description": "Subheadline for empty state on followed categories page"
10401038
},
1041-
"demoVerificationCodeMessage": "Demo Mode: Use code {code}",
1039+
"demoVerificationCodeMessage": "Demo Mode: Use code {code}",
10421040
"@demoVerificationCodeMessage": {
10431041
"description": "Message shown in demo mode to provide the verification code",
10441042
"placeholders": {
@@ -1048,6 +1046,16 @@
10481046
}
10491047
}
10501048
},
1049+
"demoEmailSuggestionMessage": "Demo Mode: Use email {email}",
1050+
"@demoEmailSuggestionMessage": {
1051+
"description": "Message shown in demo mode to suggest an email for sign-in",
1052+
"placeholders": {
1053+
"email": {
1054+
"type": "String",
1055+
"example": "demo@example.com"
1056+
}
1057+
}
1058+
},
10511059
"contentTypeHeadline": "Headlines",
10521060
"@contentTypeHeadline": {
10531061
"description": "Label for Headline content type"
@@ -1117,4 +1125,4 @@
11171125
"@unfollowButtonText": {
11181126
"description": "Text for the unfollow button on suggestion items"
11191127
}
1120-
}
1128+
}

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:flutter_news_app_mobile_client_full_source_code/app/services/spl
66
import 'package:flutter_news_app_mobile_client_full_source_code/bootstrap.dart';
77

88
// Define the current application environment (production/development/demo).
9-
const appEnvironment = AppEnvironment.development;
9+
const appEnvironment = AppEnvironment.demo;
1010

1111
void main() async {
1212
final appConfig = switch (appEnvironment) {

0 commit comments

Comments
 (0)