-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
205 lines (184 loc) · 6.61 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'api/api.dart';
import 'api/api_helpers.dart';
import 'api/api_models.dart';
import 'app_type/app_type_managment.dart';
import 'components/menu.dart';
import 'dynimports/webinfo.dart' if (dart.library.html) 'dart:html' as webinfo;
import 'push_notifications.dart';
void main() async {
// This does not need to be waited on as we do not use it in HomeScreen
initializeDateFormatting("de_DE");
Intl.defaultLocale = "de_DE";
// Preload binary messenger to load Shared Preferences
WidgetsFlutterBinding.ensureInitialized();
API api = new API();
await api.preloadUserData(); // Load user data, that is required to build some app functions
runApp(
APIHolder(
MaterialApp(
title: 'KAG',
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'Zen Maru Gothic',
primaryColor: Color.fromRGBO(0, 112, 1, 1),
appBarTheme: AppBarTheme(
color: const Color.fromRGBO(0, 112, 1, 1)
),
colorScheme: ColorScheme(
primary: Color.fromRGBO(0, 112, 1, 1),
background: Color.fromRGBO(47, 47, 47, 1),
secondary: Color.fromRGBO(255, 145, 10, 1),
onSecondary: Color.fromRGBO(255, 145, 10, 1),
brightness: Brightness.light,
error: Colors.red,
onError: Colors.white,
onBackground: Color.fromRGBO(255, 145, 10, 1),
onPrimary: Colors.white,
onSurface: Colors.black,
surface: Colors.white
),
backgroundColor: Color.fromRGBO(47, 47, 47, 1),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateColor.resolveWith((states) => Color.fromRGBO(0, 82, 1, 1))
)
)
),
home: KAGApp(),
), api),
);
// Push Notification Stuff
if (PushNotificationsManager.isPushPlatform()) {
PushNotificationsManager pushNotificationsManager = new PushNotificationsManager();
pushNotificationsManager.init().then((_) {
if (FirebaseMessaging.instance != null) {
FirebaseMessaging.instance.subscribeToTopic(PushNotificationsManager.TOPIC_ALL);
api.hasLoginCredentials().then((value) {
// the messages sent are still public. This is only to annoy less people
// similar code in Views/Login.dart
if (value) {
FirebaseMessaging.instance.subscribeToTopic(PushNotificationsManager.TOPIC_LOGGED_IN);
if (api.requests.getUserInfo().isAppDev) FirebaseMessaging.instance.subscribeToTopic(PushNotificationsManager.TOPIC_APP_DEV);
} else {
FirebaseMessaging.instance.unsubscribeFromTopic(PushNotificationsManager.TOPIC_LOGGED_IN);
FirebaseMessaging.instance.unsubscribeFromTopic(PushNotificationsManager.TOPIC_APP_DEV);
}
});
}
});
}
}
class KAGApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return KAGAppState();
}
}
class KAGAppState extends State<KAGApp> with TickerProviderStateMixin {
AppTypeState _type = getDefaultAppType();
GlobalKey _key = GlobalKey();
static KAGAppState app;
TabController controller;
@override
void initState() {
super.initState();
controller = TabController(initialIndex: 0, length: _type.length, vsync: this); // If you change something here change it in build, too.
API.of(context).hasLoginCredentials().then((loggedIn) => {
if (loggedIn) {
setState(() => type = getLoggedInAppType(API.of(context).requests.getUserInfo()))
}
});
}
KAGAppState(){
app = this;
}
@override
Widget build(BuildContext context) {
if (_type.length != controller.length) {
controller = TabController(initialIndex: controller.index, length: _type.length, vsync: this);
}
if (MediaQuery.of(context).size.width > 700 || (!kIsWeb && Platform.isMacOS)) { // !kIsWeb is required to not cause an exception by calling Platform on web.
return Row(
key: _key,
children: [
NavigationRail(
selectedIndex: controller.index,
selectedIconTheme: IconThemeData(
color: Theme.of(context).colorScheme.secondary
),
selectedLabelTextStyle: TextStyle(
color: Theme.of(context).colorScheme.secondary
),
groupAlignment: 0,
onDestinationSelected: (index) {
setState(() {
controller.index = index;
});
},
labelType: NavigationRailLabelType.selected,
destinations: getNavigationRail(_type.pages),
),
VerticalDivider(thickness: 2, width: 2, color: Theme.of(context).colorScheme.secondary),
// This is the main content.
Expanded(
child: IndexedStack(
index: controller.index,
children: _type.getAppViews(),
),
),
],
);
} else {
return Scaffold(
key: _key,
body: TabBarView(
controller: controller,
children: _type.getAppViews(),
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: BottomNavigationBarMenu(_type.pages, controller)
);
}
}
void setLoggedOut() {
setState(() {
type = getDefaultAppType();
});
}
void setLoggedIn() {
setState(() {
type = getLoggedInAppType(API.of(context).requests.getUserInfo());
});
}
void goToPage(AppPage page) {
setState(() {
controller.animateTo(_type.getPageNumber(page));
});
}
static AppTypeState getDefaultAppType() {
AppType type = AppType.LOGGED_OUT;
if (kIsWeb) {
if (webinfo.window.location.host.startsWith("vplan.")) type = AppType.VPLAN_LOGGED_OUT;
if (webinfo.window.location.host.startsWith("m.")) type = AppType.MOBILE_SITE;
}
return getStateForUser(type, null);
}
static AppTypeState getLoggedInAppType(KAGUser user) {
if (kIsWeb) {
if (webinfo.window.location.host.startsWith("vplan.")) return getStateForUser(AppType.VPLAN, user);
}
return getStateForUser(AppType.NORMAL, user);
}
AppType get appType => _type.appType;
AppTypeState get type => _type;
set type (AppTypeState type) {
_type = type;
_key = GlobalKey();
}
}