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

197 lines
4.7 KiB
Dart
Raw Normal View History

2019-09-14 11:19:33 +02:00
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:git_touch/models/theme.dart';
2019-09-23 12:28:33 +02:00
import 'package:git_touch/screens/repository.dart';
2019-09-07 11:48:59 +02:00
import 'package:git_touch/screens/user.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-09-25 07:34:13 +02:00
export 'package:flutter_vector_icons/flutter_vector_icons.dart';
2019-09-14 11:19:33 +02:00
final monospaceFont = Platform.isIOS ? 'Menlo' : 'monospace'; // FIXME:
2019-03-10 09:09:26 +01:00
class StorageKeys {
2019-09-26 16:14:14 +02:00
static const accounts = 'accounts';
2019-03-10 09:09:26 +01:00
static const account = 'account';
static const github = 'github';
static const theme = 'theme';
static const newsFilter = 'news.filter';
}
2019-02-04 11:32:39 +01:00
Color convertColor(String cssHex) {
2019-09-02 12:14:17 +02:00
if (cssHex == null) {
return Color(0xffcccccc); // Default color
}
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('');
}
2019-02-04 11:32:39 +01:00
return Color(int.parse('ff' + cssHex, radix: 16));
}
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;
// print('color: $color, $grayscale');
var showWhite = grayscale < 128;
return showWhite ? Colors.white : Colors.black;
}
2019-02-10 05:16:52 +01:00
void nextTick(Function callback, [int milliseconds = 0]) {
2019-02-08 16:20:28 +01:00
// FIXME:
2019-02-10 05:16:52 +01:00
Future.delayed(Duration(milliseconds: 0)).then((_) {
callback();
});
}
TextSpan createLinkSpan(
BuildContext context,
String text,
Widget Function(BuildContext) builder,
) {
return TextSpan(
text: text,
2019-09-24 13:58:34 +02:00
style: TextStyle(
color: PrimerColors.blue500,
fontWeight: FontWeight.w600,
),
2019-02-11 17:31:06 +01:00
recognizer: TapGestureRecognizer()
..onTap = () {
Provider.of<ThemeModel>(context).pushRoute(context, builder);
2019-02-11 17:31:06 +01:00
},
);
}
2019-09-07 11:48:59 +02:00
TextSpan createUserSpan(BuildContext context, String login) {
return createLinkSpan(context, login, (_) => UserScreen(login));
2019-09-07 11:48:59 +02:00
}
TextSpan createRepoLinkSpan(BuildContext context, String owner, String name) {
2019-09-23 12:28:33 +02:00
return createLinkSpan(
context, '$owner/$name', (_) => RepositoryScreen(owner, name));
}
class Palette {
2019-02-06 12:14:11 +01:00
static const green = Color(0xff2cbe4e);
}
// 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-05 11:58:14 +02:00
class BorderView extends StatelessWidget {
final double height;
2019-09-14 17:48:01 +02:00
final Color color;
final double leftPadding;
2019-09-05 11:58:14 +02:00
2019-09-14 17:48:01 +02:00
const BorderView({
this.height = 1,
2019-09-14 18:01:15 +02:00
this.color = PrimerColors.gray100,
2019-09-14 17:48:01 +02:00
this.leftPadding = 0,
});
2019-09-05 11:58:14 +02:00
@override
Widget build(BuildContext context) {
2019-09-14 17:48:01 +02:00
return Row(
children: <Widget>[
2019-09-15 09:08:09 +02:00
SizedBox(
width: leftPadding,
height: 1,
child: DecoratedBox(
decoration: BoxDecoration(color: PrimerColors.white),
),
),
2019-09-14 17:48:01 +02:00
Expanded(
child: SizedBox(
height: height,
child: DecoratedBox(
decoration: BoxDecoration(color: color),
),
),
),
],
2019-09-05 11:58:14 +02:00
);
}
}
2019-09-14 17:48:01 +02:00
const borderView = BorderView();
2019-09-14 20:05:34 +02:00
const borderView1 = BorderView(height: 20, color: PrimerColors.gray100);
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) {
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(
color: PrimerColors.blue500,
fontSize: 12,
height: 1,
fontFamily: monospaceFont,
),
),
);
}
}