-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
152 lines (132 loc) · 4.87 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
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:alfaaz/pages/authentication/user_info_page.dart';
import 'package:alfaaz/pages/home/home_page.dart';
import 'package:alfaaz/routes/ui_routes.dart';
import 'package:alfaaz/services/stream_chat/stream_chat_api.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'package:streaming_shared_preferences/streaming_shared_preferences.dart';
import 'routes/app_routes.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
static final navigatorKey = GlobalKey<NavigatorState>();
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp>
{
// mixins implemented for multiple inheritance like functionality
InitData? _initData; //Nullable type indicated by ?
static final navigatorKey = GlobalKey<NavigatorState>();
Future<InitData> _initConnection() async {
String? apiKey, userId, token;
final secureStorage = FlutterSecureStorage();
/*FLutter Secure Storage is used to retain user details and preferences
in order to keep them saved on each new launch*/
apiKey = await secureStorage.read(key: kStreamApiKey);
userId = await secureStorage.read(key: kStreamUserId);
token = await secureStorage.read(key: kStreamToken);
final client = StreamChatClient(
apiKey ?? StreamApi.kDefaultStreamApiKey,
// ?? null-aware operator which returns the expression on its left
// unless that expression’s value is null
logLevel: Level.INFO,
//connectTimeout: Duration(milliseconds: 1000000000000000),
)..chatPersistenceClient = StreamApi.chatPersistentClient;
// shorthand setter for chatPersistentClient
StreamApi.setClient(client);
if (userId != null && token != null) {
await client.connectUser(
// Sets the current user and connect the websocket using the userID and token generated
/// a user token using our server SDK
User(id: userId),
token,
);
}
final prefs = await StreamingSharedPreferences.instance;
//Wraps Shared Preferences with a Stream-based layer,
//allowing you to listen to changes in the underlying values.
return InitData(client, prefs);
}
@override
void initState() {
// Sets the splash screen & animation delay
final timeOfStartMs = DateTime.now().millisecondsSinceEpoch;
_initConnection().then(
(initData) {
setState(() {
_initData = initData;
});
//sets the _init as a callback after the connection is established
// with the API
final now = DateTime.now().millisecondsSinceEpoch;
},
);
super.initState();
}
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: [
if (_initData != null)
PreferenceBuilder<int>(
preference: _initData!.preferences.getInt(
'theme',
defaultValue: 0,
),
builder: (context, snapshot) => MaterialApp(
navigatorKey: navigatorKey,
// Initialising the theme of the application
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: {
-1: ThemeMode.dark,
0: ThemeMode.system,
1: ThemeMode.light,
}[snapshot],
builder: (context, child) => StreamChatTheme(
data: StreamChatThemeData(
brightness: Theme.of(context).brightness,
),
child: child!,
),
onGenerateRoute: AppRoutes.generateRoute,
onGenerateInitialRoutes: (initialRouteName) {
if (initialRouteName == Routes.home) {
return [
AppRoutes.generateRoute(
RouteSettings(
name: Routes.home,
arguments: HomePageArgs(_initData!.client),
),
)!
];
}
return [
AppRoutes.generateRoute(
const RouteSettings(
name: Routes.intro,
),
)!
];
},
initialRoute: _initData!.client.state.currentUser == null
//Choose the launch screen on basis of the state of user login
// ? Routes.SIGN_IN
? Routes.intro
: Routes.home,
),
),
],
);
}
}