-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
firestore connect; macos network fix
- Loading branch information
1 parent
b1c2c6d
commit 415c7ed
Showing
23 changed files
with
177 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:poster/core/domain/profile/data_source/profile_api.dart'; | ||
import 'package:poster/core/domain/profile/entity/profile.dart'; | ||
import 'package:poster/core/utils/functions/let.dart'; | ||
|
||
const _collectionProfile = 'profiles'; | ||
const _fieldEmail = 'email'; | ||
|
||
final class ProfileApiImpl with ProfileApi { | ||
@override | ||
Future<Profile> createProfile({ | ||
required String username, | ||
required String email, | ||
}) async { | ||
final profile = Profile(username: username, email: email); | ||
|
||
await FirebaseFirestore.instance | ||
.collection(_collectionProfile) | ||
.add(profile.toJson()); | ||
|
||
return profile; | ||
} | ||
|
||
@override | ||
Future<Profile?> getProfileByEmail({required String email}) => | ||
FirebaseFirestore.instance | ||
.collection(_collectionProfile) | ||
.where(_fieldEmail, isEqualTo: email) | ||
.get() | ||
.then((snapshot) => | ||
snapshot.docs.firstOrNull?.data().let(Profile.fromJson) | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:poster/core/data/profile/data_source/profile_storage.dart'; | ||
import 'package:poster/core/data/profile/data_source/profile_api_impl.dart'; | ||
import 'package:poster/core/data/profile/data_source/profile_store_impl.dart'; | ||
import 'package:poster/core/data/profile/repository/profile_repository_impl.dart'; | ||
import 'package:poster/core/domain/profile/data_source/profile_storage.dart'; | ||
import 'package:poster/core/di/provide_singleton.dart'; | ||
import 'package:poster/core/domain/profile/data_source/profile_api.dart'; | ||
import 'package:poster/core/domain/profile/data_source/profile_store.dart'; | ||
import 'package:poster/core/domain/profile/repository/profile_repository.dart'; | ||
|
||
extension ProfileModule on GetIt { | ||
void registerProfileModule() { | ||
registerLazySingleton<ProfileStorage>(() => ProfileStorageImpl()); | ||
registerLazySingleton<ProfileRepository>(() => ProfileRepositoryImpl(storage: this())); | ||
} | ||
List<Type> registerProfileModule() => [ | ||
provideSingleton<ProfileApi>(() => ProfileApiImpl()), | ||
provideSingleton<ProfileStore>(() => ProfileStoreImpl()), | ||
provideSingleton<ProfileRepository>(() => ProfileRepositoryImpl(api: this(), store: this())), | ||
]; | ||
} |
29 changes: 22 additions & 7 deletions
29
lib/core/data/profile/repository/profile_repository_impl.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,18 +1,33 @@ | ||
import 'package:poster/core/domain/profile/data_source/profile_storage.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:poster/core/domain/profile/data_source/profile_api.dart'; | ||
import 'package:poster/core/domain/profile/data_source/profile_store.dart'; | ||
import 'package:poster/core/domain/profile/entity/profile.dart'; | ||
import 'package:poster/core/domain/profile/repository/profile_repository.dart'; | ||
|
||
final class ProfileRepositoryImpl extends ProfileRepository { | ||
final ProfileStorage _storage; | ||
final ProfileApi _api; | ||
final ProfileStore _store; | ||
|
||
ProfileRepositoryImpl({ | ||
required ProfileStorage storage, | ||
}) : _storage = storage; | ||
required ProfileApi api, | ||
required ProfileStore store, | ||
}) : _api = api, _store = store; | ||
|
||
@override | ||
Future<Profile?> get profile => _storage.profile; | ||
Future<Profile?> get profile => _store.profile; | ||
|
||
@override | ||
Future<void> updateProfile(Profile profile) => | ||
_storage.updateProfile(profile); | ||
Future<void> createProfile({ | ||
required String username, | ||
required String email, | ||
}) => _api.createProfile(username: username, email: email) | ||
.then(_store.storeProfile); | ||
|
||
@override | ||
Future<Either<Exception, void>> saveProfile({required String email}) async { | ||
final profile = await _api.getProfileByEmail(email: email); | ||
if (profile == null) return Either.left(Exception('profile is not found')); | ||
_store.storeProfile(profile); | ||
return Either.right(null); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:poster/core/domain/profile/entity/profile.dart'; | ||
|
||
mixin ProfileApi { | ||
Future<Profile> createProfile({ | ||
required String username, | ||
required String email, | ||
}); | ||
|
||
Future<Profile?> getProfileByEmail({required String email}); | ||
} |
4 changes: 2 additions & 2 deletions
4
.../profile/data_source/profile_storage.dart → ...in/profile/data_source/profile_store.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,6 +1,6 @@ | ||
import 'package:poster/core/domain/profile/entity/profile.dart'; | ||
|
||
mixin ProfileStorage { | ||
mixin ProfileStore { | ||
Future<Profile?> get profile; | ||
Future<void> updateProfile(Profile profile); | ||
Future<void> storeProfile(Profile profile); | ||
} |
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,27 +1,29 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:poster/core/utils/functions/let.dart'; | ||
|
||
part 'profile.freezed.dart'; | ||
part 'profile.g.dart'; | ||
|
||
@freezed | ||
abstract class Profile with _$Profile { | ||
const factory Profile({ | ||
required String username, | ||
String? birthdate, | ||
int? followers, | ||
int? following, | ||
String? location, | ||
@JsonKey(name: 'username') required String username, | ||
@JsonKey(name: 'email') required String email, | ||
@JsonKey(name: 'avatar') String? avatar, | ||
@JsonKey(name: 'birthdate') int? birthdayTimestamp, | ||
@JsonKey(name: 'location') String? location, | ||
}) = _Profile; | ||
|
||
factory Profile.fromJson(Map<String, dynamic> json) => | ||
_$ProfileFromJson(json); | ||
} | ||
|
||
extension Properties on Profile { | ||
int? get age { | ||
if (birthdate == null) return null; | ||
final born = DateTime.parse(birthdate!); | ||
DateTime? get birthdate => | ||
birthdayTimestamp?.let(DateTime.fromMillisecondsSinceEpoch); | ||
|
||
int? get age => birthdate?.let((date) { | ||
final now = DateTime.now(); | ||
return now.difference(born).inDays ~/ 365; | ||
} | ||
return now.difference(date).inDays ~/ 365; | ||
}); | ||
} |
14 changes: 12 additions & 2 deletions
14
lib/core/domain/profile/repository/profile_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,3 +1,13 @@ | ||
import 'package:poster/core/domain/profile/data_source/profile_storage.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:poster/core/domain/profile/entity/profile.dart'; | ||
|
||
abstract class ProfileRepository with ProfileStorage {} | ||
abstract class ProfileRepository { | ||
Future<Profile?> get profile; | ||
|
||
Future<void> createProfile({ | ||
required String username, | ||
required String email, | ||
}); | ||
|
||
Future<Either<Exception, void>> saveProfile({required String email}); | ||
} |
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
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
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
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
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
Oops, something went wrong.