Untitled
unknown
plain_text
4 years ago
14 kB
6
Indexable
import 'dart:async';
import 'dart:convert';
import 'dart:io'show Platform;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geocoding/geocoding.dart';
import 'package:one_on_us/src/interface/base_bloc.dart';
import 'package:one_on_us/src/network/modals/request/RegisterRequest.dart';
import 'package:one_on_us/src/network/modals/response/CountryResponse.dart';
import 'package:one_on_us/src/network/modals/response/LoginResponse.dart';
import 'package:one_on_us/src/network/utils/RestApiCalls.dart';
import 'package:one_on_us/src/network/utils/api_config.dart';
import 'package:one_on_us/src/ui/stateless/locationhelper.dart';
import 'package:one_on_us/src/utils/SharedPrefConstants.dart';
import 'package:one_on_us/src/utils/app_config.dart';
import 'package:one_on_us/src/utils/app_constants.dart';
import 'package:one_on_us/src/utils/app_defaults.dart';
import 'package:one_on_us/src/utils/app_utils.dart';
class RegisterBloc with BaseBloc {
BuildContext context;
AppConfig appconfig ;
Address first;
String third;
bool isLoggedIn;
List<Placemark> second;
RegisterBloc(BuildContext context) {
this.context = context;
appconfig = AppConfig();
getislogedin();
}
/// checking user login status
getislogedin()async{
isLoggedIn = await SharedPrefHelper()
.getWithDefault(SharedPrefConstants.isLoggedIn,false);
print("Get Logged In Called ");
}
/// Controllers
final _getCountryController = StreamController<List<CountryData>>.broadcast();
final _getCityController = StreamController<List<CountryData>>.broadcast();
final _getStateController = StreamController<List<CountryData>>.broadcast();
final _registerController = StreamController<LoginResponse>.broadcast();
/// Stream
Stream<List<CountryData>> get countryStream => _getCountryController.stream;
Stream<List<CountryData>> get cityStream => _getCityController.stream;
Stream<List<CountryData>> get stateStream => _getStateController.stream;
Stream<LoginResponse> get registerStream => _registerController.stream;
/// sink
StreamSink<List<CountryData>> get countrySink => _getCountryController.sink;
StreamSink<List<CountryData>> get citySink => _getCityController.sink;
StreamSink<List<CountryData>> get stateSink => _getStateController.sink;
StreamSink<LoginResponse> get registerSink => _registerController.sink;
/// Subscription
StreamSubscription<List<CountryData>> get taskSubscription =>
countryStream.listen((event) {});
StreamSubscription<List<CountryData>> get citySubscription =>
cityStream.listen((event) {});
StreamSubscription<LoginResponse> get registerSubscription =>
registerStream.listen((event) {});
StreamSubscription<List<CountryData>> get stateSubscription =>
stateStream.listen((event) {});
/// calling getCountryResponse method and adding data to the stream
/// on success response
getCountry() {
RestApiCalls apiCalls = RestApiCalls();
CommonUtils.isNetworkAvailable().then((value)async {
if (value) {
await apiCalls.getCountryResponse(context).then((value) {
if (value.status == 200) {
countrySink.add(value.data);
} else {
countrySink.addError(value.message);
}
}).catchError((error) {
countrySink.addError(ApiConstants.apiError);
});
} else {
countrySink.addError(AppConstants.INTERNET_NOT_CONNECTED);
}
});
}
/// calling getStateResponse method and adding data to the stream
/// on success response
getState(String countryId, {Map<String, dynamic> params}) {
RestApiCalls apiCalls = RestApiCalls();
CommonUtils.isNetworkAvailable().then((value) {
if (value) {
apiCalls.getStateResponse(context, countryId, params).then((value) {
if (value.status == 200) {
if(!_getCityController.isClosed){
citySink.add(value.data);
// for(int i=0 ; i<value.data.length; i++){
//
// }
print("getState is calling");
}
}
else if(value.status==400)
{
citySink.addError(value.message);
print("error" + value.message);
}
else {
citySink.addError(value.message);
print("error" + value.message);
}
}).catchError((error) {
citySink.addError(ApiConstants.apiError);
print("error" + error.toString());
});
} else {
citySink.addError(AppConstants.INTERNET_NOT_CONNECTED);
}
});
}
/// calling getCityResponse method and adding data to the stream
/// on success response
getCity(
String countryId,
{
int byDefaultCityId, Map<String, dynamic> params
}) {
RestApiCalls apiCalls = RestApiCalls();
CommonUtils.isNetworkAvailable().then((value) async {
if(isLoggedIn == false){
CommonUtils.fullScreenProgress(context);
}
if(AppConfig.cityLoaderForInvite=="true"){
CommonUtils.fullScreenProgress(context);
}
if(AppConfig.cityLoaderForChnageLocation=="true"){
CommonUtils.fullScreenProgress(context);
}
if (value) {
apiCalls.getCityResponse(context, countryId, params).then((value) async{
if(isLoggedIn == true){
second= await LocationService.getUserlocationthroughgeocoding();
//third = await LocationService.getAddressFromLatLngGoogleApi();
// print(third);
print("thirld value ");
}
else{}
if (value.status == 200) {
if(isLoggedIn == false){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForInvite=="true"){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForChnageLocation=="true"){
CommonUtils.dismissProgressDialog(context);
}
value.data.forEach((element) {
if (element.id == byDefaultCityId) {
element.isSelcted = true;
if(AppConfig.toReflectLocationlabel=="false" && isLoggedIn ==true){
appconfig.selectedindex.city = element.city;
}else{
}
}
else{
print('Inside else block for default location');
print(element.city.toLowerCase()+ " element.city");
if(isLoggedIn ==true){
if(
Platform.isIOS
){
if(
element.city.substring(0,2).toLowerCase().trim() == second[0].locality.substring(0,2).toLowerCase().trim()
){
print("get location if in register bloc working");
print(element.city.toLowerCase()+ "element.city");
print("tarun new location fetching ");
element.isSelcted = true;
if(AppConfig.toReflectLocationlabel=="false" && isLoggedIn ==true){
print("toReflectLocationlabelfalse");
appconfig.selectedindex.city = element.city;
}
else{
}
}
else{
print("get location else working");
}
}else{
if(
element.city.toLowerCase().trim() == second[0].locality.toLowerCase().trim()
){
print("get location if in register bloc working");
print(element.city.toLowerCase()+ "element.city");
print("tarun new location fetching ");
element.isSelcted = true;
if(AppConfig.toReflectLocationlabel=="false" && isLoggedIn ==true){
print("toReflectLocationlabelfalse");
appconfig.selectedindex.city = element.city;
}
else{
}
}
else{
print("get location else working");
}
}
}
}
});
if (value.data.length != 0){
if (value.data
.where((element) => element.isSelcted)
.toList()
.length ==
0) {
print("register bloc last condition working");
value.data[0].isSelcted = true;
print(AppConfig.toReflectLocationlabel.toString() + "Location label value");
print(isLoggedIn.toString() + "is login ");
if(AppConfig.toReflectLocationlabel=="false" &&
isLoggedIn == true
&&AppConfig.torelectFirstCityLocationLabel =="false")
{
print("register bloc last condition working if working");
appconfig.selectedindex.city = value.data[0].city;
print("city "+ value.data[0].city);
}
else if (
AppConfig.torelectFirstCityLocationLabel =="true" && isLoggedIn == true
&&AppConfig.toReflectLocationlabel=="false")
{
print("register bloc last condition working else if working");
appconfig.selectedindex.city = value.data[0].city;
print("city "+ value.data[0].city);
print("city appconfig"+ appconfig.selectedindex.city);
}
else if (
AppConfig.torelectFirstCityLocationLabel =="" && isLoggedIn == true
&& AppConfig.toReflectLocationlabel== "false"
)
{
print("register bloc last condition working else if working ...");
appconfig.selectedindex.city = value.data[0].city;
print("city "+ value.data[0].city);
print("city appconfig"+ appconfig.selectedindex.city);
print("register bloc last condition working else if first entry working");
}
else{
print("register bloc last condition working else working");
}
}
stateSink.add(value.data);
}else{
if(isLoggedIn == false){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForInvite=="true"){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForChnageLocation=="true"){
CommonUtils.dismissProgressDialog(context);
}
stateSink.addError("There is no city available for this state");
}
}
else {
if(isLoggedIn == false){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForInvite=="true"){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForChnageLocation=="true"){
CommonUtils.dismissProgressDialog(context);
}
stateSink.addError(value.message);
}
}).catchError((error) {
if(isLoggedIn == false){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForInvite=="true"){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForChnageLocation=="true"){
CommonUtils.dismissProgressDialog(context);
}
print("taru");
print(error.toString());
stateSink.addError(ApiConstants.apiError);
});
} else {
if(isLoggedIn == false){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForInvite=="true"){
CommonUtils.dismissProgressDialog(context);
}
if(AppConfig.cityLoaderForChnageLocation=="true"){
CommonUtils.dismissProgressDialog(context);
}
stateSink.addError(AppConstants.INTERNET_NOT_CONNECTED);
}
});
}
/// calling register method and adding data to the stream
/// on success response
register(RegisterRequest request) {
RestApiCalls apiCalls = RestApiCalls();
CommonUtils.fullScreenProgress(context);
apiCalls.register(context, jsonEncode(request)).then((value) {
CommonUtils.dismissProgressDialog(context);
registerSink.add(value);
}).catchError((error) {
CommonUtils.dismissProgressDialog(context);
registerSink.addError(ApiConstants.apiError);
});
}
@override
void dispose() {
_getCountryController?.close();
_getCityController?.close();
_registerController?.close();
_getStateController?.close();
}
}
Editor is loading...