Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return BlocProvider<SettingsBloc>(
- bloc: SettingsBloc(),
- child: MaterialApp(
- theme: ThemeData(
- primarySwatch: Colors.red,
- ),
- title: APP_TITLE,
- debugShowCheckedModeBanner: false,
- home: SyncWrapper(),
- ),
- );
- }
- }
- class SyncWrapper extends StatefulWidget {
- @override
- _SyncWrapperState createState() => _SyncWrapperState();
- }
- class _SyncWrapperState extends State<SyncWrapper> {
- AbastecimentoHelper _abHelper;
- @override
- void initState() {
- super.initState();
- _abHelper = AbastecimentoHelper();
- _abHelper.syncPendentes();
- }
- @override
- Widget build(BuildContext context) {
- final bloc = BlocProvider.of<SettingsBloc>(context);
- return StreamBuilder<SettingsState>(
- stream: bloc.outState,
- builder: (context, snapshot) {
- if (snapshot.data != SettingsState.DONE) {
- return Center(
- child: CircularProgressIndicator(),
- );
- }
- return HomeScreen();
- },
- );
- }
- @override
- void dispose() {
- _abHelper.close();
- super.dispose();
- }
- }
- class AbastecimentoHelper {
- static final AbastecimentoHelper _instance = AbastecimentoHelper.internal();
- factory AbastecimentoHelper() => _instance;
- AbastecimentoHelper.internal();
- Database _db;
- Future<Database> get db async {
- if (_db != null) return _db;
- _db = await initDb();
- return _db;
- }
- Future<Database> initDb() async {
- final databasesPath = await getDatabasesPath();
- final path = join(databasesPath, 'maissync.db');
- return openDatabase(path, version: 1,
- onCreate: (Database db, int newerVersion) async {
- String schema = "CREATE TABLE $abastecimentoTable("
- "$stringColumn TEXT NOT NULL,"
- "$dataLeituraColumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,"
- "$dataSincronizadoColumn TIMESTAMP DEFAULT NULL"
- ")";
- await db.execute(schema);
- });
- }
- Future close() async {
- Database dbAbastecimento = await db;
- dbAbastecimento.close();
- }
- Future<void> syncPendentes() async {
- List pendentes = await getPendentes();
- if (pendentes.length > 0) {
- try {
- String body = json.encode({"abastecimentos": pendentes});
- var response = await http.post(API_ENDPOINT_ABASTECIMENTOS, body: body);
- if (response.statusCode == 200) {
- List<int> ids = [];
- String whereStr = "";
- for (var p in pendentes) {
- ids.add(p["rowid"]);
- whereStr += "?, ";
- }
- whereStr = "(${whereStr.substring(0, whereStr.length - 2)})";
- Database dbAbastecimento = await db;
- await dbAbastecimento.update(
- abastecimentoTable,
- {"$dataSincronizadoColumn": nowStr()},
- where: "$rowIdColumn IN $whereStr",
- whereArgs: ids,
- );
- } else {
- debugLog(
- file: "helpers/abastecimento-helper.dart",
- method: "syncPendentes",
- location: "Falha na requisição",
- message: "Falha na requisição... Status: ${response.statusCode}.",
- );
- }
- } catch (e) {
- debugLog(
- file: "helpers/abastecimento-helper.dart",
- method: "syncPendentes",
- location: "Erro endpoint/desconhecido",
- message: "Falha desconhecida na requisição HTTP...\n"
- "\t\t- Provavelmente o Endpoint não está respondendo!",
- );
- }
- }
- // Interval
- Future.delayed(SQLITE_CHECK_INTERVAL).then((_) {
- syncPendentes();
- });
- }
- Future<List> getPendentes() async {
- Database dbAbastecimento = await db;
- List listMap = await dbAbastecimento.query(
- abastecimentoTable,
- columns: [rowIdColumn, stringColumn],
- where: "$dataSincronizadoColumn IS NULL",
- orderBy: "$rowIdColumn",
- limit: SYNC_ROWS,
- );
- return listMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment