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

256 lines
7.6 KiB
Dart
Raw Normal View History

2020-01-14 11:13:07 +01:00
import 'package:flutter/material.dart';
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';
2020-02-02 10:30:48 +01:00
import 'package:git_touch/screens/bb_explore.dart';
2020-02-02 10:47:05 +01:00
import 'package:git_touch/screens/bb_teams.dart';
2020-02-02 09:40:12 +01:00
import 'package:git_touch/screens/bb_user.dart';
2020-02-07 15:20:06 +01:00
import 'package:git_touch/screens/gt_orgs.dart';
import 'package:git_touch/screens/gt_user.dart';
2020-02-07 15:26:37 +01:00
import 'package:git_touch/screens/gl_explore.dart';
import 'package:git_touch/screens/gl_groups.dart';
import 'package:git_touch/screens/gl_user.dart';
2020-01-14 11:27:43 +01:00
import 'package:git_touch/screens/login.dart';
2020-02-07 07:17:05 +01:00
import 'package:git_touch/screens/gh_notification.dart';
import 'package:git_touch/screens/gh_user.dart';
2020-01-14 11:13:07 +01:00
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
2020-02-07 07:17:05 +01:00
import 'package:git_touch/screens/gh_news.dart';
import 'package:git_touch/screens/gh_search.dart';
import 'package:git_touch/screens/gh_trending.dart';
2020-01-14 11:13:07 +01:00
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
// Created 5 different variables instead of a list as list doesn't work
final GlobalKey<NavigatorState> tab1 = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> tab2 = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> tab3 = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> tab4 = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> tab5 = GlobalKey<NavigatorState>();
final List<Widget> screens = List<Widget>();
2020-01-14 11:13:07 +01:00
int active = 0;
_buildScreen(int index) {
2020-02-07 15:26:37 +01:00
// return GlProjectScreen(32221);
2020-01-14 11:13:07 +01:00
// return IssuesScreen('flutter', 'flutter', isPullRequest: true);
// return IssueScreen('reactjs', 'rfcs', 29);
// return IssueScreen('reactjs', 'rfcs', 68, isPullRequest: true);
// return Image.asset('images/spinner.webp', width: 32, height: 32);
final auth = Provider.of<AuthModel>(context);
switch (auth.activeAccount.platform) {
case PlatformType.github:
switch (index) {
case 0:
2020-02-07 07:17:05 +01:00
return GhNewsScreen();
2020-01-14 11:13:07 +01:00
case 1:
2020-02-07 07:17:05 +01:00
return GhNotificationScreen();
2020-01-14 11:13:07 +01:00
case 2:
2020-02-07 07:17:05 +01:00
return GhTrendingScreen();
2020-01-14 11:13:07 +01:00
case 3:
2020-02-07 07:17:05 +01:00
return GhSearchScreen();
2020-01-14 11:13:07 +01:00
case 4:
2020-02-07 07:17:05 +01:00
return GhUserScreen(null);
2020-01-14 11:13:07 +01:00
}
break;
case PlatformType.gitlab:
switch (index) {
case 0:
2020-02-07 15:26:37 +01:00
return GlExploreScreen();
2020-01-14 11:13:07 +01:00
case 1:
2020-02-07 15:26:37 +01:00
return GlGroupsScreenn();
2020-02-01 03:32:29 +01:00
case 2:
2020-02-07 15:26:37 +01:00
return GlUserScreen(null);
2020-01-14 11:13:07 +01:00
}
break;
2020-02-02 09:40:12 +01:00
case PlatformType.bitbucket:
switch (index) {
case 0:
2020-02-02 10:47:05 +01:00
return BbExploreScreen();
2020-02-02 09:40:12 +01:00
case 1:
2020-02-02 10:47:05 +01:00
return BbTeamsScreen();
case 2:
2020-02-02 09:40:12 +01:00
return BbUserScreen(null);
}
break;
2020-01-29 10:33:54 +01:00
case PlatformType.gitea:
switch (index) {
case 0:
2020-02-07 15:20:06 +01:00
return GtOrgsScreen();
2020-01-29 10:33:54 +01:00
case 1:
2020-02-07 15:20:06 +01:00
return GtUserScreen(null);
2020-01-29 10:33:54 +01:00
}
break;
2020-01-14 11:13:07 +01:00
}
}
Widget _buildNotificationIcon(BuildContext context, bool isActive) {
final theme = Provider.of<ThemeModel>(context);
final iconData = Icons.notifications;
int count = Provider.of<NotificationModel>(context).count;
if (count == 0) {
return Icon(iconData);
}
// String text = count > 99 ? '99+' : count.toString();
return Stack(
children: <Widget>[
Icon(iconData),
Positioned(
right: -2,
top: -2,
child: Icon(Octicons.primitive_dot,
2020-01-27 08:11:51 +01:00
color: theme.palette.primary, size: 14))
2020-01-14 11:13:07 +01:00
],
);
}
List<BottomNavigationBarItem> get _navigationItems {
switch (Provider.of<AuthModel>(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.whatshot),
title: Text('Trending'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
activeIcon: Icon(Icons.person),
title: Text('Me'),
),
];
case PlatformType.gitlab:
return [
BottomNavigationBarItem(
2020-01-29 07:38:27 +01:00
icon: Icon(Icons.explore),
title: Text('Explore'),
2020-01-14 11:13:07 +01:00
),
2020-02-01 03:32:29 +01:00
BottomNavigationBarItem(
icon: Icon(Icons.group),
title: Text('Groups'),
),
2020-01-14 11:13:07 +01:00
BottomNavigationBarItem(
2020-01-29 07:38:27 +01:00
icon: Icon(Icons.person),
2020-01-14 11:13:07 +01:00
title: Text('Me'),
),
];
2020-02-02 09:40:12 +01:00
case PlatformType.bitbucket:
return [
BottomNavigationBarItem(
icon: Icon(Icons.explore),
title: Text('Explore'),
),
2020-02-02 10:47:05 +01:00
BottomNavigationBarItem(
icon: Icon(Icons.group),
title: Text('Teams'),
),
2020-02-02 09:40:12 +01:00
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Me'),
),
];
2020-01-29 10:33:54 +01:00
case PlatformType.gitea:
return [
BottomNavigationBarItem(
2020-02-01 11:51:33 +01:00
icon: Icon(Icons.group),
title: Text('Organizations'),
2020-02-01 03:32:29 +01:00
),
2020-01-29 10:33:54 +01:00
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Me'),
),
];
2020-01-14 11:13:07 +01:00
}
}
GlobalKey<NavigatorState> getNavigatorKey(int index) {
switch(index) {
case 0:
return tab1;
case 1:
return tab2;
case 2:
return tab3;
case 3:
return tab4;
case 4:
return tab5;
}
return tab1;
}
2020-01-14 11:13:07 +01:00
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
2020-01-14 11:27:43 +01:00
final auth = Provider.of<AuthModel>(context);
final CupertinoTabController _controller = CupertinoTabController();
2020-01-14 11:27:43 +01:00
if (auth.activeAccount == null) {
return LoginScreen();
}
2020-01-14 11:13:07 +01:00
switch (theme.theme) {
case AppThemeType.cupertino:
return WillPopScope(
onWillPop: () async {
return !await getNavigatorKey(_controller.index).currentState.maybePop();
2020-01-14 11:13:07 +01:00
},
child: CupertinoTabScaffold(
controller: _controller,
tabBuilder: (context, index) {
return CupertinoTabView(
navigatorKey: getNavigatorKey(index),
builder: (context) {
return _buildScreen(index);
}
);
},
tabBar: CupertinoTabBar(items: _navigationItems,
onTap: (index) {
if(active == index) {
getNavigatorKey(index).currentState.popUntil((route) => route.isFirst);
}
active = index;
}
)
)
2020-01-14 11:13:07 +01:00
);
default:
return Scaffold(
body: _buildScreen(active),
bottomNavigationBar: BottomNavigationBar(
2020-01-27 08:11:51 +01:00
selectedItemColor: theme.palette.primary,
2020-01-14 11:13:07 +01:00
items: _navigationItems,
currentIndex: active,
type: BottomNavigationBarType.fixed,
onTap: (int index) {
setState(() {
active = index;
});
},
),
);
}
}
}