|
|
|
|
@ -1,10 +1,18 @@
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
import 'package:test_sa/controllers/providers/api/user_provider.dart';
|
|
|
|
|
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
|
|
|
|
|
import 'package:test_sa/main.dart';
|
|
|
|
|
import 'package:test_sa/models/user.dart';
|
|
|
|
|
import 'package:test_sa/new_views/pages/login_page.dart';
|
|
|
|
|
|
|
|
|
|
class ApiManager {
|
|
|
|
|
ApiManager._();
|
|
|
|
|
@ -24,19 +32,22 @@ class ApiManager {
|
|
|
|
|
enableToastMessage = true,
|
|
|
|
|
}) async {
|
|
|
|
|
headers ??= {};
|
|
|
|
|
|
|
|
|
|
headers.addAll(_headers);
|
|
|
|
|
|
|
|
|
|
Uri _url = Uri.parse(url);
|
|
|
|
|
// print(_url);
|
|
|
|
|
// print(headers);
|
|
|
|
|
http.Response response = await http.get(_url, headers: headers);
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty && enableToastMessage) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
showLoginDialog();
|
|
|
|
|
} else {
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
@ -63,15 +74,18 @@ class ApiManager {
|
|
|
|
|
|
|
|
|
|
http.StreamedResponse _streamedResponse = await request.send();
|
|
|
|
|
http.Response response = await http.Response.fromStream(_streamedResponse);
|
|
|
|
|
// print(response.statusCode);
|
|
|
|
|
// log(response.body);
|
|
|
|
|
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
try {
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
showLoginDialog();
|
|
|
|
|
} else {
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
@ -95,14 +109,18 @@ class ApiManager {
|
|
|
|
|
|
|
|
|
|
http.StreamedResponse streamedResponse = await request.send();
|
|
|
|
|
http.Response response = await http.Response.fromStream(streamedResponse);
|
|
|
|
|
// print(response.statusCode);
|
|
|
|
|
// log(response.body);
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
try {
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
showLoginDialog();
|
|
|
|
|
} else {
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -136,14 +154,59 @@ class ApiManager {
|
|
|
|
|
// print(request.files);
|
|
|
|
|
http.StreamedResponse streamedResponse = await request.send();
|
|
|
|
|
http.Response response = await http.Response.fromStream(streamedResponse);
|
|
|
|
|
// print(response.statusCode);
|
|
|
|
|
// log(response.body);
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
try {
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
showLoginDialog();
|
|
|
|
|
} else {
|
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
|
|
|
final message = jsonDecode(response.body)["message"];
|
|
|
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
|
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void showLoginDialog() {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: navigatorKey.currentContext,
|
|
|
|
|
barrierDismissible: false,
|
|
|
|
|
builder: (_) => Platform.isIOS
|
|
|
|
|
? CupertinoAlertDialog(
|
|
|
|
|
title: Text('Session Expired'),
|
|
|
|
|
content: Text('Login session is expired,\nPlease login.'),
|
|
|
|
|
//actionsAlignment: MainAxisAlignment.center,
|
|
|
|
|
actions: [
|
|
|
|
|
CupertinoButton(
|
|
|
|
|
child: Text("Login"),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
logout(navigatorKey.currentContext);
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
: AlertDialog(
|
|
|
|
|
title: Text('Session Expired'),
|
|
|
|
|
content: Text('Login session is expired,\nPlease login.'),
|
|
|
|
|
actionsAlignment: MainAxisAlignment.center,
|
|
|
|
|
contentPadding: EdgeInsets.only(left: 24.0, top: 20.0, right: 24.0, bottom: 16.0),
|
|
|
|
|
actions: [
|
|
|
|
|
FilledButton(
|
|
|
|
|
child: Text("Login"),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
logout(navigatorKey.currentContext);
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void logout(context) async {
|
|
|
|
|
await Provider.of<SettingProvider>(context, listen: false).resetSettings();
|
|
|
|
|
Provider.of<UserProvider>(context, listen: false).reset();
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
Navigator.of(context).pushNamedAndRemoveUntil(LoginPage.routeName, (routes) => true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|