//base_services import 'dart:convert';
import 'dart:io';
import 'package:aqark/models/ad_master.dart';
import 'package:aqark/models/category_master.dart';
import 'package:aqark/models/google_address.dart';
import 'package:aqark/models/message/message.dart';
import 'package:aqark/models/message_master.dart';
import 'package:aqark/models/notification_master.dart';
import 'package:aqark/models/otp_send_master.dart';
import 'package:aqark/models/property_detail_master.dart';
import 'package:aqark/models/property_list_master.dart';
import 'package:aqark/models/region_master.dart';
import 'package:aqark/models/user.dart';
import 'package:aqark/utils/closable_multipart_request.dart';
import 'package:aqark/utils/constant.dart';
import 'package:async/async.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'api_para.dart';
import 'api_url.dart';
class Services {
Future? userDetails(String params) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
final http.Response response =
await http.post(ApiUrl.USER_DETAILS, body: params);
print("URL: " + response.request!.url.toString());
if (response.statusCode == 200) {
print(response.body);
return LoginMaster.fromJson(json.decode(response.body));
} else {
throw Exception("The username or password is incorrect.");
}
} catch (err) {
throw err;
}
} else {
throw Exception("No internet connection");
}
}
Future? login(
{isSocial,
name,
phone,
password,
deviceToken,
languageCode,
email,
facebookId,
googleId,
isEmailVerified,
isPhoneVerified,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getLoginParams() {
var map = new Map();
map[ApiParams.languageCode] = languageCode;
if (phone != null) {
map[ApiParams.phone] = phone;
}
if (password != null) {
map[ApiParams.password] = password;
}
map[ApiParams.deviceToken] = deviceToken;
map[ApiParams.name] = name;
if (email != null) {
map[ApiParams.Email_ID] = email;
}
map[ApiParams.issocial] = isSocial;
if (facebookId != null) {
map[ApiParams.facebookId] = facebookId;
map[ApiParams.isEmailVerified] = "1";
map[ApiParams.isPhoneVerified] = "0";
}
if (googleId != null) {
map[ApiParams.googleId] = googleId;
map[ApiParams.isEmailVerified] = "1";
map[ApiParams.isPhoneVerified] = "0";
}
map[ApiParams.deviceName] = Platform.isAndroid
? AppConstants.DEVICE_ACCESS_ANDROID
: AppConstants.DEVICE_ACCESS_IOS;
print("Parameter: " + json.encode(map));
return json.encode(map);
}
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.LOGIN, body: getLoginParams());
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print(response.body);
return LoginMaster.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
/* forgot password*/
Future? forgotPassword(
{email,
languageCode,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getLoginParams() {
var map = new Map();
map[ApiParams.languageCode] = languageCode;
map[ApiParams.email] = email;
print("Parameter: " + json.encode(map));
return json.encode(map);
}
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.FORGOT_PASSWORD, body: getLoginParams());
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
return Message.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future? otpSend(
{mobile,
Function? onStartLoading,
Function? onStopLoading,
Function? onFailed,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getLoginParams() {
var map = new Map();
map[ApiParams.phone] = mobile;
print("Parameter: " + json.encode(map));
return json.encode(map);
}
print("URL: " + ApiUrl.SEND_OTP.toString());
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.SEND_OTP, body: getLoginParams());
print("URL: " + response.request!.url.toString());
print("Response: " + response.body.toString());
if (onStopLoading != null) onStopLoading();
return OtpSendMaster.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future? otpVerify(
{mobile,
otp,
verificationId,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getLoginParams() {
var map = new Map();
map[ApiParams.phone] = mobile;
map[ApiParams.otp] = otp;
map[ApiParams.verificationId] = int.parse(verificationId);
print("Parameter: " + json.encode(map));
return json.encode(map);
}
try {
if (onStartLoading != null) onStartLoading();
final http.Response response = await http.post(ApiUrl.OTP_VERIFY,
body: getLoginParams(),
headers: {"Content-Type": "application/json"});
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
return OtpSendMaster.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future? createUser(
{isSocial,
name,
email,
phone,
password,
deviceToken,
languageCode,
facebookId,
googleId,
String? image,
isEmailverified,
isPhoneverified,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
// TODO: implement createUser
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getRegisterParams() {
var map = new Map();
map[ApiParams.languageCode] = languageCode;
map[ApiParams.name] = name;
if (phone != null) {
map[ApiParams.phone] = phone;
}
if (password != null && isSocial == "0") {
map[ApiParams.password] = password;
}
map[ApiParams.deviceToken] = deviceToken;
map[ApiParams.issocial] = isSocial;
if (email != null) {
map[ApiParams.Email_ID] = email;
}
map[ApiParams.isEmailVerified] = isEmailverified ?? "0";
map[ApiParams.isPhoneVerified] = isPhoneverified ?? "0";
if (facebookId != null) {
map[ApiParams.facebookId] = facebookId;
}
if (googleId != null) {
map[ApiParams.googleId] = googleId;
}
if (image != null) {
map[ApiParams.image] = image ?? "";
}
map[ApiParams.deviceName] = Platform.isAndroid
? AppConstants.DEVICE_ACCESS_ANDROID
: AppConstants.DEVICE_ACCESS_IOS;
print("SignUp Parameter: " + json.encode(map));
return json.encode(map);
}
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.REGISTER, body: getRegisterParams());
print("URL: " + response.request!.url.toString());
print("Response: " + response.body);
if (onStopLoading != null) onStopLoading();
return LoginMaster.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
print("Service error: " + err.toString());
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future updateProfile({
String? params,
String? imagePath,
Function? success,
Function? fail,
Function? onNoInternet,
}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
final request = await http.MultipartRequest(
AppConstants.METHOD_POST, ApiUrl.UPDATE_PROFILE);
request.fields[ApiParams.json_content] = params!;
try {
if (imagePath != null || !imagePath!.startsWith("http")) {
File imageFile = new File(imagePath);
var stream =
new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length = await imageFile.length();
var multipartFile = new http.MultipartFile(
ApiParams.image, stream, length,
filename: basename(imageFile.path));
request.files.add(multipartFile);
}
} catch (e) {}
request.send().then((response) {
print("URL: " + response.request!.url.toString());
try {
response.stream.transform(utf8.decoder).listen((value) {
print("Response: " + value);
print("statusCode: " + response.statusCode.toString());
if (response.statusCode == 200) {
LoginMaster user = LoginMaster.fromJson(json.decode(value));
success!(user);
} else {
fail!();
}
});
} catch (e) {
print("Error: " + e.toString());
fail!();
}
});
} else {
if (onNoInternet != null) onNoInternet();
}
}
Future? appLogout(
{jsons,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.APP_LOGOUT, body: jsons);
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print("APP LOGOUT" + response.body);
return MessageMaster.fromJson(json.decode(response.body));
} catch (err) {
print("APP LOGOUT ERROR: " + err.toString());
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future? getCategory(
{languageCode,
type,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getLoginParams() {
var map = new Map();
map[ApiParams.languageCode] = languageCode;
if (type != null) {
map[ApiParams.type] = type;
}
print("Parameter: " + json.encode(map));
return json.encode(map);
}
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.GET_CATEGORY_LIST, body: getLoginParams());
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
return CategoryMaster.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
print(err.toString());
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future? getRegions(
{languageCode,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
String getLoginParams() {
var map = new Map();
map[ApiParams.languageCode] = languageCode;
print("Parameter: " + json.encode(map));
return json.encode(map);
}
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.GET_REGION_LIST, body: getLoginParams());
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
return RegionMaster.fromJson(json.decode(response.body));
} catch (err) {
if (onStopLoading != null) onStopLoading();
print(err.toString());
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
/* get Notification list */
Future? getNotificationList(
{jsons,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.GET_NOTIFICATION_LIST, body: jsons);
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print("jobsResponse" + response.body);
return NotificationMaster.fromJson(json.decode(response.body));
} catch (err) {
print("Job list error: " + err.toString());
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
/* get Property list */
Future? getPropertyList(
{jsons,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.GET_PROPERTY_LIST, body: jsons);
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print("jobsResponse" + response.body);
return PropertyListMaster.fromJson(json.decode(response.body));
} catch (err) {
print("Job list error: " + err.toString());
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
/* get user Property list */
Future? getUserPropertyList(
{jsons,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.USER_PROPERTY_LIST, body: jsons);
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print("jobsResponse" + response.body);
return PropertyListMaster.fromJson(json.decode(response.body));
} catch (err) {
print("Job list error: " + err.toString());
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
/* get Property list */
Future? getMapPropertyList(
{jsons,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.GET_MAP_PROPERTY_LIST, body: jsons);
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print("jobsResponse" + response.body);
return PropertyListMaster.fromJson(json.decode(response.body));
} catch (err) {
print("Job list error: " + err.toString());
if (onStopLoading != null) onStopLoading();
return null;
}
} else {
if (onNoInternet != null) onNoInternet();
}
return null;
}
Future? getFavoutitePropertyList(
{jsons,
Function? onStartLoading,
Function? onStopLoading,
Function? onNoInternet}) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {
try {
if (onStartLoading != null) onStartLoading();
final http.Response response =
await http.post(ApiUrl.GET_FAVOURITE_PROPERTY_LIST, body: jsons);
print("URL: " + response.request!.url.toString());
if (onStopLoading != null) onStopLoading();
print("jobsResponse" + response.body);
return Pr
Top comments (0)