2022-09-25 02:46:37 +08:00
|
|
|
import 'package:antd_mobile/antd_mobile.dart';
|
2019-01-29 19:41:24 +08:00
|
|
|
import 'package:flutter/gestures.dart';
|
2022-09-17 20:35:45 +08:00
|
|
|
import 'package:flutter/widgets.dart';
|
2019-10-02 14:58:11 +08:00
|
|
|
import 'package:git_touch/widgets/border_view.dart';
|
2022-09-23 01:50:45 +08:00
|
|
|
import 'package:go_router/go_router.dart';
|
2019-09-13 23:34:19 +08:00
|
|
|
import 'package:intl/intl.dart';
|
2019-08-31 21:37:29 +08:00
|
|
|
import 'package:primer/primer.dart';
|
2019-11-02 23:56:10 +08:00
|
|
|
import 'package:tuple/tuple.dart';
|
2022-09-12 22:59:17 +08:00
|
|
|
import 'package:universal_io/io.dart';
|
2019-09-29 00:25:14 +08:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2022-09-12 22:59:17 +08:00
|
|
|
|
|
|
|
export 'extensions.dart';
|
|
|
|
|
2019-03-10 16:09:26 +08:00
|
|
|
class StorageKeys {
|
2022-10-03 18:09:41 +08:00
|
|
|
@Deprecated('Use `accounts` instead')
|
2019-03-10 16:09:26 +08:00
|
|
|
static const account = 'account';
|
2022-10-03 18:09:41 +08:00
|
|
|
@Deprecated('Use `accounts` instead')
|
2019-03-10 16:09:26 +08:00
|
|
|
static const github = 'github';
|
2022-10-03 18:09:41 +08:00
|
|
|
@Deprecated('Split into several keys')
|
2020-01-16 12:52:47 +08:00
|
|
|
static const iTheme = 'theme';
|
2022-09-17 21:57:43 +08:00
|
|
|
|
|
|
|
static const accounts = 'accounts';
|
2020-01-16 12:52:47 +08:00
|
|
|
static const iBrightness = 'brightness';
|
|
|
|
static const codeTheme = 'code-theme';
|
|
|
|
static const codeThemeDark = 'code-theme-dark';
|
|
|
|
static const iCodeFontSize = 'code-font-size';
|
|
|
|
static const codeFontFamily = 'code-font-family';
|
2021-02-14 10:27:34 +08:00
|
|
|
static const iMarkdown = 'markdown';
|
|
|
|
static const iDefaultAccount = 'default-account';
|
2021-02-14 09:35:41 +05:30
|
|
|
static const locale = 'locale';
|
2020-05-01 18:05:49 +08:00
|
|
|
|
|
|
|
static getDefaultStartTabKey(String platform) =>
|
|
|
|
'default-start-tab-$platform';
|
2019-03-10 16:09:26 +08:00
|
|
|
}
|
|
|
|
|
2019-10-02 16:09:54 +08:00
|
|
|
class CommonStyle {
|
2022-10-04 20:18:04 +08:00
|
|
|
CommonStyle._();
|
2022-10-03 13:27:11 +08:00
|
|
|
static const padding = EdgeInsets.all(12);
|
2022-09-07 00:28:12 +08:00
|
|
|
static const border = BorderView();
|
2019-10-02 16:09:54 +08:00
|
|
|
static const verticalGap = SizedBox(height: 18);
|
|
|
|
static final monospace = Platform.isIOS ? 'Menlo' : 'monospace'; // FIXME:
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:33:27 +08:00
|
|
|
TextSpan createLinkSpan(
|
|
|
|
BuildContext context,
|
2021-05-16 15:16:35 +08:00
|
|
|
String? text,
|
2019-12-12 20:29:56 +08:00
|
|
|
String url,
|
2019-09-23 20:33:27 +08:00
|
|
|
) {
|
2019-01-29 19:41:24 +08:00
|
|
|
return TextSpan(
|
|
|
|
text: text,
|
2019-09-24 19:58:34 +08:00
|
|
|
style: TextStyle(
|
2022-09-25 02:46:37 +08:00
|
|
|
color: AntTheme.of(context).colorPrimary,
|
2019-09-24 19:58:34 +08:00
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
),
|
2019-02-12 00:31:06 +08:00
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = () {
|
2022-09-23 01:50:45 +08:00
|
|
|
context.pushUrl(url);
|
2019-02-12 00:31:06 +08:00
|
|
|
},
|
2019-01-29 19:41:24 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-02 23:56:10 +08:00
|
|
|
Tuple2<String, String> parseRepositoryFullName(String fullName) {
|
|
|
|
final ls = fullName.split('/');
|
|
|
|
assert(ls.length == 2);
|
|
|
|
return Tuple2(ls[0], ls[1]);
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:22:41 +08:00
|
|
|
class GithubPalette {
|
2019-11-08 20:23:09 +08:00
|
|
|
static const open = Color(0xff2cbe4e);
|
|
|
|
static const closed = PrimerColors.red600;
|
|
|
|
static const merged = PrimerColors.purple500;
|
2019-01-28 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
2019-02-19 15:25:15 +08:00
|
|
|
// final pageSize = 5;
|
2022-10-04 01:05:29 +08:00
|
|
|
const kPageSize = 30;
|
2019-01-28 21:32:01 +08:00
|
|
|
|
2022-10-08 00:46:34 +08:00
|
|
|
extension MyList on List {
|
|
|
|
List<T> withSeparator<T>(T separator) {
|
|
|
|
final result = <T>[];
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (i != 0) {
|
|
|
|
result.add(separator);
|
|
|
|
}
|
|
|
|
result.add(this[i]);
|
2019-09-03 21:39:00 +08:00
|
|
|
}
|
2022-10-08 00:46:34 +08:00
|
|
|
return result;
|
|
|
|
}
|
2019-09-03 21:39:00 +08:00
|
|
|
}
|
|
|
|
|
2019-09-13 23:34:19 +08:00
|
|
|
final numberFormat = NumberFormat();
|
|
|
|
|
2022-09-21 02:00:03 +08:00
|
|
|
Future<void> launchStringUrl(String? url) async {
|
2019-09-29 13:32:53 +08:00
|
|
|
if (url == null) return;
|
|
|
|
|
2022-09-07 00:40:53 +08:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
if (await canLaunchUrl(uri)) {
|
|
|
|
await launchUrl(uri);
|
2019-09-29 13:32:53 +08:00
|
|
|
} else {
|
|
|
|
// TODO: fallback
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 13:13:45 +08:00
|
|
|
|
2020-01-01 15:58:49 +08:00
|
|
|
final dateFormat = DateFormat.yMMMMd();
|
2020-01-29 18:50:17 +08:00
|
|
|
|
2020-01-30 19:24:59 +08:00
|
|
|
int sortByKey<T>(T key, T a, T b) {
|
|
|
|
if (a == key && b != key) return -1;
|
|
|
|
if (a != key && b == key) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-31 00:41:51 +08:00
|
|
|
|
2022-10-04 01:05:29 +08:00
|
|
|
const kTotalCountFallback = 999; // TODO:
|
2021-06-14 02:13:11 +08:00
|
|
|
|
|
|
|
class ListPayload<T, K> {
|
|
|
|
ListPayload({
|
|
|
|
required this.cursor,
|
|
|
|
required this.hasMore,
|
2021-06-14 11:28:12 +08:00
|
|
|
required this.items,
|
2021-06-14 02:13:11 +08:00
|
|
|
});
|
2022-09-22 00:28:21 +08:00
|
|
|
K cursor;
|
|
|
|
bool hasMore;
|
|
|
|
Iterable<T> items;
|
2021-06-14 02:13:11 +08:00
|
|
|
}
|
2022-09-23 01:50:45 +08:00
|
|
|
|
|
|
|
extension MyHelper on BuildContext {
|
2022-09-24 02:22:17 +08:00
|
|
|
Future<void> pushUrl(String url, {bool replace = false}) async {
|
2022-09-23 01:50:45 +08:00
|
|
|
// Fimber.d(url);
|
|
|
|
if (url.startsWith('/')) {
|
|
|
|
if (replace) {
|
|
|
|
this.replace(url);
|
|
|
|
} else {
|
|
|
|
push(url);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
launchStringUrl(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|