git-touch-android-ios-app/lib/utils/utils.dart

189 lines
4.7 KiB
Dart
Raw Normal View History

import 'package:universal_io/io.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:git_touch/models/theme.dart';
2019-10-02 08:58:11 +02:00
import 'package:git_touch/widgets/border_view.dart';
import 'package:intl/intl.dart';
2019-08-31 15:37:29 +02:00
import 'package:primer/primer.dart';
import 'package:provider/provider.dart';
2019-11-02 16:56:10 +01:00
import 'package:tuple/tuple.dart';
2019-09-28 18:25:14 +02:00
import 'package:url_launcher/url_launcher.dart';
2019-12-12 14:06:12 +01:00
export 'extensions.dart';
2019-09-25 07:34:13 +02:00
export 'package:flutter_vector_icons/flutter_vector_icons.dart';
2019-03-10 09:09:26 +01:00
class StorageKeys {
2020-01-16 05:45:04 +01:00
@deprecated
2019-03-10 09:09:26 +01:00
static const account = 'account';
2020-01-16 05:45:04 +01:00
@deprecated
2019-03-10 09:09:26 +01:00
static const github = 'github';
2020-01-16 05:45:04 +01:00
static const accounts = 'accounts';
2020-01-16 05:52:47 +01:00
static const iTheme = 'theme';
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';
static const markdown = 'markdown';
2020-05-01 12:05:49 +02:00
static getDefaultStartTabKey(String platform) =>
'default-start-tab-$platform';
2019-03-10 09:09:26 +01:00
}
2019-10-02 10:09:54 +02:00
class CommonStyle {
2020-01-11 10:57:22 +01:00
static const padding = EdgeInsets.symmetric(horizontal: 16, vertical: 12);
2019-11-05 14:22:41 +01:00
static final border = BorderView();
2019-10-02 10:09:54 +02:00
static const verticalGap = SizedBox(height: 18);
static final monospace = Platform.isIOS ? 'Menlo' : 'monospace'; // FIXME:
}
2019-02-04 11:32:39 +01:00
Color convertColor(String cssHex) {
2019-09-02 12:14:17 +02:00
if (cssHex == null) {
2020-02-08 07:22:20 +01:00
return Color(0xffededed); // Default color
2019-09-02 12:14:17 +02:00
}
2019-02-04 11:32:39 +01:00
if (cssHex.startsWith('#')) {
cssHex = cssHex.substring(1);
}
2019-03-12 12:00:31 +01:00
if (cssHex.length == 3) {
cssHex = cssHex.split('').map((char) => char + char).join('');
}
2020-10-29 14:02:45 +01:00
return Color(int.tryParse('ff' + cssHex, radix: 16) ?? 0);
2019-02-04 11:32:39 +01:00
}
2019-09-14 09:39:38 +02:00
Color getFontColorByBrightness(Color color) {
var grayscale = color.red * 0.3 + color.green * 0.59 + color.blue * 0.11;
// Fimber.d('color: $color, $grayscale');
2019-09-14 09:39:38 +02:00
var showWhite = grayscale < 128;
return showWhite ? Colors.white : Colors.black;
}
TextSpan createLinkSpan(
BuildContext context,
String text,
2019-12-12 13:29:56 +01:00
String url,
) {
2019-12-21 10:26:26 +01:00
final theme = Provider.of<ThemeModel>(context);
return TextSpan(
text: text,
2019-09-24 13:58:34 +02:00
style: TextStyle(
2020-01-27 08:11:51 +01:00
color: theme.palette.primary,
2019-09-24 13:58:34 +02:00
fontWeight: FontWeight.w600,
),
2019-02-11 17:31:06 +01:00
recognizer: TapGestureRecognizer()
..onTap = () {
2019-12-21 10:26:26 +01:00
theme.push(context, url);
2019-02-11 17:31:06 +01:00
},
);
}
2019-11-02 16:56:10 +01:00
Tuple2<String, String> parseRepositoryFullName(String fullName) {
final ls = fullName.split('/');
assert(ls.length == 2);
return Tuple2(ls[0], ls[1]);
}
2019-11-05 14:22:41 +01:00
class GithubPalette {
2019-11-08 13:23:09 +01:00
static const open = Color(0xff2cbe4e);
static const closed = PrimerColors.red600;
static const merged = PrimerColors.purple500;
}
// final pageSize = 5;
2019-02-09 07:20:21 +01:00
final pageSize = 30;
2019-01-29 06:34:52 +01:00
var createWarning =
(String text) => Text(text, style: TextStyle(color: Colors.redAccent));
var warningSpan =
TextSpan(text: 'xxx', style: TextStyle(color: Colors.redAccent));
2019-02-09 06:36:15 +01:00
List<T> join<T>(T seperator, List<T> xs) {
List<T> result = [];
xs.asMap().forEach((index, x) {
if (x == null) return;
result.add(x);
if (index < xs.length - 1) {
result.add(seperator);
}
});
return result;
}
List<T> joinAll<T>(T seperator, List<List<T>> xss) {
List<T> result = [];
xss.asMap().forEach((index, x) {
if (x == null || x.isEmpty) return;
result.addAll(x);
if (index < xss.length - 1) {
result.add(seperator);
}
});
return result;
}
final numberFormat = NumberFormat();
2019-09-14 17:48:01 +02:00
bool isNotNullOrEmpty(String text) {
return text != null && text.isNotEmpty;
}
2019-09-23 14:04:53 +02:00
String getBranchQueryKey(String branch, {bool withParams = false}) {
if (branch == null) return 'defaultBranchRef';
return 'ref' + (withParams ? '(qualifiedName: "$branch")' : '');
}
2019-09-25 07:34:13 +02:00
// TODO: Primer
class PrimerBranchName extends StatelessWidget {
final String name;
PrimerBranchName(this.name);
static const branchBgColor = Color(0xffeaf5ff);
@override
Widget build(BuildContext context) {
2019-12-21 10:26:26 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-09-25 07:34:13 +02:00
return Container(
padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
height: 16,
decoration: BoxDecoration(
color: branchBgColor,
borderRadius: BorderRadius.all(Radius.circular(3)),
),
child: Text(
name,
style: TextStyle(
2020-01-27 08:11:51 +01:00
color: theme.palette.primary,
fontSize: 14,
2019-09-25 07:34:13 +02:00
height: 1,
2019-10-02 10:09:54 +02:00
fontFamily: CommonStyle.monospace,
2019-09-25 07:34:13 +02:00
),
),
);
}
}
2019-09-29 07:32:53 +02:00
launchUrl(String url) async {
if (url == null) return;
if (await canLaunch(url)) {
await launch(url);
} else {
// TODO: fallback
}
}
2019-12-13 06:13:45 +01:00
2020-01-01 08:58:49 +01:00
final dateFormat = DateFormat.yMMMMd();
2020-01-29 11:50:17 +01:00
2020-01-30 12:24:59 +01: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;
}