2022-04-14 15:37:47 +08:00
|
|
|
import 'package:flutter/gestures.dart';
|
2020-11-15 20:04:05 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:async';
|
2022-04-19 13:07:45 +08:00
|
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
|
|
|
|
|
|
import 'models/model.dart';
|
2021-08-02 20:54:56 +08:00
|
|
|
|
2022-03-07 22:54:34 +08:00
|
|
|
final globalKey = GlobalKey<NavigatorState>();
|
2022-04-12 22:38:39 +08:00
|
|
|
final navigationBarKey = GlobalKey();
|
2022-03-07 22:54:34 +08:00
|
|
|
|
2022-03-17 21:03:52 +08:00
|
|
|
var isAndroid = false;
|
|
|
|
var isIOS = false;
|
|
|
|
var isWeb = false;
|
|
|
|
var isDesktop = false;
|
|
|
|
var version = "";
|
2022-03-24 17:58:33 +08:00
|
|
|
int androidVersion = 0;
|
2022-03-17 21:03:52 +08:00
|
|
|
|
2021-08-02 20:54:56 +08:00
|
|
|
typedef F = String Function(String);
|
2022-02-10 02:07:53 +08:00
|
|
|
typedef FMethod = String Function(String, dynamic);
|
2021-08-02 20:54:56 +08:00
|
|
|
|
|
|
|
class Translator {
|
2022-02-17 15:22:14 +08:00
|
|
|
static late F call;
|
2021-04-25 00:19:35 +08:00
|
|
|
}
|
2020-11-17 18:10:49 +08:00
|
|
|
|
2020-11-16 01:13:26 +08:00
|
|
|
class MyTheme {
|
2020-11-20 16:37:48 +08:00
|
|
|
MyTheme._();
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-16 01:13:26 +08:00
|
|
|
static const Color grayBg = Color(0xFFEEEEEE);
|
|
|
|
static const Color white = Color(0xFFFFFFFF);
|
2020-11-16 22:00:09 +08:00
|
|
|
static const Color accent = Color(0xFF0071FF);
|
2020-11-19 17:22:42 +08:00
|
|
|
static const Color accent50 = Color(0x770071FF);
|
2020-11-27 02:14:27 +08:00
|
|
|
static const Color accent80 = Color(0xAA0071FF);
|
2020-11-19 18:41:37 +08:00
|
|
|
static const Color canvasColor = Color(0xFF212121);
|
2020-11-20 13:06:52 +08:00
|
|
|
static const Color border = Color(0xFFCCCCCC);
|
2022-01-31 16:22:05 +08:00
|
|
|
static const Color idColor = Color(0xFF00B6F0);
|
|
|
|
static const Color darkGray = Color(0xFFB9BABC);
|
2020-11-16 01:13:26 +08:00
|
|
|
}
|
|
|
|
|
2021-08-02 20:54:56 +08:00
|
|
|
final ButtonStyle flatButtonStyle = TextButton.styleFrom(
|
2022-05-18 15:47:07 +08:00
|
|
|
minimumSize: Size(0, 36),
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
|
2021-08-02 20:54:56 +08:00
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(2.0)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-04-19 17:53:35 +08:00
|
|
|
void showToast(String text, {Duration? duration}) {
|
|
|
|
SmartDialog.showToast(text, displayTime: duration);
|
2022-04-15 17:50:15 +08:00
|
|
|
}
|
|
|
|
|
2022-04-19 13:07:45 +08:00
|
|
|
void showLoading(String text, {bool clickMaskDismiss = false}) {
|
|
|
|
SmartDialog.dismiss();
|
|
|
|
SmartDialog.showLoading(
|
|
|
|
clickMaskDismiss: false,
|
|
|
|
builder: (context) {
|
|
|
|
return Container(
|
|
|
|
color: MyTheme.white,
|
|
|
|
constraints: BoxConstraints(maxWidth: 240),
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
SizedBox(height: 30),
|
|
|
|
Center(child: CircularProgressIndicator()),
|
|
|
|
SizedBox(height: 20),
|
|
|
|
Center(
|
|
|
|
child: Text(Translator.call(text),
|
|
|
|
style: TextStyle(fontSize: 15))),
|
|
|
|
SizedBox(height: 20),
|
|
|
|
Center(
|
|
|
|
child: TextButton(
|
|
|
|
style: flatButtonStyle,
|
|
|
|
onPressed: () {
|
|
|
|
SmartDialog.dismiss();
|
|
|
|
backToHome();
|
|
|
|
},
|
|
|
|
child: Text(Translator.call('Cancel'),
|
|
|
|
style: TextStyle(color: MyTheme.accent))))
|
|
|
|
]));
|
|
|
|
});
|
2020-11-16 21:21:27 +08:00
|
|
|
}
|
|
|
|
|
2022-03-13 23:07:52 +08:00
|
|
|
backToHome() {
|
|
|
|
Navigator.popUntil(globalKey.currentContext!, ModalRoute.withName("/"));
|
|
|
|
}
|
|
|
|
|
2022-03-13 00:32:44 +08:00
|
|
|
typedef DialogBuilder = CustomAlertDialog Function(
|
2022-04-19 13:07:45 +08:00
|
|
|
StateSetter setState, void Function([dynamic]) close);
|
2022-03-13 00:32:44 +08:00
|
|
|
|
|
|
|
class DialogManager {
|
2022-04-19 13:07:45 +08:00
|
|
|
static int _tag = 0;
|
2022-03-13 00:32:44 +08:00
|
|
|
|
2022-04-19 13:07:45 +08:00
|
|
|
static dismissByTag(String tag, [result]) {
|
|
|
|
SmartDialog.dismiss(tag: tag, result: result);
|
2022-02-28 18:29:25 +08:00
|
|
|
}
|
2022-03-13 00:32:44 +08:00
|
|
|
|
|
|
|
static Future<T?> show<T>(DialogBuilder builder,
|
2022-04-20 23:43:19 +08:00
|
|
|
{bool clickMaskDismiss = false,
|
2022-04-21 10:02:47 +08:00
|
|
|
bool backDismiss = false,
|
2022-04-19 13:07:45 +08:00
|
|
|
String? tag,
|
|
|
|
bool useAnimation = true}) async {
|
|
|
|
final t;
|
|
|
|
if (tag != null) {
|
|
|
|
t = tag;
|
|
|
|
} else {
|
|
|
|
_tag += 1;
|
|
|
|
t = _tag.toString();
|
|
|
|
}
|
|
|
|
SmartDialog.dismiss(status: SmartStatus.allToast);
|
|
|
|
SmartDialog.dismiss(status: SmartStatus.loading);
|
|
|
|
final close = ([res]) {
|
|
|
|
SmartDialog.dismiss(tag: t, result: res);
|
|
|
|
};
|
|
|
|
final res = await SmartDialog.show<T>(
|
|
|
|
tag: t,
|
2022-04-20 23:43:19 +08:00
|
|
|
clickMaskDismiss: clickMaskDismiss,
|
2022-04-21 10:02:47 +08:00
|
|
|
backDismiss: backDismiss,
|
2022-04-19 13:07:45 +08:00
|
|
|
useAnimation: useAnimation,
|
|
|
|
builder: (_) => StatefulBuilder(
|
|
|
|
builder: (_, setState) => builder(setState, close)));
|
2022-03-13 00:32:44 +08:00
|
|
|
return res;
|
|
|
|
}
|
2022-02-28 16:11:21 +08:00
|
|
|
}
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2022-03-13 00:32:44 +08:00
|
|
|
class CustomAlertDialog extends StatelessWidget {
|
|
|
|
CustomAlertDialog(
|
|
|
|
{required this.title,
|
|
|
|
required this.content,
|
|
|
|
required this.actions,
|
|
|
|
this.contentPadding});
|
2020-11-18 12:49:43 +08:00
|
|
|
|
2022-03-13 00:32:44 +08:00
|
|
|
final Widget title;
|
|
|
|
final Widget content;
|
|
|
|
final List<Widget> actions;
|
|
|
|
final double? contentPadding;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-04-21 10:02:47 +08:00
|
|
|
return AlertDialog(
|
|
|
|
scrollable: true,
|
|
|
|
title: title,
|
|
|
|
contentPadding:
|
|
|
|
EdgeInsets.symmetric(horizontal: contentPadding ?? 25, vertical: 10),
|
|
|
|
content: content,
|
|
|
|
actions: actions,
|
|
|
|
);
|
2022-03-13 00:32:44 +08:00
|
|
|
}
|
2020-11-16 22:00:09 +08:00
|
|
|
}
|
2020-11-16 22:12:32 +08:00
|
|
|
|
2022-03-13 00:32:44 +08:00
|
|
|
void msgBox(String type, String title, String text, {bool? hasCancel}) {
|
2020-11-29 01:36:10 +08:00
|
|
|
var wrap = (String text, void Function() onPressed) => ButtonTheme(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
|
2022-02-02 17:25:56 +08:00
|
|
|
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
|
|
//limits the touch area to the button area
|
|
|
|
minWidth: 0,
|
|
|
|
//wraps child's width
|
2020-11-29 01:36:10 +08:00
|
|
|
height: 0,
|
2021-08-02 20:54:56 +08:00
|
|
|
child: TextButton(
|
|
|
|
style: flatButtonStyle,
|
2020-11-29 01:36:10 +08:00
|
|
|
onPressed: onPressed,
|
2021-08-02 20:54:56 +08:00
|
|
|
child: Text(Translator.call(text),
|
|
|
|
style: TextStyle(color: MyTheme.accent))));
|
2020-11-29 01:36:10 +08:00
|
|
|
|
2022-04-19 13:07:45 +08:00
|
|
|
SmartDialog.dismiss();
|
2020-11-29 01:36:10 +08:00
|
|
|
final buttons = [
|
2021-08-02 20:54:56 +08:00
|
|
|
wrap(Translator.call('OK'), () {
|
2022-04-19 13:07:45 +08:00
|
|
|
SmartDialog.dismiss();
|
2022-03-13 23:07:52 +08:00
|
|
|
backToHome();
|
2020-11-29 01:36:10 +08:00
|
|
|
})
|
|
|
|
];
|
2020-11-24 11:25:56 +08:00
|
|
|
if (hasCancel == null) {
|
|
|
|
hasCancel = type != 'error';
|
|
|
|
}
|
2020-11-29 01:36:10 +08:00
|
|
|
if (hasCancel) {
|
|
|
|
buttons.insert(
|
2022-04-19 13:07:45 +08:00
|
|
|
0,
|
2021-08-02 20:54:56 +08:00
|
|
|
wrap(Translator.call('Cancel'), () {
|
2022-04-19 13:07:45 +08:00
|
|
|
SmartDialog.dismiss();
|
2020-11-29 01:36:10 +08:00
|
|
|
}));
|
|
|
|
}
|
2022-04-19 13:07:45 +08:00
|
|
|
DialogManager.show((setState, close) => CustomAlertDialog(
|
|
|
|
title: Text(translate(title), style: TextStyle(fontSize: 21)),
|
|
|
|
content: Text(Translator.call(text), style: TextStyle(fontSize: 15)),
|
|
|
|
actions: buttons));
|
2020-11-21 14:40:28 +08:00
|
|
|
}
|
2020-11-25 18:33:09 +08:00
|
|
|
|
|
|
|
Color str2color(String str, [alpha = 0xFF]) {
|
|
|
|
var hash = 160 << 16 + 114 << 8 + 91;
|
|
|
|
for (var i = 0; i < str.length; i += 1) {
|
|
|
|
hash = str.codeUnitAt(i) + ((hash << 5) - hash);
|
|
|
|
}
|
2021-08-14 14:14:01 +08:00
|
|
|
hash = hash % 16777216;
|
|
|
|
return Color((hash & 0xFF7FFF) | (alpha << 24));
|
2020-11-25 18:33:09 +08:00
|
|
|
}
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2022-03-17 21:03:52 +08:00
|
|
|
const K = 1024;
|
|
|
|
const M = K * K;
|
|
|
|
const G = M * K;
|
|
|
|
|
|
|
|
String readableFileSize(double size) {
|
|
|
|
if (size < K) {
|
|
|
|
return size.toString() + " B";
|
|
|
|
} else if (size < M) {
|
|
|
|
return (size / K).toStringAsFixed(2) + " KB";
|
|
|
|
} else if (size < G) {
|
|
|
|
return (size / M).toStringAsFixed(2) + " MB";
|
|
|
|
} else {
|
|
|
|
return (size / G).toStringAsFixed(2) + " GB";
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 15:37:47 +08:00
|
|
|
|
|
|
|
/// Flutter can't not catch PointerMoveEvent when size is 1
|
|
|
|
/// This will happen in Android AccessibilityService Input
|
|
|
|
/// android can't init dispatching size yet ,see: https://stackoverflow.com/questions/59960451/android-accessibility-dispatchgesture-is-it-possible-to-specify-pressure-for-a
|
|
|
|
/// use this temporary solution until flutter or android fixes the bug
|
|
|
|
class AccessibilityListener extends StatelessWidget {
|
|
|
|
final Widget? child;
|
|
|
|
static final offset = 100;
|
|
|
|
|
|
|
|
AccessibilityListener({this.child});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Listener(
|
|
|
|
onPointerDown: (evt) {
|
2022-05-26 18:25:16 +08:00
|
|
|
if (evt.size == 1) {
|
|
|
|
GestureBinding.instance.handlePointerEvent(PointerAddedEvent(
|
2022-04-14 15:37:47 +08:00
|
|
|
pointer: evt.pointer + offset, position: evt.position));
|
2022-05-26 18:25:16 +08:00
|
|
|
GestureBinding.instance.handlePointerEvent(PointerDownEvent(
|
2022-04-14 15:37:47 +08:00
|
|
|
pointer: evt.pointer + offset,
|
|
|
|
size: 0.1,
|
|
|
|
position: evt.position));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onPointerUp: (evt) {
|
2022-06-20 00:15:37 +08:00
|
|
|
if (evt.size == 1) {
|
2022-05-26 18:25:16 +08:00
|
|
|
GestureBinding.instance.handlePointerEvent(PointerUpEvent(
|
2022-04-14 15:37:47 +08:00
|
|
|
pointer: evt.pointer + offset,
|
|
|
|
size: 0.1,
|
|
|
|
position: evt.position));
|
2022-05-26 18:25:16 +08:00
|
|
|
GestureBinding.instance.handlePointerEvent(PointerRemovedEvent(
|
2022-04-15 17:45:48 +08:00
|
|
|
pointer: evt.pointer + offset, position: evt.position));
|
2022-04-14 15:37:47 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onPointerMove: (evt) {
|
2022-06-20 00:15:37 +08:00
|
|
|
if (evt.size == 1) {
|
2022-05-26 18:25:16 +08:00
|
|
|
GestureBinding.instance.handlePointerEvent(PointerMoveEvent(
|
2022-04-14 15:37:47 +08:00
|
|
|
pointer: evt.pointer + offset,
|
|
|
|
size: 0.1,
|
|
|
|
delta: evt.delta,
|
|
|
|
position: evt.position));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: child);
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 22:37:47 +08:00
|
|
|
|
|
|
|
class PermissionManager {
|
|
|
|
static Completer<bool>? _completer;
|
|
|
|
static Timer? _timer;
|
|
|
|
static var _current = "";
|
|
|
|
|
2022-07-16 22:31:44 +08:00
|
|
|
static final permissions = [
|
|
|
|
"audio",
|
|
|
|
"file",
|
|
|
|
"ignore_battery_optimizations",
|
|
|
|
"application_details_settings"
|
|
|
|
];
|
2022-04-20 22:37:47 +08:00
|
|
|
|
|
|
|
static bool isWaitingFile() {
|
|
|
|
if (_completer != null) {
|
|
|
|
return !_completer!.isCompleted && _current == "file";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<bool> check(String type) {
|
|
|
|
if (!permissions.contains(type))
|
|
|
|
return Future.error("Wrong permission!$type");
|
|
|
|
return FFI.invokeMethod("check_permission", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<bool> request(String type) {
|
|
|
|
if (!permissions.contains(type))
|
|
|
|
return Future.error("Wrong permission!$type");
|
|
|
|
|
2022-07-14 17:44:37 +08:00
|
|
|
FFI.invokeMethod("request_permission", type);
|
|
|
|
if (type == "ignore_battery_optimizations") {
|
|
|
|
return Future.value(false);
|
|
|
|
}
|
2022-04-20 22:37:47 +08:00
|
|
|
_current = type;
|
|
|
|
_completer = Completer<bool>();
|
|
|
|
|
|
|
|
// timeout
|
|
|
|
_timer?.cancel();
|
|
|
|
_timer = Timer(Duration(seconds: 60), () {
|
|
|
|
if (_completer == null) return;
|
|
|
|
if (!_completer!.isCompleted) {
|
|
|
|
_completer!.complete(false);
|
|
|
|
}
|
|
|
|
_completer = null;
|
|
|
|
_current = "";
|
|
|
|
});
|
|
|
|
return _completer!.future;
|
|
|
|
}
|
|
|
|
|
|
|
|
static complete(String type, bool res) {
|
|
|
|
if (type != _current) {
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
_timer?.cancel();
|
|
|
|
_completer?.complete(res);
|
|
|
|
_current = "";
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 18:34:25 +08:00
|
|
|
|
|
|
|
RadioListTile<T> getRadio<T>(
|
|
|
|
String name, T toValue, T curValue, void Function(T?) onChange) {
|
|
|
|
return RadioListTile<T>(
|
|
|
|
controlAffinity: ListTileControlAffinity.trailing,
|
|
|
|
title: Text(translate(name)),
|
|
|
|
value: toValue,
|
|
|
|
groupValue: curValue,
|
|
|
|
onChanged: onChange,
|
|
|
|
dense: true,
|
|
|
|
);
|
|
|
|
}
|