media_view_model.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'dart:convert';
  2. import 'dart:ui' as ui;
  3. import 'dart:io';
  4. import 'package:image/image.dart' as img;
  5. import 'package:camera/camera.dart';
  6. import 'package:file_picker/file_picker.dart';
  7. import 'package:flutter/foundation.dart';
  8. import "package:universal_html/html.dart" as html;
  9. import 'package:flutter/foundation.dart' show kIsWeb;
  10. import '../models/media_model.dart';
  11. import '../services/base_service.dart';
  12. class MediaViewModel extends ChangeNotifier {
  13. List<XFile> _audios = [];
  14. List<XFile> get audios => _audios;
  15. List<XFile> _temporales = [];
  16. List<XFile> get temporales => _temporales;
  17. List<XFile> _archivos = [];
  18. List<XFile> get archivos => _archivos;
  19. bool _isLoading = false;
  20. bool get isLoading => _isLoading;
  21. XFile? _firma;
  22. XFile? get firma => _firma;
  23. XFile? _firmaChofer;
  24. XFile? get firmaChofer => _firmaChofer;
  25. XFile? _firmaSupervisor;
  26. XFile? get firmaSupervisor => _firmaSupervisor;
  27. XFile? getFirmaChofer() {
  28. return _firmaChofer;
  29. }
  30. XFile? getFirmaSupervisor() {
  31. return _firmaSupervisor;
  32. }
  33. Future<void> fetchAudio() async {
  34. _temporales = [];
  35. notifyListeners();
  36. }
  37. setFirma(XFile value) {
  38. _firma = value;
  39. notifyListeners();
  40. }
  41. setFirmaChofer(XFile value) {
  42. _firmaChofer = value;
  43. notifyListeners();
  44. }
  45. setFirmaSupervisor(XFile value) {
  46. _firmaSupervisor = value;
  47. notifyListeners();
  48. }
  49. Future agregarAudio(XFile value) async {
  50. _audios.add(value);
  51. notifyListeners();
  52. }
  53. Future limpiar() async {
  54. _audios = [];
  55. _temporales = [];
  56. _archivos = [];
  57. _firma = null;
  58. _firmaChofer = null;
  59. _firmaSupervisor = null;
  60. notifyListeners();
  61. }
  62. void setIsLoading(bool loading) {
  63. _isLoading = loading;
  64. notifyListeners();
  65. }
  66. Future guardarAudio(
  67. {required int idModuloLocal,
  68. File? audio,
  69. required String nombreModulo}) async {}
  70. Future<XFile?> convertirPlatformFileAXFile(PlatformFile platformFile) async {
  71. if (!kIsWeb) {
  72. final XFile xFile = XFile(platformFile.path.toString(),
  73. name: platformFile.name, mimeType: "archivo");
  74. return xFile;
  75. }
  76. try {
  77. final Uint8List bytes = platformFile.bytes!;
  78. final html.Blob blob = html.Blob([bytes], 'application/octet-stream');
  79. String url = html.Url.createObjectUrlFromBlob(blob);
  80. final XFile xFile =
  81. XFile(url.toString(), name: platformFile.name, mimeType: "archivo");
  82. return xFile;
  83. } catch (e) {
  84. print('Error al convertir PlatformFile a XFile: $e');
  85. return null;
  86. }
  87. }
  88. void agregarArchivo(XFile? value) {
  89. if (value != null) {
  90. _archivos.add(value);
  91. notifyListeners();
  92. }
  93. }
  94. Future<XFile> convertirAImagePickerWebFile(ui.Image image) async {
  95. ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  96. Uint8List pngBytes = byteData!.buffer.asUint8List();
  97. final tempDir = Directory.systemTemp;
  98. String uniqueFileName =
  99. '${tempDir.path}/temp_image_${DateTime.now().millisecondsSinceEpoch}.png';
  100. final tempFile = await File(uniqueFileName).writeAsBytes(pngBytes);
  101. return XFile(tempFile.path);
  102. }
  103. Future<void> eliminar(Media m) async {
  104. var r = await BaseService()
  105. .delete("admin/media", body: {"id": m.id.toString()});
  106. Map<String, dynamic> resJson = jsonDecode(r.body);
  107. if (r.statusCode == 200) {}
  108. }
  109. }