git-touch-android-ios-app/lib/screens/login.dart

198 lines
7.1 KiB
Dart
Raw Normal View History

2019-10-03 06:55:17 +02:00
import 'package:flutter/cupertino.dart';
2019-02-07 07:35:19 +01:00
import 'package:flutter/material.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2019-10-03 06:55:17 +02:00
import 'package:git_touch/models/theme.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/single.dart';
2019-10-02 09:39:47 +02:00
import 'package:git_touch/utils/utils.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
import '../widgets/link.dart';
import '../widgets/loading.dart';
import '../widgets/avatar.dart';
// import 'login_gitlab.dart';
2019-02-07 07:35:19 +01:00
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
2019-10-03 06:55:17 +02:00
String _token = '';
2019-10-06 13:20:38 +02:00
String _gitlabToken = '';
String _gitlabDomain = 'https://gitlab.com';
2019-10-03 06:55:17 +02:00
2019-09-26 16:14:14 +02:00
Widget _buildAccountItem(int index) {
2019-11-08 11:29:08 +01:00
final theme = Provider.of<ThemeModel>(context);
final auth = Provider.of<AuthModel>(context);
final account = auth.accounts[index];
2019-02-21 14:21:16 +01:00
return Link(
onTap: () {
// Navigator.of(context).pop();
2019-11-08 11:29:08 +01:00
auth.setActiveAccountIndex(index);
2019-02-21 14:21:16 +01:00
},
child: Container(
2019-10-02 10:09:54 +02:00
padding: CommonStyle.padding,
2019-02-21 14:21:16 +01:00
decoration: BoxDecoration(
2019-11-08 11:29:08 +01:00
border: Border(bottom: BorderSide(color: theme.palette.border)),
2019-02-21 14:21:16 +01:00
),
2019-09-26 16:14:14 +02:00
child: Row(
children: <Widget>[
2019-10-02 09:23:33 +02:00
Avatar.large(url: account.avatarUrl),
2019-09-26 16:14:14 +02:00
Padding(padding: EdgeInsets.only(left: 10)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(account.login, style: TextStyle(fontSize: 20)),
Padding(padding: EdgeInsets.only(top: 6)),
Text(account.domain)
],
),
2019-02-21 14:21:16 +01:00
),
2019-11-08 11:29:08 +01:00
(index == auth.activeAccountIndex)
2019-09-26 16:14:14 +02:00
? Icon(Icons.check)
: Container(),
],
),
2019-02-21 14:21:16 +01:00
),
);
}
2019-09-24 15:04:30 +02:00
Widget _buildAddItem({String text, Function onTap}) {
2019-11-08 11:29:08 +01:00
final theme = Provider.of<ThemeModel>(context);
return Link(
child: Container(
padding: EdgeInsets.symmetric(vertical: 20),
decoration: BoxDecoration(
2019-11-08 11:29:08 +01:00
border: Border(bottom: BorderSide(color: theme.palette.border)),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.add),
Text(text, style: TextStyle(fontSize: 16)),
],
),
),
onTap: onTap,
);
}
2019-02-07 07:35:19 +01:00
@override
Widget build(BuildContext context) {
2019-10-03 06:55:17 +02:00
final auth = Provider.of<AuthModel>(context);
2019-11-08 11:29:08 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-09-25 11:06:36 +02:00
return SingleScaffold(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('Select account'),
2019-10-03 06:55:17 +02:00
body: auth.loading
2019-09-25 08:24:20 +02:00
? Center(child: Loading())
: Container(
child: Column(
children: [
2019-10-03 06:55:17 +02:00
...List.generate(auth.accounts.length, _buildAccountItem),
2019-09-25 08:24:20 +02:00
_buildAddItem(
2019-10-03 06:55:17 +02:00
text: 'GitHub Account by OAuth',
onTap: auth.redirectToGithubOauth,
2019-09-25 08:24:20 +02:00
),
2019-10-03 06:55:17 +02:00
_buildAddItem(
text: 'GitHub Account by Token',
onTap: () async {
var result =
await Provider.of<ThemeModel>(context).showConfirm(
context,
Column(
children: <Widget>[
CupertinoTextField(
placeholder: 'Access token',
onChanged: (v) {
setState(() {
_token = v;
});
},
),
SizedBox(height: 8),
Text(
'GitTouch needs these permissions',
style: TextStyle(
fontSize: 14, fontWeight: FontWeight.w400),
),
SizedBox(height: 8),
Text(
'user, repo, read:org',
style: TextStyle(
2019-11-08 11:29:08 +01:00
fontSize: 16, color: theme.palette.primary),
2019-10-03 06:55:17 +02:00
)
],
),
);
if (result == true) {
try {
await auth.loginWithToken(_token);
} catch (err) {
Provider.of<ThemeModel>(context).showConfirm(
context, Text('Token invalid: $err'));
}
}
},
2019-10-06 10:13:56 +02:00
),
_buildAddItem(
text: 'GitLab Account by Token',
onTap: () async {
var result =
await Provider.of<ThemeModel>(context).showConfirm(
context,
Column(
children: <Widget>[
2019-10-06 13:20:38 +02:00
CupertinoTextField(
placeholder: 'Domain',
onChanged: (v) {
setState(() {
_gitlabDomain = v;
});
},
),
SizedBox(height: 8),
2019-10-06 10:13:56 +02:00
CupertinoTextField(
placeholder: 'Access token',
onChanged: (v) {
setState(() {
2019-10-06 13:20:38 +02:00
_gitlabToken = v;
2019-10-06 10:13:56 +02:00
});
},
),
SizedBox(height: 8),
Text(
'GitTouch needs these permissions',
style: TextStyle(
fontSize: 14, fontWeight: FontWeight.w400),
),
SizedBox(height: 8),
Text(
'api, read_user, read_repository',
style: TextStyle(
2019-11-08 11:29:08 +01:00
fontSize: 16, color: theme.palette.primary),
2019-10-06 10:13:56 +02:00
)
],
),
);
if (result == true) {
try {
2019-10-06 13:20:38 +02:00
await auth.loginToGitlab(_gitlabDomain, _gitlabToken);
2019-10-06 10:13:56 +02:00
// TODO: Custom domain
} catch (err) {
Provider.of<ThemeModel>(context).showConfirm(
context, Text('Token invalid: $err'));
}
}
},
2019-10-03 06:55:17 +02:00
)
2019-09-25 08:24:20 +02:00
],
2019-05-12 08:01:12 +02:00
),
2019-09-25 08:24:20 +02:00
),
2019-02-07 07:35:19 +01:00
);
}
}