TF-2064 Handle limit storage to show warning banner

(cherry picked from commit 3e36cebd5cbe880f9832c62463e71979e15e1741)
This commit is contained in:
dab246
2023-08-04 21:43:33 +07:00
committed by Dat Vu
parent 3aa2b91a89
commit a2875ea921
17 changed files with 78 additions and 293 deletions
@@ -1,91 +0,0 @@
import 'package:core/core.dart';
import 'package:flutter/widgets.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
enum QuotasState {
notAvailable,
normal,
runningOutOfStorage,
runOutOfStorage;
Color getColorProgress() {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
return AppColor.primaryColor;
case QuotasState.runningOutOfStorage:
return AppColor.colorProgressQuotasWarning;
case QuotasState.runOutOfStorage:
return AppColor.colorOutOfStorageQuotasWarning;
}
}
Color getColorQuotasFooterText() {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
case QuotasState.runningOutOfStorage:
return AppColor.loginTextFieldHintColor;
case QuotasState.runOutOfStorage:
return AppColor.colorOutOfStorageQuotasWarning;
}
}
String getQuotasFooterText(BuildContext context, num usedCapacity, num softLimitCapacity) {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
case QuotasState.runningOutOfStorage:
return AppLocalizations.of(context).textQuotasUsed(
usedCapacity.toDouble(),
softLimitCapacity.toDouble(),
);
case QuotasState.runOutOfStorage:
return AppLocalizations.of(context).textQuotasOutOfStorage;
}
}
String getIconWarningBanner(ImagePaths imagePaths) {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
case QuotasState.runningOutOfStorage:
return imagePaths.icQuotasWarning;
case QuotasState.runOutOfStorage:
return imagePaths.icQuotasOutOfStorage;
}
}
Color getBackgroundColorWarningBanner() {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
case QuotasState.runningOutOfStorage:
return AppColor.colorBackgroundQuotasWarning.withOpacity(0.12);
case QuotasState.runOutOfStorage:
return AppColor.colorOutOfStorageQuotasWarning.withOpacity(0.12);
}
}
String getTitleWarningBanner(BuildContext context, num progress) {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
case QuotasState.runningOutOfStorage:
return AppLocalizations.of(context).textQuotasRunningOutOfStorageTitle(progress.toDouble() * 100);
case QuotasState.runOutOfStorage:
return AppLocalizations.of(context).textQuotasRunOutOfStorageTitle;
}
}
String getContentWarningBanner(BuildContext context) {
switch(this) {
case QuotasState.notAvailable:
case QuotasState.normal:
case QuotasState.runningOutOfStorage:
return AppLocalizations.of(context).textQuotasRunningOutOfStorageContent;
case QuotasState.runOutOfStorage:
return AppLocalizations.of(context).textQuotasRunOutOfStorageContent;
}
}
}
@@ -1,15 +1,15 @@
import 'package:core/core.dart';
import 'package:core/utils/double_convert.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:get/get.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/quotas/data_types.dart';
import 'package:jmap_dart_client/jmap/quotas/quota.dart';
import 'package:tmail_ui_user/features/base/base_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/quotas/domain/extensions/list_quotas_extensions.dart';
import 'package:tmail_ui_user/features/quotas/domain/state/get_quotas_state.dart';
import 'package:tmail_ui_user/features/quotas/domain/use_case/get_quotas_interactor.dart';
import 'package:tmail_ui_user/features/quotas/presentation/model/quotas_state.dart';
import 'package:tmail_ui_user/main/error/capability_validator.dart';
class QuotasController extends BaseController {
@@ -20,61 +20,28 @@ class QuotasController extends BaseController {
final GetQuotasInteractor _getQuotasInteractor;
final usedCapacity = Rx<num>(0);
final limitCapacity = Rx<num>(0);
final warningLimitCapacity = Rx<num>(0);
final quotasState = QuotasState.notAvailable.obs;
final warningProgressConstant = 0.9;
late Worker accountIdWorker;
final octetsQuota = Rxn<Quota>();
double get progressUsedCapacity => limitCapacity.value != 0
? (usedCapacity.value / limitCapacity.value)
: 0;
bool get enableShowWarningQuotas =>
quotasState.value != QuotasState.notAvailable &&
(quotasState.value == QuotasState.runningOutOfStorage
|| quotasState.value == QuotasState.runOutOfStorage);
late Worker accountIdListener;
QuotasController(this._getQuotasInteractor);
void _getQuotasAction(AccountId accountId, Session session) {
try {
requireCapability(session, accountId, [CapabilityIdentifier.jmapQuota]);
consumeState(_getQuotasInteractor.execute(mailboxDashBoardController.accountId.value!));
} catch (e) {
logError('QuotasController::_getQuotasAction():$e');
}
void _getQuotasAction(AccountId accountId) {
consumeState(_getQuotasInteractor.execute(accountId));
}
void _handleGetQuotasSuccess(GetQuotasSuccess success) {
try {
final quotas = success.quotas.firstWhere((e) => e.resourceType == ResourceType.octets);
if (quotas.used != null) {
usedCapacity.value = DoubleConvert.bytesToGigaBytes(quotas.used!.value);
}
if (quotas.limit != null) {
warningLimitCapacity.value = DoubleConvert.bytesToGigaBytes(quotas.limit!.value * warningProgressConstant);
limitCapacity.value = DoubleConvert.bytesToGigaBytes(quotas.limit!.value);
}
if(usedCapacity.value >= limitCapacity.value) {
quotasState.value = QuotasState.runOutOfStorage;
} else if (usedCapacity.value >= warningLimitCapacity.value) {
quotasState.value = QuotasState.runningOutOfStorage;
} else {
quotasState.value = QuotasState.normal;
}
} catch (e) {
quotasState.value = QuotasState.notAvailable;
logError('QuotasController::_handleGetQuotasSuccess():[NotFoundException]: $e');
}
octetsQuota.value = success.quotas.octetsQuota;
}
void _initWorker() {
accountIdWorker = ever(mailboxDashBoardController.accountId, (accountId) {
if (accountId is AccountId && mailboxDashBoardController.sessionCurrent!= null) {
_getQuotasAction(accountId, mailboxDashBoardController.sessionCurrent!);
accountIdListener = ever(mailboxDashBoardController.accountId, (accountId) {
final session = mailboxDashBoardController.sessionCurrent;
if (accountId is AccountId &&
session != null &&
CapabilityIdentifier.jmapQuota.isSupported(session, accountId)
) {
_getQuotasAction(accountId);
}
});
}
@@ -87,7 +54,7 @@ class QuotasController extends BaseController {
@override
void onClose() {
accountIdWorker.call();
accountIdListener.dispose();
super.onClose();
}
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/quotas/presentation/model/quotas_state.dart';
import 'package:tmail_ui_user/features/quotas/domain/extensions/quota_extensions.dart';
import 'package:tmail_ui_user/features/quotas/presentation/quotas_controller.dart';
import 'package:tmail_ui_user/features/quotas/presentation/styles/quotas_view_styles.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
@@ -13,7 +13,8 @@ class QuotasView extends GetWidget<QuotasController> {
@override
Widget build(BuildContext context) {
return Obx(() {
if (controller.quotasState.value != QuotasState.notAvailable) {
if (controller.octetsQuota.value != null && controller.octetsQuota.value!.storageAvailable) {
final octetQuota = controller.octetsQuota.value!;
return LayoutBuilder(builder: (context, constraints) {
return Container(
padding: const EdgeInsetsDirectional.only(
@@ -63,23 +64,19 @@ class QuotasView extends GetWidget<QuotasController> {
SizedBox(
width: _getProgressBarMaxWith(constraints.maxWidth),
child: LinearProgressIndicator(
color: controller.quotasState.value.getColorProgress(),
color: octetQuota.getQuotasStateProgressBarColor(),
minHeight: QuotasViewStyles.progressBarHeight,
backgroundColor: QuotasViewStyles.progressBarBackgroundColor,
value: controller.progressUsedCapacity,
value: octetQuota.usedStoragePercent,
),
),
const SizedBox(height: QuotasViewStyles.space),
Text(
controller.quotasState.value.getQuotasFooterText(
context,
controller.usedCapacity.value,
controller.limitCapacity.value,
),
octetQuota.getQuotasStateTitle(context),
style: TextStyle(
fontSize: QuotasViewStyles.progressStateTextSize,
fontWeight: QuotasViewStyles.progressStateFontWeight,
color: controller.quotasState.value.getColorQuotasFooterText(),
color: octetQuota.getQuotasStateTitleColor()
),
)
],
@@ -1,59 +0,0 @@
import 'package:core/core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/quotas/presentation/quotas_controller.dart';
class QuotasWarningBannerWidget extends GetWidget<QuotasController> {
const QuotasWarningBannerWidget({this.margin ,Key? key}) : super(key: key);
final EdgeInsets? margin;
@override
Widget build(BuildContext context) {
return Obx(
() => controller.enableShowWarningQuotas
? Container(
padding: const EdgeInsets.all(16),
margin: margin ?? const EdgeInsets.only(left: 12, right: 12, bottom: 12),
decoration: BoxDecoration(
color: controller.quotasState.value.getBackgroundColorWarningBanner(),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Row(
children: [
SvgPicture.asset(controller.quotasState.value.getIconWarningBanner(controller.imagePaths)),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
controller.quotasState.value.getTitleWarningBanner(
context,
controller.progressUsedCapacity,
),
style: const TextStyle(
fontSize: 17,
fontWeight: FontWeight.w700,
color: AppColor.colorTitleQuotasWarning,
),
),
const SizedBox(height: 8),
Text(
controller.quotasState.value.getContentWarningBanner(context),
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w400,
color: AppColor.loginTextFieldHintColor,
),
),
],
),
),
],
),
)
: const SizedBox.shrink(),
);
}
}