2022-11-08 08:22:12 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-07-28 14:08:27 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-10 05:07:51 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2022-12-20 09:35:11 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2023-05-05 08:55:58 +00:00
|
|
|
import "package:photos/generated/l10n.dart";
|
2021-07-28 14:08:27 +00:00
|
|
|
import 'package:photos/models/user_details.dart';
|
2022-10-10 05:07:51 +00:00
|
|
|
import 'package:photos/states/user_details_state.dart';
|
2022-10-18 08:43:42 +00:00
|
|
|
import 'package:photos/theme/colors.dart';
|
|
|
|
import 'package:photos/theme/ente_theme.dart';
|
2023-08-02 11:24:21 +00:00
|
|
|
import "package:photos/ui/common/loading_widget.dart";
|
2022-05-30 04:37:54 +00:00
|
|
|
import 'package:photos/ui/payment/subscription.dart';
|
2022-10-19 14:09:03 +00:00
|
|
|
import 'package:photos/ui/settings/storage_progress_widget.dart';
|
2021-07-28 14:08:27 +00:00
|
|
|
import 'package:photos/utils/data_util.dart';
|
|
|
|
|
2022-11-01 10:19:01 +00:00
|
|
|
class StorageCardWidget extends StatefulWidget {
|
|
|
|
const StorageCardWidget({Key? key}) : super(key: key);
|
2021-07-28 14:08:27 +00:00
|
|
|
|
|
|
|
@override
|
2022-11-01 10:19:01 +00:00
|
|
|
State<StorageCardWidget> createState() => _StorageCardWidgetState();
|
2021-07-28 14:08:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:19:01 +00:00
|
|
|
class _StorageCardWidgetState extends State<StorageCardWidget> {
|
2022-10-10 05:49:31 +00:00
|
|
|
late Image _background;
|
2022-11-01 10:19:01 +00:00
|
|
|
final _logger = Logger((_StorageCardWidgetState).toString());
|
2022-10-19 10:28:13 +00:00
|
|
|
final ValueNotifier<bool> _isStorageCardPressed = ValueNotifier(false);
|
2021-07-28 14:08:27 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-07-04 06:02:17 +00:00
|
|
|
_background = const Image(
|
2022-06-24 15:29:24 +00:00
|
|
|
image: AssetImage("assets/storage_card_background.png"),
|
2022-06-19 11:26:07 +00:00
|
|
|
fit: BoxFit.fill,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
// precache background image to avoid flicker
|
|
|
|
// https://stackoverflow.com/questions/51343735/flutter-image-preload
|
|
|
|
precacheImage(_background.image, context);
|
2021-07-28 18:06:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:24:45 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_isStorageCardPressed.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-07-28 14:08:27 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-10-10 05:49:31 +00:00
|
|
|
final inheritedUserDetails = InheritedUserDetails.of(context);
|
2022-12-13 15:00:35 +00:00
|
|
|
final userDetails = inheritedUserDetails?.userDetails;
|
2022-10-10 05:49:31 +00:00
|
|
|
|
|
|
|
if (inheritedUserDetails == null) {
|
2022-10-10 06:21:59 +00:00
|
|
|
_logger.severe(
|
2022-11-01 10:19:01 +00:00
|
|
|
(InheritedUserDetails).toString() + 'is null',
|
2022-10-10 06:21:59 +00:00
|
|
|
);
|
2022-10-10 13:51:54 +00:00
|
|
|
throw Error();
|
2022-10-10 05:49:31 +00:00
|
|
|
} else {
|
|
|
|
return GestureDetector(
|
|
|
|
behavior: HitTestBehavior.translucent,
|
|
|
|
onTap: () async {
|
2023-12-21 07:34:06 +00:00
|
|
|
// ignore: unawaited_futures
|
2022-10-10 05:49:31 +00:00
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return getSubscriptionPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2022-10-10 05:07:51 +00:00
|
|
|
},
|
2022-10-31 14:45:56 +00:00
|
|
|
onTapDown: (details) => _isStorageCardPressed.value = true,
|
|
|
|
onTapCancel: () => _isStorageCardPressed.value = false,
|
|
|
|
onTapUp: (details) => _isStorageCardPressed.value = false,
|
2022-12-13 15:00:35 +00:00
|
|
|
child: containerForUserDetails(userDetails),
|
2022-10-10 05:49:31 +00:00
|
|
|
);
|
|
|
|
}
|
2021-07-28 14:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 06:21:59 +00:00
|
|
|
Widget containerForUserDetails(
|
2022-12-13 15:00:35 +00:00
|
|
|
UserDetails? userDetails,
|
2022-10-10 06:21:59 +00:00
|
|
|
) {
|
2023-08-02 11:24:21 +00:00
|
|
|
return Stack(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: double.infinity,
|
|
|
|
child: _background,
|
|
|
|
),
|
|
|
|
Positioned.fill(
|
|
|
|
child: userDetails is UserDetails
|
|
|
|
? _userDetails(userDetails)
|
|
|
|
: const EnteLoadingWidget(
|
|
|
|
color: strokeBaseDark,
|
2022-10-19 07:05:29 +00:00
|
|
|
),
|
2023-08-02 11:24:21 +00:00
|
|
|
),
|
|
|
|
Positioned.fill(
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 4),
|
|
|
|
child: ValueListenableBuilder<bool>(
|
|
|
|
builder: (BuildContext context, bool value, Widget? child) {
|
|
|
|
return Icon(
|
|
|
|
Icons.chevron_right_outlined,
|
|
|
|
color: value ? strokeMutedDark : strokeBaseDark,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
valueListenable: _isStorageCardPressed,
|
2022-10-19 07:05:29 +00:00
|
|
|
),
|
2022-06-14 04:48:22 +00:00
|
|
|
),
|
2023-08-02 11:24:21 +00:00
|
|
|
),
|
2022-10-19 07:05:29 +00:00
|
|
|
),
|
2023-08-02 11:24:21 +00:00
|
|
|
],
|
2021-07-28 14:08:27 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-10 06:21:59 +00:00
|
|
|
|
2022-12-13 15:00:35 +00:00
|
|
|
Widget _userDetails(UserDetails userDetails) {
|
2022-10-19 04:25:34 +00:00
|
|
|
const hundredMBinBytes = 107374182;
|
2022-11-07 13:29:45 +00:00
|
|
|
const oneTBinBytes = 1073741824000;
|
2022-10-19 04:25:34 +00:00
|
|
|
|
2022-11-07 13:56:18 +00:00
|
|
|
final usedStorageInBytes = userDetails.getFamilyOrPersonalUsage();
|
2022-11-07 13:29:45 +00:00
|
|
|
final totalStorageInBytes = userDetails.getTotalStorage();
|
|
|
|
final freeStorageInBytes = totalStorageInBytes - usedStorageInBytes;
|
2022-10-19 04:25:34 +00:00
|
|
|
|
2022-12-20 09:35:11 +00:00
|
|
|
final isMobileScreenSmall =
|
|
|
|
MediaQuery.of(context).size.width <= mobileSmallThreshold;
|
2022-11-08 08:45:17 +00:00
|
|
|
final shouldShowFreeSpaceInMBs = freeStorageInBytes < hundredMBinBytes;
|
2022-11-07 13:29:45 +00:00
|
|
|
final shouldShowFreeSpaceInTBs = freeStorageInBytes >= oneTBinBytes;
|
2022-11-07 13:56:18 +00:00
|
|
|
final shouldShowUsedStorageInTBs = usedStorageInBytes >= oneTBinBytes;
|
|
|
|
final shouldShowTotalStorageInTBs = totalStorageInBytes >= oneTBinBytes;
|
|
|
|
final shouldShowUsedStorageInMBs = usedStorageInBytes < hundredMBinBytes;
|
2022-11-07 13:29:45 +00:00
|
|
|
|
|
|
|
final usedStorageInGB = roundBytesUsedToGBs(
|
|
|
|
usedStorageInBytes,
|
|
|
|
freeStorageInBytes,
|
2022-10-26 12:15:48 +00:00
|
|
|
);
|
2022-11-07 13:29:45 +00:00
|
|
|
final totalStorageInGB = convertBytesToGBs(totalStorageInBytes).truncate();
|
|
|
|
|
|
|
|
final usedStorageInTB = roundGBsToTBs(usedStorageInGB);
|
|
|
|
final totalStorageInTB = roundGBsToTBs(totalStorageInGB);
|
2023-05-05 09:28:54 +00:00
|
|
|
late String freeSpace, freeSpaceUnit;
|
|
|
|
|
|
|
|
// Determine the appropriate free space and units
|
|
|
|
if (shouldShowFreeSpaceInTBs) {
|
|
|
|
freeSpace =
|
|
|
|
_roundedFreeSpace(totalStorageInTB, usedStorageInTB).toString();
|
|
|
|
freeSpaceUnit = "TB";
|
|
|
|
} else if (shouldShowFreeSpaceInMBs) {
|
|
|
|
freeSpace = max(0, convertBytesToMBs(freeStorageInBytes)).toString();
|
|
|
|
freeSpaceUnit = "MB";
|
|
|
|
} else {
|
|
|
|
freeSpace =
|
|
|
|
_roundedFreeSpace(totalStorageInGB, usedStorageInGB).toString();
|
|
|
|
freeSpaceUnit = "GB";
|
|
|
|
}
|
2022-10-19 02:19:20 +00:00
|
|
|
|
2022-10-19 11:10:21 +00:00
|
|
|
return Padding(
|
2022-10-19 13:22:16 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(
|
|
|
|
16,
|
|
|
|
20,
|
|
|
|
16,
|
2022-10-29 11:58:09 +00:00
|
|
|
isMobileScreenSmall ? 12 : 20,
|
2022-10-19 13:22:16 +00:00
|
|
|
),
|
2022-10-19 11:10:21 +00:00
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topLeft,
|
|
|
|
child: Column(
|
2022-10-19 07:05:29 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2022-10-10 06:21:59 +00:00
|
|
|
children: [
|
2022-10-19 11:10:21 +00:00
|
|
|
Text(
|
2023-05-05 08:55:58 +00:00
|
|
|
isMobileScreenSmall
|
|
|
|
? S.of(context).usedSpace
|
|
|
|
: S.of(context).storage,
|
2022-10-19 11:10:21 +00:00
|
|
|
style: getEnteTextTheme(context)
|
|
|
|
.small
|
|
|
|
.copyWith(color: textMutedDark),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 2),
|
2022-10-19 07:05:29 +00:00
|
|
|
RichText(
|
2022-10-19 11:10:21 +00:00
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
2022-10-19 07:05:29 +00:00
|
|
|
text: TextSpan(
|
|
|
|
style: getEnteTextTheme(context)
|
2022-10-19 11:10:21 +00:00
|
|
|
.h3Bold
|
|
|
|
.copyWith(color: textBaseDark),
|
2022-11-08 08:45:17 +00:00
|
|
|
children: _usedStorageDetails(
|
2022-11-07 13:29:45 +00:00
|
|
|
isMobileScreenSmall: isMobileScreenSmall,
|
2022-11-07 13:56:18 +00:00
|
|
|
shouldShowTotalStorageInTBs: shouldShowTotalStorageInTBs,
|
|
|
|
shouldShowUsedStorageInTBs: shouldShowUsedStorageInTBs,
|
|
|
|
shouldShowUsedStorageInMBs: shouldShowUsedStorageInMBs,
|
|
|
|
usedStorageInBytes: usedStorageInBytes,
|
2022-11-07 13:29:45 +00:00
|
|
|
usedStorageInGB: usedStorageInGB,
|
|
|
|
totalStorageInTB: totalStorageInTB,
|
|
|
|
usedStorageInTB: usedStorageInTB,
|
|
|
|
totalStorageInGB: totalStorageInGB,
|
|
|
|
),
|
2022-10-19 07:05:29 +00:00
|
|
|
),
|
2022-10-10 06:21:59 +00:00
|
|
|
),
|
|
|
|
],
|
2022-10-19 07:05:29 +00:00
|
|
|
),
|
2022-10-19 11:10:21 +00:00
|
|
|
),
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Stack(
|
|
|
|
children: <Widget>[
|
2022-10-19 14:09:03 +00:00
|
|
|
const StorageProgressWidget(
|
2022-10-20 10:11:57 +00:00
|
|
|
color:
|
|
|
|
Color.fromRGBO(255, 255, 255, 0.2), //hardcoded in figma
|
2022-10-19 14:09:03 +00:00
|
|
|
fractionOfStorage: 1,
|
2022-10-19 11:10:21 +00:00
|
|
|
),
|
2022-10-19 13:22:16 +00:00
|
|
|
userDetails.isPartOfFamily()
|
2022-10-19 14:09:03 +00:00
|
|
|
? StorageProgressWidget(
|
|
|
|
color: strokeBaseDark,
|
|
|
|
fractionOfStorage:
|
2022-10-19 13:22:16 +00:00
|
|
|
((userDetails.getFamilyOrPersonalUsage()) /
|
|
|
|
userDetails.getTotalStorage()),
|
|
|
|
)
|
|
|
|
: const SizedBox.shrink(),
|
2022-10-19 14:09:03 +00:00
|
|
|
StorageProgressWidget(
|
|
|
|
color: userDetails.isPartOfFamily()
|
|
|
|
? getEnteColorScheme(context).primary300
|
|
|
|
: strokeBaseDark,
|
|
|
|
fractionOfStorage:
|
2022-10-19 11:10:21 +00:00
|
|
|
(userDetails.usage / userDetails.getTotalStorage()),
|
2023-08-19 11:39:56 +00:00
|
|
|
),
|
2022-10-19 11:10:21 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
userDetails.isPartOfFamily()
|
|
|
|
? Row(
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
width: 8.71,
|
|
|
|
height: 8.99,
|
2022-10-19 13:22:16 +00:00
|
|
|
decoration: BoxDecoration(
|
2022-10-19 11:10:21 +00:00
|
|
|
shape: BoxShape.circle,
|
2022-10-19 13:22:16 +00:00
|
|
|
color: getEnteColorScheme(context).primary300,
|
2022-10-19 11:10:21 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 4),
|
|
|
|
Text(
|
2023-05-05 08:55:58 +00:00
|
|
|
S.of(context).storageBreakupYou,
|
2022-10-19 11:10:21 +00:00
|
|
|
style: getEnteTextTheme(context)
|
|
|
|
.miniBold
|
|
|
|
.copyWith(color: textBaseDark),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 12),
|
|
|
|
Container(
|
|
|
|
width: 8.71,
|
|
|
|
height: 8.99,
|
2022-10-19 13:22:16 +00:00
|
|
|
decoration: const BoxDecoration(
|
2022-10-19 11:10:21 +00:00
|
|
|
shape: BoxShape.circle,
|
2022-10-19 13:22:16 +00:00
|
|
|
color: textBaseDark,
|
2022-10-19 11:10:21 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 4),
|
|
|
|
Text(
|
2023-05-05 08:55:58 +00:00
|
|
|
S.of(context).storageBreakupFamily,
|
2022-10-19 11:10:21 +00:00
|
|
|
style: getEnteTextTheme(context)
|
|
|
|
.miniBold
|
|
|
|
.copyWith(color: textBaseDark),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2022-10-29 11:49:50 +00:00
|
|
|
: const SizedBox.shrink(),
|
2023-05-05 09:28:54 +00:00
|
|
|
Text(
|
|
|
|
S.of(context).freeStorageSpace(freeSpace, freeSpaceUnit),
|
|
|
|
style: getEnteTextTheme(context)
|
|
|
|
.mini
|
|
|
|
.copyWith(color: textFaintDark),
|
2022-10-19 11:10:21 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2023-08-19 11:39:56 +00:00
|
|
|
),
|
2022-10-19 11:10:21 +00:00
|
|
|
],
|
|
|
|
),
|
2022-10-10 06:21:59 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-19 02:19:20 +00:00
|
|
|
|
2022-11-07 13:29:45 +00:00
|
|
|
num _roundedFreeSpace(num totalStorageInGB, num usedStorageInGB) {
|
2022-10-19 02:19:20 +00:00
|
|
|
int fractionDigits;
|
2022-10-19 04:49:06 +00:00
|
|
|
//subtracting usedSpace from totalStorage in GB instead of converting from bytes so that free space and used space adds up in the UI
|
2022-11-07 13:29:45 +00:00
|
|
|
final freeStorage = totalStorageInGB - usedStorageInGB;
|
|
|
|
|
|
|
|
if (freeStorage >= 1000) {
|
|
|
|
return roundGBsToTBs(freeStorage);
|
|
|
|
}
|
2022-10-19 02:19:20 +00:00
|
|
|
//show one decimal place if free space is less than 10GB
|
2022-11-07 13:29:45 +00:00
|
|
|
if (freeStorage < 10) {
|
2022-10-19 02:19:20 +00:00
|
|
|
fractionDigits = 1;
|
|
|
|
} else {
|
|
|
|
fractionDigits = 0;
|
|
|
|
}
|
|
|
|
//omit decimal if decimal is 0
|
2022-11-07 13:29:45 +00:00
|
|
|
if (fractionDigits == 1 && freeStorage.remainder(1) == 0) {
|
2022-10-19 02:19:20 +00:00
|
|
|
fractionDigits = 0;
|
|
|
|
}
|
2022-11-07 13:29:45 +00:00
|
|
|
return num.parse(freeStorage.toStringAsFixed(fractionDigits));
|
|
|
|
}
|
|
|
|
|
2022-11-08 08:45:17 +00:00
|
|
|
List<TextSpan> _usedStorageDetails({
|
2022-11-07 13:29:45 +00:00
|
|
|
@required isMobileScreenSmall,
|
2022-11-07 13:56:18 +00:00
|
|
|
@required shouldShowUsedStorageInTBs,
|
|
|
|
@required shouldShowTotalStorageInTBs,
|
|
|
|
@required shouldShowUsedStorageInMBs,
|
|
|
|
@required usedStorageInBytes,
|
2022-11-07 13:29:45 +00:00
|
|
|
@required usedStorageInGB,
|
|
|
|
@required totalStorageInGB,
|
|
|
|
@required usedStorageInTB,
|
|
|
|
@required totalStorageInTB,
|
|
|
|
}) {
|
|
|
|
if (isMobileScreenSmall) {
|
|
|
|
return [
|
2023-05-05 09:15:07 +00:00
|
|
|
TextSpan(text: '$usedStorageInGB/$totalStorageInGB GB'),
|
2022-11-07 13:29:45 +00:00
|
|
|
];
|
|
|
|
}
|
2023-05-05 09:15:07 +00:00
|
|
|
late num currentUsage, totalStorage;
|
|
|
|
late String currentUsageUnit, totalStorageUnit;
|
|
|
|
|
|
|
|
// Determine the appropriate usage and units
|
|
|
|
if (shouldShowUsedStorageInTBs) {
|
|
|
|
currentUsage = usedStorageInTB;
|
|
|
|
currentUsageUnit = "TB";
|
|
|
|
} else if (shouldShowUsedStorageInMBs) {
|
|
|
|
currentUsage = convertBytesToMBs(usedStorageInBytes);
|
|
|
|
currentUsageUnit = "MB";
|
|
|
|
} else {
|
|
|
|
currentUsage = usedStorageInGB;
|
|
|
|
currentUsageUnit = "GB";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the appropriate total storage and units
|
|
|
|
if (shouldShowTotalStorageInTBs) {
|
|
|
|
totalStorage = totalStorageInTB;
|
|
|
|
totalStorageUnit = "TB";
|
|
|
|
} else {
|
|
|
|
totalStorage = totalStorageInGB;
|
|
|
|
totalStorageUnit = "GB";
|
|
|
|
}
|
2022-11-07 13:29:45 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
TextSpan(
|
2023-05-05 09:15:07 +00:00
|
|
|
text: S.of(context).storageUsageInfo(
|
|
|
|
currentUsage,
|
|
|
|
currentUsageUnit,
|
|
|
|
totalStorage,
|
|
|
|
totalStorageUnit,
|
|
|
|
),
|
2023-08-19 11:39:56 +00:00
|
|
|
),
|
2022-11-07 13:29:45 +00:00
|
|
|
];
|
2022-10-19 02:19:20 +00:00
|
|
|
}
|
2021-07-28 14:08:27 +00:00
|
|
|
}
|