Advertisement
danielbrito1987

categoria-bloc.dart

Apr 3rd, 2020
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.52 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:PhotoReport/helpers/categoria_helper.dart';
  4. import 'package:PhotoReport/helpers/http_helper.dart';
  5. import 'package:PhotoReport/repository/categoria_repository.dart';
  6. import 'package:dio/dio.dart';
  7. import 'package:flutter/widgets.dart';
  8. import 'package:rxdart/rxdart.dart';
  9. import 'package:shared_preferences/shared_preferences.dart';
  10.  
  11. import '../global.dart';
  12.  
  13. enum CategoriaState { IDLE, LOADING, SUCCESS, FAIL }
  14.  
  15. class CategoriaBloc extends ChangeNotifier {
  16.   final CategoriaRepository _repository = CategoriaRepository();
  17.   final BehaviorSubject<CategoriaResponse> _subject =
  18.       BehaviorSubject<CategoriaResponse>();
  19.   final BehaviorSubject<bool> _subjectSalvar = BehaviorSubject<bool>();
  20.   final BehaviorSubject<bool> _subjectAlterar = BehaviorSubject<bool>();
  21.   final BehaviorSubject<bool> _subjectDeletar = BehaviorSubject<bool>();
  22.   final _stateController = BehaviorSubject<CategoriaState>();
  23.   Http _http = Http();
  24.   List<Categoria> categorias = List();
  25.  
  26.   CategoriaBloc({int buildingId}) {
  27.     if (buildingId != null) obterCategorias(buildingId);
  28.   }
  29.  
  30.   getCategorias(int buildingId) async {
  31.     CategoriaResponse response = await _repository.getCategoria(buildingId);
  32.     _stateController.add(CategoriaState.SUCCESS);
  33.     notifyListeners();
  34.     _subject.sink.add(response);
  35.   }
  36.  
  37.   obterCategorias(int buildingId) async {
  38.     categorias = List();
  39.     _stateController.add(CategoriaState.LOADING);
  40.     SharedPreferences prefs = await SharedPreferences.getInstance();
  41.  
  42.     _http
  43.         .get(API_URL + "categories?building=$buildingId")
  44.         .then((response) async {
  45.       Map<String, dynamic> retorno = response.data;
  46.  
  47.       if (!retorno["error"]) {
  48.         var lista = retorno["data"];
  49.  
  50.         for (Map m in lista) {
  51.           var imagens = await obterImagensCategoria(m["id"]);
  52.  
  53.           Categoria categoria = Categoria.fromMap(m);
  54.           categoria.qtdImagens = imagens.length;
  55.  
  56.           categorias.add(categoria);
  57.         }
  58.  
  59.         prefs.setString("categories", jsonEncode(lista));
  60.  
  61.         _stateController.add(CategoriaState.SUCCESS);
  62.         notifyListeners();
  63.       } else {
  64.         _stateController.add(CategoriaState.FAIL);
  65.         notifyListeners();
  66.       }
  67.     }).catchError((onError) {
  68.       String json = prefs.getString("categories");
  69.       var lista = jsonDecode(json);
  70.  
  71.       for (Map m in lista) {
  72.         categorias.add(Categoria.fromMap(m));
  73.       }
  74.  
  75.       _stateController.add(CategoriaState.SUCCESS);
  76.       notifyListeners();
  77.     });
  78.   }
  79.  
  80.   Future<List> obterImagensCategoria(int categoriaId) async {
  81.     _stateController.add(CategoriaState.LOADING);
  82.     notifyListeners();
  83.  
  84.     Response responseImages =
  85.         await _http.get(API_URL + "categories/$categoriaId/images");
  86.     Map<String, dynamic> retornoImages = responseImages.data;
  87.  
  88.     var lista = retornoImages["data"];
  89.  
  90.     _stateController.add(CategoriaState.SUCCESS);
  91.     notifyListeners();
  92.  
  93.     return lista;
  94.   }
  95.  
  96.   salvar(Categoria categoria) async {
  97.     _stateController.add(CategoriaState.LOADING);
  98.  
  99.     bool response = await _repository.salvar(categoria);
  100.  
  101.     if (response) {
  102.       //await obterCategorias(categoria.obraId);
  103.     } else {
  104.       _stateController.add(CategoriaState.FAIL);
  105.       notifyListeners();
  106.     }
  107.  
  108.     _subjectSalvar.sink.add(response);
  109.   }
  110.  
  111.   alterar(Categoria categoria) async {
  112.     _stateController.add(CategoriaState.LOADING);
  113.  
  114.     bool response = await _repository.alterar(categoria);
  115.  
  116.     if (response) {
  117.       obterCategorias(categoria.obraId);
  118.     } else {
  119.       _stateController.add(CategoriaState.FAIL);
  120.       notifyListeners();
  121.     }
  122.  
  123.     _subjectAlterar.sink.add(response);
  124.   }
  125.  
  126.   deletar(Categoria categoria) async {
  127.     _stateController.add(CategoriaState.LOADING);
  128.  
  129.     bool response = await _repository.deletar(categoria);
  130.  
  131.     if (response) {
  132.       obterCategorias(categoria.obraId);
  133.     } else {
  134.       _stateController.add(CategoriaState.FAIL);
  135.       notifyListeners();
  136.     }
  137.  
  138.     _subjectDeletar.sink.add(response);
  139.   }
  140.  
  141.   @override
  142.   void dispose() {
  143.     _subject.close();
  144.     _subjectSalvar.close();
  145.     _subjectAlterar.close();
  146.     _subjectDeletar.close();
  147.     _stateController.close();
  148.   }
  149.  
  150.   BehaviorSubject<CategoriaResponse> get subject => _subject;
  151.   BehaviorSubject<bool> get save => _subjectSalvar;
  152.   BehaviorSubject<bool> get update => _subjectAlterar;
  153.   BehaviorSubject<bool> get delete => _subjectDeletar;
  154.   Stream<CategoriaState> get outState => _stateController.stream;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement