TF-145 Implement LoadMoreEmails in ThreadView
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
|
||||
enum LoadMoreState {
|
||||
IDLE,
|
||||
LOADING,
|
||||
COMPLETED
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/repository/thread_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/repository/thread_repository.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/get_emails_in_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/load_more_emails_in_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/mark_as_multiple_email_read_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/move_multiple_email_to_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/mark_as_star_multiple_email_interactor.dart';
|
||||
@@ -56,6 +57,7 @@ class ThreadBindings extends Bindings {
|
||||
Get.lazyPut(() => MarkAsStarEmailInteractor(Get.find<EmailRepository>()));
|
||||
Get.lazyPut(() => MarkAsStarMultipleEmailInteractor(Get.find<EmailRepository>()));
|
||||
Get.lazyPut(() => RefreshChangesEmailsInMailboxInteractor(Get.find<ThreadRepository>()));
|
||||
Get.lazyPut(() => LoadMoreEmailsInMailboxInteractor(Get.find<ThreadRepository>()));
|
||||
Get.put(ThreadController(
|
||||
Get.find<ResponsiveUtils>(),
|
||||
Get.find<GetEmailsInMailboxInteractor>(),
|
||||
@@ -66,6 +68,7 @@ class ThreadBindings extends Bindings {
|
||||
Get.find<MarkAsStarEmailInteractor>(),
|
||||
Get.find<MarkAsStarMultipleEmailInteractor>(),
|
||||
Get.find<RefreshChangesEmailsInMailboxInteractor>(),
|
||||
Get.find<LoadMoreEmailsInMailboxInteractor>(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -22,15 +22,16 @@ import 'package:tmail_ui_user/features/email/presentation/model/composer_argumen
|
||||
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/mailbox_dashboard_controller.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/constants/thread_constants.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/get_all_email_state.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/load_more_emails_state.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/mark_as_multiple_email_read_state.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/move_multiple_email_to_mailbox_state.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/mark_as_star_multiple_email_state.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/get_emails_in_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/load_more_emails_in_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/mark_as_multiple_email_read_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/move_multiple_email_to_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/mark_as_star_multiple_email_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/usecases/refresh_changes_emails_in_mailbox_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/thread/presentation/model/load_more_state.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
import 'package:tmail_ui_user/main/routes/app_routes.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
@@ -48,13 +49,12 @@ class ThreadController extends BaseController {
|
||||
final MarkAsStarEmailInteractor _markAsStarEmailInteractor;
|
||||
final MarkAsStarMultipleEmailInteractor _markAsStarMultipleEmailInteractor;
|
||||
final RefreshChangesEmailsInMailboxInteractor _refreshChangesEmailsInMailboxInteractor;
|
||||
final LoadMoreEmailsInMailboxInteractor _loadMoreEmailsInMailboxInteractor;
|
||||
|
||||
final emailList = <PresentationEmail>[].obs;
|
||||
final loadMoreState = LoadMoreState.IDLE.obs;
|
||||
final currentSelectMode = SelectMode.INACTIVE.obs;
|
||||
|
||||
int _currentPosition = 0;
|
||||
int _totalNumberOfEmails = 0;
|
||||
bool canLoadMore = true;
|
||||
MailboxId? _currentMailboxId;
|
||||
jmap.State? _currentEmailState;
|
||||
|
||||
@@ -77,6 +77,7 @@ class ThreadController extends BaseController {
|
||||
this._markAsStarEmailInteractor,
|
||||
this._markAsStarMultipleEmailInteractor,
|
||||
this._refreshChangesEmailsInMailboxInteractor,
|
||||
this._loadMoreEmailsInMailboxInteractor,
|
||||
);
|
||||
|
||||
@override
|
||||
@@ -122,20 +123,27 @@ class ThreadController extends BaseController {
|
||||
@override
|
||||
void onData(Either<Failure, Success> newState) {
|
||||
super.onData(newState);
|
||||
newState.map((success) {
|
||||
if (success is GetAllEmailSuccess) {
|
||||
_getAllEmailSuccess(success);
|
||||
newState.fold(
|
||||
(failure) {
|
||||
if (failure is LoadMoreEmailsFailure) {
|
||||
canLoadMore = false;
|
||||
}
|
||||
},
|
||||
(success) {
|
||||
if (success is GetAllEmailSuccess) {
|
||||
_getAllEmailSuccess(success);
|
||||
} else if (success is LoadMoreEmailsSuccess) {
|
||||
_loadMoreEmailsSuccess(success);
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void onDone() {
|
||||
viewState.value.fold(
|
||||
(failure) {
|
||||
if (failure is GetAllEmailFailure) {
|
||||
_resetPositionCurrentAndLoadMoreState();
|
||||
} else if (failure is MarkAsMultipleEmailReadAllFailure
|
||||
if (failure is MarkAsMultipleEmailReadAllFailure
|
||||
|| failure is MarkAsMultipleEmailReadFailure) {
|
||||
_markAsSelectedEmailReadFailure(failure);
|
||||
} else if (failure is MarkAsStarMultipleEmailAllFailure
|
||||
@@ -165,49 +173,36 @@ class ThreadController extends BaseController {
|
||||
|
||||
void _getAllEmail() {
|
||||
if (_accountId != null) {
|
||||
_getAllEmailAction(_accountId!, inMailboxId: _currentMailboxId);
|
||||
_getAllEmailAction(_accountId!, mailboxId: _currentMailboxId);
|
||||
}
|
||||
}
|
||||
|
||||
void _resetToOriginalValue() {
|
||||
dispatchState(Right(LoadingState()));
|
||||
emailList.value = <PresentationEmail>[];
|
||||
loadMoreState.value = LoadMoreState.IDLE;
|
||||
_currentPosition = 0;
|
||||
canLoadMore = true;
|
||||
}
|
||||
|
||||
void _getAllEmailSuccess(Success success) {
|
||||
if (success is GetAllEmailSuccess) {
|
||||
_currentEmailState = success.currentEmailState;
|
||||
|
||||
if (loadMoreState.value == LoadMoreState.LOADING) {
|
||||
emailList.addAll(success.emailList);
|
||||
} else {
|
||||
emailList.value = success.emailList;
|
||||
}
|
||||
|
||||
_totalNumberOfEmails = emailList.length;
|
||||
loadMoreState.value = success.emailList.isEmpty ? LoadMoreState.COMPLETED : LoadMoreState.IDLE;
|
||||
}
|
||||
void _getAllEmailSuccess(GetAllEmailSuccess success) {
|
||||
_currentEmailState = success.currentEmailState;
|
||||
emailList.value = success.emailList;
|
||||
}
|
||||
|
||||
void _getAllEmailAction(AccountId accountId, {MailboxId? inMailboxId}) {
|
||||
void _getAllEmailAction(AccountId accountId, {MailboxId? mailboxId}) {
|
||||
consumeState(_getEmailsInMailboxInteractor.execute(
|
||||
accountId,
|
||||
limit: ThreadConstants.defaultLimit,
|
||||
position: _currentPosition,
|
||||
sort: _sortOrder,
|
||||
filter: _filterCondition,
|
||||
emailFilter: EmailFilter(
|
||||
filter: _filterCondition,
|
||||
mailboxId: mailboxId ?? _currentMailboxId),
|
||||
propertiesCreated: ThreadConstants.propertiesDefault,
|
||||
propertiesUpdated: ThreadConstants.propertiesUpdatedDefault,
|
||||
inMailboxId: inMailboxId ?? _currentMailboxId
|
||||
));
|
||||
}
|
||||
|
||||
void refreshAllEmail() {
|
||||
dispatchState(Right(LoadingState()));
|
||||
loadMoreState.value = LoadMoreState.IDLE;
|
||||
_currentPosition = 0;
|
||||
_getAllEmail();
|
||||
}
|
||||
|
||||
@@ -224,20 +219,28 @@ class ThreadController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
void loadMoreEmailAction() {
|
||||
loadMoreState.value = LoadMoreState.LOADING;
|
||||
_currentPosition += _totalNumberOfEmails;
|
||||
|
||||
if (_accountId != null) {
|
||||
_getAllEmailAction(_accountId!);
|
||||
void loadMoreEmails() {
|
||||
if (canLoadMore) {
|
||||
if (_accountId != null) {
|
||||
consumeState(_loadMoreEmailsInMailboxInteractor.execute(
|
||||
_accountId!,
|
||||
limit: ThreadConstants.defaultLimit,
|
||||
sort: _sortOrder,
|
||||
filter: EmailFilterCondition(
|
||||
inMailbox: mailboxDashBoardController.selectedMailbox.value?.id,
|
||||
before: emailList.last.receivedAt),
|
||||
properties: ThreadConstants.propertiesDefault,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _resetPositionCurrentAndLoadMoreState() {
|
||||
if (loadMoreState.value == LoadMoreState.LOADING) {
|
||||
_currentPosition -= _totalNumberOfEmails;
|
||||
void _loadMoreEmailsSuccess(LoadMoreEmailsSuccess success) {
|
||||
if (success.emailList.isNotEmpty) {
|
||||
emailList.addAll(success.emailList);
|
||||
} else {
|
||||
canLoadMore = false;
|
||||
}
|
||||
loadMoreState.value = LoadMoreState.IDLE;
|
||||
}
|
||||
|
||||
SelectMode getSelectMode(PresentationEmail presentationEmail, PresentationEmail? selectedEmail) {
|
||||
@@ -421,7 +424,6 @@ class ThreadController extends BaseController {
|
||||
final mailboxCurrent = mailboxDashBoardController.selectedMailbox.value;
|
||||
if (_accountId != null && mailboxCurrent != null) {
|
||||
final importantAction = presentationEmail.isFlaggedEmail() ? MarkStarAction.unMarkStar : MarkStarAction.markStar;
|
||||
dispatchState(Right(LoadingState()));
|
||||
consumeState(_markAsStarEmailInteractor.execute(_accountId!, presentationEmail.toEmail(), importantAction));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:model/email/presentation_email.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:tmail_ui_user/features/thread/presentation/model/load_more_state.dart';
|
||||
import 'package:tmail_ui_user/features/thread/presentation/thread_controller.dart';
|
||||
import 'package:tmail_ui_user/features/thread/presentation/widgets/app_bar_thread_select_mode_active_builder.dart';
|
||||
import 'package:tmail_ui_user/features/thread/presentation/widgets/app_bar_thread_widget_builder.dart';
|
||||
@@ -184,7 +183,7 @@ class ThreadView extends GetWidget<ThreadController> {
|
||||
Widget _buildLoadingView() {
|
||||
return Obx(() => controller.viewState.value.fold(
|
||||
(failure) => SizedBox.shrink(),
|
||||
(success) => success is LoadingState && controller.loadMoreState.value != LoadMoreState.LOADING
|
||||
(success) => success is LoadingState
|
||||
? Center(child: Padding(
|
||||
padding: EdgeInsets.only(top: 16, bottom: 16),
|
||||
child: SizedBox(
|
||||
@@ -195,14 +194,16 @@ class ThreadView extends GetWidget<ThreadController> {
|
||||
}
|
||||
|
||||
Widget _buildLoadingViewLoadMore() {
|
||||
return Obx(() => controller.loadMoreState.value == LoadMoreState.LOADING
|
||||
? Center(child: Padding(
|
||||
padding: EdgeInsets.only(top: 16, bottom: 16),
|
||||
child: SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(color: AppColor.primaryColor))))
|
||||
: SizedBox.shrink());
|
||||
return Obx(() => controller.viewState.value.fold(
|
||||
(failure) => SizedBox.shrink(),
|
||||
(success) => success is LoadingMoreState
|
||||
? Center(child: Padding(
|
||||
padding: EdgeInsets.only(top: 16, bottom: 16),
|
||||
child: SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(color: AppColor.primaryColor))))
|
||||
: SizedBox.shrink()));
|
||||
}
|
||||
|
||||
Widget _buildListEmail(BuildContext context) {
|
||||
@@ -232,9 +233,8 @@ class ThreadView extends GetWidget<ThreadController> {
|
||||
return NotificationListener<ScrollNotification>(
|
||||
onNotification: (ScrollNotification scrollInfo) {
|
||||
if (scrollInfo is ScrollEndNotification
|
||||
&& controller.loadMoreState.value == LoadMoreState.IDLE
|
||||
&& scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent) {
|
||||
controller.loadMoreEmailAction();
|
||||
controller.loadMoreEmails();
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user