Skip to content

Commit

Permalink
Added missing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey committed Jun 26, 2020
1 parent 444cc7e commit e246f0c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.tablemi.flutter_sunmi_printer;

import android.content.Context;
import android.util.Log;

import com.tablemi.flutter_sunmi_printer.utils.AidlUtil;
import com.tablemi.flutter_sunmi_printer.utils.Base64Utils;
import com.tablemi.flutter_sunmi_printer.utils.BitmapUtil;
import com.tablemi.flutter_sunmi_printer.utils.ESCUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FlutterSunmiPrinterModule {

private static boolean isPrinting = false;
Expand Down Expand Up @@ -75,7 +80,56 @@ public void text(String text, int align, boolean bold, boolean underline, int si
setFontSize(DEFAULT_FONT_SIZE);

// Reset styles
boldOff();
underlineOff();
if (bold) {
boldOff();
}
if (underline) {
underlineOff();
}
}

public void row(String colsStr, boolean bold, boolean underline, int textSize, int linesAfter) {
try {
// Set styles
if (bold) {
boldOn();
}
if (underline) {
underlineOn();
}

// Prepare row data
JSONArray cols = new JSONArray(colsStr);
String[] colsText = new String[cols.length()];
int[] colsWidth = new int[cols.length()];
int[] colsAlign = new int[cols.length()];
for (int i = 0; i < cols.length(); i++) {
JSONObject col = cols.getJSONObject(i);
String text = col.getString("text");
int width = col.getInt("width");
int align = col.getInt("align");
colsText[i] = text;
colsWidth[i] = width;
colsAlign[i] = align;
}

// Print row
setFontSize(textSize);
AidlUtil.getInstance().printTableItem(colsText, colsWidth, colsAlign);
if (linesAfter > 0) {
emptyLines(linesAfter);
}
setFontSize(DEFAULT_FONT_SIZE);

// Reset styles
if (bold) {
boldOff();
}
if (underline) {
underlineOff();
}
} catch (Exception err) {
Log.d("SunmiPrinter", err.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class FlutterSunmiPrinterPlugin implements FlutterPlugin, MethodCallHandl
private String UNDERLINE_OFF = "underlineOff";
private String EMPTY_LINES = "emptyLines";
private String PRINT_TEXT = "printText";
private String PRINT_ROW = "printRow";

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
Expand Down Expand Up @@ -75,6 +76,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
int n = call.argument("n");
flutterSunmiPrinterModule.emptyLines(n);
result.success(null);
} else if (call.method.equals(PRINT_ROW)) {
String cols = call.argument("cols");
boolean bold = call.argument("bold");
boolean underline = call.argument("underline");
int textSize = call.argument("textSize");
int linesAfter = call.argument("linesAfter");
flutterSunmiPrinterModule.row(cols, bold, underline, textSize, linesAfter);
result.success(null);
} else {
result.notImplemented();
}
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_sunmi_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
library flutter_sunmi_printer;

export './src/enums.dart';
export './src/sunmi_col.dart';
export './src/sunmi_printer.dart';
export './src/sunmi_styles.dart';
65 changes: 48 additions & 17 deletions lib/src/sunmi_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
*/

import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_sunmi_printer/src/enums.dart';
import 'sunmi_col.dart';
import 'sunmi_styles.dart';

class SunmiPrinter {
static const String RESET = "reset";
static const String START_PRINT = "startPrint";
static const String STOP_PRINT = "stopPrint";
static const String IS_PRINTING = "isPrinting";
static const String BOLD_ON = "boldOn";
static const String BOLD_OFF = "boldOff";
static const String UNDERLINE_ON = "underlineOn";
static const String UNDERLINE_OFF = "underlineOff";
// static const String START_PRINT = "startPrint";
// static const String STOP_PRINT = "stopPrint";
// static const String IS_PRINTING = "isPrinting";
// static const String BOLD_ON = "boldOn";
// static const String BOLD_OFF = "boldOff";
// static const String UNDERLINE_ON = "underlineOn";
// static const String UNDERLINE_OFF = "underlineOff";
static const String EMPTY_LINES = "emptyLines";
static const String PRINT_TEXT = "printText";
static const String PRINT_ROW = "printRow";

static const MethodChannel _channel =
const MethodChannel('flutter_sunmi_printer');
Expand All @@ -29,25 +33,24 @@ class SunmiPrinter {
await _channel.invokeMethod(RESET);
}

static Future<void> startPrint() async {
await _channel.invokeMethod(START_PRINT);
}
// static Future<void> startPrint() async {
// await _channel.invokeMethod(START_PRINT);
// }

static Future<void> stopPrint() async {
await _channel.invokeMethod(STOP_PRINT);
}
// static Future<void> stopPrint() async {
// await _channel.invokeMethod(STOP_PRINT);
// }

static Future<void> isPrinting() async {
await _channel.invokeMethod(IS_PRINTING);
}
// static Future<void> isPrinting() async {
// await _channel.invokeMethod(IS_PRINTING);
// }

/// Print [text] with [styles] and skip [linesAfter] after
static Future<void> text(
String text, {
SunmiStyles styles = const SunmiStyles(),
int linesAfter = 0,
}) async {
// Text
await _channel.invokeMethod(PRINT_TEXT, {
"text": text,
"bold": styles.bold,
Expand All @@ -73,4 +76,32 @@ class SunmiPrinter {
}) async {
await text(List.filled(len, ch[0]).join(), linesAfter: linesAfter);
}

/// Print a row.
///
/// A row contains up to 12 columns. A column has a width between 1 and 12.
/// Total width of columns in one row must be equal to 12.
static Future<void> row({
List<SunmiCol> cols,
bool bold: false,
bool underline: false,
SunmiSize textSize: SunmiSize.md,
int linesAfter: 0,
}) async {
final isSumValid = cols.fold(0, (int sum, col) => sum + col.width) == 12;
if (!isSumValid) {
throw Exception('Total columns width must be equal to 12');
}

final colsJson = List<Map<String, String>>.from(
cols.map<Map<String, String>>((SunmiCol col) => col.toJson()));

await _channel.invokeMethod(PRINT_ROW, {
"cols": json.encode(colsJson),
"bold": bold,
"underline": underline,
"textSize": textSize.value,
"linesAfter": linesAfter,
});
}
}

0 comments on commit e246f0c

Please sign in to comment.