feat: view type stored in view/cubit, itself, cleanup coe

This commit is contained in:
Anton Stubenbord
2023-02-10 12:41:30 +01:00
parent e65e152d44
commit f04edece16
72 changed files with 307 additions and 568 deletions

View File

@@ -0,0 +1,44 @@
import 'dart:io';
import 'dart:isolate';
import 'package:image/image.dart' as im;
typedef ImageOperationCallback = im.Image Function(im.Image);
class DecodeParam {
final File file;
final SendPort sendPort;
final im.Image Function(im.Image) imageOperation;
DecodeParam(this.file, this.sendPort, this.imageOperation);
}
void decodeIsolate(DecodeParam param) {
// Read an image from file (webp in this case).
// decodeImage will identify the format of the image and use the appropriate
// decoder.
var image = im.decodeImage(param.file.readAsBytesSync())!;
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
var processed = param.imageOperation(image);
param.sendPort.send(processed);
}
// Decode and process an image file in a separate thread (isolate) to avoid
// stalling the main UI thread.
Future<File> processImage(
File file,
ImageOperationCallback imageOperation,
) async {
var receivePort = ReceivePort();
await Isolate.spawn(
decodeIsolate,
DecodeParam(
file,
receivePort.sendPort,
imageOperation,
));
var image = await receivePort.first as im.Image;
return file.writeAsBytes(im.encodePng(image));
}