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';
|
2020-02-01 10:00:30 +01:00
|
|
|
import 'package:git_touch/widgets/action_button.dart';
|
2019-09-11 13:59:47 +02:00
|
|
|
import 'package:git_touch/widgets/app_bar_title.dart';
|
2020-02-01 10:18:21 +01:00
|
|
|
import 'package:git_touch/widgets/text_field.dart';
|
2019-09-08 14:07:35 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2019-02-08 12:34:07 +01:00
|
|
|
import '../widgets/link.dart';
|
2019-02-10 06:17:25 +01:00
|
|
|
import '../widgets/loading.dart';
|
2019-03-09 10:03:33 +01:00
|
|
|
import '../widgets/avatar.dart';
|
2019-12-13 06:13:45 +01:00
|
|
|
|
2019-02-07 07:35:19 +01:00
|
|
|
class LoginScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_LoginScreenState createState() => _LoginScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
2020-02-01 10:00:30 +01:00
|
|
|
final _tokenController = TextEditingController();
|
|
|
|
final _domainController = TextEditingController();
|
|
|
|
|
2020-02-02 07:08:58 +01:00
|
|
|
// For Bitbucket
|
|
|
|
final _usernameController = TextEditingController();
|
|
|
|
final _passwordController = TextEditingController();
|
|
|
|
|
2020-02-01 10:00:30 +01:00
|
|
|
// @override
|
|
|
|
// initState() {
|
|
|
|
// super.initState();
|
|
|
|
// _tokenController.addListener(() {
|
|
|
|
// print(_tokenController.text);
|
|
|
|
// });
|
|
|
|
// }
|
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: () {
|
2020-02-01 07:44:15 +01:00
|
|
|
auth.setActiveAccountAndReload(index);
|
2019-02-21 14:21:16 +01:00
|
|
|
},
|
2020-02-01 11:30:32 +01:00
|
|
|
onLongPress: () {
|
|
|
|
theme.showActions(context, [
|
|
|
|
ActionItem(
|
|
|
|
text: 'Remove account',
|
|
|
|
isDestructiveAction: true,
|
|
|
|
onTap: (_) {
|
|
|
|
auth.removeAccount(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(
|
2020-01-27 08:11:51 +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-12-22 04:11:13 +01:00
|
|
|
Avatar(url: account.avatarUrl, size: AvatarSize.large),
|
2019-09-26 16:14:14 +02:00
|
|
|
Padding(padding: EdgeInsets.only(left: 10)),
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
2020-02-01 11:30:32 +01:00
|
|
|
Text(
|
|
|
|
account.login,
|
|
|
|
style: TextStyle(fontSize: 20, color: theme.palette.text),
|
|
|
|
),
|
2019-09-26 16:14:14 +02:00
|
|
|
Padding(padding: EdgeInsets.only(top: 6)),
|
2020-02-01 11:30:32 +01:00
|
|
|
Text(
|
|
|
|
account.domain,
|
|
|
|
style: TextStyle(color: theme.palette.secondaryText),
|
|
|
|
)
|
2019-09-26 16:14:14 +02:00
|
|
|
],
|
|
|
|
),
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-01 10:00:30 +01:00
|
|
|
Widget _buildAddItem({IconData brand, String text, Function onTap}) {
|
2019-11-08 11:29:08 +01:00
|
|
|
final theme = Provider.of<ThemeModel>(context);
|
2019-02-21 11:43:41 +01:00
|
|
|
return Link(
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 20),
|
|
|
|
decoration: BoxDecoration(
|
2020-01-27 08:11:51 +01:00
|
|
|
border: Border(bottom: BorderSide(color: theme.palette.border)),
|
2019-02-21 11:43:41 +01:00
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(Icons.add),
|
2020-02-01 10:00:30 +01:00
|
|
|
SizedBox(width: 4),
|
|
|
|
Icon(brand),
|
|
|
|
SizedBox(width: 8),
|
2019-02-21 11:43:41 +01:00
|
|
|
Text(text, style: TextStyle(fontSize: 16)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onTap: onTap,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-01 10:00:30 +01:00
|
|
|
Widget _buildPopup(
|
|
|
|
BuildContext context, {
|
|
|
|
List<Widget> notes,
|
|
|
|
bool showDomain = false,
|
|
|
|
}) {
|
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
if (showDomain)
|
2020-02-02 07:08:58 +01:00
|
|
|
MyTextField(controller: _domainController, placeholder: 'Domain'),
|
2020-02-01 10:00:30 +01:00
|
|
|
SizedBox(height: 8),
|
2020-02-02 07:08:58 +01:00
|
|
|
MyTextField(placeholder: 'Access token', controller: _tokenController),
|
2020-02-01 10:00:30 +01:00
|
|
|
SizedBox(height: 8),
|
|
|
|
if (notes != null) ...notes,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void showError(err) {
|
|
|
|
final theme = Provider.of<ThemeModel>(context);
|
|
|
|
theme.showConfirm(context, Text('Something bad happens: $err'));
|
|
|
|
}
|
|
|
|
|
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(
|
2020-02-01 10:00:30 +01:00
|
|
|
text: 'GitHub Account',
|
|
|
|
brand: FontAwesome5Brands.github,
|
|
|
|
onTap: () async {
|
|
|
|
theme.showActions(context, [
|
|
|
|
ActionItem(
|
|
|
|
text: 'via OAuth',
|
|
|
|
onTap: (_) {
|
|
|
|
auth.redirectToGithubOauth();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ActionItem(
|
|
|
|
text: 'via Personal token',
|
|
|
|
onTap: (_) async {
|
|
|
|
final result = await theme.showConfirm(
|
|
|
|
context,
|
|
|
|
_buildPopup(context, notes: [
|
|
|
|
Text(
|
|
|
|
'GitTouch needs these permissions',
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: FontWeight.w400),
|
|
|
|
),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
Text(
|
|
|
|
'user, repo, read:org, notifications',
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
color: theme.palette.primary),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
if (result == true) {
|
|
|
|
try {
|
|
|
|
await auth
|
|
|
|
.loginWithToken(_tokenController.text);
|
|
|
|
_tokenController.clear();
|
|
|
|
} catch (err) {
|
|
|
|
showError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
},
|
2019-09-25 08:24:20 +02:00
|
|
|
),
|
2019-10-03 06:55:17 +02:00
|
|
|
_buildAddItem(
|
2020-02-01 10:00:30 +01:00
|
|
|
text: 'GitLab Account',
|
|
|
|
brand: FontAwesome5Brands.gitlab,
|
2019-10-03 06:55:17 +02:00
|
|
|
onTap: () async {
|
2020-02-01 10:00:30 +01:00
|
|
|
_domainController.text = 'https://gitlab.com';
|
|
|
|
final result = await theme.showConfirm(
|
2019-10-03 06:55:17 +02:00
|
|
|
context,
|
2020-02-01 10:00:30 +01:00
|
|
|
_buildPopup(
|
|
|
|
context,
|
|
|
|
showDomain: true,
|
|
|
|
notes: [
|
2019-10-03 06:55:17 +02:00
|
|
|
Text(
|
|
|
|
'GitTouch needs these permissions',
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 14, fontWeight: FontWeight.w400),
|
|
|
|
),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
Text(
|
2020-02-01 10:00:30 +01:00
|
|
|
'api, read_user, read_repository',
|
2019-10-03 06:55:17 +02:00
|
|
|
style: TextStyle(
|
2020-01-27 08:11:51 +01:00
|
|
|
fontSize: 16, color: theme.palette.primary),
|
2019-10-03 06:55:17 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (result == true) {
|
|
|
|
try {
|
2020-02-01 10:00:30 +01:00
|
|
|
await auth.loginToGitlab(
|
|
|
|
_domainController.text, _tokenController.text);
|
|
|
|
_tokenController.clear();
|
2019-10-03 06:55:17 +02:00
|
|
|
} catch (err) {
|
2020-02-01 10:00:30 +01:00
|
|
|
showError(err);
|
2019-10-03 06:55:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-10-06 10:13:56 +02:00
|
|
|
),
|
2020-02-02 07:08:58 +01:00
|
|
|
_buildAddItem(
|
|
|
|
text: 'Bitbucket Account',
|
|
|
|
brand: FontAwesome5Brands.bitbucket,
|
|
|
|
onTap: () async {
|
|
|
|
_domainController.text = 'https://bitbucket.org';
|
|
|
|
final result = await theme.showConfirm(
|
|
|
|
context,
|
|
|
|
Column(
|
|
|
|
children: <Widget>[
|
|
|
|
MyTextField(
|
|
|
|
controller: _domainController,
|
|
|
|
placeholder: 'Domain'),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
MyTextField(
|
|
|
|
placeholder: 'Username',
|
|
|
|
controller: _usernameController),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
MyTextField(
|
|
|
|
placeholder: 'App password',
|
|
|
|
controller: _passwordController),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
// TODO: permissions
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (result == true) {
|
|
|
|
try {
|
|
|
|
await auth.loginToBb(
|
|
|
|
_domainController.text,
|
|
|
|
_usernameController.text,
|
|
|
|
_passwordController.text);
|
|
|
|
} catch (err) {
|
|
|
|
showError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2020-02-01 10:00:30 +01:00
|
|
|
_buildAddItem(
|
|
|
|
text: 'Gitea Account',
|
|
|
|
brand: Octicons.git_branch, // TODO: brand icon
|
|
|
|
onTap: () async {
|
|
|
|
_domainController.text = 'https://try.gitea.io';
|
|
|
|
final result = await theme.showConfirm(
|
|
|
|
context,
|
|
|
|
_buildPopup(context, showDomain: true),
|
|
|
|
);
|
|
|
|
if (result == true) {
|
|
|
|
try {
|
|
|
|
await auth.loginToGitea(
|
|
|
|
_domainController.text, _tokenController.text);
|
|
|
|
_tokenController.clear();
|
|
|
|
} catch (err) {
|
|
|
|
showError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-02-01 11:30:32 +01:00
|
|
|
),
|
|
|
|
Container(
|
|
|
|
padding: CommonStyle.padding,
|
|
|
|
child: Text(
|
|
|
|
'Note: Long press to remove account',
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
color: theme.palette.secondaryText,
|
|
|
|
),
|
|
|
|
),
|
2020-02-01 10:00:30 +01: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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|