255 lines
12 KiB
Dart
255 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:rkm/screens/login/sync_login_screen.dart';
|
||
import 'package:rkm/service/api/api_data_sync.dart';
|
||
import 'package:rkm/service/db/db_sqlite_sync.dart';
|
||
import 'package:rkm/config/color_palette.dart';
|
||
import 'package:rkm/widgets/alert_dialog_default.dart';
|
||
import 'package:rkm/widgets/button_default.dart';
|
||
import 'package:rkm/widgets/text_default.dart';
|
||
import '../service/db/db_sqlite_load.dart';
|
||
import '../service/shared_preferences/shared_preferences_service.dart';
|
||
|
||
class SyncInitScreen extends StatefulWidget {
|
||
|
||
@override
|
||
State<SyncInitScreen> createState() => _SyncInitScreenState();
|
||
}
|
||
|
||
class _SyncInitScreenState extends State<SyncInitScreen> {
|
||
|
||
final PrefService _prefService = PrefService();
|
||
String messageSteps = 'Aguarde ...';
|
||
bool finish = false;
|
||
String idUsuario = '';
|
||
String idUnidade = '';
|
||
String nomeUnidade = '';
|
||
String area = '';
|
||
String microarea = '';
|
||
bool foraArea = false;
|
||
String urlAPI='';
|
||
String mode = '';
|
||
String nameCityApi = '';
|
||
String nameCityFront = '';
|
||
String user = '';
|
||
int codCity = 0;
|
||
int codCityIbge = 0;
|
||
String codState = '';
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
recupDB();
|
||
}
|
||
|
||
recupDB()async{
|
||
await DbSqliteLoad().intialDB().then((db)async{
|
||
List list = await db.query('ARGS');
|
||
urlAPI = list[0]['URLAPI'];
|
||
mode = list[0]['MODE'];
|
||
nameCityApi = list[0]['NAME_CITY_API'];
|
||
nameCityFront = list[0]['NAME_CITY_FRONT'];
|
||
user = list[0]['USER'];
|
||
codCity = list[0]['COD_CITY'];
|
||
codState = list[0]['COD_STATE'];
|
||
codCityIbge = list[0]['COD_IBGE'];
|
||
if(urlAPI!=''){
|
||
dataUser();
|
||
}
|
||
});
|
||
}
|
||
|
||
dataUser()async{
|
||
_prefService.readCacheUnidade('idUnidade').then((listUnidade)async{
|
||
_prefService.readCacheSync('area', false).then((list){
|
||
foraArea = list[1];
|
||
List splited = list[0].split(': ');
|
||
area = splited[2].toString().substring(0,1);
|
||
microarea = splited[3];
|
||
idUnidade = listUnidade[0];
|
||
nomeUnidade = listUnidade[1];
|
||
initSync();
|
||
});
|
||
});
|
||
}
|
||
|
||
initSync()async{
|
||
await DbSqliteSync().deleteSync().then((_)async{
|
||
await ApiDataSync().syncPerm(urlAPI,mode,nameCityApi,user).then((response)async{
|
||
if(response['status'] == 0 || response['sync_perm'] != "S"){
|
||
AlertDialogDefault().alert(context, 'Erro', 'Usuário sem permissão para Sincronização de Unidade!', 'Voltar');
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 1 de 10 - Importando informações do CONECTASUS");
|
||
await ApiDataSync().syncBairros(urlAPI,mode,nameCityApi,user).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Bairros - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 2 de 10 - Gravando os Bairros no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertBairros(value).then((value)async{
|
||
await ApiDataSync().syncTiposLogradouros(urlAPI,mode,nameCityApi,user).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Tipos de Logradouros - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 3 de 10 - Gravando os Tipos de Logradouros no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertTiposLogradouros(value).then((value)async{
|
||
await ApiDataSync().syncLogradouros(urlAPI,mode,nameCityApi,user).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Logradouros - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 4 de 10 - Gravando os Logradouros no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertLogradouros(value).then((value)async{
|
||
await ApiDataSync().syncLoteamentos(urlAPI,mode,nameCityApi,user).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Loteamentos - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 5 de 10 - Gravando os Loteamentos no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertLoteamentos(value).then((value)async{
|
||
await ApiDataSync().syncProfissionais(urlAPI,mode,nameCityApi,user,idUnidade).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Profissionais - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 6 de 10 - Gravando os Profissionais no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertProfissionais(value).then((value)async{
|
||
await ApiDataSync().syncProfissionaisCBOs(urlAPI,mode,nameCityApi,user,idUnidade).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os CBOs dos Profissionais - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 7 de 10 - Gravando os CBO´s dos Profissionais no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertProfissionaisCBOs(value).then((value)async{
|
||
await ApiDataSync().syncCidadaos(urlAPI,mode,nameCityApi,user,idUnidade,area,microarea,foraArea).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Cadastros dos Cidadãos - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 8 de 10 - Gravando os Cidadãos no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertCidadaos(value,'sync','').then((value)async{
|
||
await ApiDataSync().syncCadastrosDomiciliares(urlAPI,mode,nameCityApi,user,idUnidade,area,microarea,foraArea).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Cadastros Domiciliares - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 9 de 10 - Gravando os Cadastros Domiciliares no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertCadastrosDomiciliares(value,'sync',0,0,'').then((value)async{
|
||
await ApiDataSync().syncCadastrosIndividuais(urlAPI,mode,nameCityApi,user,idUnidade,area,microarea,foraArea).then((value)async{
|
||
if(value.length == 0){
|
||
setState(()=> messageSteps = "ERRO: Erro para carregar os Cadastros Individuais - Por favor, tente novamente!");
|
||
}else{
|
||
setState(()=> messageSteps = "Etapa 10 de 10 - Gravando os Cadastros Individuais no Aplicativo");
|
||
}
|
||
await DbSqliteSync().insertCadastrosIndividuais(value,'sync',0).then((value)async{
|
||
await DbSqliteSync().saveSync(codCity,codState,urlAPI,mode,nameCityApi,nameCityFront,codCityIbge,user,idUnidade,nomeUnidade,area,microarea).then((value)async{
|
||
Navigator.pushNamed(context, '/home');
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
|
||
double width = MediaQuery.of(context).size.width;
|
||
double height = MediaQuery.of(context).size.height;
|
||
|
||
return Scaffold(
|
||
bottomSheet: Container(
|
||
width: width,
|
||
height: height*0.05,
|
||
color: ColorPalette.appBar,
|
||
child: finish
|
||
?Row(
|
||
children: [
|
||
Spacer(),
|
||
ButtonDefault(
|
||
onPressed: (){
|
||
Navigator.pushReplacement(
|
||
context,
|
||
MaterialPageRoute(builder: (context) => SyncLoginScreen()),
|
||
);
|
||
},
|
||
title: 'Continuar',
|
||
width: width*0.3,
|
||
height: height*0.1,
|
||
textColor: ColorPalette.white,
|
||
background: ColorPalette.appBar,
|
||
borderColor: ColorPalette.appBar,
|
||
),
|
||
],
|
||
)
|
||
:Padding(
|
||
padding: const EdgeInsets.all(10.0),
|
||
child: TextDefault(
|
||
fontSize: 15,
|
||
text: messageSteps,
|
||
color: ColorPalette.white,
|
||
maxLines: 2,
|
||
),
|
||
),
|
||
),
|
||
body: Container(
|
||
padding: EdgeInsets.all(50),
|
||
width: width,
|
||
height: height,
|
||
decoration: BoxDecoration(
|
||
color: ColorPalette.grayBackground,
|
||
image: DecorationImage(
|
||
fit: BoxFit.fitHeight,
|
||
image: AssetImage('assets/images/load_image.png'
|
||
),
|
||
)
|
||
),
|
||
child: finish?ListView(
|
||
children: [
|
||
Image.asset('assets/images/logo.PNG',width: width*0.3,height: height*0.2,),
|
||
Image.asset('assets/images/load-done.png',width: width*0.9,height: height*0.2,),
|
||
],
|
||
):ListView(
|
||
children: [
|
||
Image.asset('assets/images/logo.PNG',width: width*0.2,height: height*0.1,),
|
||
SizedBox(height: height*0.1),
|
||
Image.asset('assets/images/loading.gif',width: width*0.2,height: height*0.1,),
|
||
SizedBox(height: height*0.1),
|
||
TextDefault(
|
||
text: 'Aguarde enquanto a Sincronização da Unidade é realizada.\n',
|
||
textAlign: TextAlign.center,
|
||
maxLines: 2,
|
||
color: Colors.black,
|
||
fontWeigth: FontWeight.bold,
|
||
),
|
||
TextDefault(
|
||
text: 'Esse processo pode levar vários minutos dependendo da sua conexão!',
|
||
color: Colors.black,
|
||
fontWeigth: FontWeight.bold,
|
||
textAlign: TextAlign.center,
|
||
maxLines: 3,
|
||
)
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|