fix & opt

fix: android in-app upgrade
fmt: proj struct
opt: fetch primaryColor
This commit is contained in:
lollipopkit
2023-02-01 12:52:40 +08:00
parent 068089d207
commit 4d741ac82a
30 changed files with 523 additions and 448 deletions

23
lib/core/utils/misc.dart Normal file
View File

@@ -0,0 +1,23 @@
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:share_plus/share_plus.dart';
import '../../generated/l10n.dart';
Future<bool> shareFiles(BuildContext context, List<String> filePaths) async {
for (final filePath in filePaths) {
if (!await File(filePath).exists()) {
return false;
}
}
var text = '';
if (filePaths.length == 1) {
text = filePaths.first.split('/').last;
} else {
text = '${filePaths.length} ${S.of(context).files}';
}
final xfiles = filePaths.map((e) => XFile(e)).toList();
await Share.shareXFiles(xfiles, text: 'ServerBox -> $text');
return filePaths.isNotEmpty;
}

View File

@@ -0,0 +1,32 @@
import 'dart:async';
import 'package:dartssh2/dartssh2.dart';
import 'package:flutter/foundation.dart';
import '../../data/model/server/server_private_info.dart';
import '../../data/store/private_key.dart';
import '../../locator.dart';
/// Must put this func out of any Class.
/// Because of this function is called by [compute] in [ServerProvider.genClient].
/// https://stackoverflow.com/questions/51998995/invalid-arguments-illegal-argument-in-isolate-message-object-is-a-closure
List<SSHKeyPair> loadIndentity(String key) {
return SSHKeyPair.fromPem(key);
}
Future<SSHClient> genClient(ServerPrivateInfo spi) async {
final socket = await SSHSocket.connect(spi.ip, spi.port);
if (spi.pubKeyId == null) {
return SSHClient(
socket,
username: spi.user,
onPasswordRequest: () => spi.pwd,
);
}
final key = locator<PrivateKeyStore>().get(spi.pubKeyId!);
return SSHClient(
socket,
username: spi.user,
identities: await compute(loadIndentity, key.privateKey),
);
}

103
lib/core/utils/ui.dart Normal file
View File

@@ -0,0 +1,103 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:toolbox/core/extension/stringx.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../generated/l10n.dart';
import '../../view/widget/card_dialog.dart';
import '../persistant_store.dart';
bool isDarkMode(BuildContext context) =>
Theme.of(context).brightness == Brightness.dark;
void showSnackBar(BuildContext context, Widget child) =>
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: child));
void showSnackBarWithAction(BuildContext context, String content, String action,
GestureTapCallback onTap) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(content),
action: SnackBarAction(
label: action,
onPressed: onTap,
),
));
}
Future<bool> openUrl(String url) async {
return await launchUrl(url.uri, mode: LaunchMode.externalApplication);
}
Future<T?>? showRoundDialog<T>(
BuildContext context, String title, Widget child, List<Widget> actions,
{EdgeInsets? padding, bool barrierDismiss = true}) {
return showDialog<T>(
context: context,
barrierDismissible: barrierDismiss,
builder: (ctx) {
return CardDialog(
title: Text(title),
content: child,
actions: actions,
padding: padding,
);
});
}
Widget buildSwitch(BuildContext context, StoreProperty<bool> prop,
{Function(bool)? func}) {
return ValueListenableBuilder(
valueListenable: prop.listenable(),
builder: (context, bool value, widget) {
return Switch(
value: value,
onChanged: (value) {
if (func != null) func(value);
prop.put(value);
});
},
);
}
void setTransparentNavigationBar(BuildContext context) {
if (Platform.isAndroid) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
systemNavigationBarColor: Colors.transparent,
systemNavigationBarContrastEnforced: true));
}
}
Widget buildPopuopMenu(
{required List<PopupMenuEntry> items,
required Function(dynamic) onSelected}) {
return PopupMenuButton(
itemBuilder: (_) => items,
onSelected: onSelected,
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
child: const Padding(
padding: EdgeInsets.only(left: 7),
child: Icon(
Icons.more_vert,
size: 21,
),
),
);
}
String tabTitleName(BuildContext context, int i) {
final s = S.of(context);
switch (i) {
case 0:
return s.server;
case 1:
return s.convert;
case 2:
return s.ping;
default:
return '';
}
}