From c6c5465ad35e77767ab5ca45d9d7099f18446866 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 2 Nov 2019 01:48:42 +0800 Subject: [PATCH] feat: gitlab user screen --- lib/main.dart | 100 +++++++++++++++++++++++------------ lib/screens/gitlab/user.dart | 69 ++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 lib/screens/gitlab/user.dart diff --git a/lib/main.dart b/lib/main.dart index 37236ce..8575acd 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,8 @@ import 'package:flutter/cupertino.dart'; import 'package:git_touch/models/code.dart'; import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/theme.dart'; +import 'package:git_touch/screens/gitlab/todos.dart'; +import 'package:git_touch/screens/gitlab/user.dart'; import 'package:git_touch/screens/issues.dart'; import 'package:git_touch/screens/notification.dart'; import 'package:git_touch/screens/repository.dart'; @@ -60,30 +62,45 @@ class _HomeState extends State { } List get _navigationItems { - return [ - BottomNavigationBarItem( - icon: Icon(Icons.rss_feed), - title: Text('News'), - ), - BottomNavigationBarItem( - icon: _buildNotificationIcon(context, false), - activeIcon: _buildNotificationIcon(context, true), - title: Text('Notification'), - ), - BottomNavigationBarItem( - icon: Icon(Icons.trending_up), - title: Text('Trending'), - ), - BottomNavigationBarItem( - icon: Icon(Icons.search), - title: Text('Search'), - ), - BottomNavigationBarItem( - icon: Icon(Icons.person_outline), - activeIcon: Icon(Icons.person), - title: Text('Me'), - ), - ]; + switch (Provider.of(context).activeAccount.platform) { + case PlatformType.github: + return [ + BottomNavigationBarItem( + icon: Icon(Icons.rss_feed), + title: Text('News'), + ), + BottomNavigationBarItem( + icon: _buildNotificationIcon(context, false), + activeIcon: _buildNotificationIcon(context, true), + title: Text('Notification'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.trending_up), + title: Text('Trending'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.search), + title: Text('Search'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.person_outline), + activeIcon: Icon(Icons.person), + title: Text('Me'), + ), + ]; + case PlatformType.gitlab: + return [ + BottomNavigationBarItem( + icon: Icon(Icons.timeline), + title: Text('Todos'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.person_outline), + activeIcon: Icon(Icons.person), + title: Text('Me'), + ), + ]; + } } _buildScreen(int index) { @@ -96,17 +113,30 @@ class _HomeState extends State { // return TrendingScreen(); // return RepoScreen('flutter', 'flutter'); // return Image.asset('images/spinner.webp', width: 32, height: 32); - switch (index) { - case 0: - return NewsScreen(); - case 1: - return NotificationScreen(); - case 2: - return TrendingScreen(); - case 3: - return SearchScreen(); - case 4: - return UserScreen(null); + switch (Provider.of(context).activeAccount.platform) { + case PlatformType.github: + switch (index) { + case 0: + return NewsScreen(); + case 1: + return NotificationScreen(); + case 2: + return TrendingScreen(); + case 3: + return SearchScreen(); + case 4: + return UserScreen(null); + } + break; + case PlatformType.gitlab: + switch (index) { + case 0: + return GitlabTodosScreen(); + case 1: + return GitlabUserScreen( + Provider.of(context).activeAccount.login); + } + break; } } diff --git a/lib/screens/gitlab/user.dart b/lib/screens/gitlab/user.dart new file mode 100644 index 0000000..73b253a --- /dev/null +++ b/lib/screens/gitlab/user.dart @@ -0,0 +1,69 @@ +import 'package:flutter/widgets.dart'; +import 'package:git_touch/models/auth.dart'; +import 'package:git_touch/scaffolds/refresh_stateful.dart'; +import 'package:git_touch/widgets/border_view.dart'; +import 'package:git_touch/widgets/repository_item.dart'; +import 'package:git_touch/widgets/user_item.dart'; +import 'package:provider/provider.dart'; + +class GitlabUserScreen extends StatelessWidget { + final String username; + + GitlabUserScreen(this.username); + + @override + Widget build(BuildContext context) { + return RefreshStatefulScaffold( + title: Text('User'), + fetchData: () async { + final items = await Provider.of(context) + .fetchGitlab('/users?username=$username'); + final user = items[0]; + final projects = await Provider.of(context) + .fetchGitlab('/users/${user['id']}/projects') as List; + final langs = await Future.wait(projects.map((p) => + Provider.of(context) + .fetchGitlab('/projects/${p['id']}/languages'))); + for (var i = 0; i < projects.length; i++) { + projects[i]['language'] = langs[i]; + } + return [user, projects]; + }, + bodyBuilder: (payload) { + final data = payload.data[0]; + final projects = payload.data[1] as List; + + return Column( + children: [ + UserItem( + login: data['username'], + avatarUrl: data['avatar_url'], + name: data['name'], + ), + BorderView(height: 10), + Column( + children: projects.map((project) { + return RepositoryItem({ + 'owner': { + '__typename': 'user', + 'login': project['owner']['name'], + 'avatarUrl': project['owner']['avatar_url'], + }, + 'name': project['name'], + 'description': project['description'], + 'isPrivate': project['visibility'] == 'private', + 'isFork': false, + 'stargazers': {'totalCount': project['star_count']}, + 'forks': { + 'totalCount': project['forks_count'], + }, + 'primaryLanguage': null + }); + }).toList(), + ) + ], + ); + }, + ); + } +}