You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0KB

  1. import 'package:bloc/bloc.dart';
  2. import 'package:dio/dio.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:farm_tpf/presentation/screens/codes/bloc/stamp_bloc.dart';
  5. import 'package:farm_tpf/presentation/screens/codes/models/stamp_timeline.dart';
  6. import 'package:flutter/widgets.dart';
  7. import 'package:flutter_bloc/flutter_bloc.dart';
  8. import 'package:open_filex/open_filex.dart';
  9. import 'package:path_provider/path_provider.dart';
  10. import '../../../../data/api/app_exception.dart';
  11. import '../../../../data/api/dio_provider.dart';
  12. import '../../../../data/repository/repository.dart';
  13. import '../../../../utils/const_common.dart';
  14. import '../../../../utils/utils.dart';
  15. import '../../../custom_widgets/widget_utils.dart';
  16. import '../models/stamp.dart';
  17. part 'detail_stamp_state.dart';
  18. class DetailStampCubit extends Cubit<DetailStampState> {
  19. final repository = Repository();
  20. DetailStampCubit() : super(DetailStampInitial());
  21. static String pathDownload = 'download';
  22. String currentLocalPath = '';
  23. Future<void> preparedData(int stampId) async {
  24. try {
  25. await Future.delayed(const Duration(seconds: 0));
  26. emit(DetailStampLoading());
  27. var stamp = await repository.getStampDetail(id: stampId);
  28. var timeline = await repository.getStampTimeline(stampId: stampId);
  29. emit(DetailStampSuccessful(stamp, timeline));
  30. } catch (e) {
  31. emit(DetailStampFailure(AppException.handleError(e)));
  32. }
  33. }
  34. Future<void> updateStampStatus({
  35. required int stampId,
  36. required String status,
  37. }) async {
  38. UtilWidget.showLoading();
  39. await repository.updateStampStatus(
  40. (success) {
  41. UtilWidget.hideDialog();
  42. Utils.showSnackBarSuccess();
  43. preparedData(stampId);
  44. },
  45. (errorMessage) {
  46. UtilWidget.hideDialog();
  47. Utils.showSnackBarError();
  48. },
  49. stampId: stampId,
  50. status: status,
  51. );
  52. }
  53. Future downloadFile(String stampCode) async {
  54. try {
  55. var dio = DioProvider();
  56. print(DateTime.now().millisecondsSinceEpoch);
  57. var fileName = '${DateTime.now().millisecondsSinceEpoch.toString()}.xlsx';
  58. currentLocalPath = await getFilePath(fileName);
  59. UtilWidget.showLoading();
  60. await dio.download(
  61. '${ConstCommon.baseUrl}/api/tb-codes/export?tbCodeValue=$stampCode',
  62. currentLocalPath,
  63. onReceiveProgress: (rec, total) {},
  64. );
  65. UtilWidget.hideDialog();
  66. await onOpenFile();
  67. } catch (e) {
  68. UtilWidget.hideDialog();
  69. Utils.showSnackBarError(message: 'Không thể mở tập tin');
  70. print(e.toString());
  71. }
  72. }
  73. Future<void> onOpenFile() async {
  74. var result = await OpenFilex.open(currentLocalPath);
  75. if (result.type == ResultType.done) {
  76. //can open
  77. } else {
  78. Utils.showSnackBarError(message: 'Không thể mở tập tin');
  79. }
  80. }
  81. Future<String> getFilePath(uniqueFileName) async {
  82. var dir = await getApplicationDocumentsDirectory();
  83. var path = '${dir.path}/$pathDownload/$uniqueFileName';
  84. print(path);
  85. return path;
  86. }
  87. }