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';
|
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';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/common/loading_widget.dart';
|
2022-10-10 13:51:54 +00:00
|
|
|
// ignore: import_of_legacy_library_into_null_safe
|
2022-05-30 04:37:54 +00:00
|
|
|
import 'package:photos/ui/payment/subscription.dart';
|
2022-10-19 11:10:21 +00:00
|
|
|
import 'package:photos/ui/settings/storage_error_widget.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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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-10-10 06:21:59 +00:00
|
|
|
child: containerForUserDetails(inheritedUserDetails),
|
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(
|
|
|
|
InheritedUserDetails inheritedUserDetails,
|
|
|
|
) {
|
2022-10-05 12:37:43 +00:00
|
|
|
return ConstrainedBox(
|
2022-10-31 13:48:54 +00:00
|
|
|
constraints: const BoxConstraints(maxWidth: 350),
|
2022-10-19 07:05:29 +00:00
|
|
|
child: AspectRatio(
|
|
|
|
aspectRatio: 2 / 1,
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
_background,
|
2022-10-19 11:10:21 +00:00
|
|
|
FutureBuilder(
|
|
|
|
future: inheritedUserDetails.userDetails,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
return userDetails(snapshot.data as UserDetails);
|
|
|
|
}
|
2022-10-20 11:56:48 +00:00
|
|
|
if (snapshot.hasError) {
|
2022-10-19 11:10:21 +00:00
|
|
|
_logger.severe(
|
|
|
|
'failed to load user details',
|
|
|
|
snapshot.error,
|
|
|
|
);
|
|
|
|
return const StorageErrorWidget();
|
|
|
|
}
|
2022-10-19 12:17:45 +00:00
|
|
|
return const EnteLoadingWidget(color: strokeBaseDark);
|
2022-10-19 11:10:21 +00:00
|
|
|
},
|
2022-06-09 15:30:45 +00:00
|
|
|
),
|
2022-10-19 10:28:13 +00:00
|
|
|
Align(
|
2022-10-19 07:05:29 +00:00
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
child: Padding(
|
2022-10-19 10:28:13 +00:00
|
|
|
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
|
|
|
),
|
2022-10-19 07:05:29 +00:00
|
|
|
],
|
|
|
|
),
|
2021-07-28 14:08:27 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-10-10 06:21:59 +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-11-21 11:12:52 +00:00
|
|
|
final isMobileScreenSmall = MediaQuery.of(context).size.width <= 336;
|
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);
|
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(
|
2022-10-20 10:44:46 +00:00
|
|
|
isMobileScreenSmall ? "Used space" : "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()),
|
2022-10-19 14:09:03 +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(
|
|
|
|
"You",
|
|
|
|
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(
|
|
|
|
"Family",
|
|
|
|
style: getEnteTextTheme(context)
|
|
|
|
.miniBold
|
|
|
|
.copyWith(color: textBaseDark),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2022-10-29 11:49:50 +00:00
|
|
|
: const SizedBox.shrink(),
|
2022-10-19 11:10:21 +00:00
|
|
|
RichText(
|
|
|
|
text: TextSpan(
|
|
|
|
style: getEnteTextTheme(context)
|
|
|
|
.mini
|
|
|
|
.copyWith(color: textFaintDark),
|
|
|
|
children: [
|
|
|
|
TextSpan(
|
|
|
|
text:
|
2022-11-08 08:22:12 +00:00
|
|
|
"${shouldShowFreeSpaceInMBs ? max(0, convertBytesToMBs(freeStorageInBytes)) : _roundedFreeSpace(totalStorageInGB, usedStorageInGB)}",
|
2022-10-19 11:10:21 +00:00
|
|
|
),
|
|
|
|
TextSpan(
|
2022-11-07 13:29:45 +00:00
|
|
|
text: shouldShowFreeSpaceInTBs
|
|
|
|
? " TB free"
|
|
|
|
: shouldShowFreeSpaceInMBs
|
|
|
|
? " MB free"
|
|
|
|
: " GB free",
|
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 [
|
|
|
|
TextSpan(text: usedStorageInGB.toString() + "/"),
|
|
|
|
TextSpan(text: totalStorageInGB.toString() + " GB"),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
TextSpan(
|
2022-11-07 13:56:18 +00:00
|
|
|
text: shouldShowUsedStorageInTBs
|
2022-11-07 13:29:45 +00:00
|
|
|
? usedStorageInTB.toString() + " TB of "
|
2022-11-07 13:56:18 +00:00
|
|
|
: shouldShowUsedStorageInMBs
|
|
|
|
? convertBytesToMBs(usedStorageInBytes).toString() + " MB of "
|
|
|
|
: usedStorageInGB.toString() + " GB of ",
|
2022-11-07 13:29:45 +00:00
|
|
|
),
|
|
|
|
TextSpan(
|
2022-11-07 13:56:18 +00:00
|
|
|
text: shouldShowTotalStorageInTBs
|
2022-11-07 13:29:45 +00:00
|
|
|
? totalStorageInTB.toString() + " TB used"
|
|
|
|
: totalStorageInGB.toString() + " GB used",
|
|
|
|
),
|
|
|
|
];
|
2022-10-19 02:19:20 +00:00
|
|
|
}
|
2021-07-28 14:08:27 +00:00
|
|
|
}
|