-
Notifications
You must be signed in to change notification settings - Fork 90
/
login_screen.dart
190 lines (168 loc) · 4.84 KB
/
login_screen.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
import 'package:flutter/material.dart';
import 'package:connectycube_sdk/connectycube_sdk.dart';
import 'select_opponents_screen.dart';
import 'utils/configs.dart' as utils;
import 'utils/pref_util.dart';
class LoginScreen extends StatelessWidget {
static const String tag = "LoginScreen";
const LoginScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false, title: const Text('P2P calls')),
body: const BodyLayout(),
);
}
}
class BodyLayout extends StatefulWidget {
const BodyLayout({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return BodyState();
}
}
class BodyState extends State<BodyLayout> {
static const String tag = "LoginScreen.BodyState";
bool _isLoginContinues = false;
int? _selectedUserId;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(48),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
"Select user to login:",
style: TextStyle(
fontSize: 22,
),
),
Expanded(
child: _getUsersList(context),
),
],
),
);
}
@override
void initState() {
super.initState();
SharedPrefs.getUser().then((loggedUser) {
if (loggedUser != null) {
_loginToCC(context, loggedUser);
}
});
}
Widget _getUsersList(BuildContext context) {
final users = utils.users;
return ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
return Card(
color: _isLoginContinues ? Colors.white70 : Colors.white,
child: ListTile(
title: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
users[index].fullName!,
style: TextStyle(
color: _isLoginContinues
? Colors.black26
: Colors.black87),
),
Container(
margin: const EdgeInsets.only(left: 8),
height: 18,
width: 18,
child: Visibility(
visible: _isLoginContinues &&
users[index].id == _selectedUserId,
child: const CircularProgressIndicator(
strokeWidth: 2,
),
),
),
],
),
),
onTap: () => _loginToCC(
context,
users[index],
),
),
);
},
);
}
_loginToCC(BuildContext context, CubeUser user) {
if (_isLoginContinues) return;
setState(() {
_isLoginContinues = true;
_selectedUserId = user.id;
});
if (CubeSessionManager.instance.isActiveSessionValid() &&
CubeSessionManager.instance.activeSession!.user != null) {
if (CubeChatConnection.instance.isAuthenticated()) {
setState(() {
_isLoginContinues = false;
_selectedUserId = 0;
});
_goSelectOpponentsScreen(context, user);
} else {
_loginToCubeChat(context, user);
}
} else {
createSession(user).then((cubeSession) {
_loginToCubeChat(context, user);
}).catchError((exception) {
_processLoginError(exception);
});
}
}
void _loginToCubeChat(BuildContext context, CubeUser user) {
CubeChatConnection.instance.login(user).then((cubeUser) {
SharedPrefs.saveNewUser(user);
setState(() {
_isLoginContinues = false;
_selectedUserId = 0;
});
_goSelectOpponentsScreen(context, cubeUser);
}).catchError((exception) {
_processLoginError(exception);
});
}
void _processLoginError(exception) {
log("Login error $exception", tag);
setState(() {
_isLoginContinues = false;
_selectedUserId = 0;
});
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Login Error"),
content:
const Text("Something went wrong during login to ConnectyCube"),
actions: <Widget>[
TextButton(
child: const Text("OK"),
onPressed: () => Navigator.of(context).pop(),
)
],
);
});
}
void _goSelectOpponentsScreen(BuildContext context, CubeUser cubeUser) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => SelectOpponentsScreen(cubeUser),
),
);
}
}