Files
flutter_server_box/lib/view/widget/unix_perm.dart
lollipopkit🏳️‍⚧️ 3a615449e3 feat: Windows compatibility (#836)
* feat: win compatibility

* fix

* fix: uptime parse

* opt.: linux uptime accuracy

* fix: windows temperature fetching

* opt.

* opt.: powershell exec

* refactor: address PR review feedback and improve code quality

### Major Improvements:
- **Refactored Windows status parsing**: Broke down large `_getWindowsStatus` method into 13 smaller, focused helper methods for better maintainability and readability
- **Extracted system detection logic**: Created dedicated `SystemDetector` helper class to separate OS detection concerns from ServerProvider
- **Improved concurrency handling**: Implemented proper synchronization for server updates using Future-based locks to prevent race conditions

### Bug Fixes:
- **Fixed CPU percentage parsing**: Removed incorrect '*100' multiplication in BSD CPU parsing (values were already percentages)
- **Enhanced memory parsing**: Added validation and error handling to BSD memory fallback parsing with proper logging
- **Improved uptime parsing**: Added support for multiple Windows date formats and robust error handling with validation
- **Fixed division by zero**: Added safety checks in Swap.usedPercent getter

### Code Quality Enhancements:
- **Added comprehensive documentation**: Documented Windows CPU counter limitations and approach
- **Strengthened error handling**: Added detailed logging and validation throughout parsing methods
- **Improved robustness**: Enhanced BSD CPU parsing with percentage validation and warnings
- **Better separation of concerns**: Each parsing method now has single responsibility

### Files Changed:
- `lib/data/helper/system_detector.dart` (new): System detection helper
- `lib/data/model/server/cpu.dart`: Fixed percentage parsing and added validation
- `lib/data/model/server/memory.dart`: Enhanced fallback parsing and division-by-zero protection
- `lib/data/model/server/server_status_update_req.dart`: Refactored into 13 focused parsing methods
- `lib/data/provider/server.dart`: Improved synchronization and extracted system detection

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: parse & shell fn struct

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-08 16:56:36 +08:00

151 lines
3.5 KiB
Dart

import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
final class UnixPermOp {
final bool r;
final bool w;
final bool x;
const UnixPermOp({required this.r, required this.w, required this.x});
UnixPermOp copyWith({bool? r, bool? w, bool? x}) {
return UnixPermOp(r: r ?? this.r, w: w ?? this.w, x: x ?? this.x);
}
int get value {
return (r ? 4 : 0) + (w ? 2 : 0) + (x ? 1 : 0);
}
}
enum UnixPermScope {
user,
group,
other;
String get title {
return switch (this) {
user => 'User',
group => 'Group',
other => 'Other',
};
}
}
final class UnixPerm {
final UnixPermOp user;
final UnixPermOp group;
final UnixPermOp other;
const UnixPerm({
required this.user,
required this.group,
required this.other,
});
UnixPerm copyWith({UnixPermOp? user, UnixPermOp? group, UnixPermOp? other}) {
return UnixPerm(
user: user ?? this.user,
group: group ?? this.group,
other: other ?? this.other,
);
}
UnixPerm copyWithScope(UnixPermScope scope, UnixPermOp rwx) {
switch (scope) {
case UnixPermScope.user:
return copyWith(user: rwx);
case UnixPermScope.group:
return copyWith(group: rwx);
case UnixPermScope.other:
return copyWith(other: rwx);
}
}
/// eg.: 744
String get perm {
return '${user.value}${group.value}${other.value}';
}
static UnixPerm get empty => const UnixPerm(
user: UnixPermOp(r: false, w: false, x: false),
group: UnixPermOp(r: false, w: false, x: false),
other: UnixPermOp(r: false, w: false, x: false),
);
}
final class UnixPermEditor extends StatefulWidget {
final UnixPerm perm;
final void Function(UnixPerm) onChanged;
const UnixPermEditor({
super.key,
required this.perm,
required this.onChanged,
});
@override
State<UnixPermEditor> createState() => _UnixPermEditorState();
}
final class _UnixPermEditorState extends State<UnixPermEditor> {
late UnixPerm perm;
@override
void initState() {
super.initState();
perm = widget.perm;
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('Read'),
Text('Writ'), // Keep it short to fit UI
Text('Exec'),
],
).paddingOnly(left: 13),
UIs.height7,
_buildRow(UnixPermScope.user, perm.user),
_buildRow(UnixPermScope.group, perm.group),
_buildRow(UnixPermScope.other, perm.other),
],
);
}
Widget _buildRow(UnixPermScope scope, UnixPermOp rwx) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(width: 7, child: Text(scope.title)),
_buildSwitch(rwx.r, (v) {
setState(() {
perm = perm.copyWithScope(scope, rwx.copyWith(r: v));
widget.onChanged(perm);
});
}),
_buildSwitch(rwx.w, (v) {
setState(() {
perm = perm.copyWithScope(scope, rwx.copyWith(w: v));
widget.onChanged(perm);
});
}),
_buildSwitch(rwx.x, (v) {
setState(() {
perm = perm.copyWithScope(scope, rwx.copyWith(x: v));
widget.onChanged(perm);
});
}),
],
);
}
Widget _buildSwitch(bool value, void Function(bool) onChanged) {
return Switch(value: value, onChanged: onChanged);
}
}