Skip to content

Commit

Permalink
refactor(installer): port from filesize to ubuntu_localizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jpnurmi committed Jul 4, 2023
1 parent 9207ab7 commit d85378d
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -138,12 +137,13 @@ class _PartitionLabel extends StatelessWidget {
final Partition partition;
final Partition? original;

String formatPartition(AppLocalizations lang) {
String formatPartition(BuildContext context) {
final lang = AppLocalizations.of(context);
if (partition.resize == true) {
return lang.writeChangesPartitionResized(
partition.sysname,
filesize(original?.size ?? 0),
filesize(partition.size ?? 0),
context.formatByteSize(original?.size ?? 0),
context.formatByteSize(partition.size ?? 0),
);
} else if (partition.wipe != null && partition.mount?.isNotEmpty == true) {
return lang.writeChangesPartitionFormattedMounted(
Expand All @@ -169,9 +169,8 @@ class _PartitionLabel extends StatelessWidget {

@override
Widget build(BuildContext context) {
final lang = AppLocalizations.of(context);
return Html(
data: formatPartition(lang),
data: formatPartition(context),
style: {'body': Style(margin: Margins.zero)},
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:math';

import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ubuntu_desktop_installer/installer.dart';
Expand Down Expand Up @@ -53,7 +52,7 @@ class NotEnoughDiskSpacePage extends ConsumerWidget {
),
const SizedBox(width: 8),
Text(
filesize(model.installMinimumSize),
context.formatByteSize(model.installMinimumSize),
style: Theme.of(context)
.textTheme
.bodyLarge!
Expand All @@ -70,7 +69,7 @@ class NotEnoughDiskSpacePage extends ConsumerWidget {
),
const SizedBox(width: 8),
Text(
filesize(model.largestDiskSize),
context.formatByteSize(model.largestDiskSize),
style: Theme.of(context)
.textTheme
.bodyLarge!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
Expand All @@ -24,13 +23,13 @@ class GuidedReformatPage extends ConsumerWidget {
}

/// Formats a disk in a pretty way e.g. "sda ATA Maxtor (123 GB)"
String prettyFormatDisk(Disk disk) {
String prettyFormatDisk(BuildContext context, Disk disk) {
final fullName = <String?>[
disk.model,
disk.vendor,
].where((p) => p?.isNotEmpty == true).join(' ');

final size = filesize(disk.size);
final size = context.formatByteSize(disk.size);
return '${disk.sysname} - $size $fullName';
}

Expand Down Expand Up @@ -58,7 +57,8 @@ class GuidedReformatPage extends ConsumerWidget {
itemBuilder: (context, index, child) {
final disk = model.getDisk(index);
return disk != null
? Text(prettyFormatDisk(disk), key: ValueKey(index))
? Text(prettyFormatDisk(context, disk),
key: ValueKey(index))
: const SizedBox.shrink();
},
),
Expand Down Expand Up @@ -88,7 +88,7 @@ class GuidedReformatPage extends ConsumerWidget {
Text(model.selectedDisk?.sysname ?? ''),
const SizedBox(height: kWizardSpacing / 2),
Text(
filesize(model.selectedDisk?.size ?? 0),
context.formatByteSize(model.selectedDisk?.size ?? 0),
style: Theme.of(context).textTheme.headlineSmall,
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -31,7 +30,7 @@ class StorageSelector extends ConsumerWidget {
final parts = [
partition?.sysname,
if (os != null) os.long,
if (partition?.size != null) filesize(partition!.size),
if (partition?.size != null) context.formatByteSize(partition!.size!),
];
return parts.join(' - ');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:ubuntu_localizations/ubuntu_localizations.dart';
import 'package:ubuntu_wizard/ubuntu_wizard.dart';

import 'storage_icon.dart';
Expand Down Expand Up @@ -48,7 +48,7 @@ class StorageButton extends StatelessWidget {
),
const SizedBox(height: kWizardSpacing / 2),
Text(
filesize(size),
context.formatByteSize(size),
style: Theme.of(context).textTheme.headlineSmall,
overflow: TextOverflow.clip,
softWrap: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:collection/collection.dart';
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
Expand Down Expand Up @@ -150,7 +149,10 @@ class _PartitionLabel extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: Theme.of(context).textTheme.titleSmall),
Text(filesize(size), style: Theme.of(context).textTheme.bodySmall),
Text(
context.formatByteSize(size),
style: Theme.of(context).textTheme.bodySmall,
),
],
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:subiquity_client/subiquity_client.dart';
Expand Down Expand Up @@ -131,7 +130,7 @@ class StorageSizeColumn extends StorageColumn {
return Text(lang.diskHeadersSize);
},
diskBuilder: (context, disk) {
return Text(filesize(disk.size));
return Text(context.formatByteSize(disk.size));
},
gapBuilder: (context, disk, gap) {
final lang = AppLocalizations.of(context);
Expand All @@ -143,13 +142,13 @@ class StorageSizeColumn extends StorageColumn {
? lang.tooManyPrimaryPartitions
: '',
child: Text(
filesize(gap.size),
context.formatByteSize(gap.size),
style: TextStyle(color: color),
),
);
},
partitionBuilder: (context, disk, partition) {
return Text(filesize(partition.size ?? 0));
return Text(context.formatByteSize(partition.size ?? 0));
},
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:subiquity_client/subiquity_client.dart';
import 'package:ubuntu_localizations/ubuntu_localizations.dart';
import 'package:ubuntu_widgets/ubuntu_widgets.dart';

class StorageSelector extends StatelessWidget {
Expand All @@ -26,7 +26,7 @@ class StorageSelector extends StatelessWidget {
storage.model,
storage.vendor,
].where((p) => p?.isNotEmpty == true).join(' ');
return '${storage.sysname} $fullName (${filesize(storage.size)})';
return '${storage.sysname} $fullName (${context.formatByteSize(storage.size)})';
}

return Column(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:subiquity_client/subiquity_client.dart';

export 'package:subiquity_client/subiquity_client.dart'
Expand All @@ -9,16 +8,11 @@ extension GapExtension on Gap {
usable == GapUsable.TOO_MANY_PRIMARY_PARTS;
}

extension DiskExtension on Disk {
String get prettySize => filesize(size);
}

extension PartitionExtension on Partition {
bool get canWipe => PartitionFormat.fromPartition(this)?.canWipe == true;
bool get canEdit => format != 'BitLocker';
bool get isEncrypted => format == 'BitLocker';
bool get isWiped => wipe == 'superblock';
String get prettySize => filesize(size ?? 0);
bool mustWipe(String? format) {
// a preserved partition must be wiped if its format changed
return preserve == true && format != null && this.format != format;
Expand Down
1 change: 0 additions & 1 deletion packages/ubuntu_desktop_installer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dependencies:
dbus: ^0.7.3
diacritic: ^0.1.3
file: ^6.1.0
filesize: ^2.0.0
flutter:
sdk: flutter
flutter_html: 3.0.0-beta.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
Expand Down Expand Up @@ -112,8 +111,8 @@ void main() {
find.html(tester.lang.writeChangesPartitionMounted('sdb5', '/mnt/5')),
findsOneWidget);
expect(
find.html(tester.lang
.writeChangesPartitionResized('sdb6', filesize(123), filesize(66))),
find.html(
tester.lang.writeChangesPartitionResized('sdb6', '123 B', '66 B')),
findsOneWidget);
expect(find.html(tester.lang.writeChangesPartitionCreated('sdb7')),
findsOneWidget);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ubuntu_desktop_installer/l10n.dart';
import 'package:ubuntu_desktop_installer/pages/source/not_enough_disk_space/not_enough_disk_space_model.dart';
import 'package:ubuntu_desktop_installer/pages/source/not_enough_disk_space/not_enough_disk_space_page.dart';
import 'package:yaru_test/yaru_test.dart';
Expand All @@ -25,10 +25,11 @@ void main() {
largestDiskSize: 123456, installMinimumSize: 654321);
await tester.pumpWidget(tester.buildApp((_) => buildPage(model)));

final context = tester.element(find.byType(NotEnoughDiskSpacePage));
expect(find.text(tester.lang.notEnoughDiskSpaceUbuntu('Ubuntu')),
findsOneWidget);
expect(find.text(filesize(123456)), findsOneWidget);
expect(find.text(filesize(654321)), findsOneWidget);
expect(find.text(context.formatByteSize(123456)), findsOneWidget);
expect(find.text(context.formatByteSize(654321)), findsOneWidget);
});

testWidgets('quit', (tester) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -7,6 +6,7 @@ import 'package:subiquity_client/subiquity_client.dart';
import 'package:subiquity_test/subiquity_test.dart';
import 'package:ubuntu_desktop_installer/pages/storage/guided_reformat/guided_reformat_model.dart';
import 'package:ubuntu_desktop_installer/pages/storage/guided_reformat/guided_reformat_page.dart';
import 'package:ubuntu_localizations/ubuntu_localizations.dart';
import 'package:ubuntu_test/ubuntu_test.dart';
import 'package:ubuntu_widgets/ubuntu_widgets.dart';

Expand Down Expand Up @@ -80,7 +80,9 @@ void main() {
);
await tester.pumpWidget(tester.buildApp((_) => buildPage(model)));
expect(find.text(selectedDisk.sysname), findsOneWidget);
expect(find.text(filesize(selectedDisk.size)), findsOneWidget);
final context = tester.element(find.byType(GuidedReformatPage));
expect(
find.text(context.formatByteSize(selectedDisk.size)), findsOneWidget);
});

testWidgets('saves guided storage', (tester) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -9,6 +8,7 @@ import 'package:ubuntu_desktop_installer/pages/storage/manual/manual_storage_mod
import 'package:ubuntu_desktop_installer/pages/storage/manual/manual_storage_page.dart';
import 'package:ubuntu_desktop_installer/pages/storage/manual/storage_selector.dart';
import 'package:ubuntu_desktop_installer/services.dart';
import 'package:ubuntu_localizations/ubuntu_localizations.dart';
import 'package:ubuntu_test/ubuntu_test.dart';
import 'package:yaru_icons/yaru_icons.dart';
import 'package:yaru_test/yaru_test.dart';
Expand Down Expand Up @@ -78,14 +78,16 @@ void main() {
final model = buildManualStorageModel(disks: testDisks);
await tester.pumpWidget(tester.buildApp((_) => buildPage(model)));

final context = tester.element(find.byType(ManualStoragePage));
for (final disk in testDisks) {
expect(find.text(disk.sysname), findsOneWidget);
expect(find.text(filesize(disk.size)), findsOneWidget);
expect(find.text(context.formatByteSize(disk.size)), findsOneWidget);

for (final partition in disk.partitions.whereType<Partition>()) {
expect(find.text(partition.format!), findsOneWidget);
expect(find.text(partition.mount!), findsOneWidget);
expect(find.text(filesize(partition.size!)), findsOneWidget);
expect(
find.text(context.formatByteSize(partition.size!)), findsOneWidget);
}
}
});
Expand Down

0 comments on commit d85378d

Please sign in to comment.