Skip to content

Commit

Permalink
Discard Event class and simplify query mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedLSayed9 committed Oct 28, 2023
1 parent d554064 commit b5abd82
Show file tree
Hide file tree
Showing 97 changed files with 917 additions and 1,441 deletions.
17 changes: 11 additions & 6 deletions lib/auth/domain/sign_in_with_email.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ mixin _$SignInWithEmail {
String get password => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$SignInWithEmailCopyWith<SignInWithEmail> get copyWith => throw _privateConstructorUsedError;
$SignInWithEmailCopyWith<SignInWithEmail> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $SignInWithEmailCopyWith<$Res> {
factory $SignInWithEmailCopyWith(SignInWithEmail value, $Res Function(SignInWithEmail) then) =
factory $SignInWithEmailCopyWith(
SignInWithEmail value, $Res Function(SignInWithEmail) then) =
_$SignInWithEmailCopyWithImpl<$Res, SignInWithEmail>;
@useResult
$Res call({String email, String password});
Expand Down Expand Up @@ -63,7 +65,8 @@ class _$SignInWithEmailCopyWithImpl<$Res, $Val extends SignInWithEmail>
}

/// @nodoc
abstract class _$$_SignInWithEmailCopyWith<$Res> implements $SignInWithEmailCopyWith<$Res> {
abstract class _$$_SignInWithEmailCopyWith<$Res>
implements $SignInWithEmailCopyWith<$Res> {
factory _$$_SignInWithEmailCopyWith(
_$_SignInWithEmail value, $Res Function(_$_SignInWithEmail) then) =
__$$_SignInWithEmailCopyWithImpl<$Res>;
Expand Down Expand Up @@ -122,7 +125,8 @@ class _$_SignInWithEmail implements _SignInWithEmail {
(other.runtimeType == runtimeType &&
other is _$_SignInWithEmail &&
(identical(other.email, email) || other.email == email) &&
(identical(other.password, password) || other.password == password));
(identical(other.password, password) ||
other.password == password));
}

@override
Expand All @@ -136,8 +140,9 @@ class _$_SignInWithEmail implements _SignInWithEmail {
}

abstract class _SignInWithEmail implements SignInWithEmail {
const factory _SignInWithEmail({required final String email, required final String password}) =
_$_SignInWithEmail;
const factory _SignInWithEmail(
{required final String email,
required final String password}) = _$_SignInWithEmail;

@override // Note: You should consider using separate value object (with its validator method)
// for these values if they're used in other entities.
Expand Down
18 changes: 12 additions & 6 deletions lib/auth/domain/user.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ mixin _$User {

/// @nodoc
abstract class $UserCopyWith<$Res> {
factory $UserCopyWith(User value, $Res Function(User) then) = _$UserCopyWithImpl<$Res, User>;
factory $UserCopyWith(User value, $Res Function(User) then) =
_$UserCopyWithImpl<$Res, User>;
@useResult
$Res call({String id, String email, String? name, String? phone, String? image});
$Res call(
{String id, String email, String? name, String? phone, String? image});
}

/// @nodoc
class _$UserCopyWithImpl<$Res, $Val extends User> implements $UserCopyWith<$Res> {
class _$UserCopyWithImpl<$Res, $Val extends User>
implements $UserCopyWith<$Res> {
_$UserCopyWithImpl(this._value, this._then);

// ignore: unused_field
Expand Down Expand Up @@ -82,13 +85,15 @@ abstract class _$$_UserCopyWith<$Res> implements $UserCopyWith<$Res> {
__$$_UserCopyWithImpl<$Res>;
@override
@useResult
$Res call({String id, String email, String? name, String? phone, String? image});
$Res call(
{String id, String email, String? name, String? phone, String? image});
}

/// @nodoc
class __$$_UserCopyWithImpl<$Res> extends _$UserCopyWithImpl<$Res, _$_User>
implements _$$_UserCopyWith<$Res> {
__$$_UserCopyWithImpl(_$_User _value, $Res Function(_$_User) _then) : super(_value, _then);
__$$_UserCopyWithImpl(_$_User _value, $Res Function(_$_User) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
Expand Down Expand Up @@ -169,7 +174,8 @@ class _$_User extends _User {
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$_UserCopyWith<_$_User> get copyWith => __$$_UserCopyWithImpl<_$_User>(this, _$identity);
_$$_UserCopyWith<_$_User> get copyWith =>
__$$_UserCopyWithImpl<_$_User>(this, _$identity);
}

abstract class _User extends User {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions lib/auth/infrastructure/dtos/user_dto.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ abstract class $UserDtoCopyWith<$Res> {
factory $UserDtoCopyWith(UserDto value, $Res Function(UserDto) then) =
_$UserDtoCopyWithImpl<$Res, UserDto>;
@useResult
$Res call({String id, String email, String? name, String? phone, String? image});
$Res call(
{String id, String email, String? name, String? phone, String? image});
}

/// @nodoc
class _$UserDtoCopyWithImpl<$Res, $Val extends UserDto> implements $UserDtoCopyWith<$Res> {
class _$UserDtoCopyWithImpl<$Res, $Val extends UserDto>
implements $UserDtoCopyWith<$Res> {
_$UserDtoCopyWithImpl(this._value, this._then);

// ignore: unused_field
Expand Down Expand Up @@ -84,15 +86,18 @@ class _$UserDtoCopyWithImpl<$Res, $Val extends UserDto> implements $UserDtoCopyW

/// @nodoc
abstract class _$$_UserDtoCopyWith<$Res> implements $UserDtoCopyWith<$Res> {
factory _$$_UserDtoCopyWith(_$_UserDto value, $Res Function(_$_UserDto) then) =
factory _$$_UserDtoCopyWith(
_$_UserDto value, $Res Function(_$_UserDto) then) =
__$$_UserDtoCopyWithImpl<$Res>;
@override
@useResult
$Res call({String id, String email, String? name, String? phone, String? image});
$Res call(
{String id, String email, String? name, String? phone, String? image});
}

/// @nodoc
class __$$_UserDtoCopyWithImpl<$Res> extends _$UserDtoCopyWithImpl<$Res, _$_UserDto>
class __$$_UserDtoCopyWithImpl<$Res>
extends _$UserDtoCopyWithImpl<$Res, _$_UserDto>
implements _$$_UserDtoCopyWith<$Res> {
__$$_UserDtoCopyWithImpl(_$_UserDto _value, $Res Function(_$_UserDto) _then)
: super(_value, _then);
Expand Down Expand Up @@ -142,7 +147,8 @@ class _$_UserDto extends _UserDto {
required this.image})
: super._();

factory _$_UserDto.fromJson(Map<String, dynamic> json) => _$$_UserDtoFromJson(json);
factory _$_UserDto.fromJson(Map<String, dynamic> json) =>
_$$_UserDtoFromJson(json);

@override
final String id;
Expand Down Expand Up @@ -213,5 +219,6 @@ abstract class _UserDto extends UserDto {
String? get image;
@override
@JsonKey(ignore: true)
_$$_UserDtoCopyWith<_$_UserDto> get copyWith => throw _privateConstructorUsedError;
_$$_UserDtoCopyWith<_$_UserDto> get copyWith =>
throw _privateConstructorUsedError;
}
3 changes: 2 additions & 1 deletion lib/auth/infrastructure/dtos/user_dto.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/auth/infrastructure/repos/auth_repo.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions lib/auth/presentation/components/login_form_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:flutter/material.dart';

import '../../../core/presentation/helpers/localization_helper.dart';
import '../../../core/presentation/styles/styles.dart';
import '../../../core/presentation/utils/event.dart';
import '../../../core/presentation/utils/fp_framework.dart';
import '../../../core/presentation/utils/riverpod_framework.dart';
import '../../../core/presentation/widgets/custom_elevated_button.dart';
import '../../../core/presentation/widgets/platform_widgets/platform_icons.dart';
Expand All @@ -25,7 +23,7 @@ class LoginFormComponent extends HookConsumerWidget {
email: emailController.text,
password: passwordController.text,
);
ref.read(signInWithEmailEventProvider.notifier).update((_) => Some(Event.unique(params)));
ref.read(signInStateProvider.notifier).signIn(params);
}
}

Expand Down
8 changes: 5 additions & 3 deletions lib/auth/presentation/providers/auth_state_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions lib/auth/presentation/providers/check_auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import '../../../auth/domain/user.dart';
import '../../../auth/infrastructure/repos/auth_repo.dart';
import '../../../auth/presentation/providers/auth_state_provider.dart';
import '../../../auth/presentation/providers/sign_out_provider.dart';
import '../../../core/presentation/utils/event.dart';
import '../../../core/presentation/utils/fp_framework.dart';
import '../../../core/presentation/utils/riverpod_framework.dart';

part 'check_auth_provider.g.dart';
Expand All @@ -16,7 +14,7 @@ Future<User> checkAuth(CheckAuthRef ref) async {
ref.listenSelf((previous, next) {
next.whenOrNull(
data: (user) => sub.read().authenticateUser(user),
error: (err, st) => ref.read(signOutProvider(const Event(arg: unit))),
error: (err, st) => ref.read(signOutStateProvider.notifier).signOut(),
);
});

Expand Down
5 changes: 3 additions & 2 deletions lib/auth/presentation/providers/check_auth_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 14 additions & 35 deletions lib/auth/presentation/providers/sign_in_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '../../../core/presentation/providers/provider_utils.dart';
import '../../../core/infrastructure/services/fcm_service/fcm_provider.dart';
import '../../../core/presentation/utils/fp_framework.dart';
import '../../../core/presentation/utils/riverpod_framework.dart';
Expand All @@ -12,41 +11,21 @@ part 'sign_in_provider.g.dart';
//Using [Option] to indicate idle(none)/success(some) states.
//This is a shorthand. You can use custom states using [freezed] instead.
@riverpod
FutureOr<Option<User>> signInState(SignInStateRef ref) {
final sub = ref.listen(authStateProvider.notifier, (prev, next) {});
ref.listenSelf((previous, next) {
next.whenData(
(user) {
if (user is Some<User>) {
sub.read().authenticateUser(user.value);
}
},
);
});
class SignInState extends _$SignInState {
@override
FutureOr<Option<User>> build() => const None();

final event = ref.watch(signInWithEmailEventProvider);
return event.match(
() => const None(),
(event) {
return ref.watch(signInProvider(event).future).then(Some.new);
},
);
}
Future<void> signIn(SignInWithEmail params) async {
state = const AsyncLoading();
state = await AsyncValue.guard(() async {
final authRepo = ref.read(authRepoProvider);
final userFromCredential = await authRepo.signInWithEmail(params);
final user = await authRepo.getUserData(userFromCredential.id);
await ref.read(fcmProvider).subscribeToTopic('general');

@riverpod
class SignInWithEmailEvent extends _$SignInWithEmailEvent with NotifierUpdate {
@override
Option<Event<SignInWithEmail>> build() => const None();
}
ref.read(authStateProvider.notifier).authenticateUser(user);

@riverpod
Future<User> signIn(
SignInRef ref,
Event<SignInWithEmail> event,
) async {
final authRepo = ref.watch(authRepoProvider);
final userFromCredential = await authRepo.signInWithEmail(event.arg);
final user = await authRepo.getUserData(userFromCredential.id);
await ref.watch(fcmProvider).subscribeToTopic('general');
return user;
return Some(user);
});
}
}
Loading

0 comments on commit b5abd82

Please sign in to comment.