diff --git a/lib/home.dart b/lib/home.dart index befc08a..bd24234 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart'; import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/notification.dart'; import 'package:git_touch/models/theme.dart'; +import 'package:git_touch/screens/gitea_user.dart'; import 'package:git_touch/screens/gitlab_explore.dart'; import 'package:git_touch/screens/gitlab_project.dart'; import 'package:git_touch/screens/gitlab_todos.dart'; @@ -55,6 +56,14 @@ class _HomeState extends State { return GitlabUserScreen(null); } break; + case PlatformType.gitea: + switch (index) { + case 0: + return GiteaUserScreen(null); + case 1: + return GiteaUserScreen(null); + } + break; } } @@ -117,6 +126,17 @@ class _HomeState extends State { title: Text('Me'), ), ]; + case PlatformType.gitea: + return [ + BottomNavigationBarItem( + icon: Icon(Icons.explore), + title: Text('Explore'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.person), + title: Text('Me'), + ), + ]; } } diff --git a/lib/models/auth.dart b/lib/models/auth.dart index 0843c29..2730742 100644 --- a/lib/models/auth.dart +++ b/lib/models/auth.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'dart:convert'; import 'dart:async'; +import 'package:git_touch/models/gitea.dart'; import 'package:git_touch/utils/request_serilizer.dart'; import 'package:gql_http_link/gql_http_link.dart'; import 'package:artemis/artemis.dart'; @@ -21,6 +22,7 @@ const clientId = 'df930d7d2e219f26142a'; class PlatformType { static const github = 'github'; static const gitlab = 'gitlab'; + static const gitea = 'gitea'; } class DataWithPage { @@ -161,6 +163,35 @@ class AuthModel with ChangeNotifier { return DataWithPage(info, next, next != null); } + Future loginToGitea(String domain, String token) async { + try { + loading = true; + notifyListeners(); + + final res = await http.get('$domain/api/v1/user', + headers: {'Authorization': 'token $token'}); + final info = json.decode(res.body); + if (info['message'] != null) { + throw info['message']; + } + final user = GiteaUser.fromJson(info); + + await _addAccount(Account( + platform: PlatformType.gitea, + domain: domain, + token: token, + login: user.login, + avatarUrl: user.avatarUrl, + )); + } catch (err) { + Fimber.e('loginToGitea failed', ex: err); + // TODO: show errors + } finally { + loading = false; + notifyListeners(); + } + } + Future fetchGitea(String p) async { final res = await http.get('https://try.gitea.io' + '/api/v1' + p, headers: {'Authorization': ''}); diff --git a/lib/screens/gitea_user.dart b/lib/screens/gitea_user.dart index c60c44d..b5fca97 100644 --- a/lib/screens/gitea_user.dart +++ b/lib/screens/gitea_user.dart @@ -9,23 +9,25 @@ import 'package:provider/provider.dart'; import 'package:tuple/tuple.dart'; class GiteaUserScreen extends StatelessWidget { - final String username; - - GiteaUserScreen(this.username); + final String login; + GiteaUserScreen(this.login); @override Widget build(BuildContext context) { return RefreshStatefulScaffold< Tuple2>>( - title: Text('User'), + title: Text(login == null ? 'Me' : 'User'), fetchData: () async { final auth = Provider.of(context); + final u = login ?? auth.activeAccount.login; final items = await Future.wait([ - auth.fetchGitea('/users/$username'), - auth.fetchGitea('/users/$username/repos'), + auth.fetchGitea('/users/$u'), + auth.fetchGitea('/users/$u/repos'), ]); - return Tuple2(GiteaUser.fromJson(items[0]), - (items[1] as List).map((v) => GiteaRepository.fromJson(v))); + return Tuple2( + GiteaUser.fromJson(items[0]), + (items[1] as List).map((v) => GiteaRepository.fromJson(v)), + ); }, bodyBuilder: (data, _) { final user = data.item1; diff --git a/lib/screens/login.dart b/lib/screens/login.dart index 165fc9d..16f29b8 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -22,6 +22,8 @@ class _LoginScreenState extends State { String _token = ''; String _gitlabToken = ''; String _gitlabDomain = 'https://gitlab.com'; + String _giteaToken = ''; + String _giteaDomain = 'https://try.gitea.io'; Widget _buildAccountItem(int index) { final theme = Provider.of(context); @@ -146,7 +148,7 @@ class _LoginScreenState extends State { // _buildAddItem( // text: 'GitLab Account by Token', // onTap: () async { - // var result = + // final result = // await Provider.of(context).showConfirm( // context, // Column( @@ -193,6 +195,44 @@ class _LoginScreenState extends State { // } // } // }, + // ), + // _buildAddItem( + // text: 'Gitea Account by Token', + // onTap: () async { + // final result = + // await Provider.of(context).showConfirm( + // context, + // Column( + // children: [ + // CupertinoTextField( + // placeholder: 'Domain', + // onChanged: (v) { + // setState(() { + // _giteaDomain = v; + // }); + // }, + // ), + // SizedBox(height: 8), + // CupertinoTextField( + // placeholder: 'Access token', + // onChanged: (v) { + // setState(() { + // _giteaToken = v; + // }); + // }, + // ), + // ], + // ), + // ); + // if (result == true) { + // try { + // await auth.loginToGitea(_giteaDomain, _giteaToken); + // } catch (err) { + // Provider.of(context).showConfirm( + // context, Text('Token invalid: $err')); + // } + // } + // }, // ) ], ),