236 lines
8.7 KiB
Dart
236 lines
8.7 KiB
Dart
import 'dart:io';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:aman_kassa_flutter/core/models/dialog_models.dart';
|
||
import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart';
|
||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||
import 'package:aman_kassa_flutter/core/locator.dart';
|
||
import 'package:aman_kassa_flutter/core/logger.dart';
|
||
import 'package:aman_kassa_flutter/core/route_names.dart';
|
||
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
|
||
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
|
||
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
||
import 'package:aman_kassa_flutter/redux/state/setting_state.dart';
|
||
import 'package:aman_kassa_flutter/redux/store.dart';
|
||
import 'package:aman_kassa_flutter/shared/app_colors.dart';
|
||
import 'package:aman_kassa_flutter/widgets/fields/aman_icon_button_horizontal.dart';
|
||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import 'package:aman_kassa_flutter/views/settings/printer/PrinterTest.dart';
|
||
import 'package:flutter_redux/flutter_redux.dart';
|
||
import 'package:logger/logger.dart';
|
||
|
||
import 'component/setting_item.dart';
|
||
import './printer/data/settings_envi.dart';
|
||
import './printer/example/check_test.dart';
|
||
|
||
class SettingPrinterView extends StatefulWidget {
|
||
@override
|
||
_SettingPrinterViewState createState() => _SettingPrinterViewState();
|
||
}
|
||
|
||
class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||
NavigatorService _navigatorService = locator<NavigatorService>();
|
||
final DialogService _dialogService = locator<DialogService>();
|
||
final PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||
final BluetoothManager bluetoothManager = BluetoothManager.instance;
|
||
final Logger log = getLogger('SettingPrinterView');
|
||
|
||
bool _printing = false;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_permission();
|
||
|
||
}
|
||
|
||
|
||
void _testPrint() async {
|
||
setState(() {
|
||
_printing = true;
|
||
});
|
||
try {
|
||
final SettingState state = Redux.store.state.settingState;
|
||
printerManager.selectPrinter(PrinterBluetooth(state.printerBT));
|
||
bool isIos = Platform.isIOS;
|
||
int chunkSizeBytes = 3096;
|
||
int queueSleepTimeMs = 100;
|
||
|
||
if (isIos) {
|
||
chunkSizeBytes = 75;
|
||
queueSleepTimeMs = 10;
|
||
}
|
||
|
||
log.i(chunkSizeBytes);
|
||
log.i(queueSleepTimeMs);
|
||
|
||
// TODO Don't forget to choose printer's paper
|
||
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58;
|
||
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
||
final PosPrintResult res = await printerManager.printTicket(
|
||
await testTicketImage(paper),
|
||
chunkSizeBytes: chunkSizeBytes,
|
||
queueSleepTimeMs: queueSleepTimeMs
|
||
);
|
||
_dialogService.showDialog(description: res.msg);
|
||
} else {
|
||
final PosPrintResult res = await printerManager.printTicket(
|
||
await printTextCheck(paper, state.printerEncoding, exampleJson['check_text']),
|
||
chunkSizeBytes: chunkSizeBytes,
|
||
queueSleepTimeMs: queueSleepTimeMs
|
||
);
|
||
_dialogService.showDialog(description: res.msg);
|
||
}
|
||
} catch (e) {
|
||
print('ERROR');
|
||
print(e);
|
||
}
|
||
|
||
//7 sec safe disconnect
|
||
await Future.delayed(Duration(seconds: 7));
|
||
|
||
setState(() {
|
||
_printing = false;
|
||
});
|
||
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: Text('Настройка принтера'),
|
||
),
|
||
body: Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
||
child: StoreConnector<AppState, SettingState>(
|
||
converter: (store) => store.state.settingState,
|
||
builder: (context, vm) {
|
||
return Column(
|
||
children: [
|
||
SettingItem(
|
||
title: 'Принтер',
|
||
name: vm.printerBT?.name,
|
||
value: vm.printerBT != null
|
||
? 'BT: ${vm.printerBT.address} '
|
||
: 'не выбран',
|
||
onTap: () {
|
||
_navigatorService.push(SettingsPrinterBTRoute);
|
||
}),
|
||
SettingItem(
|
||
title: 'Кодировка',
|
||
name: vm.printerEncoding != null ? encoding[vm.printerEncoding] : null ,
|
||
onTap: () {
|
||
_navigatorService.push(SettingsPrinterEncodingRoute);
|
||
}),
|
||
SettingItem(
|
||
title: 'Ширина ленты',
|
||
name: vm.printerPaperSize != null ? paperSize[vm.printerPaperSize] : null ,
|
||
onTap: () {
|
||
_navigatorService.push(SettingsPrinterPaperRoute);
|
||
}),
|
||
Expanded(
|
||
child: Padding(
|
||
padding: const EdgeInsets.only(bottom: 24.0),
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.end,
|
||
children: [
|
||
BusyButtonIcon(
|
||
icon: Icons.local_printshop_outlined,
|
||
title: 'Напечатать тестовую страницу',
|
||
busy: _printing,
|
||
enabled: vm.printerBT != null,
|
||
onPressed: () {
|
||
|
||
_startInitialPrint();
|
||
|
||
},
|
||
),
|
||
],
|
||
),
|
||
),
|
||
)
|
||
],
|
||
);
|
||
}),
|
||
),
|
||
);
|
||
}
|
||
|
||
//Метод для получения постоянного доступа к местополения
|
||
//только для Android
|
||
void _permission() async {
|
||
if( Platform.isAndroid) {
|
||
var statusLocation = await Permission.location.status;
|
||
var statusLocationAlways = await Permission.locationAlways.status;
|
||
log.i(statusLocation);
|
||
log.i(statusLocationAlways);
|
||
if (
|
||
statusLocation.isUndetermined
|
||
|| statusLocation.isDenied
|
||
|| statusLocation.isPermanentlyDenied
|
||
|| statusLocationAlways.isUndetermined
|
||
|| statusLocationAlways.isDenied
|
||
|| statusLocationAlways.isPermanentlyDenied
|
||
) {
|
||
DialogResponse response = await _dialogService.showConfirmationDialog(
|
||
title: 'Доступ',
|
||
description: 'Для поиска устройств Bluetooth необходимо предоставить доступ к собиру данных о местоположении, даже когда приложение закрыто или не используется.',
|
||
cancelTitle: 'Нет',
|
||
confirmationTitle: 'Хорошо',
|
||
);
|
||
if (response.confirmed) {
|
||
|
||
PermissionStatus location = await Permission.location
|
||
.request();
|
||
|
||
if (location.isGranted ) {
|
||
PermissionStatus locationAlways = await Permission.locationAlways
|
||
.request();
|
||
if(locationAlways.isGranted) {
|
||
print('Granted');
|
||
} else {
|
||
_dialogService.showDialog(
|
||
description: 'Необходимо предоставить постоянный доступ к местоположении для поиска принтера, даже когда приложение закрыто или не используется.');
|
||
_navigatorService.pop();
|
||
}
|
||
} else {
|
||
_dialogService.showDialog(
|
||
description: 'Необходимо предоставить доступ к местоположении для поиска принтера, даже когда приложение закрыто или не используется.');
|
||
_navigatorService.pop();
|
||
}
|
||
} else {
|
||
|
||
_navigatorService.pop();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void _startInitialPrint() async {
|
||
|
||
|
||
if (Platform.isIOS) {
|
||
await _testPrint();
|
||
} else {
|
||
bluetoothManager.state.listen((val) {
|
||
print("state = $val");
|
||
if (!mounted) return;
|
||
if (val == 12) {
|
||
print('on');
|
||
_testPrint();
|
||
|
||
} else if (val == 10) {
|
||
print('off');
|
||
_dialogService.showDialog(
|
||
description: 'Отсутвует соеденение Bluetooth или он отключен' , title: 'Bluetooth');
|
||
}
|
||
print('state is $val');
|
||
});
|
||
}
|
||
}
|
||
}
|