-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
145 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
recipients_app/lib/data/datasource/demo/no_op_document_reference.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
recipients_app/lib/data/datasource/demo/organization_demo_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
recipients_app/lib/data/datasource/organization_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
28 changes: 28 additions & 0 deletions
28
recipients_app/lib/data/datasource/remote/organization_remote_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
recipients_app/lib/data/repositories/organization_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters