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

272 lines
8.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-10-17 11:35:08 +02:00
import 'package:git_touch/screens/ge_user.dart';
import 'package:git_touch/screens/gl_search.dart';
2021-01-23 15:08:05 +01:00
import 'package:git_touch/screens/go_search.dart';
import 'package:git_touch/screens/go_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-11-01 17:32:47 +01:00
import 'package:git_touch/screens/ge_search.dart';
import 'package:flutter_gen/gen_l10n/S.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>();
2020-01-14 11:13:07 +01:00
_buildScreen(int index) {
2021-02-14 06:01:09 +01:00
// print(Localizations.localeOf(context).toString());
2020-02-07 15:26:37 +01:00
// return GlProjectScreen(32221);
2021-01-17 15:08:32 +01:00
// return GhIssuesScreen('flutter', 'flutter', isPullRequest: true);
// return GhIssueScreen('reactjs', 'rfcs', 29);
// return GhIssueScreen('reactjs', 'rfcs', 68);
2020-01-14 11:13:07 +01:00
// return Image.asset('images/spinner.webp', width: 32, height: 32);
// return GhRepoScreen('shreyas1599', 'test');
2020-01-14 11:13:07 +01:00
final auth = Provider.of<AuthModel>(context);
2021-05-16 09:16:35 +02:00
switch (auth.activeAccount!.platform) {
2020-01-14 11:13:07 +01:00
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:
2021-01-17 15:08:32 +01:00
return GhViewer();
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:
return GlSearchScreen();
case 3:
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:
2021-05-16 09:16:35 +02:00
return GtUserScreen(auth.activeAccount!.login, isViewer: true);
2020-01-29 10:33:54 +01:00
}
break;
2020-10-17 11:35:08 +02:00
case PlatformType.gitee:
switch (index) {
case 0:
2020-11-01 17:32:47 +01:00
return GeSearchScreen();
2020-10-17 11:35:08 +02:00
case 1:
2021-05-16 09:16:35 +02:00
return GeUserScreen(auth.activeAccount!.login, isViewer: true);
2020-10-17 11:35:08 +02:00
}
break;
2021-01-23 15:08:05 +01:00
case PlatformType.gogs:
switch (index) {
case 0:
return GoSearchScreen();
case 1:
2021-05-16 09:16:35 +02:00
return GoUserScreen(auth.activeAccount!.login, isViewer: true);
2021-01-23 15:08:05 +01:00
}
2020-01-14 11:13:07 +01:00
}
}
2021-02-01 16:30:19 +01:00
Widget _buildNotificationIcon(BuildContext context, IconData iconData) {
2020-01-14 11:13:07 +01:00
final theme = Provider.of<ThemeModel>(context);
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
],
);
}
GlobalKey<NavigatorState> getNavigatorKey(int index) {
2020-04-16 05:36:48 +02:00
switch (index) {
case 0:
return tab1;
case 1:
return tab2;
2020-04-16 05:36:48 +02:00
case 2:
return tab3;
2020-04-16 05:36:48 +02:00
case 3:
return tab4;
2020-04-16 05:36:48 +02:00
case 4:
return tab5;
}
return tab1;
}
2021-02-01 16:30:19 +01:00
List<BottomNavigationBarItem> _buildNavigationItems(String platform) {
final search = BottomNavigationBarItem(
icon: Icon(Ionicons.search_outline),
activeIcon: Icon(Ionicons.search),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.search,
2021-02-01 16:30:19 +01:00
);
final group = BottomNavigationBarItem(
icon: Icon(Ionicons.people_outline),
activeIcon: Icon(Ionicons.people),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.organizations,
2021-02-01 16:30:19 +01:00
);
final me = BottomNavigationBarItem(
icon: Icon(Ionicons.person_outline),
activeIcon: Icon(Ionicons.person),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.me,
2021-02-01 16:30:19 +01:00
);
final explore = BottomNavigationBarItem(
icon: Icon(Ionicons.compass_outline),
activeIcon: Icon(Ionicons.compass),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.explore,
2021-02-01 16:30:19 +01:00
);
2021-02-01 16:30:19 +01:00
switch (platform) {
case PlatformType.github:
2021-02-01 16:30:19 +01:00
return [
BottomNavigationBarItem(
icon: Icon(Ionicons.newspaper_outline),
activeIcon: Icon(Ionicons.newspaper),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.news,
2021-02-01 16:30:19 +01:00
),
BottomNavigationBarItem(
icon:
_buildNotificationIcon(context, Ionicons.notifications_outline),
activeIcon: _buildNotificationIcon(context, Ionicons.notifications),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.notification,
2021-02-01 16:30:19 +01:00
),
BottomNavigationBarItem(
icon: Icon(Ionicons.flame_outline),
activeIcon: Icon(Ionicons.flame),
2021-05-16 09:16:35 +02:00
label: AppLocalizations.of(context)!.trending,
2021-02-01 16:30:19 +01:00
),
search,
me,
];
case PlatformType.gitlab:
2021-02-01 16:30:19 +01:00
return [explore, group, search, me];
case PlatformType.bitbucket:
2021-02-01 16:30:19 +01:00
return [explore, group, me];
case PlatformType.gitea:
2021-02-01 16:30:19 +01:00
return [group, me];
2020-10-17 11:35:08 +02:00
case PlatformType.gitee:
2021-01-23 15:08:05 +01:00
case PlatformType.gogs:
2021-02-01 16:30:19 +01:00
return [search, me];
default:
return [];
}
}
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
final auth = Provider.of<AuthModel>(context);
if (auth.activeAccount == null) {
return LoginScreen();
}
2021-05-16 09:16:35 +02:00
final navigationItems = _buildNavigationItems(auth.activeAccount!.platform);
2021-02-01 16:30:19 +01:00
2020-01-14 11:13:07 +01:00
switch (theme.theme) {
case AppThemeType.cupertino:
2020-04-16 05:36:48 +02:00
return WillPopScope(
2020-05-01 12:05:49 +02:00
onWillPop: () async {
2021-05-16 09:16:35 +02:00
return !(await getNavigatorKey(auth.activeTab)
2020-05-01 12:05:49 +02:00
.currentState
2021-05-16 09:16:35 +02:00
?.maybePop())!;
2020-05-01 12:05:49 +02:00
},
child: CupertinoTabScaffold(
tabBuilder: (context, index) {
return CupertinoTabView(
navigatorKey: getNavigatorKey(index),
builder: (context) {
return _buildScreen(index);
2020-04-16 05:36:48 +02:00
},
2020-05-01 12:05:49 +02:00
);
},
tabBar: CupertinoTabBar(
items: navigationItems,
2020-05-01 12:05:49 +02:00
currentIndex: auth.activeTab,
onTap: (index) {
if (auth.activeTab == index) {
getNavigatorKey(index)
.currentState
?.popUntil((route) => route.isFirst);
} else {
auth.setActiveTab(index);
}
},
),
),
);
2020-01-14 11:13:07 +01:00
default:
return Scaffold(
body: IndexedStack(
index: auth.activeTab,
children: [
for (var i = 0; i < navigationItems.length; i++) _buildScreen(i)
],
),
2020-01-14 11:13:07 +01:00
bottomNavigationBar: BottomNavigationBar(
2020-01-27 08:11:51 +01:00
selectedItemColor: theme.palette.primary,
items: navigationItems,
2020-05-01 12:05:49 +02:00
currentIndex: auth.activeTab,
2020-01-14 11:13:07 +01:00
type: BottomNavigationBarType.fixed,
onTap: (int index) {
2020-05-01 12:05:49 +02:00
auth.setActiveTab(index);
2020-01-14 11:13:07 +01:00
},
),
);
}
}
}