fix & opt.

fix: servers will only refresh once if `spi.autoConnect` is false
opt.: `UrlText` algo
This commit is contained in:
lollipopkit
2023-09-19 13:31:06 +08:00
parent f47aacaf6f
commit 96d5b750ba
3 changed files with 31 additions and 55 deletions

View File

@@ -79,10 +79,6 @@ bool get isLinux => OS.type == OS.linux;
bool get isMacOS => OS.type == OS.macos;
bool get isWindows => OS.type == OS.windows;
bool get isWeb => OS.type == OS.web;
bool get isMobile =>
OS.type == OS.ios ||
OS.type == OS.android;
bool get isMobile => OS.type == OS.ios || OS.type == OS.android;
bool get isDesktop =>
OS.type == OS.linux ||
OS.type == OS.macos ||
OS.type == OS.windows;
OS.type == OS.linux || OS.type == OS.macos || OS.type == OS.windows;

View File

@@ -104,7 +104,15 @@ class ServerProvider extends ChangeNotifier {
if (s.state != ServerState.failed) return;
_limiter.reset(s.spi.id);
}
if (!(s.spi.autoConnect ?? true)) return;
/// If [spi.autoConnect] is false and server is disconnected, then skip.
///
/// If [spi.autoConnect] is false and server is connected, then refresh.
/// If no this, the server will only refresh once by clicking refresh button.
///
/// If [spi.autoConnect] is true, then refresh.
if (!(s.spi.autoConnect ?? true) && s.state == ServerState.disconnected) {
return;
}
return await _getData(s.spi);
}));
}

View File

@@ -7,8 +7,6 @@ import '../../core/utils/ui.dart';
final _reg = RegExp(
r"(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]*");
const _textStyle = TextStyle();
class UrlText extends StatelessWidget {
final String text;
final String? replace;
@@ -20,61 +18,42 @@ class UrlText extends StatelessWidget {
required this.text,
this.replace,
this.textAlign,
this.style = _textStyle,
this.style = const TextStyle(),
}) : super(key: key);
List<InlineSpan> getTextSpans(Color c) {
List<InlineSpan> _buildTextSpans(Color c) {
final widgets = <InlineSpan>[];
Iterable<Match> matches = _reg.allMatches(text);
List<_ResultMatch> resultMatches = <_ResultMatch>[];
int start = 0;
for (Match match in matches) {
for (final match in _reg.allMatches(text)) {
final group0 = match.group(0);
if (group0 != null && group0.isNotEmpty) {
if (start != match.start) {
_ResultMatch result1 = _ResultMatch(
false,
text.substring(start, match.start),
widgets.add(
TextSpan(
text: text.substring(start, match.start),
style: style.copyWith(color: c),
),
);
resultMatches.add(result1);
}
_ResultMatch result2 = _ResultMatch(
true,
match.group(0)!,
);
resultMatches.add(result2);
widgets.add(_LinkTextSpan(
replace: replace,
text: group0,
style: style.copyWith(color: primaryColor),
));
start = match.end;
}
}
if (start < text.length) {
_ResultMatch result1 = _ResultMatch(
false,
text.substring(start),
);
resultMatches.add(result1);
}
for (var result in resultMatches) {
if (result.isUrl) {
widgets.add(
_LinkTextSpan(
replace: replace ?? result.text,
text: result.text,
style: style.copyWith(color: primaryColor),
),
);
} else {
widgets.add(
TextSpan(
text: result.text,
text: text.substring(start),
style: style.copyWith(color: c),
),
);
}
}
return widgets;
}
@@ -83,7 +62,7 @@ class UrlText extends StatelessWidget {
return RichText(
textAlign: textAlign ?? TextAlign.start,
text: TextSpan(
children: getTextSpans(DynamicColors.content.resolve(context)),
children: _buildTextSpans(DynamicColors.content.resolve(context)),
),
);
}
@@ -93,17 +72,10 @@ class _LinkTextSpan extends TextSpan {
_LinkTextSpan({TextStyle? style, required String text, String? replace})
: super(
style: style,
text: replace,
text: replace ?? text,
recognizer: TapGestureRecognizer()
..onTap = () {
openUrl(text);
},
);
}
class _ResultMatch {
final bool isUrl;
final String text;
_ResultMatch(this.isUrl, this.text);
}