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

270 lines
8.1 KiB
Dart
Raw Normal View History

2018-04-18 14:38:28 +02:00
import 'package:flutter/material.dart';
2019-01-22 16:39:44 +01:00
import 'package:flutter/cupertino.dart';
2019-09-15 11:36:09 +02:00
import 'package:git_touch/models/code.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/theme.dart';
2020-01-04 06:28:29 +01:00
import 'package:git_touch/screens/about.dart';
2020-01-12 10:13:48 +01:00
import 'package:git_touch/screens/code_theme.dart';
2019-12-12 13:29:56 +01:00
import 'package:git_touch/screens/commits.dart';
2019-12-14 17:08:12 +01:00
import 'package:git_touch/screens/gitlab_blob.dart';
import 'package:git_touch/screens/gitlab_issue.dart';
2019-12-14 17:08:12 +01:00
import 'package:git_touch/screens/gitlab_project.dart';
2019-12-11 15:26:04 +01:00
import 'package:git_touch/screens/gitlab_todos.dart';
2019-12-14 17:08:12 +01:00
import 'package:git_touch/screens/gitlab_tree.dart';
2019-12-11 15:26:04 +01:00
import 'package:git_touch/screens/gitlab_user.dart';
2019-12-12 13:29:56 +01:00
import 'package:git_touch/screens/issue_form.dart';
2019-09-13 11:45:09 +02:00
import 'package:git_touch/screens/issues.dart';
2019-09-30 11:37:51 +02:00
import 'package:git_touch/screens/notification.dart';
2019-12-12 13:29:56 +01:00
import 'package:git_touch/screens/object.dart';
2019-09-23 12:28:33 +02:00
import 'package:git_touch/screens/repository.dart';
2019-12-12 15:02:58 +01:00
import 'package:git_touch/screens/settings.dart';
2019-12-13 06:40:05 +01:00
import 'package:git_touch/screens/stargazers.dart';
2019-09-13 11:00:45 +02:00
import 'package:git_touch/screens/user.dart';
2019-12-13 06:40:05 +01:00
import 'package:git_touch/screens/watchers.dart';
2019-12-25 03:41:51 +01:00
import 'package:git_touch/utils/utils.dart';
2019-09-02 14:40:20 +02:00
import 'package:provider/provider.dart';
import 'package:git_touch/models/notification.dart';
2019-12-12 13:29:56 +01:00
import 'package:fluro/fluro.dart';
2019-12-30 13:55:37 +01:00
import 'package:git_touch/screens/news.dart';
import 'package:git_touch/screens/search.dart';
import 'package:git_touch/screens/login.dart';
import 'package:git_touch/screens/issue.dart';
import 'package:git_touch/screens/trending.dart';
import 'package:fimber/fimber.dart';
2018-04-18 14:38:28 +02:00
class Home extends StatefulWidget {
@override
2019-02-07 07:35:19 +01:00
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int active = 0;
2019-02-08 16:20:28 +01:00
// String login;
2019-09-14 19:43:41 +02:00
Widget _buildNotificationIcon(BuildContext context, bool isActive) {
2019-12-21 10:26:26 +01:00
final theme = Provider.of<ThemeModel>(context);
2020-01-14 07:01:08 +01:00
final iconData = Icons.notifications;
2019-09-02 14:40:20 +02:00
int count = Provider.of<NotificationModel>(context).count;
2019-01-31 15:07:52 +01:00
if (count == 0) {
2019-09-14 19:43:41 +02:00
return Icon(iconData);
2019-01-31 15:07:52 +01:00
}
2019-02-06 14:35:52 +01:00
// String text = count > 99 ? '99+' : count.toString();
return Stack(
children: <Widget>[
2019-09-14 19:43:41 +02:00
Icon(iconData),
Positioned(
2019-09-14 19:43:41 +02:00
right: -2,
top: -2,
2019-09-14 11:19:33 +02:00
child: Icon(Octicons.primitive_dot,
2019-12-21 10:26:26 +01:00
color: theme.palette.primary, size: 14))
],
);
2019-01-31 15:07:52 +01:00
}
2019-10-02 08:58:11 +02:00
List<BottomNavigationBarItem> get _navigationItems {
2019-11-01 18:48:42 +01:00
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(
2020-01-14 07:01:08 +01:00
icon: Icon(Icons.whatshot),
2019-11-01 18:48:42 +01:00
title: Text('Trending'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
2020-01-14 07:01:08 +01:00
icon: Icon(Icons.person),
2019-11-01 18:48:42 +01:00
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) {
2019-12-14 17:08:12 +01:00
// return GitlabProjectScreen(32221);
2020-01-12 09:09:46 +01:00
// return IssuesScreen('flutter', 'flutter', isPullRequest: true);
// return IssueScreen('reactjs', 'rfcs', 29);
// return IssueScreen('reactjs', 'rfcs', 68, isPullRequest: true);
2019-05-12 09:14:28 +02:00
// return Image.asset('images/spinner.webp', width: 32, height: 32);
2019-12-06 14:54:09 +01:00
final auth = Provider.of<AuthModel>(context);
switch (auth.activeAccount.platform) {
2019-11-01 18:48:42 +01:00
case PlatformType.github:
switch (index) {
case 0:
return NewsScreen();
case 1:
return NotificationScreen();
case 2:
return TrendingScreen();
case 3:
return SearchScreen();
case 4:
2020-01-01 09:35:50 +01:00
return UserScreen('');
2019-11-01 18:48:42 +01:00
}
break;
case PlatformType.gitlab:
switch (index) {
case 0:
return GitlabTodosScreen();
case 1:
2019-12-30 13:55:37 +01:00
return GitlabUserScreen(auth.activeAccount.gitlabId);
2019-11-01 18:48:42 +01:00
}
break;
}
}
@override
Widget build(BuildContext context) {
2019-12-12 13:29:56 +01:00
final auth = Provider.of<AuthModel>(context);
final theme = Provider.of<ThemeModel>(context);
2019-11-05 14:22:41 +01:00
final themData = ThemeData(
2020-01-13 14:07:28 +01:00
brightness: theme.brightnessEnum,
2020-01-14 07:52:14 +01:00
primaryColor: theme.palette.background,
);
2019-02-07 07:35:19 +01:00
// TODO:
2019-12-12 13:29:56 +01:00
if (!auth.ready || !Provider.of<ThemeModel>(context).ready) {
return MaterialApp(theme: themData, home: Scaffold(body: Text('a')));
2019-02-07 07:35:19 +01:00
}
// Fimber.d(settings.activeLogin);
2019-12-12 13:29:56 +01:00
if (auth.activeAccount == null) {
return MaterialApp(theme: themData, home: LoginScreen());
2019-02-07 07:35:19 +01:00
}
2019-12-12 13:29:56 +01:00
switch (theme.theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
return CupertinoApp(
2019-11-05 14:22:41 +01:00
theme: CupertinoThemeData(
2020-01-13 14:07:28 +01:00
brightness: theme.brightnessEnum,
2019-12-21 10:26:26 +01:00
primaryColor: theme.palette.primary,
2019-11-05 14:22:41 +01:00
),
home: CupertinoTabScaffold(
tabBar: CupertinoTabBar(items: _navigationItems),
tabBuilder: (context, index) {
return CupertinoTabView(
builder: (context) {
return _buildScreen(index);
2019-11-05 14:22:41 +01:00
},
);
},
),
);
default:
return MaterialApp(
theme: themData,
home: Scaffold(
body: _buildScreen(active),
bottomNavigationBar: BottomNavigationBar(
2019-12-21 10:26:26 +01:00
selectedItemColor: theme.palette.primary,
2019-10-02 08:58:11 +02:00
items: _navigationItems,
currentIndex: active,
type: BottomNavigationBarType.fixed,
onTap: (int index) {
setState(() {
active = index;
});
},
),
),
);
}
}
}
void main() async {
2019-11-05 08:09:54 +01:00
WidgetsFlutterBinding.ensureInitialized();
2019-02-07 07:35:19 +01:00
// Platform messages may fail, so we use a try/catch PlatformException.
// try {
// String initialLink = await getInitialLink();
// Fimber.d(initialLink);
2019-02-07 07:35:19 +01:00
// } on PlatformException {
// Fimber.d('test');
2019-02-07 07:35:19 +01:00
// }
// DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
// AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
// Fimber.d('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
// IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
// Fimber.d('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
// TODO: Later, should check to enable debug log in debug build only
Fimber.plantTree(DebugTree());
2019-11-05 08:09:54 +01:00
final notificationModel = NotificationModel();
final themeModel = ThemeModel();
final authModel = AuthModel();
final codeModel = CodeModel();
await Future.wait([
themeModel.init(),
authModel.init(),
codeModel.init(),
]);
2019-12-25 03:41:51 +01:00
final List<RouterScreen> routers = [
2020-01-12 10:13:48 +01:00
codeThemeRouter,
2020-01-04 06:28:29 +01:00
aboutRouter,
2019-12-30 13:55:37 +01:00
gitlabUserRouter,
2019-12-14 17:08:12 +01:00
gitlabBlobRouter,
gitlabTreeRouter,
gitlabProjectRouter,
gitlabIssueRouter,
2019-12-13 06:13:45 +01:00
loginRouter,
settingsRouter,
userRouter,
repositoryRouter,
2020-01-07 08:55:39 +01:00
issueAddRouter,
2019-12-13 06:13:45 +01:00
issuesRouter,
pullsRouter,
issueRouter,
pullRouter,
commitsRouter,
objectRouter,
2019-12-13 06:40:05 +01:00
stargazersRouter,
watchersRouter,
2019-12-13 06:13:45 +01:00
];
2019-12-12 13:29:56 +01:00
2019-12-13 06:13:45 +01:00
routers.forEach((screen) {
themeModel.router
.define(screen.path, handler: Handler(handlerFunc: screen.handler));
});
2019-11-05 08:09:54 +01:00
runApp(MultiProvider(
providers: [
2019-12-07 16:12:31 +01:00
ChangeNotifierProvider(create: (context) => notificationModel),
ChangeNotifierProvider(create: (context) => themeModel),
ChangeNotifierProvider(create: (context) => authModel),
ChangeNotifierProvider(create: (context) => codeModel),
2019-11-05 08:09:54 +01:00
],
child: Home(),
));
}