package by.vshkl.pxlsrt.mvp.presenter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.arellomobile.mvp.InjectViewState;
import com.arellomobile.mvp.MvpPresenter;

import java.io.FileOutputStream;
import java.io.IOException;

import by.vshkl.pxlsrt.mvp.view.CameraView;
import io.reactivex.disposables.Disposable;

@InjectViewState
public class CameraPresenter extends MvpPresenter<CameraView> {

    private static final String TAG = CameraPresenter.class.getSimpleName();
//    private static final int IMAGE_SIZE_PX = 1024;
    public static final int IMAGE_SIZE_PX = 1000;

    private Disposable disposable;

    public void onStop() {
        if (disposable != null) {
            disposable.dispose();
        }
    }

    public void toggleGrid() {
        getViewState().toggleGrid();
    }

    public void toggleCamera() {
        getViewState().toggleCamera();
    }

    public void toggleFlash() {
        getViewState().toggleFlash();
    }

    public void takePicture() {
        getViewState().takePicture();
    }

    //TODO: move this stuff to separate thread via RxJava2
    public void processPicture(FileOutputStream fos, String filename, byte[] data) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getWidth());
        bitmap = Bitmap.createScaledBitmap(bitmap, IMAGE_SIZE_PX, IMAGE_SIZE_PX, false);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bitmap.recycle();
        getViewState().sendPictureData(filename);
    }
}

@@ -37,80 +37,8 @@
 r;%0A%0A
-import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0A%0A
 impo
@@ -158,36 +158,55 @@
 am;%0A
+%0A
 import 
-java.io.IOException;%0A
+by.vshkl.pxlsrt.core.utils.BitmapUtils;
 %0Aimp
@@ -270,244 +270,255 @@
 vex.
-disposables.Disposable;%0A%0A@InjectViewState%0Apublic class CameraPresenter extends MvpPresenter%3CCameraView%3E %7B%0A%0A    private static final String TAG = CameraPresenter.class.getSimpleName();%0A//    private static final int IMAGE_SIZE_PX = 1024;
+android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.disposables.Disposable;%0Aimport io.reactivex.functions.Consumer;%0Aimport io.reactivex.schedulers.Schedulers;%0A%0A@InjectViewState%0Apublic class CameraPresenter extends MvpPresenter%3CCameraView%3E %7B%0A
 %0A   
@@ -1025,67 +1025,8 @@
  %7D%0A%0A
-    //TODO: move this stuff to separate thread via RxJava2%0A
     
@@ -1082,19 +1082,16 @@
 String f
-ile
 name, by
@@ -1115,166 +1115,120 @@
     
-Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);%0A        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getWidth
+disposable = BitmapUtils.saveTempBitmap(fos, fname, data)%0A                .subscribeOn(Schedulers.computation
 ())
-;
 %0A   
@@ -1236,150 +1236,110 @@
     
-bitmap = Bitmap.createScaledBitmap(bitmap, IMAGE_SIZE_PX, IMAGE_SIZE_PX, false);%0A        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
+        .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Consumer%3CString%3E() %7B
 %0A   
@@ -1347,14 +1347,8 @@
     
-try %7B%0A
     
@@ -1359,87 +1359,90 @@
     
-fos.close();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();
+@Override%0A                    public void accept(String string) throws Exception %7B
 %0A   
@@ -1450,18 +1450,16 @@
     
-%7D%0A
         
 bitm
@@ -1458,26 +1458,8 @@
     
-bitmap.recycle();%0A
     
@@ -1497,16 +1497,56 @@
 ata(
-filename
+string);%0A                    %7D%0A                %7D
 );%0A 

Update CameraPresenter. Update processPicture method