1
1
import 'dart:convert' ;
2
-
3
2
import 'package:dio/dio.dart' ;
3
+ import 'package:flood_mobile/Constants/api_endpoints.dart' ;
4
4
import 'package:flood_mobile/Model/register_user_model.dart' ;
5
- import 'package:flood_mobile/Provider/user_detail_provider.dart' ;
6
5
import 'package:flood_mobile/Model/current_user_detail_model.dart' ;
6
+ import 'package:flood_mobile/Blocs/api_bloc/api_bloc.dart' ;
7
+ import 'package:flood_mobile/Blocs/user_detail_bloc/user_detail_bloc.dart' ;
7
8
import 'package:flutter/cupertino.dart' ;
8
- import 'package:provider/provider .dart' ;
9
+ import 'package:flutter_bloc/flutter_bloc .dart' ;
9
10
import 'package:shared_preferences/shared_preferences.dart' ;
10
11
11
- import '../Provider/api_provider.dart' ;
12
-
13
12
class AuthApi {
14
13
static Future <bool > loginUser (
15
14
{required String username,
16
15
required String password,
17
16
required BuildContext context}) async {
18
17
try {
19
- String url = Provider .of <ApiProvider >(context, listen : false ) .baseUrl +
20
- ApiProvider .authenticateUrl;
18
+ String url = BlocProvider .of <ApiBloc >(context).state .baseUrl +
19
+ ApiEndpoints .authenticateUrl;
21
20
print ('---LOGIN USER---' );
22
21
print (url);
23
22
Response response;
@@ -46,17 +45,17 @@ class AuthApi {
46
45
SharedPreferences prefs = await SharedPreferences .getInstance ();
47
46
await prefs.setString ('floodToken' , token);
48
47
await prefs.setString ('floodUsername' , username);
49
- Provider .of <UserDetailProvider >(context, listen: false )
50
- .setUserDetails ( token, username);
48
+ BlocProvider .of <UserDetailBloc >(context, listen: false )
49
+ .add ( SetUserDetailsEvent ( token: token , username: username) );
51
50
return true ;
52
51
}
53
52
return false ;
54
53
} else {
55
54
return false ;
56
55
}
57
- } catch (e ) {
58
- print ('--ERROR--' );
59
- print (e .toString ());
56
+ } catch (error ) {
57
+ print ('--ERROR IN LOGIN USER --' );
58
+ print (error .toString ());
60
59
return false ;
61
60
}
62
61
}
@@ -66,8 +65,9 @@ class AuthApi {
66
65
required BuildContext context,
67
66
}) async {
68
67
try {
69
- String url = Provider .of <ApiProvider >(context, listen: false ).baseUrl +
70
- ApiProvider .registerUser;
68
+ String url =
69
+ BlocProvider .of <ApiBloc >(context, listen: false ).state.baseUrl +
70
+ ApiEndpoints .registerUser;
71
71
print ('---REGISTER USER---' );
72
72
print (url);
73
73
Response response;
@@ -77,7 +77,7 @@ class AuthApi {
77
77
dio.options.headers['Content-Type' ] = "application/json" ;
78
78
dio.options.headers['Connection' ] = "keep-alive" ;
79
79
dio.options.headers['Cookie' ] =
80
- Provider .of <UserDetailProvider >(context, listen: false ).token;
80
+ BlocProvider .of <UserDetailBloc >(context, listen: false ).token;
81
81
Map <String , dynamic > mp = model.toJson ();
82
82
String rawBody = json.encode (mp);
83
83
print (rawBody);
@@ -86,17 +86,18 @@ class AuthApi {
86
86
if (response.statusCode == 200 ) {
87
87
print (response);
88
88
getUsersList (context);
89
- } else {}
90
- } catch (e ) {
91
- print ('--ERROR--' );
92
- print (e .toString ());
89
+ }
90
+ } catch (error ) {
91
+ print ('--ERROR IN REGISTER USER --' );
92
+ print (error .toString ());
93
93
}
94
94
}
95
95
96
96
static getUsersList (BuildContext context) async {
97
97
try {
98
- String url = Provider .of <ApiProvider >(context, listen: false ).baseUrl +
99
- ApiProvider .getUsersList;
98
+ String url =
99
+ BlocProvider .of <ApiBloc >(context, listen: false ).state.baseUrl +
100
+ ApiEndpoints .getUsersList;
100
101
print ('---GET USERS LIST---' );
101
102
print (url);
102
103
Response response;
@@ -106,7 +107,7 @@ class AuthApi {
106
107
dio.options.headers['Content-Type' ] = "application/json" ;
107
108
dio.options.headers['Connection' ] = "keep-alive" ;
108
109
dio.options.headers['Cookie' ] =
109
- Provider .of <UserDetailProvider >(context, listen: false ).token;
110
+ BlocProvider .of <UserDetailBloc >(context, listen: false ).token;
110
111
response = await dio.get (
111
112
url,
112
113
);
@@ -115,23 +116,24 @@ class AuthApi {
115
116
for (final user in response.data) {
116
117
usersList.add (CurrentUserDetailModel .fromJson (user));
117
118
}
118
- Provider .of <UserDetailProvider >(context, listen: false )
119
- .setUsersList ( usersList);
119
+ BlocProvider .of <UserDetailBloc >(context, listen: false )
120
+ .add ( SetUsersListEvent ( usersList: usersList) );
120
121
print ('---USERS LIST---' );
121
122
print (response);
122
- } else {}
123
- } catch (e ) {
124
- print ('--ERROR--' );
125
- print (e .toString ());
123
+ }
124
+ } catch (error ) {
125
+ print ('--ERROR IN GET USER LIST --' );
126
+ print (error .toString ());
126
127
}
127
128
}
128
129
129
130
static deleteUser (BuildContext context, String username) async {
130
131
try {
131
- String url = Provider .of <ApiProvider >(context, listen: false ).baseUrl +
132
- ApiProvider .deleteUser +
133
- "/" +
134
- username;
132
+ String url =
133
+ BlocProvider .of <ApiBloc >(context, listen: false ).state.baseUrl +
134
+ ApiEndpoints .deleteUser +
135
+ "/" +
136
+ username;
135
137
print ('---DELETE USER---' );
136
138
print (url);
137
139
Response response;
@@ -141,7 +143,7 @@ class AuthApi {
141
143
dio.options.headers['Content-Type' ] = "application/json" ;
142
144
dio.options.headers['Connection' ] = "keep-alive" ;
143
145
dio.options.headers['Cookie' ] =
144
- Provider .of <UserDetailProvider >(context, listen: false ).token;
146
+ BlocProvider .of <UserDetailBloc >(context, listen: false ).token;
145
147
response = await dio.delete (
146
148
url,
147
149
);
@@ -150,10 +152,10 @@ class AuthApi {
150
152
print ('---USER DELETED---' );
151
153
// *Getting the users list again
152
154
getUsersList (context);
153
- } else {}
154
- } catch (e ) {
155
- print ('--ERROR--' );
156
- print (e .toString ());
155
+ }
156
+ } catch (error ) {
157
+ print ('--ERROR IN DELETE USER --' );
158
+ print (error .toString ());
157
159
}
158
160
}
159
161
}
0 commit comments