Skip to content

Commit

Permalink
Add demo manager and demo button on start screen
Browse files Browse the repository at this point in the history
  • Loading branch information
MDikkii committed Dec 1, 2024
1 parent 041f992 commit 1686c96
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ class UserDemoDataSource implements UserDataSource {
communicationMobilePhone: Phone(23271118897),
);
final _userStreamController = StreamController<User?>();
late final _userBroadcastStreamController = _getBroadcastStream();
final _user = DemoUser();

@override
Stream<User?> authStateChanges() {
_userStreamController.add(_user);
return _userStreamController.stream;
return _userBroadcastStreamController;
}

Stream<User?> _getBroadcastStream() {
return _userStreamController.stream.asBroadcastStream();
}

@override
Expand All @@ -46,7 +51,6 @@ class UserDemoDataSource implements UserDataSource {
@override
Future<void> signOut() async {
_userStreamController.add(null);
_userStreamController.done;
}

@override
Expand Down
7 changes: 4 additions & 3 deletions recipients_app/lib/data/repositories/payment_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import "package:app/data/datasource/demo/payment_demo_data_source.dart";
import "package:app/data/datasource/payment_data_source.dart";
import "package:app/data/datasource/remote/payment_remote_data_source.dart";
import "package:app/data/models/models.dart";
import "package:app/demo_manager.dart";
import "package:cloud_firestore/cloud_firestore.dart";

class PaymentRepository {
late PaymentDataSource remoteDataSource = PaymentRemoteDataSource(firestore: firestore);
late PaymentDataSource demoDataSource = PaymentDemoDataSource();

final bool useRemoteDataSource;
final DemoManager demoManager;
final FirebaseFirestore firestore;

PaymentRepository({
required this.firestore,
this.useRemoteDataSource = false,
required this.demoManager,
});

PaymentDataSource get _activeDataSource => useRemoteDataSource ? remoteDataSource : demoDataSource;
PaymentDataSource get _activeDataSource => demoManager.isDemoEnabled ? demoDataSource : remoteDataSource;

Future<List<SocialIncomePayment>> fetchPayments({
required String recipientId,
Expand Down
7 changes: 4 additions & 3 deletions recipients_app/lib/data/repositories/survey_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import "package:app/data/datasource/remote/survey_remote_data_source.dart";
import "package:app/data/datasource/survey_data_source.dart";
import "package:app/data/models/models.dart";
import "package:app/data/models/survey/survey.dart";
import "package:app/demo_manager.dart";
import "package:cloud_firestore/cloud_firestore.dart";

class SurveyRepository {
late SurveyDataSource remoteDataSource = SurveyRemoteDataSource(firestore: firestore);
late SurveyDataSource demoDataSource = SurveyDemoDataSource(); // Assuming you have a demo source

final bool useRemoteDataSource;
final DemoManager demoManager;
final FirebaseFirestore firestore;

SurveyRepository({
required this.firestore,
this.useRemoteDataSource = false,
required this.demoManager,
});

SurveyDataSource get _activeDataSource => useRemoteDataSource ? remoteDataSource : demoDataSource;
SurveyDataSource get _activeDataSource => demoManager.isDemoEnabled ? demoDataSource : remoteDataSource;

Future<List<Survey>> fetchSurveys({
required String recipientId,
Expand Down
39 changes: 34 additions & 5 deletions recipients_app/lib/data/repositories/user_repository.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
import "dart:async";

import "package:app/data/datasource/demo/user_demo_data_source.dart";
import "package:app/data/datasource/remote/user_remote_data_source.dart";
import "package:app/data/datasource/user_data_source.dart";
import "package:app/data/models/models.dart";
import "package:app/demo_manager.dart";
import "package:cloud_firestore/cloud_firestore.dart";
import "package:firebase_auth/firebase_auth.dart";

class UserRepository {
late UserDataSource remoteDataSource = UserRemoteDataSource(firestore: firestore, firebaseAuth: firebaseAuth);
late UserDataSource demoDataSource = UserDemoDataSource();

final bool useRemoteDataSource;
final DemoManager demoManager;
final FirebaseFirestore firestore;
final FirebaseAuth firebaseAuth;

UserRepository({
required this.firestore,
required this.firebaseAuth,
this.useRemoteDataSource = false,
required this.demoManager,
});

UserDataSource get _activeDataSource => useRemoteDataSource ? remoteDataSource : demoDataSource;
UserDataSource get _activeDataSource => demoManager.isDemoEnabled ? demoDataSource : remoteDataSource;

Stream<User?> authStateChanges() {
final StreamController<User?> authStateController = StreamController();
StreamSubscription<User?>? authStateSubscription;

authStateSubscription = _activeDataSource.authStateChanges().listen((authState) {
authStateController.add(authState);
});

demoManager.isDemoEnabledStream.listen((isDemoMode) {
authStateSubscription?.cancel();

authStateSubscription = _activeDataSource.authStateChanges().listen((authState) {
authStateController.add(authState);
});

authStateController.onCancel = () {
authStateSubscription?.cancel();
};
});

return authStateController.stream;
}

Stream<User?> authStateChanges() => _activeDataSource.authStateChanges();
User? get currentUser => _activeDataSource.currentUser;

Future<Recipient?> fetchRecipient(User firebaseUser) => _activeDataSource.fetchRecipient(firebaseUser);
Expand All @@ -41,7 +66,11 @@ class UserRepository {
forceResendingToken: forceResendingToken,
);

Future<void> signOut() => _activeDataSource.signOut();
Future<void> signOut() {
return _activeDataSource.signOut().whenComplete(() {
demoManager.isDemoEnabled = false;
});
}

Future<void> signInWithCredential(PhoneAuthCredential credentials) =>
_activeDataSource.signInWithCredential(credentials);
Expand Down
26 changes: 26 additions & 0 deletions recipients_app/lib/demo_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "dart:async";

class DemoManager {
factory DemoManager() {
return _instance;
}

DemoManager._privateConstructor() {
_isDemoEnabled = false;
_controller.add(false);
}

static final DemoManager _instance = DemoManager._privateConstructor();

bool _isDemoEnabled = false;

final _controller = StreamController<bool>.broadcast();
Stream<bool> get isDemoEnabledStream => _controller.stream;

set isDemoEnabled(bool value) {
_isDemoEnabled = value;
_controller.add(_isDemoEnabled);
}

bool get isDemoEnabled => _isDemoEnabled;
}
3 changes: 2 additions & 1 deletion recipients_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,6 @@
"missingRecaptchaVersion": "Missing reCAPTCHA version",
"invalidRecaptchaVersion": "Invalid reCAPTCHA version",
"invalidReqType": "Invalid request type",
"errorEmailInvalid": "Invalid email address"
"errorEmailInvalid": "Invalid email address",
"demoCta": "Demo"
}
3 changes: 2 additions & 1 deletion recipients_app/lib/l10n/app_kri.arb
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@
"invalidVerificationCodeError": "Di spɛshal kod we wi sɛn yu nɔ kɔrɛkt. Duya chɛk di SMS kod ɛn tray bak.",
"userDisabledError": "Wi dɔn lɔk yu akawnt. Rich awt to wi if yu want fɔ no mɔ.",
"invalidCredentialError": " Di fon nɔmba ɔ di spɛshal kod we wi sɛn to yu in tɛm dɔn pas fɔ yuz. Duya tray bak.",
"errorEmailInvalid": "Imel nɔ kɔrɛkt"
"errorEmailInvalid": "Imel nɔ kɔrɛkt",
"demoCta": "Demo"
}
3 changes: 3 additions & 0 deletions recipients_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "package:app/core/helpers/custom_bloc_observer.dart";
import "package:app/demo_manager.dart";
import "package:app/my_app.dart";
import "package:cloud_firestore/cloud_firestore.dart";
import "package:firebase_app_check/firebase_app_check.dart";
Expand Down Expand Up @@ -31,6 +32,7 @@ Future<void> main() async {
final firestore = FirebaseFirestore.instance;
final firebaseAuth = FirebaseAuth.instance;
final messaging = FirebaseMessaging.instance;
final demoManager = DemoManager();

if (appFlavor == "dev") {
firestore.useFirestoreEmulator("localhost", 8080);
Expand All @@ -52,6 +54,7 @@ Future<void> main() async {
firebaseAuth: firebaseAuth,
firestore: firestore,
messaging: messaging,
demoManager: demoManager,
),
),
);
Expand Down
6 changes: 6 additions & 0 deletions recipients_app/lib/my_app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "package:app/core/cubits/auth/auth_cubit.dart";
import "package:app/core/cubits/settings/settings_cubit.dart";
import "package:app/data/repositories/repositories.dart";
import "package:app/demo_manager.dart";
import "package:app/kri_intl.dart";
import "package:app/ui/configs/configs.dart";
import "package:app/view/pages/main_app_page.dart";
Expand All @@ -19,12 +20,14 @@ class MyApp extends StatelessWidget {
final FirebaseAuth firebaseAuth;
final FirebaseFirestore firestore;
final FirebaseMessaging messaging;
final DemoManager demoManager;

const MyApp({
super.key,
required this.firebaseAuth,
required this.firestore,
required this.messaging,
required this.demoManager,
});

// This widget is the root of your application.
Expand All @@ -41,6 +44,7 @@ class MyApp extends StatelessWidget {
create: (context) => UserRepository(
firebaseAuth: firebaseAuth,
firestore: firestore,
demoManager: demoManager,
),
),
RepositoryProvider(
Expand All @@ -49,11 +53,13 @@ class MyApp extends StatelessWidget {
RepositoryProvider(
create: (context) => PaymentRepository(
firestore: firestore,
demoManager: demoManager,
),
),
RepositoryProvider(
create: (context) => SurveyRepository(
firestore: firestore,
demoManager: demoManager,
),
),
RepositoryProvider(
Expand Down
21 changes: 17 additions & 4 deletions recipients_app/lib/view/widgets/welcome/phone_input_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "package:app/core/cubits/signup/signup_cubit.dart";
import "package:app/demo_manager.dart";
import "package:app/ui/buttons/buttons.dart";
import "package:app/ui/configs/app_colors.dart";
import "package:flutter/material.dart";
Expand All @@ -18,13 +19,15 @@ class _PhoneInputPageState extends State<PhoneInputPage> {
late final RoundedLoadingButtonController btnController;
late final TextEditingController phoneNumberController;
late PhoneNumber number;
late DemoManager demoManager;

@override
void initState() {
super.initState();
btnController = RoundedLoadingButtonController();
phoneNumberController = TextEditingController();
number = PhoneNumber(isoCode: "SL");
demoManager = DemoManager();
}

@override
Expand All @@ -45,6 +48,18 @@ class _PhoneInputPageState extends State<PhoneInputPage> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.topRight,
child: SafeArea(
child: ButtonSmall(
onPressed: () {
demoManager.isDemoEnabled = true;
},
label: localizations.demoCta,
buttonType: ButtonSmallType.outlined,
),
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down Expand Up @@ -97,10 +112,8 @@ class _PhoneInputPageState extends State<PhoneInputPage> {
),
inputDecoration: InputDecoration(
labelText: localizations.phoneNumber,
labelStyle: Theme.of(context)
.textTheme
.headlineMedium!
.copyWith(color: AppColors.primaryColor),
labelStyle:
Theme.of(context).textTheme.headlineMedium!.copyWith(color: AppColors.primaryColor),
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(color: AppColors.primaryColor),
borderRadius: BorderRadius.all(Radius.circular(10)),
Expand Down

0 comments on commit 1686c96

Please sign in to comment.