288 lines
14 KiB
Dart
288 lines
14 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:rkm/screens/home_screen.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/api/api_data_import_sigtap.dart';
|
||
import '../service/db/db_sqlite_Sigtap.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 = '';
|
||
int AREA_ID = 0;
|
||
int AREA_DESCRICAO = 0;
|
||
int MICROAREA_ID = 0;
|
||
int MICROAREA_DESCRICAO = 0;
|
||
bool foraArea = false;
|
||
int foraAreaInt = 0;
|
||
String urlAPI='';
|
||
String mode = '';
|
||
String nameCityApi = '';
|
||
String nameCityFront = '';
|
||
String user = '';
|
||
int codCity = 0;
|
||
int codCityIbge = 0;
|
||
String codState = '';
|
||
double countProg = 0.0;
|
||
|
||
@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!=''){
|
||
print('chegou');
|
||
dataUser();
|
||
}
|
||
});
|
||
}
|
||
|
||
dataUser()async{
|
||
_prefService.readCacheUnidade().then((listUnidade)async{
|
||
print('listUnidade');
|
||
print(listUnidade);
|
||
_prefService.readCacheSync().then((list){
|
||
print('datauser');
|
||
print(list);
|
||
AREA_ID = list[0];
|
||
AREA_DESCRICAO = list[1];
|
||
MICROAREA_ID = list[2];
|
||
MICROAREA_DESCRICAO = list[3];
|
||
foraArea = list[9];
|
||
foraAreaInt = foraArea?1:0;
|
||
idUnidade = list[5].toString();
|
||
nomeUnidade = listUnidade[1];
|
||
initSync();
|
||
});
|
||
});
|
||
}
|
||
|
||
initSync()async{
|
||
await DbSqliteSync().deleteSync().then((_)async{
|
||
await ApiDataSync().syncPerm(urlAPI,mode,nameCityApi,user).then((response)async{
|
||
countProg = 0.1;
|
||
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{
|
||
countProg = 0.2;
|
||
setState(() {});
|
||
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{
|
||
countProg =0.3;
|
||
setState(() {});
|
||
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{
|
||
countProg = 0.4;
|
||
setState(() {});
|
||
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{
|
||
countProg = 0.5;
|
||
setState(() {});
|
||
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{
|
||
countProg = 0.6;
|
||
setState(() {});
|
||
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{
|
||
countProg = 0.7;
|
||
setState(() {});
|
||
await ApiDataSync().syncCidadaos(urlAPI,mode,nameCityApi,user,idUnidade,AREA_ID,MICROAREA_ID,foraAreaInt).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{
|
||
countProg = 0.8;
|
||
setState(() {});
|
||
await ApiDataSync().syncCadastrosDomiciliares(urlAPI,mode,nameCityApi,user,idUnidade,AREA_ID,MICROAREA_ID,foraAreaInt).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{
|
||
countProg = 0.9;
|
||
setState(() {});
|
||
await ApiDataSync().syncCadastrosIndividuais(urlAPI,mode,nameCityApi,user,idUnidade,AREA_ID,MICROAREA_ID,foraAreaInt).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_ID,AREA_DESCRICAO,MICROAREA_ID,MICROAREA_DESCRICAO).then((value)async{
|
||
countProg = 1;
|
||
setState(() {});
|
||
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>HomeScreen(username: user)));
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
});
|
||
});
|
||
}
|
||
|
||
@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: 70,
|
||
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,
|
||
),
|
||
SizedBox(height: 30),
|
||
TextDefault(text: countProg>0.94?'100 % finalizando etapa atual':(countProg*100).toStringAsFixed(0) + '%',color: Colors.black,textAlign: TextAlign.center,fontWeigth: FontWeight.bold,fontSize: 20),
|
||
SizedBox(height: 10),
|
||
LinearProgressIndicator(value: countProg,minHeight: 15,color: ColorPalette.appBar,backgroundColor: ColorPalette.titleContainer,)
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|