Skip to content

Commit

Permalink
Add organization demo data source
Browse files Browse the repository at this point in the history
  • Loading branch information
MDikkii committed Jan 2, 2025
1 parent 2884044 commit 8388659
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 14 deletions.
9 changes: 7 additions & 2 deletions recipients_app/lib/core/cubits/auth/auth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ class AuthCubit extends Cubit<AuthState> {
if (user != null) {
try {
final recipient = await userRepository.fetchRecipient(user);
Organization? organization;

if (recipient?.organizationRef != null) {
organization = await organizationRepository.fetchOrganization(recipient!.organizationRef!);
}

emit(
AuthState(
status: AuthStatus.authenticated,
firebaseUser: user,
recipient: recipient,
organization: organization,
),
);
} on Exception catch (ex, stackTrace) {
Expand Down Expand Up @@ -59,8 +65,7 @@ class AuthCubit extends Cubit<AuthState> {
Organization? organization;

if (recipient?.organizationRef != null) {
organization = await organizationRepository
.fetchOrganization(recipient!.organizationRef!);
organization = await organizationRepository.fetchOrganization(recipient!.organizationRef!);
}

emit(
Expand Down
4 changes: 3 additions & 1 deletion recipients_app/lib/core/cubits/auth/auth_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ class AuthState extends Equatable {
});

@override
List<Object?> get props => [status, firebaseUser, recipient, exception];
List<Object?> get props => [status, firebaseUser, recipient, exception, organization];

AuthState copyWith({
AuthStatus? status,
User? firebaseUser,
Recipient? recipient,
Exception? exception,
Organization? organization,
}) {
return AuthState(
status: status ?? this.status,
firebaseUser: firebaseUser ?? this.firebaseUser,
recipient: recipient ?? this.recipient,
exception: exception ?? this.exception,
organization: organization ?? this.organization,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import "package:cloud_firestore/cloud_firestore.dart";

// ignore: subtype_of_sealed_class
class NoOpDocumentReference implements DocumentReference<Map<String, dynamic>> {
const NoOpDocumentReference();

@override
CollectionReference<Map<String, dynamic>> collection(String collectionPath) {
// TODO: implement collection
throw UnimplementedError();
}

@override
Future<void> delete() {
// TODO: implement delete
throw UnimplementedError();
}

@override
// TODO: implement firestore
FirebaseFirestore get firestore => throw UnimplementedError();

@override
Future<DocumentSnapshot<Map<String, dynamic>>> get([GetOptions? options]) {
// TODO: implement get
throw UnimplementedError();
}

@override
// TODO: implement id
String get id => throw UnimplementedError();

@override
// TODO: implement parent
CollectionReference<Map<String, dynamic>> get parent => throw UnimplementedError();

@override
// TODO: implement path
String get path => throw UnimplementedError();

@override
Future<void> set(Map<String, dynamic> data, [SetOptions? options]) {
// TODO: implement set
throw UnimplementedError();
}

@override
Stream<DocumentSnapshot<Map<String, dynamic>>> snapshots(
{bool includeMetadataChanges = false, ListenSource source = ListenSource.defaultSource,}) {
// TODO: implement snapshots
throw UnimplementedError();
}

@override
Future<void> update(Map<Object, Object?> data) {
// TODO: implement update
throw UnimplementedError();
}

@override
DocumentReference<R> withConverter<R>(
{required FromFirestore<R> fromFirestore, required ToFirestore<R> toFirestore,}) {
// TODO: implement withConverter
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "package:app/data/datasource/organization_data_source.dart";
import "package:app/data/models/models.dart";
import "package:cloud_firestore/cloud_firestore.dart";

class OrganizationDemoDataSource implements OrganizationDataSource {
final Organization _organization = _generateOrganization();

static Organization _generateOrganization() {
return const Organization(name: "Demo organization", contactName: "Demo manager", contactNumber: "+232 123456789");
}

@override
Future<Organization?> fetchOrganization(DocumentReference<Object?> organizationRef) async {
return _organization;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "dart:async";

import "package:app/data/datasource/demo/demo_user.dart";
import "package:app/data/datasource/demo/no_op_document_reference.dart";
import "package:app/data/datasource/user_data_source.dart";
import "package:app/data/models/models.dart";
import "package:firebase_auth/firebase_auth.dart";
Expand All @@ -12,6 +13,7 @@ class UserDemoDataSource implements UserDataSource {
lastName: "SocialIncome",
mobileMoneyPhone: Phone(23271118897),
communicationMobilePhone: Phone(23271118897),
organizationRef: NoOpDocumentReference(),
);
final _userStreamController = StreamController<User?>();
late final _userBroadcastStreamController = _getBroadcastStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "package:app/data/models/organization.dart";
import "package:cloud_firestore/cloud_firestore.dart";

abstract class OrganizationDataSource {
Future<Organization?> fetchOrganization(
DocumentReference organizationRef,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "package:app/data/datasource/organization_data_source.dart";
import "package:app/data/models/organization.dart";
import "package:cloud_firestore/cloud_firestore.dart";

const String surveyCollection = "surveys";

class OrganizationRemoteDataSource implements OrganizationDataSource {
final FirebaseFirestore firestore;

const OrganizationRemoteDataSource({
required this.firestore,
});

@override
Future<Organization?> fetchOrganization(
DocumentReference organizationRef,
) async {
final organization = organizationRef.withConverter(
fromFirestore: (snapshot, _) {
final data = snapshot.data()!;
return Organization.fromJson(data);
},
toFirestore: (organization, _) => organization.toJson(),
);

return (await organization.get()).data();
}
}
23 changes: 13 additions & 10 deletions recipients_app/lib/data/repositories/organization_repository.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import "package:app/data/datasource/demo/organization_demo_data_source.dart";
import "package:app/data/datasource/organization_data_source.dart";
import "package:app/data/datasource/remote/organization_remote_data_source.dart";
import "package:app/data/models/organization.dart";
import "package:app/demo_manager.dart";
import "package:cloud_firestore/cloud_firestore.dart";

class OrganizationRepository {
late OrganizationDataSource remoteDataSource = OrganizationRemoteDataSource(firestore: firestore);
late OrganizationDataSource demoDataSource = OrganizationDemoDataSource();

final DemoManager demoManager;
final FirebaseFirestore firestore;

const OrganizationRepository({
OrganizationRepository({
required this.firestore,
required this.demoManager,
});

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

Future<Organization?> fetchOrganization(
DocumentReference organizationRef,
) async {
final organization = organizationRef.withConverter(
fromFirestore: (snapshot, _) {
final data = snapshot.data()!;
return Organization.fromJson(data);
},
toFirestore: (organization, _) => organization.toJson(),
);

return (await organization.get()).data();
return _activeDataSource.fetchOrganization(organizationRef);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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
late SurveyDataSource demoDataSource = SurveyDemoDataSource();

final DemoManager demoManager;
final FirebaseFirestore firestore;
Expand Down
1 change: 1 addition & 0 deletions recipients_app/lib/my_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MyApp extends StatelessWidget {
RepositoryProvider(
create: (context) => OrganizationRepository(
firestore: firestore,
demoManager: demoManager,
),
),
],
Expand Down

0 comments on commit 8388659

Please sign in to comment.