Skip to content

Commit

Permalink
TF-2269 Show the report once a day
Browse files Browse the repository at this point in the history
Signed-off-by: dab246 <[email protected]>

Signed-off-by: dab246 <[email protected]>

Signed-off-by: dab246 <[email protected]>
  • Loading branch information
dab246 committed Nov 7, 2023
1 parent 55cd12b commit 74bed6e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import 'package:core/utils/app_logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/model/spam_report_state.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/utils/mailbox_dashboard_constant.dart';

class LocalSpamReportManager {
static const String _keyLastTimeDismissedSpamReported = 'KEY_LAST_TIME_DISMISSED_SPAM_REPORTED';
static const String _keySpamReportState = 'KEY_SPAM_REPORT_STATE';

final SharedPreferences _sharedPreferences;

LocalSpamReportManager(this._sharedPreferences);

Future<DateTime> getLastTimeDismissedSpamReported() async {
final timeStamp = _sharedPreferences.getInt(MailboxDashboardConstant.keyLastTimeDismissedSpamReported) ?? 0;
final timeStamp = _sharedPreferences.getInt(_keyLastTimeDismissedSpamReported) ?? 0;
log('LocalSpamReportManager::getLastTimeDismissedSpamReported:timeStamp: $timeStamp');
final lastTimeDismissedSpamReported = DateTime.fromMillisecondsSinceEpoch(timeStamp);
return lastTimeDismissedSpamReported;
}

Future<bool> storeLastTimeDismissedSpamReported(DateTime lastTimeDismissedSpamReported) async {
final timeStamp = lastTimeDismissedSpamReported.millisecondsSinceEpoch;
return await _sharedPreferences.setInt(MailboxDashboardConstant.keyLastTimeDismissedSpamReported,timeStamp);
log('LocalSpamReportManager::storeLastTimeDismissedSpamReported:timeStamp: $timeStamp');
return await _sharedPreferences.setInt(_keyLastTimeDismissedSpamReported, timeStamp);
}

Future<bool> deleteLastTimeDismissedSpamReported() async {
return await _sharedPreferences.remove(MailboxDashboardConstant.keyLastTimeDismissedSpamReported);
return await _sharedPreferences.remove(_keyLastTimeDismissedSpamReported);
}

Future<bool> deleteSpamReportState() async {
return await _sharedPreferences.remove(MailboxDashboardConstant.keySpamReportState);
return await _sharedPreferences.remove(_keySpamReportState);
}

Future<SpamReportState> getSpamReportState() async {
final spamReportState = _sharedPreferences.getString(MailboxDashboardConstant.keySpamReportState) ?? '';
final spamReportState = _sharedPreferences.getString(_keySpamReportState) ?? '';
return spamReportState == SpamReportState.disabled.keyValue ? SpamReportState.disabled : SpamReportState.enabled;
}

Future<bool> storeSpamReportState(SpamReportState spamReportState) async {
final spamReportState0 = spamReportState.keyValue;
return await _sharedPreferences.setString(MailboxDashboardConstant.keySpamReportState, spamReportState0);
return await _sharedPreferences.setString(_keySpamReportState, spamReportState0);
}

Future<void> clear() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/repository/spam_report_repository.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/get_number_of_unread_spam_emails_state.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/get_spam_mailbox_cached_state.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/utils/mailbox_dashboard_constant.dart';

class GetSpamMailboxCachedInteractor {
static const int spamReportBannerDisplayIntervalInHour = 12;

final SpamReportRepository _spamReportRepository;

GetSpamMailboxCachedInteractor(this._spamReportRepository);

Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName) async* {
try {
yield Right<Failure, Success>(GetSpamMailboxCachedLoading());

final lastTimeDismissedSpamReported = await _spamReportRepository.getLastTimeDismissedSpamReported();
final timeLast = DateTime.now().difference(lastTimeDismissedSpamReported);
final checkTimeCondition = timeLast.inHours > MailboxDashboardConstant.spamReportBannerDisplayTimeOut;
log('GetSpamMailboxCachedInteractor::execute:lastTimeDismissedSpamReported: $lastTimeDismissedSpamReported | timeLast: $timeLast | checkTimeCondition: $checkTimeCondition');
if (checkTimeCondition) {
if (await _validateIntervalToShowBanner()) {
final spamMailbox = await _spamReportRepository.getSpamMailboxCached(accountId, userName);
final countUnreadSpamMailbox = spamMailbox.unreadEmails?.value.value.toInt() ?? 0;
if (countUnreadSpamMailbox > 0) {
Expand All @@ -38,4 +34,11 @@ class GetSpamMailboxCachedInteractor {
yield Left<Failure, Success>(GetSpamMailboxCachedFailure(e));
}
}

Future<bool> _validateIntervalToShowBanner() async {
final lastTimeDismissedSpamReported = await _spamReportRepository.getLastTimeDismissedSpamReported();
final currentTime = DateTime.now().difference(lastTimeDismissedSpamReported);
log('GetSpamMailboxCachedInteractor::_compareSpamReportTime:lastTimeDismissedSpamReported: $lastTimeDismissedSpamReported | currentTime: $currentTime');
return currentTime.inHours > spamReportBannerDisplayIntervalInHour;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

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/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox_filter_condition.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/repository/spam_report_repository.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/get_number_of_unread_spam_emails_state.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/usecases/get_spam_mailbox_cached_interactor.dart';

class GetUnreadSpamMailboxInteractor {
static const int conditionsForDisplayingSpamReportBanner = 4;
final SpamReportRepository _spamReportRepository;

GetUnreadSpamMailboxInteractor(this._spamReportRepository);
Expand All @@ -25,12 +26,7 @@ class GetUnreadSpamMailboxInteractor {
) async* {
try {
yield Right(GetUnreadSpamMailboxLoading());
final lastTimeDismissedSpamReported = await _spamReportRepository.getLastTimeDismissedSpamReported();
final timeLast = DateTime.now().difference(lastTimeDismissedSpamReported);

final checkTimeCondition = (timeLast.inHours > 0) && (timeLast.inHours > conditionsForDisplayingSpamReportBanner);

if (checkTimeCondition) {
if (await _validateIntervalToShowBanner()) {
final response = await _spamReportRepository.getUnreadSpamMailbox(
session,
accountId,
Expand All @@ -50,4 +46,11 @@ class GetUnreadSpamMailboxInteractor {
yield Left(GetUnreadSpamMailboxFailure(e));
}
}

Future<bool> _validateIntervalToShowBanner() async {
final lastTimeDismissedSpamReported = await _spamReportRepository.getLastTimeDismissedSpamReported();
final currentTime = DateTime.now().difference(lastTimeDismissedSpamReported);
log('GetUnreadSpamMailboxInteractor::_compareSpamReportTime:lastTimeDismissedSpamReported: $lastTimeDismissedSpamReported | currentTime: $currentTime');
return currentTime.inHours > GetSpamMailboxCachedInteractor.spamReportBannerDisplayIntervalInHour;
}
}

This file was deleted.

0 comments on commit 74bed6e

Please sign in to comment.