Skip to content

Commit

Permalink
feat: complete app scan config page
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Oct 17, 2024
1 parent 6dcc5a9 commit 1950989
Show file tree
Hide file tree
Showing 6 changed files with 574 additions and 152 deletions.
111 changes: 78 additions & 33 deletions lib/common/app_scan/_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ Future<CommonAppFolderScanResult> scanCommonApps(
continue;
}
final pathFields = entry.path
.replaceFirst(setting.basePath + Platform.pathSeparator, '')
.replaceFirst(
setting.basePath.endsWith(Platform.pathSeparator)
? setting.basePath
: setting.basePath + Platform.pathSeparator,
'')
.split(Platform.pathSeparator);
for (var i = 0; i < pathFields.length; i++) {
if (i < setting.minInstallDirDepth) {
continue;
}
final currentPath = [
setting.basePath,
if (setting.basePath.endsWith(Platform.pathSeparator))
setting.basePath
else
setting.basePath + Platform.pathSeparator,
pathFields.sublist(0, i).join(Platform.pathSeparator),
].join(Platform.pathSeparator);
].join();

if (appMap[currentPath] != null) {
if (setting.minExecutableDepth <= i &&
Expand All @@ -84,7 +91,7 @@ Future<CommonAppFolderScanResult> scanCommonApps(
break;
}

if (i == pathFields.length - 1) {
if (i == setting.maxInstallDirDepth || i == pathFields.length - 1) {
appMap[currentPath] = InstalledCommonApps(
name: pathFields.last,
version: '',
Expand All @@ -94,37 +101,64 @@ Future<CommonAppFolderScanResult> scanCommonApps(
break;
}
}
}
for (final app in appMap.values) {
final pathFields = app.installPath
.replaceFirst(
setting.basePath.endsWith(Platform.pathSeparator)
? setting.basePath
: setting.basePath + Platform.pathSeparator,
'')
.split(Platform.pathSeparator);

for (final app in appMap.values) {
final pathFields = app.installPath
.replaceFirst(setting.basePath, '')
.split(Platform.pathSeparator);

var name = app.name;
var version = app.version;
late int endIndex;
switch (setting.pathFieldMatcherAlignment) {
case CommonAppFolderScanPathFieldMatcherAlignment.left:
endIndex = min(pathFields.length, setting.pathFieldMatcher.length);
case CommonAppFolderScanPathFieldMatcherAlignment.right:
endIndex = max(pathFields.length, setting.pathFieldMatcher.length);
}
for (var i = endIndex - pathFields.length; i < endIndex; i++) {
final fieldValue = pathFields[i];
switch (setting.pathFieldMatcher[i]) {
case CommonAppFolderScanPathFieldMatcher.name:
name = fieldValue;
case CommonAppFolderScanPathFieldMatcher.version:
version = fieldValue;
case CommonAppFolderScanPathFieldMatcher.ignore:
continue;
}
var name = app.name;
var version = app.version;
late int indexOffset;
switch (setting.pathFieldMatcherAlignment) {
case CommonAppFolderScanPathFieldMatcherAlignment.left:
indexOffset = 0;
case CommonAppFolderScanPathFieldMatcherAlignment.right:
indexOffset = max(pathFields.length, setting.pathFieldMatcher.length) -
pathFields.length;
}
for (var i = 0; i < pathFields.length; i++) {
final fieldValue = pathFields[i];
final currentPath = [
if (setting.basePath.endsWith(Platform.pathSeparator))
setting.basePath
else
setting.basePath + Platform.pathSeparator,
pathFields.sublist(0, i + 1).join(Platform.pathSeparator),
].join();
switch (setting.pathFieldMatcher[i + indexOffset]) {
case CommonAppFolderScanPathFieldMatcher.name:
name = fieldValue;
if (entryMap[currentPath] != null) {
entryMap[currentPath] = entryMap[currentPath]!.copyWith(
usedMatchers: [
...entryMap[currentPath]!.usedMatchers,
CommonAppFolderScanPathFieldMatcher.name
],
);
}
case CommonAppFolderScanPathFieldMatcher.version:
version = fieldValue;
if (entryMap[currentPath] != null) {
entryMap[currentPath] = entryMap[currentPath]!.copyWith(
usedMatchers: [
...entryMap[currentPath]!.usedMatchers,
CommonAppFolderScanPathFieldMatcher.version
],
);
}
case CommonAppFolderScanPathFieldMatcher.ignore:
continue;
}
appMap[app.installPath] = app.copyWith(
name: name,
version: version,
);
}
appMap[app.installPath] = app.copyWith(
name: name,
version: version,
);
}
final details = entryMap.values.toList();
details.sort((a, b) => a.path.compareTo(b.path));
Expand Down Expand Up @@ -183,6 +217,12 @@ Future<List<CommonAppFolderScanResultDetail>?> _walkDirectory(
if (!dir.existsSync()) {
return null;
}
if (PlatformHelper.isWindowsApp()) {
final result = await Process.run('attrib', [path]);
if (result.stdout.toString().contains('H')) {
return null;
}
}
final skipped = CommonAppFolderScanResultDetail(
path: path,
type: CommonAppFolderScanEntryType.directory,
Expand All @@ -191,7 +231,12 @@ Future<List<CommonAppFolderScanResultDetail>?> _walkDirectory(
if (remainWalkDepth == 0) {
return [skipped];
}
final dirName = dir.path.split(Platform.pathSeparator).last;
late String dirName;
if (dir.path.endsWith(Platform.pathSeparator)) {
dirName = dir.path.split(Platform.pathSeparator).reversed.skip(1).first;
} else {
dirName = dir.path.split(Platform.pathSeparator).last;
}
for (final matcher in setting.excludeDirectoryMatchers) {
if (Glob(matcher).matches(dirName)) {
return [skipped];
Expand Down
5 changes: 5 additions & 0 deletions lib/common/app_scan/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const defaultCommonAppFolderScanExcludeDirectoryMatchers = <String>[
'.*',
];

const allowedMinInstallDirDepth = 1;
const allowedMaxInstallDirDepth = 20;
const allowedMinExecutableDepth = 1;
const allowedMaxExecutableDepth = 20;

enum CommonAppFolderScanPathFieldMatcher {
ignore,
name,
Expand Down
77 changes: 62 additions & 15 deletions lib/view/form/form_field.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:input_quantity/input_quantity.dart';

class SectionDividerFormField extends FormField<void> {
SectionDividerFormField({super.key, required Widget title})
Expand All @@ -17,13 +18,13 @@ class SectionDividerFormField extends FormField<void> {
}

class CheckboxFormField extends FormField<bool> {
CheckboxFormField(
{super.key,
Widget? title,
super.onSaved,
super.validator,
bool super.initialValue = false})
: super(builder: (FormFieldState<bool> state) {
CheckboxFormField({
super.key,
Widget? title,
super.onSaved,
super.validator,
bool super.initialValue = false,
}) : super(builder: (FormFieldState<bool> state) {
return CheckboxListTile(
dense: state.hasError,
title: title,
Expand All @@ -44,18 +45,24 @@ class CheckboxFormField extends FormField<bool> {
}

class SwitchFormField extends FormField<bool> {
SwitchFormField(
{super.key,
Widget? title,
super.onSaved,
super.validator,
bool super.initialValue = false})
: super(builder: (FormFieldState<bool> state) {
SwitchFormField({
super.key,
Widget? title,
super.onSaved,
ValueChanged<bool>? onChanged,
super.validator,
bool super.initialValue = false,
}) : super(builder: (FormFieldState<bool> state) {
return SwitchListTile(
dense: state.hasError,
title: title,
value: state.value ?? false,
onChanged: state.didChange,
onChanged: (value) {
state.didChange(value);
if (onChanged != null) {
onChanged(value);
}
},
subtitle: state.hasError
? Builder(
builder: (BuildContext context) => Text(
Expand All @@ -69,3 +76,43 @@ class SwitchFormField extends FormField<bool> {
);
});
}

class NumberFormField extends FormField<num> {
NumberFormField({
super.key,
Widget? title,
super.onSaved,
num maxValue = double.infinity,
num minValue = double.negativeInfinity,
num steps = 1,
num super.initialValue = 0,
super.validator,
}) : super(builder: (FormFieldState<num> state) {
return Column(
children: [
if (title != null) title,
InputQty(
maxVal: maxValue,
initVal: initialValue,
minVal: minValue,
steps: steps,
validator: validator,
decoration: const QtyDecorationProps(
plusBtn: Icon(Icons.add),
minusBtn: Icon(Icons.remove),
isBordered: false,
borderShape: BorderShapeBtn.circle,
),
onQtyChanged: (value) {
if (value is num) {
state.didChange(value);
if (onSaved != null) {
onSaved(value);
}
}
},
),
],
);
});
}
27 changes: 19 additions & 8 deletions lib/view/helper/spacing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ final class SpacingHelper {
required Iterable<Widget> children,
double height = 0,
double width = 0,
}) =>
children
.expand((item) sync* {
yield SizedBox(height: height, width: width);
yield item;
})
.skip(1)
.toList();
bool includeEnds = false,
}) {
final list = children
.expand((item) sync* {
yield SizedBox(height: height, width: width);
yield item;
})
.skip(1)
.toList();
if (includeEnds) {
return [
SizedBox(height: height, width: width),
...list,
SizedBox(height: height, width: width),
];
} else {
return list;
}
}
}

class _DefaultDivider extends StatelessWidget {
Expand Down
Loading

0 comments on commit 1950989

Please sign in to comment.