Skip to content

Commit

Permalink
Fix missing notification when execute receive new email & `mark as …
Browse files Browse the repository at this point in the history
…read` at same time in terminated app
  • Loading branch information
dab246 authored and hoangdat committed May 13, 2024
1 parent 43732a6 commit fde0955
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 81 deletions.
6 changes: 4 additions & 2 deletions lib/features/home/domain/state/get_session_state.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';

class GetSessionLoading extends LoadingState {}

class GetSessionSuccess extends UIState {
final Session session;
final StateChange? stateChange;

GetSessionSuccess(this.session);
GetSessionSuccess(this.session, {this.stateChange});

@override
List<Object> get props => [session];
List<Object?> get props => [session, stateChange];
}

class GetSessionFailure extends FeatureFailure {
Expand Down
14 changes: 9 additions & 5 deletions lib/features/home/domain/usecases/get_session_interactor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:tmail_ui_user/features/home/domain/repository/session_repository.dart';
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';

Expand All @@ -11,26 +12,29 @@ class GetSessionInteractor {

GetSessionInteractor(this.sessionRepository);

Stream<Either<Failure, Success>> execute() async* {
Stream<Either<Failure, Success>> execute({StateChange? stateChange}) async* {
try {
yield Right<Failure, Success>(GetSessionLoading());
final session = await sessionRepository.getSession();
yield Right<Failure, Success>(GetSessionSuccess(session));
yield Right<Failure, Success>(GetSessionSuccess(session, stateChange: stateChange));
} catch (e) {
if (PlatformInfo.isMobile) {
yield* _getStoredSessionFromCache(remoteException: e);
yield* _getStoredSessionFromCache(remoteException: e, stateChange: stateChange);
} else {
yield Left<Failure, Success>(GetSessionFailure(e));
}
}
}

Stream<Either<Failure, Success>> _getStoredSessionFromCache({dynamic remoteException}) async* {
Stream<Either<Failure, Success>> _getStoredSessionFromCache({
dynamic remoteException,
StateChange? stateChange
}) async* {
try {
log('GetSessionInteractor::_getStoredSessionFromCache:remoteException: $remoteException');
yield Right<Failure, Success>(GetSessionLoading());
final session = await sessionRepository.getStoredSession();
yield Right<Failure, Success>(GetSessionSuccess(session));
yield Right<Failure, Success>(GetSessionSuccess(session, stateChange: stateChange));
} catch (e) {
yield Left<Failure, Success>(GetSessionFailure(remoteException ?? e));
}
Expand Down
18 changes: 16 additions & 2 deletions lib/features/login/domain/state/get_credential_state.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:model/account/password.dart';
import 'package:model/account/personal_account.dart';

class GetCredentialViewState extends UIState {
final Uri baseUrl;
final UserName userName;
final Password password;
final PersonalAccount personalAccount;
final StateChange? stateChange;

GetCredentialViewState(this.baseUrl, this.userName, this.password);
GetCredentialViewState(
this.baseUrl,
this.userName,
this.password,
this.personalAccount,
{this.stateChange});

@override
List<Object> get props => [baseUrl, userName, password];
List<Object?> get props => [
baseUrl,
userName,
password,
personalAccount,
stateChange];
}

class GetCredentialFailure extends FeatureFailure {
Expand Down
19 changes: 17 additions & 2 deletions lib/features/login/domain/state/get_stored_token_oidc_state.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:model/account/personal_account.dart';
import 'package:model/oidc/oidc_configuration.dart';
import 'package:model/oidc/token_oidc.dart';

class GetStoredTokenOidcSuccess extends UIState {
final Uri baseUrl;
final TokenOIDC tokenOidc;
final OIDCConfiguration oidcConfiguration;
final PersonalAccount personalAccount;
final StateChange? stateChange;

GetStoredTokenOidcSuccess(this.baseUrl, this.tokenOidc, this.oidcConfiguration);
GetStoredTokenOidcSuccess(
this.baseUrl,
this.tokenOidc,
this.oidcConfiguration,
this.personalAccount,
{this.stateChange}
);

@override
List<Object?> get props => [baseUrl, tokenOidc, oidcConfiguration];
List<Object?> get props => [
baseUrl,
tokenOidc,
oidcConfiguration,
personalAccount,
stateChange];
}

class GetStoredTokenOidcFailure extends FeatureFailure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:model/account/authentication_type.dart';
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
Expand All @@ -18,15 +19,19 @@ class GetAuthenticatedAccountInteractor {
this._getStoredTokenOidcInteractor
);

Stream<Either<Failure, Success>> execute() async* {
Stream<Either<Failure, Success>> execute({StateChange? stateChange}) async* {
try {
yield Right<Failure, Success>(LoadingState());
final account = await _accountRepository.getCurrentAccount();
yield Right(GetAuthenticatedAccountSuccess(account));
if (account.authenticationType == AuthenticationType.oidc) {
yield* _getStoredTokenOidcInteractor.execute(account.id);
yield* _getStoredTokenOidcInteractor.execute(
personalAccount: account,
stateChange: stateChange);
} else {
yield await _getCredentialInteractor.execute();
yield await _getCredentialInteractor.execute(
personalAccount: account,
stateChange: stateChange);
}
} catch (e) {
yield Left(GetAuthenticatedAccountFailure(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:model/account/password.dart';
import 'package:model/account/personal_account.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
import 'package:tmail_ui_user/features/login/domain/extensions/uri_extension.dart';
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
Expand All @@ -15,15 +17,20 @@ class GetCredentialInteractor {

GetCredentialInteractor(this.credentialRepository);

Future<Either<Failure, Success>> execute() async {
Future<Either<Failure, Success>> execute({
required PersonalAccount personalAccount,
StateChange? stateChange
}) async {
try {
final baseUrl = await credentialRepository.getBaseUrl();
final authenticationInfo = await credentialRepository.getAuthenticationInfoStored();
if (isCredentialValid(baseUrl)) {
return Right(GetCredentialViewState(
baseUrl,
UserName(authenticationInfo.username),
Password(authenticationInfo.password)));
Password(authenticationInfo.password),
personalAccount,
stateChange: stateChange));
} else {
return Left(GetCredentialFailure(BadCredentials()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:model/account/personal_account.dart';
import 'package:model/oidc/oidc_configuration.dart';
import 'package:model/oidc/token_oidc.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
Expand All @@ -16,13 +18,15 @@ class GetStoredTokenOidcInteractor {

GetStoredTokenOidcInteractor(this._authenticationOIDCRepository, this._credentialRepository);

Stream<Either<Failure, Success>> execute(String tokenIdHash) async* {
Stream<Either<Failure, Success>> execute({
required PersonalAccount personalAccount,
StateChange? stateChange
}) async* {
try {
log('GetStoredTokenOidcInteractor::execute(): tokenIdHash: $tokenIdHash');
yield Right<Failure, Success>(LoadingState());
final futureValue = await Future.wait([
_credentialRepository.getBaseUrl(),
_authenticationOIDCRepository.getStoredTokenOIDC(tokenIdHash),
_authenticationOIDCRepository.getStoredTokenOIDC(personalAccount.id),
_authenticationOIDCRepository.getStoredOidcConfiguration(),
], eagerError: true);

Expand All @@ -33,7 +37,12 @@ class GetStoredTokenOidcInteractor {
log('GetStoredTokenOidcInteractor::execute(): oidcConfiguration: $oidcConfiguration');

if (_isCredentialValid(baseUrl)) {
yield Right(GetStoredTokenOidcSuccess(baseUrl, tokenOidc, oidcConfiguration));
yield Right(GetStoredTokenOidcSuccess(
baseUrl,
tokenOidc,
oidcConfiguration,
personalAccount,
stateChange: stateChange));
} else {
yield Left(GetStoredTokenOidcFailure(InvalidBaseUrl()));
}
Expand Down
Loading

0 comments on commit fde0955

Please sign in to comment.